Skip to content

Instantly share code, notes, and snippets.

@adcroft
Last active August 29, 2015 13:57
Show Gist options
  • Save adcroft/9663382 to your computer and use it in GitHub Desktop.
Save adcroft/9663382 to your computer and use it in GitHub Desktop.
An example interactive python session to plot SST from MOM6 post-processed files, using just matplotlib and netCDF4
# Import necessary modules
import netCDF4
import matplotlib.pyplot as plt
tFiles = netCDF4.MFDataset('/archive/bls/tikal_201406_mom6_2014.07.01/OM4_SIS_baseline/gfdl.ncrc2-intel-prod/pp/ocean_monthly/ts/monthly/10yr/ocean_monthly.*.temp.nc')
print tFiles
tType = tFiles.variables['temp']
print tType
print tType.shape
# These all give the same results
sst = tType[0,0]
sst = tType[0,0,:]
sst = tType[0,0,:,:]
print sst.shape
# Quickly check data
plt.pcolormesh( sst ); plt.show(block=False)
# Add a colorbar. Note different usages of plt.show() - I prefer block=False for instant results
plt.colorbar()
plt.show()
# Now read the grid from the super-grid. Note the various one-line equivalents of the multiple steps used above.
superGridFile = netCDF4.Dataset('/archive/gold/datasets/OM4_025/mosaic.v20140610.unpacked/ocean_hgrid.nc')
lon = superGridFile.variables['x'][::2,::2]
lat = superGridFile.variables['y'][::2,::2]
lat = netCDF4.Dataset('/archive/gold/datasets/OM4_025/mosaic.v20140610.unpacked/ocean_hgrid.nc').variables['y'][::2,0::2]
# lon,lat are the corner positions of T-cells (nodes of mesh where SST will be cell-cnetered) and thus 1 row/col wider than SST
# Plot with proper coordinates
plt.pcolormesh(lon, lat, sst); plt.colorbar(); plt.show(block=False)
# Now animate
plt.clf(); plt.show(block=False)
for n in range(tType.shape[0]):
plt.clf(); plt.pcolormesh(lon, lat, tType[n,0,:,:]); plt.colorbar(); plt.draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment