Differences between revisions 9 and 10
Revision 9 as of 2012-08-15 20:56:20
Size: 1958
Editor: MikhailItkin
Comment:
Revision 10 as of 2012-08-15 21:07:05
Size: 2224
Editor: MikhailItkin
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:

Find full PyNio documentation [[ http://www.pyngl.ucar.edu/Nio.shtml | here ]]
Line 61: Line 63:
f.create_variable('temperature, 'i', ('time')) # dimension 'time' must be created prior to this step f.create_variable('temperature', 'i', ('time',)) # dimension 'time' must be created prior to this step

# dimensions and variables can have same names
f.create_variable('time', 'i', ('time',))
Line 84: Line 89:
time = numpy.arange(1000,1100,1, dtype = numpy.int32)
f.variables['time'][:] = time
Line 88: Line 96:
Any netcdf file can be reopened later and new dimensions and variables can be added Any netcdf file can be reopened later and new dimensions and variables can be added.

PyNIO

Find full PyNio documentation here

module load python/2.7-ve0
ipython

Ipython listing

Read NetCDF files

   1 # load Nio module
   2 import Nio
   3 
   4 # open netcdf file
   5 f = Nio.open_file('sresa1b_ncar_ccsm3_0_run1_200001.nc', 'r') # 'r' stands for "read rights"
   6 
   7 # check contents
   8 print f
   9 
  10 # get dimensions
  11 dimNames = f.dimensions.keys()
  12 
  13 # get the size of dimension
  14 dimSize = f.dimensions['dimName']
  15 
  16 # read single variable:
  17 pr = f.variables['pr']
  18 print pr
  19 
  20 # read variables contents
  21 pr_data = pr[:]
  22 print pr_data
  23 
  24 # print slice of the variable data
  25 slice = pr_data[0,::-1,:]
  26 
  27 # close file
  28 f.close()

Create NetCDF file

   1 # create new file
   2 import Nio
   3 import numpy
   4 
   5 f = Nio.open_file('test.nc', 'w') # "w" stands for writing rights
   6 
   7 # create a dimension
   8 f.create_dimension('time',100)
   9 
  10 # create variable
  11 f.create_variable('temperature', 'i', ('time',)) # dimension 'time' must be created prior to this step
  12 
  13 # dimensions and variables can have same names
  14 f.create_variable('time', 'i', ('time',))
  15 
  16 # Dimension types:
  17 #    'd': 64 bit float
  18 #    'f': 32 bit float
  19 #    'l': long
  20 #    'i': 32 bit integer
  21 #    'h': 16 bit integer
  22 #    'b': 8 bit integer
  23 #    'S1': character 
  24 
  25 # Create an attribute 
  26 f.variables['temperature'].units = "K"
  27 
  28 # Add scaling factor and an offset
  29 # when the variable will be read, it will need to be multiplied by the scale_factor first and the added to the offset value
  30 f.variables['temperature'].scale_factor = 0.1
  31 f.variables['temperature'].add_offset = 273
  32 
  33 # create variable contents and assign it to the NetCDF variable
  34 temp = numpy.arange(0,100,1, dtype = numpy.int32)
  35 f.variables['temperature'][:] = temp
  36 
  37 time = numpy.arange(1000,1100,1, dtype = numpy.int32)
  38 f.variables['time'][:] = time
  39 
  40 f.close()

Any netcdf file can be reopened later and new dimensions and variables can be added.

LehreWiki: IoBasemapNgl (last edited 2012-08-16 08:22:08 by MikhailItkin)