From: Jeff W. <jef...@no...> - 2004-10-24 12:17:07
|
Hi: I just wrote a script to convert a netcdf datasets to hdf5. It's posted below, in case others might find it useful (you can try it on the files at ftp://ftp.cdc.noaa.gov/Datasets/ncep.reanalysis). I have a question regarding EArrays - I see how to append values and to write the entire array at once, but is there a way to update an array record without re-writing the whole thing? Here's the nctoh5 script (requires Scientific from http://starship.python.net/~hinsen/ScientificPython). """ convert netCDF file to HDF5 using Scientific.IO.NetCDF and PyTables. Jeff Whitaker <jef...@no...> """ import Scientific.IO.NetCDF as NetCDF import tables, sys # open netCDF file ncfile = NetCDF.NetCDFFile(sys.argv[1],mode = "r") # open h5 file. h5file = tables.openFile(sys.argv[2], mode = "w") # loop over variables in netCDF file. for varname in ncfile.variables.keys(): var = ncfile.variables[varname] vardims = list(var.dimensions) vardimsizes = [ncfile.dimensions[vardim] for vardim in vardims] # use long_name for title. if hasattr(var,'long_name'): title = var.long_name else: # or, just use some bogus title. title = varname + ' array' # if variable has unlimited dimension or has rank>1, # make it enlargeable (with zlib compression). if vardimsizes[0] == None or len(vardimsizes) > 1: vardimsizes[0] = 0 vardata = h5file.createEArray(h5file.root, varname, tables.Atom(shape=tuple(vardimsizes),dtype=var.typecode(),), title,filters=tables.Filters(complevel=6,complib='zlib')) # write data to enlargeable array on record at a time. # (so the whole array doesn't have to be kept in memory). for n in range(var.shape[0]): vardata.append(var[n:n+1]) # or else, create regular array write data to it all at once. else: vardata=h5file.createArray(h5file.root,varname,var[:],title) # set variable attributes. for key,val in var.__dict__.iteritems(): setattr(vardata.attrs,key,val) setattr(vardata.attrs,'dimensions',tuple(vardims)) # set global (file) attributes. for key,val in ncfile.__dict__.iteritems(): setattr(h5file.root._v_attrs,key,val) # Close the file h5file.close() -Jeff -- Jeffrey S. Whitaker Phone : (303)497-6313 NOAA/OAR/CDC R/CDC1 FAX : (303)497-6449 325 Broadway Web : http://www.cdc.noaa.gov/~jsw Boulder, CO, USA 80305-3328 Office: Skaggs Research Cntr 1D-124 |