Creating Non Rectilinear Grids from Scratch

Download the Notebook here

Preparing The Notebook

In [1]:
# Prepare the notebook elements
import requests
def download(fnm):
    r = requests.get("https://uvcdat.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-alive new chunks
                f.write(chunk)
                
download("sampleCurveGrid4.nc")
download("sampleGenGrid3.nc")

Curvilinear Grids

In this section we will create a curvilinear grid from raw data and regrid it to T42 Gaussian Grid

Back To Top

Load the data

Back To Top

In [2]:
import cdms2

fnm = "sampleCurveGrid4.nc"
f = cdms2.open(fnm)
data = f("sample").filled() # Get data and forget all about it (pure numpy)
lat = f("lat").filled() # latitude coordinates
lon = f("lon").filled() # longitude coords
blon = f("bounds_lon").filled() # longitude vertices
blat = f("bounds_lat").filled() #latitude vertices

Creating latitude and longitude two dimensional "axes"

Let's prepare the "axes" for our data (i.e 2D lat lon)

Back To Top

In [3]:
lat_axis = cdms2.coord.TransientAxis2D(lat)
lat_axis.setBounds(blat)

lon_axis = cdms2.coord.TransientAxis2D(lon)
lon_axis.setBounds(blon)

Creating two dimensional curvilinear grid

Let's prepare the "axes" for our data (i.e 2D lat lon)

Back To Top

In [4]:
# Now let's create the grid
grid = cdms2.hgrid.TransientCurveGrid(lat_axis,lon_axis)

Applying grid to raw data

Let's apply the grid to our data

Back To Top

In [5]:
data = cdms2.MV2.array(data)
data.setAxisList(grid.getAxisList())
data.setGrid(grid)

Converting to another grid

Let's regrid our data to a T42 grid

Back To Top

In [6]:
regrid_data = data.regrid(cdms2.createGaussianGrid(128),regridTool="esmf",regridMethod="conserve")
WARNING: Bounds exceed +/-90 degree latitude: min/max lats = -90/88.42

Display results

let's plot the raw data and the regrided data

Back To Top

In [7]:
# And llet's plot it to be sure it worked
import vcs
import EzTemplate

M = EzTemplate.Multi(rows=1,columns=2)

# Raw data
mesh = vcs.createmeshfill()
x=vcs.init(bg=True)
x.plot(data,mesh,M.get(row=0,column=0))

# Regridded data
x.plot(regrid_data,M.get(row=0,column=1))
Out[7]:

Generic Grids

In this section we will create a genric grid from raw data and regrid it to T42 Gaussian Grid

Back To Top

Load the data

Back To Top

In [8]:
import cdms2

fnm = "sampleGenGrid3.nc"
f = cdms2.open(fnm)
data = f("sample").filled() # Get data and forget all about it (pure numpy)
lat = f("lat").filled() # latitude coordinates
lon = f("lon").filled() # longitude coords
blon = f("bounds_lon").filled() # longitude vertices
blat = f("bounds_lat").filled() #latitude vertices

Creating latitude and longitude "axes"

Let's prepare the "axes" for our data (i.e 1D lat lon)

Back To Top

In [9]:
#Ncells = len(lat)
#iaxis = cdms2.coord.TransientVirtualAxis("cell", Ncells)
lat_axis = cdms2.auxcoord.TransientAuxAxis1D(lat)
lat_axis.setBounds(blat)

lon_axis = cdms2.auxcoord.TransientAuxAxis1D(lon)
lon_axis.setBounds(blon)

Creating Unidimensional Generic Grid

Let's prepare the "axes" for our data (i.e 2D lat lon)

Back To Top

In [10]:
# Now let's create the grid
grid = cdms2.gengrid.TransientGenericGrid(lat_axis,lon_axis)

Applying grid to raw data

Let's apply the grid to our data

Back To Top

In [11]:
data = cdms2.MV2.array(data)
data.setAxisList(grid.getAxisList())
data.setGrid(grid)

Display results

let's plot the raw data and the regrided data

Back To Top

In [12]:
# And llet's plot it to be sure it worked
import vcs

# Raw data
mesh = vcs.createmeshfill()
mesh.mesh = True
x=vcs.init(bg=True)
x.plot(data,mesh)
Out[12]: