Skip to content

Commit

Permalink
Completely nuke the lib.py class with refactoring
Browse files Browse the repository at this point in the history
- Rename `infiles` to `vars_to_filepaths` which is a dict with the key being a var and value being list of strings
  • Loading branch information
tomvothecoder committed Sep 19, 2023
1 parent f57f54b commit f55315f
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 524 deletions.
46 changes: 15 additions & 31 deletions e3sm_to_cmip/cmor_handlers/handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import abc
from typing import Any, Dict, List, Literal, Optional, TypedDict, Union
from typing import Any, Dict, List, Literal, Optional, TypedDict

import cmor
import numpy as np
Expand All @@ -14,13 +16,6 @@
# Used by areacella.py
RADIUS = 6.37122e6

# The names for hybrid levels.
LEVEL_NAMES = [
"atmosphere_sigma_coordinate",
"standard_hybrid_sigma",
"standard_hybrid_sigma_half",
]


class Levels(TypedDict):
name: str
Expand Down Expand Up @@ -162,7 +157,7 @@ def to_dict(self) -> Dict[str, Any]:

def cmorize(
self,
infiles: List[str], # command-line arg (--input-path)
vars_to_filepaths: Dict[str, List[str]], # command-line arg (--input-path)
tables: str, # command-line arg (--tables-path)
user_input_path: str, # command-line arg (--user-metadata)
**kwargs,
Expand All @@ -176,7 +171,7 @@ def cmorize(
# outpath=None, # command-line arg (--output-path)
if self.unit_conversion is None:
return handle_variables(
infiles=infiles,
vars_to_filepaths=vars_to_filepaths,
raw_variables=self.raw_variables,
write_data=self._write_data,
outvar_name=self.name,
Expand All @@ -187,14 +182,11 @@ def cmorize(
serial=kwargs.get("serial"),
positive=self.positive,
levels=self.levels,
axis=kwargs.get("axis"),
logdir=kwargs.get("logdir"),
simple=kwargs.get("simple"),
outpath=kwargs.get("outpath"),
)

return default_handler(
infiles,
vars_to_filepaths,
tables=tables,
user_input_path=user_input_path,
raw_variables=self.raw_variables,
Expand All @@ -212,43 +204,35 @@ def cmorize(
def _write_data(
self,
varid: int,
data: Dict[str, Union[xr.DataArray, np.ndarray]],
data: Dict[str, xr.DataArray],
timeval: float,
timebnds: List[List[float]],
index: int,
**kwargs,
):
# TODO: Replace **kwargs with explicit method parameters.

# Convert all variable arrays to np.ndarray to ensure compatibility with
# the CMOR library. For handlers that use `self.levels`, the array data
# structures are already `np.ndarray` due to additional processing.
for var in self.raw_variables:
if isinstance(data[var], xr.DataArray):
data[var] = data[var].values # type: ignore

if self.formula is not None:
outdata = self.formula_method(data, index)
else:
outdata = data[self.raw_variables[0]][index, :]

if self._has_hybrid_sigma_levels():
elif self._has_hybrid_sigma_levels():
outdata = self._hybrid_to_pressure(
data["ps"], data["hyai"], data["hybi"], p0=100000 # type: ignore
data["PS"][index, :], data["hyai"], data["hybi"], p0=100000 # type: ignore
)
else:
outdata = data[self.raw_variables[0]][index, :]

# Replace `np.nan` with static FILL_VALUE
# Replace `np.nan` with static FILL_VALUE.
outdata = outdata.values
outdata[np.isnan(outdata)] = FILL_VALUE

if kwargs["simple"]:
return outdata

cmor.write(varid, outdata, time_vals=timeval, time_bnds=timebnds)

if self.levels is not None and self.levels.get("name") in LEVEL_NAMES:
if "PS" in data.keys() and "ips" in data.keys():
cmor.write(
data["ips"],
data["ps"],
data["PS"].values,
time_vals=timeval,
time_bnds=timebnds,
store_with=varid,
Expand Down
8 changes: 4 additions & 4 deletions e3sm_to_cmip/cmor_handlers/handlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -452,30 +452,30 @@
levels: null
- name: pfull
units: Pa
raw_variables: ["PS", "hyai", "hybi", "hyam", "hybm"]
raw_variables: ["hyai", "hybi", "hyam", "hybm", "PS"]
table: CMIP6_Amon.json
unit_conversion: null
formula: null
positive: null
levels:
{
name: "standard_hybrid_sigma",
unit": "1",
units: "1",
e3sm_axis_name: "lev",
e3sm_axis_bnds: "ilev",
time_name: "time2",
}
- name: phalf
units: Pa
raw_variables: ["PS", "hyai", "hybi", "hyam", "hybm"]
raw_variables: ["hyai", "hybi", "hyam", "hybm", "PS"]
table: CMIP6_Amon.json
unit_conversion: null
formula: null
positive: null
levels:
{
name: "atmosphere_sigma_coordinate",
unit": "1",
units: "1",
e3sm_axis_name: "lev",
e3sm_axis_bnds: "ilev",
time_name: "time2",
Expand Down
6 changes: 2 additions & 4 deletions e3sm_to_cmip/cmor_handlers/vars/clcalipso.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def write_data(varid, data, timeval, timebnds, index, **kwargs):
# ------------------------------------------------------------------


def handle(infiles, tables, user_input_path, **kwargs):
def handle(vars_to_filepaths, tables, user_input_path, **kwargs):
"""
Parameters
----------
Expand All @@ -51,19 +51,17 @@ def handle(infiles, tables, user_input_path, **kwargs):
"""

return handle_variables(
vars_to_filepaths=vars_to_filepaths,
metadata_path=user_input_path,
tables=tables,
table=kwargs.get("table", TABLE),
infiles=infiles,
raw_variables=RAW_VARIABLES,
write_data=write_data,
outvar_name=VAR_NAME,
outvar_units=VAR_UNITS,
serial=kwargs.get("serial"),
levels=LEVELS,
logdir=kwargs.get("logdir"),
simple=kwargs.get("simple"),
outpath=kwargs.get("outpath"),
)


Expand Down
8 changes: 2 additions & 6 deletions e3sm_to_cmip/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
from e3sm_to_cmip.lib import handle_variables


def default_handler(infiles, tables, user_input_path, **kwargs):

def default_handler(vars_to_filepaths, tables, user_input_path, **kwargs): # noqa: C901
RAW_VARIABLES = kwargs["raw_variables"]
unit_conversion = kwargs.get("unit_conversion")

def write_data(varid, data, timeval=None, timebnds=None, index=None, **kwargs):

if timeval is not None:
if unit_conversion is not None:
if unit_conversion == "g-to-kg":
Expand Down Expand Up @@ -62,16 +60,14 @@ def write_data(varid, data, timeval=None, timebnds=None, index=None, **kwargs):
metadata_path=user_input_path,
tables=tables,
table=kwargs["table"],
infiles=infiles,
vars_to_filepaths=vars_to_filepaths,
raw_variables=RAW_VARIABLES,
write_data=write_data,
outvar_name=kwargs["name"],
outvar_units=kwargs["units"],
serial=kwargs.get("serial"),
positive=kwargs.get("positive"),
logdir=kwargs.get("logdir"),
simple=kwargs.get("simple"),
outpath=kwargs.get("outpath"),
)


Expand Down
Loading

0 comments on commit f55315f

Please sign in to comment.