Differences between revisions 3 and 6 (spanning 3 versions)
Revision 3 as of 2008-04-16 17:45:35
Size: 710
Editor: anonymous
Comment:
Revision 6 as of 2008-04-17 14:22:22
Size: 2230
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
The file object is used for reading and writing plain text as well as unformatted binary data. The following Codeline
writes a message in the file with the name ''out.txt''
The [[http://docs.python.org/lib/bltin-file-objects.html|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
Line 17: Line 18:
file('out.txt','wb').write('Hallo Datentraeger') file('out.txt','w').write('Hallo Datentraeger')
print file('out.txt').read()
Line 20: Line 22:
The data can be read in with the method '''.read()'''
{{{#!python
s=file('out.txt').read()
print s
'Hallo Datentraeger'
}}}
 * {{{write()}}} writes a string to the file
 * {{{read()}}} reads complete file
 * {{{read(N)}}} reads N bytes
 * {{{readlines()}}} reads the file with linebreaks
 * {{{readline()}}} reads only the next line
Line 34: Line 34:
NASA's standard file format, the [[http://hdf.ncsa.uiuc.edu/index.html||Hierarchical Data Format (HDF)]] 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 ===
[[http://pysclint.sourceforge.net/pyhdf/|pyhdf]] is a python interface to the NCSA HDF4 library.

The following example demonstrates how to read level-3 data from the [[http://eosweb.larc.nasa.gov/PRODOCS/misr/level3/download_data.html|MISR instrument]]

{{{#!python
from scipy import array
from pylab import imshow,colorbar,title,savefig
from pyhdf.SD import SD

f=SD('MISR_AM1_CGLS_MAY_2007_F04_0025.hdf')
print f.datasets().keys()
data=array(f.select('NDVI average').get())
data[data<0]=0

imshow(data,interpolation='nearest',cmap=cm.YlGn)
colorbar()
title('Normalized Difference Vegetation Index')
savefig('ndvi.png',dpi=100)
}}}

Line 5 opens the HDF file object. line 6 prints the keywords of the included datasets. From this one can identify the keyword for the desired parameter. Line 7 reads the data in a SciPy array. Line 8 selects the bad data and sets them to zero.
 
{{attachment:ndvi.png}}

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()
  • write() writes a string to the file

  • read() reads complete file

  • read(N) reads N bytes

  • readlines() reads the file with linebreaks

  • readline() reads only the next line

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 following example demonstrates how to read 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 f=SD('MISR_AM1_CGLS_MAY_2007_F04_0025.hdf')
   6 print f.datasets().keys()
   7 data=array(f.select('NDVI average').get())
   8 data[data<0]=0
   9 
  10 imshow(data,interpolation='nearest',cmap=cm.YlGn)
  11 colorbar()
  12 title('Normalized Difference Vegetation Index')
  13 savefig('ndvi.png',dpi=100)

Line 5 opens the HDF file object. line 6 prints the keywords of the included datasets. From this one can identify the keyword for the desired parameter. Line 7 reads the data in a SciPy array. Line 8 selects the bad data and sets them to zero.

ndvi.png

netCDF

Various Satellite data formats

LehreWiki: SiaProgrammingPythonIo (last edited 2008-04-21 12:10:37 by anonymous)