Masking Data Land-Sea Masks (Beginner)

In this example we will show simple examples on how to generate land and sea masks.

The CDAT software was developed by LLNL. This tutorial was written by Jiwoo Lee with Carlos Downie. This work was performed under the auspices of the U.S. Department of Energy by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344.

Download the Jupyter Notebook

Preparing the Notebook

Back to Top

In [6]:
import cdms2
import vcs
import cdutil
import MV2
import genutil
In [10]:
# Download data
import requests
r = requests.get("https://cdat.llnl.gov/cdat/sample_data/clt.nc",stream=True)
with open("clt.nc","wb") as f:
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:  # filter local_filename keep-alive new chunks
            f.write(chunk)

# Load the data
f = cdms2.open('clt.nc')
d = f('clt') # Get the 'clt' variable
In [11]:
# Prepare graphics and generate initial plot
x = vcs.init()
x.clear()
x.plot(d)
Out[11]:
<vcs.displayplot.Dp at 0x7f93e12cf450>

Generate Mask (0: ocean, 1: land)

In [12]:
# Call the cdutil function to generate a mask, 0 for ocean, 1 for land.
mask = cdutil.generateLandSeaMask(d) 
In [13]:
x.clear()
x.plot(mask)
Out[13]:
<vcs.displayplot.Dp at 0x7f93e12cf790>

Match Dimension

In [15]:
# Match dimension using "grower" function
d, mask2 = genutil.grower(d, mask)
print(mask2.shape)
(120, 46, 72)

Ocean grid (mask out land area)

In [16]:
d_ocean = MV2.masked_where(mask2, d)  # Or MV2.masked_where(mask2==1., d)"
In [17]:
x.clear()
x.plot(d_ocean)
Out[17]:
<vcs.displayplot.Dp at 0x7f93e12cf6c0>

Land grid (mask out ocean area)

In [18]:
d_land = MV2.masked_where(mask2==0., d) # Setting mask2==0 will mask out ocean
In [19]:
x.clear()
x.plot(d_land)
Out[19]:
<vcs.displayplot.Dp at 0x7f93e12cfe10>
In [ ]: