-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AdfData class for streamlined I/O in plotting scripts #269
Changes from 6 commits
b28b6ac
796a8c0
18706b3
efdfba5
6456fdc
aababb2
988011b
91bce2a
bf02a1f
71578ef
06d3ea1
11f1477
c7286b7
97e1cbd
9068ec9
ceecec9
b48d01a
dd015ef
84150a4
42614fb
b7765ab
857a441
577c9e7
87aebf2
c751d02
f9f296d
9ec949b
3adbfe0
51394a6
cf2128c
3be03db
536d2f2
6b9f671
7d2c946
5d46906
ce50839
142d4e5
935b6f4
cae606f
0183799
26a059c
7d6cdc9
df14b1b
b966637
39da27e
2d5a7dc
de213fc
231aaaa
6e20dae
496d5c7
e36585f
febddf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is OK for now, but do you remember what the specific error was that prompted this try/except? In general we want to try and check for a specific type of exception, otherwise it could catch potential errors that we actually want to be exposed (for example if the computer itself has a system/hardware error). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, this is a remnant of me trying to do testing. I'm taking it out. I was hitting a problem where multiple time series files had been generated for case (and put in the same directory). It resulted in having multiple files identified in the search that couldn't be concatenated because they had overlapping times. Maybe that needs to be revisited sometime, but I don't think this check needs to be included here. |
||
|
||
#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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ | |
import warnings # use to warn user about missing files. | ||
|
||
import plotting_functions as pf | ||
from adf_dataset import AdfData | ||
|
||
#Format warning messages: | ||
def my_formatwarning(msg, *args, **kwargs): | ||
|
@@ -83,7 +82,7 @@ def global_latlon_map_B(adfobj): | |
# | ||
# Use ADF api to get all necessary information | ||
# | ||
data = AdfData(adfobj) | ||
# data = AdfData(adfobj) NO LONGER NEEDED | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming this script works as expected I would just delete this line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
var_list = adfobj.diag_var_list | ||
#Special ADF variable which contains the output paths for | ||
#all generated plots and tables for each case: | ||
|
@@ -128,7 +127,7 @@ def global_latlon_map_B(adfobj): | |
|
||
# probably want to do this one variable at a time: | ||
for var in var_list: | ||
if var not in data.ref_var_nam: | ||
if var not in adfobj.data.ref_var_nam: | ||
dmsg = f"No reference data found for variable `{var}`, zonal mean plotting skipped." | ||
adfobj.debug_log(dmsg) | ||
continue | ||
|
@@ -156,7 +155,7 @@ def global_latlon_map_B(adfobj): | |
vres['central_longitude'] = pf.get_central_longitude(adfobj) | ||
|
||
# load reference data (observational or baseline) | ||
odata = data.load_reference_da(var) | ||
odata = adfobj.data.load_reference_da(var) | ||
if odata is None: | ||
continue | ||
has_dims = pf.lat_lon_validate_dims(odata) # T iff dims are (lat,lon) -- can't plot unless we have both | ||
|
@@ -165,10 +164,10 @@ def global_latlon_map_B(adfobj): | |
continue | ||
|
||
#Loop over model cases: | ||
for case_idx, case_name in enumerate(data.case_names): | ||
for case_idx, case_name in enumerate(adfobj.data.case_names): | ||
|
||
#Set case nickname: | ||
case_nickname = data.test_nicknames[case_idx] | ||
case_nickname = adfobj.data.test_nicknames[case_idx] | ||
|
||
#Set output plot location: | ||
plot_loc = Path(plot_locations[case_idx]) | ||
|
@@ -179,7 +178,7 @@ def global_latlon_map_B(adfobj): | |
plot_loc.mkdir(parents=True) | ||
|
||
#Load re-gridded model files: | ||
mdata = data.load_regrid_da(case_name, var) | ||
mdata = adfobj.data.load_regrid_da(case_name, var) | ||
|
||
#Skip this variable/case if the regridded climo file doesn't exist: | ||
if mdata is None: | ||
|
@@ -239,11 +238,11 @@ def global_latlon_map_B(adfobj): | |
# difference: each entry should be (lat, lon) | ||
dseasons[s] = mseasons[s] - oseasons[s] | ||
|
||
pf.plot_map_and_save(plot_name, case_nickname, data.ref_nickname, | ||
pf.plot_map_and_save(plot_name, case_nickname, adfobj.data.ref_nickname, | ||
[syear_cases[case_idx],eyear_cases[case_idx]], | ||
[syear_baseline,eyear_baseline], | ||
mseasons[s], oseasons[s], dseasons[s], | ||
obs=data.reference_is_obs, **vres) | ||
obs=adfobj.compare_obs, **vres) | ||
|
||
#Add plot to website (if enabled): | ||
adfobj.add_website_data(plot_name, var, case_name, category=web_category, | ||
|
@@ -283,7 +282,7 @@ def global_latlon_map_B(adfobj): | |
[syear_cases[case_idx],eyear_cases[case_idx]], | ||
[syear_baseline,eyear_baseline], | ||
mseasons[s].sel(lev=pres), oseasons[s].sel(lev=pres), dseasons[s].sel(lev=pres), | ||
obs=data.reference_is_obs, **vres) | ||
obs=adfobj.compare_obs, **vres) | ||
|
||
#Add plot to website (if enabled): | ||
adfobj.add_website_data(plot_name, f"{var}_{pres}hpa", case_name, category=web_category, | ||
|
@@ -343,14 +342,14 @@ def plot_file_op(adfobj, plot_name, var, case_name, season, web_category, redo_p | |
if plot_name.is_file(): | ||
if redo_plot: | ||
plot_name.unlink() | ||
return 1 | ||
return True | ||
else: | ||
#Add already-existing plot to website (if enabled): | ||
adfobj.add_website_data(plot_name, var, case_name, category=web_category, | ||
season=season, plot_type=plot_type) | ||
return None # None tells caller that file exists and not to overwrite | ||
return False # False tells caller that file exists and not to overwrite | ||
else: | ||
return 1 | ||
return True | ||
|
||
############## | ||
#END OF SCRIPT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think (?) this should be
warn
notwarning
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. Fixed it.