{ "cells": [ { "cell_type": "markdown", "id": "c94bbff9", "metadata": {}, "source": [ "# General Dataset Utilities\n", "\n", "Authors:\n", "\n", "* [Tom Vo](https://github.com/tomvothecoder/)\n", "* [Stephen Po-Chedley](https://github.com/pochedls/)\n", "\n", "\n", "Date: 05/26/22" ] }, { "cell_type": "markdown", "id": "ef67fe43", "metadata": {}, "source": [ "## Overview\n", "\n", "This notebook demonstrates the use of general utility methods available in `xcdat`, including\n", "the reorientation of the longitude axis, centering of time coordinates using time bounds, and\n", "adding and getting bounds." ] }, { "cell_type": "code", "execution_count": 1, "id": "0b4e8461", "metadata": {}, "outputs": [], "source": [ "import xcdat" ] }, { "cell_type": "markdown", "id": "6078eb43", "metadata": {}, "source": [ "## Open a dataset\n", "\n", "Datasets can be opened and read using `open_dataset()` or `open_mfdataset()` (multi-file).\n", "\n", "Related APIs:\n", "\n", "* [xcdat.open_dataset()](../generated/xcdat.open_dataset.rst)\n", "* [xcdat.open_mfdataset()](../generated/xcdat.open_mfdataset.rst)" ] }, { "cell_type": "code", "execution_count": 2, "id": "e83b0a2b", "metadata": {}, "outputs": [], "source": [ "dataset_links = [\n", " \"https://esgf-data2.llnl.gov/thredds/dodsC/user_pub_work/E3SM/1_0/amip_1850_aeroF/1deg_atm_60-30km_ocean/atmos/180x360/time-series/mon/ens2/v3/TS_187001_189412.nc\",\n", " \"https://esgf-data2.llnl.gov/thredds/dodsC/user_pub_work/E3SM/1_0/amip_1850_aeroF/1deg_atm_60-30km_ocean/atmos/180x360/time-series/mon/ens2/v3/TS_189501_191912.nc\",\n", "]\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "e027623a", "metadata": {}, "outputs": [], "source": [ "# NOTE: Opening a multi-file dataset will result in data variables to be dask\n", "# arrays.\n", "ds = xcdat.open_mfdataset(dataset_links)" ] }, { "cell_type": "code", "execution_count": 4, "id": "37392c81", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (lat: 180, lon: 360, nbnd: 2, time: 600)\n", "Coordinates:\n", " * lat (lat) float64 -89.5 -88.5 -87.5 -86.5 ... 86.5 87.5 88.5 89.5\n", " * lon (lon) float64 0.5 1.5 2.5 3.5 4.5 ... 356.5 357.5 358.5 359.5\n", " * time (time) object 1870-02-01 00:00:00 ... 1920-01-01 00:00:00\n", "Dimensions without coordinates: nbnd\n", "Data variables:\n", " lat_bnds (lat, nbnd) float64 dask.array<chunksize=(180, 2), meta=np.ndarray>\n", " lon_bnds (lon, nbnd) float64 dask.array<chunksize=(360, 2), meta=np.ndarray>\n", " gw (lat) float64 dask.array<chunksize=(180,), meta=np.ndarray>\n", " time_bnds (time, nbnd) object dask.array<chunksize=(300, 2), meta=np.ndarray>\n", " area (lat, lon) float64 dask.array<chunksize=(180, 360), meta=np.ndarray>\n", " TS (time, lat, lon) float32 dask.array<chunksize=(300, 180, 360), meta=np.ndarray>\n", "Attributes: (12/21)\n", " ne: 30\n", " np: 4\n", " Conventions: CF-1.0\n", " source: CAM\n", " case: 20180622.DECKv1b_A2_1850aeroF.ne30_oEC.e...\n", " title: UNSET\n", " ... ...\n", " remap_script: ncremap\n", " remap_hostname: acme1\n", " remap_version: 4.9.6\n", " map_file: /export/zender1/data/maps/map_ne30np4_to...\n", " input_file: /p/user_pub/e3sm/baldwin32/workshop/amip...\n", " DODS_EXTRA.Unlimited_Dimension: time
<xarray.DataArray 'lon' (lon: 360)>\n", "array([ 0.5, 1.5, 2.5, ..., 357.5, 358.5, 359.5])\n", "Coordinates:\n", " * lon (lon) float64 0.5 1.5 2.5 3.5 4.5 ... 355.5 356.5 357.5 358.5 359.5\n", "Attributes:\n", " long_name: Longitude of Grid Cell Centers\n", " standard_name: longitude\n", " units: degrees_east\n", " axis: X\n", " valid_min: 0.0\n", " valid_max: 360.0\n", " bounds: lon_bnds
<xarray.DataArray 'lon' (lon: 360)>\n", "array([-179.5, -178.5, -177.5, ..., 177.5, 178.5, 179.5])\n", "Coordinates:\n", " * lon (lon) float64 -179.5 -178.5 -177.5 -176.5 ... 177.5 178.5 179.5\n", "Attributes:\n", " long_name: Longitude of Grid Cell Centers\n", " standard_name: longitude\n", " units: degrees_east\n", " axis: X\n", " valid_min: 0.0\n", " valid_max: 360.0\n", " bounds: lon_bnds
<xarray.DataArray 'time' (time: 600)>\n", "array([cftime.DatetimeNoLeap(1870, 2, 1, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1870, 3, 1, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1870, 4, 1, 0, 0, 0, 0, has_year_zero=True), ...,\n", " cftime.DatetimeNoLeap(1919, 11, 1, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1919, 12, 1, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1920, 1, 1, 0, 0, 0, 0, has_year_zero=True)],\n", " dtype=object)\n", "Coordinates:\n", " * time (time) object 1870-02-01 00:00:00 ... 1920-01-01 00:00:00\n", "Attributes:\n", " long_name: time\n", " bounds: time_bnds\n", " cell_methods: time: mean
<xarray.DataArray 'time' (time: 600)>\n", "array([cftime.DatetimeNoLeap(1870, 1, 16, 12, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1870, 2, 15, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1870, 3, 16, 12, 0, 0, 0, has_year_zero=True),\n", " ...,\n", " cftime.DatetimeNoLeap(1919, 10, 16, 12, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1919, 11, 16, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1919, 12, 16, 12, 0, 0, 0, has_year_zero=True)],\n", " dtype=object)\n", "Coordinates:\n", " * time (time) object 1870-01-16 12:00:00 ... 1919-12-16 12:00:00\n", "Attributes:\n", " long_name: time\n", " bounds: time_bnds\n", " cell_methods: time: mean
<xarray.DataArray 'time_bnds' (time: 600, bnds: 2)>\n", "array([[cftime.DatetimeNoLeap(1870, 1, 18, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1870, 2, 15, 0, 0, 0, 0, has_year_zero=True)],\n", " [cftime.DatetimeNoLeap(1870, 2, 15, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1870, 3, 16, 12, 0, 0, 0, has_year_zero=True)],\n", " [cftime.DatetimeNoLeap(1870, 3, 16, 12, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1870, 4, 16, 0, 0, 0, 0, has_year_zero=True)],\n", " ...,\n", " [cftime.DatetimeNoLeap(1919, 10, 16, 12, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1919, 11, 16, 0, 0, 0, 0, has_year_zero=True)],\n", " [cftime.DatetimeNoLeap(1919, 11, 16, 0, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1919, 12, 16, 12, 0, 0, 0, has_year_zero=True)],\n", " [cftime.DatetimeNoLeap(1919, 12, 16, 12, 0, 0, 0, has_year_zero=True),\n", " cftime.DatetimeNoLeap(1920, 1, 16, 12, 0, 0, 0, has_year_zero=True)]],\n", " dtype=object)\n", "Coordinates:\n", " * time (time) object 1870-02-01 00:00:00 ... 1920-01-01 00:00:00\n", "Dimensions without coordinates: bnds\n", "Attributes:\n", " xcdat_bounds: True
<xarray.Dataset>\n", "Dimensions: (lat: 180, lon: 360, time: 600)\n", "Coordinates:\n", " * lat (lat) float64 -89.5 -88.5 -87.5 -86.5 -85.5 ... 86.5 87.5 88.5 89.5\n", " * lon (lon) float64 0.5 1.5 2.5 3.5 4.5 ... 355.5 356.5 357.5 358.5 359.5\n", " * time (time) object 1870-02-01 00:00:00 ... 1920-01-01 00:00:00\n", "Data variables:\n", " gw (lat) float64 dask.array<chunksize=(180,), meta=np.ndarray>\n", " area (lat, lon) float64 dask.array<chunksize=(180, 360), meta=np.ndarray>\n", " TS (time, lat, lon) float32 dask.array<chunksize=(300, 180, 360), meta=np.ndarray>\n", "Attributes: (12/21)\n", " ne: 30\n", " np: 4\n", " Conventions: CF-1.0\n", " source: CAM\n", " case: 20180622.DECKv1b_A2_1850aeroF.ne30_oEC.e...\n", " title: UNSET\n", " ... ...\n", " remap_script: ncremap\n", " remap_hostname: acme1\n", " remap_version: 4.9.6\n", " map_file: /export/zender1/data/maps/map_ne30np4_to...\n", " input_file: /p/user_pub/e3sm/baldwin32/workshop/amip...\n", " DODS_EXTRA.Unlimited_Dimension: time
<xarray.Dataset>\n", "Dimensions: (lat: 180, lon: 360, time: 600, bnds: 2)\n", "Coordinates:\n", " * lat (lat) float64 -89.5 -88.5 -87.5 -86.5 ... 86.5 87.5 88.5 89.5\n", " * lon (lon) float64 0.5 1.5 2.5 3.5 4.5 ... 356.5 357.5 358.5 359.5\n", " * time (time) object 1870-02-01 00:00:00 ... 1920-01-01 00:00:00\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", " gw (lat) float64 dask.array<chunksize=(180,), meta=np.ndarray>\n", " area (lat, lon) float64 dask.array<chunksize=(180, 360), meta=np.ndarray>\n", " TS (time, lat, lon) float32 dask.array<chunksize=(300, 180, 360), meta=np.ndarray>\n", " lon_bnds (lon, bnds) float64 0.0 1.0 1.0 2.0 ... 358.0 359.0 359.0 360.0\n", " lat_bnds (lat, bnds) float64 -90.0 -89.0 -89.0 -88.0 ... 89.0 89.0 90.0\n", " time_bnds (time, bnds) object 1870-01-18 00:00:00 ... 1920-01-16 12:00:00\n", "Attributes: (12/21)\n", " ne: 30\n", " np: 4\n", " Conventions: CF-1.0\n", " source: CAM\n", " case: 20180622.DECKv1b_A2_1850aeroF.ne30_oEC.e...\n", " title: UNSET\n", " ... ...\n", " remap_script: ncremap\n", " remap_hostname: acme1\n", " remap_version: 4.9.6\n", " map_file: /export/zender1/data/maps/map_ne30np4_to...\n", " input_file: /p/user_pub/e3sm/baldwin32/workshop/amip...\n", " DODS_EXTRA.Unlimited_Dimension: time
<xarray.DataArray 'lat' (lat: 180)>\n", "array([-89.5, -88.5, -87.5, -86.5, -85.5, -84.5, -83.5, -82.5, -81.5, -80.5,\n", " -79.5, -78.5, -77.5, -76.5, -75.5, -74.5, -73.5, -72.5, -71.5, -70.5,\n", " -69.5, -68.5, -67.5, -66.5, -65.5, -64.5, -63.5, -62.5, -61.5, -60.5,\n", " -59.5, -58.5, -57.5, -56.5, -55.5, -54.5, -53.5, -52.5, -51.5, -50.5,\n", " -49.5, -48.5, -47.5, -46.5, -45.5, -44.5, -43.5, -42.5, -41.5, -40.5,\n", " -39.5, -38.5, -37.5, -36.5, -35.5, -34.5, -33.5, -32.5, -31.5, -30.5,\n", " -29.5, -28.5, -27.5, -26.5, -25.5, -24.5, -23.5, -22.5, -21.5, -20.5,\n", " -19.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5,\n", " -9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,\n", " 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5,\n", " 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5,\n", " 20.5, 21.5, 22.5, 23.5, 24.5, 25.5, 26.5, 27.5, 28.5, 29.5,\n", " 30.5, 31.5, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5,\n", " 40.5, 41.5, 42.5, 43.5, 44.5, 45.5, 46.5, 47.5, 48.5, 49.5,\n", " 50.5, 51.5, 52.5, 53.5, 54.5, 55.5, 56.5, 57.5, 58.5, 59.5,\n", " 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5,\n", " 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5, 79.5,\n", " 80.5, 81.5, 82.5, 83.5, 84.5, 85.5, 86.5, 87.5, 88.5, 89.5])\n", "Coordinates:\n", " * lat (lat) float64 -89.5 -88.5 -87.5 -86.5 -85.5 ... 86.5 87.5 88.5 89.5\n", "Attributes:\n", " long_name: Latitude of Grid Cell Centers\n", " standard_name: latitude\n", " units: degrees_north\n", " axis: Y\n", " valid_min: -90.0\n", " valid_max: 90.0\n", " bounds: lat_bnds