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')
Example #2
0
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')
Example #3
0
 def show_dimensions(self):
     """
     Print the dimension names, lengths and whether they are unlimited.
     """
     netcdf_builder.show_dimensions(self.netcdf_object)
Example #4
0
 def show_dimensions(self):
     """
     Print the dimension names, lengths and whether they are unlimited.
     """
     netcdf_builder.show_dimensions(self.netcdf_object)