Skip to content

Commit

Permalink
Merge pull request #928 from PCMDI/lee1043-patch-1
Browse files Browse the repository at this point in the history
Simplify codes
  • Loading branch information
lee1043 committed Apr 28, 2023
2 parents 48b777e + 54e7375 commit 293daee
Show file tree
Hide file tree
Showing 23 changed files with 25 additions and 51 deletions.
3 changes: 1 addition & 2 deletions pcmdi_metrics/diurnal/scripts/compositeDiurnalStatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ def compute(params):
str(args.lastyear),
)
LSToutfile = "%s_%s_LocalSolarTimes.nc" % (varbname, dataname)
if not os.path.exists(args.results_dir):
os.makedirs(args.results_dir)
os.makedirs(args.results_dir, exist_ok=True)
f = cdms2.open(os.path.join(args.results_dir, avgoutfile), "w")
g = cdms2.open(os.path.join(args.results_dir, stdoutfile), "w")
h = cdms2.open(os.path.join(args.results_dir, LSToutfile), "w")
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/diurnal/scripts/computeStdOfDailyMeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ def compute(params):
)
except Exception as err:
print("Failed for model: %s with error: %s" % (dataname, err))
if not os.path.exists(args.results_dir):
os.makedirs(args.results_dir)
os.makedirs(args.results_dir, exist_ok=True)
g = cdms2.open(os.path.join(args.results_dir, stdoutfile), "w")
g.write(stdvalues)
g.close()
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/diurnal/scripts/fourierDiurnalGridpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def main():
ascFile.month = monthname
ascname = os.path.join(os.path.abspath(args.results_dir), ascFile())

if not os.path.exists(os.path.dirname(ascname)):
os.makedirs(os.path.dirname(ascname))
os.makedirs(os.path.dirname(ascname), exist_ok=True)
fasc = open(ascname, "w")

gridptlats = [float(x) for x in args.lats]
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/diurnal/scripts/savg_fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ def spacevavg(tvarb1, tvarb2, sftlf, model):
return outD

print("Preparing to write output to JSON file ...")
if not os.path.exists(args.results_dir):
os.makedirs(args.results_dir)
os.makedirs(args.results_dir, exist_ok=True)
jsonFile = populateStringConstructor(args.outnamejson, args)
jsonFile.month = monthname

Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/diurnal/scripts/std_of_dailymeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ def compute(param):
print("TEMPLATE NAME:", template())

print("Preparing to write output to JSON file ...")
if not os.path.exists(args.results_dir):
os.makedirs(args.results_dir)
os.makedirs(args.results_dir, exist_ok=True)
jsonFile = populateStringConstructor(args.outnamejson, args)
jsonFile.month = monthname

Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/diurnal/scripts/std_of_hourlyvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ def compute(param):
# lonrange = (285.0, 295.0)

print("Preparing to write output to JSON file ...")
if not os.path.exists(args.results_dir):
os.makedirs(args.results_dir)
os.makedirs(args.results_dir, exist_ok=True)
jsonFile = populateStringConstructor(args.outnamejson, args)
jsonFile.month = monthname

Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/diurnal/scripts/std_of_meandiurnalcycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ def compute(param):
# lonrange = (285.0, 295.0)

print("Preparing to write output to JSON file ...")
if not os.path.exists(args.results_dir):
os.makedirs(args.results_dir)
os.makedirs(args.results_dir, exist_ok=True)
jsonFile = populateStringConstructor(args.outnamejson, args)
jsonFile.month = monthname

Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/enso/enso_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@

# Create output directory
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
os.makedirs(outdir(output_type=output_type))
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print("output directory for " + output_type + ":" + outdir(output_type=output_type))

# list of variables
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/enso/scripts_pcmdi/enso_driver_obsOnly.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@

# Create output directory
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
os.makedirs(outdir(output_type=output_type))
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print(outdir(output_type=output_type))

# list of variables
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/enso/scripts_pcmdi/parallel_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@
# Create output directories
# -------------------------------------------------
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
os.makedirs(outdir(output_type=output_type))
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print(outdir(output_type=output_type))

# =================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,7 @@ def runClim(A):
print(
"Your Python does not have CMOR, using regular cdms to write out files"
)
if not os.path.exists(A.results_dir):
os.makedirs(A.results_dir)
os.makedirs(A.results_dir, exist_ok=True)
end_tc = tc[-1].add(1, cdtime.Month)

# Populate fout template with values
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/mjo/lib/debug_chk_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

def debug_chk_plot(d_seg_x_ano, Power, OEE, segment_year, daSeaCyc, segment_ano_year):

