class BiasCorrecter(object): def __init__(self, dt, mp, cr): self.transformer = TransformerWrapper(cr) self.retrender = RetrenderWrapper(dt, mp) self.detrender = DetrenderWrapper(dt, mp) self.unrealizable = mp == 'false' and cr == 'mean-scale' def correct(self, sim, obs, tsim, tobs): """ Corrects simulated sequence sim using observations obs, with tsim and tobs arrays of years """ tmin = max([tsim[0], tobs[0]]) tmax = min([tsim[-1], tobs[-1]]) odT, T = self.detrender.detrend(obs) # detrend observations sdT, _ = self.detrender.detrend(sim) # detrend simulations dT = masked_array(zeros(len(tsim)), mask=ones(len(tsim))) rT = dT.copy() if not self.unrealizable and not sdT.mask.all() and not odT.mask.all(): odT_c = self.__toverlap(odT, tobs, tmin, tmax) # transform distribution sdT_c = self.__toverlap(sdT, tsim, tmin, tmax) dT = self.transformer.transform(sdT, sdT_c, odT_c) obs_c = self.__toverlap(obs, tobs, tmin, tmax) # combine T_c = self.__toverlap(T, tobs, tmin, tmax) dT_c = self.__toverlap(dT, tsim, tmin, tmax) rT_c = self.retrender.retrend(T_c, dT_c, obs_c) rT[logical_and(tsim >= tmin, tsim <= tmax)] = rT_c return dT, rT def __toverlap(self, x, tx, tmin, tmax): return x[logical_and(tx >= tmin, tx <= tmax)]
class BiasCorrecter(object): def __init__(self, dt, mp, cr): self.transformer = TransformerWrapper(cr) self.retrender = RetrenderWrapper(dt, mp) self.detrender = DetrenderWrapper(dt, mp) self.unrealizable = mp == 'false' and cr == 'mean-scale' def correct(self, sim, obs, tsim, tobs): """ Corrects simulated sequence sim using observations obs, with tsim and tobs arrays of years """ tmin = max([tsim[0], tobs[0]]) tmax = min([tsim[-1], tobs[-1]]) odT, T = self.detrender.detrend(obs) # detrend observations sdT, _ = self.detrender.detrend(sim) # detrend simulations dT = masked_array(zeros(len(tsim)), mask = ones(len(tsim))) rT = dT.copy() if not self.unrealizable and not sdT.mask.all() and not odT.mask.all(): odT_c = self.__toverlap(odT, tobs, tmin, tmax) # transform distribution sdT_c = self.__toverlap(sdT, tsim, tmin, tmax) dT = self.transformer.transform(sdT, sdT_c, odT_c) obs_c = self.__toverlap(obs, tobs, tmin, tmax) # combine T_c = self.__toverlap(T, tobs, tmin, tmax) dT_c = self.__toverlap(dT, tsim, tmin, tmax) rT_c = self.retrender.retrend(T_c, dT_c, obs_c) rT[logical_and(tsim >= tmin, tsim <= tmax)] = rT_c return dT, rT def __toverlap(self, x, tx, tmin, tmax): return x[logical_and(tx >= tmin, tx <= tmax)]
'mp', zlib=True, shuffle=False, complevel=9) mpvar[:] = range(1, nmp + 1) mpvar.units = 'mapping' mpvar.long_name = ', '.join(mp) for v in variables: var = fi.variables[v] vardt = masked_array(zeros(sh), mask=ones(sh)) for latidx, lonidx in product(range(nlats), range(nlons)): varmat = var[:, latidx, lonidx] if not isMaskedArray(varmat) or not varmat.mask[0]: for d, c in product(range(ndt), range(nmp)): detrender = DetrenderWrapper(dt[d], mp[c]) vardt[:, latidx, lonidx, d, c] = detrender.detrend(varmat)[0] vvar = fo.createVariable(v, 'f4', ('time', latname, lonname, 'dt', 'mp'), zlib=True, shuffle=False, complevel=9, fill_value=1e20) vvar[:] = vardt vvar.units = var.units if 'units' in var.ncattrs() else '' vvar.long_name = var.long_name if 'long_name' in var.ncattrs() else '' fi.close() fo.close()
mpvar = fo.createVariable('mp', 'i4', 'mp', zlib=True, shuffle=False, complevel=9) mpvar[:] = range(1, nmp + 1) mpvar.units = 'mapping' mpvar.long_name = ', '.join(mp) for cr in crops: yld = fi.variables['yield_' + cr][:] ylddt = masked_array(zeros(sh), mask=ones(sh)) for d, c in product(range(ndt), range(nmp)): detrender = DetrenderWrapper(dt[d], mp[c]) for i in range(naggs): ylddt[i, :, d, c] = detrender.detrend(yld[i, :])[0] yvar = fo.createVariable('yield_' + cr, 'f4', (agglvl, 'time', 'dt', 'mp'), zlib=True, shuffle=False, complevel=9, fill_value=1e20) yvar[:] = ylddt yvar.units = fi.variables['yield_' + cr].units yvar.long_name = fi.variables['yield_' + cr].long_name fi.close() fo.close()
dtvar[:] = range(1, ndt + 1) dtvar.units = "mapping" dtvar.long_name = ", ".join(dt) fo.createDimension("mp", nmp) mpvar = fo.createVariable("mp", "i4", "mp", zlib=True, shuffle=False, complevel=9) mpvar[:] = range(1, nmp + 1) mpvar.units = "mapping" mpvar.long_name = ", ".join(mp) for v in variables: var = fi.variables[v] vardt = masked_array(zeros(sh), mask=ones(sh)) for latidx, lonidx in product(range(nlats), range(nlons)): varmat = var[:, latidx, lonidx] if not isMaskedArray(varmat) or not varmat.mask[0]: for d, c in product(range(ndt), range(nmp)): detrender = DetrenderWrapper(dt[d], mp[c]) vardt[:, latidx, lonidx, d, c] = detrender.detrend(varmat)[0] vvar = fo.createVariable( v, "f4", ("time", latname, lonname, "dt", "mp"), zlib=True, shuffle=False, complevel=9, fill_value=1e20 ) vvar[:] = vardt vvar.units = var.units if "units" in var.ncattrs() else "" vvar.long_name = var.long_name if "long_name" in var.ncattrs() else "" fi.close() fo.close()
timevar.units = fi.variables['time'].units timevar.long_name = 'time' fo.createDimension('dt', ndt) dtvar = fo.createVariable('dt', 'i4', 'dt', zlib = True, shuffle = False, complevel = 9) dtvar[:] = range(1, ndt + 1) dtvar.units = 'mapping' dtvar.long_name = ', '.join(dt) fo.createDimension('mp', nmp) mpvar = fo.createVariable('mp', 'i4', 'mp', zlib = True, shuffle = False, complevel = 9) mpvar[:] = range(1, nmp + 1) mpvar.units = 'mapping' mpvar.long_name = ', '.join(mp) for cr in crops: yld = fi.variables['yield_' + cr][:] ylddt = masked_array(zeros(sh), mask = ones(sh)) for d, c in product(range(ndt), range(nmp)): detrender = DetrenderWrapper(dt[d], mp[c]) for i in range(naggs): ylddt[i, :, d, c] = detrender.detrend(yld[i, :])[0] yvar = fo.createVariable('yield_' + cr, 'f4', (agglvl, 'time', 'dt', 'mp'), zlib = True, shuffle = False, complevel = 9, fill_value = 1e20) yvar[:] = ylddt yvar.units = fi.variables['yield_' + cr].units yvar.long_name = fi.variables['yield_' + cr].long_name fi.close() fo.close()