{ "cells": [ { "cell_type": "markdown", "id": "d741d57e-03a1-4c35-b6af-6273992cdb03", "metadata": {}, "source": [ "# Explore the National Water Model Reanalysis v2.1 \n", "\n", "\n", "### CASE 1: each HDF file contains chunks in multiple variables; doesn't need h5py\n", "\n", "370k files." ] }, { "cell_type": "code", "execution_count": 1, "id": "cab2ddcf", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/mdurant/conda/envs/py38/lib/python3.8/site-packages/xarray/conventions.py:512: SerializationWarning: variable 'qBtmVertRunoff' has multiple fill values {-9999000, 0}, decoding all values to NaN.\n", " new_vars[k] = decode_cf_variable(\n", "/Users/mdurant/conda/envs/py38/lib/python3.8/site-packages/xarray/conventions.py:512: SerializationWarning: variable 'qBucket' has multiple fill values {-999900000, 0}, decoding all values to NaN.\n", " new_vars[k] = decode_cf_variable(\n", "/Users/mdurant/conda/envs/py38/lib/python3.8/site-packages/xarray/conventions.py:512: SerializationWarning: variable 'qSfcLatRunoff' has multiple fill values {-999900000, 0}, decoding all values to NaN.\n", " new_vars[k] = decode_cf_variable(\n", "/Users/mdurant/conda/envs/py38/lib/python3.8/site-packages/xarray/conventions.py:512: SerializationWarning: variable 'q_lateral' has multiple fill values {0, -99990}, decoding all values to NaN.\n", " new_vars[k] = decode_cf_variable(\n", "/Users/mdurant/conda/envs/py38/lib/python3.8/site-packages/xarray/conventions.py:512: SerializationWarning: variable 'streamflow' has multiple fill values {0, -999900}, decoding all values to NaN.\n", " new_vars[k] = decode_cf_variable(\n", "/Users/mdurant/conda/envs/py38/lib/python3.8/site-packages/xarray/conventions.py:512: SerializationWarning: variable 'velocity' has multiple fill values {0, -999900}, decoding all values to NaN.\n", " new_vars[k] = decode_cf_variable(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 38.5 s, sys: 1.56 s, total: 40.1 s\n", "Wall time: 45.3 s\n" ] } ], "source": [ "%%time\n", "import fsspec\n", "import xarray as xr\n", "mapper = fsspec.get_mapper(\n", " \"reference://\",\n", " fo=\"s3://esip-qhub-public/noaa/nwm/nwm_reanalysis.json.zst\",\n", " target_options={\n", " \"anon\": True,\n", " \"compression\": \"zstd\"\n", " },\n", " remote_options={\"anon\": True}\n", ")\n", "ds = xr.open_dataset(mapper, engine=\"zarr\", backend_kwargs={\"consolidated\": False})" ] }, { "cell_type": "code", "execution_count": 2, "id": "c656c93e-9c10-4676-a8c0-3e7f24306f70", "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: 367439, feature_id: 2776738)\n",
       "Coordinates:\n",
       "  * feature_id      (feature_id) float64 101.0 179.0 181.0 ... 1.18e+09 1.18e+09\n",
       "    latitude        (feature_id) float32 ...\n",
       "    longitude       (feature_id) float32 ...\n",
       "  * time            (time) datetime64[ns] 1979-02-01T01:00:00 ... 2020-12-31T...\n",
       "Data variables:\n",
       "    elevation       (time, feature_id) float32 ...\n",
       "    order           (time, feature_id) float64 ...\n",
       "    qBtmVertRunoff  (time, feature_id) float64 ...\n",
       "    qBucket         (time, feature_id) float64 ...\n",
       "    qSfcLatRunoff   (time, feature_id) float64 ...\n",
       "    q_lateral       (time, feature_id) float64 ...\n",
       "    streamflow      (time, feature_id) float64 ...\n",
       "    velocity        (time, feature_id) float64 ...\n",
       "Attributes: (12/19)\n",
       "    Conventions:                CF-1.6\n",
       "    TITLE:                      OUTPUT FROM WRF-Hydro v5.2.0-beta2\n",
       "    _NCProperties:              version=2,netcdf=4.7.4,hdf5=1.10.7,\n",
       "    cdm_datatype:               Station\n",
       "    code_version:               v5.2.0-beta2\n",
       "    dev:                        dev_ prefix indicates development/internal me...\n",
       "    ...                         ...\n",
       "    model_output_type:          channel_rt\n",
       "    model_output_valid_time:    1979-02-01_01:00:00\n",
       "    model_total_valid_times:    1416\n",
       "    proj4:                      +proj=lcc +units=m +a=6370000.0 +b=6370000.0 ...\n",
       "    station_dimension:          feature_id\n",
       "    stream_order_output:        1
" ], "text/plain": [ "\n", "Dimensions: (time: 367439, feature_id: 2776738)\n", "Coordinates:\n", " * feature_id (feature_id) float64 101.0 179.0 181.0 ... 1.18e+09 1.18e+09\n", " latitude (feature_id) float32 ...\n", " longitude (feature_id) float32 ...\n", " * time (time) datetime64[ns] 1979-02-01T01:00:00 ... 2020-12-31T...\n", "Data variables:\n", " elevation (time, feature_id) float32 ...\n", " order (time, feature_id) float64 ...\n", " qBtmVertRunoff (time, feature_id) float64 ...\n", " qBucket (time, feature_id) float64 ...\n", " qSfcLatRunoff (time, feature_id) float64 ...\n", " q_lateral (time, feature_id) float64 ...\n", " streamflow (time, feature_id) float64 ...\n", " velocity (time, feature_id) float64 ...\n", "Attributes: (12/19)\n", " Conventions: CF-1.6\n", " TITLE: OUTPUT FROM WRF-Hydro v5.2.0-beta2\n", " _NCProperties: version=2,netcdf=4.7.4,hdf5=1.10.7,\n", " cdm_datatype: Station\n", " code_version: v5.2.0-beta2\n", " dev: dev_ prefix indicates development/internal me...\n", " ... ...\n", " model_output_type: channel_rt\n", " model_output_valid_time: 1979-02-01_01:00:00\n", " model_total_valid_times: 1416\n", " proj4: +proj=lcc +units=m +a=6370000.0 +b=6370000.0 ...\n", " station_dimension: feature_id\n", " stream_order_output: 1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds" ] }, { "cell_type": "code", "execution_count": 3, "id": "e196d737-9c7c-48c3-a5f0-44e390375426", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [ { "data": { "text/plain": [ "(61.21695740624, 8.162254671856)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.nbytes/1e12, ds.streamflow.nbytes/1e12 # How many terabytes total/in a single variable" ] }, { "cell_type": "markdown", "id": "fa498305-a6ad-419d-a2dc-a9584eb4510c", "metadata": {}, "source": [ "#### Read and plot streamflow for a specific time \n", "The local National Weather Service office in Houston observed all-time record daily rainfall accumulations on both August 26 and 27, measured at 14.4 in (370 mm) and 16.08 in (408 mm) respectively" ] }, { "cell_type": "code", "execution_count": null, "id": "8b4117e1-7b6b-40bd-aa5c-1530828dd257", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "import hvplot.pandas\n", "import geoviews as gv\n", "from holoviews.operation.datashader import rasterize\n", "import cartopy.crs as ccrs\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "id": "d0de6dd8-9ec8-476e-bbee-59e433ec0017", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "ds1 = ds.sel(time='2017-08-27 18:00:00', method='nearest')" ] }, { "cell_type": "code", "execution_count": null, "id": "d1b32143-bfa5-4803-922a-baf937840904", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "var = 'streamflow'" ] }, { "cell_type": "code", "execution_count": null, "id": "ad8edc51-f843-4c5a-8207-d6d2e24676f0", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "df = ds1[var].to_pandas().to_frame()" ] }, { "cell_type": "code", "execution_count": null, "id": "301888d3-a29d-4cf9-839c-a31ecdd88759", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "date_title = pd.to_datetime(ds1.time.values).strftime('%Y-%m-%d %H-%M-%D')\n", "date_title = f'{var}: {date_title}'\n", "date_title" ] }, { "cell_type": "code", "execution_count": null, "id": "2ff4f769-e030-444e-a489-96075f7d478c", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "df = df.assign(latitude=ds.latitude)\n", "df = df.assign(longitude=ds.longitude)\n", "df.rename(columns={0: var}, inplace=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "fe8b09e6-dcab-4063-9778-67f6c1c2e938", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "p = df.hvplot.points(x='longitude', y='latitude', geo=True,\n", " c=var, colorbar=True, size=14, label=date_title)\n", "g = rasterize(p, aggregator='mean', x_sampling=0.02, \n", " y_sampling=0.02, width=500).opts(tools=['hover'], \n", " aspect='equal', logz=True, cmap='viridis', clim=(1e-2, np.nan))\n", "g * gv.tile_sources.OSM" ] }, { "cell_type": "code", "execution_count": null, "id": "c9b3f239", "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.8" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }