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.
import cdms2
import vcs
import cdutil
import MV2
import genutil
# 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
# Prepare graphics and generate initial plot
x = vcs.init()
x.clear()
x.plot(d)
# Call the cdutil function to generate a mask, 0 for ocean, 1 for land.
mask = cdutil.generateLandSeaMask(d)
x.clear()
x.plot(mask)
# Match dimension using "grower" function
d, mask2 = genutil.grower(d, mask)
print(mask2.shape)
d_ocean = MV2.masked_where(mask2, d) # Or MV2.masked_where(mask2==1., d)"
x.clear()
x.plot(d_ocean)
d_land = MV2.masked_where(mask2==0., d) # Setting mask2==0 will mask out ocean
x.clear()
x.plot(d_land)