Differences between revisions 4 and 9 (spanning 5 versions)
Revision 4 as of 2012-08-15 20:12:22
Size: 215
Editor: MikhailItkin
Comment:
Revision 9 as of 2012-08-15 20:56:20
Size: 1958
Editor: MikhailItkin
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
== Read NetCDF files ==
Download a [[http://www.unidata.ucar.edu/software/netcdf/examples/files.html| sample file (CCM precipitation flux, air temperature, etc)]]
Line 6: Line 4:
{{{#python  * Download a [[ http://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3_0_run1_200001.nc | sample file (CCM precipitation flux, air temperature, etc)]]
 * load python module and start ipython
Line 8: Line 7:
print Hello! {{{
module load python/2.7-ve0
ipython
}}}

Ipython listing


== Read NetCDF files ==

{{{#!python
# load Nio module
import Nio

# open netcdf file
f = Nio.open_file('sresa1b_ncar_ccsm3_0_run1_200001.nc', 'r') # 'r' stands for "read rights"

# check contents
print f

# get dimensions
dimNames = f.dimensions.keys()

# get the size of dimension
dimSize = f.dimensions['dimName']

# read single variable:
pr = f.variables['pr']
print pr

# read variables contents
pr_data = pr[:]
print pr_data

# print slice of the variable data
slice = pr_data[0,::-1,:]

# close file
f.close()
}}}

== Create NetCDF file ==

{{{#!python
# create new file
import Nio
import numpy

f = Nio.open_file('test.nc', 'w') # "w" stands for writing rights

# create a dimension
f.create_dimension('time',100)

# create variable
f.create_variable('temperature, 'i', ('time')) # dimension 'time' must be created prior to this step

# Dimension types:
# 'd': 64 bit float
# 'f': 32 bit float
# 'l': long
# 'i': 32 bit integer
# 'h': 16 bit integer
# 'b': 8 bit integer
# 'S1': character

# Create an attribute
f.variables['temperature'].units = "K"

# Add scaling factor and an offset
# when the variable will be read, it will need to be multiplied by the scale_factor first and the added to the offset value
f.variables['temperature'].scale_factor = 0.1
f.variables['temperature'].add_offset = 273

# create variable contents and assign it to the NetCDF variable
temp = numpy.arange(0,100,1, dtype = numpy.int32)
f.variables['temperature'][:] = temp

f.close()
Line 11: Line 87:

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

PyNIO

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 # Dimension types:
  14 #    'd': 64 bit float
  15 #    'f': 32 bit float
  16 #    'l': long
  17 #    'i': 32 bit integer
  18 #    'h': 16 bit integer
  19 #    'b': 8 bit integer
  20 #    'S1': character 
  21 
  22 # Create an attribute 
  23 f.variables['temperature'].units = "K"
  24 
  25 # Add scaling factor and an offset
  26 # when the variable will be read, it will need to be multiplied by the scale_factor first and the added to the offset value
  27 f.variables['temperature'].scale_factor = 0.1
  28 f.variables['temperature'].add_offset = 273
  29 
  30 # create variable contents and assign it to the NetCDF variable
  31 temp = numpy.arange(0,100,1, dtype = numpy.int32)
  32 f.variables['temperature'][:] = temp
  33 
  34 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)