def BIL2netCDF(BILfile, BILdtype='uint16', outdir=os.getcwd(), theme_name='undefined', theme_unit='undefined', long_name='undefined'): ''' Convenience function converting the given BIL file to netCDF. @param BILfile: Input BIl file. @param outdir: Output directory. @param theme_name: Short name for the theme. @param theme_unit: Metric unit of the theme data. @param long_name: Descriptive name for the theme. @return: netCDF file in the output directory. ''' ncfilename = os.path.splitext(os.path.basename(BILfile))[0] + '.nc' bd = BILdata(BILfile, BILdtype) bd.read() yy, mm, dd = "13", "09", "11" tstring = yy+'-'+mm+'-'+dd+' 06:00:00' secs = date2num(iso2datetime(tstring), timeunit) mask = flipud(senorge_mask()) ncdata = flipud(int16(bd.data)) # array needs to be flipped ud-down and transposed to fit the coordinate system ncdata[mask] = get_FillValue(ncdata.dtype) ncfile = NCdata(os.path.join(outdir, ncfilename)) # ncfile.zip = True ncfile.new(secs) print ncdata.dtype.str ncfile.add_variable(theme_name, ncdata.dtype.str, theme_unit, long_name, ncdata, lsd=1) ncfile.close()
def __init__(self, filename, datatype): ''' Initializes defaults. ''' self.nrows = 1550 self.ncols = 1195 self.datatype = eval(datatype) self.nodata = get_FillValue(self.datatype) self.data = zeros((self.nrows, self.ncols), self.datatype) self.filename = filename
def BIL2netCDF(BILfile, BILdtype='uint16', outdir=os.getcwd(), theme_name='undefined', theme_unit='undefined', long_name='undefined'): ''' Convenience function converting the given BIL file to netCDF. @param BILfile: Input BIl file. @param outdir: Output directory. @param theme_name: Short name for the theme. @param theme_unit: Metric unit of the theme data. @param long_name: Descriptive name for the theme. @return: netCDF file in the output directory. ''' ncfilename = os.path.splitext(os.path.basename(BILfile))[0] + '.nc' bd = BILdata(BILfile, BILdtype) bd.read() yy, mm, dd = "13", "09", "11" tstring = yy + '-' + mm + '-' + dd + ' 06:00:00' secs = date2num(iso2datetime(tstring), timeunit) mask = flipud(senorge_mask()) ncdata = flipud( int16(bd.data) ) # array needs to be flipped ud-down and transposed to fit the coordinate system ncdata[mask] = get_FillValue(ncdata.dtype) ncfile = NCdata(os.path.join(outdir, ncfilename)) # ncfile.zip = True ncfile.new(secs) print ncdata.dtype.str ncfile.add_variable(theme_name, ncdata.dtype.str, theme_unit, long_name, ncdata, lsd=1) ncfile.close()
def add_variable(self, theme_name, theme_dtype, theme_unit, long_name, data, lsd=None): """ Creates a netCDF variable instances and adds data from a numpy.array to it. :Parameters: - theme_name: Abbreviation for the theme. - theme_dtype: Data-type in which the theme values should be stored. - theme_unit: SI unit of the data. - long_name: Decriptive name for the theme. - data: The input data to be stored. - lsd: Least significant digit to be stored. """ var = self.rootgrp.createVariable(theme_name, theme_dtype, ('time', 'y', 'x'), fill_value=get_FillValue(data.dtype), zlib=self.zip, # reduces disk space enormously least_significant_digit=lsd ) # set default attributes var.units = theme_unit var.long_name = long_name var._CoordinateSystems = "UTM_Projection" # Check for correct dimensions if data.shape == (1550, 1195): var[:] = data elif data.shape == (1195, 1550): var[:] = data.T print "Data array transposed before saving!" else: print "Data array does not have seNorge standard dimensions: (y=1550, x=1195)." self.close() print """Added variable "%s" to file %s""" % (theme_name, self.filename)