Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions e3sm_to_cmip/cmor_handlers/_formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,44 @@ def rtmt(ds: xr.Dataset) -> xr.DataArray:

def tran(ds: xr.Dataset) -> xr.DataArray:
"""
tran = QSOIL + QVEGT
tran = QVEGT
"""
result = ds["QSOIL"] + ds["QVEGT"]
result = ds["QVEGT"]

return result


def burntFractionAll(ds: xr.Dataset) -> xr.DataArray:
"""
burntFractionAll = FAREA_BURNED*3600*24*30*100
"""
result = ds["FAREA_BURNED"] * 3600 * 24 * 30 * 100

return result


def cRoot(ds: xr.Dataset) -> xr.DataArray:
"""
cRoot = (FROOTC+LIVECROOTC+DEADCROOTC)/1000
"""
result = (ds["FROOTC"] + ds["LIVECROOTC"] + ds["DEADCROOTC"]) / 1000

return result


def rGrowth(ds: xr.Dataset) -> xr.DataArray:
"""
rGrowth = (AR-MR)/1000
"""
result = (ds["AR"] - ds["MR"]) / 1000

return result


def sootsn(ds: xr.Dataset) -> xr.DataArray:
"""
sootsn = SNOBCMSL+SNODSTMSL+SNOOCMSL
"""
result = ds["SNOBCMSL"] + ds["SNODSTMSL"] + ds["SNOOCMSL"]

return result
132 changes: 130 additions & 2 deletions e3sm_to_cmip/cmor_handlers/handlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,134 @@
formula: null
positive: null
levels: null
- name: burntFractionAll
units: "%"
raw_variables: [FAREA_BURNED]
table: CMIP6_Lmon.json
unit_conversion: null
formula: FAREA_BURNED*3600*24*30*100
positive: null
levels: null
- name: cCwd
units: kg m-2
raw_variables: [CWDC]
table: CMIP6_Lmon.json
unit_conversion: g-to-kg
formula: null
positive: null
levels: null
- name: cLeaf
units: kg m-2
raw_variables: [LEAFC]
table: CMIP6_Lmon.json
unit_conversion: g-to-kg
formula: null
positive: null
levels: null
- name: cRoot
units: kg m-2
raw_variables: [FROOTC, LIVECROOTC, DEADCROOTC]
table: CMIP6_Lmon.json
unit_conversion: null
formula: (FROOTC+LIVECROOTC+DEADCROOTC)/1000
positive: null
levels: null
- name: hfdsn
units: W m-2
raw_variables: [FGR]
table: CMIP6_LImon.json
unit_conversion: "-1"
formula: null
positive: down
levels: null
- name: lwsnl
units: kg m-2
raw_variables: [SNOWLIQ]
table: CMIP6_LImon.json
unit_conversion: null
formula: null
positive: null
levels: null
- name: nppLeaf
units: kg m-2 s-1
raw_variables: [LEAFC_ALLOC]
table: CMIP6_Lmon.json
unit_conversion: null
formula: null
positive: down
levels: null
- name: nppRoot
units: kg m-2 s-1
raw_variables: [FROOTC_ALLOC]
table: CMIP6_Lmon.json
unit_conversion: g-to-kg
formula: null
positive: down
levels: null
- name: nppWood
units: kg m-2 s-1
raw_variables: [WOODC_ALLOC]
table: CMIP6_Lmon.json
unit_conversion: g-to-kg
formula: null
positive: down
levels: null
- name: rGrowth
units: kg m-2 s-1
raw_variables: [AR, MR]
table: CMIP6_Lmon.json
unit_conversion: null
formula: (AR-MR)/1000
positive: down
levels: null
- name: rMaint
units: kg m-2 s-1
raw_variables: [MR]
table: CMIP6_Lmon.json
unit_conversion: g-to-kg
formula: null
positive: down
levels: null
- name: snc
units: "%"
raw_variables: [FSNO]
table: CMIP6_LImon.json
unit_conversion: 1-to-%
formula: null
positive: null
levels: null
- name: snd
units: m
raw_variables: [SNOWDP]
table: CMIP6_LImon.json
unit_conversion: null
formula: null
positive: null
levels: null
- name: snm
units: kg m-2 s-1
raw_variables: [QSNOMELT]
table: CMIP6_LImon.json
unit_conversion: null
formula: null
positive: null
levels: null
- name: snw
units: kg m-2
raw_variables: [H2OSNO_TOP]
table: CMIP6_LImon.json
unit_conversion: null
formula: null
positive: null
levels: null
- name: sootsn
units: kg m-2
raw_variables: [SNOBCMSL, SNODSTMSL, SNOOCMSL]
table: CMIP6_LImon.json
unit_conversion: null
formula: SNOBCMSL+SNODSTMSL+SNOOCMSL
positive: null
levels: null
- name: albisccp
units: "1"
raw_variables: [MEANCLDALB_ISCCP]
Expand Down Expand Up @@ -980,10 +1108,10 @@
levels: null
- name: tran
units: kg m-2 s-1
raw_variables: [QVEGT, QSOIL]
raw_variables: [QVEGT]
table: CMIP6_Lmon.json
unit_conversion: null
formula: QSOIL + QVEGT
formula: QVEGT
positive: up
levels: null
- name: ts
Expand Down
25 changes: 16 additions & 9 deletions e3sm_to_cmip/mpas.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,19 +802,26 @@ def _compute_moc_time_series(
).sum(dim="nCells")
mocSlices.append(mocTop)

