def exportDataset(self, dataset): ''' method to export a Dataset instance to NetCDF format and write to disk ''' # create NetCDF file filepath = self.filepath writeNetCDF(dataset=dataset, ncfile=filepath, **self.export_arguments) # check first and last if not os.path.exists(filepath): raise IOError, filepath
def writeNetCDF(self, filename=None, folder=None, ncformat='NETCDF4', zlib=True, writeData=True, close=False, flush=False): ''' Write current temporary storage to a NetCDF file. ''' if self.tmp: if not isinstance(filename,basestring): raise TypeError if folder is not None: filename = folder + filename output = writeNetCDF(self.tmpput, filename, ncformat=ncformat, zlib=zlib, writeData=writeData, close=False) if flush: self.tmpput.unload() if self.feedback: print('\nOutput written to {0:s}\n'.format(filename)) else: self.output.sync() output = self.output.dataset # get (primary) NetCDF file if self.feedback: print('\nSynchronized dataset {0:s} with temporary storage.\n'.format(output.name)) # flush? if flush: self.output.unload() # close file or return file handle if close: output.close() else: return output
dataset.atts.resolution = res # load data into memory dataset.load() # add landmask addLandMask(dataset) # create landmask from precip mask dataset.mask(dataset.landmask) # mask all fields using the new landmask # add length and names of month addLengthAndNamesOfMonth(dataset, noleap=False) # figure out a different filename filename = getFileName(grid=res, period=None, name='GPCC', filepattern=avgfile) print('\n'+filename+'\n') if os.path.exists(avgfolder+filename): os.remove(avgfolder+filename) # write data and some annotation ncset = writeNetCDF(dataset, avgfolder+filename, close=False) # add_var(ncset,'name_of_month', name_of_month, 'time', # add names of month # atts=dict(name='name_of_month', units='', long_name='Name of the Month')) # close... ncset.close() dataset.close() # print dataset before print(dataset) print('') elif mode == 'average_timeseries': # load source
dataset.title = 'NARR Long-term Climatology' # load data into memory dataset.load() # # add landmask # addLandMask(dataset) # create landmask from precip mask # dataset.mask(dataset.landmask) # mask all fields using the new landmask # add length and names of month addLengthAndNamesOfMonth(dataset, noleap=False) # figure out a different filename filename = getFileName(grid='NARR', period=None, name='NARR', filepattern=avgfile) print('\n'+filename+'\n') if os.path.exists(avgfolder+filename): os.remove(avgfolder+filename) # write data and some annotation ncset = writeNetCDF(dataset, avgfolder+filename, close=False) add_strvar(ncset,'name_of_month', name_of_month, 'time', # add names of month atts=dict(name='name_of_month', units='', long_name='Name of the Month')) # close... ncset.close() dataset.close() # print dataset before print(dataset) print('') # generate averaged climatology elif mode == 'average_timeseries': # load source