if not os.path.exists("debug"):
os.makedirs("debug")
os.makedirs("debug", exist_ok=True)

""" FIX ME ---
x = vcs.init()
Expand Down
6 changes: 2 additions & 4 deletions pcmdi_metrics/mjo/lib/mjo_metric_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,13 @@ def mjo_metric_ewr_calculation(

# NetCDF output
if nc_out:
if not os.path.exists(outdir(output_type="diagnostic_results")):
os.makedirs(outdir(output_type="diagnostic_results"))
os.makedirs(outdir(output_type="diagnostic_results"), exist_ok=True)
fout = os.path.join(outdir(output_type="diagnostic_results"), output_filename)
write_netcdf_output(OEE, fout)

# Plot
if plot:
if not os.path.exists(outdir(output_type="graphics")):
os.makedirs(outdir(output_type="graphics"))
os.makedirs(outdir(output_type="graphics"), exist_ok=True)
fout = os.path.join(outdir(output_type="graphics"), output_filename)
title = (
mip.upper()
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/mjo/lib/post_process_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def main():
imgdir = "/work/lee1043/imsi/result_test/mjo_metrics/plot_test"
# imgdir = '/p/user_pub/pmp/pmp_results/pmp_v1.1.2/graphics/mjo/'+mip+'/historical/'+version

if not os.path.exists(imgdir):
os.makedirs(imgdir)
os.makedirs(imgdir, exist_ok=True)

ncfile_list = glob.glob(os.path.join(datadir, "*.nc"))

Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/mjo/mjo_metrics_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@

# Create output directory
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
os.makedirs(outdir(output_type=output_type))
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print(outdir(output_type=output_type))

# Generate CMEC compliant json
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/mjo/scripts/parallel_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@
# Create output directories
# -------------------------------------------------
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
os.makedirs(outdir(output_type=output_type))
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print(outdir(output_type=output_type))

# =================================================
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/monsoon_sperber/driver_monsoon_sperber.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ def tree():

# Create output directory
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
os.makedirs(outdir(output_type=output_type))
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print(outdir(output_type=output_type))

# Debug
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/precip_variability/scripts_pcmdi/calc_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
/ psdmfm_ref["RESULTS"][dat_ref][frc][dom][frq]
)

if not (os.path.isdir(outdir)):
os.makedirs(outdir)
os.makedirs(outdir, exist_ok=True)
outfile = open(os.path.join(outdir, model.split("/")[-1]), "w")
json.dump(psdmfm, outfile, sort_keys=True, indent=4, separators=(",", ": "))
outfile.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@
str(outdir_template(output_type="%(output_type)", mip=mip, case_id=case_id))
)
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
try:
os.makedirs(outdir(output_type=output_type))
except FileExistsError:
pass
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print(outdir(output_type=output_type))

# Check data in advance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@
# Create output directories
# -------------------------------------------------
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
os.makedirs(outdir(output_type=output_type))
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print(outdir(output_type=output_type))

# =================================================
Expand Down
3 changes: 1 addition & 2 deletions pcmdi_metrics/variability_mode/variability_modes_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@
# Create output directories
# -------------------------------------------------
for output_type in ["graphics", "diagnostic_results", "metrics_results"]:
if not os.path.exists(outdir(output_type=output_type)):
os.makedirs(outdir(output_type=output_type))
os.makedirs(outdir(output_type=output_type), exist_ok=True)
print(outdir(output_type=output_type))

# =================================================
Expand Down
3 changes: 1 addition & 2 deletions tests/deprecated/basepmpgraphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def setUp(self):
# self.x.open()
self.orig_cwd = os.getcwd()
self.pngsdir = "tests_png"
if not os.path.exists(self.pngsdir):
os.makedirs(self.pngsdir)
os.makedirs(self.pngsdir, exist_ok=True)
self.basedir = os.path.join(os.path.dirname(__file__), "graphics")


Expand Down
4 changes: 2 additions & 2 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_conda_env(tmpdir):

pmp_share_path = os.path.join(conda_prefix, "share", "pmp")

os.makedirs(pmp_share_path)
os.makedirs(pmp_share_path, exist_ok=True)

with mock.patch.dict(os.environ, {"CONDA_PREFIX": conda_prefix}):
path = resources.resource_path()
Expand All @@ -27,7 +27,7 @@ def test_conda_env_no_exist(resource_filename, getcwd, tmpdir):

getcwd_path = os.path.join(tmpdir, "share", "pmp")

os.makedirs(getcwd_path)
os.makedirs(getcwd_path, exist_ok=True)

getcwd.return_value = tmpdir

Expand Down

0 comments on commit 293daee

Please sign in to comment.