Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lee1043 committed Jan 13, 2025
1 parent 7838e00 commit eca7d89
Show file tree
Hide file tree
Showing 3 changed files with 406 additions and 130 deletions.
23 changes: 6 additions & 17 deletions pcmdi_metrics/mean_climate/lib/calculate_climatology.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def calculate_climatology(
overwrite_output: bool = True,
save_ac_netcdf: bool = True,
plot=True,

):
"""
Calculate climatology from a dataset over a specified period.
Expand Down Expand Up @@ -106,7 +105,7 @@ def calculate_climatology(
ds = xcdat_open(infile, data_var=var)
print("type(d):", type(ds))

atts = d.attrs
atts = ds.attrs
print("atts:", atts) # Print dataset attributes

# Check if dataset time axis is okay
Expand Down Expand Up @@ -276,29 +275,18 @@ def calculate_climatology(
# Add annual or seasonal climatology to the dictionary
ds_clim_dict[s] = ds_clim_s

if outfilename is not None:
out = os.path.join(outdir, outfilename)

out_season = out.replace(".nc", addf)

d_clim_dict[s].to_netcdf(
out_season
) # global attributes are automatically saved as well

print("output file:", out_season)

# Plot climatology
if plot and s == "AC":
# Check if variable is 4D
if is_4d_variable(d_ac, var):
if is_4d_variable(ds_clim_s, var):
# Plot 3 levels (hPa) for 4D variables for quick check
levels_to_plot = [200, 500, 850]
else:
levels_to_plot = [None]

# Plot climatology for each level
for level in levels_to_plot:
output_fig_path = out_season.replace(".nc", ".png")
output_fig_path = outpath_season.replace(".nc", ".png")
if level is not None:
if var in output_fig_path:
output_fig_path = os.path.join(
Expand All @@ -314,19 +302,20 @@ def calculate_climatology(

# plot climatology for each level
plot_climatology(
d_ac,
ds_clim_s,
var,
level=level,
season_to_plot="all",
output_filename=output_fig_path,
period=f"{start_yr_str}-{end_yr_str}",
period=f"{start_yr:04d}-{end_yr:04d}",
)

print("output figure:", output_fig_path)

ds.close()
return ds_clim_dict # Return the dictionary of all climatology datasets


def is_4d_variable(ds, data_var):
da = ds[data_var]
print("data_var, da.shape:", data_var, da.shape)
Expand Down
1 change: 1 addition & 0 deletions pcmdi_metrics/mean_climate/lib_unified/lib_unified.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ def process_dataset(
interp_filename_nc = interp_filename_head.replace(
".nc", f"_interp_{grid_resolution}.nc"
)
os.makedirs(os.path.join(out_path_interp, version), exist_ok=True)
ds_ac_level_interp.to_netcdf(
os.path.join(out_path_interp, version, interp_filename_nc)
)
Expand Down
Loading

0 comments on commit eca7d89

Please sign in to comment.