def set_variable(self, varname, dtype='f4', dims=None, chunksize=None, fill=None, zlib=False, **kwargs): """ Define (create) a variable in a netCDF object. No data is written to the variable yet. Give the variable's dimensions as a tuple of dimension names. Dimensions must have been previously created with self.netcdf_object.createDimension (e.g. see set_timelatlon()). Recommended ordering of dimensions is: time, height or depth (Z), latitude (Y), longitude (X). Any other dimensions should be defined before (placed to the left of) the spatio-temporal coordinates. To create a scalar variable, use an empty tuple for the dimensions. Variables can be renamed with the 'renameVariable' method of the netCDF object. Specify compression with zlib=True (default = False). Specify the chunksize with a sequence (tuple, list) of the same length as dims (i.e., the number of dimensions) where each element of chunksize corresponds to the size of the chunk along the corresponding dimension. There are some tips and tricks associated with chunking - see http://data.auscover.org.au/node/73 for an overview. The default behaviour is to create a floating-point (f4) variable with dimensions ('time','latitude','longitude'), with no chunking and no compression. """ netcdf_builder.set_variable(self.netcdf_object, varname, dtype=dtype, dims=dims, chunksize=chunksize, fill=fill, zlib=zlib, **kwargs)
def asciigrid_to_nc(arcfilename,fileroot): """ The main routine that calls the calls other routines to prepare the data and metadata and create the netCDF file. """ # Read ascii grid file asciihead,asciidata,asciitail = split_asciigrid(arcfilename) meta = arcasciihdr_to_dict(asciihead) # Create numpy components latvec,lonvec,datadict = set_latlon(meta) data,datadict = asciigrid_to_numpy(asciidata,meta,datadict) # Resample or edit array # Add a default no_data value if required. miss = -999.0 if 'nodata_value' in meta: miss = float(meta['nodata_value']) datadict['missing'] = miss #data,latvec,lonvec,datadict = resample_array(data,latvec,lonvec,datadict) # Prepare time, variable name and metadata d1,d2,datadict = set_datetime(arcfilename,datadict) datadict = set_varname(arcfilename,datadict) attributes = set_attributes(arcfilename,meta,datadict) # Netcdf options # http://netcdf4-python.googlecode.com/svn/trunk/docs/netCDF4-module.html nc_format = 'NETCDF4_CLASSIC' nc_compress = True debug = False # Write netcdf file if os.path.exists(fileroot+'.nc'): os.remove(fileroot+'.nc') varname = datadict['varname'] vartype = datadict['datatype'] fillval = datadict['missing'] timevec = [d1] ncobj = nb.ncopen(fileroot+'.nc','w',format=nc_format) nb.set_timelatlon(ncobj,None,len(latvec),len(lonvec)) # unlimited time nb.set_variable(ncobj,varname,dtype=vartype,fill=fillval,zlib=nc_compress) nb.set_variable(ncobj,'crs',dims=(),dtype="i4") # Grid mapping container nb.add_time(ncobj,timevec) nb.add_data(ncobj,'latitude',latvec) nb.add_data(ncobj,'longitude',lonvec) if debug: print varname,data.shape nb.show_dimensions(ncobj) # nb.add_data should work but is presently broken. Use direct method #nb.add_data(ncobj,varname,data) #ncobj.variables[varname][0,:,:] = data # 2D numpy array ncobj.variables[varname][:] = data # 3D numpy array nb.set_attributes(ncobj,attributes) nb.ncclose(ncobj) print 'Wrote:',fileroot+'.nc' # Write metadata to json if os.path.exists(fileroot+'.json'): os.remove(fileroot+'.json') jh.json_dump(attributes,fileroot+'.json')
def asciigrid_to_nc(arcfilename, fileroot): """ The main routine that calls the calls other routines to prepare the data and metadata and create the netCDF file. """ # Read ascii grid file asciihead, asciidata, asciitail = split_asciigrid(arcfilename) meta = arcasciihdr_to_dict(asciihead) # Create numpy components latvec, lonvec, datadict = set_latlon(meta) data, datadict = asciigrid_to_numpy(asciidata, meta, datadict) # Resample or edit array # Add a default no_data value if required. miss = -999.0 if 'nodata_value' in meta: miss = float(meta['nodata_value']) datadict['missing'] = miss #data,latvec,lonvec,datadict = resample_array(data,latvec,lonvec,datadict) # Prepare time, variable name and metadata d1, d2, datadict = set_datetime(arcfilename, datadict) datadict = set_varname(arcfilename, datadict) attributes = set_attributes(arcfilename, meta, datadict) # Netcdf options # http://netcdf4-python.googlecode.com/svn/trunk/docs/netCDF4-module.html nc_format = 'NETCDF4_CLASSIC' nc_compress = True debug = False # Write netcdf file if os.path.exists(fileroot + '.nc'): os.remove(fileroot + '.nc') varname = datadict['varname'] vartype = datadict['datatype'] fillval = datadict['missing'] timevec = [d1] ncobj = nb.ncopen(fileroot + '.nc', 'w', format=nc_format) nb.set_timelatlon(ncobj, None, len(latvec), len(lonvec)) # unlimited time nb.set_variable(ncobj, varname, dtype=vartype, fill=fillval, zlib=nc_compress) nb.set_variable(ncobj, 'crs', dims=(), dtype="i4") # Grid mapping container nb.add_time(ncobj, timevec) nb.add_data(ncobj, 'latitude', latvec) nb.add_data(ncobj, 'longitude', lonvec) if debug: print varname, data.shape nb.show_dimensions(ncobj) # nb.add_data should work but is presently broken. Use direct method #nb.add_data(ncobj,varname,data) #ncobj.variables[varname][0,:,:] = data # 2D numpy array ncobj.variables[varname][:] = data # 3D numpy array nb.set_attributes(ncobj, attributes) nb.ncclose(ncobj) print 'Wrote:', fileroot + '.nc' # Write metadata to json if os.path.exists(fileroot + '.json'): os.remove(fileroot + '.json') jh.json_dump(attributes, fileroot + '.json')