コード例 #1
0
def RetrieveData(Date, args):
    """
    This function retrieves MOD13 NDVI data for a given date from the
    http://e4ftl01.cr.usgs.gov/ server.

    Keyword arguments:
    Date -- 'yyyy-mm-dd'
    args -- A list of parameters defined in the DownloadData function.
    """

    # WAPOR modules
    import pyWAPOR.Functions.Processing_Functions as PF

    # Argument
    [
        output_folder, TilesVertical, TilesHorizontal, latlim, lonlim,
        username, password, hdf_library
    ] = args

    # Define output file
    NDVIfileName = os.path.join(
        output_folder, 'NDVI_MYD13Q1_-_16-daily_' + Date.strftime('%Y') + '.' +
        Date.strftime('%m') + '.' + Date.strftime('%d') + '.tif')

    if not os.path.exists(NDVIfileName):

        # Collect the data from the MODIS webpage and returns the data and lat and long in meters of those tiles
        try:
            Collect_data(TilesHorizontal, TilesVertical, Date, username,
                         password, output_folder, hdf_library)

            # Define the output name of the collect data function
            name_collect = os.path.join(output_folder, 'Merged.tif')

            # Reproject the MODIS product to epsg_to
            epsg_to = '4326'
            name_reprojected = PF.reproject_MODIS(name_collect, epsg_to)

            # Clip the data to the users extend
            data, geo = PF.clip_data(name_reprojected, latlim, lonlim)

            # Save results as Gtiff
            PF.Save_as_tiff(name=NDVIfileName,
                            data=data,
                            geo=geo,
                            projection='WGS84')

            # remove the side products
            os.remove(name_collect)
            os.remove(name_reprojected)

        except:
            print("Was not able to download the file")

    return True
コード例 #2
0
def DownloadData(output_folder, Startdate, Enddate, latlim, lonlim, Waitbar=1):

    import pyWAPOR.Functions.Processing_Functions as PF

    output_folder_Tot = os.path.join(output_folder, "WAPOR", "LandCover")
    if not os.path.exists(output_folder_Tot):
        os.makedirs(output_folder_Tot)

    # Define dates
    Dates = pd.date_range(Startdate, Enddate, freq="AS")

    # Create Waitbar
    if Waitbar == 1:
        import pyWAPOR.Functions.WaitbarConsole as WaitbarConsole
        total_amount = len(Dates)
        amount = 0
        WaitbarConsole.printWaitBar(amount,
                                    total_amount,
                                    prefix='Progress:',
                                    suffix='Complete',
                                    length=50)

    # Loop over dates
    for Date in Dates:

        file_name = os.path.join(output_folder_Tot,
                                 "LC_WAPOR_%s.01.01.tif" % Date.year)

        if not os.path.exists(file_name):

            # Get the data
            try:
                Array, geo, proj, file_name_temp = Download_data(
                    output_folder_Tot, Date, latlim, lonlim)
            except:
                print("Was not able to create %s" % file_name)
            # Save the data
            PF.Save_as_tiff(file_name, Array, geo, proj)

            # remove temp folder
            os.remove(file_name_temp)

        if Waitbar == 1:
            amount += 1
            WaitbarConsole.printWaitBar(amount,
                                        total_amount,
                                        prefix='Progress:',
                                        suffix='Complete',
                                        length=50)

    return ()
コード例 #3
0
ファイル: DataAccess.py プロジェクト: IMEN-CMD/wapor-et-look
def DownloadData(Dir, Var, Startdate, Enddate, latlim, lonlim, TimeStep,
                 Period, username, password, Waitbar):

    # WAPOR modules
    import pyWAPOR.Functions.Processing_Functions as PF

    # Check the latitude and longitude and otherwise set lat or lon on greatest extent
    if latlim[0] < -90 or latlim[1] > 90:
        print(
            'Latitude above 90N or below 90S is not possible. Value set to maximum'
        )
        latlim[0] = np.max(latlim[0], -90)
        latlim[1] = np.min(latlim[1], 90)
    if lonlim[0] < -180 or lonlim[1] > 180:
        print(
            'Longitude must be between 180E and 180W. Now value is set to maximum'
        )
        lonlim[0] = np.max(lonlim[0], -180)
        lonlim[1] = np.min(lonlim[1], 180)

    # Get information of the parameter
    VarInfo = VariablesInfo(TimeStep)
    Parameter = VarInfo.names[Var]
    unit = VarInfo.units[Var]
    types = VarInfo.types[Var]

    if TimeStep == "yearly":
        Parameter = "Temperature_Amplitude"

    # Create output folder
    output_folder = os.path.join(Dir, "MERRA", Parameter, TimeStep)
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    if TimeStep.split("_")[-1] == "MERRA2":
        corrx = 0.625 * 0.5
        corry = 0.5 * 0.5
    else:
        corrx = 0
        corry = 0

    # Define IDs
    IDx = [
        np.floor((lonlim[0] + corrx + 180) / 0.625),
        np.ceil((lonlim[1] + corrx + 180) / 0.625)
    ]
    IDy = [
        np.floor((latlim[0] + corry + 90) / 0.5),
        np.ceil((latlim[1] + corry + 90) / 0.5)
    ]

    # Create output geo transform
    Xstart = -180 + 0.625 * IDx[0] - corrx
    Ystart = -90 + 0.5 * IDy[1] - corry

    geo_out = tuple([Xstart, 0.625, 0, Ystart, 0, -0.5])
    proj = "WGS84"

    if TimeStep == "yearly":
        Dates = pd.date_range(Startdate, Enddate, freq="AS")
    else:
        Dates = pd.date_range(Startdate, Enddate, freq="D")

    # Create Waitbar
    if Waitbar == 1:
        import pyWAPOR.Functions.WaitbarConsole as WaitbarConsole
        total_amount = len(Dates)
        amount = 0
        WaitbarConsole.printWaitBar(amount,
                                    total_amount,
                                    prefix='Progress:',
                                    suffix='Complete',
                                    length=50)

    for Date in Dates:

        # Define the IDz
        if TimeStep == "hourly_MERRA2":
            Hour = int((Period - 1) * 1)
            output_name = os.path.join(
                output_folder,
                "%s_MERRA_%s_hourly_%d.%02d.%02d_H%02d.M00.tif" %
                (Var, unit, Date.year, Date.month, Date.day, Hour))
            output_folder_temp = os.path.join(Dir, "MERRA", "Temp")
            if not os.path.exists(output_folder_temp):
                os.makedirs(output_folder_temp)
            year = Date.year
            month = Date.month
            day = Date.day

        if TimeStep == "daily_MERRA2":
            output_name = os.path.join(
                output_folder, "%s_MERRA_%s_daily_%d.%02d.%02d.tif" %
                (Var, unit, Date.year, Date.month, Date.day))
            output_folder_temp = os.path.join(Dir, "MERRA", "Temp")
            if not os.path.exists(output_folder_temp):
                os.makedirs(output_folder_temp)
            year = Date.year
            month = Date.month
            day = Date.day

        if TimeStep == "three_hourly":
            IDz_start = IDz_end = int(
                ((Date - pd.Timestamp("2002-07-01")).days) * 8) + (Period - 1)
            Hour = int((Period - 1) * 3)
            output_name = os.path.join(
                output_folder,
                "%s_MERRA_%s_3-hourly_%d.%02d.%02d_H%02d.M00.tif" %
                (Var, unit, Date.year, Date.month, Date.day, Hour))

        if TimeStep == "daily":
            IDz_start = int(((Date - pd.Timestamp("2002-07-01")).days) * 8)
            IDz_end = IDz_start + 7
            output_name = os.path.join(
                output_folder, "%s_MERRA_%s_daily_%d.%02d.%02d.tif" %
                (Var, unit, Date.year, Date.month, Date.day))

        if TimeStep == "yearly":

            # Change date for now, there is no OpenDAP system for those years.
            if Date >= datetime.datetime(2015, 1, 1):
                Data_data = datetime.datetime(2014, 1, 1)
            else:
                Data_data = Date

            IDz_start = (Data_data.year - pd.Timestamp("2002-07-01").year
                         ) * 12 + Data_data.month - pd.Timestamp(
                             "2002-07-01").month
            IDz_end = IDz_start + 11
            output_name = os.path.join(
                output_folder, "Tamp_MERRA_%s_yearly_%d.%02d.%02d.tif" %
                (unit, Date.year, Date.month, Date.day))

        if not os.path.exists(output_name):

            if (TimeStep == "hourly_MERRA2" or TimeStep == "daily_MERRA2"):

                if Date < datetime.datetime(1992, 1, 1):
                    number = 1
                elif (Date >= datetime.datetime(1992, 1, 1)
                      and Date < datetime.datetime(2001, 1, 1)):
                    number = 2
                elif (Date >= datetime.datetime(2001, 1, 1)
                      and Date < datetime.datetime(2011, 1, 1)):
                    number = 3
                else:
                    number = 4

                if Var == "swgnet":
                    url_MERRA = r"https://goldsmr4.gesdisc.eosdis.nasa.gov/data/MERRA2/M2T1NXRAD.5.12.4/%d/%02d/MERRA2_%s00.tavg1_2d_rad_Nx.%d%02d%02d.nc4" % (
                        year, month, number, year, month, day)

                else:
                    url_MERRA = r"https://goldsmr4.gesdisc.eosdis.nasa.gov/data/MERRA2/M2I1NXASM.5.12.4/%d/%02d/MERRA2_%s00.inst1_2d_asm_Nx.%d%02d%02d.nc4" % (
                        year, month, number, year, month, day)

            if (TimeStep == "three_hourly" or TimeStep == "daily"):

                # define total url
                if (Var == "ps" or Var == "slp"):
                    url_start = r"https://opendap.nccs.nasa.gov/dods/GEOS-5/MERRAero/hourly/inst3hr_3d_asm_Nv."
                else:
                    url_start = r"https://opendap.nccs.nasa.gov/dods/GEOS-5/MERRAero/hourly/tavg3hr_2d_asm_Nx."

                url_MERRA = url_start + 'ascii?%s[%s:1:%s][%s:1:%s][%s:1:%s]' % (
                    Var, IDz_start, IDz_end, int(IDy[0]), int(
                        IDy[1]), int(IDx[0]), int(IDx[1]))

            if TimeStep == "yearly":
                url_start = r"https://opendap.nccs.nasa.gov/dods/GEOS-5/MERRAero/hourly/tavg3hr_2d_asm_Nx."
                url_MERRA = url_start + 'ascii?%s[%s:1:%s][%s:1:%s][%s:1:%s]' % (
                    Var, IDz_start, IDz_end, int(IDy[0]), int(
                        IDy[1]), int(IDx[0]), int(IDx[1]))
                url_MERRA = url_MERRA.replace("hourly", "monthly")

            # Reset the begin parameters for downloading
            downloaded = 0
            N = 0

            # if not downloaded try to download file
            while downloaded == 0:
                try:
                    if (TimeStep == "hourly_MERRA2"
                            or TimeStep == "daily_MERRA2"):

                        # Define the output name that is downloaded
                        file_name = os.path.join(output_folder_temp,
                                                 url_MERRA.split("/")[-1])
                        if not os.path.exists(file_name):

                            # make contact with server
                            x = requests.get(url_MERRA, allow_redirects=False)
                            try:
                                y = requests.get(x.headers['location'],
                                                 auth=(username, password))
                            except:
                                from requests.packages.urllib3.exceptions import InsecureRequestWarning
                                requests.packages.urllib3.disable_warnings(
                                    InsecureRequestWarning)
                                y = requests.get(x.headers['location'],
                                                 auth=(username, password),
                                                 verify=False)

                            # Write the download in the output directory
                            z = open(file_name, 'wb')
                            z.write(y.content)
                            z.close()
                            statinfo = os.stat(file_name)
                            # Say that download was succesfull
                            if int(statinfo.st_size) > 1000:
                                downloaded = 1
                        else:
                            downloaded = 1

                        data_end = Get_NC_data_end(file_name, Var, TimeStep,
                                                   Period, IDy, IDx, VarInfo)
                        #os.remove(file_name)

                    else:

                        # download data (first save as text file)
                        pathtext = os.path.join(output_folder,
                                                'temp%s.txt' % str(IDz_start))

                        # Download the data
                        urllib.request.urlretrieve(url_MERRA,
                                                   filename=pathtext)

                        # Reshape data
                        datashape = [
                            int(IDy[1] - IDy[0] + 1),
                            int(IDx[1] - IDx[0] + 1)
                        ]
                        data_start = np.genfromtxt(pathtext,
                                                   dtype=float,
                                                   skip_header=1,
                                                   skip_footer=6,
                                                   delimiter=',')
                        data_list = np.asarray(data_start[:, 1:])
                        if TimeStep == "yearly":
                            data_end = np.resize(
                                data_list, (12, datashape[0], datashape[1]))
                        if TimeStep == "daily":
                            data_end = np.resize(
                                data_list, (8, datashape[0], datashape[1]))
                        if TimeStep == "three_hourly":
                            data_end = np.resize(data_list,
                                                 (datashape[0], datashape[1]))
                        os.remove(pathtext)

                        # Set no data value
                        data_end[data_end > 1000000] = -9999

                        if TimeStep == "daily":
                            if types == "state":
                                data_end = np.nanmean(data_end, 0)
                            else:
                                data_end = np.nansum(data_end, 0)

                        if TimeStep == "yearly":
                            data_min = np.nanmin(data_end, 0)
                            data_max = np.nanmax(data_end, 0)
                            data_end = data_max - data_min

                        # Download was succesfull
                        downloaded = 1

                    # Add the VarFactor
                    if VarInfo.factors[Var] < 0:
                        data_end[data_end != -9999] = data_end[
                            data_end != -9999] + VarInfo.factors[Var]
                    else:
                        data_end[data_end != -9999] = data_end[
                            data_end != -9999] * VarInfo.factors[Var]
                    data_end[data_end < -9999] = -9999

                    # twist the data
                    data_end = np.flipud(data_end)

                    # Save as tiff file
                    PF.Save_as_tiff(output_name, data_end, geo_out, proj)

                # If download was not succesfull
                except:

                    # Try another time
                    N = N + 1

                    # Stop trying after 10 times
                    if N == 10:
                        print('Data from ' + Date.strftime('%Y-%m-%d') +
                              ' is not available')
                        downloaded = 1

        if Waitbar == 1:
            amount += 1
            WaitbarConsole.printWaitBar(amount,
                                        total_amount,
                                        prefix='Progress:',
                                        suffix='Complete',
                                        length=50)

    return ()
コード例 #4
0
def DownloadData(output_folder, latlim, lonlim):

    import pyWAPOR.Functions.Processing_Functions as PF

    # Check the latitude and longitude and otherwise set lat or lon on greatest extent
    if latlim[0] < -90 or latlim[1] > 90:
        print(
            'Latitude above 90N or below 90S is not possible. Value set to maximum'
        )
        latlim[0] = np.max(latlim[0], -90)
        latlim[1] = np.min(latlim[1], 90)
    if lonlim[0] < -180 or lonlim[1] > 180:
        print(
            'Longitude must be between 180E and 180W. Now value is set to maximum'
        )
        lonlim[0] = np.max(lonlim[0], -180)
        lonlim[1] = np.min(lonlim[1], 180)

    # Check output path and create if not exists
    output_folder_end = os.path.join(output_folder, "GlobCover", "Landuse")
    if not os.path.exists(output_folder_end):
        os.makedirs(output_folder_end)

    # Define end output
    filename_out_tiff = os.path.join(output_folder_end,
                                     "LC_GLOBCOVER_V2.3.tif")

    if not os.path.exists(filename_out_tiff):

        # Define url where to download the globcover data
        url = r"http://due.esrin.esa.int/files/Globcover2009_V2.3_Global_.zip"

        # Create temp folder
        output_folder_temp = os.path.join(output_folder, "GlobCover",
                                          "Landuse", "Temp")
        if not os.path.exists(output_folder_temp):
            os.makedirs(output_folder_temp)

        # define output layer
        filename_out = os.path.join(output_folder_temp,
                                    "Globcover2009_V2.3_Global_.zip")

        # Download the data
        urllib.request.urlretrieve(url, filename=filename_out)

        # Extract data
        PF.Extract_Data(filename_out, output_folder_temp)

        # Define extracted tiff file
        globcover_filename = os.path.join(
            output_folder_temp, "GLOBCOVER_L4_200901_200912_V2.3.tif")

        # Open extract file
        dest = gdal.Open(globcover_filename)
        Array = dest.GetRasterBand(1).ReadAsArray()

        # Get information of geotransform and projection
        geo = dest.GetGeoTransform()
        proj = "WGS84"

        # define the spatial ids
        Xid = [
            np.floor((-geo[0] + lonlim[0]) / geo[1]),
            np.ceil((-geo[0] + lonlim[1]) / geo[1])
        ]
        Yid = [
            np.floor((geo[3] - latlim[1]) / -geo[5]),
            np.ceil((geo[3] - latlim[0]) / -geo[5])
        ]

        # Define the geotransform
        Xstart = geo[0] + Xid[0] * geo[1]
        Ystart = geo[3] + Yid[0] * geo[5]
        geo_out = tuple([Xstart, geo[1], 0, Ystart, 0, geo[5]])

        # Clip data out
        Array_end = Array[int(Yid[0]):int(Yid[1]), int(Xid[0]):int(Xid[1])]

        # Save data as tiff
        PF.Save_as_tiff(filename_out_tiff, Array_end, geo_out, proj)
        dest = None

        # remove temporary folder
        shutil.rmtree(output_folder_temp)
コード例 #5
0
ファイル: DataAccess.py プロジェクト: IMEN-CMD/wapor-et-look
def DownloadData(output_folder, latlim, lonlim):
    """
    This function downloads DEM data from SRTM

    Keyword arguments:
    output_folder -- directory of the result
	latlim -- [ymin, ymax] (values must be between -60 and 60)
    lonlim -- [xmin, xmax] (values must be between -180 and 180)
    """

    # WA+ modules
    import pyWAPOR.Functions.Processing_Functions as PF

    # Check the latitude and longitude and otherwise set lat or lon on greatest extent
    if latlim[0] < -60 or latlim[1] > 60:
        print(
            'Latitude above 60N or below 60S is not possible. Value set to maximum'
        )
        latlim[0] = np.max(latlim[0], -60)
        latlim[1] = np.min(latlim[1], 60)
    if lonlim[0] < -180 or lonlim[1] > 180:
        print(
            'Longitude must be between 180E and 180W. Now value is set to maximum'
        )
        lonlim[0] = np.max(lonlim[0], -180)
        lonlim[1] = np.min(lonlim[1], 180)

    # converts the latlim and lonlim into names of the tiles which must be
    # downloaded
    name, rangeLon, rangeLat = Find_Document_Names(latlim, lonlim)

    # Memory for the map x and y shape (starts with zero)
    size_X_tot = 0
    size_Y_tot = 0

    nameResults = []

    # Create a temporary folder for processing
    output_folder_trash = os.path.join(output_folder, "Temp")
    if not os.path.exists(output_folder_trash):
        os.makedirs(output_folder_trash)

    # Download, extract, and converts all the files to tiff files
    for nameFile in name:

        try:
            # Download the data from
            # http://earlywarning.usgs.gov/hydrodata/
            output_file, file_name = Download_Data(nameFile,
                                                   output_folder_trash)

            # extract zip data
            PF.Extract_Data(output_file, output_folder_trash)

            # The input is the file name and in which directory the data must be stored
            file_name_tiff = file_name.replace(".zip", ".tif")
            output_tiff = os.path.join(output_folder_trash, file_name_tiff)

            # convert data from adf to a tiff file
            dest_SRTM = gdal.Open(output_tiff)
            geo_out = dest_SRTM.GetGeoTransform()
            size_X = dest_SRTM.RasterXSize
            size_Y = dest_SRTM.RasterYSize

            if (int(size_X) != int(6001) or int(size_Y) != int(6001)):
                data = np.ones((6001, 6001)) * -9999

                # Create the latitude bound
                Vfile = nameFile.split("_")[2][0:2]
                Bound2 = 60 - 5 * (int(Vfile) - 1)

                # Create the longitude bound
                Hfile = nameFile.split("_")[1]
                Bound1 = -180 + 5 * (int(Hfile) - 1)

                Expected_X_min = Bound1
                Expected_Y_max = Bound2

                Xid_start = int(
                    np.round((geo_out[0] - Expected_X_min) / geo_out[1]))
                Xid_end = int(
                    np.round(
                        ((geo_out[0] + size_X * geo_out[1]) - Expected_X_min) /
                        geo_out[1]))
                Yid_start = int(
                    np.round((Expected_Y_max - geo_out[3]) / (-geo_out[5])))
                Yid_end = int(
                    np.round((Expected_Y_max - (geo_out[3] +
                                                (size_Y * geo_out[5]))) /
                             (-geo_out[5])))

                data_SRTM = dest_SRTM.GetRasterBand(1).ReadAsArray()

                data[Yid_start:Yid_end, Xid_start:Xid_end] = data_SRTM
                if np.max(data) == 255:
                    data[data == 255] = -9999
                data[data < -9999] = -9999

                geo_in = [
                    Bound1 - 0.5 * 0.00083333333333333, 0.00083333333333333,
                    0.0, Bound2 + 0.5 * 0.00083333333333333, 0.0,
                    -0.0008333333333333333333
                ]

                # save chunk as tiff file
                PF.Save_as_tiff(name=output_tiff,
                                data=data,
                                geo=geo_in,
                                projection="WGS84")

                dest_SRTM = None

        except:

            # If tile not exist create a replacing zero tile (sea tiles)
            file_name_tiff = file_name.replace(".zip", ".tif")
            output_tiff = os.path.join(output_folder_trash, file_name_tiff)
            file_name = nameFile
            data = np.ones((6001, 6001)) * -9999
            data = data.astype(np.float32)

            # Create the latitude bound
            Vfile = nameFile.split("_")[2][0:2]
            Bound2 = 60 - 5 * (int(Vfile) - 1)

            # Create the longitude bound
            Hfile = nameFile.split("_")[1]
            Bound1 = -180 + 5 * (int(Hfile) - 1)

            # Geospatial data for the tile
            geo_in = [
                Bound1 - 0.5 * 0.00083333333333333, 0.00083333333333333, 0.0,
                Bound2 + 0.5 * 0.00083333333333333, 0.0,
                -0.0008333333333333333333
            ]

            # save chunk as tiff file
            PF.Save_as_tiff(name=output_tiff,
                            data=data,
                            geo=geo_in,
                            projection="WGS84")

        # clip data
        Data, Geo_data = PF.clip_data(output_tiff, latlim, lonlim)
        size_Y_out = int(np.shape(Data)[0])
        size_X_out = int(np.shape(Data)[1])

        # Total size of the product so far
        size_Y_tot = int(size_Y_tot + size_Y_out)
        size_X_tot = int(size_X_tot + size_X_out)

        if nameFile is name[0]:
            Geo_x_end = Geo_data[0]
            Geo_y_end = Geo_data[3]
        else:
            Geo_x_end = np.min([Geo_x_end, Geo_data[0]])
            Geo_y_end = np.max([Geo_y_end, Geo_data[3]])

        # create name for chunk
        FileNameEnd = "%s_temporary.tif" % (file_name)
        nameForEnd = os.path.join(output_folder_trash, FileNameEnd)
        nameResults.append(str(nameForEnd))

        # save chunk as tiff file
        PF.Save_as_tiff(name=nameForEnd,
                        data=Data,
                        geo=Geo_data,
                        projection="WGS84")

    size_X_end = int(size_X_tot / len(rangeLat)) + 1  #!
    size_Y_end = int(size_Y_tot / len(rangeLon)) + 1  #!

    # Define the georeference of the end matrix
    geo_out = [Geo_x_end, Geo_data[1], 0, Geo_y_end, 0, Geo_data[5]]

    latlim_out = [geo_out[3] + geo_out[5] * size_Y_end, geo_out[3]]
    lonlim_out = [geo_out[0], geo_out[0] + geo_out[1] * size_X_end]

    # merge chunk together resulting in 1 tiff map
    datasetTot = Merge_DEM(latlim_out, lonlim_out, nameResults, size_Y_end,
                           size_X_end)

    datasetTot[datasetTot < -9999] = -9999

    # name of the end result
    output_DEM_name = "DEM_SRTM_m_3s.tif"

    Save_name = os.path.join(output_folder, output_DEM_name)

    # Make geotiff file
    PF.Save_as_tiff(name=Save_name,
                    data=datasetTot,
                    geo=geo_out,
                    projection="WGS84")
    os.chdir(output_folder)

    # Delete the temporary folder
    try:
        shutil.rmtree(output_folder_trash)
    except:
        pass

    return ()
コード例 #6
0
def RetrieveData(Date, args):
    """
    This function retrieves MYD11 LST data for a given date from the
    https://e4ftl01.cr.usgs.gov/ server.

    Keyword arguments:
    Date -- 'yyyy-mm-dd'
    args -- A list of parameters defined in the DownloadData function.
    """
    
    # WAPOR modules
    import pyWAPOR.Functions.Processing_Functions as PF
    
    # Argument
    [output_folder, TilesVertical, TilesHorizontal,lonlim, latlim, username, password, hdf_library] = args
    
    # Define output names
    LSTfileName = os.path.join(output_folder, 'LST_MYD11A1_K_daily_' + Date.strftime('%Y') + '.' + Date.strftime('%m') + '.' + Date.strftime('%d') + '.tif')
    TimefileName = os.path.join(output_folder, 'Time_MYD11A1_hour_daily_' + Date.strftime('%Y') + '.' + Date.strftime('%m') + '.' + Date.strftime('%d') + '.tif')    
    OnsangfileName = os.path.join(output_folder, 'Angle_MYD11A1_degrees_daily_' + Date.strftime('%Y') + '.' + Date.strftime('%m') + '.' + Date.strftime('%d') + '.tif')    

    if not (os.path.exists(LSTfileName) and os.path.exists(TimefileName)):

        # Collect the data from the MODIS webpage and returns the data and lat and long in meters of those tiles
        try:
            Collect_data(TilesHorizontal, TilesVertical, Date, username, password, output_folder, hdf_library)
        
            # Define the output name of the collect data function
            name_collect = os.path.join(output_folder, 'Merged.tif')
            name_collect_time = os.path.join(output_folder, 'Merged_Time.tif')
            name_collect_obsang = os.path.join(output_folder, 'Merged_Obsang.tif')
            
            # Reproject the MODIS product to epsg_to
            epsg_to ='4326'
            name_reprojected = PF.reproject_MODIS(name_collect, epsg_to)
            name_reprojected_time = PF.reproject_MODIS(name_collect_time, epsg_to)
            name_reprojected_obsang = PF.reproject_MODIS(name_collect_obsang, epsg_to)
            
            # Clip the data to the users extend
            data, geo = PF.clip_data(name_reprojected, latlim, lonlim)
            data_time, geo = PF.clip_data(name_reprojected_time, latlim, lonlim)
            data_obsang, geo = PF.clip_data(name_reprojected_obsang, latlim, lonlim)
            
            # remove wrong values
            data[data==0.] = -9999
            data_time[data_time==25.5] = -9999
           
            # Save results as Gtiff 
            PF.Save_as_tiff(name=LSTfileName, data=data, geo=geo, projection='WGS84')
            PF.Save_as_tiff(name=TimefileName, data=data_time, geo=geo, projection='WGS84')
            PF.Save_as_tiff(name=OnsangfileName, data=data_obsang, geo=geo, projection='WGS84')
             
            # remove the side products
            os.remove(name_collect)
            os.remove(name_reprojected)
            os.remove(name_collect_time)
            os.remove(name_reprojected_time) 
            os.remove(name_collect_obsang)
            os.remove(name_reprojected_obsang)             
        except:
            print("Was not able to download the file")

    return True
コード例 #7
0
def RetrieveData(Date, args):
    """
    This function retrieves CHIRPS data for a given date from the
    ftp://chg-ftpout.geog.ucsb.edu server.

    Keyword arguments:
    Date -- 'yyyy-mm-dd'
    args -- A list of parameters defined in the DownloadData function.
    """
    
    # WA+ modules
    import pyWAPOR.Functions.Processing_Functions as PF  

    # Argument
    [output_folder, xID, yID, lonlim, latlim] = args

    # Define output
    DirFileEnd = os.path.join(output_folder,'P_CHIRPS.v2.0_mm-day-1_daily_%s.%02s.%02s.tif' %(Date.strftime('%Y'), Date.strftime('%m'), Date.strftime('%d')))

    if not os.path.exists(DirFileEnd):

        # open ftp server
        ftp = FTP("chg-ftpout.geog.ucsb.edu", "", "")
        ftp.login()
    
    	# Define FTP path to directory
        pathFTP = 'pub/org/chg/products/CHIRPS-2.0/global_daily/tifs/p05/%s/' %Date.strftime('%Y')
    
        # find the document name in this directory
        ftp.cwd(pathFTP)
        listing = []
    
    	# read all the file names in the directory
        ftp.retrlines("LIST", listing.append)
    
    	# create all the input name (filename) and output (outfilename, filetif, DiFileEnd) names
        filename = 'chirps-v2.0.%s.%02s.%02s.tif.gz' %(Date.strftime('%Y'), Date.strftime('%m'), Date.strftime('%d'))
        outfilename = os.path.join(output_folder,'chirps-v2.0.%s.%02s.%02s.tif' %(Date.strftime('%Y'), Date.strftime('%m'), Date.strftime('%d')))
    
        # download the global rainfall file
        try:
            local_filename = os.path.join(output_folder, filename)
            lf = open(local_filename, "wb")
            ftp.retrbinary("RETR " + filename, lf.write, 8192)
            lf.close()
    
            # unzip the file
            zip_filename = os.path.join(output_folder, filename)
            PF.Extract_Data_gz(zip_filename, outfilename)
    
            # open tiff file
            dest = gdal.Open(outfilename)
            dataset = dest.GetRasterBand(1).ReadAsArray()
    
            # clip dataset to the given extent
            data = dataset[yID[0]:yID[1], xID[0]:xID[1]]
            data[data < 0] = -9999
    
            # save dataset as geotiff file
            geo = [lonlim[0], 0.05, 0, latlim[1], 0, -0.05]
            
            PF.Save_as_tiff(name=DirFileEnd, data=data, geo=geo, projection="WGS84")
    
        except:
            print("file not exists")
        
    return True
コード例 #8
0
def Combine_LST(folders_input_RAW, Startdate, Enddate):

    import pyWAPOR.Functions.Processing_Functions as PF

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

    output_folder_end = os.path.join(folders_input_RAW, "MODIS", "LST")

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

    for Date in Dates:

        LST_file = os.path.join(
            output_folder_end, "LST_MCD11A1_K_daily_%d.%02d.%02d.tif" %
            (Date.year, Date.month, Date.day))
        Time_file = os.path.join(
            output_folder_end, "Time_MCD11A1_hour_daily_%d.%02d.%02d.tif" %
            (Date.year, Date.month, Date.day))

        if not (os.path.exists(Time_file) or os.path.exists(LST_file)):
            filename_angle_mod = os.path.join(
                folders_input_RAW, "MODIS", "MOD11",
                "Angle_MOD11A1_degrees_daily_%d.%02d.%02d.tif" %
                (Date.year, Date.month, Date.day))
            filename_angle_myd = os.path.join(
                folders_input_RAW, "MODIS", "MYD11",
                "Angle_MYD11A1_degrees_daily_%d.%02d.%02d.tif" %
                (Date.year, Date.month, Date.day))
            filename_time_mod = os.path.join(
                folders_input_RAW, "MODIS", "MOD11",
                "Time_MOD11A1_hour_daily_%d.%02d.%02d.tif" %
                (Date.year, Date.month, Date.day))
            filename_time_myd = os.path.join(
                folders_input_RAW, "MODIS", "MYD11",
                "Time_MYD11A1_hour_daily_%d.%02d.%02d.tif" %
                (Date.year, Date.month, Date.day))
            filename_lst_mod = os.path.join(
                folders_input_RAW, "MODIS", "MOD11",
                "LST_MOD11A1_K_daily_%d.%02d.%02d.tif" %
                (Date.year, Date.month, Date.day))
            filename_lst_myd = os.path.join(
                folders_input_RAW, "MODIS", "MYD11",
                "LST_MYD11A1_K_daily_%d.%02d.%02d.tif" %
                (Date.year, Date.month, Date.day))

            dest_angle_mod = gdal.Open(filename_angle_mod)
            dest_angle_myd = gdal.Open(filename_angle_myd)
            dest_time_mod = gdal.Open(filename_time_mod)
            dest_time_mod = gdal.Open(filename_time_myd)
            dest_lst_mod = gdal.Open(filename_lst_mod)
            dest_lst_myd = gdal.Open(filename_lst_myd)

            Array_angle_mod = dest_angle_mod.GetRasterBand(1).ReadAsArray()
            Array_angle_myd = dest_angle_myd.GetRasterBand(1).ReadAsArray()
            Array_time_mod = dest_time_mod.GetRasterBand(1).ReadAsArray()
            Array_time_myd = dest_time_mod.GetRasterBand(1).ReadAsArray()
            Array_lst_mod = dest_lst_mod.GetRasterBand(1).ReadAsArray()
            Array_lst_myd = dest_lst_myd.GetRasterBand(1).ReadAsArray()

            LST = Array_lst_mod
            Time = Array_time_mod
            LST = np.where(
                np.abs(Array_angle_myd) < np.abs(Array_angle_mod),
                Array_lst_myd, LST)
            Time = np.where(
                np.abs(Array_angle_myd) < np.abs(Array_angle_mod),
                Array_time_myd, Time)

            proj_ex = dest_angle_mod.GetProjection()
            geo_ex = dest_angle_mod.GetGeoTransform()

            PF.Save_as_tiff(LST_file, LST, geo_ex, proj_ex)
            PF.Save_as_tiff(Time_file, Time, geo_ex, proj_ex)

    return ()
コード例 #9
0
def main(output_folder,
         Startdate,
         Enddate,
         latlim,
         lonlim,
         username,
         password,
         LandCover="GlobCover"):

    import pyWAPOR
    import pyWAPOR.Functions.Processing_Functions as PF

    ############################ Get General inputs ###############################

    # Define the input folders
    folders_input_RAW = os.path.join(output_folder, "RAW")
    folder_input_ETLook = os.path.join(output_folder, "ETLook_input")

    # Create folders if not exists
    if not os.path.exists(folders_input_RAW):
        os.makedirs(folders_input_RAW)
    if not os.path.exists(folder_input_ETLook):
        os.makedirs(folder_input_ETLook)

    # Define the dates
    Dates = pd.date_range(Startdate, Enddate, freq="D")

    ######################### Download LST MODIS data #############################

    # Download LST data
    pyWAPOR.Collect.MOD11.LST(folders_input_RAW, Startdate, Enddate, latlim,
                              lonlim, username, password)
    pyWAPOR.Collect.MYD11.LST(folders_input_RAW, Startdate, Enddate, latlim,
                              lonlim, username, password)
    Combine_LST(folders_input_RAW, Startdate, Enddate)

    ################## Download ALBEDO and NDVI MODIS data ########################

    # Extend the days for NDVI data with +8 from both sides
    Startdate_NDVI = datetime.datetime.strptime(
        Startdate, "%Y-%m-%d") - datetime.timedelta(days=8)
    Enddate_NDVI = datetime.datetime.strptime(
        Enddate, "%Y-%m-%d") + datetime.timedelta(days=8)

    Startdate_NDVI_str = datetime.datetime.strftime(Startdate_NDVI, "%Y-%m-%d")
    Enddate_NDVI_str = datetime.datetime.strftime(Enddate_NDVI, "%Y-%m-%d")

    # Download NDVI and ALBEDO data
    pyWAPOR.Collect.MOD13.NDVI(folders_input_RAW, Startdate_NDVI_str,
                               Enddate_NDVI_str, latlim, lonlim, username,
                               password)
    pyWAPOR.Collect.MYD13.NDVI(folders_input_RAW, Startdate_NDVI_str,
                               Enddate_NDVI_str, latlim, lonlim, username,
                               password)
    pyWAPOR.Collect.MCD43.ALBEDO(folders_input_RAW, Startdate, Enddate, latlim,
                                 lonlim, username, password)

    ######################## Download Rainfall Data ###############################

    # Download CHIRPS data
    pyWAPOR.Collect.CHIRPS.daily(folders_input_RAW, Startdate, Enddate, latlim,
                                 lonlim)

    ########################### Download DEM data #################################

    # Download DEM data
    pyWAPOR.Collect.SRTM.DEM(folders_input_RAW, latlim, lonlim)

    ############################ Download Landuse #################################
    if LandCover == "GlobCover":
        # Download Globcover data
        pyWAPOR.Collect.Globcover.Landuse(folders_input_RAW, latlim, lonlim)
    if LandCover == "WAPOR":
        # Download Globcover data
        pyWAPOR.Collect.WAPOR.LandCover(folders_input_RAW, latlim, lonlim,
                                        "%s-01-01" % (Startdate.split("-")[0]),
                                        "%s-12-31" % (Enddate.split("-")[0]))
    ############### Loop over days for the dynamic data ###############################

    # Create the inputs of MODIS for all the Dates
    for Date in Dates:

        try:
            # Define output folder
            folder_input_ETLook_Date = os.path.join(
                folder_input_ETLook,
                "%d%02d%02d" % (Date.year, Date.month, Date.day))
            if not os.path.exists(folder_input_ETLook_Date):
                os.makedirs(folder_input_ETLook_Date)

            # Find nearest date for NDVI
            Startdate_year = "%d-01-01" % Date.year
            Enddate_year = "%d-12-31" % Date.year

            # Create MODIS NDVI dataset
            Dates_eight_daily_year = pd.date_range(Startdate_year,
                                                   Enddate_year,
                                                   freq="8D")

            # find nearest NDVI date
            Date_nearest = min(Dates_eight_daily_year,
                               key=lambda Dates_eight_daily_year: abs(
                                   Dates_eight_daily_year - Date))

            # Create NDVI files for ETLook

            # try MOD13 and MYD13
            NDVI_file = os.path.join(
                folder_input_ETLook_Date,
                "NDVI_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            if not os.path.exists(NDVI_file):
                folder_RAW_file_NDVI = os.path.join(folders_input_RAW, "MODIS",
                                                    "{v}13")
                filename_NDVI = "NDVI_{v}13Q1_-_16-daily_%d.%02d.%02d.tif" % (
                    Date_nearest.year, Date_nearest.month, Date_nearest.day)

                if os.path.exists(
                        os.path.join(folder_RAW_file_NDVI,
                                     filename_NDVI).format(v="MOD")):
                    shutil.copy(
                        os.path.join(folder_RAW_file_NDVI,
                                     filename_NDVI).format(v="MOD"),
                        folder_input_ETLook_Date)
                    os.rename(
                        os.path.join(folder_input_ETLook_Date,
                                     filename_NDVI).format(v="MOD"), NDVI_file)

                elif os.path.exists(
                        os.path.join(folder_RAW_file_NDVI,
                                     filename_NDVI).format(v="MYD")):
                    shutil.copy(
                        os.path.join(folder_RAW_file_NDVI,
                                     filename_NDVI).format(v="MYD"),
                        folder_input_ETLook_Date)
                    os.rename(
                        os.path.join(folder_input_ETLook_Date,
                                     filename_NDVI).format(v="MYD"), NDVI_file)

                else:
                    print("NDVI is not available for date: %d%02d%02d" %
                          (Date.year, Date.month, Date.day))

            # Get example files
            dest_ex = gdal.Open(NDVI_file)
            geo_ex = dest_ex.GetGeoTransform()
            proj_ex = dest_ex.GetProjection()
            size_x_ex = dest_ex.RasterXSize
            size_y_ex = dest_ex.RasterYSize

            # Create ALBEDO files for ETLook

            # try MCD43
            ALBEDO_file = os.path.join(
                folder_input_ETLook_Date,
                "ALBEDO_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            if not os.path.exists(ALBEDO_file):
                folder_RAW_file_ALBEDO = os.path.join(folders_input_RAW,
                                                      "MODIS", "MCD43")
                filename_ALBEDO = "Albedo_MCD43A3_-_daily_%d.%02d.%02d.tif" % (
                    Date.year, Date.month, Date.day)

                if os.path.exists(
                        os.path.join(folder_RAW_file_ALBEDO, filename_ALBEDO)):
                    destalbedo = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_ALBEDO, filename_ALBEDO),
                                                              NDVI_file,
                                                              method=1)
                    albedo = destalbedo.GetRasterBand(1).ReadAsArray()
                    albedo[albedo <= -0.4] = -9999
                    PF.Save_as_tiff(ALBEDO_file, albedo, geo_ex, proj_ex)

                else:
                    print("ALBEDO is not available for date: %d%02d%02d" %
                          (Date.year, Date.month, Date.day))

            # Create LST files for ETLook
            LST_file = os.path.join(
                folder_input_ETLook_Date,
                "LST_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            Time_file = os.path.join(
                folder_input_ETLook_Date,
                "Time_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            if not os.path.exists(LST_file):
                folder_RAW_file_LST = os.path.join(folders_input_RAW, "MODIS",
                                                   "LST")
                filename_LST = "LST_MCD11A1_K_daily_%d.%02d.%02d.tif" % (
                    Date.year, Date.month, Date.day)
                filename_Time = "Time_MCD11A1_hour_daily_%d.%02d.%02d.tif" % (
                    Date.year, Date.month, Date.day)
                if os.path.exists(
                        os.path.join(folder_RAW_file_LST, filename_LST)):
                    destLST = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_LST, filename_LST),
                                                           NDVI_file,
                                                           method=2)
                    LST = destLST.GetRasterBand(1).ReadAsArray()
                    LST[LST == 0.0] = -9999
                    PF.Save_as_tiff(LST_file, LST, geo_ex, proj_ex)

                    destTime = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_LST, filename_Time),
                                                            NDVI_file,
                                                            method=1)
                    Time = destTime.GetRasterBand(1).ReadAsArray()
                    Time[Time == 0.0] = -9999
                    PF.Save_as_tiff(Time_file, Time, geo_ex, proj_ex)

                else:
                    print("LST is not available for date: %d%02d%02d" %
                          (Date.year, Date.month, Date.day))

            ####################### Create lat and lon rasters ############################

            Lon_file = os.path.join(
                folder_input_ETLook_Date,
                "Lon_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            Lat_file = os.path.join(
                folder_input_ETLook_Date,
                "Lat_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            if not (os.path.exists(Lon_file) or os.path.exists(Lat_file)):
                lon_deg = np.array(
                    [geo_ex[0] + np.arange(0, size_x_ex) * geo_ex[1]] *
                    size_y_ex)
                lat_deg = np.array(
                    [geo_ex[3] + np.arange(0, size_y_ex) * geo_ex[5]] *
                    size_x_ex).transpose()

                # save as tiff
                PF.Save_as_tiff(Lon_file, lon_deg, geo_ex, proj_ex)
                PF.Save_as_tiff(Lat_file, lat_deg, geo_ex, proj_ex)

            ########################## Create Time rasters ################################

            # calculate overall time
            dest_time = gdal.Open(Time_file)
            Time_array = dest_time.GetRasterBand(1).ReadAsArray()
            Time_array[Time_array == -9999] = np.nan
            dtime = np.nanmean(Time_array)
            if np.isnan(dtime):
                dtime = 12
            NowTime = datetime.datetime(Date.year, Date.month, Date.day,
                                        int(np.floor(dtime)),
                                        int((dtime - np.floor(dtime)) * 60))

            # Get DOY
            doy = int(Date.strftime("%j"))

            ####################### Create DEM rasters ############################

            # Create DEM files for ETLook
            DEM_file = os.path.join(folder_input_ETLook_Date, "DEM.tif")
            if not os.path.exists(DEM_file):
                folder_RAW_file_DEM = os.path.join(folders_input_RAW, "SRTM",
                                                   "DEM")
                filename_DEM = "DEM_SRTM_m_3s.tif"
                if os.path.exists(
                        os.path.join(folder_RAW_file_DEM, filename_DEM)):
                    destDEM = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_DEM, filename_DEM),
                                                           NDVI_file,
                                                           method=4)
                    DEM = destDEM.GetRasterBand(1).ReadAsArray()
                    PF.Save_as_tiff(DEM_file, DEM, geo_ex, proj_ex)

                else:
                    print("DEM is not available")

            ##################### Calculate SLope and Aspect ##############################
            Slope_file = os.path.join(folder_input_ETLook_Date, "Slope.tif")
            Aspect_file = os.path.join(folder_input_ETLook_Date, "Aspect.tif")
            if not (os.path.exists(Slope_file)
                    and os.path.exists(Aspect_file)):

                # open DEM
                destDEM = gdal.Open(DEM_file)
                DEM = destDEM.GetRasterBand(1).ReadAsArray()

                # constants
                pixel_spacing = 1000
                deg2rad = np.pi / 180.0  # Factor to transform from degree to rad
                rad2deg = 180.0 / np.pi  # Factor to transform from rad to degree

                # Calculate slope
                x, y = np.gradient(DEM, pixel_spacing, pixel_spacing)
                hypotenuse_array = np.hypot(x, y)
                slope = np.arctan(hypotenuse_array) * rad2deg

                # calculate aspect
                aspect = np.arctan2(y / pixel_spacing,
                                    -x / pixel_spacing) * rad2deg
                aspect = 180 + aspect

                # Save as tiff files
                PF.Save_as_tiff(Slope_file, slope, geo_ex, proj_ex)
                PF.Save_as_tiff(Aspect_file, aspect, geo_ex, proj_ex)
            ######################### Create Rainfall file ################################

            P_file = os.path.join(
                folder_input_ETLook_Date, "Precipitation_%d%02d%02d.tif" %
                (Date.year, Date.month, Date.day))
            if not os.path.exists(P_file):
                folder_RAW_file_P = os.path.join(folders_input_RAW,
                                                 "Precipitation", "CHIRPS")
                filename_P = "P_CHIRPS.v2.0_mm-day-1_daily_%d.%02d.%02d.tif" % (
                    Date.year, Date.month, Date.day)
                destP = PF.reproject_dataset_example(os.path.join(
                    folder_RAW_file_P, filename_P),
                                                     NDVI_file,
                                                     method=2)
                P = destP.GetRasterBand(1).ReadAsArray()
                PF.Save_as_tiff(P_file, P, geo_ex, proj_ex)

            ############################# Download METEO ##################################

            # Define the startdates for the METEO
            StartTime = datetime.datetime(Date.year, Date.month, Date.day, 0,
                                          0)
            EndTime = datetime.datetime(Date.year, Date.month, Date.day, 23,
                                        59)

            if (Date >= datetime.datetime(2016, 1, 1)
                    and Date < datetime.datetime(2017, 12, 1)):
                # find nearest Meteo time
                DateTime = pd.date_range(StartTime, EndTime,
                                         freq="H") + pd.offsets.Minute(30)
                Time_nearest = min(
                    DateTime, key=lambda DateTime: abs(DateTime - NowTime))
                Period = np.argwhere(DateTime == Time_nearest)[0][0] + 1

            else:
                # find nearest Meteo time
                DateTime = pd.date_range(StartTime, EndTime,
                                         freq="3H") + pd.offsets.Minute(90)
                Time_nearest = min(
                    DateTime, key=lambda DateTime: abs(DateTime - NowTime))
                Period = np.argwhere(DateTime == Time_nearest)[0][0] + 1

            # Download METEO data
            if Date < datetime.datetime(2016, 1, 1):
                pyWAPOR.Collect.MERRA.daily(
                    folders_input_RAW,
                    ['t2m', 'u2m', 'v2m', 'q2m', 'tpw', 'ps', 'slp'],
                    StartTime, EndTime, latlim, lonlim)
                pyWAPOR.Collect.MERRA.three_hourly(
                    folders_input_RAW,
                    ['t2m', 'u2m', 'v2m', 'q2m', 'tpw', 'ps', 'slp'],
                    StartTime, EndTime, latlim, lonlim, [int(Period)])
                str_METEO = "MERRA"
                inst_name = "three_hourly"
                day_name = "daily"
                hour_steps = 3
                file_time_inst = "3-hourly"

            elif (Date >= datetime.datetime(2016, 1, 1)
                  and Date < datetime.datetime(2017, 12, 1)):
                pyWAPOR.Collect.MERRA.daily_MERRA2(
                    folders_input_RAW,
                    ['t2m', 'u2m', 'v2m', 'q2m', 'tpw', 'ps', 'slp'],
                    StartTime, EndTime, latlim, lonlim, username, password)
                pyWAPOR.Collect.MERRA.hourly_MERRA2(
                    folders_input_RAW,
                    ['t2m', 'u2m', 'v2m', 'q2m', 'tpw', 'ps', 'slp'],
                    StartTime, EndTime, latlim, lonlim, [int(Period)],
                    username, password)
                str_METEO = "MERRA"
                inst_name = "hourly_MERRA2"
                day_name = "daily_MERRA2"
                hour_steps = 1
                file_time_inst = "hourly"

            else:
                pyWAPOR.Collect.GEOS.daily(
                    folders_input_RAW,
                    ['t2m', 'u2m', 'v2m', 'qv2m', 'tqv', 'ps', 'slp'],
                    StartTime, EndTime, latlim, lonlim)
                pyWAPOR.Collect.GEOS.three_hourly(
                    folders_input_RAW,
                    ['t2m', 'u2m', 'v2m', 'qv2m', 'tqv', 'ps', 'slp'],
                    StartTime, EndTime, latlim, lonlim, [int(Period)])
                str_METEO = "GEOS"
                inst_name = "three_hourly"
                day_name = "daily"
                hour_steps = 3
                file_time_inst = "3-hourly"

            # Air pressure
            pair_inst_file = os.path.join(
                folder_input_ETLook_Date,
                "Pair_inst_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            pair_inst_0_file = os.path.join(
                folder_input_ETLook_Date, "Pair_inst_0_%d%02d%02d.tif" %
                (Date.year, Date.month, Date.day))
            pair_24_0_file = os.path.join(
                folder_input_ETLook_Date,
                "Pair_24_0_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))

            if not (os.path.exists(pair_inst_file)
                    and os.path.exists(pair_inst_0_file)
                    and os.path.exists(pair_24_0_file)):
                folder_RAW_file_pair_inst = os.path.join(
                    folders_input_RAW, str_METEO, "Surface_Pressure",
                    inst_name)
                folder_RAW_file_pair_inst_0 = os.path.join(
                    folders_input_RAW, str_METEO, "Sea_Level_Pressure",
                    inst_name)
                folder_RAW_file_pair_24_0 = os.path.join(
                    folders_input_RAW, str_METEO, "Sea_Level_Pressure",
                    day_name)
                HourPeriod = hour_steps * (Period - 1)

                filename_pair_inst = "ps_%s_kpa_%s_%d.%02d.%02d_H%02d.M00.tif" % (
                    str_METEO, file_time_inst, Date.year, Date.month, Date.day,
                    HourPeriod)
                if os.path.exists(
                        os.path.join(folder_RAW_file_pair_inst,
                                     filename_pair_inst)):
                    destPairInst = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_pair_inst, filename_pair_inst),
                                                                NDVI_file,
                                                                method=2)
                    Pair_inst = destPairInst.GetRasterBand(1).ReadAsArray()
                    PF.Save_as_tiff(pair_inst_file, Pair_inst, geo_ex, proj_ex)

                else:
                    print("Pair instantenious is not available")

                filename_pair_inst_sea = "slp_%s_kpa_%s_%d.%02d.%02d_H%02d.M00.tif" % (
                    str_METEO, file_time_inst, Date.year, Date.month, Date.day,
                    HourPeriod)
                if os.path.exists(
                        os.path.join(folder_RAW_file_pair_inst_0,
                                     filename_pair_inst_sea)):
                    destPairInstSea = PF.reproject_dataset_example(
                        os.path.join(folder_RAW_file_pair_inst_0,
                                     filename_pair_inst_sea),
                        NDVI_file,
                        method=2)
                    Pair_inst_sea = destPairInstSea.GetRasterBand(
                        1).ReadAsArray()
                    PF.Save_as_tiff(pair_inst_0_file, Pair_inst_sea, geo_ex,
                                    proj_ex)

                else:
                    print("Pair sea level instantenious is not available")

                filename_pair_24_sea = "slp_%s_kpa_daily_%d.%02d.%02d.tif" % (
                    str_METEO, Date.year, Date.month, Date.day)
                if os.path.exists(
                        os.path.join(folder_RAW_file_pair_24_0,
                                     filename_pair_24_sea)):
                    destPair24Sea = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_pair_24_0, filename_pair_24_sea),
                                                                 NDVI_file,
                                                                 method=2)
                    Pair_24_sea = destPair24Sea.GetRasterBand(1).ReadAsArray()
                    PF.Save_as_tiff(pair_24_0_file, Pair_24_sea, geo_ex,
                                    proj_ex)

                else:
                    print("Pair sea level daily is not available")

            # Specific Humidity
            qv_inst_file = os.path.join(
                folder_input_ETLook_Date,
                "qv_inst_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            qv_24_file = os.path.join(
                folder_input_ETLook_Date,
                "qv_24_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))

            if not (os.path.exists(qv_inst_file)
                    and os.path.exists(qv_24_file)):
                folder_RAW_file_qv_inst = os.path.join(folders_input_RAW,
                                                       str_METEO,
                                                       "Specific_Humidity",
                                                       inst_name)
                folder_RAW_file_qv_24 = os.path.join(folders_input_RAW,
                                                     str_METEO,
                                                     "Specific_Humidity",
                                                     day_name)
                HourPeriod = hour_steps * (Period - 1)
                if str_METEO == "MERRA":
                    para = "q2m"
                else:
                    para = "qv2m"

                filename_qv_inst = "%s_%s_kg-kg-1_%s_%d.%02d.%02d_H%02d.M00.tif" % (
                    para, str_METEO, file_time_inst, Date.year, Date.month,
                    Date.day, HourPeriod)
                if os.path.exists(
                        os.path.join(folder_RAW_file_qv_inst,
                                     filename_qv_inst)):
                    destqvInst = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_qv_inst, filename_qv_inst),
                                                              NDVI_file,
                                                              method=2)
                    qv_inst = destqvInst.GetRasterBand(1).ReadAsArray()
                    PF.Save_as_tiff(qv_inst_file, qv_inst, geo_ex, proj_ex)

                else:
                    print("qv instantenious is not available")

                filename_qv_24 = "%s_%s_kg-kg-1_daily_%d.%02d.%02d.tif" % (
                    para, str_METEO, Date.year, Date.month, Date.day)
                if os.path.exists(
                        os.path.join(folder_RAW_file_qv_24, filename_qv_24)):
                    destqv24 = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_qv_24, filename_qv_24),
                                                            NDVI_file,
                                                            method=2)
                    qv_24 = destqv24.GetRasterBand(1).ReadAsArray()
                    PF.Save_as_tiff(qv_24_file, qv_24, geo_ex, proj_ex)

                else:
                    print("daily qv is not available")

            # Air temperature
            Tair_inst_file = os.path.join(
                folder_input_ETLook_Date,
                "tair_inst_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            Tair_24_file = os.path.join(
                folder_input_ETLook_Date,
                "tair_24_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))

            if not (os.path.exists(Tair_inst_file)
                    and os.path.exists(Tair_24_file)):
                folder_RAW_file_tair_inst = os.path.join(
                    folders_input_RAW, str_METEO, "Air_Temperature", inst_name)
                folder_RAW_file_tair_24 = os.path.join(folders_input_RAW,
                                                       str_METEO,
                                                       "Air_Temperature",
                                                       day_name)
                HourPeriod = hour_steps * (Period - 1)

                filename_tair_inst = "t2m_%s_K_%s_%d.%02d.%02d_H%02d.M00.tif" % (
                    str_METEO, file_time_inst, Date.year, Date.month, Date.day,
                    HourPeriod)
                if os.path.exists(
                        os.path.join(folder_RAW_file_tair_inst,
                                     filename_tair_inst)):
                    tair_inst = lapse_rate_temp(
                        os.path.join(folder_RAW_file_tair_inst,
                                     filename_tair_inst), DEM_file)
                    PF.Save_as_tiff(Tair_inst_file, tair_inst, geo_ex, proj_ex)

                else:
                    print("Tair instantenious is not available")

                filename_tair_24 = "t2m_%s_K_daily_%d.%02d.%02d.tif" % (
                    str_METEO, Date.year, Date.month, Date.day)
                if os.path.exists(
                        os.path.join(folder_RAW_file_tair_24,
                                     filename_tair_24)):
                    tair_24 = lapse_rate_temp(
                        os.path.join(folder_RAW_file_tair_24,
                                     filename_tair_24), DEM_file)
                    PF.Save_as_tiff(Tair_24_file, tair_24, geo_ex, proj_ex)
                # Also save the maximum and minimum daily temperatures
                Tair_24_file = os.path.join(
                    folder_input_ETLook_Date, "tair_max_24_%d%02d%02d.tif" %
                    (Date.year, Date.month, Date.day))
                filename_tair_max_24 = "t2m_%s_K_daily_max_%d.%02d.%02d.tif" % (
                    str_METEO, Date.year, Date.month, Date.day)
                if os.path.exists(
                        os.path.join(folder_RAW_file_tair_24,
                                     filename_tair_max_24)):
                    tair_24 = lapse_rate_temp(
                        os.path.join(folder_RAW_file_tair_24,
                                     filename_tair_max_24), DEM_file)
                    PF.Save_as_tiff(Tair_24_file, tair_24, geo_ex, proj_ex)
                Tair_24_file = os.path.join(
                    folder_input_ETLook_Date, "tair_min_24_%d%02d%02d.tif" %
                    (Date.year, Date.month, Date.day))
                filename_tair_min_24 = "t2m_%s_K_daily_min_%d.%02d.%02d.tif" % (
                    str_METEO, Date.year, Date.month, Date.day)
                if os.path.exists(
                        os.path.join(folder_RAW_file_tair_24,
                                     filename_tair_min_24)):
                    tair_24 = lapse_rate_temp(
                        os.path.join(folder_RAW_file_tair_24,
                                     filename_tair_min_24), DEM_file)
                    PF.Save_as_tiff(Tair_24_file, tair_24, geo_ex, proj_ex)

                else:
                    print("daily Tair is not available")

            # Wind Speed
            wind_inst_file = os.path.join(
                folder_input_ETLook_Date,
                "wind_inst_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))
            wind_24_file = os.path.join(
                folder_input_ETLook_Date,
                "wind_24_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))

            if not (os.path.exists(wind_inst_file)
                    and os.path.exists(wind_24_file)):
                folder_RAW_file_u2_inst = os.path.join(folders_input_RAW,
                                                       str_METEO,
                                                       "Eastward_Wind",
                                                       inst_name)
                folder_RAW_file_u2_24 = os.path.join(folders_input_RAW,
                                                     str_METEO,
                                                     "Eastward_Wind", day_name)
                folder_RAW_file_v2_inst = os.path.join(folders_input_RAW,
                                                       str_METEO,
                                                       "Northward_Wind",
                                                       inst_name)
                folder_RAW_file_v2_24 = os.path.join(folders_input_RAW,
                                                     str_METEO,
                                                     "Northward_Wind",
                                                     day_name)

                HourPeriod = hour_steps * (Period - 1)

                filename_u2_inst = "u2m_%s_m-s-1_%s_%d.%02d.%02d_H%02d.M00.tif" % (
                    str_METEO, file_time_inst, Date.year, Date.month, Date.day,
                    HourPeriod)
                filename_v2_inst = "v2m_%s_m-s-1_%s_%d.%02d.%02d_H%02d.M00.tif" % (
                    str_METEO, file_time_inst, Date.year, Date.month, Date.day,
                    HourPeriod)
                if (os.path.exists(
                        os.path.join(folder_RAW_file_u2_inst,
                                     filename_u2_inst)) and os.path.exists(
                                         os.path.join(folder_RAW_file_v2_inst,
                                                      filename_v2_inst))):
                    destu2inst = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_u2_inst, filename_u2_inst),
                                                              NDVI_file,
                                                              method=2)
                    destv2inst = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_v2_inst, filename_v2_inst),
                                                              NDVI_file,
                                                              method=2)
                    u2_inst = destu2inst.GetRasterBand(1).ReadAsArray()
                    v2_inst = destv2inst.GetRasterBand(1).ReadAsArray()
                    wind_inst = np.sqrt(u2_inst**2 + v2_inst**2)
                    PF.Save_as_tiff(wind_inst_file, wind_inst, geo_ex, proj_ex)

                else:
                    print("Wind instantenious is not available")

                filename_u2_24 = "u2m_%s_m-s-1_daily_%d.%02d.%02d.tif" % (
                    str_METEO, Date.year, Date.month, Date.day)
                filename_v2_24 = "v2m_%s_m-s-1_daily_%d.%02d.%02d.tif" % (
                    str_METEO, Date.year, Date.month, Date.day)
                if (os.path.exists(
                        os.path.join(folder_RAW_file_u2_24, filename_u2_24))
                        and os.path.exists(
                            os.path.join(folder_RAW_file_v2_24,
                                         filename_v2_24))):
                    destu224 = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_u2_24, filename_u2_24),
                                                            NDVI_file,
                                                            method=2)
                    destv224 = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_v2_24, filename_v2_24),
                                                            NDVI_file,
                                                            method=2)
                    u2_24 = destu224.GetRasterBand(1).ReadAsArray()
                    v2_24 = destv224.GetRasterBand(1).ReadAsArray()
                    wind_24 = np.sqrt(u2_24**2 + v2_24**2)
                    PF.Save_as_tiff(wind_24_file, wind_24, geo_ex, proj_ex)

                else:
                    print("daily Wind is not available")

            # Precipitable Water Vapor
            wv_inst_file = os.path.join(
                folder_input_ETLook_Date,
                "wv_inst_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))

            if not os.path.exists(wv_inst_file):
                folder_RAW_file_wv_inst = os.path.join(
                    folders_input_RAW, str_METEO,
                    "Total_Precipitable_Water_Vapor", inst_name)
                HourPeriod = hour_steps * (Period - 1)
                if str_METEO == "MERRA":
                    para = "tpw"
                else:
                    para = "tqv"

                filename_wv_inst = "%s_%s_mm_%s_%d.%02d.%02d_H%02d.M00.tif" % (
                    para, str_METEO, file_time_inst, Date.year, Date.month,
                    Date.day, HourPeriod)
                if os.path.exists(
                        os.path.join(folder_RAW_file_wv_inst,
                                     filename_wv_inst)):
                    destwvinst = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_wv_inst, filename_wv_inst),
                                                              NDVI_file,
                                                              method=2)
                    wv_inst = destwvinst.GetRasterBand(1).ReadAsArray()
                    PF.Save_as_tiff(wv_inst_file, wv_inst, geo_ex, proj_ex)

                else:
                    print(
                        "Total Precipitable Water Vapour instantenious is not available"
                    )

            ##################### Calculate Landmask ##############################

            LM_file = os.path.join(folder_input_ETLook_Date, "LandMask.tif")
            Bulk_file = os.path.join(folder_input_ETLook_Date,
                                     "Bulk_Stomatal_resistance.tif")
            MaxObs_file = os.path.join(folder_input_ETLook_Date,
                                       "Maximum_Obstacle_Height.tif")
            if not (os.path.exists(LM_file) and os.path.exists(Bulk_file)
                    and os.path.exists(MaxObs_file)):

                if LandCover == "GlobCover":
                    folder_RAW_file_LC = os.path.join(folders_input_RAW,
                                                      "GlobCover", "Landuse")
                    filename_LC = "LC_GLOBCOVER_V2.3.tif"
                if LandCover == "WAPOR":
                    folder_RAW_file_LC = os.path.join(folders_input_RAW,
                                                      "WAPOR", "LandCover")
                    filename_LC = "LC_WAPOR_%s.01.01.tif" % (Date.year)

                if os.path.exists(os.path.join(folder_RAW_file_LC,
                                               filename_LC)):
                    destLC = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_LC, filename_LC),
                                                          NDVI_file,
                                                          method=1)
                    LC = destLC.GetRasterBand(1).ReadAsArray()
                    LC[np.isnan(LC)] = -9999

                    # import list with numbers to convert globcover into other maps
                    import pyWAPOR.Functions.LandCover_Converter as LCC

                    if LandCover == "GlobCover":
                        # Get conversion between globcover and landmask
                        LU_LM_Classes = LCC.Globcover_LM()
                        LU_Bulk_Classes = LCC.Globcover_Bulk()
                        LU_MaxObs_Classes = LCC.Globcover_MaxObs()

                    if LandCover == "WAPOR":
                        # Get conversion between globcover and landmask
                        LU_LM_Classes = LCC.WAPOR_LM_LM()
                        LU_Bulk_Classes = LCC.WAPOR_Bulk_Bulk()
                        LU_MaxObs_Classes = LCC.WAPOR_MaxObs_MaxObs()

                    # Create Array for LandMask
                    LM = np.ones([size_y_ex, size_x_ex]) * np.nan
                    Bulk = np.ones([size_y_ex, size_x_ex]) * np.nan
                    MaxObs = np.ones([size_y_ex, size_x_ex]) * np.nan

                    # Create LandMask
                    for LU_LM_Class in LU_LM_Classes.keys():
                        Value_LM = LU_LM_Classes[LU_LM_Class]
                        Value_Bulk = LU_Bulk_Classes[LU_LM_Class]
                        Value_MaxObs = LU_MaxObs_Classes[LU_LM_Class]
                        LM[LC == LU_LM_Class] = Value_LM
                        Bulk[LC == LU_LM_Class] = Value_Bulk
                        MaxObs[LC == LU_LM_Class] = Value_MaxObs

                    # Save as tiff files
                    PF.Save_as_tiff(LM_file, LM, geo_ex, proj_ex)
                    PF.Save_as_tiff(Bulk_file, Bulk, geo_ex, proj_ex)
                    PF.Save_as_tiff(MaxObs_file, MaxObs, geo_ex, proj_ex)

                else:
                    print("LandCover is not available")

            ########################### Download amplitude ################################

            pyWAPOR.Collect.MERRA.yearly_T_Amplitude(folders_input_RAW,
                                                     [Date.year], latlim,
                                                     lonlim)

            # yearly amplitude temperature air
            Tair_amp_file = os.path.join(
                folder_input_ETLook_Date,
                "Tair_amp_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))

            if not os.path.exists(Tair_amp_file):
                folder_RAW_file_Tair_amp = os.path.join(
                    folders_input_RAW, "MERRA", "Temperature_Amplitude",
                    "yearly")

                filename_Tair_amp = "Tamp_MERRA_K_yearly_%d.01.01.tif" % (
                    Date.year)
                if os.path.exists(
                        os.path.join(folder_RAW_file_Tair_amp,
                                     filename_Tair_amp)):
                    desttairamp = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_Tair_amp, filename_Tair_amp),
                                                               NDVI_file,
                                                               method=2)
                    tair_amp = desttairamp.GetRasterBand(1).ReadAsArray()
                    PF.Save_as_tiff(Tair_amp_file, tair_amp, geo_ex, proj_ex)

                else:
                    print("Yearly Tair amplitude is not available")

            ######################## Download Transmissivity ##############################

            # Download MSGCPP data
            if Date < datetime.datetime(2016, 1, 1):
                pyWAPOR.Collect.MERRA.daily(folders_input_RAW, ['swgnet'],
                                            StartTime, EndTime, latlim, lonlim)
                str_TRANS = "MERRA"
                day_name = "daily"

            elif (Date >= datetime.datetime(2016, 1, 1)
                  and Date < datetime.datetime(2017, 1, 1)):
                pyWAPOR.Collect.MERRA.daily_MERRA2(folders_input_RAW,
                                                   ['swgnet'], StartTime,
                                                   EndTime, latlim, lonlim,
                                                   username, password)
                str_TRANS = "MERRA"
                day_name = "daily_MERRA2"

            else:
                pyWAPOR.Collect.MSGCPP.SDS(folders_input_RAW, StartTime,
                                           EndTime, latlim, lonlim)
                str_TRANS = "MSGCPP"
                day_name = "daily"

            # yearly amplitude temperature air
            Trans_file = os.path.join(
                folder_input_ETLook_Date,
                "Trans_24_%d%02d%02d.tif" % (Date.year, Date.month, Date.day))

            if not os.path.exists(Trans_file):

                # Calculate the extraterrestrial daily radiation
                destLat = gdal.Open(Lat_file)
                lat = destLat.GetRasterBand(1).ReadAsArray()

                Gsc = 1367  # Solar constant (W / m2)
                deg2rad = np.pi / 180.0
                # Computation of Hour Angle (HRA = w)
                B = 360. / 365 * (doy - 81)  # (degrees)
                # Computation of cos(theta), where theta is the solar incidence angle
                # relative to the normal to the land surface
                delta = np.arcsin(
                    np.sin(23.45 * deg2rad) *
                    np.sin(np.deg2rad(B)))  # Declination angle (radians)

                phi = lat * deg2rad  # latitude of the pixel (radians)

                dr = 1 + 0.033 * np.cos(doy * 2 * np.pi / 365)

                # Daily 24 hr radiation - For flat terrain only !
                ws_angle = np.arccos(-np.tan(phi) *
                                     np.tan(delta))  # Sunset hour angle ws

                # Extraterrestrial daily radiation, Ra (W/m2):
                Ra24_flat = (Gsc / np.pi * dr *
                             (ws_angle * np.sin(phi) * np.sin(delta) +
                              np.cos(phi) * np.cos(delta) * np.sin(ws_angle)))

                if str_TRANS == "MERRA":
                    folder_RAW_file_trans = os.path.join(
                        folders_input_RAW, str_TRANS,
                        "Surface_Net_Downward_Shortwave_Flux", day_name)
                    filename_trans = "swgnet_MERRA_W-m-2_daily_%d.%02d.%02d.tif" % (
                        Date.year, Date.month, Date.day)
                if str_TRANS == "MSGCPP":
                    folder_RAW_file_trans = os.path.join(
                        folders_input_RAW, str_TRANS, "SDS", "15min")
                    filename_trans = "SDS_MSGCPP_W-m-2_15min_%d.%02d.%02d_H{hour}.M{minutes}.tif" % (
                        Date.year, Date.month, Date.day)

                if str_TRANS == "MSGCPP":
                    import glob
                    os.chdir(folder_RAW_file_trans)
                    files = glob.glob(
                        filename_trans.format(hour="*", minutes="*"))
                    i = 0

                    # Open all the 15 minutes files
                    for file in files:
                        file_in = os.path.join(folder_RAW_file_trans, file)
                        destswgone = gdal.Open(file_in)
                        try:
                            swgnet_one = destswgone.GetRasterBand(
                                1).ReadAsArray()
                            swgnet_one[swgnet_one < 0] = 0
                            if not "geo_trans" in locals():
                                swgnet = np.ones([
                                    destswgone.RasterYSize,
                                    destswgone.RasterXSize,
                                    len(files)
                                ]) * np.nan
                                geo_trans = destswgone.GetGeoTransform()
                                proj_trans = destswgone.GetProjection()
                            swgnet[:, :, i] = swgnet_one
                        except:
                            pass
                        i += 1

                    # Calculate the daily mean
                    swgnet_mean = np.nanmean(swgnet, 2)
                    dest_swgnet_mean = PF.Save_as_MEM(swgnet_mean, geo_trans,
                                                      proj_trans)
                    destswgnet = PF.reproject_dataset_example(dest_swgnet_mean,
                                                              NDVI_file,
                                                              method=2)

                else:
                    destswgnet = PF.reproject_dataset_example(os.path.join(
                        folder_RAW_file_trans, filename_trans),
                                                              NDVI_file,
                                                              method=2)

                swgnet = destswgnet.GetRasterBand(1).ReadAsArray()
                trans = swgnet / Ra24_flat
                PF.Save_as_tiff(Trans_file, trans, geo_ex, proj_ex)
                del geo_trans

        except:
            print("No ETLook input dataset for %s" % Date)

    return ()
コード例 #10
0
ファイル: ETLook_code.py プロジェクト: DHI-GRAS/wapor-et-look
def main(input_folder, output_folder, Date):

    # Do not show warnings
    warnings.filterwarnings('ignore')

    import pyWAPOR.ETLook as ETLook
    import pyWAPOR.Functions.Processing_Functions as PF
    import pyWAPOR.ETLook.outputs as out

    # Define Date string
    Date_str = "%d%02d%02d" % (Date.year, Date.month, Date.day)

    # Input folder Date
    input_folder_date = os.path.join(input_folder, Date_str)

    ############################ Define inputs ################################

    #input_files
    ALBEDO_filename = os.path.join(input_folder_date,
                                   "ALBEDO_%s.tif" % Date_str)
    NDVI_filename = os.path.join(input_folder_date, "NDVI_%s.tif" % Date_str)
    LST_filename = os.path.join(input_folder_date, "LST_%s.tif" % Date_str)
    Time_filename = os.path.join(input_folder_date, "Time_%s.tif" % Date_str)
    Lat_filename = os.path.join(input_folder_date, "Lat_%s.tif" % Date_str)
    Lon_filename = os.path.join(input_folder_date, "Lon_%s.tif" % Date_str)
    DEM_filename = os.path.join(input_folder_date, "DEM.tif")
    Slope_filename = os.path.join(input_folder_date, "Slope.tif")
    Aspect_filename = os.path.join(input_folder_date, "Aspect.tif")
    LandMask_filename = os.path.join(input_folder_date, "LandMask.tif")
    Bulk_filename = os.path.join(input_folder_date,
                                 "Bulk_Stomatal_resistance.tif")
    MaxObs_filename = os.path.join(input_folder_date,
                                   "Maximum_Obstacle_Height.tif")
    Pair_24_0_filename = os.path.join(input_folder_date,
                                      "Pair_24_0_%s.tif" % Date_str)
    Pair_inst_0_filename = os.path.join(input_folder_date,
                                        "Pair_inst_0_%s.tif" % Date_str)
    Pair_inst_filename = os.path.join(input_folder_date,
                                      "Pair_inst_%s.tif" % Date_str)
    Pre_filename = os.path.join(input_folder_date,
                                "Precipitation_%s.tif" % Date_str)
    Hum_24_filename = os.path.join(input_folder_date,
                                   "qv_24_%s.tif" % Date_str)
    Hum_inst_filename = os.path.join(input_folder_date,
                                     "qv_inst_%s.tif" % Date_str)
    Tair_24_filename = os.path.join(input_folder_date,
                                    "tair_24_%s.tif" % Date_str)
    Tair_max_24_filename = os.path.join(input_folder_date,
                                        "tair_max_24_%s.tif" % Date_str)
    Tair_min_24_filename = os.path.join(input_folder_date,
                                        "tair_min_24_%s.tif" % Date_str)
    Tair_inst_filename = os.path.join(input_folder_date,
                                      "tair_inst_%s.tif" % Date_str)
    Tair_amp_filename = os.path.join(input_folder_date,
                                     "Tair_amp_%s.tif" % Date_str)
    Wind_24_filename = os.path.join(input_folder_date,
                                    "wind_24_%s.tif" % Date_str)
    Wind_inst_filename = os.path.join(input_folder_date,
                                      "wind_inst_%s.tif" % Date_str)
    WatCol_inst_filename = os.path.join(input_folder_date,
                                        "wv_inst_%s.tif" % Date_str)
    Trans_24_filename = os.path.join(input_folder_date,
                                     "Trans_24_%s.tif" % Date_str)

    ############################ Define outputs ###############################

    # Output folder Date
    output_folder_date = os.path.join(output_folder, Date_str)
    if not os.path.exists(output_folder_date):
        os.makedirs(output_folder_date)

    #output_files
    vc_filename = os.path.join(output_folder_date, "vc_%s.tif" % Date_str)
    lai_filename = os.path.join(output_folder_date, "LAI_%s.tif" % Date_str)
    lai_eff_filename = os.path.join(output_folder_date,
                                    "LAI_eff_%s.tif" % Date_str)
    sf_soil_filename = os.path.join(output_folder_date,
                                    "sf_soil_%s.tif" % Date_str)
    lat_filename = os.path.join(output_folder_date, "lat_%s.tif" % Date_str)
    slope_filename = os.path.join(output_folder_date,
                                  "slope_%s.tif" % Date_str)
    aspect_filename = os.path.join(output_folder_date,
                                   "aspect_%s.tif" % Date_str)
    ra_24_toa_filename = os.path.join(output_folder_date,
                                      "ra_24_toa_%s.tif" % Date_str)
    ws_filename = os.path.join(output_folder_date, "ws_%s.tif" % Date_str)
    diffusion_index_filename = os.path.join(
        output_folder_date, "diffusion_index_%s.tif" % Date_str)
    ra_24_filename = os.path.join(output_folder_date,
                                  "ra_24_%s.tif" % Date_str)
    stress_rad_filename = os.path.join(output_folder_date,
                                       "stress_rad_%s.tif" % Date_str)
    p_air_24_filename = os.path.join(output_folder_date,
                                     "p_air_24_%s.tif" % Date_str)
    vp_24_filename = os.path.join(output_folder_date,
                                  "vp_24_%s.tif" % Date_str)
    svp_24_filename = os.path.join(output_folder_date,
                                   "svp_24_%s.tif" % Date_str)
    vpd_24_filename = os.path.join(output_folder_date,
                                   "vpd_24_%s.tif" % Date_str)
    stress_vpd_filename = os.path.join(output_folder_date,
                                       "stress_vpd_%s.tif" % Date_str)
    stress_temp_filename = os.path.join(output_folder_date,
                                        "stress_temp_%s.tif" % Date_str)
    r_canopy_0_filename = os.path.join(output_folder_date,
                                       "r_canopy_0_%s.tif" % Date_str)
    t_air_k_24_filename = os.path.join(output_folder_date,
                                       "t_air_k_24_%s.tif" % Date_str)
    l_net_filename = os.path.join(output_folder_date,
                                  "l_net_%s.tif" % Date_str)
    int_mm_filename = os.path.join(output_folder_date,
                                   "int_mm_%s.tif" % Date_str)
    lh_24_filename = os.path.join(output_folder_date,
                                  "lh_24_%s.tif" % Date_str)
    int_wm2_filename = os.path.join(output_folder_date,
                                    "int_wm2_%s.tif" % Date_str)
    rn_24_filename = os.path.join(output_folder_date,
                                  "rn_24_%s.tif" % Date_str)
    rn_24_canopy_filename = os.path.join(output_folder_date,
                                         "rn_24_canopy_%s.tif" % Date_str)
    t_air_k_i_filename = os.path.join(output_folder_date,
                                      "t_air_k_i_%s.tif" % Date_str)
    vp_i_filename = os.path.join(output_folder_date, "vp_i_%s.tif" % Date_str)
    ad_moist_i_filename = os.path.join(output_folder_date,
                                       "ad_moist_i_%s.tif" % Date_str)
    ad_dry_i_filename = os.path.join(output_folder_date,
                                     "ad_dry_i_%s.tif" % Date_str)
    ad_i_filename = os.path.join(output_folder_date, "ad_i_%s.tif" % Date_str)
    u_b_i_bare_filename = os.path.join(output_folder_date,
                                       "u_b_i_bare_%s.tif" % Date_str)
    lon_filename = os.path.join(output_folder_date, "lon_%s.tif" % Date_str)
    ha_filename = os.path.join(output_folder_date, "ha_%s.tif" % Date_str)
    ied_filename = os.path.join(output_folder_date, "ied_%s.tif" % Date_str)
    h0_filename = os.path.join(output_folder_date, "h0_%s.tif" % Date_str)
    h0ref_filename = os.path.join(output_folder_date,
                                  "h0ref_%s.tif" % Date_str)
    m_filename = os.path.join(output_folder_date, "m_%s.tif" % Date_str)
    rotm_filename = os.path.join(output_folder_date, "rotm_%s.tif" % Date_str)
    Tl2_filename = os.path.join(output_folder_date, "Tl2_%s.tif" % Date_str)
    B0c_filename = os.path.join(output_folder_date, "B0c_%s.tif" % Date_str)
    Bhc_filename = os.path.join(output_folder_date, "Bhc_%s.tif" % Date_str)
    Dhc_filename = os.path.join(output_folder_date, "Dhc_%s.tif" % Date_str)
    ra_hor_clear_i_filename = os.path.join(output_folder_date,
                                           "ra_hor_clear_i_%s.tif" % Date_str)
    emiss_atm_i_filename = os.path.join(output_folder_date,
                                        "emiss_atm_i_%s.tif" % Date_str)
    rn_bare_filename = os.path.join(output_folder_date,
                                    "rn_bare_%s.tif" % Date_str)
    rn_full_filename = os.path.join(output_folder_date,
                                    "rn_full_%s.tif" % Date_str)
    u_b_i_full_filename = os.path.join(output_folder_date,
                                       "u_b_i_full_%s.tif" % Date_str)
    u_star_i_bare_filename = os.path.join(output_folder_date,
                                          "u_star_i_bare_%s.tif" % Date_str)
    u_star_i_full_filename = os.path.join(output_folder_date,
                                          "u_star_i_full_%s.tif" % Date_str)
    u_i_soil_filename = os.path.join(output_folder_date,
                                     "u_i_soil_%s.tif" % Date_str)
    ras_filename = os.path.join(output_folder_date, "ras_%s.tif" % Date_str)
    raa_filename = os.path.join(output_folder_date, "raa_%s.tif" % Date_str)
    rac_filename = os.path.join(output_folder_date, "rac_%s.tif" % Date_str)
    t_max_bare_filename = os.path.join(output_folder_date,
                                       "t_max_bare_%s.tif" % Date_str)
    t_max_full_filename = os.path.join(output_folder_date,
                                       "t_max_full_%s.tif" % Date_str)
    w_i_filename = os.path.join(output_folder_date, "w_i_%s.tif" % Date_str)
    t_dew_i_filename = os.path.join(output_folder_date,
                                    "t_dew_i_%s.tif" % Date_str)
    t_wet_i_filename = os.path.join(output_folder_date,
                                    "t_wet_i_%s.tif" % Date_str)
    t_wet_k_i_filename = os.path.join(output_folder_date,
                                      "t_wet_k_i_%s.tif" % Date_str)
    lst_max_filename = os.path.join(output_folder_date,
                                    "lst_max_%s.tif" % Date_str)
    se_root_filename = os.path.join(output_folder_date,
                                    "se_root_%s.tif" % Date_str)
    stress_moist_filename = os.path.join(output_folder_date,
                                         "stress_moist_%s.tif" % Date_str)
    r_canopy_0_filename = os.path.join(output_folder_date,
                                       "r_canopy_0_%s.tif" % Date_str)
    r_canopy_filename = os.path.join(output_folder_date,
                                     "r_canopy_%s.tif" % Date_str)
    z_obst_filename = os.path.join(output_folder_date,
                                   "z_obst_%s.tif" % Date_str)
    z_oro_filename = os.path.join(output_folder_date,
                                  "z_oro_%s.tif" % Date_str)
    z0m_filename = os.path.join(output_folder_date, "z0m_%s.tif" % Date_str)
    ra_canopy_init_filename = os.path.join(output_folder_date,
                                           "ra_canopy_init_%s.tif" % Date_str)
    u_b_24_filename = os.path.join(output_folder_date,
                                   "u_b_24_%s.tif" % Date_str)
    disp_filename = os.path.join(output_folder_date, "disp_%s.tif" % Date_str)
    u_star_24_init_filename = os.path.join(output_folder_date,
                                           "u_star_24_init_%s.tif" % Date_str)
    ad_dry_24_filename = os.path.join(output_folder_date,
                                      "ad_dry_24_%s.tif" % Date_str)
    ad_moist_24_filename = os.path.join(output_folder_date,
                                        "ad_moist_24_%s.tif" % Date_str)
    ad_24_filename = os.path.join(output_folder_date,
                                  "ad_24_%s.tif" % Date_str)
    psy_24_filename = os.path.join(output_folder_date,
                                   "psy_24_%s.tif" % Date_str)
    ssvp_24_filename = os.path.join(output_folder_date,
                                    "ssvp_24_%s.tif" % Date_str)
    t_24_init_filename = os.path.join(output_folder_date,
                                      "t_24_init_%s.tif" % Date_str)
    h_canopy_24_init_filename = os.path.join(
        output_folder_date, "h_canopy_24_init_%s.tif" % Date_str)
    t_24_filename = os.path.join(output_folder_date, "t_24_%s.tif" % Date_str)
    t_24_mm_filename = os.path.join(output_folder_date,
                                    "t_24_mm_%s.tif" % Date_str)
    sf_soil_filename = os.path.join(output_folder_date,
                                    "sf_soil_%s.tif" % Date_str)
    rn_24_soil_filename = os.path.join(output_folder_date,
                                       "rn_24_soil_%s.tif" % Date_str)
    r_soil_filename = os.path.join(output_folder_date,
                                   "r_soil_%s.tif" % Date_str)
    ra_soil_init_filename = os.path.join(output_folder_date,
                                         "ra_soil_init_%s.tif" % Date_str)
    u_b_24_filename = os.path.join(output_folder_date,
                                   "u_b_24_%s.tif" % Date_str)
    u_star_24_soil_init_filename = os.path.join(
        output_folder_date, "u_star_24_soil_init_%s.tif" % Date_str)
    g0_bs_filename = os.path.join(output_folder_date,
                                  "g0_bs_%s.tif" % Date_str)
    g0_24_filename = os.path.join(output_folder_date,
                                  "g0_24_%s.tif" % Date_str)
    e_24_init_filename = os.path.join(output_folder_date,
                                      "e_24_init_%s.tif" % Date_str)
    h_soil_24_init_filename = os.path.join(output_folder_date,
                                           "h_soil_24_init_%s.tif" % Date_str)
    e_24_filename = os.path.join(output_folder_date, "e_24_%s.tif" % Date_str)
    e_24_mm_filename = os.path.join(output_folder_date,
                                    "e_24_mm_%s.tif" % Date_str)
    et_24_mm_filename = os.path.join(output_folder_date,
                                     "et_24_mm_%s.tif" % Date_str)
    rn_24_grass_filename = os.path.join(output_folder_date,
                                        "rn_24_grass_%s.tif" % Date_str)
    et_ref_24_filename = os.path.join(output_folder_date,
                                      "et_ref_24_%s.tif" % Date_str)
    et_ref_24_mm_filename = os.path.join(output_folder_date,
                                         "et_ref_24_mm_%s.tif" % Date_str)

    ########################## Open input rasters #############################
    dest_lst = gdal.Open(LST_filename)
    lst = dest_lst.GetRasterBand(1).ReadAsArray()
    lst[lst == -9999] = np.nan

    dest_albedo = gdal.Open(ALBEDO_filename)
    r0 = dest_albedo.GetRasterBand(1).ReadAsArray()
    r0[np.isnan(lst)] = np.nan

    dest_ndvi = gdal.Open(NDVI_filename)
    ndvi = dest_ndvi.GetRasterBand(1).ReadAsArray()
    ndvi[np.isnan(lst)] = np.nan

    desttime = gdal.Open(Time_filename)
    dtime = desttime.GetRasterBand(1).ReadAsArray()
    dtime[np.isnan(lst)] = np.nan

    dest_lat = gdal.Open(Lat_filename)
    lat_deg = dest_lat.GetRasterBand(1).ReadAsArray()
    lat_deg[np.isnan(lst)] = np.nan

    dest_lon = gdal.Open(Lon_filename)
    lon_deg = dest_lon.GetRasterBand(1).ReadAsArray()
    lon_deg[np.isnan(lst)] = np.nan

    dest_dem = gdal.Open(DEM_filename)
    z = dest_dem.GetRasterBand(1).ReadAsArray()
    z[np.isnan(lst)] = np.nan

    dest_slope = gdal.Open(Slope_filename)
    slope_deg = dest_slope.GetRasterBand(1).ReadAsArray()
    slope_deg[np.isnan(lst)] = np.nan

    dest_aspect = gdal.Open(Aspect_filename)
    aspect_deg = dest_aspect.GetRasterBand(1).ReadAsArray()
    aspect_deg[np.isnan(lst)] = np.nan

    dest_lm = gdal.Open(LandMask_filename)
    land_mask = dest_lm.GetRasterBand(1).ReadAsArray()
    land_mask[np.isnan(lst)] = np.nan

    #dest_bulk = gdal.Open(Bulk_filename)
    #bulk = dest_bulk.GetRasterBand(1).ReadAsArray()

    dest_maxobs = gdal.Open(MaxObs_filename)
    z_obst_max = dest_maxobs.GetRasterBand(1).ReadAsArray()
    z_obst_max[np.isnan(lst)] = np.nan

    dest_pairsea24 = gdal.Open(Pair_24_0_filename)
    p_air_0_24 = dest_pairsea24.GetRasterBand(1).ReadAsArray()
    p_air_0_24 = ETLook.meteo.air_pressure_kpa2mbar(p_air_0_24)
    p_air_0_24[np.isnan(lst)] = np.nan

    dest_pairseainst = gdal.Open(Pair_inst_0_filename)
    p_air_0_i = dest_pairseainst.GetRasterBand(1).ReadAsArray()
    p_air_0_i = ETLook.meteo.air_pressure_kpa2mbar(p_air_0_i)
    p_air_0_i[np.isnan(lst)] = np.nan

    dest_pairinst = gdal.Open(Pair_inst_filename)
    p_air_i = dest_pairinst.GetRasterBand(1).ReadAsArray()
    p_air_i = ETLook.meteo.air_pressure_kpa2mbar(p_air_i)
    p_air_i[np.isnan(lst)] = np.nan

    dest_precip = gdal.Open(Pre_filename)
    P_24 = dest_precip.GetRasterBand(1).ReadAsArray()
    P_24[np.isnan(lst)] = np.nan

    dest_hum24 = gdal.Open(Hum_24_filename)
    qv_24 = dest_hum24.GetRasterBand(1).ReadAsArray()
    qv_24[np.isnan(lst)] = np.nan

    dest_huminst = gdal.Open(Hum_inst_filename)
    qv_i = dest_huminst.GetRasterBand(1).ReadAsArray()
    qv_i[np.isnan(lst)] = np.nan

    dest_tair24 = gdal.Open(Tair_24_filename)
    t_air_24 = dest_tair24.GetRasterBand(1).ReadAsArray()
    #t_air_24 = ETLook.meteo.disaggregate_air_temperature_daily(t_air_24_coarse, z, z_coarse, lapse)
    t_air_24[np.isnan(lst)] = np.nan

    dest_tair24 = gdal.Open(Tair_max_24_filename)
    t_air_max_24 = dest_tair24.GetRasterBand(1).ReadAsArray()
    t_air_max_24[np.isnan(lst)] = np.nan

    dest_tair24 = gdal.Open(Tair_min_24_filename)
    t_air_min_24 = dest_tair24.GetRasterBand(1).ReadAsArray()
    t_air_min_24[np.isnan(lst)] = np.nan

    dest_tairinst = gdal.Open(Tair_inst_filename)
    t_air_i = dest_tairinst.GetRasterBand(1).ReadAsArray()
    t_air_i[np.isnan(lst)] = np.nan

    dest_tairamp = gdal.Open(Tair_amp_filename)
    t_amp_year = dest_tairamp.GetRasterBand(1).ReadAsArray()
    t_amp_year[np.isnan(lst)] = np.nan

    dest_wind24 = gdal.Open(Wind_24_filename)
    u_24 = dest_wind24.GetRasterBand(1).ReadAsArray()
    u_24[np.isnan(lst)] = np.nan

    dest_windinst = gdal.Open(Wind_inst_filename)
    u_i = dest_windinst.GetRasterBand(1).ReadAsArray()
    u_i[np.isnan(lst)] = np.nan

    dest_watcol = gdal.Open(WatCol_inst_filename)
    wv_i = dest_watcol.GetRasterBand(1).ReadAsArray()
    wv_i[np.isnan(lst)] = np.nan

    dest_trans = gdal.Open(Trans_24_filename)
    trans_24 = dest_trans.GetRasterBand(1).ReadAsArray()
    trans_24[np.isnan(lst)] = np.nan

    # example file
    geo_ex = dest_albedo.GetGeoTransform()
    proj_ex = dest_albedo.GetProjection()

    ########################## Open input constants ###########################

    doy = int(Date.strftime("%j"))
    aod550_i = 0.01  # https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/61/MOD04_L2 heb niet echt een standaard product hiervan gevonden
    se_top = 0.5
    porosity = 0.4
    '''
    http://lawr.ucdavis.edu/classes/SSC100/probsets/pset01.html
    6. Calculate the porosity of a soil sample that has a bulk density of 1.35 g/cm3. Assume the particle density is 2.65 g/cm3.
    Porosity = (1-(r b/r d) x 100 = (1-(1.35/2.65)) x 100 = 49%
    '''

    # Create QC array
    QC = np.ones(lst.shape)
    QC[np.isnan(lst)] = np.nan

    # page 31 flow diagram

    # **effective_leaf_area_index**************************************************
    # constants or predefined:
    nd_min = 0.125
    nd_max = 0.8
    vc_pow = 0.7
    vc_min = 0
    vc_max = 0.9677324224821418
    lai_pow = -0.45

    # **atmospheric canopy resistance***********************************************
    # constants or predefined:
    diffusion_slope = -1.33
    diffusion_intercept = 1.15
    t_opt = 25  # optimal temperature for plant growth
    t_min = 0  # minimal temperature for plant growth
    t_max = 50  # maximal temperature for plant growth
    vpd_slope = -0.3
    rs_min = 70
    rcan_max = 1000000

    # **net radiation canopy******************************************************
    # constants or predefined:
    vp_slope = 0.14
    vp_offset = 0.34
    lw_slope = 1.35
    lw_offset = 0.35
    int_max = 0.2

    # **canopy resistance***********************************************************
    # constants or predefined:
    z_obs = 2
    z_b = 100
    z0m_bare = 0.001
    r0_bare = 0.38
    r0_full = 0.18
    tenacity = 1.5
    disp_bare = 0.0
    disp_full = 0.667
    fraction_h_bare = 0.65
    fraction_h_full = 0.95
    z0m_full = 0.1

    # **initial canopy aerodynamic resistance***********************************************************
    # constants or predefined:
    ndvi_obs_min = 0.25
    ndvi_obs_max = 0.75
    obs_fr = 0.25
    dem_resolution = 250

    # **ETLook.unstable.initial_friction_velocity_daily***********************************************************
    # constants or predefined:
    c1 = 1

    # **ETLook.unstable.transpiration***********************************************************
    # constants or predefined:
    iter_h = 3

    # **ETLook.resistance.soil_resistance***********************************************************
    # constants or predefined:
    r_soil_pow = -2.1
    r_soil_min = 800

    # **ETLook.unstable.initial_sensible_heat_flux_soil_daily***********************************************************
    # constants or predefined:
    #porosity = 0.4 #Note: soil dependent
    #se_top = 1.0 #Note should be input !
    rn_slope = 0.92
    rn_offset = -61.0

    # **ETLook.unstable.evaporation***********************************************************
    # constants or predefined:
    r0_grass = 0.23

    ######################## MODEL ETLOOK #########################################

    # **effective_leaf_area_index**************************************************
    vc = ETLook.leaf.vegetation_cover(ndvi, nd_min, nd_max, vc_pow)
    lai = ETLook.leaf.leaf_area_index(vc, vc_min, vc_max, lai_pow)
    lai_eff = ETLook.leaf.effective_leaf_area_index(lai)

    vc[np.isnan(QC)] = np.nan
    lai[np.isnan(QC)] = np.nan
    lai_eff[np.isnan(QC)] = np.nan
    if out.vc == 1:
        PF.Save_as_tiff(vc_filename, vc, geo_ex, proj_ex)
    if out.lai == 1:
        PF.Save_as_tiff(lai_filename, lai, geo_ex, proj_ex)
    if out.lai_eff == 1:
        PF.Save_as_tiff(lai_eff_filename, lai_eff, geo_ex, proj_ex)

    #*******TRANSPIRATION COMPONENT****************************************************************

    # **soil fraction**************************************************************
    sf_soil = ETLook.radiation.soil_fraction(lai)

    sf_soil[np.isnan(QC)] = np.nan
    if out.sf_soil == 1:
        PF.Save_as_tiff(sf_soil_filename, sf_soil, geo_ex, proj_ex)

    # **atmospheric canopy resistance***********************************************
    iesd = ETLook.solar_radiation.inverse_earth_sun_distance(doy)
    sc = ETLook.solar_radiation.seasonal_correction(doy)
    day_angle = ETLook.clear_sky_radiation.day_angle(doy)
    decl = ETLook.solar_radiation.declination(doy)
    lat = ETLook.solar_radiation.latitude_rad(lat_deg)
    slope = ETLook.solar_radiation.slope_rad(slope_deg)
    aspect = ETLook.solar_radiation.aspect_rad(aspect_deg)
    ra_24_toa = ETLook.solar_radiation.daily_solar_radiation_toa(
        sc, decl, iesd, lat, slope, aspect)
    ws = ETLook.solar_radiation.sunset_hour_angle(lat, decl)
    ra_24_toa_flat = ETLook.solar_radiation.daily_solar_radiation_toa_flat(
        decl, iesd, lat, ws)
    diffusion_index = ETLook.solar_radiation.diffusion_index(
        trans_24, diffusion_slope, diffusion_intercept)

    # choose one of the two options below
    #ra_24 = ETLook.solar_radiation.daily_solar_radiation_flat(ra_24_toa_flat, trans_24)
    ra_24 = ETLook.solar_radiation.daily_total_solar_radiation(
        ra_24_toa, ra_24_toa_flat, diffusion_index, trans_24)
    stress_rad = ETLook.stress.stress_radiation(ra_24)
    p_air_24 = ETLook.meteo.air_pressure_daily(z, p_air_0_24)
    vp_24 = ETLook.meteo.vapour_pressure_from_specific_humidity_daily(
        qv_24, p_air_24)
    svp_24 = ETLook.meteo.saturated_vapour_pressure_average(
        ETLook.meteo.saturated_vapour_pressure_maximum(t_air_max_24),
        ETLook.meteo.saturated_vapour_pressure_minimum(t_air_min_24))
    vpd_24 = ETLook.meteo.vapour_pressure_deficit_daily(svp_24, vp_24)
    stress_vpd = ETLook.stress.stress_vpd(vpd_24, vpd_slope)
    stress_temp = ETLook.stress.stress_temperature(t_air_24, t_opt, t_min,
                                                   t_max)
    r_canopy_0 = ETLook.resistance.atmospheric_canopy_resistance(
        lai_eff, stress_rad, stress_vpd, stress_temp, rs_min, rcan_max)

    # Save as tiff files
    lat[np.isnan(QC)] = np.nan
    slope[np.isnan(QC)] = np.nan
    aspect[np.isnan(QC)] = np.nan
    ra_24_toa[np.isnan(QC)] = np.nan
    ws[np.isnan(QC)] = np.nan
    ra_24_toa_flat[np.isnan(QC)] = np.nan
    diffusion_index[np.isnan(QC)] = np.nan
    ra_24[np.isnan(QC)] = np.nan
    stress_rad[np.isnan(QC)] = np.nan
    p_air_24[np.isnan(QC)] = np.nan
    vp_24[np.isnan(QC)] = np.nan
    svp_24[np.isnan(QC)] = np.nan
    vpd_24[np.isnan(QC)] = np.nan
    stress_vpd[np.isnan(QC)] = np.nan
    stress_temp[np.isnan(QC)] = np.nan
    r_canopy_0[np.isnan(QC)] = np.nan
    if out.lat == 1:
        PF.Save_as_tiff(lat_filename, lat, geo_ex, proj_ex)
    if out.slope == 1:
        PF.Save_as_tiff(slope_filename, slope, geo_ex, proj_ex)
    if out.aspect == 1:
        PF.Save_as_tiff(aspect_filename, aspect, geo_ex, proj_ex)
    if out.ws == 1:
        PF.Save_as_tiff(ws_filename, ws, geo_ex, proj_ex)
    if out.ra_24_toa == 1:
        PF.Save_as_tiff(ra_24_toa_filename, ra_24_toa, geo_ex, proj_ex)
    if out.diffusion_index == 1:
        PF.Save_as_tiff(diffusion_index_filename, diffusion_index, geo_ex,
                        proj_ex)
    if out.ra_24 == 1:
        PF.Save_as_tiff(ra_24_filename, ra_24, geo_ex, proj_ex)
    if out.stress_rad == 1:
        PF.Save_as_tiff(stress_rad_filename, stress_rad, geo_ex, proj_ex)
    if out.p_air_24 == 1:
        PF.Save_as_tiff(p_air_24_filename, p_air_24, geo_ex, proj_ex)
    if out.vp_24 == 1:
        PF.Save_as_tiff(vp_24_filename, vp_24, geo_ex, proj_ex)
    if out.svp_24 == 1:
        PF.Save_as_tiff(svp_24_filename, svp_24, geo_ex, proj_ex)
    if out.vpd_24 == 1:
        PF.Save_as_tiff(vpd_24_filename, vpd_24, geo_ex, proj_ex)
    if out.stress_vpd == 1:
        PF.Save_as_tiff(stress_vpd_filename, stress_vpd, geo_ex, proj_ex)
    if out.stress_temp == 1:
        PF.Save_as_tiff(stress_temp_filename, stress_temp, geo_ex, proj_ex)
    if out.r_canopy_0 == 1:
        PF.Save_as_tiff(r_canopy_0_filename, r_canopy_0, geo_ex, proj_ex)

    # **net radiation canopy******************************************************
    t_air_k_24 = ETLook.meteo.air_temperature_kelvin_daily(t_air_24)
    # select one of the below two
    #l_net = ETLook.radiation.longwave_radiation_fao_etref(t_air_k_24, vp_24, trans_24)
    l_net = ETLook.radiation.longwave_radiation_fao(t_air_k_24, vp_24,
                                                    trans_24, vp_slope,
                                                    vp_offset, lw_slope,
                                                    lw_offset)
    int_mm = ETLook.evapotranspiration.interception_mm(P_24, vc, lai, int_max)
    lh_24 = ETLook.meteo.latent_heat_daily(t_air_24)
    int_wm2 = ETLook.radiation.interception_wm2(int_mm, lh_24)
    rn_24 = ETLook.radiation.net_radiation(r0, ra_24, l_net, int_wm2)
    rn_24_canopy = ETLook.radiation.net_radiation_canopy(rn_24, sf_soil)

    # Save as tiff files
    t_air_k_24[np.isnan(QC)] = np.nan
    l_net[np.isnan(QC)] = np.nan
    int_mm[np.isnan(QC)] = np.nan
    lh_24[np.isnan(QC)] = np.nan
    int_wm2[np.isnan(QC)] = np.nan
    rn_24[np.isnan(QC)] = np.nan
    rn_24_canopy[np.isnan(QC)] = np.nan
    if out.t_air_k_24 == 1:
        PF.Save_as_tiff(t_air_k_24_filename, t_air_k_24, geo_ex, proj_ex)
    if out.l_net == 1:
        PF.Save_as_tiff(l_net_filename, l_net, geo_ex, proj_ex)
    if out.int_mm == 1:
        PF.Save_as_tiff(int_mm_filename, int_mm, geo_ex, proj_ex)
    if out.lh_24 == 1:
        PF.Save_as_tiff(lh_24_filename, lh_24, geo_ex, proj_ex)
    if out.int_wm2 == 1:
        PF.Save_as_tiff(int_wm2_filename, int_wm2, geo_ex, proj_ex)
    if out.rn_24 == 1:
        PF.Save_as_tiff(rn_24_filename, rn_24, geo_ex, proj_ex)
    if out.rn_24_canopy == 1:
        PF.Save_as_tiff(rn_24_canopy_filename, rn_24_canopy, geo_ex, proj_ex)

    # **canopy resistance***********************************************************

    t_air_k_i = ETLook.meteo.air_temperature_kelvin_inst(t_air_i)
    vp_i = ETLook.meteo.vapour_pressure_from_specific_humidity_inst(
        qv_i, p_air_i)
    ad_moist_i = ETLook.meteo.moist_air_density_inst(vp_i, t_air_k_i)
    ad_dry_i = ETLook.meteo.dry_air_density_inst(p_air_i, vp_i, t_air_k_i)
    ad_i = ETLook.meteo.air_density_inst(ad_dry_i, ad_moist_i)
    u_b_i_bare = ETLook.soil_moisture.wind_speed_blending_height_bare(
        u_i, z0m_bare, z_obs, z_b)
    lon = ETLook.solar_radiation.longitude_rad(lon_deg)
    ha = ETLook.solar_radiation.hour_angle(sc, dtime, lon)
    I0 = ETLook.clear_sky_radiation.solar_constant()
    ied = ETLook.clear_sky_radiation.inverse_earth_sun_distance(day_angle)
    h0 = ETLook.clear_sky_radiation.solar_elevation_angle(lat, decl, ha)
    h0ref = ETLook.clear_sky_radiation.solar_elevation_angle_refracted(h0)
    m = ETLook.clear_sky_radiation.relative_optical_airmass(
        p_air_i, p_air_0_i, h0ref)
    rotm = ETLook.clear_sky_radiation.rayleigh_optical_thickness(m)
    Tl2 = ETLook.clear_sky_radiation.linke_turbidity(wv_i, aod550_i, p_air_i,
                                                     p_air_0_i)
    G0 = ETLook.clear_sky_radiation.extraterrestrial_irradiance_normal(I0, ied)
    B0c = ETLook.clear_sky_radiation.beam_irradiance_normal_clear(
        G0, Tl2, m, rotm, h0)
    Bhc = ETLook.clear_sky_radiation.beam_irradiance_horizontal_clear(B0c, h0)
    Dhc = ETLook.clear_sky_radiation.diffuse_irradiance_horizontal_clear(
        G0, Tl2, h0)
    ra_hor_clear_i = ETLook.clear_sky_radiation.ra_clear_horizontal(Bhc, Dhc)
    emiss_atm_i = ETLook.soil_moisture.atmospheric_emissivity_inst(
        vp_i, t_air_k_i)
    rn_bare = ETLook.soil_moisture.net_radiation_bare(ra_hor_clear_i,
                                                      emiss_atm_i, t_air_k_i,
                                                      lst, r0_bare)
    rn_full = ETLook.soil_moisture.net_radiation_full(ra_hor_clear_i,
                                                      emiss_atm_i, t_air_k_i,
                                                      lst, r0_full)
    h_bare = ETLook.soil_moisture.sensible_heat_flux_bare(
        rn_bare, fraction_h_bare)
    h_full = ETLook.soil_moisture.sensible_heat_flux_full(
        rn_full, fraction_h_full)
    u_b_i_full = ETLook.soil_moisture.wind_speed_blending_height_full_inst(
        u_i, z0m_full, z_obs, z_b)
    u_star_i_bare = ETLook.soil_moisture.friction_velocity_bare_inst(
        u_b_i_bare, z0m_bare, disp_bare, z_b)
    u_star_i_full = ETLook.soil_moisture.friction_velocity_full_inst(
        u_b_i_full, z0m_full, disp_full, z_b)
    L_bare = ETLook.soil_moisture.monin_obukhov_length_bare(
        h_bare, ad_i, u_star_i_bare, t_air_k_i)
    L_full = ETLook.soil_moisture.monin_obukhov_length_full(
        h_full, ad_i, u_star_i_full, t_air_k_i)
    u_i_soil = ETLook.soil_moisture.wind_speed_soil_inst(u_i, L_bare, z_obs)
    ras = ETLook.soil_moisture.aerodynamical_resistance_soil(u_i_soil)
    raa = ETLook.soil_moisture.aerodynamical_resistance_bare(
        u_i, L_bare, z0m_bare, disp_bare, z_obs)
    rac = ETLook.soil_moisture.aerodynamical_resistance_full(
        u_i, L_full, z0m_full, disp_full, z_obs)
    t_max_bare = ETLook.soil_moisture.maximum_temperature_bare(
        ra_hor_clear_i, emiss_atm_i, t_air_k_i, ad_i, raa, ras, r0_bare)
    t_max_full = ETLook.soil_moisture.maximum_temperature_full(
        ra_hor_clear_i, emiss_atm_i, t_air_k_i, ad_i, rac, r0_full)
    w_i = ETLook.soil_moisture.dew_point_temperature_inst(vp_i)
    t_dew_i = ETLook.soil_moisture.dew_point_temperature_inst(vp_i)
    t_wet_i = ETLook.soil_moisture.wet_bulb_temperature_inst(t_air_i, t_dew_i)
    t_wet_k_i = ETLook.meteo.wet_bulb_temperature_kelvin_inst(t_wet_i)
    lst_max = ETLook.soil_moisture.maximum_temperature(t_max_bare, t_max_full,
                                                       vc)
    lst_min = ETLook.soil_moisture.minimum_temperature(t_wet_k_i, t_air_k_i,
                                                       vc)
    se_root = ETLook.soil_moisture.soil_moisture_from_maximum_temperature(
        lst_max, lst, lst_min)
    stress_moist = ETLook.stress.stress_moisture(se_root, tenacity)
    r_canopy_0 = ETLook.resistance.atmospheric_canopy_resistance(
        lai_eff, stress_rad, stress_vpd, stress_temp, rs_min, rcan_max)
    r_canopy = ETLook.resistance.canopy_resistance(r_canopy_0, stress_moist,
                                                   rcan_max)

    # Save as tiff files
    t_air_k_i[np.isnan(QC)] = np.nan
    vp_i[np.isnan(QC)] = np.nan
    ad_moist_i[np.isnan(QC)] = np.nan
    ad_dry_i[np.isnan(QC)] = np.nan
    ad_i[np.isnan(QC)] = np.nan
    u_b_i_bare[np.isnan(QC)] = np.nan
    lon[np.isnan(QC)] = np.nan
    ha[np.isnan(QC)] = np.nan
    h0[np.isnan(QC)] = np.nan
    h0ref[np.isnan(QC)] = np.nan
    m[np.isnan(QC)] = np.nan
    rotm[np.isnan(QC)] = np.nan
    Tl2[np.isnan(QC)] = np.nan
    B0c[np.isnan(QC)] = np.nan
    Bhc[np.isnan(QC)] = np.nan
    Dhc[np.isnan(QC)] = np.nan
    ra_hor_clear_i[np.isnan(QC)] = np.nan
    emiss_atm_i[np.isnan(QC)] = np.nan
    rn_bare[np.isnan(QC)] = np.nan
    rn_full[np.isnan(QC)] = np.nan
    u_b_i_full[np.isnan(QC)] = np.nan
    u_star_i_bare[np.isnan(QC)] = np.nan
    u_star_i_full[np.isnan(QC)] = np.nan
    u_i_soil[np.isnan(QC)] = np.nan
    ras[np.isnan(QC)] = np.nan
    raa[np.isnan(QC)] = np.nan
    rac[np.isnan(QC)] = np.nan
    t_max_bare[np.isnan(QC)] = np.nan
    t_max_full[np.isnan(QC)] = np.nan
    w_i[np.isnan(QC)] = np.nan
    t_dew_i[np.isnan(QC)] = np.nan
    t_wet_i[np.isnan(QC)] = np.nan
    t_wet_k_i[np.isnan(QC)] = np.nan
    lst_max[np.isnan(QC)] = np.nan
    se_root[np.isnan(QC)] = np.nan
    stress_moist[np.isnan(QC)] = np.nan
    r_canopy_0[np.isnan(QC)] = np.nan
    r_canopy[np.isnan(QC)] = np.nan
    if out.t_air_k_i == 1:
        PF.Save_as_tiff(t_air_k_i_filename, t_air_k_i, geo_ex, proj_ex)
    if out.vp_i == 1:
        PF.Save_as_tiff(vp_i_filename, vp_i, geo_ex, proj_ex)
    if out.ad_moist_i == 1:
        PF.Save_as_tiff(ad_moist_i_filename, ad_moist_i, geo_ex, proj_ex)
    if out.ad_dry_i == 1:
        PF.Save_as_tiff(ad_dry_i_filename, ad_dry_i, geo_ex, proj_ex)
    if out.ad_i == 1:
        PF.Save_as_tiff(ad_i_filename, ad_i, geo_ex, proj_ex)
    if out.u_b_i_bare == 1:
        PF.Save_as_tiff(u_b_i_bare_filename, u_b_i_bare, geo_ex, proj_ex)
    if out.lon == 1:
        PF.Save_as_tiff(lon_filename, lon, geo_ex, proj_ex)
    if out.ha == 1:
        PF.Save_as_tiff(ha_filename, ha, geo_ex, proj_ex)
    if out.ied == 1:
        PF.Save_as_tiff(ied_filename, ied, geo_ex, proj_ex)
    if out.h0 == 1:
        PF.Save_as_tiff(h0_filename, h0, geo_ex, proj_ex)
    if out.h0ref == 1:
        PF.Save_as_tiff(h0ref_filename, h0ref, geo_ex, proj_ex)
    if out.m == 1:
        PF.Save_as_tiff(m_filename, m, geo_ex, proj_ex)
    if out.rotm == 1:
        PF.Save_as_tiff(rotm_filename, rotm, geo_ex, proj_ex)
    if out.Tl2 == 1:
        PF.Save_as_tiff(Tl2_filename, Tl2, geo_ex, proj_ex)
    if out.B0c == 1:
        PF.Save_as_tiff(B0c_filename, B0c, geo_ex, proj_ex)
    if out.Bhc == 1:
        PF.Save_as_tiff(Bhc_filename, Bhc, geo_ex, proj_ex)
    if out.Dhc == 1:
        PF.Save_as_tiff(Dhc_filename, Dhc, geo_ex, proj_ex)
    if out.ra_hor_clear_i == 1:
        PF.Save_as_tiff(ra_hor_clear_i_filename, ra_hor_clear_i, geo_ex,
                        proj_ex)
    if out.emiss_atm_i == 1:
        PF.Save_as_tiff(emiss_atm_i_filename, emiss_atm_i, geo_ex, proj_ex)
    if out.rn_bare == 1:
        PF.Save_as_tiff(rn_bare_filename, rn_bare, geo_ex, proj_ex)
    if out.rn_full == 1:
        PF.Save_as_tiff(rn_full_filename, rn_full, geo_ex, proj_ex)
    if out.u_b_i_full == 1:
        PF.Save_as_tiff(u_b_i_full_filename, u_b_i_full, geo_ex, proj_ex)
    if out.u_star_i_bare == 1:
        PF.Save_as_tiff(u_star_i_bare_filename, u_star_i_bare, geo_ex, proj_ex)
    if out.u_star_i_full == 1:
        PF.Save_as_tiff(u_star_i_full_filename, u_star_i_full, geo_ex, proj_ex)
    if out.u_i_soil == 1:
        PF.Save_as_tiff(u_i_soil_filename, u_i_soil, geo_ex, proj_ex)
    if out.ras == 1:
        PF.Save_as_tiff(ras_filename, ras, geo_ex, proj_ex)
    if out.raa == 1:
        PF.Save_as_tiff(raa_filename, raa, geo_ex, proj_ex)
    if out.rac == 1:
        PF.Save_as_tiff(rac_filename, rac, geo_ex, proj_ex)
    if out.t_max_bare == 1:
        PF.Save_as_tiff(t_max_bare_filename, t_max_bare, geo_ex, proj_ex)
    if out.t_max_full == 1:
        PF.Save_as_tiff(t_max_full_filename, t_max_full, geo_ex, proj_ex)
    if out.w_i == 1:
        PF.Save_as_tiff(w_i_filename, w_i, geo_ex, proj_ex)
    if out.t_dew_i == 1:
        PF.Save_as_tiff(t_dew_i_filename, t_dew_i, geo_ex, proj_ex)
    if out.t_wet_i == 1:
        PF.Save_as_tiff(t_wet_i_filename, t_wet_i, geo_ex, proj_ex)
    if out.t_wet_k_i == 1:
        PF.Save_as_tiff(t_wet_k_i_filename, t_wet_k_i, geo_ex, proj_ex)
    if out.lst_max == 1:
        PF.Save_as_tiff(lst_max_filename, lst_max, geo_ex, proj_ex)
    if out.se_root == 1:
        PF.Save_as_tiff(se_root_filename, se_root, geo_ex, proj_ex)
    if out.stress_moist == 1:
        PF.Save_as_tiff(stress_moist_filename, stress_moist, geo_ex, proj_ex)
    if out.r_canopy_0 == 1:
        PF.Save_as_tiff(r_canopy_0_filename, r_canopy_0, geo_ex, proj_ex)
    if out.r_canopy == 1:
        PF.Save_as_tiff(r_canopy_filename, r_canopy, geo_ex, proj_ex)

    # **initial canopy aerodynamic resistance***********************************************************

    z_obst = ETLook.roughness.obstacle_height(ndvi, z_obst_max, ndvi_obs_min,
                                              ndvi_obs_max, obs_fr)
    z_oro = ETLook.roughness.orographic_roughness(
        slope, dem_resolution)  #careful - standard res is set to 250 # !!!
    z0m = ETLook.roughness.roughness_length(lai, z_oro, z_obst, z_obst_max,
                                            land_mask)
    ra_canopy_init = ETLook.neutral.initial_canopy_aerodynamic_resistance(
        u_24, z0m, z_obs)

    # Save as tiff files
    z_obst[np.isnan(QC)] = np.nan
    z_oro[np.isnan(QC)] = np.nan
    z0m[np.isnan(QC)] = np.nan
    ra_canopy_init[np.isnan(QC)] = np.nan
    if out.z_obst == 1:
        PF.Save_as_tiff(z_obst_filename, z_obst, geo_ex, proj_ex)
    if out.z_oro == 1:
        PF.Save_as_tiff(z_oro_filename, z_oro, geo_ex, proj_ex)
    if out.z0m == 1:
        PF.Save_as_tiff(z0m_filename, z0m, geo_ex, proj_ex)
    if out.ra_canopy_init == 1:
        PF.Save_as_tiff(ra_canopy_init_filename, ra_canopy_init, geo_ex,
                        proj_ex)

    # **windspeed blending height daily***********************************************************
    u_b_24 = ETLook.meteo.wind_speed_blending_height_daily(u_24, z_obs, z_b)

    # Save as tiff files
    u_b_24[np.isnan(QC)] = np.nan
    if out.u_b_24 == 1:
        PF.Save_as_tiff(u_b_24_filename, u_b_24, geo_ex, proj_ex)

    # **ETLook.unstable.initial_friction_velocity_daily***********************************************************
    disp = ETLook.roughness.displacement_height(lai, z_obst, land_mask, c1)
    u_star_24_init = ETLook.unstable.initial_friction_velocity_daily(
        u_b_24, z0m, disp, z_b)

    # Save as tiff files
    disp[np.isnan(QC)] = np.nan
    u_star_24_init[np.isnan(QC)] = np.nan
    if out.disp == 1:
        PF.Save_as_tiff(disp_filename, disp, geo_ex, proj_ex)
    if out.u_star_24_init == 1:
        PF.Save_as_tiff(u_star_24_init_filename, u_star_24_init, geo_ex,
                        proj_ex)

    # **ETLook.neutral.initial_daily_transpiration***********************************************************
    ad_dry_24 = ETLook.meteo.dry_air_density_daily(p_air_24, vp_24, t_air_k_24)
    ad_moist_24 = ETLook.meteo.moist_air_density_daily(vp_24, t_air_k_24)
    ad_24 = ETLook.meteo.air_density_daily(ad_dry_24, ad_moist_24)
    psy_24 = ETLook.meteo.psychrometric_constant_daily(p_air_24, lh_24)
    ssvp_24 = ETLook.meteo.slope_saturated_vapour_pressure_daily(t_air_24)
    t_24_init = ETLook.neutral.initial_daily_transpiration(
        rn_24_canopy, ssvp_24, ad_24, vpd_24, psy_24, r_canopy, ra_canopy_init)

    # Save as tiff files
    ad_dry_24[np.isnan(QC)] = np.nan
    ad_moist_24[np.isnan(QC)] = np.nan
    ad_24[np.isnan(QC)] = np.nan
    psy_24[np.isnan(QC)] = np.nan
    ssvp_24[np.isnan(QC)] = np.nan
    t_24_init[np.isnan(QC)] = np.nan
    if out.ad_dry_24 == 1:
        PF.Save_as_tiff(ad_dry_24_filename, ad_dry_24, geo_ex, proj_ex)
    if out.ad_moist_24 == 1:
        PF.Save_as_tiff(ad_moist_24_filename, ad_moist_24, geo_ex, proj_ex)
    if out.ad_24 == 1:
        PF.Save_as_tiff(ad_24_filename, ad_24, geo_ex, proj_ex)
    if out.psy_24 == 1:
        PF.Save_as_tiff(psy_24_filename, psy_24, geo_ex, proj_ex)
    if out.ssvp_24 == 1:
        PF.Save_as_tiff(ssvp_24_filename, ssvp_24, geo_ex, proj_ex)
    if out.t_24_init == 1:
        PF.Save_as_tiff(t_24_init_filename, t_24_init, geo_ex, proj_ex)

    # **ETLook.unstable.initial_sensible_heat_flux_canopy_daily***********************************************************
    h_canopy_24_init = ETLook.unstable.initial_sensible_heat_flux_canopy_daily(
        rn_24_canopy, t_24_init)

    # Save as tiff files
    h_canopy_24_init[np.isnan(QC)] = np.nan
    if out.h_canopy_24_init == 1:
        PF.Save_as_tiff(h_canopy_24_init_filename, h_canopy_24_init, geo_ex,
                        proj_ex)

    # **ETLook.unstable.transpiration***********************************************************

    t_24 = ETLook.unstable.transpiration(rn_24_canopy, ssvp_24, ad_24, vpd_24,
                                         psy_24, r_canopy, h_canopy_24_init,
                                         t_air_k_24, u_star_24_init, z0m, disp,
                                         u_b_24, z_obs, z_b, iter_h)
    t_24_mm = ETLook.unstable.transpiration_mm(t_24, lh_24)

    # Save as tiff files
    t_24[np.isnan(QC)] = np.nan
    t_24_mm[np.isnan(QC)] = np.nan
    if out.t_24 == 1:
        PF.Save_as_tiff(t_24_filename, t_24, geo_ex, proj_ex)
    if out.t_24_mm == 1:
        PF.Save_as_tiff(t_24_mm_filename, t_24_mm, geo_ex, proj_ex)

    #*******EVAPORATION COMPONENT****************************************************************

    # **ETLook.radiation.net_radiation_soil***********************************************************
    sf_soil = ETLook.radiation.soil_fraction(lai)
    rn_24_soil = ETLook.radiation.net_radiation_soil(rn_24, sf_soil)

    # Save as tiff files
    sf_soil[np.isnan(QC)] = np.nan
    rn_24_soil[np.isnan(QC)] = np.nan
    if out.sf_soil == 1:
        PF.Save_as_tiff(sf_soil_filename, sf_soil, geo_ex, proj_ex)
    if out.rn_24_soil == 1:
        PF.Save_as_tiff(rn_24_soil_filename, rn_24_soil, geo_ex, proj_ex)

    # **ETLook.resistance.soil_resistance***********************************************************

    r_soil = ETLook.resistance.soil_resistance(se_top, land_mask, r_soil_pow,
                                               r_soil_min)

    # Save as tiff files
    r_soil[np.isnan(QC)] = np.nan
    if out.r_soil == 1:
        PF.Save_as_tiff(r_soil_filename, r_soil, geo_ex, proj_ex)

    # **ETLook.resistance.soil_resistance***********************************************************

    ra_soil_init = ETLook.neutral.initial_soil_aerodynamic_resistance(
        u_24, z_obs)

    # Save as tiff files
    ra_soil_init[np.isnan(QC)] = np.nan
    if out.ra_soil_init == 1:
        PF.Save_as_tiff(ra_soil_init_filename, ra_soil_init, geo_ex, proj_ex)

    # **ETLook.meteo.wind_speed_blending_height_daily***********************************************************

    u_b_24 = ETLook.meteo.wind_speed_blending_height_daily(u_24, z_obs, z_b)

    # Save as tiff files
    u_b_24[np.isnan(QC)] = np.nan
    if out.u_b_24 == 1:
        PF.Save_as_tiff(u_b_24_filename, u_b_24, geo_ex, proj_ex)

    # **ETLook.unstable.initial_friction_velocity_soil_daily***********************************************************

    u_star_24_soil_init = ETLook.unstable.initial_friction_velocity_soil_daily(
        u_b_24, disp, z_b)

    # Save as tiff files
    u_star_24_soil_init[np.isnan(QC)] = np.nan
    if out.u_star_24_soil_init == 1:
        PF.Save_as_tiff(u_star_24_soil_init_filename, u_star_24_soil_init,
                        geo_ex, proj_ex)

    # **ETLook.unstable.initial_sensible_heat_flux_soil_daily***********************************************************

    stc = ETLook.radiation.soil_thermal_conductivity(se_top)
    vhc = ETLook.radiation.volumetric_heat_capacity(se_top, porosity)
    dd = ETLook.radiation.damping_depth(stc, vhc)
    g0_bs = ETLook.radiation.bare_soil_heat_flux(doy, dd, stc, t_amp_year, lat)
    g0_24 = ETLook.radiation.soil_heat_flux(g0_bs, sf_soil, land_mask,
                                            rn_24_soil, trans_24, ra_24, l_net,
                                            rn_slope, rn_offset)
    e_24_init = ETLook.neutral.initial_daily_evaporation(
        rn_24_soil, g0_24, ssvp_24, ad_24, vpd_24, psy_24, r_soil,
        ra_soil_init)
    h_soil_24_init = ETLook.unstable.initial_sensible_heat_flux_soil_daily(
        rn_24_soil, e_24_init, g0_24)

    # Save as tiff files
    g0_bs[np.isnan(QC)] = np.nan
    g0_24[np.isnan(QC)] = np.nan
    e_24_init[np.isnan(QC)] = np.nan
    h_soil_24_init[np.isnan(QC)] = np.nan
    if out.g0_bs == 1:
        PF.Save_as_tiff(g0_bs_filename, g0_bs, geo_ex, proj_ex)
    if out.g0_24 == 1:
        PF.Save_as_tiff(g0_24_filename, g0_24, geo_ex, proj_ex)
    if out.e_24_init == 1:
        PF.Save_as_tiff(e_24_init_filename, e_24_init, geo_ex, proj_ex)
    if out.h_soil_24_init == 1:
        PF.Save_as_tiff(h_soil_24_init_filename, h_soil_24_init, geo_ex,
                        proj_ex)

    # **ETLook.unstable.evaporation***********************************************************

    e_24 = ETLook.unstable.evaporation(rn_24_soil, g0_24, ssvp_24, ad_24,
                                       vpd_24, psy_24, r_soil, h_soil_24_init,
                                       t_air_k_24, u_star_24_soil_init, disp,
                                       u_b_24, z_b, z_obs, iter_h)
    e_24_mm = ETLook.unstable.evaporation_mm(e_24, lh_24)
    et_24_mm = ETLook.evapotranspiration.et_actual_mm(e_24_mm, t_24_mm)

    # Save as tiff files
    e_24[np.isnan(QC)] = np.nan
    e_24_mm[np.isnan(QC)] = np.nan
    et_24_mm[np.isnan(QC)] = np.nan
    if out.e_24 == 1:
        PF.Save_as_tiff(e_24_filename, e_24, geo_ex, proj_ex)
    if out.e_24_mm == 1:
        PF.Save_as_tiff(e_24_mm_filename, e_24_mm, geo_ex, proj_ex)
    if out.et_24_mm == 1:
        PF.Save_as_tiff(et_24_mm_filename, et_24_mm, geo_ex, proj_ex)

    # **ETLook.unstable.evaporation***********************************************************
    rn_24_grass = ETLook.radiation.net_radiation_grass(ra_24, l_net, r0_grass)
    et_ref_24 = ETLook.evapotranspiration.et_reference(rn_24_grass, ad_24,
                                                       psy_24, vpd_24, ssvp_24,
                                                       u_24)
    et_ref_24_mm = ETLook.evapotranspiration.et_reference_mm(et_ref_24, lh_24)

    # Save as tiff files
    rn_24_grass[np.isnan(QC)] = np.nan
    et_ref_24[np.isnan(QC)] = np.nan
    et_ref_24_mm[np.isnan(QC)] = np.nan
    if out.rn_24_grass == 1:
        PF.Save_as_tiff(rn_24_grass_filename, rn_24_grass, geo_ex, proj_ex)
    if out.et_ref_24 == 1:
        PF.Save_as_tiff(et_ref_24_filename, et_ref_24, geo_ex, proj_ex)
    if out.et_ref_24_mm == 1:
        PF.Save_as_tiff(et_ref_24_mm_filename, et_ref_24_mm, geo_ex, proj_ex)
    return ()
コード例 #11
0
def DownloadData(Dir, Var, Startdate, Enddate, latlim, lonlim, TimeStep,
                 Period, Waitbar):

    # WAPOR modules
    import pyWAPOR.Functions.Processing_Functions as PF

    # Check the latitude and longitude and otherwise set lat or lon on greatest extent
    if latlim[0] < -90 or latlim[1] > 90:
        print(
            'Latitude above 90N or below 90S is not possible. Value set to maximum'
        )
        latlim[0] = np.max(latlim[0], -90)
        latlim[1] = np.min(latlim[1], 90)
    if lonlim[0] < -180 or lonlim[1] > 180:
        print(
            'Longitude must be between 180E and 180W. Now value is set to maximum'
        )
        lonlim[0] = np.max(lonlim[0], -180)
        lonlim[1] = np.min(lonlim[1], 180)

    # Get information of the parameter
    VarInfo = VariablesInfo(TimeStep)
    Parameter = VarInfo.names[Var]
    unit = VarInfo.units[Var]
    types = VarInfo.types[Var]

    # Create output folder
    output_folder = os.path.join(Dir, "GEOS", Parameter, TimeStep)
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # Define IDs
    IDx = [
        np.floor((lonlim[0] + 180) / 0.3125),
        np.ceil((lonlim[1] + 180) / 0.3125)
    ]
    IDy = [np.floor((latlim[0] + 90) / 0.25), np.ceil((latlim[1] + 90) / 0.25)]

    # Create output geo transform
    Xstart = -180 + 0.3125 * IDx[0]
    Ystart = -90 + 0.25 * IDy[1]
    geo_out = tuple([Xstart, 0.3125, 0, Ystart, 0, -0.25])
    proj = "WGS84"

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

    # Create Waitbar
    if Waitbar == 1:
        import pyWAPOR.Functions.WaitbarConsole as WaitbarConsole
        total_amount = len(Dates)
        amount = 0
        WaitbarConsole.printWaitBar(amount,
                                    total_amount,
                                    prefix='Progress:',
                                    suffix='Complete',
                                    length=50)

    for Date in Dates:

        # Define the IDz
        if TimeStep == "three_hourly":
            IDz_start = IDz_end = int(
                ((Date - pd.Timestamp("2017-12-01")).days) * 8) + (Period - 1)
            Hour = int((Period - 1) * 3)
            output_name = os.path.join(
                output_folder,
                "%s_GEOS_%s_3-hourly_%d.%02d.%02d_H%02d.M00.tif" %
                (Var, unit, Date.year, Date.month, Date.day, Hour))

        if TimeStep == "daily":
            IDz_start = int(((Date - pd.Timestamp("2017-12-01")).days) * 8)
            IDz_end = IDz_start + 7
            output_name = os.path.join(
                output_folder, "%s_GEOS_%s_daily_%d.%02d.%02d.tif" %
                (Var, unit, Date.year, Date.month, Date.day))

        if not os.path.exists(output_name):

            # define total url
            url_start = r"https://opendap.nccs.nasa.gov/dods/GEOS-5/fp/0.25_deg/assim/inst3_2d_asm_Nx."
            url_GEOS = url_start + 'ascii?%s[%s:1:%s][%s:1:%s][%s:1:%s]' % (
                Var, IDz_start, IDz_end, int(IDy[0]), int(IDy[1]), int(
                    IDx[0]), int(IDx[1]))

            # Reset the begin parameters for downloading
            downloaded = 0
            N = 0

            # if not downloaded try to download file
            while downloaded == 0:
                try:

                    # download data (first save as text file)
                    pathtext = os.path.join(output_folder,
                                            'temp%s.txt' % str(IDz_start))

                    # Download the data
                    urllib.request.urlretrieve(url_GEOS, filename=pathtext)

                    # Reshape data
                    datashape = [
                        int(IDy[1] - IDy[0] + 1),
                        int(IDx[1] - IDx[0] + 1)
                    ]
                    data_start = np.genfromtxt(pathtext,
                                               dtype=float,
                                               skip_header=1,
                                               skip_footer=6,
                                               delimiter=',')
                    data_list = np.asarray(data_start[:, 1:])
                    if TimeStep == "daily":
                        data_end = np.resize(data_list,
                                             (8, datashape[0], datashape[1]))
                    if TimeStep == "three_hourly":
                        data_end = np.resize(data_list,
                                             (datashape[0], datashape[1]))
                    os.remove(pathtext)

                    # Set no data value
                    data_end[data_end > 1000000] = -9999

                    if TimeStep == "daily":
                        if Var == "t2m":
                            data_end_max = np.nanmax(data_end, 0)
                            data_end_min = np.nanmin(data_end, 0)
                        if types == "state":
                            data_end = np.nanmean(data_end, 0)
                        else:
                            data_end = np.nansum(data_end, 0)

                    # Add the VarFactor
                    if VarInfo.factors[Var] < 0:
                        data_end[data_end != -9999] = data_end[
                            data_end != -9999] + VarInfo.factors[Var]
                    else:
                        data_end[data_end != -9999] = data_end[
                            data_end != -9999] * VarInfo.factors[Var]
                    data_end[data_end < -9999] = -9999

                    # Download was succesfull
                    downloaded = 1

                    # twist the data
                    data_end = np.flipud(data_end)

                    # Save as tiff file
                    PF.Save_as_tiff(output_name, data_end, geo_out, proj)

                    if TimeStep == "daily" and Var == "t2m":
                        # Add the VarFactor
                        if VarInfo.factors[Var] < 0:
                            data_end_max[data_end != -9999] = data_end_max[
                                data_end != -9999] + VarInfo.factors[Var]
                            data_end_min[data_end != -9999] = data_end_min[
                                data_end != -9999] + VarInfo.factors[Var]
                        else:
                            data_end_max[data_end != -9999] = data_end_max[
                                data_end != -9999] * VarInfo.factors[Var]
                            data_end_min[data_end != -9999] = data_end_min[
                                data_end != -9999] * VarInfo.factors[Var]
                        data_end_max[data_end < -9999] = -9999
                        data_end_min[data_end < -9999] = -9999

                        # twist the data
                        data_end_min = np.flipud(data_end_min)
                        data_end_max = np.flipud(data_end_max)

                        # Save as tiff file
                        output_name = os.path.join(
                            output_folder,
                            "%s_GEOS_%s_daily_max_%d.%02d.%02d.tif" %
                            (Var, unit, Date.year, Date.month, Date.day))
                        PF.Save_as_tiff(output_name, data_end_max, geo_out,
                                        proj)
                        output_name = os.path.join(
                            output_folder,
                            "%s_GEOS_%s_daily_min_%d.%02d.%02d.tif" %
                            (Var, unit, Date.year, Date.month, Date.day))
                        PF.Save_as_tiff(output_name, data_end_min, geo_out,
                                        proj)

                # If download was not succesfull
                except:

                    # Try another time
                    N = N + 1

                    # Stop trying after 10 times
                    if N == 10:
                        print('Data from ' + Date.strftime('%Y-%m-%d') +
                              ' is not available')
                        downloaded = 1

        if Waitbar == 1:
            amount += 1
            WaitbarConsole.printWaitBar(amount,
                                        total_amount,
                                        prefix='Progress:',
                                        suffix='Complete',
                                        length=50)

    return ()