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 set_attributes(self, ncdict, delval='DELETE'): """ Copy attribute names and values from a dict (or OrderedDict) to a netCDF object. Global attributes are keyed in the OrderedDict by the attribute name. Variable attributes are keyed in the OrderedDict by the variable name and attribute name separated by a colon, i.e. variable:attribute. If any value is equal to delval then, if the corresponding attribute exists in the netCDF object, the corresponding attribute is removed from the netCDF object. The default value of delval is 'DELETE'. For example, nc3_set_attributes(self.netcdf_object, {'temperature:missing_value':'DELETE'}) will delete the missing_value attribute from the temperature variable. A ValueError exception is raised if a key refers to a variable name that is not defined in the netCDF object. """ netcdf_builder.set_attributes(self.netcdf_object, ncdict, delval)
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')