diff --git a/tds/src/main/webapp/WEB-INF/altContent/startup/jupyter_notebooks/dataset_to_xarray_notebook.ipynb b/tds/src/main/webapp/WEB-INF/altContent/startup/jupyter_notebooks/dataset_to_xarray_notebook.ipynb new file mode 100644 index 0000000000..6ed4ff7185 --- /dev/null +++ b/tds/src/main/webapp/WEB-INF/altContent/startup/jupyter_notebooks/dataset_to_xarray_notebook.ipynb @@ -0,0 +1,1108 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Unidata\n", + "\n", + "# Siphon THREDDS Jupyter Notebook - loading data into xarray\n", + "\n", + "## Dataset: {{datasetName}}\n", + "___" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Dependencies:\n", + "* *Siphon*: `pip install siphon`\n", + "* *xarray*: `pip install xarray` or 'conda install -c conda-forge xarray dask netCDF4 bottleneck'\n", + "\n", + "* *ipywidgets*:`pip install ipywidgets` or `conda install -c conda-forge ipywidgets` \n", + "* enable *ipywidgets*:\n", + " * using Juputer Notebooks: `jupyter nbextension enable --py widgetsnbextension`\n", + " * using JupyterLab:\n", + " * nodejs: `conda install nodejs`\n", + " * `jupyter labextension install @jupyter-widgets/jupyterlab-manage" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from siphon.catalog import TDSCatalog\n", + "import xarray as xr\n", + "\n", + "import ipywidgets as widgets" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "catUrl = \"{{catUrl}}\";\n", + "datasetName = \"{{datasetName}}\";" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Access a dataset\n", + "With the TDS catalog url, we can use Siphon to get the dataset named `datasetName`.\n", + "\n", + "The docs for Siphon are [here](https://unidata.github.io/siphon/latest/). " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "catalog = TDSCatalog(catUrl)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ds is a: \n" + ] + } + ], + "source": [ + "ds = catalog.datasets[datasetName].remote_access(use_xarray=True)\n", + "print('ds is a:', type(ds))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.Dataset>\n",
+       "Dimensions:                                                                                                              (time: 18, : 2, time2: 19, pressure_difference_layer: 1, pressure_difference_layer1: 1, height_above_ground_layer: 1, height_above_ground_layer1: 1, height_above_ground_layer2: 2, pressure_difference_layer2: 1, height_above_ground_layer3: 2, isobaric_layer: 1, sigma_layer: 1, pressure_difference_layer3: 2, time1: 19, y: 1377, x: 2145, isobaric1: 5, height_above_ground3: 1, isobaric: 4, height_above_ground1: 1, height_above_ground: 1, height_above_ground2: 2)\n",
+       "Coordinates: (12/22)\n",
+       "  * x                                                                                                                    (x) float32 ...\n",
+       "  * y                                                                                                                    (y) float32 ...\n",
+       "    reftime                                                                                                              datetime64[ns] ...\n",
+       "  * time                                                                                                                 (time) datetime64[ns] ...\n",
+       "  * time1                                                                                                                (time1) datetime64[ns] ...\n",
+       "  * time2                                                                                                                (time2) datetime64[ns] ...\n",
+       "    ...                                                                                                                   ...\n",
+       "  * height_above_ground3                                                                                                 (height_above_ground3) float32 ...\n",
+       "  * isobaric_layer                                                                                                       (isobaric_layer) float32 ...\n",
+       "  * sigma_layer                                                                                                          (sigma_layer) float32 ...\n",
+       "  * pressure_difference_layer3                                                                                           (pressure_difference_layer3) float32 ...\n",
+       "  * isobaric                                                                                                             (isobaric) float32 ...\n",
+       "  * isobaric1                                                                                                            (isobaric1) float32 ...\n",
+       "Dimensions without coordinates: \n",
+       "Data variables: (12/68)\n",
+       "    LambertConformal_Projection                                                                                          >i4 ...\n",
+       "    time_bounds                                                                                                          (time, ) datetime64[ns] ...\n",
+       "    time2_bounds                                                                                                         (time2, ) datetime64[ns] ...\n",
+       "    pressure_difference_layer_bounds                                                                                     (pressure_difference_layer, ) >f4 ...\n",
+       "    pressure_difference_layer1_bounds                                                                                    (pressure_difference_layer1, ) >f4 ...\n",
+       "    height_above_ground_layer_bounds                                                                                     (height_above_ground_layer, ) >f4 ...\n",
+       "    ...                                                                                                                   ...\n",
+       "    u-component_of_wind_isobaric                                                                                         (time1, isobaric1, y, x) float32 ...\n",
+       "    u-component_of_wind_height_above_ground                                                                              (time1, height_above_ground2, y, x) float32 ...\n",
+       "    u-component_storm_motion_height_above_ground_layer                                                                   (time1, height_above_ground_layer1, y, x) float32 ...\n",
+       "    v-component_of_wind_isobaric                                                                                         (time1, isobaric1, y, x) float32 ...\n",
+       "    v-component_of_wind_height_above_ground                                                                              (time1, height_above_ground2, y, x) float32 ...\n",
+       "    v-component_storm_motion_height_above_ground_layer                                                                   (time1, height_above_ground_layer1, y, x) float32 ...\n",
+       "Attributes:\n",
+       "    Originating_or_generating_Center:                                        ...\n",
+       "    Originating_or_generating_Subcenter:                                     ...\n",
+       "    GRIB_table_version:                                                      ...\n",
+       "    Type_of_generating_process:                                              ...\n",
+       "    Analysis_or_forecast_generating_process_identifier_defined_by_originating...\n",
+       "    file_format:                                                             ...\n",
+       "    Conventions:                                                             ...\n",
+       "    history:                                                                 ...\n",
+       "    featureType:                                                             ...\n",
+       "    _CoordSysBuilder:                                                        ...
" + ], + "text/plain": [ + "\n", + "Dimensions: (time: 18, : 2, time2: 19, pressure_difference_layer: 1, pressure_difference_layer1: 1, height_above_ground_layer: 1, height_above_ground_layer1: 1, height_above_ground_layer2: 2, pressure_difference_layer2: 1, height_above_ground_layer3: 2, isobaric_layer: 1, sigma_layer: 1, pressure_difference_layer3: 2, time1: 19, y: 1377, x: 2145, isobaric1: 5, height_above_ground3: 1, isobaric: 4, height_above_ground1: 1, height_above_ground: 1, height_above_ground2: 2)\n", + "Coordinates: (12/22)\n", + " * x (x) float32 ...\n", + " * y (y) float32 ...\n", + " reftime datetime64[ns] ...\n", + " * time (time) datetime64[ns] ...\n", + " * time1 (time1) datetime64[ns] ...\n", + " * time2 (time2) datetime64[ns] ...\n", + " ... ...\n", + " * height_above_ground3 (height_above_ground3) float32 ...\n", + " * isobaric_layer (isobaric_layer) float32 ...\n", + " * sigma_layer (sigma_layer) float32 ...\n", + " * pressure_difference_layer3 (pressure_difference_layer3) float32 ...\n", + " * isobaric (isobaric) float32 ...\n", + " * isobaric1 (isobaric1) float32 ...\n", + "Dimensions without coordinates: \n", + "Data variables: (12/68)\n", + " LambertConformal_Projection int32 ...\n", + " time_bounds (time, ) datetime64[ns] ...\n", + " time2_bounds (time2, ) datetime64[ns] ...\n", + " pressure_difference_layer_bounds (pressure_difference_layer, ) float32 ...\n", + " pressure_difference_layer1_bounds (pressure_difference_layer1, ) float32 ...\n", + " height_above_ground_layer_bounds (height_above_ground_layer, ) float32 ...\n", + " ... ...\n", + " u-component_of_wind_isobaric (time1, isobaric1, y, x) float32 ...\n", + " u-component_of_wind_height_above_ground (time1, height_above_ground2, y, x) float32 ...\n", + " u-component_storm_motion_height_above_ground_layer (time1, height_above_ground_layer1, y, x) float32 ...\n", + " v-component_of_wind_isobaric (time1, isobaric1, y, x) float32 ...\n", + " v-component_of_wind_height_above_ground (time1, height_above_ground2, y, x) float32 ...\n", + " v-component_storm_motion_height_above_ground_layer (time1, height_above_ground_layer1, y, x) float32 ...\n", + "Attributes:\n", + " Originating_or_generating_Center: ...\n", + " Originating_or_generating_Subcenter: ...\n", + " GRIB_table_version: ...\n", + " Type_of_generating_process: ...\n", + " Analysis_or_forecast_generating_process_identifier_defined_by_originating...\n", + " file_format: ...\n", + " Conventions: ...\n", + " history: ...\n", + " featureType: ...\n", + " _CoordSysBuilder: ..." + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## We can make a small widget to select the variable of interest" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "var_name = widgets.RadioButtons(\n", + " options=list(ds.variables),\n", + " description='Variable:')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "e52842fef1fe40b48cfd9b93400d4123", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "RadioButtons(description='Variable:', options=('LambertConformal_Projection', 'time_bounds', 'time2_bounds', '…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(var_name)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### We now can make a new xarray data array with the variable of interest:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'LambertConformal_Projection' ()>\n",
+       "array(0, dtype=int32)\n",
+       "Coordinates:\n",
+       "    reftime  datetime64[ns] 2022-11-16T14:00:00\n",
+       "Attributes:\n",
+       "    grid_mapping_name:              lambert_conformal_conic\n",
+       "    latitude_of_projection_origin:  25.0\n",
+       "    longitude_of_central_meridian:  265.0\n",
+       "    standard_parallel:              25.0\n",
+       "    earth_radius:                   6371229.0\n",
+       "    _CoordinateTransformType:       Projection\n",
+       "    _CoordinateAxisTypes:           GeoX GeoY
" + ], + "text/plain": [ + "\n", + "array(0, dtype=int32)\n", + "Coordinates:\n", + " reftime datetime64[ns] 2022-11-16T14:00:00\n", + "Attributes:\n", + " grid_mapping_name: lambert_conformal_conic\n", + " latitude_of_projection_origin: 25.0\n", + " longitude_of_central_meridian: 265.0\n", + " standard_parallel: 25.0\n", + " earth_radius: 6371229.0\n", + " _CoordinateTransformType: Projection\n", + " _CoordinateAxisTypes: GeoX GeoY" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_da = ds[var_name.value]\n", + "new_da" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Further xarray turtorial can be found [here](https://tutorial.xarray.dev/overview/xarray-in-45-min.html) and documentation is [here](https://docs.xarray.dev/en/stable/). It is a very powerful package for multi-index (x,y,z,time, etc.) datasets!" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "# your code here!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.9.13 ('ml_scratch')", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + }, + "viewer_info": { + "accepts": {}, + "description": "The TDS default viewer attempts to plot any Variable contained in the Dataset." + }, + "vscode": { + "interpreter": { + "hash": "3538842ee661b57f24cfeaeb491ecfd345bd2dd43fab6b8744c56a63a0fbbd62" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}