pyhdf.VS | index /usr/lib/python2.4/site-packages/pyhdf/VS.py |
A module of the pyhdf package implementing the VS (Vdata table)
API of the NCSA HDF4 library.
(see: hdf.ncsa.uiuc.edu)
Author: Andre Gosselin
Maurice-Lamontagne Institute
gosselina@dfo-mpo.gc.ca
Version: 0.7-3
Date: July 13 2005
Table of contents
-----------------
Introduction
VS module key features
Accessing the VS module
Package components
Prerequisites
Documentation
Summary of differences between the pyhdf and C VS API
Error handling
VS needs support from the HDF module
Classes summary
Data types
Attribute access: low and high level
Predefined attributes
Record access: low and high level
Programming models
Creating and initializing a new vdata
Appending records to a vdata
Updating records in a vdata
Reading a vdata
Module documentation
Introduction
------------
VS is one of the modules composing pyhdf, a python package implementing
the NCSA HDF library and letting one manage HDF files from within a python
program. Two versions of the HDF library currently exist, version 4 and
version 5. pyhdf only implements version 4 of the library. Many
different APIs are to be found inside the HDF4 specification.
Currently, pyhdf implements just a few of those: the SD, VS and V APIs.
Other APIs should be added in the future (GR, AN, etc).
VS allows the definition of structured data tables inside an HDF file.
Those tables are designated as "vdatas" (the name has to do with data
associated with the "vertices" of geometrical models, the storage of which
the API was originally designed for). A vdata is composed of a fixed
number of columns (also called fields), where a column can store a fixed
number of data values, all of the same type. The number of values allowed
inside a field is called the "order" of the field. A table is composed of a
varying number of rows (also called records), a record representing the
sequence of values stored in each field of the vdata.
A vdata is associated with a descriptive name, and likewise each field of
the vdata. A vdata can also be tagged with a "class" to further describe the
vdata purpose. Records and fields are identified by a zero-based index.
An arbitrary number of attributes of different types can be attached to
a vdata as a whole, or to its individual fields. An attribute is a
(name, value) pair, where "value" can be of many types, and be either
single or multi-valued. The number of values stored in an attribute is
called the "order" of the attribute.
The following example illustrates a simple vdata that could be stored
inside an HDF file. See section "Programming models" for an example
program implementing this vdata.
INVENTORY (experimental status)
------------------------------------------i-------
partid description qty wght(lb) price($)
--------------------------------------------------
Q1234 bolt 12 0.01 0.05
B5432 brush 10 0.4 4.25
S7613 scissor 2 0.2 3.75
The vdata is composed of 5 fields. 3 records are shown (of course, a vdata
can store much more than that). "INVENTORY" would be the vdata name, and
"partid", "description", etc, would be the field names. The data type varies
between fields. "partid" and "description" would be of "multicharacter" type
(aka "string"), "qty" would be a integer, and "wght" and "price" would be
floats. The text in parentheses could be stored as attributes. A "status"
attribute could be defined for the table as a whole, and given the
value "experimental". Likewise, a "unit" attribute could be associated
with fields "wght" and "price", and given the values "lb" and "$", resp.
The VS API allows one to create, locate and open a vdata inside an
HDF file, update and append records inside it, read records randomly
or sequentially, and access and update the vdata and field attributes.
Attributes can be read and written using the familiar python "dot
notation", and records can be read and written by indexing and slicing the
vdata as if it were a python sequence.
VS module key features
----------------------
VS key features are as follows.
-pyhdf implements almost every routine of the original VS API.
Only a few have been ignored, most of them being of a rare use:
- VSgetblocksize() / VSsetblocksize()
- VSsetnumblocks()
- VSlone
-It is quite straightforward to go from a C version to a python version
of a program accessing the VS API, and to learn VS usage by refering to
the C API documentation.
-A few high-level python methods have been developped to ease
programmers task. Of greatest interest are the following:
- Access to attributes through the familiar "dot notation".
- Indexing and slicing a vdata to read and write its records,
similarly to a python sequence.
- Easy retrieval of info on a vdata and its fields.
- Easy creation of vdatas.
Accessing the VS module
-----------------------
To access the VS module a python program can say one of:
>>> import pyhdf.VS # must prefix names with "pyhdf.VS."
>>> from pyhdf import VS # must prefix names with "VS."
>>> from pyhdf.VS import * # names need no prefix
This document assumes the last import style is used.
VS is not self-contained, and needs functionnality provided by another
pyhdf module, namely the HDF module. This module must thus be imported
also:
>>> from HDF import *
Package components
------------------
pyhdf is a proper Python package, eg a collection of modules stored under
a directory whose name is that of the package and which stores an
__init__.py file. Following the normal installation procedure, this
directory will be <python-lib>/site-packages/pyhdf', where <python-lib>
stands for the python installation directory.
For each HDF API exists a corresponding set of modules.
The following modules are related to the VS API.
_hdfext C extension module responsible for wrapping the HDF
C library for all python modules
hdfext python module implementing some utility functions
complementing the _hdfext extension module
error defines the HDF4Error exception
HDF python module providing support to the VS module
VS python module wrapping the VS API routines inside
an OOP framework
_hdfext and hdfext were generated using the SWIG preprocessor.
SWIG is however *not* needed to run the package. Those two modules
are meant to do their work in the background, and should never be called
directly. Only HDF and VS should be imported by the user program.
Prerequisites
-------------
The following software must be installed in order for VS to
work.
HDF (v4) library
pyhdf does *not* include the HDF4 library, which must
be installed separately.
HDF is available at:
"http://hdf.ncsa.uiuc.edu/obtain.html".
Numeric is also needed by the SD module. See the SD module documentation.
Documentation
-------------
pyhdf has been written so as to stick as closely as possible to
the naming conventions and calling sequences documented inside the
"HDF User s Guide" manual. Even if pyhdf gives an OOP twist
to the C API, the manual can be easily used as a documentary source
for pyhdf, once the class to which a function belongs has been
identified, and of course once requirements imposed by the Python
langage have been taken into account. Consequently, this documentation
will not attempt to provide an exhaustive coverage of the HDF VS
API. For this, the user is referred to the above manual.
The documentation of each pyhdf method will indicate the name
of the equivalent routine as it is found inside the C API.
This document (in both its text and html versions) has been completely
produced using "pydoc", the Python documentation generator (which
made its debut in the 2.1 Python release). pydoc can also be used
as an on-line help tool. For example, to know everything about
the VS.VD class, say:
>>> from pydoc import help
>>> from pyhdf.VS import *
>>> help(VD)
To be more specific and get help only for the read() method of the
VD class:
>>> help(VD.read)
pydoc can also be called from the command line, as in:
% pydoc pyhdf.VS.VD # doc for the whole VD class
% pydoc pyhdf.VS.VD.read # doc for the VD.read method
Summary of differences between the pyhdf and C VS API
-----------------------------------------------------
Most of the differences between the pyhdf and C VS API can
be summarized as follows.
-In the C API, every function returns an integer status code, and values
computed by the function are returned through one or more pointers
passed as arguments.
-In pyhdf, error statuses are returned through the Python exception
mechanism, and values are returned as the method result. When the
C API specifies that multiple values are returned, pyhdf returns a
sequence of values, which are ordered similarly to the pointers in the
C function argument list.
Error handling
--------------
All errors reported by the C VS API with a SUCCESS/FAIL error code
are reported by pyhdf using the Python exception mechanism.
When the C library reports a FAIL status, pyhdf raises an HDF4Error
exception (a subclass of Exception) with a descriptive message.
Unfortunately, the C library is rarely informative about the cause of
the error. pyhdf does its best to try to document the error, but most
of the time cannot do more than saying "execution error".
VS needs support from the HDF module
------------------------------------
The VS module is not self-contained (countrary to the SD module).
It requires help from the HDF module, namely:
-the HDF.HDF class to open and close the HDF file, and initialize the
VS interface
-the HDF.HC class to provide different sorts of constants (opening modes,
data types, etc).
A program wanting to access HDF vdatas will almost always need to execute
the following minimal set of calls:
>>> from pyhdf.HDF import *
>>> from pyhdf.VS import *
>>> hdfFile = HDF(name, HC.xxx)# open HDF file
>>> vs = hdfFile.vstart() # initialize VS interface on HDF file
>>> ... # manipulate vdatas through "vs"
>>> vs.end() # terminate VS interface
>>> hdfFile.close() # close HDF file
Classes summary
---------------
pyhdf wraps the VS API using different python classes:
VS HDF VS interface
VD vdata
VDField vdata field
VDattr attribute (either at the vdata or field level)
In more detail:
VS The VS class implements the VS (Vdata) interface applied to an
HDF file. This class encapsulates the hdf instance, and all
the top-level functions of the VS API.
To create a VS instance, call the vstart() method of an
HDF instance.
methods:
constructors:
attach() open an existing vdata given its name or
reference number, or create a new one,
returning a VD instance
create() create a new vdata and define its structure,
returning a VD instance
creating and initializing a simple vdata
storedata() create a single-field vdata and initialize
its values
closing the interface
end() close the VS interface on the HDF file
searching
find() get a vdata reference number given its name
next() get the reference number of the vdata following
a given one
inquiry
vdatainfo() return info about all the vdatas in the
HDF file
VD The VD class describes a vdata. It encapsulates
the VS instance to which the vdata belongs, and the vdata
identifier.
To instantiate a VD class, call the attach() or create()
method of a VS class instance.
methods:
constructors
attr() create a VDAttr instance representing a
vdata attribute; "dot notation" can also be
used to access a vdata attribute
field() return a VDField instance representing a given
field of the vdata
closing vdata
detach() end access to the vdata
defining fields
fdefine() define the name, type and order of a new field
setfields() define the field names and field order for
the read() and write() methods; also used to
initialize the structure of a vdata previously
created with the VS.attach() method
reading and writing
note: a vdata can be indexed and sliced like a
python sequence
read() return the values of a number of records
starting at the current record position
seek() reset the current record position
seekend() seek past the last record
tell() return the current record position
write() write a number of records starting at the
current record position
inquiry
attrinfo() return info about all the vdata attributes
fexist() check if a vdata contains a given set of fields
fieldinfo() return info about all the vdata fields
findattr() locate an attribute, returning a VDAttr instance
if found
inquire() return info about the vdata
sizeof() return the size in bytes of one or more fields
VDField The VDField class represents a vdata field. It encapsulates
the VD instance to which the field belongs, and the field
index number.
To instantiate a VDField, call the field() method of a VD class
instance.
methods:
constructors:
attr() return a VDAttr instance representing an
attribute of the field; "dot notation"
can also be used to get/set an attribute.
inquiry
attrinfo() return info about all the field attributes
find() locate an attribute, returning a VDAttr
instance if found
VDAttr The VDAttr class encapsulates methods used to set and query
attributes defined at the level either of the vdata or the
vdata field.
To create an instance of this class, call the attr() or
findattr() methods of a VD instance (for vdata attributes),
or call the attr() or find() methods of a VDField instance
(for field attributes).
methods:
get / set
get() get the attribute value
set() set the attribute value
info
info() retrieve info about the attribute
Data types
----------
Data types come into play when first defining vdata fields and attributes,
and later when querying the definition of those fields and attributes.
Data types are specified using the symbolic constants defined inside the
HC class of the HDF module.
- CHAR and CHAR8 (equivalent): an 8-bit character.
- UCHAR, UCHAR8 and UINT8 (equivalent): unsigned 8-bit values (0 to 255)
- INT8: signed 8-bit values (-128 to 127)
- INT16: signed 16-bit values
- UINT16: unsigned 16 bit values
- INT32: signed 32 bit values
- UINT32: unsigned 32 bit values
- FLOAT32: 32 bit floating point values (C floats)
- FLOAT64: 64 bit floating point values (C doubles)
There is no explicit "string" type. To simulate a string, set the field or
attribute type to CHAR, and set the field or attribute "order" to
a value of 'n' > 1. This creates and "array of characters", close
to a string (except that strings will always be of length 'n', right-padded
with spaces if necessary).
Attribute access: low and high level
------------------------------------
The VS API allow setting attributes on vdatas and vdata fields. Attributes
can be of many types (int, float, char) of different bit lengths (8, 16, 32,
64 bits), and can be single or multi-valued. Values of a multi-valued
attribute must all be of the same type.
Attributes can be set and queried in two different ways. First, given a
VD instance (describing a vdata object) or a VDField instance (describing a
vdata field), the attr() method of that instance is called to create a
VDAttr instance representing the wanted attribute (possibly non existent).
The set() method of this VDAttr instance is then called to define the
attribute value, creating it if it does not already exist. The get() method
returns the current attribute value. Here is an example.
>>> from pyhdf.HDF import *
>>> from pyhdf.VS import *
>>> f = HDF('test.hdf', HC.WRITE) # Open file 'test.hdf' in write mode
>>> vs = f.vstart() # init vdata interface
>>> vd = vs.attach('vtest', 1) # attach vdata 'vtest' in write mode
>>> attr = vd.attr('version') # prepare to define the 'version' attribute
# on the vdata
>>> attr.set(HC.CHAR8,'1.0') # set attribute 'version' to string '1.0'
>>> print attr.get() # get and print attribute value
>>> fld = vd.field('fld1') # obtain a field instance for field 'fld1'
>>> attr = fld.attr('range') # prepare to define attribute 'range' on
# this field
>>> attr.set(HC.INT32,(-10, 15)) # set attribute 'range' to a pair of ints
>>> print attr.get() # get and print attribute value
>>> vd.detach() # "close" the vdata
>>> vs.end() # terminate the vdata interface
>>> f.close() # close the HDF file
The second way consists of setting/querying an attribute as if it were a
normal python class attribute, using the usual dot notation. Above example
then becomes:
>>> from pyhdf.HDF import *
>>> from pyhdf.VS import *
>>> f = HDF('test.hdf', HC.WRITE) # Open file 'test.hdf' in write mode
>>> vs = f.vstart() # init vdata interface
>>> vd = vs.attach('vtest', 1) # attach vdata 'vtest' in write mode
>>> vd.version = '1.0' # create vdata attribute 'version',
# setting it to string '1.0'
>>> print vd.version # print attribute value
>>> fld = vd.field('fld1') # obtain a field instance for field 'fld1'
>>> fld.range = (-10, 15) # create field attribute 'range', setting
# it to the pair of ints (-10, 15)
>>> print fld.range # print attribute value
>>> vd.detach() # "close" the vdata
>>> vs.end() # terminate the vdata interface
>>> f.close() # close the HDF file
Note how the dot notation greatly simplifies and clarifies the code.
Some latitude is however lost by manipulating attributes in that way,
because the pyhdf package, not the programmer, is then responsible of
setting the attribute type. The attribute type is chosen to be one of:
HC.CHAR8 if the attribute value is a string
HC.INT32 if all attribute values are integers
HC.FLOAT64 otherwise
The first way of handling attribute values must be used if one wants to
define an attribute of any other type (for ex. 8 or 16 bit integers,
signed or unsigned). Also, only a VDAttr instance gives access to attribute
info, through its info() method.
However, accessing HDF attributes as if they were python attributes raises
an important issue. There must exist a way to assign generic attributes
to the python objects without requiring those attributes to be converted
to HDF attributes. pyhdf uses the following rule: an attribute whose name
starts with an underscore ('_') is either a "predefined" attribute
(see below) or a standard python attribute. Otherwise, the attribute
is handled as an HDF attribute. Also, HDF attributes are not stored inside
the object dictionnary: the python dir() function will not list them.
Attribute values can be updated, but it is illegal to try to change the
value type, or the attribute order (number of values). This is important
for attributes holding string values. An attribute initialized with an
'n' character string is simply a character attribute of order 'n' (eg a
character array of length 'n'). If 'vd' is a vdata and we initialize its
'a1' attribute as 'vd.a1 = "abcdef"', then a subsequent update attempt
like 'vd.a1 = "12"' will fail, because we then try to change the order
of the attribute (from 6 to 2). It is mandatory to keep the length of string
attributes constant. Examples below show simple ways how this can be done.
Predefined attributes
---------------------
The VD and VDField classes support predefined attributes to get (and
occasionnaly set) attribute values easily, without having to call a
class method. The names of predefined attributes all start with an
underscore ('_').
In the following tables, the RW column holds an X if the attribute
is read/write. See the HDF User s guide for details about more
"exotic" topics like "class", "faked vdata" and "tag".
VD predefined attributes
name RW description C library routine
--------------------------------------------------------------
_class X class name VSgetclass/VSsetclass
_fields list of field names VSgetfields
_interlace X interlace mode VSgetinterlace/VSsetinterlace
_isattr true if vdata is "faked" VSisattr
by HDF to hold attributes
_name X name of the vdata VSgetname/VSsetname
_nattrs number of attributes VSfnattrs
_nfields number of fields VFnfields
_nrecs number of records VSelts
_recsize record size (bytes) VSQueryvsize
_refnum reference number VSQueryref
_tag vdata tag VSQuerytag
_tnattrs total number of vdata and VSnattrs
field attributes
VDField predefined attributes
name RW description C library routine
--------------------------------------------------------------
_esize external size (bytes) VFfieldesize
_index index number VSfindex
_isize internal size (bytes) VFfieldisize
_name name VFfieldname
_nattrs number of attributes VSfnattrs
_order order (number of values) VFfieldorder
_type field type (HC.xxx) VFfieldtype
Record access: low and high level
---------------------------------
vdata records can be read and written in two different ways. The first one
consists of calling the basic I/O methods of the vdata:
- seek() to set the current record position, if necessary;
- read() to retrieve a given number of records from that position;
- write() to write a given number of records starting at
that position
A second, higher level way, lets one see a vdata similarly to a python
sequence, and access its contents using the familiar indexing and slicing
notation in square brackets. Reading and writing a vdata as if it were a
python sequence may often look simpler, and improve code legibility.
Here are some examples of how a vdata 'vd' holding 3 fields could be read.
>>> print vd[0] # print record 0
>>> print vd[-1] # print last record
>>> print vd[2:] # print records 2 and those that follow
>>> print vd[:] # print all records
>>> print vd[:,0] # print field 0 of all records
>>> print vd[:3,:2] # print first 2 fields of first 3 records
As the above examples show, the usual python rules are obeyed regarding
the interpretation of indexing and slicing values. Note that the vdata
fields can be indexed and sliced, not only the records. The setfields()
method can also be used to select a subset to the vdata fields
(setfields() also let you reorder the fields). When the vdata is
indexed (as opposed to being sliced), a single record is returned as a list
of values. When the vdata is sliced, a list of records is
always returned (thus a 2-level list), even if the slice contains only
one record.
A vdata can also be written similarly to a python sequence. When indexing
the vdata (as opposed to slicing it), a single record must be assigned,
and the record must be given as a sequence of values. It is legal to use
as an index the current number of records in the vdata: the record is then
appended to the vdata. When slicing the vdata, the records assigned to the
slice must always be given as a list of records, even
if only one record is assigned. Also, the number of records assigned must
always match the width of the slice, except if the slice includes or goes
past the last record of the vdata. In that case, the number of records
assigned can exceed the width of the slice, and the extra records are
appended to the vdata. So, to append records to vdata 'vd', simply
assign records to the slice 'vd[vd._nrecs:]'. Note that, even if the
'field' dimension can be specified in the left-hand side expression,
there is no real interest in doing so, since all fields must
be specified when assigning a record to the vdata: it is an error to
try to assign just a few of the fields.
For example, given a vdata 'vd' holding 5 records, and lists 'reca',
'recb', etc, holding record values:
vd[0] = reca # updates record 0
vd[0,:] = reca # specifying fields is OK, but useless
vd[0,1:] = reca[1:] # error: all fields must be assigned
vd[1] = [recb, recc] # error: only one record allowed
vd[5] = recc # append one record
vd[1:3] = [reca,recb] # updates second and third record
vd[1:4] = [reca, recb] # error: 3 records needed
vd[5:] = [reca,recb] # appends 2 records to the vdata
vd[4:] = [reca, recb] # updates last record, append one
Programming models
------------------
Creating and initializing a new vdata
-------------------------------------
The following code can serve as a model for the creation and
initialization of a new vdata. It implements the INVENTORY example
described in the "Introduction" section.
from pyhdf.HDF import *
from pyhdf.VS import *
# Open HDF file and initialize the VS interface
f = HDF('inventory.hdf', # Open file 'inventory.hdf' in write mode
HC.WRITE|HC.CREATE) # creating it if it does not exist
vs = f.vstart() # init vdata interface
# Create vdata and define its structure
vd = vs.create( # create a new vdata
'INVENTORY', # name of the vdata
# fields of the vdata follow
(('partid',HC.CHAR8, 5), # 5 char string
('description',HC.CHAR8, 10), # 10 char string field
('qty',HC.INT16, 1), # 1 16 bit int field
('wght',HC.FLOAT32, 1), # 1 32 bit float
('price',HC.FLOAT32,1) # 1 32 bit float
)) # 5 fields allocated in the vdata
# Set attributes on the vdata and its fields
vd.field('wght').unit = 'lb'
vd.field('price').unit = '$'
# In order to be able to update a string attribute, it must
# always be set to the same length. This sets 'status' to a 20
# char long, left-justified string, padded with spaces on the right.
vd.status = "%-20s" % 'phase 1 done'
# Store records
vd.write(( # write 3 records
('Q1234', 'bolt',12, 0.01, 0.05), # record 1
('B5432', 'brush', 10, 0.4, 4.25), # record 2
('S7613', 'scissor', 2, 0.2, 3.75) # record 3
))
vd.detach() # "close" the vdata
vs.end() # terminate the vdata interface
f.close() # close the HDF file
Note that is mandatory to always write whole records to the vdata.
Note also the comments about the initialization of the 'status'
vdata attribute. We want to be able update this attribute (see
following examples). However, the VS API prohibits changing an attribute
type when updating its value. Since the length (order) of an attribute
is part of its type, we make sure of setting the attribute to a length
long enough to accomodate the longest possible string we migh want to
assign to the attribute.
Appending records to a vdata
----------------------------
Appending records requires first seeking to the end of the vdata, to avoid
overwriting existing records. The following code can serve as a model. The
INVENTORY vdata created before is used.
from pyhdf.HDF import *
from pyhdf.VS import *
f = HDF('inventory.hdf', # Open 'inventory.hdf' in write mode
HC.WRITE|HC.CREATE) # creating it if it does not exist
vs = f.vstart() # init vdata interface
vd = vs.attach('INVENTORY', 1) # attach 'INVENTORY' in write mode
# Update the `status' vdata attribute. The attribute length must not
# change. We call the attribute info() method, which returns a list
# where number of values (eg string length) is stored at index 2.
# We then assign a left justified string of exactly that length.
len = vd.attr('status').info()[2]
vd.status = '%-*s' % (len, 'phase 2 done')
vd[vd._nrecs:] = ( # append 2 records
('A4321', 'axe', 5, 1.5, 25), # first record
('C3214', 'cup', 100, 0.1, 3.25) # second record
)
vd.detach() # "close" the vdata
vs.end() # terminate the vdata interface
f.close() # close the HDF file
Note how, when updating the value of the 'status' vdata attribute,
we take care of assigning a value of the same length as that of the
original value. Otherwise, the assignment would raise an exception.
Records are written by assigning the vdata through a slicing
expression, like a python sequence. By specifying the number of records
as the start of the slice, the records are appended to the vdata.
Updating records in a vdata
---------------------------
Updating requires seeking to the record to update before writing the new
records. New data will overwrite this record and all records that follow,
until a new seek is performed or the vdata is closed. Note that record
numbering starts at 0.
The following code can serve as a model. The INVENTORY vdata created
before is used.
from pyhdf.HDF import *
from pyhdf.VS import *
f = HDF('inventory.hdf', # Open 'inventory.hdf' in write mode
HC.WRITE|HC.CREATE) # creating it if it does not exist
vs = f.vstart() # init vdata interface
vd = vs.attach('INVENTORY', 1) # attach 'INVENTORY' in write mode
# Update the `status' vdata attribute. The attribute length must not
# change. We call the attribute info() method, which returns a list
# where number of values (eg string length) is stored at index 2.
# We then assign a left justified string of exactly that length.
len = vd.attr('status').info()[2]
vd.status = '%-*s' % (len, 'phase 3 done')
# Update record at index 1 (second record)
vd[1] = ('Z4367', 'surprise', 10, 3.1, 44.5)
# Update record at index 4, and all those that follow
vd[4:] = (
('QR231', 'toy', 12, 2.5, 45),
('R3389', 'robot', 3, 45, 2000)
)
vd.detach() # "close" the vdata
vs.end() # terminate the vdata interface
f.close() # close the HDF file
Reading a vdata
---------------
The following example shows how read the vdata attributes and sequentially
maneuver through its records. Note how we use the exception mechanism
to break out of the reading loop when we reach the end of the vdata.
from pyhdf.HDF import *
from pyhdf.VS import *
f = HDF('inventory.hdf') # open 'inventory.hdf' in read mode
vs = f.vstart() # init vdata interface
vd = vs.attach('INVENTORY') # attach 'INVENTORY' in read mode
# Display some vdata attributes
print "status:", vd.status
print "vdata: ", vd._name # predefined attribute: vdata name
print "nrecs: ", vd._nrecs # predefined attribute: num records
# Display value of attribute 'unit' for all fields on which
# this attribute is set
print "units: ",
for fieldName in vd._fields: # loop over all field names
try:
# instantiate field and obtain value of attribute 'unit'
v = vd.field(fieldName).unit
print "%s: %s" % (fieldName, v),
except: # no 'unit' attribute: ignore
pass
print ""
print ""
# Display table header.
header = "%-7s %-12s %3s %4s %8s" % tuple(vd._fields)
print "-" * len(header)
print header
print "-" * len(header)
# Loop over the vdata records, displaying each record as a table row.
# Current record position is 0 after attaching the vdata.
while 1:
try:
rec = vd.read() # read next record
# equivalent to:
# rec = vd[vd.tell()]
print "%-7s %-12s %3d %4.1f %8.2f" % tuple(rec[0])
except HDF4Error: # end of vdata reached
break
vd.detach() # "close" the vdata
vs.end() # terminate the vdata interface
f.close() # close the HDF file
In the previous example, the reading/displaying loop can be greatly
simplified by rewriting it as follows:
from pyhdf.HDF import *
from pyhdf.VS import *
f = HDF('inventory.hdf') # open 'inventory.hdf' in read mode
vs = f.vstart() # init vdata interface
vd = vs.attach('INVENTORY') # attach 'INVENTORY' in read mode
....
# Read all records at once, and loop over the sequence.
for rec in vd[:]:
print "%-7s %-12s %3d %4.1f %8.2f" % tuple(rec)
vd.detach() # "close" the vdata
...
The indexing expression 'vd[:]' returns the complete set of records,
which can then be looped over using a 'for' statement. This style of loop
is quite clean, and should look very familiar to python adepts.
Modules | ||||||
|
Classes | ||||||||||||||||||||||||||||||||||
|
Data | ||
__all__ = ['VS', 'VD', 'VDField', 'VDAttr'] |