{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# E3SM VCS Native Grid Plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook shows how to use VCS to create a plot of data on E3SM's native grid.\n", "\n", "To [download this Jupyter Notebook](https://cdat.llnl.gov/Jupyter-notebooks/scientific/E3SM_VCS_Native_Grid_Plot/E3SM_VCS_Native_Grid_Plot.ipynb), right click on the link and choose \"Download Linked File As...\" or \"Save Link as...\". Remember where you saved the downloaded file, which should have an .ipynb extension. (You'll need to launch the Jupyter notebook or JupyterLab instance from the location where you store the notebook file.)\n", "\n", "Note: this notebook uses Python 3." ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Necessary Conda Environment\n", "Prior to running this notebook, create an appropriate conda environment by running the following commands:\n", "\n", "```conda create -n cdat82mesa-e3sm_nex -c cdat/label/v82 -c conda-forge python=3.6 cdat mesalib```\n", "\n", "```conda activate cdat82mesa-e3sm_nex```\n", "\n", "\n", "Next type the following command to add e3sm_nex to the already-activated cdat81-mesa-e3sm_nex environment:\n", "\n", "```conda install -c conda-forge -c cdat e3sm_nex```\n", "\n", "Finally, to run Jupyter Notebooks, add JupyterLab to this environment:\n", "\n", "```conda install -c conda-forge jupyterlab```\n", "\n", "Note: for the image below to display correctly, you must use a CDAT v8.2 or later environment.\n", "\n", "\n", "## Start the Jupyter Notebook\n", "\n", "To run the notebook, make sure you are in the directory containing the notebook (or in a higher directory where you can navigate down to the notebook) and type: \n", "\n", "```jupyter-notebook```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Necessary Modules/Libraries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import e3sm_nex # Library for E3SM NE30 native grid\n", "import cdms2 \n", "import vcs \n", "import requests \n", "import numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download Data\n", "The data file for this tutorial is \"**ne30_TS.nc**\" and the grid file is \"**ne30np4_latlon.091226.nc**\". \n", "\n", "Both files are downloaded from CDAT's sample data directory in the following lines of code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def download(fnm):\n", " r = requests.get(\"https://cdat.llnl.gov/cdat/sample_data/%s\" % fnm,stream=True)\n", " with open(fnm,\"wb\") as f:\n", " for chunk in r.iter_content(chunk_size=1024):\n", " if chunk: # filter local_filename, keep new chunks alive\n", " f.write(chunk)\n", "\n", "for filename in [\"ne30_TS.nc\",\"ne30np4_latlon.091226.nc\"]:\n", " download(filename)\n", "\n", "\n", "data_path = \"ne30_TS.nc\"\n", "grid_file_path = \"ne30np4_latlon.091226.nc\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read Grid Info into Variables for Future Use\n", "The following line reads in the data from the grid file and stores it in the variable `gf`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gf = cdms2.open(grid_file_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need to work with a complete dataset so the next lines of code takes the latitude, longitude and element corners from the grid file, fills them out and loads them into the variables `lats`, `lons`, and `ec`, respectively." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lats = gf(\"lat\").filled()\n", "lons = gf(\"lon\").filled()\n", "ec = gf(\"element_corners\").filled()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double checking the contents of the different variables:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lats" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lons" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate the ne30 Grid\n", "The next line generates the ne30 grid using the filled out `lats`, `lons` and `ec` variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid = e3sm_nex.generateNEXGrid(lats, lons, ec)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double checking that the grid was created." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Data and Apply Grid\n", "The first line below opens the data file, \"ne30_TS.nc\", and assigns it to the variable `fd` (for \"file, data\" as opposed to \"file, grid\").\n", "\n", "The second line takes the radiative surface temperature data (`TS`) from the netCDF data file (`fd`) and loads it into the `data` variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fd = cdms2.open(data_path)\n", "data = fd(\"TS\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double checking that the data was loaded into the `data` variable:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next line applies the native ne30 grid to the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = e3sm_nex.applyGrid(data,grid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot the Gridded Data\n", "Create the canvas and call it `x`. Set the canvas size to 2400 pixels by 1600 pixels." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x=vcs.init(bg=True, geometry=(2400,1600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the graphics method variable `mesh`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh = vcs.createmeshfill()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following lines of code allow you to display a sub-section or sub-domain of the globe. By using -180 to 180 for the x-direction (longitude) and -90 to 90 for the y-direction (latitude), we'll display the whole globe." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh.datawc_x1 = -180\n", "mesh.datawc_x2 = 180\n", "mesh.datawc_y1 = -90\n", "mesh.datawc_y2 = 90" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to display the mesh on the globe, set `mesh.mesh = True`. For this plot we will not display the mesh, but it is displayed in the second plot below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#mesh.mesh = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the projection, `p`, for the plot. Here we'll use the Orthographic projection with a center longitude of 30 degrees and a center latitude of 45 degrees.\n", "\n", "The last line of code assigns this projection, `p`, to the `mesh.projection` attribute." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p=vcs.createprojection()\n", "p.type=\"orthographic\"\n", "p.centerlongitude=30.\n", "p.centerlatitude=45.\n", "mesh.projection=p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, plot the data which has been already scaled to E3SM's native grid." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.plot(data, mesh, ratio=\"autot\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the image above as a .png file titled \"NE30_orthographic.png\" to make it easier to zoom in on the image." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.png(\"NE30_orthographic\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open the .png file and display on the computer's screen." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!open NE30_orthographic.png" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot with Mesh Showing\n", "To display the mesh on the plot, set `mesh.mesh = True`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh.mesh = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clear the VCS canvas, `x`, before creating the plot; then create the plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.clear()\n", "x.plot(data, mesh, ratio=\"autot\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the image above as a .png file titled \"NE30_orthographic_w_mesh.png\" to zoom in to see more details." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.png(\"NE30_orthographic_w_mesh\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open the .png file and display on the computer's screen." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!open NE30_orthographic_w_mesh.png" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Switch Projections\n", "\n", "First let's check the existing projection to make sure everything is as it should be with the orthographic projection. Use `p.list` to list the details of the projection, `p`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p.list()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's see what other default projections VCS provides. The following line of code lists VCS's default projections." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vcs.listelements('projection')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For comparison, let's switch to the Lambert projection with the default settings, and set the mesh's projection to this new Lambert projection." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p=vcs.createprojection()\n", "p.type=\"lambert\"\n", "mesh.projection=p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this plot, let's turn off the mesh." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh.mesh = False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clear the canvas and create the Lambert plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.clear()\n", "x.plot(data, mesh, ratio=\"autot\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the image above as a .png file titled \"NE30_Lambert.png\", again to enable zooming in to see more details, then display the file on the computer screen." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.png(\"NE30_Lambert\")\n", "!open NE30_Lambert.png" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's turn the mesh back on for this new projection." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh.mesh = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clear the canvas again and plot the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.clear()\n", "x.plot(data, mesh, ratio=\"autot\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save this image as a .png to enable zooming and open the .png to inspect it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.png(\"NE30_Lambert_w_mesh\")\n", "!open NE30_Lambert_w_mesh.png" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In both the orthographic and the Lambert projections, you can see the corners of the E3SM native grid as well as the non-standard grid cell size, though both features are easier to visualize using the orthographic projection." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The CDAT software was developed by LLNL. The initial material for this tutorial was written by Charles Doutriaux. Updates and additional code/plots were added by Holly Davis on December 18, 2019. This work was performed under the auspices of the U.S. Department of Energy by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344.\n", "\n", "If you have questions about this notebook, please email our [CDAT Support](cdat-support@llnl.gov) address, cdat-support@llnl.gov." ] } ], "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.6.7" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": false, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": false, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }