Skip to content

Commit

Permalink
Merge pull request #192 from blychs/develop
Browse files Browse the repository at this point in the history
Fix bugs in layer height and temperature
  • Loading branch information
zmoon authored Sep 12, 2024
2 parents b45c1fa + 44a6ee7 commit 0954436
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions monetio/models/_camx_mm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ def open_mfdataset(
dset = add_met_data_3D(dset, dset_met)
if "alt_agl_m_mid" in dset.variables:
var_list = var_list + ["alt_agl_m_mid"]
if "layer_height" in dset.variables:
var_list = var_list + ["layer_height"]
if "layer_height_agl" in dset.variables:
var_list = var_list + ["layer_height_agl"]
if "pres_pa_mid" in dset.variables:
var_list = var_list + ["pres_pa_mid"]
if "temperature_k" in dset.variables:
var_list = var_list + ["temperature_k"]
else:
warnings.warn("Filename for meteorological input not provided. Adding only altitude.")
if (landuse_file is not None) and ("alt_agl_m_mid" in dset.variables):
Expand Down Expand Up @@ -196,22 +198,26 @@ def add_met_data_3D(d_chem, d_met):
d_chem["pres_pa_mid"] = d_met["PRESS_MB"] * 100
else:
warnings.warn("No pressure variable found. PRESS_MB and pressure were tested.")

if "press_pa_mid" in d_chem.variables:
d_chem["pres_pa_mid"].attrs = {
"units": "Pa",
"long_name": "pressure",
"var_desc": "pressure",
}
if ("z" in d_met.variables) or ("ZGRID_M" in d_met.variables):
d_chem["alt_agl_m_mid"], d_chem["layer_height"] = _calc_midlayer_height_agl(d_met)
d_chem["alt_agl_m_mid"], d_chem["layer_height_agl"] = _calc_midlayer_height_agl(d_met)
else:
warnings.warn("No altitude AGL was found.")
if "temperature" in list(d_met.variabled):

if "temperature" in d_met.variables:
d_chem["temperature_k"] = d_met["temperature"]
elif "TEMP_K" in list(d_met.variables):
elif "TEMP_K" in d_met.variables:
d_chem["temperature_k"] = d_met["TEMP_K"]
else:
warnings.warn("No temperature variable found. TEMP_K and temperature were tested.")
if "temperature_k" in d_chem.variables:
d_chem["temperature_k"].attrs["var_desc"] = "Temperature of layer in K."

return d_chem

Expand Down Expand Up @@ -434,29 +440,25 @@ def _calc_midlayer_height_agl(dset):
DataArray with the midlayer height above ground level
"""

assert dset["z"].dims == (
"TSTEP",
"LAY",
"ROW",
"COL",
), "Check dims of z, should be [TSTEP, LAY, ROW, COL]"

if "z" in dset.variables:
height = "z"
elif "ZGRID_M" in dset.variables:
height = "ZGRID_M"
else:
raise "No height variable found, but _calc_midlayer_height_agl was called."
mid_layer_height = np.array(dset[height]) # height in the layer upper interface of each layer
layer_height = dset[height]
layer_height_agl = dset[height]
layer_height_agl.attrs["long_name"] = "Height AGL at top"
layer_height_agl.attrs["var_desc"] = "Layer height above ground level at top"
mid_layer_height[:, 1:, :, :] = (
mid_layer_height[:, :-1, :, :] + mid_layer_height[:, 1:, :, :]
) / 2
mid_layer_height[0, 0, :, :] = mid_layer_height[0, 0, :, :] / 2
alt_agl_m_mid = xr.zeros_like(dset[height])
alt_agl_m_mid[:, :, :, :] = mid_layer_height
alt_agl_m_mid.attrs["var_desc"] = "Layer height above ground level at midpoint"
return alt_agl_m_mid, layer_height
alt_agl_m_mid.attrs["long_name"] = "Height AGL at midpoint"
return alt_agl_m_mid, layer_height_agl


def _calc_midlayer_height_msl(dset, dset_lu):
Expand Down

0 comments on commit 0954436

Please sign in to comment.