for var,atts in varatts.iteritems(): newatts = dict() # new atts dictionary # and iterate over atts dict contents for key1,val1 in atts.iteritems(): if isinstance(val1,dict): # flatten nested dicts for key2,val2 in val1.iteritems(): newatts[key2] = val2 else: newatts[key1] = val1 ncatts[var] = newatts # create time dimensions and coordinate variables add_coord(outdata, 'time', data=np.arange(1,len(days_per_month)+1), dtype='i4', atts=ncatts['time']) add_var(outdata, 'length_of_month', ['time'], data=days_per_month, atts=dict(name='length_of_month',units='days',long_name='Length of Month')) # names of months (as char array) add_strvar(outdata, 'name_of_month', name_of_month, 'time', atts=dict(name='name_of_month', units='', long_name='Name of the Month')) # create new lat/lon dimensions and coordinate variables add_coord(outdata, 'lat', data=lat, atts=ncatts['lon']) add_coord(outdata, 'lon', data=lon, atts=ncatts['lat']) # create climatology variables fillValue = -9999; axes = ('time','lat','lon') for name,field in data.iteritems(): add_var(outdata, name, axes, data=field.filled(fillValue), atts=ncatts[name], fillValue=fillValue) # create land/sea/no-data mask mask = ma.getmaskarray(data['precip'])[0,:,:] atts = dict(name='datamask', units='', long_name='Mask for Climatology Fields', description='where this mask is non-zero, no data is available') add_var(outdata, 'datamask', ['lat','lon'], data=mask, atts=atts) # dataset feedback and diagnostics
# 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 periodstr = '%4i-%4i'%period print('\n')
'time', data=np.arange(1, len(days_per_month) + 1), dtype='i4', atts=ncatts['time']) add_var(outdata, 'length_of_month', ['time'], data=days_per_month, atts=dict(name='length_of_month', units='days', long_name='Length of Month')) # names of months (as char array) add_strvar(outdata, 'name_of_month', name_of_month, 'time', atts=dict(name='name_of_month', units='', long_name='Name of the Month')) # create new lat/lon dimensions and coordinate variables add_coord(outdata, 'lat', data=lat, atts=ncatts['lon']) add_coord(outdata, 'lon', data=lon, atts=ncatts['lat']) # create climatology variables fillValue = -9999 axes = ('time', 'lat', 'lon') for name, field in data.iteritems(): add_var(outdata, name, axes, data=field.filled(fillValue),