variables.sort() scens = list(set(scens)) scens.sort() scens_full = list(set(scens_full)) scens_full.sort() # number of variables, times, scenarios, aggregation levels nv, nt, ns, na = len(variables), len(time), len(scens), len(audata) # preallocate final averages and areas averages = masked_array(zeros((nv, na, nt, ns, 3)), mask=ones((nv, na, nt, ns, 3))) areas = masked_array(zeros((na, nt, ns, 3)), mask=ones((na, nt, ns, 3))) # aggregator object avobj = MeanAverager() vunits = [''] * nv for i in range(len(scens_full)): scen_irr = scens_full[i] # find scenario and irr scen_irr_split = scen_irr.split('_') if len(scen_irr_split) == 2: sidx = scens.index(scen_irr_split[0]) else: sidx = scens.index('_'.join(scen_irr_split[0::2])) iidx = int(scen_irr_split[1] != 'firr') # load planting file plantingfile = findfile(files, scen_irr, 'plant-day')
variables = list(set(variables)) variables.sort() # number of variables, times, scenarios, aggregation levels nvariables = len(variables) ntimes = len(time) naudata = len(audata) nirrs = len(irr_levels) + 1 # preallocate final averages and areas averages = masked_array(zeros((nvariables, naudata, ntimes, nirrs)), mask=ones((nvariables, naudata, ntimes, nirrs))) areas = masked_array(zeros((naudata, ntimes, 3)), mask=ones((naudata, ntimes, nirrs))) # aggregator object avobj = MeanAverager() vunits = [''] * nvariables for iidx in range(len(irr_levels)): # Load planting file plantingfile = findfile(files, 'plant-day') if plantingfile: with Dataset(plantingfile) as f: planting_date = f.variables['plant-day_' + crop][0] else: planting_date = pdate[iidx] # load harvest file harvestfile = findfile(files, 'maty-day') if harvestfile:
rfweights = f.variables['rainfed'][:] irweights = f.variables['irrigated'][:] else: rfweights = irweights = None aggloader = AggMaskLoader(aggf, lats = lats, lons = lons) adata = aggloader.data() audata = aggloader.udata() anames = aggloader.names() aunits = aggloader.units() alongnames = aggloader.longnames() createAggFile(outputf, time, tunits, audata, anames, aunits, alongnames, scensel, irr, leaddim, hasscen) f = nc(outputf, 'a') avobj = MeanAverager() for i in range(len(audata)): if leaddim == 'scen': dimsv = ('scen', 'time', anames[i], 'irr') elif hasscen: dimsv = ('time', 'scen', anames[i], 'irr') else: dimsv = ('time', anames[i], 'irr') for j in range(nvars): avev = f.createVariable(ivars[j] + '_' + anames[i], 'f4', dimsv, fill_value = 1e20, zlib = True, complevel = 9) avev.units = vunits[j] avev.long_name = 'average ' + anames[i] + ' ' + ivars[j] slicer = [slice(0, n) for n in var[j].shape] dimslist = list(dims[j])
else: continue # irregular files variables = list(set(variables)); variables.sort() scens = list(set(scens)); scens.sort() scens_full = list(set(scens_full)); scens_full.sort() # number of variables, times, scenarios, aggregation levels nv, nt, ns, na = len(variables), len(time), len(scens), len(audata) # preallocate final averages and areas averages = masked_array(zeros((nv, na, nt, ns, 3)), mask = ones((nv, na, nt, ns, 3))) areas = masked_array(zeros((na, nt, ns, 3)), mask = ones((na, nt, ns, 3))) # aggregator object avobj = MeanAverager() vunits = [''] * nv for i in range(len(scens_full)): scen_irr = scens_full[i] # find scenario and irr scen_irr_split = scen_irr.split('_') if len(scen_irr_split) == 2: sidx = scens.index(scen_irr_split[0]) else: sidx = scens.index('_'.join(scen_irr_split[0 :: 2])) iidx = int(scen_irr_split[1] != 'firr') # load planting file plantingfile = findfile(files, scen_irr, 'plant-day')
audata = aggloader.udata() anames = aggloader.names() aunits = aggloader.units() alongnames = aggloader.longnames() alats = aggloader.latitudes() alons = aggloader.longitudes() if abs(lats - alats).max() > tol: raise Exception('Latitudes in output file and aggregation mask do not agree!') if abs(lons - alons).max() > tol: raise Exception('Longitudes in output file and aggregation mask do not agree!') createAggFile(outputf, time, tunits, audata, anames, aunits, alongnames, scensel, leaddim) f = nc(outputf, 'a') avobj = MeanAverager() for i in range(len(audata)): if leaddim == 'scen': dimsv = ('scen', 'time', anames[i] + '_index') else: dimsv = ('time', 'scen', anames[i] + '_index') for j in range(nvars): avev = f.createVariable(ivars[j] + '_' + anames[i], 'f4', dimsv, fill_value = 1e20, zlib = True, complevel = 9) avev.units = vunits[j] avev.long_name = 'average ' + anames[i] + ' ' + ivars[j] slicer = [slice(0, n) for n in var[j].shape] dimslist = list(dims[j]) dimslist.remove('scen') timeidx, latidx, lonidx = dimslist.index('time'), dimslist.index('lat'), dimslist.index('lon')
t = f.variables['time'] time = t[:] tunits = t.units if 'units' in t.ncattrs() else '' for i in range(nvars): v = f.variables[ivars[i]] var[i] = v[:] var[i] = masked_where(isnan(var[i]), var[i]) # mask out NaNs var[i].mask = resize(var[i].mask, var[i].shape) # resize mask if necessary vunits[i] = v.units if 'units' in v.ncattrs() else '' createAggFile(options.output, time, tunits, audata, anames, aunits, alongnames) f = nc(options.output, 'a') # open file for appending if options.type == 'mean': # averager instance avobj = MeanAverager() else: avobj = SumAverager() for i in range(nmasks): dims = (anames[i] + '_index', 'time') for j in range(nvars): avev = f.createVariable(ivars[j] + '_' + anames[i], 'f4', dims, fill_value=1e20, zlib=True, complevel=9) avev[:] = avobj.av(var[j], adata[i], lats,
alongnames = aggloader.longnames() alats = aggloader.latitudes() alons = aggloader.longitudes() if abs(lats - alats).max() > tol: raise Exception( 'Latitudes in output file and aggregation mask do not agree!') if abs(lons - alons).max() > tol: raise Exception( 'Longitudes in output file and aggregation mask do not agree!') createAggFile(outputf, time, tunits, audata, anames, aunits, alongnames, scensel, irr, leaddim) f = nc(outputf, 'a') avobj = MeanAverager() for i in range(len(audata)): if leaddim == 'scen': dimsv = ('scen', 'time', anames[i] + '_index', 'irr') else: dimsv = ('time', 'scen', anames[i] + '_index', 'irr') for j in range(nvars): avev = f.createVariable(ivars[j] + '_' + anames[i], 'f4', dimsv, fill_value=1e20, zlib=True, complevel=9) avev.units = vunits[j] avev.long_name = 'average ' + anames[i] + ' ' + ivars[j]
ivars = [v.strip() for v in ivars.split(',')] nvars = len(ivars) var = [0] * nvars; vunits = [0] * nvars with nc(ifile) as f: lats = f.variables['lat'][:] t = f.variables['time'] time = t[:] tunits = t.units if 'units' in t.ncattrs() else '' for i in range(nvars): v = f.variables[ivars[i]] var[i] = v[:] var[i] = masked_where(isnan(var[i]), var[i]) # mask out NaNs var[i].mask = resize(var[i].mask, var[i].shape) # resize mask if necessary vunits[i] = v.units if 'units' in v.ncattrs() else '' createAggFile(options.output, time, tunits, audata, anames, aunits, alongnames) f = nc(options.output, 'a') # open file for appending if options.type == 'mean': # averager instance avobj = MeanAverager() else: avobj = SumAverager() for i in range(nmasks): dims = (anames[i] + '_index', 'time') for j in range(nvars): avev = f.createVariable(ivars[j] + '_' + anames[i], 'f4', dims, fill_value = 1e20, zlib = True, complevel = 9) avev[:] = avobj.av(var[j], adata[i], lats, weights, calcarea = options.calcarea) avev.units = vunits[j] avev.long_name = ' '.join([options.type, anames[i], ivars[j]]) f.close()