#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Jan 3 15:51:39 2018 @author: jamiller, SGT Inc. for NASA/GSFC/Code 615 """ from netCDF4 import Dataset # netcdf routines that you may need to install into python rootgrp = Dataset("2016730smeltfreeze.nc", "r") # open file for reading print(rootgrp.variables.keys()) # show the keys / variables in the file print(rootgrp.variables['Melt']) # show the information on the 'melt' variable for attr in rootgrp.ncattrs(): # show the global attributes in the file print(attr, '=', getattr(rootgrp, attr)) year = getattr(rootgrp, 'Year') # pull year for plots vers = getattr(rootgrp, 'Melt Version') # pull version for plot savefiles # extract variables melt = rootgrp.variables['Melt'][:] earlymelt = rootgrp.variables['Earlymelt'][:] freeze = rootgrp.variables['Freeze'][:] earlyfreeze = rootgrp.variables['Earlyfreeze'][:] rootgrp.close() # close out the netcdf file import matplotlib.pyplot as plt # plot libraries imgplot = plt.imshow(melt, clim=(75, 210)) # do an image plot scaled from 75 to 210 plt.colorbar() # give it a colorbar plt.title(year+ ' Melt') # title it plt.savefig('/Users/jamiller/Desktop/'+year+vers+'meltexample.pdf') # save it plt.show() # show it and clear the graphics buffer # repeat for the other variables in the melt file imgplot2 = plt.imshow(earlymelt, clim=(75, 210)) plt.colorbar() plt.title(year+ ' Early Melt') plt.savefig('/Users/jamiller/Desktop/'+year+vers+'earlymeltexample.pdf') plt.show() imgplot3 = plt.imshow(freeze, clim=(210, 410)) plt.colorbar() plt.title(year+ ' Freeze') plt.savefig('/Users/jamiller/Desktop/'+year+vers+'freezeexample.pdf') plt.show() imgplot4 = plt.imshow(earlyfreeze, clim=(210, 410)) plt.colorbar() plt.title(year+ ' Early Freeze') plt.savefig('/Users/jamiller/Desktop/'+year+vers+'earlyfreezeexample.pdf') plt.show()