moc = xarray.concat(mocSlices, dim="lat") # type: ignore
moc = moc.transpose("Time", "nVertLevelsP1", "lat") # type: ignore
moc = xarray.concat(mocSlices, dim="lat") # type: ignore[arg-type]
moc = moc.transpose("Time", "nVertLevelsP1", "lat")

# average to bin and level centers
moc = 0.25 * (
moc[:, 0:-1, 0:-1] + moc[:, 0:-1, 1:] + moc[:, 1:, 0:-1] + moc[:, 1:, 1:]
moc_avg = (
0.25
* (
moc[:, 0:-1, 0:-1] # type: ignore[index]
+ moc[:, 0:-1, 1:] # type: ignore[index]
+ moc[:, 1:, 0:-1] # type: ignore[index]
+ moc[:, 1:, 1:] # type: ignore[index]
)
)
moc = moc.rename({"nVertLevelsP1": "depth"}) # type: ignore
binCounts = xarray.DataArray(binCounts, dims=("lat")) # type: ignore
moc = moc.where(binCounts > 0) # type: ignore
moc_avg = moc_avg.rename({"nVertLevelsP1": "depth"})
binCounts = xarray.DataArray(binCounts, dims=("lat")) # type: ignore[assignment]
moc_masked = moc_avg.where(binCounts > 0) # type: ignore[operator]

_compute_dask(moc, showProgress, "Computing {} MOC".format(regionName))
_compute_dask(moc_masked, showProgress, "Computing {} MOC".format(regionName))

mocs[regionName] = moc
mocs[regionName] = moc_masked
Comment on lines +805 to +818
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mpas.py changes to make mypy happy


mocs = xarray.concat(mocs.values(), dim="basin") # type: ignore
mocs = mocs.transpose("Time", "basin", "depth", "lat") # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion e3sm_to_cmip/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def _get_e3sm_vars(self, input_path: str) -> list[str]:

for path in paths:
ds = xr.open_dataset(path, decode_timedelta=True)
data_vars = list(ds.data_vars.keys())
data_vars = [str(var) for var in ds.data_vars.keys()]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mpas.py changes to make mypy happy


e3sm_vars = e3sm_vars + data_vars

Expand Down
55 changes: 55 additions & 0 deletions scripts/debug/334-new_v3_land_handlers/run_v3_land.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

# Conda Environment
# -----------------
#conda activate e3sm_to_cmip_dev

# -----------------
# PATHS
# -----------------
e2c_path=/lcrc/group/e3sm/public_html/diagnostics/e3sm_to_cmip_data
model_data=/gpfs/fs0/globalscratch/ac.zhang40/e2c_tests/model_data
scratch=/gpfs/fs0/globalscratch/ac.zhang40

# TODO: Update result_dir
result_dir=${scratch}/e2c_tests

rgr_dir=${result_dir}/rgr
rgr_dir_vert=${result_dir}/rgr_vert
rgr_dir_vert_plev=${result_dir}/rgr_vert_plev
native_dir=${result_dir}/native

map_file=${e2c_path}/maps/map_r05_to_cmip6_180x360_traave.20231110.nc
tables_path=${e2c_path}/cmip6-cmor-tables/Tables/
metadata_path=${e2c_path}/default_metadata.json

start=1850
end=1850
ypf=1
caseid=v3.LR.historical_0101

## ------------------------------------------------------
## TEST CASE - land monthly h0
## ------------------------------------------------------
#input_path=${model_data}/
input_path=/lcrc/group/e3sm2/ac.wlin/E3SMv3/v3.LR.historical_0101/archive/lnd/hist
flags='-7 --dfl_lvl=1 --no_cll_msr'
#raw_var_list="LAISHA,LAISUN,QINTR,QOVER,QRUNOFF,QSOIL,QVEGE,QVEGT,SOILICE,SOILLIQ,SOILWATER_10CM,TSA,TSOI,H2OSNO"
#cmip_var_list="mrsos, mrso, mrfso, mrros, mrro, prveg, evspsblveg, evspsblsoi, tran, tsl, lai"
raw_var_list="LEAFC,FROOTC,LIVECROOTC,DEADCROOTC,CWDC,AR,MR,QVEGT,LEAFC_ALLOC,FROOTC_ALLOC,WOODC_ALLOC,FAREA_BURNED,LAND_USE_FLUX,FSNO,SNOWDP,SNOWLIQ,H2OSNO_TOP,QSNOMELT,FGR,SNOBCMSL,SNODSTMSL,SNOOCMSL"
cmip_var_list="burntFractionAll, cCwd, cLeaf, cRoot, nppLeaf, nppRoot, nppWood, rGrowth, rMaint, tran, hfdsn, lwsnl, snc, snd, snm, snw, sootsn"

rgr_dir=${result_dir}/rgr_lnd
native_dir=${result_dir}/native_lnd
mkdir -p $rgr_dir
mkdir -p $native_dir

# Note either include the extra variable landfrac or specify the file that has landfrac for subgrid scale mode to work.
#ncclimo -P elm -j 1 --var_xtr=landfrac --map=${map_file} --start=$start --end=$end --ypf=$ypf --split -c $caseid -o ${native_dir} -O ${rgr_dir} -v ${raw_var_list} -i ${input_path} ${flags}
# Alternative ncclimo invocation
#ncclimo -P elm -j 1 --map=${map_file} --start=$start --end=$end --ypf=$ypf --split -c $caseid -o ${native_dir} -O ${rgr_dir} -v ${raw_var_list} -i ${input_path} ${flags} --sgs_frc=${input_path}/v2.LR.historical_0101.elm.h0.1850-01.nc/landfrac

#raw_var_list_elm_bgc="TOTLITC,CWDC,TOTPRODC,SOIL1C,SOIL2C,SOIL3C,^SOIL4C$,COL_FIRE_CLOSS,WOOD_HARVESTC,TOTVEGC,NBP,GPP,AR,HR"
# CMORIZE Land Monthly variables
e3sm_to_cmip -i ${rgr_dir} -o $result_dir -v ${cmip_var_list} -t ${tables_path} -u ${metadata_path} --realm lnd --serial

10 changes: 4 additions & 6 deletions tests/cmor_handlers/test__formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,21 +382,19 @@ def test_rtmt():

# Test when required variable keys are NOT in the data dictionary.
with pytest.raises(KeyError):
rsuscs(xr.Dataset())
rtmt(xr.Dataset())
Comment on lines 407 to +408
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed incorrect function reference in test.



def test_tran():
ds = xr.Dataset(
data_vars={"QSOIL": _dummy_dataarray(), "QVEGT": _dummy_dataarray()}
)
ds = xr.Dataset(data_vars={"QVEGT": _dummy_dataarray()})

result = tran(ds)
expected = xr.DataArray(
dims=["lat", "lon"],
data=np.array([[0, 2, 4], [0, 2, 4], [0, 2, 4]]),
data=np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]]),
Comment on lines 411 to +417
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated test_tran() to get it passing for v3 formula code change.

)
xr.testing.assert_allclose(result, expected)

# Test when required variable keys are NOT in the data dictionary.
with pytest.raises(KeyError):
rsuscs(xr.Dataset())
tran(xr.Dataset())
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed incorrect function reference in test.

Loading