{ "cells": [ { "cell_type": "markdown", "id": "9abc7ae0-1bba-47d8-a1df-17386def470c", "metadata": {}, "source": [ "## yt_xarray and cartopy\n", "\n", "This notebook demonstrates loading a geographic xarray dataset into yt. In addition to `yt_xarray` and `xarray`, this notebook requires `cartopy` and relies on the netcdf-gridded seismic tomography model of Schmand and Humphreys 2010, available from IRIS [here](https://doi.org/10.17611/DP/9991760).\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "12f7f44b-5041-4254-9e42-29586311b9cf", "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import yt_xarray\n", "import yt\n", "from cartopy.feature import NaturalEarthFeature" ] }, { "cell_type": "markdown", "id": "c0f06a1a-7196-4da0-b41d-4298fa5644e7", "metadata": {}, "source": [ "yt_xarray provides a simple wrapper of the standard xarray `open_dataset` function that will check yt's `test_data_dir` for data if the file is not found in the local path: " ] }, { "cell_type": "code", "execution_count": 2, "id": "625a2709-5d7f-43c5-a2cf-3d5e23d6930b", "metadata": {}, "outputs": [], "source": [ "ds = yt_xarray.open_dataset(\"IRIS/wUS-SH-2010_percent.nc\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "272662d7-65cc-426f-8a42-b41cf5a07313", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:    (depth: 19, latitude: 93, longitude: 122)\n",
       "Coordinates:\n",
       "  * depth      (depth) float32 60.0 90.0 125.0 160.0 ... 700.0 760.0 820.0 885.0\n",
       "  * latitude   (latitude) float32 27.5 27.75 28.0 28.25 ... 50.0 50.25 50.5\n",
       "  * longitude  (longitude) float32 -125.8 -125.5 -125.2 ... -96.0 -95.75 -95.5\n",
       "Data variables:\n",
       "    dvp        (depth, latitude, longitude) float32 ...\n",
       "    dvs        (depth, latitude, longitude) float32 ...\n",
       "Attributes: (12/32)\n",
       "    title:                         P and S teleseismic body-wave tomography o...\n",
       "    id:                            wUS-SH-2010_percent\n",
       "    summary:                       Teleseismic travel-time residuals from the...\n",
       "    keywords:                      seismic, tomography, compressional wave, p...\n",
       "    Conventions:                   CF-1.0\n",
       "    Metadata_Conventions:          Unidata Dataset Discovery v1.0\n",
       "    ...                            ...\n",
       "    author_email:                  bschmandt@unm.edu\n",
       "    author_institution:            Department of Earth and Planetary Science,...\n",
       "    author_url:                    \n",
       "    repository_name:               EMC\n",
       "    repository_institution:        IRIS DMC\n",
       "    repository_pid:                doi:10.17611/DP/9991760
" ], "text/plain": [ "\n", "Dimensions: (depth: 19, latitude: 93, longitude: 122)\n", "Coordinates:\n", " * depth (depth) float32 60.0 90.0 125.0 160.0 ... 700.0 760.0 820.0 885.0\n", " * latitude (latitude) float32 27.5 27.75 28.0 28.25 ... 50.0 50.25 50.5\n", " * longitude (longitude) float32 -125.8 -125.5 -125.2 ... -96.0 -95.75 -95.5\n", "Data variables:\n", " dvp (depth, latitude, longitude) float32 ...\n", " dvs (depth, latitude, longitude) float32 ...\n", "Attributes: (12/32)\n", " title: P and S teleseismic body-wave tomography o...\n", " id: wUS-SH-2010_percent\n", " summary: Teleseismic travel-time residuals from the...\n", " keywords: seismic, tomography, compressional wave, p...\n", " Conventions: CF-1.0\n", " Metadata_Conventions: Unidata Dataset Discovery v1.0\n", " ... ...\n", " author_email: bschmandt@unm.edu\n", " author_institution: Department of Earth and Planetary Science,...\n", " author_url: \n", " repository_name: EMC\n", " repository_institution: IRIS DMC\n", " repository_pid: doi:10.17611/DP/9991760" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds" ] }, { "cell_type": "markdown", "id": "c1299f4d-430b-4113-b186-7c4263b1e2ab", "metadata": {}, "source": [ "to create a yt dataset that loads all the data variables, we simply call `ds.yt.load_grid`. For this dataset, our data is on a stretched grid, meaning the spacing in each dimension varies within a dimension (i.e., the depth spacing is changes), in which case we have to supply the `use_callable=False` flag to explicitly tell yt_xarray that we are OK with loading the data into memory (one of the current limitations of loading stretched grids): " ] }, { "cell_type": "code", "execution_count": 4, "id": "6e02706d-2f34-46ee-86be-45928110988b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt_xarray : [INFO ] 2023-02-06 15:56:02,810: Inferred geometry type is geodetic. To override, use ds.yt.set_geometry\n", "yt_xarray : [INFO ] 2023-02-06 15:56:02,816: Attempting to detect if yt_xarray will require field interpolation:\n", "yt_xarray : [INFO ] 2023-02-06 15:56:02,820: stretched grid detected: yt_xarray will interpolate.\n", "yt : [INFO ] 2023-02-06 15:56:03,011 Parameters: current_time = 0.0\n", "yt : [INFO ] 2023-02-06 15:56:03,012 Parameters: domain_dimensions = [ 18 92 121]\n", "yt : [INFO ] 2023-02-06 15:56:03,014 Parameters: domain_left_edge = [ 60. 27.5 -125.75]\n", "yt : [INFO ] 2023-02-06 15:56:03,015 Parameters: domain_right_edge = [885. 50.5 -95.5]\n", "yt : [INFO ] 2023-02-06 15:56:03,016 Parameters: cosmological_simulation = 0\n" ] } ], "source": [ "yt_ds = ds.yt.load_grid(use_callable=False)" ] }, { "cell_type": "markdown", "id": "ccde9182-3221-4ade-a4e7-2b85bb1cdde6", "metadata": {}, "source": [ "From here, yt will automatically use cartopy when plotting perpendicular to the vertical axis:" ] }, { "cell_type": "code", "execution_count": 5, "id": "f8bdd9b1-ad86-459e-ab7b-5baded948653", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt : [INFO ] 2023-02-06 15:56:03,137 xlim = -125.750000 -95.500000\n", "yt : [INFO ] 2023-02-06 15:56:03,137 ylim = 27.500000 50.500000\n", "yt : [INFO ] 2023-02-06 15:56:03,138 Setting origin='native' for internal_geographic geometry.\n", "yt : [INFO ] 2023-02-06 15:56:03,139 xlim = -125.750000 -95.500000\n", "yt : [INFO ] 2023-02-06 15:56:03,140 ylim = 27.500000 50.500000\n", "yt : [INFO ] 2023-02-06 15:56:03,149 Making a fixed resolution buffer of (('stream', 'dvs')) 800 by 800\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c = yt_ds.domain_center.copy()\n", "c[0] = 150.\n", "slc = yt.SlicePlot(yt_ds, \"depth\", (\"stream\", \"dvs\"), center = c)\n", "slc.set_log((\"stream\", \"dvs\"), False)\n", "slc.set_cmap((\"stream\", \"dvs\"), \"magma_r\")\n", "slc.set_zlim((\"stream\", \"dvs\"), -8, 8)\n", "slc._setup_plots()\n", "\n", "states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',\n", " name='admin_1_states_provinces')\n", "slc[(\"stream\", \"dvs\")].axes.add_feature(states, edgecolor='gray')\n", "slc.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }