{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Non Rectilinear Grids from Scratch\n", "\n", "Download the Notebook [here](Creating_Non_Rectiilinear_Grids_From_Scratch.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of Contents\n", "\n", "- [Preparing the notebook](#prepare)\n", "- [Curvilinear Grid](#curv)\n", " - [Load in data](#load_curv)\n", " - [Prepare 2D \"axes\"](#axes_curv)\n", " - [Create Curvilinear Grid](#grid_curv)\n", " - [Applying Grid To Data](#data_curv)\n", " - [Regrid to T42 Gaussian Grid](#regrid_curv)\n", " - [Display Results](#plot_curv)\n", "- [Generic Grid](#gen)\n", " - [Load in data](#load_gen)\n", " - [Prepare \"axes\"](#axes_gen)\n", " - [Create Generic Grid](#grid_gen)\n", " - [Applying Grid To Data](#data_gen)\n", " - [Display Results](#plot_gen)\n", "\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Preparing The Notebook" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Prepare the notebook elements\n", "import requests\n", "def download(fnm):\n", " r = requests.get(\"https://uvcdat.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-alive new chunks\n", " f.write(chunk)\n", " \n", "download(\"sampleCurveGrid4.nc\")\n", "download(\"sampleGenGrid3.nc\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Curvilinear Grids\n", "\n", "In this section we will create a curvilinear grid from raw data and regrid it to T42 Gaussian Grid\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load the data\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cdms2\n", "\n", "fnm = \"sampleCurveGrid4.nc\"\n", "f = cdms2.open(fnm)\n", "data = f(\"sample\").filled() # Get data and forget all about it (pure numpy)\n", "lat = f(\"lat\").filled() # latitude coordinates\n", "lon = f(\"lon\").filled() # longitude coords\n", "blon = f(\"bounds_lon\").filled() # longitude vertices\n", "blat = f(\"bounds_lat\").filled() #latitude vertices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating latitude and longitude two dimensional \"axes\"\n", "\n", "Let's prepare the \"axes\" for our data (i.e 2D lat lon)\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lat_axis = cdms2.coord.TransientAxis2D(lat)\n", "lat_axis.setBounds(blat)\n", "\n", "lon_axis = cdms2.coord.TransientAxis2D(lon)\n", "lon_axis.setBounds(blon)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating two dimensional curvilinear grid\n", "\n", "Let's prepare the \"axes\" for our data (i.e 2D lat lon)\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now let's create the grid\n", "grid = cdms2.hgrid.TransientCurveGrid(lat_axis,lon_axis)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Applying grid to raw data\n", "\n", "Let's apply the grid to our data\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = cdms2.MV2.array(data)\n", "data.setAxisList(grid.getAxisList())\n", "data.setGrid(grid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Converting to another grid\n", "\n", "Let's regrid our data to a T42 grid\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "regrid_data = data.regrid(cdms2.createGaussianGrid(128),regridTool=\"esmf\",regridMethod=\"conserve\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display results\n", "\n", "let's plot the raw data and the regrided data\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# And llet's plot it to be sure it worked\n", "import vcs\n", "import EzTemplate\n", "\n", "M = EzTemplate.Multi(rows=1,columns=2)\n", "\n", "# Raw data\n", "mesh = vcs.createmeshfill()\n", "x=vcs.init(bg=True)\n", "x.plot(data,mesh,M.get(row=0,column=0))\n", "\n", "# Regridded data\n", "x.plot(regrid_data,M.get(row=0,column=1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Generic Grids\n", "\n", "In this section we will create a genric grid from raw data and regrid it to T42 Gaussian Grid\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load the data\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cdms2\n", "\n", "fnm = \"sampleGenGrid3.nc\"\n", "f = cdms2.open(fnm)\n", "data = f(\"sample\").filled() # Get data and forget all about it (pure numpy)\n", "lat = f(\"lat\").filled() # latitude coordinates\n", "lon = f(\"lon\").filled() # longitude coords\n", "blon = f(\"bounds_lon\").filled() # longitude vertices\n", "blat = f(\"bounds_lat\").filled() #latitude vertices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating latitude and longitude \"axes\"\n", "\n", "Let's prepare the \"axes\" for our data (i.e 1D lat lon)\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Ncells = len(lat)\n", "#iaxis = cdms2.coord.TransientVirtualAxis(\"cell\", Ncells)\n", "lat_axis = cdms2.auxcoord.TransientAuxAxis1D(lat)\n", "lat_axis.setBounds(blat)\n", "\n", "lon_axis = cdms2.auxcoord.TransientAuxAxis1D(lon)\n", "lon_axis.setBounds(blon)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating Unidimensional Generic Grid\n", "\n", "Let's prepare the \"axes\" for our data (i.e 2D lat lon)\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now let's create the grid\n", "grid = cdms2.gengrid.TransientGenericGrid(lat_axis,lon_axis)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Applying grid to raw data\n", "\n", "Let's apply the grid to our data\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = cdms2.MV2.array(data)\n", "data.setAxisList(grid.getAxisList())\n", "data.setGrid(grid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display results\n", "\n", "let's plot the raw data and the regrided data\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# And llet's plot it to be sure it worked\n", "import vcs\n", "\n", "# Raw data\n", "mesh = vcs.createmeshfill()\n", "mesh.mesh = True\n", "x=vcs.init(bg=True)\n", "x.plot(data,mesh)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Back To Top](#top)" ] } ], "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.7.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 2 }