Skip to content

Commit

Permalink
Address pre-commit issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvothecoder committed Oct 16, 2024
1 parent aaed0ed commit 037a248
Show file tree
Hide file tree
Showing 34 changed files with 7,587 additions and 7,224 deletions.
68 changes: 41 additions & 27 deletions e3sm_to_cmip/cmor_handlers/mpas_vars/mlotst.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@

"""
compute Ocean Mixed Layer Thickness Defined by Sigma T, mlotst
"""

from __future__ import absolute_import, division, print_function

import xarray
import logging

import xarray

from e3sm_to_cmip import mpas, util
from e3sm_to_cmip.util import print_message

# 'MPAS' as a placeholder for raw variables needed
RAW_VARIABLES = ['MPASO', 'MPAS_mesh', 'MPAS_map']
RAW_VARIABLES = ["MPASO", "MPAS_mesh", "MPAS_map"]

# output variable name
VAR_NAME = 'mlotst'
VAR_UNITS = 'm'
TABLE = 'CMIP6_Omon.json'
VAR_NAME = "mlotst"
VAR_UNITS = "m"
TABLE = "CMIP6_Omon.json"


def handle(infiles, tables, user_input_path, **kwargs):
Expand All @@ -38,23 +40,26 @@ def handle(infiles, tables, user_input_path, **kwargs):
varname : str
the name of the processed variable after processing is complete
"""
if kwargs.get('simple'):
if kwargs.get("simple"):
msg = f"{VAR_NAME} is not supported for simple conversion"
print_message(msg)
return
msg = 'Starting {name}'.format(name=__name__)

msg = "Starting {name}".format(name=__name__)
logging.info(msg)

meshFileName = infiles['MPAS_mesh']
mappingFileName = infiles['MPAS_map']
timeSeriesFiles = infiles['MPASO']
meshFileName = infiles["MPAS_mesh"]
mappingFileName = infiles["MPAS_map"]
timeSeriesFiles = infiles["MPASO"]

dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False)
cellMask2D, _ = mpas.get_mpaso_cell_masks(dsMesh)

variableList = ['timeMonthly_avg_dThreshMLD',
'xtime_startMonthly', 'xtime_endMonthly']
variableList = [
"timeMonthly_avg_dThreshMLD",
"xtime_startMonthly",
"xtime_endMonthly",
]

ds = xarray.Dataset()
with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn:
Expand All @@ -64,22 +69,31 @@ def handle(infiles, tables, user_input_path, **kwargs):
ds.compute()

ds = mpas.add_mask(ds, cellMask2D)
ds = mpas.remap(ds, 'mpasocean', mappingFileName)
ds = mpas.remap(ds, "mpasocean", mappingFileName)

util.setup_cmor(var_name=VAR_NAME, table_path=tables, table_name=TABLE,
user_input_path=user_input_path)
util.setup_cmor(
var_name=VAR_NAME,
table_path=tables,
table_name=TABLE,
user_input_path=user_input_path,
)

# create axes
axes = [{'table_entry': 'time',
'units': ds.time.units},
{'table_entry': 'latitude',
'units': 'degrees_north',
'coord_vals': ds.lat.values,
'cell_bounds': ds.lat_bnds.values},
{'table_entry': 'longitude',
'units': 'degrees_east',
'coord_vals': ds.lon.values,
'cell_bounds': ds.lon_bnds.values}]
axes = [
{"table_entry": "time", "units": ds.time.units},
{
"table_entry": "latitude",
"units": "degrees_north",
"coord_vals": ds.lat.values,
"cell_bounds": ds.lat_bnds.values,
},
{
"table_entry": "longitude",
"units": "degrees_east",
"coord_vals": ds.lon.values,
"cell_bounds": ds.lon_bnds.values,
},
]
try:
mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS)
except Exception:
Expand Down
96 changes: 56 additions & 40 deletions e3sm_to_cmip/cmor_handlers/mpas_vars/pbo.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@

"""
compute Sea Water Pressure at Sea floor, pbo
"""

from __future__ import absolute_import, division, print_function

import xarray
import logging

import xarray

from e3sm_to_cmip import mpas, util
from e3sm_to_cmip.util import print_message

# 'MPAS' as a placeholder for raw variables needed
RAW_VARIABLES = ['MPASO', 'MPASO_namelist', 'MPAS_mesh', 'MPAS_map', 'PSL']
RAW_VARIABLES = ["MPASO", "MPASO_namelist", "MPAS_mesh", "MPAS_map", "PSL"]

# output variable name
VAR_NAME = 'pbo'
VAR_UNITS = 'Pa'
TABLE = 'CMIP6_Omon.json'
VAR_NAME = "pbo"
VAR_UNITS = "Pa"
TABLE = "CMIP6_Omon.json"


def handle(infiles, tables, user_input_path, **kwargs):
Expand All @@ -40,64 +42,78 @@ def handle(infiles, tables, user_input_path, **kwargs):
varname : str
the name of the processed variable after processing is complete
"""
if kwargs.get('simple'):
if kwargs.get("simple"):
msg = f"{VAR_NAME} is not supported for simple conversion"
print_message(msg)
return
msg = 'Starting {name}'.format(name=__name__)

msg = "Starting {name}".format(name=__name__)
logging.info(msg)

namelistFileName = infiles['MPASO_namelist']
meshFileName = infiles['MPAS_mesh']
mappingFileName = infiles['MPAS_map']
timeSeriesFiles = infiles['MPASO']
pslFileNames = infiles['PSL']
namelistFileName = infiles["MPASO_namelist"]
meshFileName = infiles["MPAS_mesh"]
mappingFileName = infiles["MPAS_map"]
timeSeriesFiles = infiles["MPASO"]
pslFileNames = infiles["PSL"]

namelist = mpas.convert_namelist_to_dict(namelistFileName)
config_density0 = float(namelist['config_density0'])
config_density0 = float(namelist["config_density0"])
gravity = 9.80616

dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False)
cellMask2D, cellMask3D = mpas.get_mpaso_cell_masks(dsMesh)

variableList = ['timeMonthly_avg_pressureAdjustedSSH',
'timeMonthly_avg_ssh', 'timeMonthly_avg_layerThickness',
'timeMonthly_avg_density', 'xtime_startMonthly',
'xtime_endMonthly']
variableList = [
"timeMonthly_avg_pressureAdjustedSSH",
"timeMonthly_avg_ssh",
"timeMonthly_avg_layerThickness",
"timeMonthly_avg_density",
"xtime_startMonthly",
"xtime_endMonthly",
]

ds = xarray.Dataset()
with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn:
seaIcePressure = config_density0 * gravity * \
(dsIn.timeMonthly_avg_pressureAdjustedSSH -
dsIn.timeMonthly_avg_ssh)
ds[VAR_NAME] = seaIcePressure.where(cellMask2D) + gravity * \
(dsIn.timeMonthly_avg_density *
dsIn.timeMonthly_avg_layerThickness).where(cellMask3D).sum(
dim='nVertLevels')
seaIcePressure = (
config_density0
* gravity
* (dsIn.timeMonthly_avg_pressureAdjustedSSH - dsIn.timeMonthly_avg_ssh)
)
ds[VAR_NAME] = seaIcePressure.where(cellMask2D) + gravity * (
dsIn.timeMonthly_avg_density * dsIn.timeMonthly_avg_layerThickness
).where(cellMask3D).sum(dim="nVertLevels")

ds = mpas.add_time(ds, dsIn)
ds.compute()

ds = mpas.remap(ds, 'mpasocean', mappingFileName)
ds = mpas.remap(ds, "mpasocean", mappingFileName)

with xarray.open_mfdataset(pslFileNames, concat_dim='time') as dsIn:
with xarray.open_mfdataset(pslFileNames, concat_dim="time") as dsIn:
ds[VAR_NAME] = ds[VAR_NAME] + dsIn.PSL.values

util.setup_cmor(var_name=VAR_NAME, table_path=tables, table_name=TABLE,
user_input_path=user_input_path)
util.setup_cmor(
var_name=VAR_NAME,
table_path=tables,
table_name=TABLE,
user_input_path=user_input_path,
)

# create axes
axes = [{'table_entry': 'time',
'units': ds.time.units},
{'table_entry': 'latitude',
'units': 'degrees_north',
'coord_vals': ds.lat.values,
'cell_bounds': ds.lat_bnds.values},
{'table_entry': 'longitude',
'units': 'degrees_east',
'coord_vals': ds.lon.values,
'cell_bounds': ds.lon_bnds.values}]
axes = [
{"table_entry": "time", "units": ds.time.units},
{
"table_entry": "latitude",
"units": "degrees_north",
"coord_vals": ds.lat.values,
"cell_bounds": ds.lat_bnds.values,
},
{
"table_entry": "longitude",
"units": "degrees_east",
"coord_vals": ds.lon.values,
"cell_bounds": ds.lon_bnds.values,
},
]
try:
mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS)
except Exception:
Expand Down
89 changes: 53 additions & 36 deletions e3sm_to_cmip/cmor_handlers/mpas_vars/pso.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@

"""
compute Sea Water Pressure at Sea Water Surface, pso
"""

from __future__ import absolute_import, division, print_function

import xarray
import logging

import xarray

from e3sm_to_cmip import mpas, util
from e3sm_to_cmip.util import print_message

