This notebook shows how to use VCS to create a plot of data on E3SM's native grid.
To download this Jupyter Notebook, 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.)
Note: this notebook uses Python 3.
Prior to running this notebook, create an appropriate conda environment by running the following commands:
conda create -n cdat82mesa-e3sm_nex -c cdat/label/v82 -c conda-forge python=3.6 cdat mesalib
conda activate cdat82mesa-e3sm_nex
Next type the following command to add e3sm_nex to the already-activated cdat81-mesa-e3sm_nex environment:
conda install -c conda-forge -c cdat e3sm_nex
Finally, to run Jupyter Notebooks, add JupyterLab to this environment:
conda install -c conda-forge jupyterlab
Note: for the image below to display correctly, you must use a CDAT v8.2 or later environment.
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:
jupyter-notebook
import e3sm_nex # Library for E3SM NE30 native grid
import cdms2
import vcs
import requests
import numpy
The data file for this tutorial is "ne30_TS.nc" and the grid file is "ne30np4_latlon.091226.nc".
Both files are downloaded from CDAT's sample data directory in the following lines of code.
def download(fnm):
r = requests.get("https://cdat.llnl.gov/cdat/sample_data/%s" % fnm,stream=True)
with open(fnm,"wb") as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter local_filename, keep new chunks alive
f.write(chunk)
for filename in ["ne30_TS.nc","ne30np4_latlon.091226.nc"]:
download(filename)
data_path = "ne30_TS.nc"
grid_file_path = "ne30np4_latlon.091226.nc"
The following line reads in the data from the grid file and stores it in the variable gf
.
gf = cdms2.open(grid_file_path)
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.
lats = gf("lat").filled()
lons = gf("lon").filled()
ec = gf("element_corners").filled()
Double checking the contents of the different variables:
gf
lats
lons
ec
The next line generates the ne30 grid using the filled out lats
, lons
and ec
variables.
grid = e3sm_nex.generateNEXGrid(lats, lons, ec)
Double checking that the grid was created.
grid
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").
The second line takes the radiative surface temperature data (TS
) from the netCDF data file (fd
) and loads it into the data
variable.
fd = cdms2.open(data_path)
data = fd("TS")
Double checking that the data was loaded into the data
variable:
data
The next line applies the native ne30 grid to the data.
data = e3sm_nex.applyGrid(data,grid)
Create the canvas and call it x
. Set the canvas size to 2400 pixels by 1600 pixels.
x=vcs.init(bg=True, geometry=(2400,1600))
Create the graphics method variable mesh
:
mesh = vcs.createmeshfill()
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.
mesh.datawc_x1 = -180
mesh.datawc_x2 = 180
mesh.datawc_y1 = -90
mesh.datawc_y2 = 90
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.
#mesh.mesh = True
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.
The last line of code assigns this projection, p
, to the mesh.projection
attribute.
p=vcs.createprojection()
p.type="orthographic"
p.centerlongitude=30.
p.centerlatitude=45.
mesh.projection=p
Finally, plot the data which has been already scaled to E3SM's native grid.
x.plot(data, mesh, ratio="autot")
Save the image above as a .png file titled "NE30_orthographic.png" to make it easier to zoom in on the image.
x.png("NE30_orthographic")
Open the .png file and display on the computer's screen.
!open NE30_orthographic.png
To display the mesh on the plot, set mesh.mesh = True
.
mesh.mesh = True
Clear the VCS canvas, x
, before creating the plot; then create the plot.
x.clear()
x.plot(data, mesh, ratio="autot")
Save the image above as a .png file titled "NE30_orthographic_w_mesh.png" to zoom in to see more details.
x.png("NE30_orthographic_w_mesh")
Open the .png file and display on the computer's screen.
!open NE30_orthographic_w_mesh.png
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
.
p.list()
Now let's see what other default projections VCS provides. The following line of code lists VCS's default projections.
vcs.listelements('projection')
For comparison, let's switch to the Lambert projection with the default settings, and set the mesh's projection to this new Lambert projection.
p=vcs.createprojection()
p.type="lambert"
mesh.projection=p
For this plot, let's turn off the mesh.
mesh.mesh = False
Clear the canvas and create the Lambert plot.
x.clear()
x.plot(data, mesh, ratio="autot")
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.
x.png("NE30_Lambert")
!open NE30_Lambert.png
Now let's turn the mesh back on for this new projection.
mesh.mesh = True
Clear the canvas again and plot the data.
x.clear()
x.plot(data, mesh, ratio="autot")
Save this image as a .png to enable zooming and open the .png to inspect it.
x.png("NE30_Lambert_w_mesh")
!open NE30_Lambert_w_mesh.png
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.
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.
If you have questions about this notebook, please email our CDAT Support address, cdat-support@llnl.gov.