From 8a239756e511642bebfa2c6377af3cd13d5feef1 Mon Sep 17 00:00:00 2001 From: ChengzhuZhang Date: Wed, 24 Jan 2024 11:40:07 -0800 Subject: [PATCH] revert some derived variable logic; fix one derived variable --- e3sm_diags/derivations/derivations.py | 2 +- e3sm_diags/derivations/formulas.py | 19 ++++++++-------- e3sm_diags/driver/utils/dataset_xr.py | 6 ++--- examples/test_refactor/diags.cfg | 32 +++++++++++++++++---------- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/e3sm_diags/derivations/derivations.py b/e3sm_diags/derivations/derivations.py index 7153df946b..5ac1261348 100644 --- a/e3sm_diags/derivations/derivations.py +++ b/e3sm_diags/derivations/derivations.py @@ -927,7 +927,7 @@ "QFLX", ), lambda precc, precl, qflx: pminuse_convert_units( - prect(precc, precl) - pminuse_convert_units(qflx) + prect(precc, precl) - qflxconvert_units(qflx) ), ), ( diff --git a/e3sm_diags/derivations/formulas.py b/e3sm_diags/derivations/formulas.py index 13c29bc574..85f65b9069 100644 --- a/e3sm_diags/derivations/formulas.py +++ b/e3sm_diags/derivations/formulas.py @@ -77,15 +77,16 @@ def qflx_convert_to_lhflx_approxi(var: xr.DataArray): def pminuse_convert_units(var: xr.DataArray): - if ( - var.attrs["units"] == "kg/m2/s" - or var.attrs["units"] == "kg m-2 s-1" - or var.attrs["units"] == "kg/s/m^2" - ): - # need to find a solution for units not included in udunits - # var = convert_units( var, 'kg/m2/s' ) - var = var * 3600.0 * 24 # convert to mm/day - var.attrs["units"] = "mm/day" + if hasattr(var, "units"): + if ( + var.attrs["units"] == "kg/m2/s" + or var.attrs["units"] == "kg m-2 s-1" + or var.attrs["units"] == "kg/s/m^2" + ): + # need to find a solution for units not included in udunits + # var = convert_units( var, 'kg/m2/s' ) + var = var * 3600.0 * 24 # convert to mm/day + var.attrs["units"] = "mm/day" var.attrs["long_name"] = "precip. flux - evap. flux" return var diff --git a/e3sm_diags/driver/utils/dataset_xr.py b/e3sm_diags/driver/utils/dataset_xr.py index 688eccef60..4cf9b0a289 100644 --- a/e3sm_diags/driver/utils/dataset_xr.py +++ b/e3sm_diags/driver/utils/dataset_xr.py @@ -386,10 +386,10 @@ def _get_climo_dataset(self, season: str) -> xr.Dataset: filepath = self._get_climo_filepath(season) ds = xc.open_dataset(filepath, use_cftime=True, add_bounds=["X", "Y"]) - if self.var in ds.variables: - pass - elif self.var in self.derived_vars_map: + if self.var in self.derived_vars_map: ds = self._get_dataset_with_derived_climo_var(ds) + elif self.var in ds.variables: + pass else: raise IOError( f"Variable '{self.var}' was not in the file '{filepath}', nor was " diff --git a/examples/test_refactor/diags.cfg b/examples/test_refactor/diags.cfg index ff76f4b210..9854f056c4 100644 --- a/examples/test_refactor/diags.cfg +++ b/examples/test_refactor/diags.cfg @@ -1,10 +1,18 @@ #[#] #sets = ["zonal_mean_xy"] -#case_id = "GPCP_OAFLux" -#variables = ["PminusE"] -#ref_name = "GPCP_OAFLux" -#reference_name = "PRECT(GPCP) minus QFLX(OAFLux)" +#case_id = "MERRA2" +#variables = ["TREFHT"] +#regions = ["global"] +#ref_name = "MERRA2" +#reference_name = "MERRA2 Reanalysis" #seasons = ["ANN"] +[#] +sets = ["zonal_mean_xy"] +case_id = "GPCP_OAFLux" +variables = ["PminusE"] +ref_name = "GPCP_OAFLux" +reference_name = "PRECT(GPCP) minus QFLX(OAFLux)" +seasons = ["ANN"] # #[#] #sets = ["zonal_mean_xy"] @@ -24,11 +32,11 @@ #seasons = ["ANN"] #regions = ["global"] -[#] -sets = ["zonal_mean_xy"] -case_id = "ERA5" -variables = ["T"] -ref_name = "ERA5" -reference_name = "ERA5 Reanalysis" -seasons = ["ANN"] -plevs = [850.0] \ No newline at end of file +#[#] +#sets = ["zonal_mean_xy"] +#case_id = "ERA5" +#variables = ["T"] +#ref_name = "ERA5" +#reference_name = "ERA5 Reanalysis" +#seasons = ["ANN"] +#plevs = [850.0] \ No newline at end of file