E3SM-Project / e3sm_diags

E3SM Diagnostics package
https://e3sm-project.github.io/e3sm_diags
BSD 3-Clause "New" or "Revised" License
39 stars 32 forks source link

Refactor area_mean_time_series #750

Closed forsyth2 closed 3 months ago

forsyth2 commented 10 months ago

Description

Refactor the area_mean_time_series set. Following the directions at https://github.com/E3SM-Project/e3sm_diags/wiki/CDAT-Migration-FY24-%E2%80%90-General-Guide#getting-started:

git fetch upstream
git checkout -b refactor/662-area_mean_time_series-set upstream/cdat-migration-fy24
mamba env create -f conda-env/dev.yml -n e3sm_diags_dev_662
conda activate e3sm_diags_dev_662
python -m pip install .

TODO

5/9/24

5/13/24

Notebook

regression-test-json.ipynb

Viewer links:

Results

Possible explanation for 22 / 792 metric diffs

  1. The cdms2.open slice flag ("ccb") adds an additional time coordinate point when opening up time series datasets (#759).

Here's the logic from dataset.py:

if sub_monthly:
     start_time = "{}-01-01".format(start_year)
     end_time = "{}-01-01".format(str(int(end_year) + 1))
     slice_flag = "co"
 else:
     start_time = "{}-01-15".format(start_year)
     end_time = "{}-12-15".format(end_year)
     slice_flag = "ccb"

Result: Adding code to handle this does not change the results

  1. The way masking is applied might be slightly different between xESMF and CDAT's ESMF?

5/24/24

5/29/24

All NetCDF files and metrics json are within relative tolerance (1e-5). This PR is good to go.

Plots are also identical (example dev and main).

Next steps

In a separate PR, validate fix for #759 using ex1.py for lat_lon set and time series files.

Checklist

If applicable:

forsyth2 commented 10 months ago

Using this script:

diags_set=area_mean_time_series

cd e3sm_diags
# https://anaconda.org/cdat/repo?sort=_name&sort_order=asc
for cdat_package in cd77 cdat cdat_compute_graph cdat_info cdat_jupyter_vcdat cdms2 cdp cdtime cdutil cibots compute_graph django-allow-cidr dv3d e3sm_nex esgf-compute-api esgf-compute-cert esgf-search ezget genutil image-compare jupyter-vcdat jupyterlab-git lats libcdms lmoments mesalib myproxyclient output_viewer selenium_testlib testsrunner thermo toolchain vcdat vcs vcs-js vcsaddons vtk vtk-cdat wk xmgrace
do
    git grep -n ${cdat_package} e3sm_diags/driver/${diags_set}_driver.py > /dev/null
    if [ $? == 0 ]; then
    echo ""
    echo "git grep -n ${cdat_package} e3sm_diags/driver/${diags_set}_driver.py"
    git grep -n ${cdat_package} e3sm_diags/driver/${diags_set}_driver.py
    fi
    git grep -n ${cdat_package} e3sm_diags/plot/cartopy/${diags_set}_plot.py > /dev/null
    if [ $? == 0 ]; then
    echo ""
    echo "git grep -n ${cdat_package} e3sm_diags/plot/cartopy/${diags_set}_plot.py"
    git grep -n ${cdat_package} e3sm_diags/plot/cartopy/${diags_set}_plot.py
    fi
done

I get:

git grep -n cdms2 e3sm_diags/driver/area_mean_time_series_driver.py
e3sm_diags/driver/area_mean_time_series_driver.py:8:import cdms2
e3sm_diags/driver/area_mean_time_series_driver.py:70:        with cdms2.open(mask_path) as f:

git grep -n cdutil e3sm_diags/driver/area_mean_time_series_driver.py
e3sm_diags/driver/area_mean_time_series_driver.py:9:import cdutil
e3sm_diags/driver/area_mean_time_series_driver.py:97:            test_domain = cdutil.averager(test_domain, axis="xy")
e3sm_diags/driver/area_mean_time_series_driver.py:98:            cdutil.setTimeBoundsMonthly(test_domain)
e3sm_diags/driver/area_mean_time_series_driver.py:99:            test_domain_year = cdutil.YEAR(test_domain)
e3sm_diags/driver/area_mean_time_series_driver.py:100:            # add back attributes since they got lost after applying cdutil.YEAR
e3sm_diags/driver/area_mean_time_series_driver.py:123:                    ref_domain = cdutil.averager(ref_domain, axis="xy")
e3sm_diags/driver/area_mean_time_series_driver.py:124:                    cdutil.setTimeBoundsMonthly(ref_domain)
e3sm_diags/driver/area_mean_time_series_driver.py:133:                    ref_domain_year = cdutil.YEAR(ref_domain)
tomvothecoder commented 9 months ago

I recently squashed the cdat-migration-fy24 branch to a few commits and just rebased this branch on top of it.

Can you make sure to checkout the latest version of his branch? I also recommend stashing and reapplying any changes you might have.

forsyth2 commented 8 months ago

@tomvothecoder Do you know what parameters are best for testing area_mean_time_series? I tried python run_area_mean_time_series.py -d diags.cfg using all the variables listed on the defaults but I keep getting this error:

RuntimeError: No parameters we able to be extracted. Please check the parameters you defined.
forsyth2 commented 5 months ago

Initial analysis of refactoring needs:

Driver

https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/driver/area_mean_time_series_driver.py#L67-L72

=> ds_land_sea_mask: xr.Dataset = test_ds._get_land_sea_mask(season) (as in https://github.com/E3SM-Project/e3sm_diags/pull/749/files#r1467834914)

https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/driver/area_mean_time_series_driver.py#L97-L100 https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/driver/area_mean_time_series_driver.py#L123-L124 https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/driver/area_mean_time_series_driver.py#L133 => All the cdutil references need to be changed out here. I'm not sure with what. I'm not really seeing any cdutil in the polar set (#749), and not much relevant in the lat_lon set (#677), to compare to.

Plot

https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/plot/cartopy/area_mean_time_series_plot.py#L26-L36 => Rename to PANEL_CFG

https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/plot/cartopy/area_mean_time_series_plot.py#L40 => Rename to BORDER_PADDING

The two changes above will be passed into: https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/plot/utils.py#L59

https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/plot/cartopy/area_mean_time_series_plot.py#L100-L142 => Replace with a call to https://github.com/E3SM-Project/e3sm_diags/blob/f6c4fdfeabb6765c330b93bee8d18063efb1f9a6/e3sm_diags/plot/utils.py#L59