Skip to content

Commit

Permalink
Update notebook env setup instructions with kernel (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvothecoder authored May 8, 2024
1 parent 0214694 commit 7745757
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 97 deletions.
27 changes: 16 additions & 11 deletions docs/examples/climatology-and-departures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
"Related APIs:\n",
"\n",
"- [xarray.Dataset.temporal.climatology()](../generated/xarray.Dataset.temporal.climatology.rst)\n",
"- [xarray.Dataset.temporal.departures()](../generated/xarray.Dataset.temporal.departures.rst)\n",
"\n",
"The data used in this example can be found through the [Earth System Grid Federation (ESGF) search portal](https://aims2.llnl.gov/metagrid/search).\n"
"- [xarray.Dataset.temporal.departures()](../generated/xarray.Dataset.temporal.departures.rst)\n"
]
},
{
Expand All @@ -30,25 +28,32 @@
"- File 1: Monthly frequency from 1850-01-16 to 2014-12-16\n",
" - We want to calculate the annual and seasonal cycle climatologies and departures using this file.\n",
"- File 2: Hourly frequency from 2010-01-01 to 2015-01-01 (subset).\n",
" - We want to calculate the daily cycle climatologies and departures using this file.\n"
" - We want to calculate the daily cycle climatologies and departures using this file.\n",
"\n",
"The data used in this example can be found through the [Earth System Grid Federation (ESGF) search portal](https://aims2.llnl.gov/metagrid/search).\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Notebook Setup\n",
"### Notebook Kernel Setup\n",
"\n",
"Users can [install their own instance of xcdat](../getting-started-guide/installation.rst) and follow these examples using their own environment (e.g., with VS Code, Jupyter, Spyder, iPython) or [enable xcdat with existing JupyterHub instances](../getting-started-guide/getting-started-hpc-jupyter.rst).\n",
"\n",
"First, create the conda environment:\n",
"\n",
"```bash\n",
"conda create -n xcdat_notebook_0.7.0 -c conda-forge xcdat=0.7.0 xesmf matplotlib ipython ipykernel cartopy nc-time-axis gsw-xarray jupyter\n",
"```\n",
"\n",
"Create an Anaconda environment for this notebook using the command below, then select the\n",
"kernel in Jupyter.\n",
"Then install the kernel from the `xcdat_notebook_0.7.0` environment using `ipykernel` and name the kernel with the display name (e.g., `xcdat_notebook_0.7.0`):\n",
"\n",
"```bash\n",
"conda create -n xcdat_notebook -c conda-forge python xarray netcdf4 xcdat xesmf matplotlib nc-time-axis jupyter\n",
"python -m ipykernel install --user --name xcdat_notebook_0.7.0 --display-name xcdat_notebook_0.7.0\n",
"```\n",
"\n",
"- `xesmf` is required for horizontal regridding with xESMF\n",
"- `matplotlib` is an optional dependency required for plotting with xarray\n",
"- `nc-time-axis` is an optional dependency required for `matplotlib` to plot `cftime` coordinates\n"
"Then to select the kernel `xcdat_notebook_0.7.0` in Jupyter to use this kernel.\n"
]
},
{
Expand Down
18 changes: 15 additions & 3 deletions docs/examples/general-utilities.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@
"\n",
"The data used in this example is pulled directly from the [Earth System Grid Federation (ESGF)](https://aims2.llnl.gov/metagrid/search).\n",
"\n",
"Users can [install their own instance of xcdat](../getting-started-guide/installation.rst) and follow these examples using their own environment (e.g., with vscode, Jupyter, Spyder, iPython) or [enable xcdat with existing JupyterHub instances](../getting-started-guide/getting-started-hpc-jupyter.rst). The conda environment used in this notebook includes xcdat, xesmf, matplotlib, ipython, ipykernel, cartopy, and jupyter:\n",
"### Notebook Kernel Setup\n",
"\n",
"Users can [install their own instance of xcdat](../getting-started-guide/installation.rst) and follow these examples using their own environment (e.g., with VS Code, Jupyter, Spyder, iPython) or [enable xcdat with existing JupyterHub instances](../getting-started-guide/getting-started-hpc-jupyter.rst).\n",
"\n",
"First, create the conda environment:\n",
"\n",
"```bash\n",
"conda create -n xcdat_notebook_0.7.0 -c conda-forge xcdat=0.7.0 xesmf matplotlib ipython ipykernel cartopy nc-time-axis gsw-xarray jupyter\n",
"```\n",
"\n",
"Then install the kernel from the `xcdat_notebook_0.7.0` environment using `ipykernel` and name the kernel with the display name (e.g., `xcdat_notebook_0.7.0`):\n",
"\n",
"```bash\n",
"python -m ipykernel install --user --name xcdat_notebook_0.7.0 --display-name xcdat_notebook_0.7.0\n",
"```\n",
"conda create -n xcdat_notebook -c conda-forge xcdat xesmf matplotlib ipython ipykernel cartopy nc-time-axis jupyter\n",
"```\n"
"\n",
"Then to select the kernel `xcdat_notebook_0.7.0` in Jupyter to use this kernel.\n"
]
},
{
Expand Down
51 changes: 26 additions & 25 deletions docs/examples/introduction-to-xcdat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,23 @@
}
},
"source": [
"## Notebook Setup\n",
"### Notebook Kernel Setup\n",
"\n",
"Create an Anaconda environment for this notebook using the command below, then select the\n",
"kernel in Jupyter.\n",
"Users can [install their own instance of xcdat](../getting-started-guide/installation.rst) and follow these examples using their own environment (e.g., with VS Code, Jupyter, Spyder, iPython) or [enable xcdat with existing JupyterHub instances](../getting-started-guide/getting-started-hpc-jupyter.rst).\n",
"\n",
"First, create the conda environment:\n",
"\n",
"```bash\n",
"conda create -n xcdat_notebook_0.7.0 -c conda-forge xcdat=0.7.0 xesmf matplotlib ipython ipykernel cartopy nc-time-axis gsw-xarray jupyter\n",
"```\n",
"\n",
"Then install the kernel from the `xcdat_notebook_0.7.0` environment using `ipykernel` and name the kernel with the display name (e.g., `xcdat_notebook_0.7.0`):\n",
"\n",
"```bash\n",
"conda create -n xcdat_notebook -c conda-forge python xarray netcdf4 xcdat xesmf matplotlib nc-time-axis jupyter\n",
"python -m ipykernel install --user --name xcdat_notebook_0.7.0 --display-name xcdat_notebook_0.7.0\n",
"```\n",
"\n",
"- `xesmf` is required for horizontal regridding with xESMF\n",
"- `matplotlib` is an optional dependency required for plotting with xarray\n",
"- `nc-time-axis` is an optional dependency required for `matplotlib` to plot `cftime` coordinates\n"
"Then to select the kernel `xcdat_notebook_0.7.0` in Jupyter to use this kernel.\n"
]
},
{
Expand Down Expand Up @@ -1722,12 +1727,7 @@
"\n",
"filepath = \"https://esgf-data1.llnl.gov/thredds/dodsC/css03_data/CMIP6/CMIP/CSIRO/ACCESS-ESM1-5/historical/r10i1p1f1/Amon/tas/gn/v20200605/tas_Amon_ACCESS-ESM1-5_historical_r10i1p1f1_gn_185001-201412.nc\"\n",
"\n",
"ds = xc.open_dataset(\n",
" filepath,\n",
" add_bounds=True,\n",
" decode_times=True,\n",
" center_times=True\n",
")\n",
"ds = xc.open_dataset(filepath, add_bounds=True, decode_times=True, center_times=True)\n",
"\n",
"# Unit adjustment from Kelvin to Celcius.\n",
"ds[\"tas\"] = ds.tas - 273.15"
Expand Down Expand Up @@ -2726,7 +2726,7 @@
}
],
"source": [
"ds_trop_avg = ds.spatial.average(\"tas\", axis=[\"X\",\"Y\"], lat_bounds=(-25,25))\n",
"ds_trop_avg = ds.spatial.average(\"tas\", axis=[\"X\", \"Y\"], lat_bounds=(-25, 25))\n",
"ds_trop_avg.tas"
]
},
Expand Down Expand Up @@ -3885,11 +3885,15 @@
"fig, axes = plt.subplots(ncols=2, figsize=(16, 6))\n",
"\n",
"input_grid = ds.regridder.grid\n",
"input_grid.plot.scatter(x='lon', y='lat', s=5, ax=axes[0], add_colorbar=False, cmap=plt.cm.RdBu)\n",
"axes[0].set_title('Input Grid')\n",
"input_grid.plot.scatter(\n",
" x=\"lon\", y=\"lat\", s=5, ax=axes[0], add_colorbar=False, cmap=plt.cm.RdBu\n",
")\n",
"axes[0].set_title(\"Input Grid\")\n",
"\n",
"output_grid.plot.scatter(x='lon', y='lat', s=5, ax=axes[1], add_colorbar=False, cmap=plt.cm.RdBu)\n",
"axes[1].set_title('Output Grid')\n",
"output_grid.plot.scatter(\n",
" x=\"lon\", y=\"lat\", s=5, ax=axes[1], add_colorbar=False, cmap=plt.cm.RdBu\n",
")\n",
"axes[1].set_title(\"Output Grid\")\n",
"\n",
"plt.tight_layout()"
]
Expand Down Expand Up @@ -3919,7 +3923,7 @@
"outputs": [],
"source": [
"# xesmf supports \"bilinear\", \"conservative\", \"nearest_s2d\", \"nearest_d2s\", and \"patch\"\n",
"output = ds.regridder.horizontal('tas', output_grid, tool='xesmf', method='bilinear')"
"output = ds.regridder.horizontal(\"tas\", output_grid, tool=\"xesmf\", method=\"bilinear\")"
]
},
{
Expand All @@ -3946,10 +3950,10 @@
"fig, axes = plt.subplots(ncols=2, figsize=(16, 4))\n",
"\n",
"ds.tas.isel(time=0).plot(ax=axes[0])\n",
"axes[0].set_title('Input data')\n",
"axes[0].set_title(\"Input data\")\n",
"\n",
"output.tas.isel(time=0).plot(ax=axes[1])\n",
"axes[1].set_title('Output data')\n",
"axes[1].set_title(\"Output data\")\n",
"\n",
"plt.tight_layout()"
]
Expand Down Expand Up @@ -4041,10 +4045,7 @@
"# Use .chunk() to activate Dask arrays\n",
"# NOTE: `open_mfdataset()` automatically chunks by the number of files, which\n",
"# might not be optimal.\n",
"ds = xc.open_dataset(\n",
" filepath,\n",
" chunks={\"time\": \"auto\"}\n",
")"
"ds = xc.open_dataset(filepath, chunks={\"time\": \"auto\"})"
]
},
{
Expand Down
30 changes: 17 additions & 13 deletions docs/examples/regridding-horizontal.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,38 @@
"## Other Resources\n",
"\n",
"- [xESMF documentation](https://xesmf.readthedocs.io/en/latest/)\n",
"- The data used in this example can be found through the [Earth System Grid Federation (ESGF) search portal](https://aims2.llnl.gov/search).\n",
"\n",
"## Overview\n",
"\n",
"We'll cover horizontal regridding using the `xESMF` and `Regrid2` tools as well as various methods supported by `xESMF`.\n",
"\n",
"It should be noted that `Regrid2` treats the grid cells as being flat.\n"
"It should be noted that `Regrid2` treats the grid cells as being flat.\n",
"\n",
"The data used in this example can be found through the [Earth System Grid Federation (ESGF) search portal](https://aims2.llnl.gov/search).\n"
]
},
{
"cell_type": "markdown",
"id": "be06f4de",
"metadata": {},
"source": [
"## Notebook Setup\n",
"### Notebook Kernel Setup\n",
"\n",
"Users can [install their own instance of xcdat](../getting-started-guide/installation.rst) and follow these examples using their own environment (e.g., with VS Code, Jupyter, Spyder, iPython) or [enable xcdat with existing JupyterHub instances](../getting-started-guide/getting-started-hpc-jupyter.rst).\n",
"\n",
"Create an Anaconda environment for this notebook using the command below, \n",
"First, create the conda environment:\n",
"\n",
"```bash\n",
"conda create -n xcdat_notebook -c conda-forge xcdat xesmf matplotlib ipython ipykernel cartopy nc-time-axis jupyter\n",
"conda create -n xcdat_notebook_0.7.0 -c conda-forge xcdat=0.7.0 xesmf matplotlib ipython ipykernel cartopy nc-time-axis gsw-xarray jupyter\n",
"```\n",
"Then create a kernel in your xcdat conda environment using ipykernel and name the kernel with the display name, e.g. `xc070`:\n",
"\n",
"Then install the kernel from the `xcdat_notebook_0.7.0` environment using `ipykernel` and name the kernel with the display name (e.g., `xcdat_notebook_0.7.0`):\n",
"\n",
"```bash\n",
"python -m ipykernel install --user --name xcdat_notebook --display-name xc070\n",
"python -m ipykernel install --user --name xcdat_notebook_0.7.0 --display-name xcdat_notebook_0.7.0\n",
"```\n",
"Then to select the kernel `xc070` in Jupyter to use the xcdat enviroment.\n",
"- `xesmf` is required for horizontal regridding with xESMF\n",
"- `matplotlib` is an optional dependency required for plotting with xarray\n",
"- `nc-time-axis` is an optional dependency required for `matplotlib` to plot `cftime` coordinates\n"
"\n",
"Then to select the kernel `xcdat_notebook_0.7.0` in Jupyter to use this kernel.\n"
]
},
{
Expand All @@ -60,11 +63,12 @@
},
"outputs": [],
"source": [
"#%matplotlib inline\n",
"# %matplotlib inline\n",
"\n",
"import os\n",
"import sys\n",
"os.environ[\"ESMFMKFILE\"] = sys.prefix + \"/lib/esmf.mk\" # TODO remove after esmf>=8.5\n",
"\n",
"os.environ[\"ESMFMKFILE\"] = sys.prefix + \"/lib/esmf.mk\" # TODO remove after esmf>=8.5\n",
"import xesmf"
]
},
Expand Down
62 changes: 31 additions & 31 deletions docs/examples/regridding-vertical.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,23 @@
"id": "60252713",
"metadata": {},
"source": [
"## Notebook Setup\n",
"### Notebook Kernel Setup\n",
"\n",
"Create an Anaconda environment for this notebook using the command below,\n",
"```bash\n",
"Users can [install their own instance of xcdat](../getting-started-guide/installation.rst) and follow these examples using their own environment (e.g., with VS Code, Jupyter, Spyder, iPython) or [enable xcdat with existing JupyterHub instances](../getting-started-guide/getting-started-hpc-jupyter.rst).\n",
"\n",
"First, create the conda environment:\n",
"\n",
"conda create -n xcdat_notebook -c conda-forge xcdat xesmf matplotlib ipython ipykernel cartopy nc-time-axis gsw-xarray jupyter\n",
"```\n",
"Activate the conda env with: \n",
"```bash\n",
"conda activate xcdat_notebook\n",
"conda create -n xcdat_notebook_0.7.0 -c conda-forge xcdat=0.7.0 xesmf matplotlib ipython ipykernel cartopy nc-time-axis gsw-xarray jupyter\n",
"```\n",
"Then create a kernel in your xcdat conda environment using `ipykernel` and name the kernel with the display name, e.g. `xc070`:\n",
"```bash\n",
"\n",
"python -m ipykernel install --user --name xcdat_notebook --display-name xc070\n",
"Then install the kernel from the `xcdat_notebook_0.7.0` environment using `ipykernel` and name the kernel with the display name (e.g., `xcdat_notebook_0.7.0`):\n",
"\n",
"```bash\n",
"python -m ipykernel install --user --name xcdat_notebook_0.7.0 --display-name xcdat_notebook_0.7.0\n",
"```\n",
"Then to select the kernel `xc070` in Jupyter to use the xcdat enviroment.\n",
"\n",
"- `xesmf` is required for horizontal regridding with xESMF\n",
"- `matplotlib` is an optional dependency required for plotting with xarray\n",
"- `nc-time-axis` is an optional dependency required for `matplotlib` to plot `cftime` coordinates\n",
"- `gsw_xarray` is a wrapper for GSW-Python:the Python implementation of the Gibbs SeaWater (GSW) Oceanographic Toolbox of TEOS-10\n"
"Then to select the kernel `xcdat_notebook_0.7.0` in Jupyter to use this kernel.\n"
]
},
{
Expand Down Expand Up @@ -1729,9 +1724,7 @@
}
],
"source": [
"output_grid = xcdat.create_grid(\n",
" z=xcdat.create_axis(\"lev\", np.linspace(5, 537, 10))\n",
")\n",
"output_grid = xcdat.create_grid(z=xcdat.create_axis(\"lev\", np.linspace(5, 537, 10)))\n",
"\n",
"output_grid"
]
Expand Down Expand Up @@ -1847,11 +1840,11 @@
}
],
"source": [
"density_grid = xcdat.create_grid(\n",
" z=xcdat.create_axis(\"lev\", np.linspace(6, 26, 40))\n",
")\n",
"density_grid = xcdat.create_grid(z=xcdat.create_axis(\"lev\", np.linspace(6, 26, 40)))\n",
"\n",
"output = ds.regridder.vertical(\"so\", density_grid, tool=\"xgcm\", method=\"linear\", target_data=\"dens\")\n",
"output = ds.regridder.vertical(\n",
" \"so\", density_grid, tool=\"xgcm\", method=\"linear\", target_data=\"dens\"\n",
")\n",
"\n",
"output.so.isel(time=0).mean(dim=\"nlon\").plot()\n",
"plt.gca().invert_yaxis()"
Expand Down Expand Up @@ -1909,7 +1902,13 @@
"source": [
"ds_olev = ds.cf.bounds_to_vertices(\"lev\").rename({\"lev_vertices\": \"olev\"})\n",
"\n",
"output = ds_olev.regridder.vertical(\"so\", output_grid, tool=\"xgcm\", method=\"conservative\", grid_positions={\"center\": \"lev\", \"outer\": \"olev\"})\n",
"output = ds_olev.regridder.vertical(\n",
" \"so\",\n",
" output_grid,\n",
" tool=\"xgcm\",\n",
" method=\"conservative\",\n",
" grid_positions={\"center\": \"lev\", \"outer\": \"olev\"},\n",
")\n",
"\n",
"output.so.isel(time=0).sel(lev=0, method=\"nearest\").plot()"
]
Expand Down Expand Up @@ -1940,8 +1939,8 @@
"outputs": [],
"source": [
"# Url of data from the E3SM model in CMIP6\n",
"url_ta = 'https://esgf-data2.llnl.gov/thredds/dodsC/user_pub_work/CMIP6/CMIP/E3SM-Project/E3SM-2-0/historical/r1i1p1f1/Amon/ta/gr/v20220830/ta_Amon_E3SM-2-0_historical_r1i1p1f1_gr_185001-189912.nc'\n",
"url_cl = 'https://esgf-data2.llnl.gov/thredds/dodsC/user_pub_work/CMIP6/CMIP/E3SM-Project/E3SM-2-0/historical/r1i1p1f1/Amon/cl/gr/v20220830/cl_Amon_E3SM-2-0_historical_r1i1p1f1_gr_185001-189912.nc'\n",
"url_ta = \"https://esgf-data2.llnl.gov/thredds/dodsC/user_pub_work/CMIP6/CMIP/E3SM-Project/E3SM-2-0/historical/r1i1p1f1/Amon/ta/gr/v20220830/ta_Amon_E3SM-2-0_historical_r1i1p1f1_gr_185001-189912.nc\"\n",
"url_cl = \"https://esgf-data2.llnl.gov/thredds/dodsC/user_pub_work/CMIP6/CMIP/E3SM-Project/E3SM-2-0/historical/r1i1p1f1/Amon/cl/gr/v20220830/cl_Amon_E3SM-2-0_historical_r1i1p1f1_gr_185001-189912.nc\"\n",
"\n",
"ds_ta = xcdat.open_dataset(url_ta, chunks={\"time\": 4}, add_bounds=[\"Z\"])\n",
"ds_cl = xcdat.open_dataset(url_cl, chunks={\"time\": 4})"
Expand Down Expand Up @@ -2371,9 +2370,7 @@
}
],
"source": [
"output_grid = xcdat.create_grid(\n",
" z=xcdat.create_axis(\"lev\", np.linspace(100000, 1, 13))\n",
")\n",
"output_grid = xcdat.create_grid(z=xcdat.create_axis(\"lev\", np.linspace(100000, 1, 13)))\n",
"\n",
"output_grid"
]
Expand Down Expand Up @@ -3072,7 +3069,8 @@
"source": [
"# Build hybrid pressure coordinate\n",
"def hybrid_coordinate(p0, a, b, ps, **kwargs):\n",
" return a*p0 + b*ps\n",
" return a * p0 + b * ps\n",
"\n",
"\n",
"pressure = hybrid_coordinate(**ds_cl.data_vars)\n",
"\n",
Expand Down Expand Up @@ -3111,7 +3109,9 @@
" z=xcdat.create_axis(\"lev\", np.linspace(100000, 1, 13))\n",
")\n",
"\n",
"output_cl = ds_cl.regridder.vertical(\"cl\", new_pressure_grid, method=\"linear\", target_data=pressure)\n",
"output_cl = ds_cl.regridder.vertical(\n",
" \"cl\", new_pressure_grid, method=\"linear\", target_data=pressure\n",
")\n",
"\n",
"output_cl.cl.isel(time=0, lev=0).plot()"
]
Expand All @@ -3134,7 +3134,7 @@
}
],
"source": [
"output_cl.cl.isel(time=0).mean(dim='lon').plot()\n",
"output_cl.cl.isel(time=0).mean(dim=\"lon\").plot()\n",
"plt.gca().invert_yaxis()"
]
},
Expand Down
Loading

0 comments on commit 7745757

Please sign in to comment.