{ "cells": [ { "cell_type": "markdown", "id": "2bf103d2-0215-4e91-828b-76ca0ca34dea", "metadata": {}, "source": [ "## Loading 2D fields into yt \n", "\n", "`yt_xarray` can also load in 2D fields from xarray into a yt dataset. To do so, all you have to do is provide a selection dictionary that will reduce your chosen field down to 2D. \n", "\n", "First, let's import and create a test dataset with dimensions that yt will not know:" ] }, { "cell_type": "code", "execution_count": 1, "id": "6310866a-5207-4bab-9feb-408038f2d78a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:   (x: 15, y: 10, z: 15, time: 5)\n",
       "Coordinates:\n",
       "  * x         (x) float64 0.0 0.07143 0.1429 0.2143 ... 0.7857 0.8571 0.9286 1.0\n",
       "  * y         (y) float64 0.0 0.1111 0.2222 0.3333 ... 0.6667 0.7778 0.8889 1.0\n",
       "  * z         (z) float64 0.0 0.07143 0.1429 0.2143 ... 0.7857 0.8571 0.9286 1.0\n",
       "  * time      (time) float64 0.0 0.25 0.5 0.75 1.0\n",
       "Data variables:\n",
       "    temp      (x, y, z) float64 0.326 0.4565 0.3212 ... 0.5516 0.8988 0.3817\n",
       "    precip    (x, y) float64 0.2222 0.153 0.6116 0.9373 ... 0.2619 0.4644 0.4776\n",
       "    precip_t  (x, y, time) float64 0.06265 0.5747 0.3099 ... 0.6112 0.5412\n",
       "Attributes:\n",
       "    geospatial_vertical_units:  m
" ], "text/plain": [ "\n", "Dimensions: (x: 15, y: 10, z: 15, time: 5)\n", "Coordinates:\n", " * x (x) float64 0.0 0.07143 0.1429 0.2143 ... 0.7857 0.8571 0.9286 1.0\n", " * y (y) float64 0.0 0.1111 0.2222 0.3333 ... 0.6667 0.7778 0.8889 1.0\n", " * z (z) float64 0.0 0.07143 0.1429 0.2143 ... 0.7857 0.8571 0.9286 1.0\n", " * time (time) float64 0.0 0.25 0.5 0.75 1.0\n", "Data variables:\n", " temp (x, y, z) float64 0.326 0.4565 0.3212 ... 0.5516 0.8988 0.3817\n", " precip (x, y) float64 0.2222 0.153 0.6116 0.9373 ... 0.2619 0.4644 0.4776\n", " precip_t (x, y, time) float64 0.06265 0.5747 0.3099 ... 0.6112 0.5412\n", "Attributes:\n", " geospatial_vertical_units: m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import xarray as xr\n", "import yt_xarray\n", "import yt\n", "import numpy as np\n", "from yt_xarray.sample_data import load_random_xr_data\n", "\n", "fields = {'temp': ('x', 'y', 'z'), \n", " 'precip': ('x', 'y'),\n", " 'precip_t': ('x', 'y', 'time')}\n", "dims = {'x': (0,1,15), 'y': (0, 1, 10), 'z': (0, 1, 15), 'time': (0, 1, 5)}\n", "ds = load_random_xr_data(fields, dims, length_unit='m')\n", "ds" ] }, { "cell_type": "markdown", "id": "b3ce590a-4b73-477b-8d89-78407875c8b6", "metadata": {}, "source": [ "loading the `\"temp\"` field without any selection dictionary will load the full 3D field: " ] }, { "cell_type": "code", "execution_count": 2, "id": "d0305e35-d78d-40f6-87ef-643fce9f8335", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt_xarray : [INFO ] 2023-02-06 12:24:49,168: Inferred geometry type is cartesian. To override, use ds.yt.set_geometry\n", "yt_xarray : [INFO ] 2023-02-06 12:24:49,170: Attempting to detect if yt_xarray will require field interpolation:\n", "yt_xarray : [INFO ] 2023-02-06 12:24:49,170: Cartesian geometry on uniform grid: yt_xarray will not interpolate.\n", "yt : [INFO ] 2023-02-06 12:24:49,261 Parameters: current_time = 0.0\n", "yt : [INFO ] 2023-02-06 12:24:49,262 Parameters: domain_dimensions = [15 10 15]\n", "yt : [INFO ] 2023-02-06 12:24:49,263 Parameters: domain_left_edge = [-0.03571429 -0.05555556 -0.03571429]\n", "yt : [INFO ] 2023-02-06 12:24:49,264 Parameters: domain_right_edge = [1.03571429 1.05555556 1.03571429]\n", "yt : [INFO ] 2023-02-06 12:24:49,265 Parameters: cosmological_simulation = 0\n" ] } ], "source": [ "yt_ds = ds.yt.load_grid(fields=[\"temp\",], length_unit = 'km')" ] }, { "cell_type": "code", "execution_count": 3, "id": "69fd8566-500a-405c-88b9-350bc443a321", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.07142857 1.11111111 1.07142857] code_length\n" ] } ], "source": [ "print(yt_ds.domain_width)" ] }, { "cell_type": "markdown", "id": "1dba6b39-42d7-4698-863f-65aae033633a", "metadata": {}, "source": [ "loading in the `\"precip\"` field, which is two dimensional, which load in the field with a dummy 3rd dimension:" ] }, { "cell_type": "code", "execution_count": 4, "id": "a7ad863e-5670-4463-b5fc-9b439f411824", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt_xarray : [INFO ] 2023-02-06 12:24:49,284: Attempting to detect if yt_xarray will require field interpolation:\n", "yt_xarray : [INFO ] 2023-02-06 12:24:49,285: Cartesian geometry on uniform grid: yt_xarray will not interpolate.\n", "yt : [INFO ] 2023-02-06 12:24:49,367 Parameters: current_time = 0.0\n", "yt : [INFO ] 2023-02-06 12:24:49,368 Parameters: domain_dimensions = [15 10 1]\n", "yt : [INFO ] 2023-02-06 12:24:49,369 Parameters: domain_left_edge = [-0.03571429 -0.05555556 -0.5 ]\n", "yt : [INFO ] 2023-02-06 12:24:49,371 Parameters: domain_right_edge = [1.03571429 1.05555556 0.5 ]\n", "yt : [INFO ] 2023-02-06 12:24:49,372 Parameters: cosmological_simulation = 0\n" ] } ], "source": [ "yt_ds = ds.yt.load_grid(fields=[\"precip\",], length_unit = 'km')" ] }, { "cell_type": "code", "execution_count": 5, "id": "6a089a6a-3430-4ed1-b590-f9ccee5560b8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.07142857 1.11111111 1. ] code_length\n" ] } ], "source": [ "print(yt_ds.domain_width)" ] }, { "cell_type": "code", "execution_count": 6, "id": "0e5d08bd-75ce-48b8-bc22-bf6d0ef1f5eb", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt : [INFO ] 2023-02-06 12:24:49,512 xlim = -0.035714 1.035714\n", "yt : [INFO ] 2023-02-06 12:24:49,513 ylim = -0.055556 1.055556\n", "yt : [INFO ] 2023-02-06 12:24:49,513 xlim = -0.035714 1.035714\n", "yt : [INFO ] 2023-02-06 12:24:49,514 ylim = -0.055556 1.055556\n", "yt : [INFO ] 2023-02-06 12:24:49,518 Making a fixed resolution buffer of (('stream', 'precip')) 800 by 800\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "yt.SlicePlot(yt_ds, \"z\", (\"stream\", \"precip\"))" ] }, { "cell_type": "markdown", "id": "7dd8c083-9c85-411e-8eaa-6d8ce767e854", "metadata": {}, "source": [ "Finally, you can also provide a selection dictionary to reduce a field to 2D. For example, you can load select a timestep to load for `\"precip_t\"` with:" ] }, { "cell_type": "code", "execution_count": 7, "id": "508a7ae2-48d5-43a6-bb2a-d7f60f3439bb", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt_xarray : [INFO ] 2023-02-06 12:24:50,271: Attempting to detect if yt_xarray will require field interpolation:\n", "yt_xarray : [INFO ] 2023-02-06 12:24:50,273: Cartesian geometry on uniform grid: yt_xarray will not interpolate.\n", "yt : [INFO ] 2023-02-06 12:24:50,340 Parameters: current_time = 0.0\n", "yt : [INFO ] 2023-02-06 12:24:50,341 Parameters: domain_dimensions = [15 10 1]\n", "yt : [INFO ] 2023-02-06 12:24:50,343 Parameters: domain_left_edge = [-0.03571429 -0.05555556 -0.5 ]\n", "yt : [INFO ] 2023-02-06 12:24:50,344 Parameters: domain_right_edge = [1.03571429 1.05555556 0.5 ]\n", "yt : [INFO ] 2023-02-06 12:24:50,345 Parameters: cosmological_simulation = 0\n" ] } ], "source": [ "yt_ds = ds.yt.load_grid(fields=[\"precip_t\",], length_unit = 'km', geometry=\"cartesian\", sel_dict={'time':0})" ] }, { "cell_type": "code", "execution_count": 8, "id": "26ae80e5-cb0d-49ff-aa01-8b4fdb1f14dc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.07142857 1.11111111 1. ] code_length\n" ] } ], "source": [ "print(yt_ds.domain_width)" ] }, { "cell_type": "code", "execution_count": 9, "id": "6b69c409-e49e-4bf4-9e39-2cb18753c099", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "yt : [INFO ] 2023-02-06 12:24:50,453 xlim = -0.035714 1.035714\n", "yt : [INFO ] 2023-02-06 12:24:50,454 ylim = -0.055556 1.055556\n", "yt : [INFO ] 2023-02-06 12:24:50,455 xlim = -0.035714 1.035714\n", "yt : [INFO ] 2023-02-06 12:24:50,455 ylim = -0.055556 1.055556\n", "yt : [INFO ] 2023-02-06 12:24:50,460 Making a fixed resolution buffer of (('stream', 'precip_t')) 800 by 800\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "yt.SlicePlot(yt_ds, \"z\", (\"stream\", \"precip_t\"))" ] }, { "cell_type": "code", "execution_count": null, "id": "61436675-779c-4b67-b9c7-7d96a5d25cfa", "metadata": {}, "outputs": [], "source": [] } ], "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.9.0" } }, "nbformat": 4, "nbformat_minor": 5 }