usage(msg) thedem = "mydem.map" xymetres = False lat = 52 lon = 10 loglevel = logging.INFO outputdir = "output_rad" startday = 1 endday = 2 calc_interval = 60 shour = 1 ehour = 23 oformat = 'PCRaster' postfix = False lowresdem = e2o_dstools.get_data('DEM-WRR1.tif') linkemapstacks = os.path.join(e2o_dstools.get_data(''), 'linke') trans = 0.6 linkemapstack = None deminterpolmethod = 'nearest' for o, a in opts: if o == '-h': usage() if o == '-O': outputdir = a if o == '-D': thedem = a if o == '-d': if a == 'WRR1': lowresdem = e2o_dstools.get_data('DEM-WRR1.tif') elif a == 'WRR2': lowresdem = e2o_dstools.get_data('DEM-WRR2.tif') else:
def downscale(variable, data, datalow, wrrversion,serverroot,wrrsetroot,BB,currentdate,XH,YH,XL,YL, interpolmethod, ncoutfillval, hiresdem, resLowResDEM, interpolmode, logger,localdatadir=''): """ :param variable: :param data: :param wrrversion: :return: """ if variable == 'Temperature': if wrrversion == 2: standard_name = 'air_temperature_lapse_rate' tlist, timelist = get_times_daily(currentdate, currentdate, serverroot, wrrsetroot, "lapseM_EI_025_", logger) ncstepobj = getstepdaily(tlist, BB, standard_name, logger) mmstack = ncstepobj.getdates(timelist) lapse_rate = flipud(mmstack.mean(axis=0)) lapse_rate = resample_grid(lapse_rate, ncstepobj.lon, ncstepobj.lat, XH, YH, method=interpolmethod, FillVal=ncoutfillval) else: lapse_rate = -0.006 retdata = data + lapse_rate * (hiresdem - resLowResDEM) elif variable == "Rainfall" or 'SnowfallRate' or "TotalPrecipitation": if wrrversion == 2: # First read P data in WRR2 resolution yday = (currentdate.date() - datetime.date(currentdate.year, 1, 1)).days + 1 PclimMapName = getmapnamemonth(yday, "prec") PclimWRR2 = e2o_dstools.get_data(os.path.join('Prec/0.2500/', PclimMapName)) if not os.path.exists(PclimWRR2): logger.error('Cannot find WorlClim data for downscaling: ' + PclimWRR2) logger.error('Please download and process the worldclim data at resolution: ' + PclimWRR2) exit(1) resX, resY, cols, rows, CLIMLON, CLIMLAT, PlowClim, FillVal = \ readMap(PclimWRR2, 'GTiff', logger) PlowClim = PlowClim.astype(float32) PlowClim[PlowClim == FillVal] = NaN # Now read the P climate data for the current resolution Resstr = "%1.4f" % diff(XH).mean() # find current resolution PclimCUR = e2o_dstools.get_data(os.path.join('Prec',Resstr,PclimMapName)) if not os.path.exists(PclimCUR): userdir = os.path.join(localdatadir,'Prec_Clim_For_Downscale',Resstr) userfile = os.path.join(userdir,PclimMapName) if not os.path.exists(userfile): logger.info('Cannot find WorlClim data for downscaling: ' + PclimCUR) logger.info('Resampling 0.0083 to : ' + Resstr) PclimMapName = getmapnamemonth(yday, "prec") PclimBaseRes = e2o_dstools.get_data(os.path.join('Prec/0.0083/', PclimMapName)) resXX, resYX, cols_, rows, baseCLIMLON, baseCLIMLAT, baseClim, FillVal = \ readMap(PclimBaseRes, 'GTiff', logger) PHiClim = resample_grid(baseClim,baseCLIMLON,baseCLIMLAT,XH,YH,method='nearest', FillVal=NaN) HILON = XH HILAT = YH #exit(1) if not os.path.exists(userdir): logger.info('Making directory: ' + userdir) os.makedirs(userdir) writeMap(userfile,"GTiff",XH,YH,PHiClim.astype(float32),1E31) else: resX, resY, cols, rows, HILON, HILAT, PHiClim, FillVal = \ readMap(userfile, 'GTiff', logger) PHiClim = PHiClim.astype(float32) PHiClim[PHiClim == FillVal] = NaN else: resX, resY, cols, rows, HILON, HILAT, PHiClim, FillVal = \ readMap(PclimCUR, 'GTiff', logger) PHiClim = PHiClim.astype(float32) PHiClim[PHiClim==FillVal] = NaN # Now determine diff between current data at original resolution and the climatology at the same resolutions # resample climatology first as it may not cover the whole earth if YL[0] < YL[-1]: yas = YL[::-1] else: yas = YL _PlowClim= resample_grid(PlowClim, CLIMLON, CLIMLAT,XL, yas, method='nearest', FillVal=NaN) datalow[datalow<=0] = NaN multlow = _PlowClim/datalow multhi = resample_grid(multlow, XL, yas, XH, YH, method=interpolmode, FillVal=NaN) _PHiClim = resample_grid(PHiClim, HILON, HILAT,XH, YH, method='nearest', FillVal=NaN) multhi[multhi <= 0] = NaN retdata = _PHiClim/multhi # fill with interpolated original data retdata[~isfinite(retdata)] = data[~isfinite(retdata)] else: # First read P data in WRR1 resolution yday = (currentdate.date() - datetime.date(currentdate.year, 1, 1)).days + 1 PclimMapName = getmapnamemonth(yday, "prec") PclimWRR1 = e2o_dstools.get_data(os.path.join('Prec/0.5000/', PclimMapName)) if not os.path.exists(PclimWRR1): logger.error('Cannot find WorlClim data for downscaling: ' + PclimWRR1) logger.error('Please download and process the worldclim data at resolution: ' + PclimWRR1) exit(1) resX, resY, cols, rows, CLIMLON, CLIMLAT, PlowClim, FillVal = \ readMap(PclimWRR1, 'GTiff', logger) PlowClim = PlowClim.astype(float32) PlowClim[PlowClim == FillVal] = NaN # Now read the P climate data for the current resolution Resstr = "%1.4f" % diff(XH).mean() # find current resolution PclimCUR = e2o_dstools.get_data(os.path.join('Prec',Resstr,PclimMapName)) if not os.path.exists(PclimCUR): userdir = os.path.join(localdatadir,'Prec_Clim_For_Downscale',Resstr) userfile = os.path.join(userdir,PclimMapName) if not os.path.exists(userfile): logger.info('Cannot find WorlClim data for downscaling: ' + PclimCUR) logger.info('Resampling 0.0083 to : ' + Resstr) PclimMapName = getmapnamemonth(yday, "prec") PclimBaseRes = e2o_dstools.get_data(os.path.join('Prec/0.0083/', PclimMapName)) resXX, resYX, cols_, rows, baseCLIMLON, baseCLIMLAT, baseClim, FillVal = \ readMap(PclimBaseRes, 'GTiff', logger) PHiClim = resample_grid(baseClim,baseCLIMLON,baseCLIMLAT,XH,YH,method='nearest', FillVal=NaN) HILON = XH HILAT = YH #exit(1) if not os.path.exists(userdir): logger.info('Making directory: ' + userdir) os.makedirs(userdir) writeMap(userfile,"GTiff",XH,YH,PHiClim.astype(float32),1E31) else: resX, resY, cols, rows, HILON, HILAT, PHiClim, FillVal = \ readMap(userfile, 'GTiff', logger) PHiClim = PHiClim.astype(float32) PHiClim[PHiClim == FillVal] = NaN else: resX, resY, cols, rows, HILON, HILAT, PHiClim, FillVal = \ readMap(PclimCUR, 'GTiff', logger) PHiClim = PHiClim.astype(float32) PHiClim[PHiClim==FillVal] = NaN # Now determine diff between current data at original resolution and the climatology at the same resolutions # resample climatology first as it may not cover the whole earth if YL[0] < YL[-1]: yas = YL[::-1] else: yas = YL _PlowClim= resample_grid(PlowClim, CLIMLON, CLIMLAT,XL, yas, method='nearest', FillVal=NaN) datalow[datalow<=0] = NaN multlow = _PlowClim/datalow multhi = resample_grid(multlow, XL, yas, XH, YH, method=interpolmode, FillVal=NaN) _PHiClim = resample_grid(PHiClim, HILON, HILAT,XH, YH, method='nearest', FillVal=NaN) multhi[multhi <= 0] = NaN retdata = _PHiClim/multhi # fill with interpolated original data retdata[~isfinite(retdata)] = data[~isfinite(retdata)] else: retdata = data return retdata
netcdfout = configget(logger, theconf, "output", "netcdfout", "None") rwbuffer = int(configget(logger, theconf, "output", "rwbuffer", "10")) ncoutfillval = float(configget(logger, theconf, "output", "ncoutfillval", "-9999.9")) least_significant_digit = int(configget(logger, theconf, "output", "least_significant_digit", "6")) NetCDFFormat = configget(logger, theconf, "output", "netCDFFormat", "NETCDF4") interpolmethod = configget(logger, theconf, "downscaling", "interpolmethod", interpolmethod) downscaling = configget(logger, theconf, "downscaling", "downscaling", downscaling) wrrversion = int(configget(logger, theconf, "selection", "wrrversion", '2')) timestepsecs = float(configget(logger, theconf, "selection", "timestepsecs", '86400')) timeflatten =configget(logger, theconf, "conversion", "intime", 'mean') FNhighResDEM = configget(logger, theconf, "downscaling", "highResDEM", "downscaledem.map") logger.debug("Done reading settings.") if wrrversion ==2: FNlowResDEM = e2o_dstools.get_data('DEM-WRR2.tif') else: FNlowResDEM = e2o_dstools.get_data('DEM-WRR1.tif') variable = configget(logger, theconf, "selection", "variable", 'None') filename = configget(logger, theconf, "selection", "filename", 'None') standard_name = configget(logger, theconf, "selection", "standard_name", 'None') FNlowResDEM = configget(logger, theconf, "downscaling", "lowResDEM", FNlowResDEM) if downscaling =="True" or resampling == "True": # Get the extent from the high res DEM
def downscale(variable, data, datalow, wrrversion,serverroot,wrrsetroot,BB,currentdate,XH,YH,XL,YL, interpolmethod, ncoutfillval, hiresdem, resLowResDEM, interpolmode, logger,localdatadir=''): """ :param variable: :param data: :param wrrversion: :return: """ if variable == 'Temperature': if wrrversion == 2: standard_name = 'air_temperature_lapse_rate' tlist, timelist = get_times_daily(currentdate, currentdate, serverroot, wrrsetroot, "lapseM_EI_025_", logger) ncstepobj = getstepdaily(tlist, BB, standard_name, logger) mmstack = ncstepobj.getdates(timelist) lapse_rate = flipud(mmstack.mean(axis=0)) lapse_rate = resample_grid(lapse_rate, ncstepobj.lon, ncstepobj.lat, XH, YH, method=interpolmethod, FillVal=ncoutfillval) else: lapse_rate = -0.006 retdata = data + lapse_rate * (hiresdem - resLowResDEM) elif variable == "Rainfall": if wrrversion == 2: # First read P data in WRR2 resolution yday = (currentdate.date() - datetime.date(currentdate.year, 1, 1)).days + 1 PclimMapName = getmapnamemonth(yday, "prec") PclimWRR2 = e2o_dstools.get_data(os.path.join('Prec/0.2500/', PclimMapName)) if not os.path.exists(PclimWRR2): logger.error('Cannot find WorlClim data for downscaling: ' + PclimWRR2) logger.error('Please download and process the worldclim data at resolution: ' + PclimWRR2) exit(1) resX, resY, cols, rows, CLIMLON, CLIMLAT, PlowClim, FillVal = \ readMap(PclimWRR2, 'GTiff', logger) PlowClim = PlowClim.astype(float32) PlowClim[PlowClim == FillVal] = NaN # Now read the P climate data for the current resolution Resstr = "%1.4f" % diff(XH).mean() # find current resolution PclimCUR = e2o_dstools.get_data(os.path.join('Prec',Resstr,PclimMapName)) if not os.path.exists(PclimCUR): userdir = os.path.join(localdatadir,'Prec_Clim_For_Downscale',Resstr) userfile = os.path.join(userdir,PclimMapName) if not os.path.exists(userfile): logger.info('Cannot find WorlClim data for downscaling: ' + PclimCUR) logger.info('Resampling 0.0083 to : ' + Resstr) PclimMapName = getmapnamemonth(yday, "prec") PclimBaseRes = e2o_dstools.get_data(os.path.join('Prec/0.0083/', PclimMapName)) resXX, resYX, cols_, rows, baseCLIMLON, baseCLIMLAT, baseClim, FillVal = \ readMap(PclimBaseRes, 'GTiff', logger) PHiClim = resample_grid(baseClim,baseCLIMLON,baseCLIMLAT,XH,YH,method='nearest', FillVal=NaN) HILON = XH HILAT = YH #exit(1) if not os.path.exists(userdir): logger.info('Making directory: ' + userdir) os.makedirs(userdir) writeMap(userfile,"GTiff",XH,YH,PHiClim.astype(float32),1E31) else: resX, resY, cols, rows, HILON, HILAT, PHiClim, FillVal = \ readMap(userfile, 'GTiff', logger) PHiClim = PHiClim.astype(float32) PHiClim[PHiClim == FillVal] = NaN else: resX, resY, cols, rows, HILON, HILAT, PHiClim, FillVal = \ readMap(PclimCUR, 'GTiff', logger) PHiClim = PHiClim.astype(float32) PHiClim[PHiClim==FillVal] = NaN # Now determine diff between current data at original resolution and the climatology at the same resolutions # resample climatology first as it may not cover the whole earth _PlowClim= resample_grid(PlowClim, CLIMLON, CLIMLAT,XL, YL, method='nearest', FillVal=NaN) datalow[datalow<=0] = NaN multlow = _PlowClim/datalow multhi = resample_grid(multlow, XL, YL, XH, YH, method=interpolmode, FillVal=NaN) _PHiClim = resample_grid(PHiClim, HILON, HILAT,XH, YH, method='nearest', FillVal=NaN) multhi[multhi <= 0] = NaN retdata = _PHiClim/multhi # fill with interpolated original data retdata[~isfinite(retdata)] = data[~isfinite(retdata)] else: retdata = data else: retdata = data return retdata
resampling = configget(logger,theconf,"selection","resampling",resampling) FNhighResDEM = configget(logger,theconf,"downscaling","highResDEM","downscaledem.map") netcdfout = configget(logger, theconf, "output", "netcdfout", "None") rwbuffer = int(configget(logger, theconf, "output", "rwbuffer", "10")) ncoutfillval = float(configget(logger, theconf, "output", "ncoutfillval", "-9999.9")) least_significant_digit = int(configget(logger, theconf, "output", "least_significant_digit", "6")) interpolmethod = configget(logger, theconf, "downscaling", "interpolmethod", interpolmethod) downscaling = configget(logger, theconf, "downscaling", "downscaling", downscaling) wrrversion = int(configget(logger, theconf, "selection", "wrrversion", '2')) timestepsecs = float(configget(logger, theconf, "selection", "timestepsecs", '86400')) timeflatten =configget(logger, theconf, "conversion", "intime", 'mean') logger.debug("Done reading settings.") if wrrversion ==2: FNlowResDEM = e2o_dstools.get_data('DEM-WRR2.tif') else: FNlowResDEM = e2o_dstools.get_data('DEM-WRR1.tif') variable = configget(logger, theconf, "selection", "variable", 'None') filename = configget(logger, theconf, "selection", "filename", 'None') standard_name = configget(logger, theconf, "selection", "standard_name", 'None') FNlowResDEM = configget(logger, theconf, "downscaling", "lowResDEM", FNlowResDEM) if downscaling =="True" or resampling == "True": resX, resY, cols, rows, xhires, yhires, hiresdem, FillVal = readMap(FNhighResDEM,'PCRaster',logger)
thedem = "mydem.map" xymetres = False lat = 52 lon = 10 loglevel = logging.INFO outputdir="output_rad" startday = 1 endday = 2 calc_interval = 60 shour=1 ehour=23 oformat ='PCRaster' postfix =False lowresdem=e2o_dstools.get_data('DEM-WRR1.tif') linkemapstacks = os.path.join(e2o_dstools.get_data(''),'linke') trans = 0.6 linkemapstack = None deminterpolmethod = 'nearest' for o, a in opts: if o == '-h': usage() if o == '-O': outputdir = a if o == '-D': thedem = a if o == '-d': if a == 'WRR1': lowresdem= e2o_dstools.get_data('DEM-WRR1.tif') elif a == 'WRR2': lowresdem = e2o_dstools.get_data('DEM-WRR2.tif')