Skip to content

Commit

Permalink
Cleanup gcpy/ste_flux.py
Browse files Browse the repository at this point in the history
gcpy/ste_flux.py
- Implement suggestions from pylint
- Convert string formats to f-strings
- Now use util.make_directory

gcpy/oh_metrics.py
- Trimmed whitespace

CHANGELOG.md
- Updated accordingly

Signed-off-by: Bob Yantosca <[email protected]>
  • Loading branch information
yantosca committed Feb 9, 2023
1 parent c2d3598 commit db21c40
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- New functions in `benchmark.py` and `util.py` to facilitate printing of the species/emissions/inventories that differ between Dev & Ref versions.

### Changed
- Applied cleanup susggestions from pylint to `benchmark.py`, `util.py`, `plot.py`
- Replaced format with f-strings in `benchmark.py`, `util.py`
- Applied cleanup susggestions from pylint to `benchmark.py`, `util.py`, `plot.py`, `oh_metrics.py`, `ste_flux.py`
- Replaced format with f-strings in `benchmark.py`, `util.py`, `plot.py`, `oh_metrics.py`, `ste_flux.py`
- Abstract some common in `benchmark.py` into functions
- Replaced direct calls to `yaml.load` with `util.read_config.file` (mostly using `quiet=True`)

Expand Down
2 changes: 1 addition & 1 deletion gcpy/oh_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def print_metrics(common_vars, dst):
file=f
)
print("#" * 79, file=f)

# ==============================================================
# Mean OH concentration [1e5 molec/cm3]
# ==============================================================
Expand Down
24 changes: 9 additions & 15 deletions gcpy/ste_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import os
from calendar import monthrange, month_abbr
import warnings
import gc
import numpy as np
import pandas as pd
import xarray as xr
import gcpy.constants as physconsts
import gc
from gcpy.util import make_directory

# Suppress harmless run-time warnings (mostly about underflow in division)
warnings.filterwarnings("ignore", category=RuntimeWarning)
Expand Down Expand Up @@ -96,18 +97,18 @@ def __init__(self, devstr, files, dst, year,
combine="nested",
concat_dim="time"
)
except FileNotFoundError:
except FileNotFoundError as exc:
msg = f"Could not find one or more files in {files}"
raise FileNotFoundError(msg)
raise FileNotFoundError(msg) from exc
else:
try:
self.ds_flx = xr.open_mfdataset(
files,
drop_variables=physconsts.skip_these_vars,
)
except FileNotFoundError:
except FileNotFoundError as exc:
msg = f"Could not find one or more files in {files}"
raise FileNotFoundError(msg)
raise FileNotFoundError(msg) from exc

# Set a flag to denote if this data is from GCHP
self.is_gchp = "nf" in self.ds_flx.dims.keys()
Expand Down Expand Up @@ -135,8 +136,7 @@ def __init__(self, devstr, files, dst, year,
# Month names
self.mon_name = []
for t in range(self.N_MONTHS):
self.mon_name.append("{} {}".format(
month_abbr[t + 1], self.y0_str))
self.mon_name.append(f"{ month_abbr[t + 1]} {self.y0_str}")
self.mon_name.append("Annual Mean")

# Days in the benchmark year
Expand All @@ -154,8 +154,7 @@ def __init__(self, devstr, files, dst, year,
self.d_per_mon = [monthrange(self.y0, self.month)[1] * 1.0]

# Month name
self.mon_name = ["{} {}".format(month_abbr[self.month],
self.y0_str)]
self.mon_name = [f"{month_abbr[self.month]} {self.y0_str}"]

# Days in benchmark year
self.d_per_yr = 0.0
Expand Down Expand Up @@ -248,12 +247,7 @@ def print_ste(globvars, df):
Strat-trop exchange table
"""
# Create plot directory hierarchy if necessary
if os.path.isdir(globvars.dst) and not globvars.overwrite:
err_str = "Pass overwrite=True to overwrite files in that directory"
print(f"Directory {globvars.dst} exists. {err_str}")
return
elif not os.path.isdir(globvars.dst):
os.makedirs(globvars.dst)
make_directory(globvars.dst, globvars.overwrite)

# Save the file in the Tables folder of dst
filename = f"{globvars.dst}/Strat_trop_exchange.txt"
Expand Down

0 comments on commit db21c40

Please sign in to comment.