Nc files saved in fabriwin
variables # shape is time, lat, lon as shown above time_idx = 237 # some random day in 2012 # Python and the renalaysis are slightly off in time so this fixes that problem offset = dt. variables # extract/copy the data lons = nc_fid. size print_ncattr ( var ) return nc_attrs, nc_dims, nc_vars nc_f = './' # Your filename nc_fid = Dataset ( nc_f, 'r' ) # Dataset is the class behavior to open the file # and create an instance of the ncCDF4 class nc_attrs, nc_dims, nc_vars = ncdump ( nc_fid ) # Extract data from NetCDF file lats = nc_fid. nc_vars = # list of nc variables if verb : print "NetCDF variable information:" for var in nc_vars : if var not in nc_dims : print ' \t Name:', var print " \t\t dimensions:", nc_fid. dimensions ) print_ncattr ( dim ) # Variable information. if verb : print "NetCDF dimension information:" for dim in nc_dims : print " \t Name:", dim print " \t\t size:", len ( nc_fid. getncattr ( nc_attr )) nc_dims = # list of nc dimensions # Dimension shape information.
ncattrs () if verb : print "NetCDF Global Attributes:" for nc_attr in nc_attrs : print ' \t %s :' % nc_attr, repr ( nc_fid. getncattr ( ncattr )) except KeyError : print " \t\t WARNING: %s does not contain variable attributes" % key # NetCDF global attributes nc_attrs = nc_fid. ncattrs (): print ' \t\t %s :' % ncattr ,\ Parameters - nc_fid : netCDF4.Dataset A netCDF4 dateset object verb : Boolean whether or not nc_attrs, nc_dims, and nc_vars are printed Returns - nc_attrs : list A Python list of the NetCDF file global attributes nc_dims : list A Python list of the NetCDF file dimensions nc_vars : list A Python list of the NetCDF file variables ''' def print_ncattr ( key ): """ Prints the NetCDF file attributes for a given key Parameters - key : unicode a valid key """ try : print " \t\t type:", repr ( nc_fid. ncdump requires a valid instance of Dataset. The information is similar to that of NCAR's ncdump utility. 1996 (1996)0772.0.CO 2 ''' import datetime as dt # Python standard library datetime module import numpy as np from netCDF4 import Dataset # import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap, addcyclic, shiftgrid def ncdump ( nc_fid, verb = True ): ''' ncdump outputs dimensions, variables and their attribute information. PROGRAMMER(S) Chris Slocum REVISION HISTORY 20140320 - Initial version created and posted online 20140722 - Added basic error handling to ncdump Thanks to K.-Michael Aye for highlighting the issue REFERENCES netcdf4-python - NCEP/NCAR Reanalysis - Kalnay et al. Plotting using Matplotlib and Basemap is also shown.
Nc files saved in fabriwin how to#
''' NAME NetCDF with Python PURPOSE To demonstrate how to read and write data with NetCDF files using a NetCDF file from the NCEP/NCAR Reanalysis. Unpacked_valid_range: array(, dtype=float32)ĭataset: u'NCEP Reanalysis Daily Averages' Long_name: u'mean Daily Air temperature at sigma level 995' These are the 0.9950 sigma level values.'Īctual_range: array(, dtype=float32)Īctual_range: array() History: u'created 2011/12 by Hoop (netCDF2.3)'ĭescription: u'Data is from NMC initialized reanalysis\n(4x/day). Title: u'mean daily NMC reanalysis (2012)' Example output ¶ FiguresĪfter the data are read using Python, the air temperature is plotted using a NetCDF reading and writing example with plotting by Chris SlocumĬommons Attribution-NonCommercial-NoDerivs 3.0 Unported License. My contact information can be found on my NCEP/NCAR Reanalysis I project products and documentation can be found atįor more examples on using NetCDF and Python, visit the Unidata NetCDF exampleįree to contact me with any feedback, questions, comments, or concerns. In addition, we will create a contour fill plot of the departure. Temperature departure as a function of time, latitude,Īnd longitude.
We will createĪ corresponding NetCDF file entitled './air.' with the air Lastly, we will compute the global air temperature departure from its valueĪt Darwin, Australia for all of 2012. Generated in this example is not CF (Climate and Forecast) metadata convention compliant. We will create a simple line plot to visualize this data. Profile has a fixed location and is only dependent on the time dimension.
Into a NetCDF file entitled './darwin_2012.nc.' The temperature įill map in a Mollweide equal area projectionĪfter this, we will write the air temperature profile for Darwin, Australia (12.45°S, 130.83☎) Sigma level ('./') from the NCEP/NCAR Reanalysis I (Kalnay et al. In this example, I use a NetCDF file of 2012 air temperature on the 0.995 Unidata NetCDF (Network Common Data Form) files using the Here is an example of how to read and write data with
Python - NetCDF reading and writing example with plotting