# 'MPAS' as a placeholder for raw variables needed
RAW_VARIABLES = ['MPASO', 'MPASO_namelist', 'MPAS_mesh', 'MPAS_map', 'PSL']
RAW_VARIABLES = ["MPASO", "MPASO_namelist", "MPAS_mesh", "MPAS_map", "PSL"]

# output variable name
VAR_NAME = 'pso'
VAR_UNITS = 'Pa'
TABLE = 'CMIP6_Omon.json'
VAR_NAME = "pso"
VAR_UNITS = "Pa"
TABLE = "CMIP6_Omon.json"


def handle(infiles, tables, user_input_path, **kwargs):
Expand All @@ -39,61 +41,76 @@ def handle(infiles, tables, user_input_path, **kwargs):
varname : str
the name of the processed variable after processing is complete
"""
if kwargs.get('simple'):
if kwargs.get("simple"):
msg = f"{VAR_NAME} is not supported for simple conversion"
print_message(msg)
return
msg = 'Starting {name}'.format(name=__name__)

msg = "Starting {name}".format(name=__name__)
logging.info(msg)

namelistFileName = infiles['MPASO_namelist']
meshFileName = infiles['MPAS_mesh']
mappingFileName = infiles['MPAS_map']
timeSeriesFiles = infiles['MPASO']
pslFileNames = infiles['PSL']
namelistFileName = infiles["MPASO_namelist"]
meshFileName = infiles["MPAS_mesh"]
mappingFileName = infiles["MPAS_map"]
timeSeriesFiles = infiles["MPASO"]
pslFileNames = infiles["PSL"]

namelist = mpas.convert_namelist_to_dict(namelistFileName)
config_density0 = float(namelist['config_density0'])
config_density0 = float(namelist["config_density0"])
gravity = 9.80616

dsMesh = xarray.open_dataset(meshFileName, mask_and_scale=False)
cellMask2D, cellMask3D = mpas.get_mpaso_cell_masks(dsMesh)

variableList = ['timeMonthly_avg_pressureAdjustedSSH',
'timeMonthly_avg_ssh', 'timeMonthly_avg_layerThickness',
'timeMonthly_avg_density', 'xtime_startMonthly',
'xtime_endMonthly']
variableList = [
"timeMonthly_avg_pressureAdjustedSSH",
"timeMonthly_avg_ssh",
"timeMonthly_avg_layerThickness",
"timeMonthly_avg_density",
"xtime_startMonthly",
"xtime_endMonthly",
]

ds = xarray.Dataset()
with mpas.open_mfdataset(timeSeriesFiles, variableList) as dsIn:
seaIcePressure = config_density0 * gravity * \
(dsIn.timeMonthly_avg_pressureAdjustedSSH -
dsIn.timeMonthly_avg_ssh)
seaIcePressure = (
config_density0
* gravity
* (dsIn.timeMonthly_avg_pressureAdjustedSSH - dsIn.timeMonthly_avg_ssh)
)
ds[VAR_NAME] = seaIcePressure.where(cellMask2D)

ds = mpas.add_time(ds, dsIn)
ds.compute()

ds = mpas.remap(ds, 'mpasocean', mappingFileName)
ds = mpas.remap(ds, "mpasocean", mappingFileName)

with xarray.open_mfdataset(pslFileNames, concat_dim='time') as dsIn:
with xarray.open_mfdataset(pslFileNames, concat_dim="time") as dsIn:
ds[VAR_NAME] = ds[VAR_NAME] + dsIn.PSL.values

util.setup_cmor(var_name=VAR_NAME, table_path=tables, table_name=TABLE,
user_input_path=user_input_path)
util.setup_cmor(
var_name=VAR_NAME,
table_path=tables,
table_name=TABLE,
user_input_path=user_input_path,
)

# create axes
axes = [{'table_entry': 'time',
'units': ds.time.units},
{'table_entry': 'latitude',
'units': 'degrees_north',
'coord_vals': ds.lat.values,
'cell_bounds': ds.lat_bnds.values},
{'table_entry': 'longitude',
'units': 'degrees_east',
'coord_vals': ds.lon.values,
'cell_bounds': ds.lon_bnds.values}]
axes = [
{"table_entry": "time", "units": ds.time.units},
{
"table_entry": "latitude",
"units": "degrees_north",
"coord_vals": ds.lat.values,
"cell_bounds": ds.lat_bnds.values,
},
{
"table_entry": "longitude",
"units": "degrees_east",
"coord_vals": ds.lon.values,
"cell_bounds": ds.lon_bnds.values,
},
]
try:
mpas.write_cmor(axes, ds, VAR_NAME, VAR_UNITS)
except Exception:
Expand Down
Loading

0 comments on commit 037a248

Please sign in to comment.