From d32ba96b916dfe6a5f5b4485989d85a7f7e55d19 Mon Sep 17 00:00:00 2001 From: zmoon Date: Fri, 15 Nov 2024 09:24:50 -0600 Subject: [PATCH 1/9] Implement some special cases for wind speed/direction variables rotated to earth coords --- monetio/models/_wrfchem_mm.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index 4b583719..e30bd21a 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -18,7 +18,7 @@ def open_mfdataset( var_list=["o3"], surf_only=False, surf_only_nc=False, - **kwargs + **kwargs, ): """Method to open WRF-chem and RAP-chem netcdf files. @@ -104,6 +104,33 @@ def open_mfdataset( ) if var == "zstag": var_wrf = var_wrf.rename("zstag") + elif var in {"uvmet10", "uvmet"}: + # These return u and v wind components in one variable + pref = var.split("_")[0] + var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) + var_wrf_list.extend( + [ + var_wrf.isel(u_v=0).rename(f"{pref}_u"), + var_wrf.isel(u_v=1).rename(f"{pref}_v"), + ] + ) + continue + elif var in {"uvmet10_wspd_wdir", "uvmet_wspd_wdir"}: + # These return wind speed and wind direction in one variable + pref = var.split("_")[0] + var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) + var_wrf_list.extend( + [ + var_wrf.isel(wspd_wdir=0).rename(f"{pref}_wspd"), + var_wrf.isel(wspd_wdir=1).rename(f"{pref}_wdir"), + ] + ) + continue + elif var in {"uvmet10_wspd", "uvmet10_wdir", "uvmet_wspd", "uvmet_wdir"}: + # These return a variable with _wspd_wdir suffix instead of the correct name + pref = var.split("_")[0] + var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) + var_wrf = var_wrf.rename(var) else: var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) var_wrf_list.append(var_wrf) From aabe31a21063c18ce551c9153f3612f5ce5dc351 Mon Sep 17 00:00:00 2001 From: zmoon Date: Fri, 15 Nov 2024 09:41:11 -0600 Subject: [PATCH 2/9] pref not needed for this one --- monetio/models/_wrfchem_mm.py | 1 - 1 file changed, 1 deletion(-) diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index e30bd21a..ed8a59c7 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -128,7 +128,6 @@ def open_mfdataset( continue elif var in {"uvmet10_wspd", "uvmet10_wdir", "uvmet_wspd", "uvmet_wdir"}: # These return a variable with _wspd_wdir suffix instead of the correct name - pref = var.split("_")[0] var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) var_wrf = var_wrf.rename(var) else: From a60fdfcef2f736e0bc2554c135db3c2d35b29b2e Mon Sep 17 00:00:00 2001 From: zmoon Date: Fri, 15 Nov 2024 09:44:08 -0600 Subject: [PATCH 3/9] Set units for wind dir --- monetio/models/_wrfchem_mm.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index ed8a59c7..045e7afb 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -122,7 +122,7 @@ def open_mfdataset( var_wrf_list.extend( [ var_wrf.isel(wspd_wdir=0).rename(f"{pref}_wspd"), - var_wrf.isel(wspd_wdir=1).rename(f"{pref}_wdir"), + var_wrf.isel(wspd_wdir=1).rename(f"{pref}_wdir").assign_attrs(units="deg"), ] ) continue @@ -130,6 +130,8 @@ def open_mfdataset( # These return a variable with _wspd_wdir suffix instead of the correct name var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) var_wrf = var_wrf.rename(var) + if var.endswith("_wdir"): + var_wrf = var_wrf.assign_attrs(units="deg") else: var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) var_wrf_list.append(var_wrf) From 29293f7695d09ebbb5090c96c9b3432857e6f636 Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 18 Nov 2024 10:22:56 -0600 Subject: [PATCH 4/9] Simplify pref code for uv --- monetio/models/_wrfchem_mm.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index 045e7afb..1f6c6ae8 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -106,12 +106,11 @@ def open_mfdataset( var_wrf = var_wrf.rename("zstag") elif var in {"uvmet10", "uvmet"}: # These return u and v wind components in one variable - pref = var.split("_")[0] var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) var_wrf_list.extend( [ - var_wrf.isel(u_v=0).rename(f"{pref}_u"), - var_wrf.isel(u_v=1).rename(f"{pref}_v"), + var_wrf.isel(u_v=0).rename(f"{var}_u"), + var_wrf.isel(u_v=1).rename(f"{var}_v"), ] ) continue From 01e54736d6ce87b6a07f8d9d840fabc66dc16348 Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 18 Nov 2024 10:34:04 -0600 Subject: [PATCH 5/9] Remove unwanted coordinate variables --- monetio/models/_wrfchem_mm.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index 1f6c6ae8..9730adb3 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -109,8 +109,8 @@ def open_mfdataset( var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) var_wrf_list.extend( [ - var_wrf.isel(u_v=0).rename(f"{var}_u"), - var_wrf.isel(u_v=1).rename(f"{var}_v"), + var_wrf.isel(u_v=0).drop_vars("u_v").rename(f"{var}_u"), + var_wrf.isel(u_v=1).drop_vars("u_v").rename(f"{var}_v"), ] ) continue @@ -120,15 +120,20 @@ def open_mfdataset( var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) var_wrf_list.extend( [ - var_wrf.isel(wspd_wdir=0).rename(f"{pref}_wspd"), - var_wrf.isel(wspd_wdir=1).rename(f"{pref}_wdir").assign_attrs(units="deg"), + var_wrf.isel(wspd_wdir=0).drop_vars("wspd_wdir").rename(f"{pref}_wspd"), + ( + var_wrf.isel(wspd_wdir=1) + .drop_vars("wspd_wdir") + .rename(f"{pref}_wdir") + .assign_attrs(units="deg") + ), ] ) continue elif var in {"uvmet10_wspd", "uvmet10_wdir", "uvmet_wspd", "uvmet_wdir"}: # These return a variable with _wspd_wdir suffix instead of the correct name var_wrf = getvar(wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False) - var_wrf = var_wrf.rename(var) + var_wrf = var_wrf.drop_vars("wspd_wdir").rename(var) if var.endswith("_wdir"): var_wrf = var_wrf.assign_attrs(units="deg") else: From 391146789c192d955f84cf87e889cafeeb372aa5 Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 18 Nov 2024 13:58:06 -0600 Subject: [PATCH 6/9] Some `.name` comments --- monetio/models/_wrfchem_mm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index 9730adb3..a70569ab 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -82,15 +82,15 @@ def open_mfdataset( if not surf_only_nc: # Add some additional defaults needed for aircraft analysis # Turn this on also if need to convert aerosols - var_list.append("pres") + var_list.append("pres") # 'pressure' var_list.append("height") - var_list.append("tk") + var_list.append("tk") # 'temp' var_list.append("height_agl") var_list.append("PSFC") # need to calculate surface pressure and dp and optionally dz here. # Additional defaults for satellite analysis - var_list.append("zstag") + var_list.append("zstag") # 'height' var_wrf_list = [] for var in var_list: From 898528b2d5e85565a420b2ec76c786e48f534697 Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 18 Nov 2024 13:59:46 -0600 Subject: [PATCH 7/9] Mutable default arg --- monetio/models/_wrfchem_mm.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index a70569ab..8b018e85 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -15,7 +15,7 @@ def open_mfdataset( fname, convert_to_ppb=True, mech="racm_esrl_vcp", - var_list=["o3"], + var_list=None, surf_only=False, surf_only_nc=False, **kwargs, @@ -54,6 +54,9 @@ def open_mfdataset( from netCDF4 import Dataset from wrf import ALL_TIMES, extract_global_attrs, getvar + if var_list is None: + var_list = ["o3"] + # Get dictionary of summed species for the mechanism of choice. dict_sum = dict_species_sums(mech=mech) From 50eaa37619414af421e298a22a591f01bcab6737 Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 18 Nov 2024 14:06:23 -0600 Subject: [PATCH 8/9] Allow requesting 'temperature_k' MM standard variable for model temperature --- .codespell-exclude | 2 +- monetio/models/_wrfchem_mm.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.codespell-exclude b/.codespell-exclude index 2c574a95..a3b40d18 100644 --- a/.codespell-exclude +++ b/.codespell-exclude @@ -18,7 +18,7 @@ CMAQ model data including new CAf calculation # allvars = Series(["TEMP", "Q", "PRES"]) # "p": dset["PRES"][:].compute().values - var_list.append("pres") + var_list.append("pres") # 'pressure' if var == "pres": # Insert special versions. "SIZ", df.loc[con, "variable"] = "Caf" diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index 8b018e85..7c394366 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -95,6 +95,12 @@ def open_mfdataset( # Additional defaults for satellite analysis var_list.append("zstag") # 'height' + # Make sure we get 'temperature_k' if requested + if "temperature_k" in var_list: + var_list.remove("temperature_k") + if "tk" not in var_list: + var_list.append("tk") + var_wrf_list = [] for var in var_list: if var == "pres": # Insert special versions. From 558cf3d63b70bca4622135a5d28ca09e76df75e3 Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 18 Nov 2024 14:33:03 -0600 Subject: [PATCH 9/9] Just skip --- .codespell-exclude | 2 +- monetio/models/_wrfchem_mm.py | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/.codespell-exclude b/.codespell-exclude index a3b40d18..a5e0ffc0 100644 --- a/.codespell-exclude +++ b/.codespell-exclude @@ -19,7 +19,7 @@ # allvars = Series(["TEMP", "Q", "PRES"]) # "p": dset["PRES"][:].compute().values var_list.append("pres") # 'pressure' - if var == "pres": # Insert special versions. + if var == "pres": "SIZ", df.loc[con, "variable"] = "Caf" df.loc[con, "variable"] = "Laf" diff --git a/monetio/models/_wrfchem_mm.py b/monetio/models/_wrfchem_mm.py index 7c394366..42fb444c 100644 --- a/monetio/models/_wrfchem_mm.py +++ b/monetio/models/_wrfchem_mm.py @@ -95,15 +95,12 @@ def open_mfdataset( # Additional defaults for satellite analysis var_list.append("zstag") # 'height' - # Make sure we get 'temperature_k' if requested - if "temperature_k" in var_list: - var_list.remove("temperature_k") - if "tk" not in var_list: - var_list.append("tk") - var_wrf_list = [] for var in var_list: - if var == "pres": # Insert special versions. + if not surf_only_nc and var in {"temperature_k", "pres_pa_mid"}: + # These will already be included in the final result + continue + if var == "pres": var_wrf = getvar( wrflist, var, timeidx=ALL_TIMES, method="cat", squeeze=False, units="Pa" )