Climate Scientist / Climate Data Manager Task: work with netcdf files

  • extract, add, change attributes

In [2]:
from netCDF4 import Dataset

testfiledir = "/home/stephan/Repos/Data/"

In [ ]:
esgf_data1 = Dataset(testfiledir+"pr_day_MPI-ESM-LR_1pctCO2_r1i1p1_18500101-18591231.nc",'r+')
esgf_data2 = Dataset(testfiledir+"pr_day_MPI-ESM-LR_1pctCO2_r1i1p1_18600101-18691231.nc",'r+')

In [ ]:
print esgf_data1.variables
print esgf_data1.dimensions
print esgf_data1.groups

In [ ]:
print esgf_data1.tracking_id
print esgf_data1.PID

In [ ]:
esgf_data1.tracking_id = "123-und-schon-geändert"

In [ ]:
pid = esgf_data1.PID = '11.99x/dasistnepid'

In [ ]:
esgf_data1.close()

In [3]:
!ncdump -h /home/stephan/Repos/Data/pr_day_MPI-ESM-LR_1pctCO2_r1i1p1_18500101-18591231.nc


netcdf pr_day_MPI-ESM-LR_1pctCO2_r1i1p1_18500101-18591231 {
dimensions:
	time = UNLIMITED ; // (3652 currently)
	lat = 96 ;
	lon = 192 ;
	bnds = 2 ;
variables:
	double time(time) ;
		time:bounds = "time_bnds" ;
		time:units = "days since 1850-1-1 00:00:00" ;
		time:calendar = "proleptic_gregorian" ;
		time:axis = "T" ;
		time:long_name = "time" ;
		time:standard_name = "time" ;
	double time_bnds(time, bnds) ;
	double lat(lat) ;
		lat:bounds = "lat_bnds" ;
		lat:units = "degrees_north" ;
		lat:axis = "Y" ;
		lat:long_name = "latitude" ;
		lat:standard_name = "latitude" ;
	double lat_bnds(lat, bnds) ;
	double lon(lon) ;
		lon:bounds = "lon_bnds" ;
		lon:units = "degrees_east" ;
		lon:axis = "X" ;
		lon:long_name = "longitude" ;
		lon:standard_name = "longitude" ;
	double lon_bnds(lon, bnds) ;
	float pr(time, lat, lon) ;
		pr:standard_name = "precipitation_flux" ;
		pr:long_name = "Precipitation" ;
		pr:comment = "at surface; includes both liquid and solid phases from all types of clouds (both large-scale and convective)" ;
		pr:units = "kg m-2 s-1" ;
		pr:cell_methods = "time: mean" ;
		pr:cell_measures = "area: areacella" ;
		pr:history = "2011-05-31T14:07:36Z altered by CMOR: replaced missing value flag (2e+20) with standard missing value (1e+20)." ;
		pr:missing_value = 1.e+20f ;
		pr:_FillValue = 1.e+20f ;
		pr:associated_files = "baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_atmos_fx_MPI-ESM-LR_1pctCO2_r0i0p0.nc areacella: areacella_fx_MPI-ESM-LR_1pctCO2_r0i0p0.nc" ;

// global attributes:
		:institution = "Max Planck Institute for Meteorology" ;
		:institute_id = "MPI-M" ;
		:experiment_id = "1pctCO2" ;
		:source = "MPI-ESM-LR 2011; URL: http://svn.zmaw.de/svn/cosmos/branches/releases/mpi-esm-cmip5/src/mod; atmosphere: ECHAM6 (REV: 4571), T63L47; land: JSBACH (REV: 4571); ocean: MPIOM (REV: 4571), GR15L40; sea ice: 4571; marine bgc: HAMOCC (REV: 4571);" ;
		:model_id = "MPI-ESM-LR" ;
		:forcing = "N/A" ;
		:parent_experiment_id = "piControl" ;
		:parent_experiment_rip = "r1i1p1" ;
		:branch_time = 10957. ;
		:contact = "cmip5-mpi-esm@dkrz.de" ;
		:history = "Model raw output postprocessing with modelling environment (IMDI) at DKRZ: URL: http://svn-mad.zmaw.de/svn/mad/Model/IMDI/trunk, REV: 3208 2011-05-31T14:07:31Z CMOR rewrote data to comply with CF standards and CMIP5 requirements." ;
		:references = "ECHAM6: n/a; JSBACH: Raddatz et al., 2007. Will the tropical land biosphere dominate the climate-carbon cycle feedback during the twenty first century? Climate Dynamics, 29, 565-574, doi 10.1007/s00382-007-0247-8;  MPIOM: Marsland et al., 2003. The Max-Planck-Institute global ocean/sea ice model with orthogonal curvilinear coordinates. Ocean Modelling, 5, 91-127;  HAMOCC: http://www.mpimet.mpg.de/fileadmin/models/MPIOM/HAMOCC5.1_TECHNICAL_REPORT.pdf;" ;
		:initialization_method = 1 ;
		:physics_version = 1 ;
		:tracking_id = "e1e96bc1-e8a9-49f1-8002-72af88f777be" ;
		:product = "output" ;
		:experiment = "1 percent per year CO2" ;
		:frequency = "day" ;
		:creation_date = "2011-05-31T14:07:36Z" ;
		:Conventions = "CF-1.4" ;
		:project_id = "CMIP5" ;
		:table_id = "Table day (27 April 2011) 86d1558d99b6ed1e7a886ab3fd717b58" ;
		:title = "MPI-ESM-LR model output prepared for CMIP5 1 percent per year CO2" ;
		:parent_experiment = "pre-industrial control" ;
		:modeling_realm = "atmos" ;
		:realization = 1 ;
		:cmor_version = "2.5.9" ;
}

In [1]:
## own (easyly modifyable) ncdump version

In [ ]:
def ncdump(nc_fid, verb=True):
    '''
    ncdump outputs dimensions, variables and their attribute information.
    The information is similar to that of NCAR's ncdump utility.
    ncdump requires a valid instance of Dataset.

    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 netCDF4.Dataset.variables key
        """
        try:
            print "\t\ttype:", repr(nc_fid.variables[key].dtype)
            for ncattr in nc_fid.variables[key].ncattrs():
                print '\t\t%s:' % ncattr,\
                      repr(nc_fid.variables[key].getncattr(ncattr))
        except KeyError:
            print "\t\tWARNING: %s does not contain variable attributes" % key

    # NetCDF global attributes
    nc_attrs = nc_fid.ncattrs()
    if verb:
        print "NetCDF Global Attributes:"
        for nc_attr in nc_attrs:
            print '\t%s:' % nc_attr, repr(nc_fid.getncattr(nc_attr))
    nc_dims = [dim for dim in nc_fid.dimensions]  # list of nc dimensions
    # Dimension shape information.
    if verb:
        print "NetCDF dimension information:"
        for dim in nc_dims:
            print "\tName:", dim 
            print "\t\tsize:", len(nc_fid.dimensions[dim])
            print_ncattr(dim)
    # Variable information.
    nc_vars = [var for var in nc_fid.variables]  # list of nc variables
    if verb:
        print "NetCDF variable information:"
        for var in nc_vars:
            if var not in nc_dims:
                print '\tName:', var
                print "\t\tdimensions:", nc_fid.variables[var].dimensions
                print "\t\tsize:", nc_fid.variables[var].size
                print_ncattr(var)
    return nc_attrs, nc_dims, nc_vars

In [ ]:
esgf_data1.close()

In [ ]:


In [ ]: