Example #1
0
def Calc_Humidity(Temp_format,
                  P_format,
                  Hum_format,
                  output_format,
                  Startdate,
                  Enddate,
                  freq="D"):

    folder_dir_out = os.path.dirname(output_format)

    if not os.path.exists(folder_dir_out):
        os.makedirs(folder_dir_out)

    Dates = pd.date_range(Startdate, Enddate, freq="D")

    for Date in Dates:

        print(Date)

        Day = Date.day
        Month = Date.month
        Year = Date.year

        Tempfile_one = Temp_format.format(yyyy=Year, mm=Month, dd=Day)
        Presfile_one = P_format.format(yyyy=Year, mm=Month, dd=Day)
        Humfile_one = Hum_format.format(yyyy=Year, mm=Month, dd=Day)
        out_folder_one = output_format.format(yyyy=Year, mm=Month, dd=Day)

        geo_out, proj, size_X, size_Y = RC.Open_array_info(Tempfile_one)
        Tdata = RC.Open_tiff_array(Tempfile_one)
        if "MERRA_K" in Temp_format:
            Tdata = Tdata - 273.15

        Tdata[Tdata < -900] = -9999
        Pdata = RC.Open_tiff_array(Presfile_one)
        Hdata = RC.Open_tiff_array(Humfile_one)
        Pdata[Pdata < 0] = -9999
        Hdata[Hdata < 0] = -9999

        # gapfilling
        Tdata = RC.gap_filling(Tdata, -9999)
        Pdata = RC.gap_filling(Pdata, -9999)
        Hdata = RC.gap_filling(Hdata, -9999)

        Esdata = 0.6108 * np.exp((17.27 * Tdata) / (Tdata + 237.3))
        HumData = np.minimum((1.6077717 * Hdata * Pdata / Esdata), 1) * 100
        HumData = HumData.clip(0, 100)

        DC.Save_as_tiff(out_folder_one, HumData, geo_out, "WGS84")

    return ()
Example #2
0
def Calc_Humidity(Temp_format,
                  P_format,
                  Hum_format,
                  output_format,
                  Startdate,
                  Enddate,
                  freq="D"):

    folder_dir_out = os.path.dirname(output_format)

    if not os.path.exists(folder_dir_out):
        os.makedirs(folder_dir_out)

    Dates = pd.date_range(Startdate, Enddate, freq="D")

    for Date in Dates:

        print(Date)

        Day = Date.day
        Month = Date.month
        Year = Date.year

        Windyfile_one = wind_y_format.format(yyyy=Year, mm=Month, dd=Day)
        Windxfile_one = wind_x_format.format(yyyy=Year, mm=Month, dd=Day)
        out_folder_one = output_format.format(yyyy=Year, mm=Month, dd=Day)

        geo_out, proj, size_X, size_Y = RC.Open_array_info(Windxfile_one)
        Windxdata = RC.Open_tiff_array(Windxfile_one)
        Windxdata[Windxdata < -900] = -9999

        Windydata = RC.Open_tiff_array(Windyfile_one)
        Windydata[Windxdata < -900] = -9999

        # gapfilling
        Windydata = RC.gap_filling(Windydata, -9999)
        Windxdata = RC.gap_filling(Windxdata, -9999)

        Wind = np.sqrt(Windxdata**2 + Windydata**2)

        DC.Save_as_tiff(out_folder_one, Wind, geo_out, "WGS84")

    return ()
Example #3
0
    def __init__(self,
                 input_folder,
                 input_format,
                 Dates=None,
                 Conversion=1,
                 Example_Data=None,
                 Mask_Data=None,
                 gap_filling=None,
                 reprojection_type=2,
                 Variable='',
                 Product='',
                 Description='',
                 Unit='',
                 Dimension_description=''):

        if Mask_Data != None:
            dest_MASK = RC.reproject_dataset_example(Mask_Data, Example_Data,
                                                     1)
            MASK = dest_MASK.GetRasterBand(1).ReadAsArray()
            MASK = np.where(MASK == 1, 1, np.nan)
        else:
            MASK = 1

        if Dates != None:

            # Get Ordinal time
            time_or = np.array(list(map(lambda i: i.toordinal(), Dates)))
            i = 1

            # Define start and enddate string
            Startdate_str = '%d-%02d-%02d' % (Dates[0].year, Dates[0].month,
                                              Dates[0].day)
            Enddate_str = '%d-%02d-%02d' % (Dates[-1].year, Dates[-1].month,
                                            Dates[-1].day)

            # Define dimensions
            size_z = len(Dates)

            for Date in Dates:
                try:
                    sys.stdout.write("\rLoading Data %s %i/%i (%f %%)" %
                                     (Variable, i, len(Dates), i /
                                      (len(Dates)) * 100))
                    sys.stdout.flush()

                    # Create input filename
                    filename_in = os.path.join(
                        input_folder,
                        input_format.format(yyyy=Date.year,
                                            mm=Date.month,
                                            dd=Date.day))

                    dest, Array, proj, geo = Get_Data(filename_in,
                                                      Example_Data,
                                                      reprojection_type)

                    if Date == Dates[0]:
                        size_x = dest.RasterXSize
                        size_y = dest.RasterYSize
                        proj = dest.GetProjection()
                        geo = dest.GetGeoTransform()
                        Array_end = np.ones([len(Dates), size_y, size_x
                                             ]) * np.nan

                    if gap_filling != None and ~np.isnan(np.nanmean(Array)):
                        Array[np.isnan(Array)] = -9999
                        Array = RC.gap_filling(Array, -9999, gap_filling)

                    Array_end[time_or == Date.toordinal(
                    ), :, :] = Array * Conversion * MASK
                    i += 1
                except:
                    print("Was not able to collect %s %s" % (Variable, Date))

            shape = [size_z, size_y, size_x]

        else:

            sys.stdout.write("\rLoading Constant Data %s" % (Variable))
            sys.stdout.flush()

            # Define start and enddate string
            Startdate_str = ''
            Enddate_str = ''

            # Get constant data
            filename_in = os.path.join(input_folder, input_format)
            dest, Array_end, proj, geo = Get_Data(filename_in, Example_Data,
                                                  reprojection_type)

            # Get dimension
            size_x = dest.RasterXSize
            size_y = dest.RasterYSize
            shape = [size_y, size_x]
            time_or = ''

            # Apply gapfilling if needed
            if gap_filling != None and ~np.isnan(np.nanmean(Array_end)):
                Array_end[np.isnan(Array_end)] = -9999
                Array_end = RC.gap_filling(Array_end, -9999, gap_filling)
            Array_end = Array_end * MASK

        self.Projection = proj
        self.Size = shape
        self.GeoTransform = geo
        self.Data = Array_end.astype(np.float64)
        self.NoData = np.nan
        self.Ordinal_time = time_or

        # Origin Of Data
        self.Directory = input_folder
        self.Format = input_format

        # Describtion of dataset
        self.Variable = Variable
        self.Product = Product
        self.Description = Description
        self.Unit = Unit
        self.Dimension_description = Dimension_description

        # Time Series
        self.Startdate = Startdate_str
        self.Enddate = Enddate_str
        self.Timestep = ''
        print(
            "                                                                                                                      "
        )
Example #4
0
def calc_ETref(Dir, tmin_str, tmax_str, humid_str, press_str, wind_str,
               down_short_str, down_long_str, up_long_str, DEMmap_str, DOY):
    """
    This function calculates the ETref by using all the input parameters (path)
    according to FAO standards
    see: http://www.fao.org/docrep/x0490e/x0490e08.htm#TopOfPage

    Keyword arguments:
    tmin_str -- 'C:/'  path to the minimal temperature tiff file [degrees Celcius], e.g. from GLDAS
    tmax_str -- 'C:/'  path to the maximal temperature tiff file [degrees Celcius], e.g. from GLDAS
    humid_str -- 'C:/'  path to the humidity tiff file [kg/kg], e.g. from GLDAS
    press_str -- 'C:/'  path to the air pressure tiff file [kPa], e.g. from GLDAS
    wind_str -- 'C:/'  path to the wind velocity tiff file [m/s], e.g. from GLDAS
    down_short_str -- 'C:/'  path to the downward shortwave radiation tiff file [W*m-2], e.g. from CFSR/LANDSAF
    down_long_str -- 'C:/'  path to the downward longwave radiation tiff file [W*m-2], e.g. from CFSR/LANDSAF
    up_long_str -- 'C:/'  path to the upward longwave radiation tiff file [W*m-2], e.g. from CFSR/LANDSAF
    DEMmap_str -- 'C:/'  path to the DEM tiff file [m] e.g. from HydroSHED
    DOY -- Day of the year
    """

    # Get some geo-data to save results
    GeoT, Projection, xsize, ysize = RC.Open_array_info(DEMmap_str)
    #NDV, xsize, ysize, GeoT, Projection, DataType = GetGeoInfo(DEMmap_str)
    raster_shape = [xsize, ysize]

    # Create array to store results
    ETref = np.zeros(raster_shape)

    # gap fill
    tmin_str_GF = RC.gap_filling(tmin_str, -9999)
    tmax_str_GF = RC.gap_filling(tmax_str, -9999)
    humid_str_GF = RC.gap_filling(humid_str, -9999)
    press_str_GF = RC.gap_filling(press_str, -9999)
    wind_str_GF = RC.gap_filling(wind_str, -9999)
    down_short_str_GF = RC.gap_filling(down_short_str, np.nan)
    down_long_str_GF = RC.gap_filling(down_long_str, np.nan)
    if up_long_str is not 'not':
        up_long_str_GF = RC.gap_filling(up_long_str, np.nan)
    else:
        up_long_str_GF = 'nan'

    #dictionary containing all tthe paths to the input-maps
    inputs = dict({
        'tmin': tmin_str_GF,
        'tmax': tmax_str_GF,
        'humid': humid_str_GF,
        'press': press_str_GF,
        'wind': wind_str_GF,
        'down_short': down_short_str_GF,
        'down_long': down_long_str_GF,
        'up_long': up_long_str_GF
    })

    #dictionary containing numpy arrays of al initial and intermediate variables
    input_array = dict({
        'tmin': None,
        'tmax': None,
        'humid': None,
        'press': None,
        'wind': None,
        'albedo': None,
        'down_short': None,
        'down_long': None,
        'up_short': None,
        'up_long': None,
        'net_radiation': None,
        'ea': None,
        'es': None,
        'delta': None
    })

    #APPLY LAPSE RATE CORRECTION ON TEMPERATURE
    tmin = lapse_rate(Dir, inputs['tmin'], DEMmap_str)
    tmax = lapse_rate(Dir, inputs['tmax'], DEMmap_str)

    #PROCESS PRESSURE MAPS
    press = adjust_P(Dir, inputs['press'], DEMmap_str)

    #PREPARE HUMIDITY MAPS
    dest = RC.reproject_dataset_example(inputs['humid'], DEMmap_str, method=2)
    humid = dest.GetRasterBand(1).ReadAsArray()
    dest = None

    #CORRECT WIND MAPS
    dest = RC.reproject_dataset_example(inputs['wind'], DEMmap_str, method=2)
    wind = dest.GetRasterBand(1).ReadAsArray() * 0.75
    dest = None

    #PROCESS GLDAS DATA
    input_array['ea'], input_array['es'], input_array['delta'] = process_GLDAS(
        tmax, tmin, humid, press)

    ea = input_array['ea']
    es = input_array['es']
    delta = input_array['delta']

    if up_long_str == 'not':

        #CORRECT WIND MAPS
        dest = RC.reproject_dataset_example(down_short_str,
                                            DEMmap_str,
                                            method=2)
        Short_Net_data = dest.GetRasterBand(1).ReadAsArray() * 0.75
        dest = None

        dest = RC.reproject_dataset_example(down_long_str,
                                            DEMmap_str,
                                            method=2)
        Short_Clear_data = dest.GetRasterBand(1).ReadAsArray() * 0.75
        dest = None

        # Calculate Long wave Net radiation
        Rnl = 4.903e-9 * (
            ((tmin + 273.16)**4 +
             (tmax + 273.16)**4) / 2) * (0.34 - 0.14 * np.sqrt(ea)) * (
                 1.35 * Short_Net_data / Short_Clear_data - 0.35)

        # Calulate Net Radiation and converted to MJ*d-1*m-2
        net_radiation = (Short_Net_data * 0.77 + Rnl) * 86400 / 10**6

    else:
        #OPEN DOWNWARD SHORTWAVE RADIATION
        dest = RC.reproject_dataset_example(inputs['down_short'],
                                            DEMmap_str,
                                            method=2)
        down_short = dest.GetRasterBand(1).ReadAsArray()
        dest = None
        down_short, tau, bias = slope_correct(down_short, press, ea,
                                              DEMmap_str, DOY)

        #OPEN OTHER RADS
        up_short = down_short * 0.23

        dest = RC.reproject_dataset_example(inputs['down_long'],
                                            DEMmap_str,
                                            method=2)
        down_long = dest.GetRasterBand(1).ReadAsArray()
        dest = None

        dest = RC.reproject_dataset_example(inputs['up_long'],
                                            DEMmap_str,
                                            method=2)
        up_long = dest.GetRasterBand(1).ReadAsArray()
        dest = None

        #OPEN NET RADIATION AND CONVERT W*m-2 TO MJ*d-1*m-2
        net_radiation = ((down_short - up_short) +
                         (down_long - up_long)) * 86400 / 10**6

    #CALCULATE ETref
    ETref = (0.408 * delta * net_radiation + 0.665 * 10**-3 * press *
             (900 / ((tmax + tmin) / 2 + 273)) * wind *
             (es - ea)) / (delta + 0.665 * 10**-3 * press * (1 + 0.34 * wind))

    # Set limits ETref
    ETref[ETref < 0] = 0
    ETref[ETref > 400] = np.nan

    #return a reference ET map (numpy array), a dictionary containing all intermediate information and a bias of the slope correction on down_short
    return ETref
Example #5
0
Dates = pd.date_range(Startdate, Enddate, freq="D")

for Date in Dates:

    Day = Date.day
    Month = Date.month
    Year = Date.year

    Tempfile_one = Temp_folder.format(yyyy=Year, mm=Month, dd=Day)
    Presfile_one = Pres_folder.format(yyyy=Year, mm=Month, dd=Day)
    Humfile_one = Hum_folder.format(yyyy=Year, mm=Month, dd=Day)
    out_folder_one = out_folder.format(yyyy=Year, mm=Month, dd=Day)

    geo_out, proj, size_X, size_Y = RC.Open_array_info(Tempfile_one)
    Tdata = RC.Open_tiff_array(Tempfile_one)
    Tdata[Tdata < -900] = -9999
    Pdata = RC.Open_tiff_array(Presfile_one)
    Hdata = RC.Open_tiff_array(Humfile_one)
    Pdata[Pdata < 0] = -9999
    Hdata[Hdata < 0] = -9999

    # gapfilling
    Tdata = RC.gap_filling(Tdata, -9999)
    Pdata = RC.gap_filling(Pdata, -9999)
    Hdata = RC.gap_filling(Hdata, -9999)

    Esdata = 0.6108 * np.exp((17.27 * Tdata) / (Tdata + 237.3))
    HumData = np.minimum((1.6077717 * Hdata * Pdata / Esdata), 1) * 100
    HumData = HumData.clip(0, 100)

    DC.Save_as_tiff(out_folder_one, HumData, geo_out, "WGS84")