In [5]:
from netCDF4 import Dataset

testfiledir = "/home/stephan/Data/"

In [2]:
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 [18]:
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 [7]:
print esgf_data1.variables
print esgf_data1.dimensions
print esgf_data1.groups


OrderedDict([(u'time', <type 'netCDF4._netCDF4.Variable'>
float64 time(time)
    bounds: time_bnds
    units: days since 1850-1-1 00:00:00
    calendar: proleptic_gregorian
    axis: T
    long_name: time
    standard_name: time
unlimited dimensions: time
current shape = (3652,)
filling off
), (u'time_bnds', <type 'netCDF4._netCDF4.Variable'>
float64 time_bnds(time, bnds)
unlimited dimensions: time
current shape = (3652, 2)
filling off
), (u'lat', <type 'netCDF4._netCDF4.Variable'>
float64 lat(lat)
    bounds: lat_bnds
    units: degrees_north
    axis: Y
    long_name: latitude
    standard_name: latitude
unlimited dimensions: 
current shape = (96,)
filling off
), (u'lat_bnds', <type 'netCDF4._netCDF4.Variable'>
float64 lat_bnds(lat, bnds)
unlimited dimensions: 
current shape = (96, 2)
filling off
), (u'lon', <type 'netCDF4._netCDF4.Variable'>
float64 lon(lon)
    bounds: lon_bnds
    units: degrees_east
    axis: X
    long_name: longitude
    standard_name: longitude
unlimited dimensions: 
current shape = (192,)
filling off
), (u'lon_bnds', <type 'netCDF4._netCDF4.Variable'>
float64 lon_bnds(lon, bnds)
unlimited dimensions: 
current shape = (192, 2)
filling off
), (u'pr', <type 'netCDF4._netCDF4.Variable'>
float32 pr(time, lat, lon)
    standard_name: precipitation_flux
    long_name: Precipitation
    comment: at surface; includes both liquid and solid phases from all types of clouds (both large-scale and convective)
    units: kg m-2 s-1
    cell_methods: time: mean
    cell_measures: area: areacella
    history: 2011-05-31T14:07:36Z altered by CMOR: replaced missing value flag (2e+20) with standard missing value (1e+20).
    missing_value: 1e+20
    _FillValue: 1e+20
    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
unlimited dimensions: time
current shape = (3652, 96, 192)
filling off
)])
OrderedDict([(u'time', <type 'netCDF4._netCDF4.Dimension'> (unlimited): name = 'time', size = 3652
), (u'lat', <type 'netCDF4._netCDF4.Dimension'>: name = 'lat', size = 96
), (u'lon', <type 'netCDF4._netCDF4.Dimension'>: name = 'lon', size = 192
), (u'bnds', <type 'netCDF4._netCDF4.Dimension'>: name = 'bnds', size = 2
)])
OrderedDict()

In [8]:
ncdump(esgf_data2)


NetCDF Global Attributes:
	institution: u'Max Planck Institute for Meteorology'
	institute_id: u'MPI-M'
	experiment_id: u'1pctCO2'
	source: u'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: u'MPI-ESM-LR'
	forcing: u'N/A'
	parent_experiment_id: u'piControl'
	parent_experiment_rip: u'r1i1p1'
	branch_time: 10957.0
	contact: u'cmip5-mpi-esm@dkrz.de'
	history: u'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:50:49Z CMOR rewrote data to comply with CF standards and CMIP5 requirements.'
	references: u'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: u'8e6a86e8-9cba-45cc-a794-df87f657d2c1'
	product: u'output'
	experiment: u'1 percent per year CO2'
	frequency: u'day'
	creation_date: u'2011-05-31T14:50:54Z'
	Conventions: u'CF-1.4'
	project_id: u'CMIP5'
	table_id: u'Table day (27 April 2011) 86d1558d99b6ed1e7a886ab3fd717b58'
	title: u'MPI-ESM-LR model output prepared for CMIP5 1 percent per year CO2'
	parent_experiment: u'pre-industrial control'
	modeling_realm: u'atmos'
	realization: 1
	cmor_version: u'2.5.9'
NetCDF dimension information:
	Name: time
		size: 3653
		type: dtype('float64')
		bounds: u'time_bnds'
		units: u'days since 1850-1-1 00:00:00'
		calendar: u'proleptic_gregorian'
		axis: u'T'
		long_name: u'time'
		standard_name: u'time'
	Name: lat
		size: 96
		type: dtype('float64')
		bounds: u'lat_bnds'
		units: u'degrees_north'
		axis: u'Y'
		long_name: u'latitude'
		standard_name: u'latitude'
	Name: lon
		size: 192
		type: dtype('float64')
		bounds: u'lon_bnds'
		units: u'degrees_east'
		axis: u'X'
		long_name: u'longitude'
		standard_name: u'longitude'
	Name: bnds
		size: 2
		type: 		WARNING: bnds does not contain variable attributes
NetCDF variable information:
	Name: time_bnds
		dimensions: (u'time', u'bnds')
		size: 7306
		type: dtype('float64')
	Name: lat_bnds
		dimensions: (u'lat', u'bnds')
		size: 192
		type: dtype('float64')
	Name: lon_bnds
		dimensions: (u'lon', u'bnds')
		size: 384
		type: dtype('float64')
	Name: pr
		dimensions: (u'time', u'lat', u'lon')
		size: 67332096
		type: dtype('float32')
		standard_name: u'precipitation_flux'
		long_name: u'Precipitation'
		comment: u'at surface; includes both liquid and solid phases from all types of clouds (both large-scale and convective)'
		units: u'kg m-2 s-1'
		cell_methods: u'time: mean'
		cell_measures: u'area: areacella'
		history: u'2011-05-31T14:50:54Z altered by CMOR: replaced missing value flag (2e+20) with standard missing value (1e+20).'
		missing_value: 1e+20
		_FillValue: 1e+20
		associated_files: u'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'
Out[8]:
([u'institution',
  u'institute_id',
  u'experiment_id',
  u'source',
  u'model_id',
  u'forcing',
  u'parent_experiment_id',
  u'parent_experiment_rip',
  u'branch_time',
  u'contact',
  u'history',
  u'references',
  u'initialization_method',
  u'physics_version',
  u'tracking_id',
  u'product',
  u'experiment',
  u'frequency',
  u'creation_date',
  u'Conventions',
  u'project_id',
  u'table_id',
  u'title',
  u'parent_experiment',
  u'modeling_realm',
  u'realization',
  u'cmor_version'],
 [u'time', u'lat', u'lon', u'bnds'],
 [u'time', u'time_bnds', u'lat', u'lat_bnds', u'lon', u'lon_bnds', u'pr'])

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


123-und-schon-geändert
11.99x/dasistnepid

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

In [12]:
esgf_data1.close()

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

In [17]:
esgf_data1.close()

In [ ]: