Skip to content

Commit

Permalink
Fix github suggestions
Browse files Browse the repository at this point in the history
Add check for value converters for data arrays
  • Loading branch information
justin-richling committed Aug 6, 2024
1 parent e64d304 commit 3085b79
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/adf_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def load_timeseries_dataset(self, fils):
# Test case(s)
def load_climo_da(self, case, variablename):
"""Return DataArray from climo file"""
add_offset, scale_factor = self.get_defaults(case, variablename)
add_offset, scale_factor = self.get_value_converters(case, variablename)
fils = self.get_climo_file(case, variablename)
return self.load_da(fils, variablename, add_offset=add_offset, scale_factor=scale_factor)

Expand All @@ -161,7 +161,7 @@ def load_climo_file(self, case, variablename):
"""Return Dataset for climo of variablename"""
fils = self.get_climo_file(case, variablename)
if not fils:
warnings.warn(f"ERROR: Did not find climo file for variable: {variablename}. Will try to skip.")
warnings.warn(f"WARNING: Did not find climo file for variable: {variablename}. Will try to skip.")
return None
return self.load_dataset(fils)

Expand Down Expand Up @@ -212,7 +212,7 @@ def load_regrid_dataset(self, case, field):

def load_regrid_da(self, case, field):
"""Return a data array to be used as reference (aka baseline) for variable field."""
add_offset, scale_factor = self.get_defaults(case, field)
add_offset, scale_factor = self.get_value_converters(case, field)
fils = self.get_regrid_file(case, field)
if not fils:
warnings.warn(f"ERROR: Did not find regrid file(s) for case: {case}, variable: {field}")
Expand All @@ -225,7 +225,10 @@ def get_ref_regrid_file(self, case, field):
"""Return list of reference regridded files"""
if self.adf.compare_obs:
obs_loc = self.ref_var_loc.get(field, None)
fils = [str(obs_loc)]
if obs_loc:
fils = [str(obs_loc)]
else:
fils = []
else:
model_rg_loc = Path(self.adf.get_basic_info("cam_regrid_loc", required=True))
fils = sorted(model_rg_loc.glob(f"{case}_{field}_*.nc"))
Expand All @@ -236,19 +239,19 @@ def load_reference_regrid_dataset(self, case, field):
"""Return a data set to be used as reference (aka baseline) for variable field."""
fils = self.get_ref_regrid_file(case, field)
if not fils:
warnings.warn(f"ERROR: Did not find regrid file(s) for case: {case}, variable: {field}")
warnings.warn(f"ERROR: Did not find regridded file(s) for case: {case}, variable: {field}")
return None
return self.load_dataset(fils)


def load_reference_regrid_da(self, case, field):
"""Return a data array to be used as reference (aka baseline) for variable field."""
add_offset, scale_factor = self.get_defaults(case, field)
add_offset, scale_factor = self.get_value_converters(case, field)
fils = self.get_ref_regrid_file(case, field)
if not fils:
warnings.warn(f"ERROR: Did not find regrid file(s) for case: {case}, variable: {field}")
warnings.warn(f"ERROR: Did not find regridded file(s) for case: {case}, variable: {field}")
return None
#Change the variable name from CAM standard to what is is
#Change the variable name from CAM standard to what is
# listed in variable defaults for this observation field
if self.adf.compare_obs:
field = self.ref_var_nam[field]
Expand Down Expand Up @@ -286,17 +289,18 @@ def load_da(self, fils, variablename, **kwargs):
warnings.warn(f"ERROR: Load failed for {variablename}")
return None
da = (ds[variablename]).squeeze()

da = da * kwargs["scale_factor"] + kwargs["add_offset"]
scale_factor = kwargs.get('scale_factor', 1)
add_offset = kwargs.get('add_offset', 0)
da = da * scale_factor + add_offset
if variablename in self.adf.variable_defaults:
vres = self.adf.variable_defaults[variablename]
da.attrs['units'] = vres.get("new_unit", da.attrs.get('units', 'none'))
else:
da.attrs['units'] = 'none'
return da

# Get vairable defaults, if applicable
def get_defaults(self, case, variablename):
# Get variable conversion defaults, if applicable
def get_value_converters(self, case, variablename):
"""
Get variable defaults if applicable
Expand Down

0 comments on commit 3085b79

Please sign in to comment.