Skip to content

Commit

Permalink
Now print small/large numbers in exp notation in benchmark tables
Browse files Browse the repository at this point in the history
gcpy/util.py
- Added routine "format_number_for_table", which returns a format
  string to be used in routine "print_totals".  This will convert
  small and large numbers to scientific notation in benchmark
  mass and emissions tables.
- Routine print_totals now calls "format_number_for_table".
- Now truncate species & diagnostics names in benchmark mass tables
  and emissions tables to 20 characters to improve readability

CHANGELOG.md
- Updated accordingly

Signed-off-by: Bob Yantosca <[email protected]>
  • Loading branch information
yantosca committed Jul 20, 2023
1 parent c3746ec commit 3ede6e3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Added `benchmark/modules/GC_72_vertical_levels.csv` file
- Added `multi_index_lat` keyword to `reshape_MAPL_CS` function in `gcpy/util.py`
- Added FURA to `emission_species.yml` and `benchmark_categories.yml`
- Added new routine `format_number_for_table` in `util.py`

### Changed
- Simplified the Github issues templates into two options: `new-feature-or-discussion.md` and `question-issue.md`
Expand All @@ -31,6 +32,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Renamed TransportTracers species in `benchmark_categories.yml` and in documentation
- YAML files in `benchmark/` have been moved to `benchmark/config`
- Models vs. O3 obs plots are now arranged by site latitude from north to south
- Routine `print_totals` now prints small and/or large numbers in scientific notation
- Truncate names in benchmark & emissions tables to improve readability

### Fixed
- Generalized test for GCHP or GCClassic restart file in `regrid_restart_file.py`
Expand Down
64 changes: 63 additions & 1 deletion gcpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,51 @@ def create_display_name(
return display_name


def format_number_for_table(
number,
max_thresh=1.0e8,
min_thresh=1.0e-6,
f_fmt="18.6f",
e_fmt="18.8e"
):
"""
Returns a format string for use in the "print_totals" routine.
If the number is greater than a maximum threshold or smaller
than a minimum threshold, then use scientific notation format.
Otherwise use floating-piont format.
Special case: do not convert 0.0 to exponential notation.
Args:
-----
number : float
Number to be printed
max_thresh, min_thresh: float
If |number| > max_thresh, use scientific notation.
If |number| < min_thresh, use scientific notation
f_fmt, e_fmt : str
The default floating point string and default scientific
notation string.
Default values: 18.6f, 18.6e
Returns:
--------
fmt_str : str
Formatted string that can be inserted into the print
statement in print_totals.
"""
abs_number = np.abs(number)

if not (abs_number > 1e-60):
return f"{number:{f_fmt}}"

if abs_number > max_thresh or abs_number < min_thresh:
return f"{number:{e_fmt}}"
return f"{number:{f_fmt}}"


def print_totals(
ref,
dev,
Expand Down Expand Up @@ -288,7 +333,24 @@ def print_totals(
# ==================================================================
# Write output to file and return
# ==================================================================
print(f"{display_name.ljust(19)}: {total_ref:18.6f} {total_dev:18.6f} {diff:12.6f} {pctdiff:8.3f} {diff_str}", file=f)
ref_fmt = format_number_for_table(total_ref)
dev_fmt = format_number_for_table(total_dev)
diff_fmt = format_number_for_table(
diff,
max_thresh=1.0e4,
min_thresh=1.0e-4,
f_fmt="12.3f",
e_fmt="12.4e"
)
pctdiff_fmt = format_number_for_table(
pctdiff,
max_thresh=1.0e3,
min_thresh=1.0e-3,
f_fmt="8.3f",
e_fmt="8.1e"
)

print(f"{display_name[0:19].ljust(19)}: {ref_fmt} {dev_fmt} {diff_fmt} {pctdiff_fmt} {diff_str}", file=f)

return diff_list

Expand Down

0 comments on commit 3ede6e3

Please sign in to comment.