PyTables User's Guide: Hierarchical datasets in Python - Release 1.3.3
Prev | Chapter 7. NetCDF - a PyTables NetCDF3 emulation API | Next

# 7.4. Converting between true netCDF files and tables.NetCDF files

If `Scientific.IO.NetCDF` is installed, `tables.NetCDF` provides facilities for converting between true netCDF version 3 files and `tables.NetCDF` hdf5 files via the `NetCDFFile.h5tonc()` and `NetCDFFile.nctoh5()` class methods. Also, the `nctoh5` command-line utility (see Appendix C.3) uses the `NetCDFFile.nctoh5()` class method.

As an example, look how to convert a `tables.NetCDF` hdf5 file to a true netCDF version 3 file (named `test.nc`)

    >>> scale_factor = {'temp': 1.75e-4}
    >>> add_offset = {'temp': 5.}
    >>> file.h5tonc('test.nc',packshort=True, \
                     scale_factor=scale_factor,add_offset=add_offset)
    packing temp as short integers ...
    >>> file.close()

The dictionaries `scale_factor` and `add_offset` are used to optionally pack the data as short integers in the netCDF file. Since netCDF version 3 does not provide automatic compression, packing as short integers is a commonly used way of saving disk space (see this page for more details). The keys of these dictionaries are the variable names to pack, the values are the scale_factors and offsets to use in the packing. The resulting netCDF file will have the `scale_factor` and `add_offset` variable attributes set appropriately.

To convert the netCDF file back to a `tables.NetCDF` hdf5 file:

    >>> history = 'Convert from netCDF ' + time.ctime(time.time())
    >>> file = NetCDF.NetCDFFile('test2.h5', 'w', history=history)
    >>> nobjects, nbytes = file.nctoh5('test.nc',unpackshort=True)
    >>> print nobjects,' objects converted from netCDF, totaling',nbytes,'bytes'
    5  objects converted from netCDF, totaling 48008 bytes
    >>> temp = file.variables['temp']
    >>> times = file.variables['time']
    >>> print 'temp.shape after h5 --> netCDF --> h5 conversion = ',temp.shape
    temp.shape after h5 --> netCDF --> h5 conversion =  (10L, 12, 90)
    >>> for n in range(10):
    >>>     print 'time, min/max temp, temp[n,0,0] = ',\
                   times[n],min(temp[n].flat),max(temp[n].flat),temp[n,0,0]
    time, min/max temp, temp[n,0,0] = 0.0 0.0123250000179 9.99257469177 6.13049983978
    time, min/max temp, temp[n,0,0] = 1.0 0.00130000000354 9.99152469635 6.68507480621
    time, min/max temp, temp[n,0,0] = 2.0 0.0153000000864 9.98732471466 3.60542488098
    time, min/max temp, temp[n,0,0] = 3.0 0.0112749999389 9.99520015717 6.2423248291
    time, min/max temp, temp[n,0,0] = 4.0 0.00532499980181 9.99817466736 0.225124999881
    time, min/max temp, temp[n,0,0] = 5.0 0.00987500045449 9.98417472839 4.56827497482
    time, min/max temp, temp[n,0,0] = 6.0 0.01600000076 9.99152469635 6.36832523346
    time, min/max temp, temp[n,0,0] = 7.0 0.00200000009499 9.99922466278 1.42772495747
    time, min/max temp, temp[n,0,0] = 8.0 0.00392499985173 9.9908246994 2.79605007172
    time, min/max temp, temp[n,0,0] = 9.0 0.0107500003651 9.99187469482 8.18832492828
    >>> file.close()

Setting `unpackshort=True` tells `nctoh5` to unpack all of the variables which have the `scale_factor` and `add_offset` attributes back to floating point arrays. Note that `tables.NetCDF` files have some features not supported in netCDF (such as Complex data types and the ability to make any dimension unlimited). `tables.NetCDF` files which utilize these features cannot be converted to netCDF using `NetCDFFile.h5tonc`.

Prev | Home | Next
tables.NetCDF module reference | Up | tables.NetCDF file structure