SiaProgrammingPython

Input/Output

This lesson deals with the ways of reading and writing data

Basic Python

The file object can be used for reading and writing plain text as well as unformatted binary data. The following code writes a message in the file with the name out.txt, reads and print the data

   1 file('out.txt','w').write('Hallo Datentraeger')
   2 print file('out.txt').read()

NumPy/SciPy

HDF

NASA's standard file format, the http://hdf.ncsa.uiuc.edu/index.html is a self-describing data format. HDF files can contain binary data and allow direct access to parts of the file without first parsing the entire contents.

The HDF versions 4 and 5 are not compatible.

Different modules are available for reading and writing HDF files

pyhdf

pyhdf is a python interface to the NCSA HDF4 library. The interface is easy to use.

The following example demonstrates how to use the pyhdf module for reading level-3 data from the MISR instrument

   1 from scipy import array
   2 from pylab import imshow,colorbar,title,savefig
   3 from pyhdf.SD import SD
   4 
   5 #http://eosweb.larc.nasa.gov/PRODOCS/misr/level3/download_data.html
   6 
   7 f=SD('MISR_AM1_CGLS_MAY_2007_F04_0025.hdf')
   8 print f.datasets().keys()
   9 data=array(f.select('NDVI average').get())
  10 data[data<0]=0
  11 
  12 imshow(data,interpolation='nearest',cmap=cm.YlGn)
  13 colorbar()
  14 title('Normalized Difference Vegetation Index')
  15 savefig('ndvi.png',dpi=100)

attachment:ndvi.png

netCDF

Various Satellite data formats