Skip to content

Commit

Permalink
Remove unused functions in utils.py
Browse files Browse the repository at this point in the history
- Fix mypy errors
  • Loading branch information
tomvothecoder committed Jan 3, 2024
1 parent 5876dc3 commit efc82ff
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 394 deletions.
4 changes: 2 additions & 2 deletions e3sm_diags/derivations/formulas_cosp.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ def cosp_histogram_standardize(

# Align the dataset dimensions with the standardized variabe. `xr.align`
# returns both objects and element 0 is the xr.Dataset that is needed.
ds_final: xr.Dataset = xr.align(ds_new, var_std)[0] # type: ignore
ds_final = xr.align(ds_new, var_std)[0]
ds_final[target_var_key] = var_std
ds_final = ds_final.drop_vars(str(var.name))

# TODO: These functions don't actually add missing cloud bounds yet
# because it is based on the logic from the legacy code, which does not
# correctly add bounds if they are missing.
ds_final = _add_missing_cloud_bnds(ds_final, prs, "prs")
ds_final = _add_missing_cloud_bnds(ds_final, prs, "prs") # type: ignore
ds_final = _add_missing_cloud_bnds(ds_final, tau, "tau")

return ds_final
Expand Down
126 changes: 1 addition & 125 deletions e3sm_diags/derivations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
This module defines general utilities for deriving variables, including unit
conversion functions, renaming variables, etc.
"""
from typing import TYPE_CHECKING, Optional, Tuple

import MV2
import xarray as xr
from genutil import udunits

if TYPE_CHECKING:
from cdms2.axis import FileAxis
from cdms2.fvariable import FileVariable


def rename(new_name: str):
"""Given the new name, just return it."""
Expand Down Expand Up @@ -87,6 +80,7 @@ def convert_units(var: xr.DataArray, target_units: str): # noqa: C901
elif var.attrs["units"] in ["gN/m^2/day", "gP/m^2/day", "gC/m^2/day"]:
pass
else:
# FIXME: Replace genutil.udunits module.
temp = udunits(1.0, var.attrs["units"])
coeff, offset = temp.how(target_units)

Expand Down Expand Up @@ -123,121 +117,3 @@ def _apply_land_sea_mask(
masked_var = var.where(cond=cond, drop=False)

return masked_var


def adjust_prs_val_units(
prs: "FileAxis", prs_val: float, prs_val0: Optional[float]
) -> float:
"""Adjust the prs_val units based on the prs.id"""
# FIXME: Refactor this function to operate on xr.Dataset/xr.DataArray.
# COSP v2 cosp_pr in units Pa instead of hPa as in v1
# COSP v2 cosp_htmisr in units m instead of km as in v1
adjust_ids = {"cosp_prs": 100, "cosp_htmisr": 1000}

if prs_val0:
prs_val = prs_val0
if prs.id in adjust_ids.keys() and max(prs.getData()) > 1000:
prs_val = prs_val * adjust_ids[prs.id]

return prs_val


def determine_cloud_level(
prs_low: float,
prs_high: float,
low_bnds: Tuple[int, int],
high_bnds: Tuple[int, int],
) -> str:
"""Determines the cloud type based on prs values and the specified boundaries"""
# Threshold for cloud top height: high cloud (<440hPa or > 7km), midlevel cloud (440-680hPa, 3-7 km) and low clouds (>680hPa, < 3km)
if prs_low in low_bnds and prs_high in high_bnds:
return "middle cloud fraction"
elif prs_low in low_bnds:
return "high cloud fraction"
elif prs_high in high_bnds:
return "low cloud fraction"
else:
return "total cloud fraction"


def cosp_bin_sum(
cld: "FileVariable",
prs_low0: Optional[float],
prs_high0: Optional[float],
tau_low0: Optional[float],
tau_high0: Optional[float],
):
# FIXME: Refactor this function to operate on xr.Dataset/xr.DataArray.
"""sum of cosp bins to calculate cloud fraction in specified cloud top pressure / height and
cloud thickness bins, input variable has dimension (cosp_prs,cosp_tau,lat,lon)/(cosp_ht,cosp_tau,lat,lon)
"""
prs: FileAxis = cld.getAxis(0)
tau: FileAxis = cld.getAxis(1)

prs_low: float = adjust_prs_val_units(prs, prs[0], prs_low0)
prs_high: float = adjust_prs_val_units(prs, prs[-1], prs_high0)

if prs_low0 is None and prs_high0 is None:
prs_lim = "total cloud fraction"

tau_high, tau_low, tau_lim = determine_tau(tau, tau_low0, tau_high0)

if cld.id == "FISCCP1_COSP": # ISCCP model
cld_bin = cld(cosp_prs=(prs_low, prs_high), cosp_tau=(tau_low, tau_high))
simulator = "ISCCP"
if cld.id == "CLISCCP": # ISCCP obs
cld_bin = cld(isccp_prs=(prs_low, prs_high), isccp_tau=(tau_low, tau_high))

if cld.id == "CLMODIS": # MODIS
prs_lim = determine_cloud_level(prs_low, prs_high, (440, 44000), (680, 68000))
simulator = "MODIS"

if prs.id == "cosp_prs": # Model
cld_bin = cld(
cosp_prs=(prs_low, prs_high), cosp_tau_modis=(tau_low, tau_high)
)
elif prs.id == "modis_prs": # Obs
cld_bin = cld(modis_prs=(prs_low, prs_high), modis_tau=(tau_low, tau_high))

if cld.id == "CLD_MISR": # MISR model
if max(prs) > 1000: # COSP v2 cosp_htmisr in units m instead of km as in v1
cld = cld[
1:, :, :, :
] # COSP v2 cosp_htmisr[0] equals to 0 instead of -99 as in v1, therefore cld needs to be masked manually
cld_bin = cld(cosp_htmisr=(prs_low, prs_high), cosp_tau=(tau_low, tau_high))
prs_lim = determine_cloud_level(prs_low, prs_high, (7, 7000), (3, 3000))
simulator = "MISR"
if cld.id == "CLMISR": # MISR obs
cld_bin = cld(misr_cth=(prs_low, prs_high), misr_tau=(tau_low, tau_high))

cld_bin_sum = MV2.sum(MV2.sum(cld_bin, axis=1), axis=0)

try:
cld_bin_sum.long_name = simulator + ": " + prs_lim + " with " + tau_lim
# cld_bin_sum.long_name = "{}: {} with {}".format(simulator, prs_lim, tau_lim)
except BaseException:
pass
return cld_bin_sum


def determine_tau(
tau: "FileAxis", tau_low0: Optional[float], tau_high0: Optional[float]
):
# FIXME: Refactor this function to operate on xr.Dataset/xr.DataArray.
tau_low = tau[0]
tau_high = tau[-1]

if tau_low0 is None and tau_high0:
tau_high = tau_high0
tau_lim = "tau <" + str(tau_high0)
elif tau_high0 is None and tau_low0:
tau_low = tau_low0
tau_lim = "tau >" + str(tau_low0)
elif tau_low0 is None and tau_high0 is None:
tau_lim = str(tau_low) + "< tau < " + str(tau_high)
else:
tau_low = tau_low0
tau_high = tau_high0
tau_lim = str(tau_low) + "< tau < " + str(tau_high)

return tau_high, tau_low, tau_lim
Loading

0 comments on commit efc82ff

Please sign in to comment.