Skip to content

Commit

Permalink
testing AdfData. Fixes for history file naming. Fix to force loading …
Browse files Browse the repository at this point in the history
…data for derived time series.
  • Loading branch information
brianpm committed Jun 12, 2024
1 parent 935b6f4 commit cae606f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
11 changes: 7 additions & 4 deletions lib/adf_diag.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,13 @@ def call_ncrcat(cmd):
# Aerosol Calcs
#--------------
#Always make sure PMID is made if aerosols are desired in config file
# Since there's no requirement for `aerosol_zonal_list` to be included, allow it to be absent:
azl = res.get("aerosol_zonal_list", [])
if "PMID" not in diag_var_list:
if any(item in res["aerosol_zonal_list"] for item in diag_var_list):
if any(item in azl for item in diag_var_list):
diag_var_list += ["PMID"]
if "T" not in diag_var_list:
if any(item in res["aerosol_zonal_list"] for item in diag_var_list):
if any(item in azl for item in diag_var_list):
diag_var_list += ["T"]
#End aerosol calcs

Expand Down Expand Up @@ -1059,7 +1061,7 @@ def derive_variables(self, res=None, vars_to_derive=None, ts_dir=None, overwrite
print(ermsg)
else:
#Open a new dataset with all the constituent files/variables
ds = xr.open_mfdataset(constit_files)
ds = xr.open_mfdataset(constit_files).compute()

# create new file name for derived variable
derived_file = constit_files[0].replace(constit_list[0], var)
Expand Down Expand Up @@ -1091,7 +1093,8 @@ def derive_variables(self, res=None, vars_to_derive=None, ts_dir=None, overwrite
#These will be multiplied by rho (density of dry air)
ds_pmid_done = False
ds_t_done = False
if var in res["aerosol_zonal_list"]:
azl = res.get("aerosol_zonal_list", []) # User-defined defaults might not include aerosol zonal list
if var in azl:

#Only calculate once for all aerosol vars
if not ds_pmid_done:
Expand Down
24 changes: 21 additions & 3 deletions lib/adf_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,20 +673,38 @@ def get_climo_yrs_from_ts(self, input_ts_loc, case_name):

#Search for first variable in var_list to get a time series file to read
#NOTE: it is assumed all the variables have the same dates!
ts_files = sorted(input_location.glob(f"{case_name}*.{var_list[0]}.*nc"))

#Read hist_str (component.hist_num) from the yaml file, or set to default
hist_str = self.get_basic_info('hist_str')
#If hist_str is not present, then default to 'cam.h0':
if not hist_str:
hist_str = 'cam.h0'
#End if

ts_files = sorted(input_location.glob(f"{case_name}.{hist_str}.{var_list[0]}.*nc"))

#Read in file(s)
if len(ts_files) == 1:
cam_ts_data = xr.open_dataset(ts_files[0], decode_times=True)
else:
cam_ts_data = xr.open_mfdataset(ts_files, decode_times=True, combine='by_coords')
try:
cam_ts_data = xr.open_mfdataset(ts_files, decode_times=True, combine='by_coords')
except:
print(" ----------- ERROR ------------")
print(ts_files)

#Average time dimension over time bounds, if bounds exist:
if 'time_bnds' in cam_ts_data:
timeBoundsName = 'time_bnds'
elif 'time_bounds' in cam_ts_data:
timeBoundsName = 'time_bounds'
else:
timeBoundsName = None
if timeBoundsName:
time = cam_ts_data['time']
#NOTE: force `load` here b/c if dask & time is cftime,
#throws a NotImplementedError:
time = xr.DataArray(cam_ts_data['time_bnds'].load().mean(dim='nbnd').values, dims=time.dims, attrs=time.attrs)
time = xr.DataArray(cam_ts_data[timeBoundsName].load().mean(dim='nbnd').values, dims=time.dims, attrs=time.attrs)
cam_ts_data['time'] = time
cam_ts_data.assign_coords(time=time)
cam_ts_data = xr.decode_cf(cam_ts_data)
Expand Down

0 comments on commit cae606f

Please sign in to comment.