ltime = f.variables['time'][:] ltunits = f.variables['time'].units ltime += int(findall(r'\d+', ltunits)[0]) weights = weights[:, logical_and(ltime >= y1, ltime <= y2)] # restrict range else: ltime = years.copy() # restrict to overlapping years yrsinrange = logical_and(years >= min(ltime), years <= max(ltime)) years = years[yrsinrange] t0 = min(years) - y1 + 1 time = arange(t0, t0 + len(years)) # load aggregation mask afile, avar = [a.strip() for a in agg.split(':')] aggloader = AggMaskLoader(afile, avar) adata = aggloader.data()[0] audata = aggloader.udata()[0] aname = aggloader.names()[0] aunits = aggloader.units()[0] alongname = aggloader.longnames()[0] # load growing season file with Dataset(gsfile) as f: pdate = f.variables['planting_day'][:] hdate = f.variables['growing_season_length'][:] # get variables and scenarios variables = [] for i in range(len(files)):
ltime = f.variables['time'][:] ltunits = f.variables['time'].units ltime += int(findall(r'\d+', ltunits)[0]) weights = weights[:, logical_and(ltime >= y1, ltime <= y2)] # restrict range else: ltime = years.copy() # restrict to overlapping years yrsinrange = logical_and(years >= min(ltime), years <= max(ltime)) years = years[yrsinrange] t0 = min(years) - y1 + 1 time = arange(t0, t0 + len(years)) # load aggregation mask afile, avar = [a.strip() for a in agg.split(':')] aggloader = AggMaskLoader(afile, avar) adata = aggloader.data()[0] audata = aggloader.udata()[0] aname = aggloader.names()[0] aunits = aggloader.units()[0] alongname = aggloader.longnames()[0] # load growing season file with nc(gsfile) as f: pdate = f.variables['planting_day'][:] hdate = f.variables['growing_season_length'][:] # get variables and scenarios variables = []; scens = []; scens_full = [] for i in range(len(files)): fs = files[i].split('_')
if yieldvar: # pull yield yieldv = f.variables[yieldvar] slicer = [slice(0, n) for n in yieldv.shape] if hasscen: slicer[yieldv.dimensions.index('scen')] = scenidx yieldv = yieldv[slicer] iridx, rfidx = irr.index('ir'), irr.index('rf') if weightsf: with nc(weightsf) as f: 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')
weights = None if weightsf: with nc(weightsf) as f: wlats, wlons = f.variables['lat'][:], f.variables['lon'][:] sellat = logical_and(wlats >= llat - tol, wlats <= ulat + tol) sellon = logical_and(wlons >= llon - tol, wlons <= ulon + tol) wlats, wlons = wlats[sellat], wlons[sellon] if abs(lats - wlats).max() > tol: raise Exception('Latitudes in output file and weights mask do not agree!') if abs(lons - wlons).max() > tol: raise Exception('Longitudes in output file and weights mask do not agree!') weights = f.variables['weights'][sellat][:, sellon] aggloader = AggMaskLoader(aggf, lats = lats, lons = lons) adata = aggloader.data() 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')
help="Output file", metavar="FILE") options, args = parser.parse_args() if not options.type in ['mean', 'sum']: raise Exception('Invalid type') weights = None # load weights if not options.weights in ['none', 'None', '']: wfile, wvar = [w.strip() for w in options.weights.split(':')] with nc(wfile) as f: weights = f.variables[wvar][:] afile, avars = [a.strip() for a in options.agg.split(':')] # load aggregation masks avars = [v.strip() for v in avars.split(',')] aggloader = AggMaskLoader(afile, avars) adata = aggloader.data() audata = aggloader.udata() anames = aggloader.names() aunits = aggloader.units() alongnames = aggloader.longnames() nmasks = len(audata) ifile, ivars = [i.strip() for i in options.input.split(':')] # load input data 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']