for j in range(nv): # load variable varfile = findfile(files, scen_irr, variables[j]) if not len(varfile): continue with nc(indir + sep + varfile) as f: var = f.variables[variables[j] + '_' + crop] if not i: vunits[j] = var.units if 'units' in var.ncattrs() else '' var = var[:] var[isnan(var)] = 0. # change NaNs to zero var = shiftdata(var, pd, hd, yearthr) # shift data var = var[yrsinrange] # restrict to overlapping years # aggregate averages[j, :, :, sidx, iidx] = avobj.sum(var, adata, lats, weights[iidx], calcarea, ymsk) averages[j, :, :, sidx, iidx] /= areas[:, :, sidx, iidx] # sum areas area1, area2 = areas[:, :, :, 0], areas[:, :, :, 1] area1[logical_and(area1.mask, ~area2.mask)] = 0. area2[logical_and(area2.mask, ~area1.mask)] = 0. areas[:, :, :, 2] = area1 + area2 # create output file fout = AggregationFile(outfile, time, tunits, scens, audata, aname, aunits, alongname) # add area fout.append('area_' + aname, areas, (aname, 'time', 'scen', 'irr'), 'hectares', aname + ' harvested area')
for j in range(nvariables): varfile = findfile(files, variables[j]) print "Reading %s" % varfile if not varfile: continue with Dataset(varfile) as f: var = f.variables[variables[j] + '_' + crop] if not i: vunits[j] = var.units if 'units' in var.ncattrs() else '' var = var[:] var[isnan(var)] = 0. # change NaNs to zero var = shiftdata(var, planting_date, harvest_date, yearthr) # shift data var = var[yrsinrange] # restrict to overlapping years # aggregate print "sum is %s" % avobj.sum(var, adata, lats, weights[iidx], calc_area, ymsk) averages[j, :, :, iidx] = avobj.sum(var, adata, lats, weights[iidx], calc_area, ymsk) averages[j, :, :, iidx] /= areas[:, :, iidx] # sum areas area1, area2 = areas[:, :, 0], areas[:, :, 1] area1[logical_and(area1.mask, ~area2.mask)] = 0. area2[logical_and(area2.mask, ~area1.mask)] = 0. areas[:, :, 2] = area1 + area2 # create output file for iidx in range(nirrs): print "Writing %s" % outfiles[iidx] fout = AggregationFile(outfiles[iidx], time, tunits, audata, aname, aunits, alongname) # add area
areas[:, :, sidx, iidx] = avobj.areas(yvar, adata, lats, weights[iidx], calcarea) for j in range(nv): # load variable varfile = findfile(files, scen_irr, variables[j]) if not len(varfile): continue with nc(indir + sep + varfile) as f: var = f.variables[variables[j] + '_' + crop] if not i: vunits[j] = var.units if 'units' in var.ncattrs() else '' var = var[:] var[isnan(var)] = 0. # change NaNs to zero var = shiftdata(var, pd, hd, yearthr) # shift data var = var[yrsinrange] # restrict to overlapping years # aggregate averages[j, :, :, sidx, iidx] = avobj.sum(var, adata, lats, weights[iidx], calcarea, ymsk) averages[j, :, :, sidx, iidx] /= areas[:, :, sidx, iidx] # sum areas area1, area2 = areas[:, :, :, 0], areas[:, :, :, 1] area1[logical_and(area1.mask, ~area2.mask)] = 0. area2[logical_and(area2.mask, ~area1.mask)] = 0. areas[:, :, :, 2] = area1 + area2 # create output file fout = AggregationFile(outfile, time, tunits, scens, audata, aname, aunits, alongname) # add area fout.append('area_' + aname, areas, (aname, 'time', 'scen', 'irr'), 'hectares', aname + ' harvested area') # add variables