Skip to content

Commit

Permalink
Merge pull request #168 from mnlevy1981/more_workflow_updates
Browse files Browse the repository at this point in the history
Updates to sea-ice notebook to run in workflow
  • Loading branch information
dabail10 authored Jan 27, 2025
2 parents be30051 + d2f587f commit 18c0e37
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 71 deletions.
2 changes: 2 additions & 0 deletions examples/key_metrics/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ compute_notebooks:
parameter_groups:
none:
climo_nyears: 35
grid_file: '/glade/campaign/cesm/community/omwg/grids/tx2_3v2_grid.nc'
path_nsidc: '/glade/campaign/cesm/development/cross-wg/diagnostic_framework/CUPiD_obs_data/ice/analysis_datasets/hemispheric_data/NSIDC_SeaIce_extent'

lnd:
Global_TerrestrialCouplingIndex_VisualCompareObs:
Expand Down
213 changes: 142 additions & 71 deletions nblibrary/ice/Hemis_seaice_visual_compare_obs_lens.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
},
"outputs": [],
"source": [
"import os\n",
"\n",
"import xarray as xr\n",
"import numpy as np\n",
"import yaml\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt"
"import matplotlib.pyplot as plt\n",
"import nc_time_axis"
]
},
{
Expand Down Expand Up @@ -69,7 +72,10 @@
"\n",
"lc_kwargs = {}\n",
"\n",
"hist = 0"
"hist = 0\n",
"\n",
"path_nsidc = \"/glade/campaign/cesm/development/pcwg/ice/data/NSIDC_SeaIce_extent\"\n",
"grid_file = \"/glade/campaign/cesm/community/omwg/grids/tx2_3v2_grid.nc\""
]
},
{
Expand Down Expand Up @@ -140,31 +146,28 @@
"# Read in two cases. The ADF timeseries are needed here.\n",
"\n",
"ds1 = xr.open_mfdataset(\n",
" CESM_output_dir\n",
" + \"/\"\n",
" + case_name\n",
" + \"/ice/proc/tseries/month_1/\"\n",
" + case_name\n",
" + \".cice.h.\"\n",
" + \"*.nc\",\n",
" os.path.join(\n",
" CESM_output_dir, case_name, \"ice\", \"proc\", \"tseries\", f\"{case_name}.cice.h.*.nc\"\n",
" ),\n",
" data_vars=\"minimal\",\n",
" compat=\"override\",\n",
" coords=\"minimal\",\n",
").sel(time=slice(start_date, end_date))\n",
"ds2 = xr.open_mfdataset(\n",
" CESM_output_dir\n",
" + \"/\"\n",
" + base_case_name\n",
" + \"/ice/proc/tseries/month_1/\"\n",
" + base_case_name\n",
" + \".cice.h.\"\n",
" + \"*.nc\",\n",
" os.path.join(\n",
" CESM_output_dir,\n",
" base_case_name,\n",
" \"ice\",\n",
" \"proc\",\n",
" \"tseries\",\n",
" f\"{base_case_name}.cice.h.*.nc\",\n",
" ),\n",
" data_vars=\"minimal\",\n",
" compat=\"override\",\n",
" coords=\"minimal\",\n",
").sel(time=slice(base_start_date, base_end_date))\n",
"\n",
"ds_grid = xr.open_dataset(\"/glade/campaign/cesm/community/omwg/grids/tx2_3v2_grid.nc\")\n",
"ds_grid = xr.open_dataset(grid_file)\n",
"TLAT = ds_grid[\"TLAT\"]\n",
"TLON = ds_grid[\"TLONG\"]\n",
"tarea = ds_grid[\"TAREA\"] * 1.0e-4\n",
Expand Down Expand Up @@ -212,6 +215,34 @@
"# aice_cesm1[:,383-nj_nh:383,:] = ds_cesm1_nh['aice']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f8d57eb-8411-4b70-a200-35b99d3152f2",
"metadata": {},
"outputs": [],
"source": [
"# Two functions to help draw plots even if only one year of data is available\n",
"# If one year is present, horizontal line will be dotted instead of solid\n",
"\n",
"\n",
"def da_plot_len_time_might_be_one(da_in, alt_time):\n",
" # If da_in.time only has 1 value, draw horizontal line across range of alt_time\n",
" if len(da_in.time) > 1:\n",
" da_in.plot()\n",
" else:\n",
" time_arr = [alt_time.data[0], alt_time.data[-1]]\n",
" plt.plot(time_arr, [da_in.data[0], da_in.data[0]], linestyle=\":\")\n",
"\n",
"\n",
"def plt_plot_len_x_might_be_one(da_in, x_in, alt_x):\n",
" # If x_in only has one value, draw horizontal line across range of alt_x\n",
" if len(x_in) > 1:\n",
" plt.plot(x_in, da_in)\n",
" else:\n",
" plt.plot([alt_x[0], alt_x[-1]], [da_in.data[0], da_in.data[0]], linestyle=\":\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -239,8 +270,8 @@
"fig = plt.figure(figsize=(10, 10), tight_layout=True)\n",
"\n",
"ax = fig.add_subplot(3, 1, 1)\n",
"ds1_vhi_ann.plot()\n",
"ds2_vhi_ann.plot()\n",
"da_plot_len_time_might_be_one(ds1_vhi_ann, alt_time=ds2_ann.time)\n",
"da_plot_len_time_might_be_one(ds2_vhi_ann, alt_time=ds1_ann.time)\n",
"\n",
"plt.title(\"NH Annual Mean Integrated Timeseries\")\n",
"plt.ylim((0, 5))\n",
Expand All @@ -249,17 +280,17 @@
"plt.legend([case_name, base_case_name])\n",
"\n",
"ax = fig.add_subplot(3, 1, 2)\n",
"ds1_vhs_ann.plot()\n",
"ds2_vhs_ann.plot()\n",
"da_plot_len_time_might_be_one(ds1_vhs_ann, alt_time=ds2_ann.time)\n",
"da_plot_len_time_might_be_one(ds2_vhs_ann, alt_time=ds1_ann.time)\n",
"\n",
"plt.ylim((0, 0.5))\n",
"plt.xlabel(\"Year\")\n",
"plt.ylabel(\"NH Annual Mean Snow Volume $m x 10^{13}$\")\n",
"plt.legend([case_name, base_case_name])\n",
"\n",
"ax = fig.add_subplot(3, 1, 3)\n",
"ds1_area_ann.plot()\n",
"ds2_area_ann.plot()\n",
"da_plot_len_time_might_be_one(ds1_area_ann, alt_time=ds2_ann.time)\n",
"da_plot_len_time_might_be_one(ds2_area_ann, alt_time=ds1_ann.time)\n",
"\n",
"plt.ylim((0, 25))\n",
"plt.xlabel(\"Year\")\n",
Expand Down Expand Up @@ -294,8 +325,8 @@
"fig = plt.figure(figsize=(10, 10), tight_layout=True)\n",
"\n",
"ax = fig.add_subplot(3, 1, 1)\n",
"ds1_vhi_ann.plot()\n",
"ds2_vhi_ann.plot()\n",
"da_plot_len_time_might_be_one(ds1_vhi_ann, alt_time=ds2_ann.time)\n",
"da_plot_len_time_might_be_one(ds2_vhi_ann, alt_time=ds1_ann.time)\n",
"\n",
"plt.title(\"SH Annual Mean Integrated Timeseries\")\n",
"plt.ylim((0, 5))\n",
Expand All @@ -304,17 +335,17 @@
"plt.legend([case_name, base_case_name])\n",
"\n",
"ax = fig.add_subplot(3, 1, 2)\n",
"ds1_vhs_ann.plot()\n",
"ds2_vhs_ann.plot()\n",
"da_plot_len_time_might_be_one(ds1_vhs_ann, alt_time=ds2_ann.time)\n",
"da_plot_len_time_might_be_one(ds2_vhs_ann, alt_time=ds1_ann.time)\n",
"\n",
"plt.ylim((0, 0.5))\n",
"plt.xlabel(\"Year\")\n",
"plt.ylabel(\"SH Annual Mean Snow Volume $m x 10^{13}$\")\n",
"plt.legend([case_name, base_case_name])\n",
"\n",
"ax = fig.add_subplot(3, 1, 3)\n",
"ds1_area_ann.plot()\n",
"ds2_area_ann.plot()\n",
"da_plot_len_time_might_be_one(ds1_area_ann, alt_time=ds2_ann.time)\n",
"da_plot_len_time_might_be_one(ds2_area_ann, alt_time=ds1_ann.time)\n",
"\n",
"plt.ylim((0, 25))\n",
"plt.xlabel(\"Year\")\n",
Expand All @@ -339,20 +370,42 @@
"source": [
"### Read in the NSIDC data from files\n",
"\n",
"path_nsidc = \"/glade/campaign/cesm/development/pcwg/ice/data/NSIDC_SeaIce_extent/\"\n",
"\n",
"jan_nsidc = pd.read_csv(path_nsidc + \"N_01_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"feb_nsidc = pd.read_csv(path_nsidc + \"N_02_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"mar_nsidc = pd.read_csv(path_nsidc + \"N_03_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"apr_nsidc = pd.read_csv(path_nsidc + \"N_04_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"may_nsidc = pd.read_csv(path_nsidc + \"N_05_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"jun_nsidc = pd.read_csv(path_nsidc + \"N_06_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"jul_nsidc = pd.read_csv(path_nsidc + \"N_07_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"aug_nsidc = pd.read_csv(path_nsidc + \"N_08_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"sep_nsidc = pd.read_csv(path_nsidc + \"N_09_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"oct_nsidc = pd.read_csv(path_nsidc + \"N_10_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"nov_nsidc = pd.read_csv(path_nsidc + \"N_11_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"dec_nsidc = pd.read_csv(path_nsidc + \"N_12_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"jan_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_01_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"feb_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_02_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"mar_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_03_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"apr_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_04_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"may_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_05_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"jun_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_06_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"jul_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_07_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"aug_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_08_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"sep_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_09_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"oct_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_10_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"nov_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_11_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"dec_nsidc = pd.read_csv(\n",
" os.path.join(path_nsidc, \"N_12_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"\n",
"jan_area = jan_nsidc.iloc[:, 5].values\n",
"feb_area = feb_nsidc.iloc[:, 5].values\n",
Expand Down Expand Up @@ -415,20 +468,42 @@
"source": [
"### Read in the SH NSIDC data from files\n",
"\n",
"path_nsidc = \"/glade/campaign/cesm/development/pcwg/ice/data/NSIDC_SeaIce_extent/\"\n",
"\n",
"jan_nsidc_sh = pd.read_csv(path_nsidc + \"S_01_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"feb_nsidc_sh = pd.read_csv(path_nsidc + \"S_02_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"mar_nsidc_sh = pd.read_csv(path_nsidc + \"S_03_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"apr_nsidc_sh = pd.read_csv(path_nsidc + \"S_04_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"may_nsidc_sh = pd.read_csv(path_nsidc + \"S_05_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"jun_nsidc_sh = pd.read_csv(path_nsidc + \"S_06_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"jul_nsidc_sh = pd.read_csv(path_nsidc + \"S_07_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"aug_nsidc_sh = pd.read_csv(path_nsidc + \"S_08_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"sep_nsidc_sh = pd.read_csv(path_nsidc + \"S_09_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"oct_nsidc_sh = pd.read_csv(path_nsidc + \"S_10_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"nov_nsidc_sh = pd.read_csv(path_nsidc + \"S_11_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"dec_nsidc_sh = pd.read_csv(path_nsidc + \"S_12_extent_v3.0.csv\", na_values=[\"-99.9\"])\n",
"jan_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_01_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"feb_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_02_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"mar_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_03_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"apr_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_04_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"may_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_05_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"jun_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_06_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"jul_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_07_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"aug_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_08_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"sep_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_09_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"oct_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_10_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"nov_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_11_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"dec_nsidc_sh = pd.read_csv(\n",
" os.path.join(path_nsidc, \"S_12_extent_v3.0.csv\"), na_values=[\"-99.9\"]\n",
")\n",
"\n",
"jan_area_sh = jan_nsidc_sh.iloc[:, 5].values\n",
"feb_area_sh = feb_nsidc_sh.iloc[:, 5].values\n",
Expand Down Expand Up @@ -583,16 +658,12 @@
"ds1_sep = ds1_area.sel(time=(ds1_area.time.dt.month == 9))\n",
"ds2_sep = ds2_area.sel(time=(ds2_area.time.dt.month == 9))\n",
"\n",
"if hist == 0:\n",
" x1 = np.linspace(1, climo_nyears, climo_nyears)\n",
" x2 = np.linspace(1, climo_nyears, climo_nyears)\n",
" x3 = np.linspace(1, 36, 36)\n",
"else:\n",
" x1 = np.linspace(1, climo_nyears, climo_nyears)\n",
" x2 = np.linspace(1, climo_nyears, climo_nyears)\n",
" x3 = np.linspace(1, 36, 36)\n",
"plt.plot(x1, ds1_sep)\n",
"plt.plot(x2, ds2_sep)\n",
"x1 = np.linspace(1, len(ds1_sep.time), len(ds1_sep.time))\n",
"x2 = np.linspace(1, len(ds2_sep.time), len(ds2_sep.time))\n",
"x3 = np.linspace(1, 36, 36)\n",
"\n",
"plt_plot_len_x_might_be_one(ds1_sep, x1, x2)\n",
"plt_plot_len_x_might_be_one(ds2_sep, x2, x1)\n",
"plt.plot(x3, sep_area[0:36])\n",
"\n",
"plt.title(\"NH Annual Mean Sea Ice Area\")\n",
Expand Down Expand Up @@ -630,12 +701,12 @@
"ds1_sep_sh = ds1_area_sh.sel(time=(ds1_area_sh.time.dt.month == 9))\n",
"ds2_sep_sh = ds2_area_sh.sel(time=(ds2_area_sh.time.dt.month == 9))\n",
"\n",
"x1 = np.linspace(1, climo_nyears, climo_nyears)\n",
"x2 = np.linspace(1, climo_nyears, climo_nyears)\n",
"x1 = np.linspace(1, len(ds1_sep.time), len(ds1_sep.time))\n",
"x2 = np.linspace(1, len(ds2_sep.time), len(ds2_sep.time))\n",
"x3 = np.linspace(1, 36, 36)\n",
"\n",
"plt.plot(x1, ds1_sep_sh)\n",
"plt.plot(x2, ds2_sep_sh)\n",
"plt_plot_len_x_might_be_one(ds1_sep_sh, x1, x2)\n",
"plt_plot_len_x_might_be_one(ds2_sep_sh, x2, x1)\n",
"plt.plot(x3, sep_area_sh[0:36])\n",
"\n",
"plt.title(\"SH Annual Mean Sea Ice Area\")\n",
Expand Down

0 comments on commit 18c0e37

Please sign in to comment.