nctoh5.py :  » Database » PyTables » tables-2.1.2 » tables » netcdf3 » scripts » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyTables 
PyTables » tables 2.1.2 » tables » netcdf3 » scripts » nctoh5.py
"""
convert netCDF file to HDF5 using Scientific.IO.NetCDF and PyTables.
Jeff Whitaker <jeffrey.s.whitaker@noaa.gov>

Added some flags to select filters, as well as some small improvements.
Francesc Alted <faltet@pytables.com>

This requires Scientific from
http://starship.python.net/~hinsen/ScientificPython
"""

import sys, os.path, getopt, time

import tables.netcdf3

from tables.leaf import Filters


def nctoh5(ncfilename, h5filename, filters, verbose, overwritefile):
    # open h5 file
    if overwritefile:
        h5file = tables.netcdf3.NetCDFFile(h5filename, mode = "w")
    else:
        h5file = tables.netcdf3.NetCDFFile(h5filename, mode = "a")
    # convert to netCDF
    nobjects, nbytes = h5file.nctoh5(ncfilename,filters=filters)
    # ncdump-like output
    if verbose:
        print 'contents of hdf5 file:'
        print '----------------------'
        print h5file
    # Close the file
    h5file.close()
    return nobjects, nbytes


def main():
    if not tables.netcdf3.ScientificIONetCDF_imported:
        sys.stderr.write(
            'You need Scientific Python installed in order to use this utility.\n')
        sys.exit(1)

    usage = """usage: %s [-h] [-v] [-o] [--complevel=(0-9)] [--complib=lib] [--shuffle=(0|1)] [--fletcher32=(0|1)] netcdffilename hdf5filename
     -h -- Print usage message.
     -v -- Show more information.
     -o -- Overwite destination file.
     --complevel=(0-9) -- Set a compression level (0 for no compression, which
         is the default).
     --complib=lib -- Set the compression library to be used during the copy.
         lib can be set to "zlib", "lzo" or "ucl". Defaults to "zlib".
     --shuffle=(0|1) -- Activate or not the shuffling filter (default is active
         if complevel>0).
     --fletcher32=(0|1) -- Whether to activate or not the fletcher32 filter (not
         active by default).
    \n""" % os.path.basename(sys.argv[0])

    try:
        opts, pargs = getopt.getopt(sys.argv[1:], 'hvo',
                                    ['complevel=',
                                     'complib=',
                                     'shuffle=',
                                     'fletcher32=',
                                     ])
    except:
        (type, value, traceback) = sys.exc_info()
        print "Error parsing the options. The error was:", value
        sys.stderr.write(usage)
        sys.exit(0)

    # default options
    verbose = 0
    overwritefile = 0
    complevel = None
    complib = None
    shuffle = 0
    fletcher32 = 0

    # Get the options
    for option in opts:
        if option[0] == '-h':
            sys.stderr.write(usage)
            sys.exit(0)
        elif option[0] == '-v':
            verbose = 1
        elif option[0] == '-o':
            overwritefile = 1
        elif option[0] == '--complevel':
            complevel = int(option[1])
        elif option[0] == '--complib':
            complib = option[1]
        elif option[0] == '--shuffle':
            shuffle = int(option[1])
        elif option[0] == '--fletcher32':
            fletcher32 = int(option[1])
        else:
            print option[0], ": Unrecognized option"
            sys.stderr.write(usage)
            sys.exit(0)

    # if we pass a number of files different from 2, abort
    if len(pargs) <> 2:
        print "You need to pass both source and destination!."
        sys.stderr.write(usage)
        sys.exit(0)

    # Catch the files passed as the last arguments
    ncfilename = pargs[0]
    h5filename = pargs[1]


    # Build the Filters instance
    if complevel==None and complib==None and shuffle==0 and fletcher32==0:
        filters = None
    else:
        if complevel is None: complevel = 0
        if complib is None: complib = "zlib"
        if fletcher32 is None: fletcher32 = 0
        filters = Filters(complevel=complevel, complib=complib,
                          shuffle=shuffle, fletcher32=fletcher32)

    # Some timing
    t1 = time.time()
    cpu1 = time.clock()
    # Copy the file
    if verbose:
        print "+=+"*20
        print "Starting conversion from %s to %s" % (ncfilename, h5filename)
        if filters == None:
            print "Using default filters (complevel=6,complib='zlib',shuffle=1,fletcher32=0)"
        else:
            print "Applying filters:", filters
        print "+=+"*20

    # Do the conversion
    (nobjects, nbytes) = nctoh5(ncfilename, h5filename, filters, verbose, overwritefile)

    # Gather some statistics
    t2 = time.time()
    cpu2 = time.clock()
    tcopy = round(t2-t1, 3)
    cpucopy = round(cpu2-cpu1, 3)
    tpercent = int(round(cpucopy/tcopy, 2)*100)
    if verbose:
        print "Number of variables copied:", nobjects
        print "KBytes copied:", round(nbytes/1024.,3)
        print "Time copying: %s s (real) %s s (cpu)  %s%%" % \
              (tcopy, cpucopy, tpercent)
        print "Copied variable/sec: ", round(nobjects / float(tcopy),1)
        print "Copied KB/s :", int(nbytes / (tcopy * 1024))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.