Esempio n. 1
0
def Calculate(Dir_Home, Basin, LU_file_name, P_Product, ET_Product,
              LAI_Product, NDM_Product, Startdate, Enddate, Simulation):
    """
    This functions is the main framework for calculating sheet 2.

    Parameters
    ----------
    Basin : str
        Name of the basin
    P_Product : str
        Name of the rainfall product that will be used
    ET_Product : str
        Name of the evapotranspiration product that will be used
    LAI_Product : str
        Name of the LAI product that will be used
    NDM_Product : str
        Name of the NDM product that will be used
    Startdate : str
        Contains the start date of the model 'yyyy-mm-dd'
    Enddate : str
        Contains the end date of the model 'yyyy-mm-dd'
    Simulation : int
        Defines the simulation

    """
    ######################### Import WA modules ###################################

    # import general python modules
    import os
    from netCDF4 import Dataset

    from watools.General import raster_conversions as RC
    from watools.General import data_conversions as DC
    import watools.Functions.Two as Two
    import watools.Functions.Start as Start
    import watools.Generator.Sheet2 as Generate

    ######################### Set General Parameters ##############################

    # Get the boundaries of the basin based on the shapefile of the watershed
    # Boundaries, Shape_file_name_shp = Start.Boundaries.Determine(Basin)
    Boundaries, Example_dataset = Start.Boundaries.Determine_LU_Based(
        LU_file_name, Dir_Home)

    ############## Cut dates into pieces if it is needed ######################

    # Check the years that needs to be calculated
    years = list(
        range(int(Startdate.split('-')[0]),
              int(Enddate.split('-')[0]) + 1))

    for year in years:

        # Create Start and End date for time chunk
        Startdate_part = '%d-01-01' % int(year)
        Enddate_part = '%s-12-31' % int(year)

        # Create Directory
        Dir_Basin = os.path.join(Dir_Home, Basin)
        output_dir = os.path.join(Dir_Basin, "Simulations",
                                  "Simulation_%d" % Simulation)
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        # Create .nc file if not exists
        nc_outname = os.path.join(output_dir, "%d.nc" % year)
        if not os.path.exists(nc_outname):
            DC.Create_new_NC_file(nc_outname, Example_dataset, Basin)

        ############################# Download Data ###################################
        # Open variables in netcdf
        fh = Dataset(nc_outname)
        Variables_NC = [var for var in fh.variables]
        fh.close()

        if not "Precipitation" in Variables_NC:
            Data_Path_P_Monthly = Start.Download_Data.Precipitation(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, P_Product)

        if not "Rainy_Days" in Variables_NC:
            Data_Path_P_Daily = Start.Download_Data.Precipitation_Daily(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, P_Product)

        if not "Actual_Evapotranspiration" in Variables_NC:
            Data_Path_ET = Start.Download_Data.Evapotranspiration(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, ET_Product)

        if not "LAI" in Variables_NC:
            Data_Path_LAI = Start.Download_Data.LAI(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, LAI_Product)

        if not "Normalized_Dry_Matter" in Variables_NC:
            Data_Path_NPP = Start.Download_Data.NPP(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, NDM_Product)
            Data_Path_GPP = Start.Download_Data.GPP(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, NDM_Product)

        ########################### Create input data #################################

        if not "Rainy_Days" in Variables_NC:

            # Create Rainy Days based on daily CHIRPS
            Data_Path_RD = Two.Rainy_Days.Calc_Rainy_Days(
                Dir_Basin, Data_Path_P_Daily, Startdate_part, Enddate_part)

        if not "LAI" in Variables_NC:

            # Create monthly LAI
            Start.Eightdaily_to_monthly_state.Nearest_Interpolate(
                Data_Path_LAI, Startdate_part, Enddate_part)

        if not "Normalized_Dry_Matter" in Variables_NC:

            # Create NDM based on MOD17
            if NDM_Product == 'MOD17':

                # Create monthly GPP
                Start.Eightdaily_to_monthly_state.Nearest_Interpolate(
                    Data_Path_GPP, Startdate_part, Enddate_part)
                Data_Path_NDM = Two.Calc_NDM.NPP_GPP_Based(
                    Dir_Basin, Data_Path_GPP, Data_Path_NPP, Startdate_part,
                    Enddate_part)

        ###################### Save Data as netCDF files ##############################

        #______________________________Precipitation_______________________________

        # 1.) Precipitation data
        if not "Precipitation" in Variables_NC:
            # Get the data of Precipitation and save as nc
            DataCube_Prec = RC.Get3Darray_time_series_monthly(
                Data_Path_P_Monthly,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Prec,
                                     "Precipitation", "mm/month", 0.01)
            del DataCube_Prec

        #_______________________________Evaporation________________________________

        # 2.) Evapotranspiration data
        if not "Actual_Evapotranspiration" in Variables_NC:
            # Get the data of Evaporation and save as nc
            DataCube_ET = RC.Get3Darray_time_series_monthly(
                Data_Path_ET,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ET,
                                     "Actual_Evapotranspiration", "mm/month",
                                     0.01)
            del DataCube_ET

        #___________________________Normalized Dry Matter__________________________

        # 3.) Normalized Dry Matter
        if not "Normalized_Dry_Matter" in Variables_NC:
            # Get the data of Evaporation and save as nc
            DataCube_NDM = RC.Get3Darray_time_series_monthly(
                Data_Path_NDM,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_NDM,
                                     "Normalized_Dry_Matter", "kg_ha", 0.01)
            del DataCube_NDM

        #_______________________________Rainy Days_________________________________

        if not "Rainy_Days" in Variables_NC:
            # Get the data of rainy days and save as nc
            DataCube_RD = RC.Get3Darray_time_series_monthly(
                Data_Path_RD,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_RD, "Rainy_Days",
                                     "amount_of_days", 0.01)
            del DataCube_RD

        #_______________________________Leaf Area Index____________________________

        if not "LAI" in Variables_NC:
            # Get the data of leave area index and save as nc
            DataCube_LAI = RC.Get3Darray_time_series_monthly(
                Data_Path_LAI,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_LAI, "LAI", "m2-m-2",
                                     0.01)
            del DataCube_LAI

        ####################### Calculations Sheet 2 ##########################
        if not ("Interception" in Variables_NC or "Transpiration"
                in Variables_NC or "Evaporation" in Variables_NC):
            DataCube_I, DataCube_T, DataCube_E = Two.SplitET.ITE(
                Dir_Basin, nc_outname, Startdate_part, Enddate_part,
                Simulation)

            DC.Add_NC_Array_Variable(nc_outname, DataCube_I, "Interception",
                                     "mm/month", 0.01)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_T, "Transpiration",
                                     "mm/month", 0.01)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_E, "Evaporation",
                                     "mm/month", 0.01)
            del DataCube_I, DataCube_T, DataCube_E

        ######################### Create CSV 2 ################################

        Dir_Basin_CSV = Generate.CSV.Create(Dir_Basin, Simulation, Basin,
                                            Startdate_part, Enddate_part,
                                            nc_outname, Example_dataset)

    ############################ Create Sheet 2 ###############################

    Generate.PDF.Create(Dir_Basin, Basin, Simulation, Dir_Basin_CSV)

    return ()
Esempio n. 2
0
def Calculate(WA_HOME_folder, Basin, P_Product, ET_Product, ETref_Product,
              DEM_Product, Water_Occurence_Product, Inflow_Text_Files,
              WaterPIX_filename, Reservoirs_GEE_on_off, Supply_method,
              Startdate, Enddate, Simulation):
    '''
    This functions consists of the following sections:
    1. Set General Parameters
    2. Download Data
    3. Convert the RAW data to NETCDF files
    4. Run SurfWAT

    '''
    # import General modules
    import os
    import gdal
    import numpy as np
    import pandas as pd
    from netCDF4 import Dataset

    # import WA plus modules
    from watools.General import raster_conversions as RC
    from watools.General import data_conversions as DC
    import watools.Functions.Five as Five
    import watools.Functions.Start as Start
    import watools.Functions.Start.Get_Dictionaries as GD

    ######################### 1. Set General Parameters ##############################

    # Get environmental variable for the Home folder
    if WA_HOME_folder == '':
        WA_env_paths = os.environ["WA_HOME"].split(';')
        Dir_Home = WA_env_paths[0]
    else:
        Dir_Home = WA_HOME_folder

    # Create the Basin folder
    Dir_Basin = os.path.join(Dir_Home, Basin)
    output_dir = os.path.join(Dir_Basin, "Simulations",
                              "Simulation_%d" % Simulation)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # Get the boundaries of the basin based on the shapefile of the watershed
    # Boundaries, Shape_file_name_shp = Start.Boundaries.Determine(Basin)
    Boundaries, Example_dataset = Start.Boundaries.Determine_LU_Based(
        Basin, Dir_Home)
    geo_out, proj, size_X, size_Y = RC.Open_array_info(Example_dataset)

    # Define resolution of SRTM
    Resolution = '15s'

    # Find the maximum moving window value
    ET_Blue_Green_Classes_dict, Moving_Window_Per_Class_dict = GD.get_bluegreen_classes(
        version='1.0')
    Additional_Months_tail = np.max(list(
        Moving_Window_Per_Class_dict.values()))

    ############## Cut dates into pieces if it is needed ######################

    # Check the years that needs to be calculated
    years = list(
        range(int(Startdate.split('-')[0]),
              int(Enddate.split('-')[0]) + 1))

    for year in years:

        # Create .nc file if not exists
        nc_outname = os.path.join(output_dir, "%d.nc" % year)
        if not os.path.exists(nc_outname):
            DC.Create_new_NC_file(nc_outname, Example_dataset, Basin)

        # Open variables in netcdf
        fh = Dataset(nc_outname)
        Variables_NC = [var for var in fh.variables]
        fh.close()

        # Create Start and End date for time chunk
        Startdate_part = '%d-01-01' % int(year)
        Enddate_part = '%s-12-31' % int(year)

        if int(year) == int(years[0]):
            Startdate_Moving_Average = pd.Timestamp(Startdate) - pd.DateOffset(
                months=Additional_Months_tail)
            Startdate_Moving_Average_String = Startdate_Moving_Average.strftime(
                '%Y-%m-%d')
        else:
            Startdate_Moving_Average_String = Startdate_part

        ############################# 2. Download Data ###################################

        # Download data
        if not "Precipitation" in Variables_NC:
            Data_Path_P_Monthly = Start.Download_Data.Precipitation(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, P_Product)

        if not "Actual_Evapotranspiration" in Variables_NC:
            Data_Path_ET = Start.Download_Data.Evapotranspiration(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, ET_Product)

        if (WaterPIX_filename == "" or Supply_method == "Fraction") \
                and not ("Reference_Evapotranspiration" in Variables_NC):

            Data_Path_ETref = Start.Download_Data.ETreference(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']],
                Startdate_Moving_Average_String, Enddate_part, ETref_Product)

        if Reservoirs_GEE_on_off == 1 and not ("Water_Occurrence"
                                               in Variables_NC):
            Data_Path_JRC_occurrence = Start.Download_Data.JRC_occurrence(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']],
                Water_Occurence_Product)

            input_JRC = os.path.join(Data_Path_JRC_occurrence,
                                     "JRC_Occurrence_percent.tif")

        else:
            input_JRC = None

        # WaterPIX input
        Data_Path_DEM_Dir = Start.Download_Data.DEM_Dir(
            Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
            [Boundaries['Lonmin'], Boundaries['Lonmax']], Resolution,
            DEM_Product)

        Data_Path_DEM = Start.Download_Data.DEM(
            Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
            [Boundaries['Lonmin'], Boundaries['Lonmax']], Resolution,
            DEM_Product)

        ###################### 3. Convert the RAW data to NETCDF files ##############################
        # The sequence of converting the data into netcdf is:
        # Precipitation
        # Evapotranspiration
        # Reference Evapotranspiration
        # DEM flow directions

        #______________________________Precipitation_______________________________

        # 1.) Precipitation data
        if not "Precipitation" in Variables_NC:
            # Get the data of Precipitation and save as nc
            DataCube_Prec = RC.Get3Darray_time_series_monthly(
                Data_Path_P_Monthly,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)

            DC.Add_NC_Array_Variable(nc_outname, DataCube_Prec,
                                     "Precipitation", "mm/month", 0.01)
            del DataCube_Prec

        #_______________________________Evaporation________________________________

        # 2.) Evapotranspiration data
        if not "Actual_Evapotranspiration" in Variables_NC:
            # Get the data of Evaporation and save as nc
            DataCube_ET = RC.Get3Darray_time_series_monthly(
                Data_Path_ET,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ET,
                                     "Actual_Evapotranspiration", "mm/month",
                                     0.01)
            del DataCube_ET

        #_______________________Reference Evaporation______________________________

        # 3.) Reference Evapotranspiration data
        if (WaterPIX_filename == "" or Supply_method == "Fraction") and not \
                ("Reference_Evapotranspiration" in Variables_NC):
            # Get the data of Precipitation and save as nc
            DataCube_ETref = RC.Get3Darray_time_series_monthly(
                Data_Path_ETref,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ETref,
                                     "Reference_Evapotranspiration",
                                     "mm/month", 0.01)
            del DataCube_ETref

        #____________________________fraction surface water _______________________

        if not "Fraction_Surface_Water_Supply" in Variables_NC:
            DataCube_frac_sw = np.ones([size_Y, size_X]) * np.nan

            import watools.Functions.Start.Get_Dictionaries as GD

            # Open LU dataset
            DataCube_LU = RC.Open_nc_array(nc_outname, "Landuse")

            # Get dictionaries and keys
            lulc = GD.get_sheet5_classes()
            lulc_dict = list(GD.get_sheet5_classes().keys())
            consumed_frac_dict = GD.sw_supply_fractions()

            for key in lulc_dict:
                Numbers = lulc[key]
                for LU_nmbr in Numbers:
                    DataCube_frac_sw[DataCube_LU ==
                                     LU_nmbr] = consumed_frac_dict[key]

            DC.Add_NC_Array_Static(nc_outname, DataCube_frac_sw,
                                   "Fraction_Surface_Water_Supply", "fraction",
                                   0.01)
            del DataCube_frac_sw, DataCube_LU

        ################### 4. Calculate Runoff (2 methods: a = Budyko and b = WaterPIX) #####################

        ################ 4a. Calculate Runoff based on Precipitation and Evapotranspiration ##################

        if (Supply_method == "Fraction"
                and not "Surface_Runoff" in Variables_NC):

            # Calculate runoff based on Budyko
            DataCube_Runoff = Five.Fraction_Based.Calc_surface_runoff(
                Dir_Basin, nc_outname, Startdate_part, Enddate_part,
                Example_dataset, ETref_Product, P_Product)

            # Save the runoff as netcdf
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Runoff,
                                     "Surface_Runoff", "mm/month", 0.01)
            del DataCube_Runoff

        ###################### 4b. Get Runoff from WaterPIX ###########################
        if (Supply_method == "WaterPIX"
                and not "Surface_Runoff" in Variables_NC):

            # Get WaterPIX data
            WaterPIX_Var = 'TotalRunoff_M'
            DataCube_Runoff = Five.Read_WaterPIX.Get_Array(
                WaterPIX_filename, WaterPIX_Var, Example_dataset,
                Startdate_part, Enddate_part)

            # Save the runoff as netcdf
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Runoff,
                                     "Surface_Runoff", "mm/month", 0.01)
            del DataCube_Runoff

        ####################### 5. Calculate Extraction (2 methods: a = Fraction, b = WaterPIX) ##################

        ###################### 5a. Get extraction from fraction method by using budyko ###########################
        if (Supply_method == "Fraction"
                and not "Surface_Withdrawal" in Variables_NC):
            DataCube_surface_withdrawal = Five.Fraction_Based.Calc_surface_withdrawal(
                Dir_Basin, nc_outname, Startdate_part, Enddate_part,
                Example_dataset, ETref_Product, P_Product)

            # Save the runoff as netcdf
            DC.Add_NC_Array_Variable(nc_outname, DataCube_surface_withdrawal,
                                     "Surface_Withdrawal", "mm/month", 0.01)
            del DataCube_surface_withdrawal

        #################################### 5b. Get extraction from WaterPIX ####################################
        if (Supply_method == "WaterPIX"
                and not "Surface_Withdrawal" in Variables_NC):
            WaterPIX_Var = 'Supply_M'
            DataCube_Supply = Five.Read_WaterPIX.Get_Array(
                WaterPIX_filename, WaterPIX_Var, Example_dataset, Startdate,
                Enddate)

            # Open array with surface water fractions
            DataCube_frac_sw = RC.Open_nc_array(
                nc_outname, "Fraction_Surface_Water_Supply")

            # Total amount of ETblue taken out of rivers
            DataCube_surface_withdrawal = DataCube_Supply * DataCube_frac_sw[
                None, :, :]

            # Save the runoff as netcdf
            DC.Add_NC_Array_Variable(nc_outname, DataCube_surface_withdrawal,
                                     "Surface_Withdrawal", "mm/month", 0.01)
            del DataCube_surface_withdrawal

        ################################## 5. Run SurfWAT #####################################

        import watools.Models.SurfWAT as SurfWAT

        # Define formats of input data
        Format_DEM = "TIFF"  # or "TIFF"
        Format_Runoff = "NetCDF"  # or "TIFF"
        Format_Extraction = "NetCDF"  # or "TIFF"
        Format_DEM_dir = "TIFF"  # or "TIFF"
        Format_Basin = "NetCDF"  # or "TIFF"

        # Give path (for tiff) or file (netcdf)
        input_nc = os.path.join(Dir_Basin, "Simulations",
                                "Simulation_%s" % Simulation,
                                "SurfWAT_in_%d.nc" % year)
        output_nc = os.path.join(Dir_Basin, "Simulations",
                                 "Simulation_%s" % Simulation,
                                 "SurfWAT_out_%d.nc" % year)

        # Create Input File for SurfWAT
        SurfWAT.Create_input_nc.main(Data_Path_DEM_Dir, Data_Path_DEM,
                                     os.path.dirname(nc_outname),
                                     os.path.dirname(nc_outname),
                                     os.path.dirname(nc_outname), Startdate,
                                     Enddate, input_nc, Resolution,
                                     Format_DEM_dir, Format_DEM, Format_Basin,
                                     Format_Runoff, Format_Extraction)

        # Run SurfWAT
        SurfWAT.Run_SurfWAT.main(input_nc, output_nc, input_JRC,
                                 Inflow_Text_Files, Reservoirs_GEE_on_off)
    '''
    ################################# Plot graph ##################################

    # Draw graph
    Five.Channel_Routing.Graph_DEM_Distance_Discharge(Discharge_dict_CR3, Distance_dict_CR2, DEM_dict_CR2, River_dict_CR2, Startdate, Enddate, Example_dataset)

    ######################## Change data to fit the LU data #######################

    # Discharge
    # Define info for the nc files
    info = ['monthly','m3-month-1', ''.join([Startdate[5:7], Startdate[0:4]]) , ''.join([Enddate[5:7], Enddate[0:4]])]

    Name_NC_Discharge = DC.Create_NC_name('DischargeEnd', Simulation, Dir_Basin, 5, info)
    if not os.path.exists(Name_NC_Discharge):

        # Get the data of Reference Evapotranspiration and save as nc
        DataCube_Discharge_CR = DC.Convert_dict_to_array(River_dict_CR2, Discharge_dict_CR3, Example_dataset)
        DC.Save_as_NC(Name_NC_Discharge, DataCube_Discharge_CR, 'Discharge_End_CR', Example_dataset, Startdate, Enddate, 'monthly')
        del DataCube_Discharge_CR


    '''
    '''

    # DEM
    Name_NC_DEM = DC.Create_NC_name('DEM', Simulation, Dir_Basin, 5)
    if not os.path.exists(Name_NC_DEM):

        # Get the data of Reference Evapotranspiration and save as nc
        DataCube_DEM_CR = RC.Open_nc_array(Name_NC_DEM_CR)
        DataCube_DEM = RC.resize_array_example(DataCube_DEM_CR, LU_data, method=1)
        DC.Save_as_NC(Name_NC_DEM, DataCube_DEM, 'DEM', LU_dataset)
        del DataCube_DEM

    # flow direction
    Name_NC_DEM_Dir = DC.Create_NC_name('DEM_Dir', Simulation, Dir_Basin, 5)
    if not os.path.exists(Name_NC_DEM_Dir):

        # Get the data of Reference Evapotranspiration and save as nc
        DataCube_DEM_Dir_CR = RC.Open_nc_array(Name_NC_DEM_Dir_CR)
        DataCube_DEM_Dir = RC.resize_array_example(DataCube_DEM_Dir_CR, LU_data, method=1)
        DC.Save_as_NC(Name_NC_DEM_Dir, DataCube_DEM_Dir, 'DEM_Dir', LU_dataset)
        del DataCube_DEM_Dir

    # Precipitation
    # Define info for the nc files
    info = ['monthly','mm', ''.join([Startdate[5:7], Startdate[0:4]]) , ''.join([Enddate[5:7], Enddate[0:4]])]

    Name_NC_Prec = DC.Create_NC_name('Prec', Simulation, Dir_Basin, 5)
    if not os.path.exists(Name_NC_Prec):

        # Get the data of Reference Evapotranspiration and save as nc
        DataCube_Prec = RC.Get3Darray_time_series_monthly(Dir_Basin, Data_Path_P_Monthly, Startdate, Enddate, LU_dataset)
        DC.Save_as_NC(Name_NC_Prec, DataCube_Prec, 'Prec', LU_dataset, Startdate, Enddate, 'monthly', 0.01)
        del DataCube_Prec

    # Evapotranspiration
    Name_NC_ET = DC.Create_NC_name('ET', Simulation, Dir_Basin, 5)
    if not os.path.exists(Name_NC_ET):

        # Get the data of Reference Evapotranspiration and save as nc
        DataCube_ET = RC.Get3Darray_time_series_monthly(Dir_Basin, Data_Path_ET, Startdate, Enddate, LU_dataset)
        DC.Save_as_NC(Name_NC_ET, DataCube_ET, 'ET', LU_dataset, Startdate, Enddate, 'monthly', 0.01)
        del DataCube_ET

    # Reference Evapotranspiration data
    Name_NC_ETref = DC.Create_NC_name('ETref', Simulation, Dir_Basin, 5, info)
    if not os.path.exists(Name_NC_ETref):

        # Get the data of Reference Evapotranspiration and save as nc
        DataCube_ETref = RC.Get3Darray_time_series_monthly(Dir_Basin, Data_Path_ETref, Startdate, Enddate, LU_dataset)
        DC.Save_as_NC(Name_NC_ETref, DataCube_ETref, 'ETref', LU_dataset, Startdate, Enddate, 'monthly', 0.01)
        del DataCube_ETref

    # Rivers
    Name_NC_Rivers = DC.Create_NC_name('Rivers', Simulation, Dir_Basin, 5, info)
    if not os.path.exists(Name_NC_Rivers):

        # Get the data of Reference Evapotranspiration and save as nc
        Rivers_CR = RC.Open_nc_array(Name_NC_Rivers_CR)
        DataCube_Rivers = RC.resize_array_example(Rivers_CR, LU_data)
        DC.Save_as_NC(Name_NC_Rivers, DataCube_Rivers, 'Rivers', LU_dataset)
        del DataCube_Rivers, Rivers_CR

    # Discharge
    # Define info for the nc files
    info = ['monthly','m3', ''.join([Startdate[5:7], Startdate[0:4]]) , ''.join([Enddate[5:7], Enddate[0:4]])]

    Name_NC_Routed_Discharge = DC.Create_NC_name('Routed_Discharge', Simulation, Dir_Basin, 5, info)
    if not os.path.exists(Name_NC_Routed_Discharge):

        # Get the data of Reference Evapotranspiration and save as nc
        Routed_Discharge_CR = RC.Open_nc_array(Name_NC_Discharge)
        DataCube_Routed_Discharge = RC.resize_array_example(Routed_Discharge_CR, LU_data)
        DC.Save_as_NC(Name_NC_Routed_Discharge, DataCube_Routed_Discharge, 'Routed_Discharge', LU_dataset, Startdate, Enddate, 'monthly')
        del DataCube_Routed_Discharge, Routed_Discharge_CR





    # Get raster information
    geo_out, proj, size_X, size_Y = RC.Open_array_info(Example_dataset)

    Rivers = RC.Open_nc_array(Name_NC_Rivers_CR)

    # Create ID Matrix
    y,x = np.indices((size_Y, size_X))
    ID_Matrix = np.int32(np.ravel_multi_index(np.vstack((y.ravel(),x.ravel())),(size_Y,size_X),mode='clip').reshape(x.shape)) + 1

    # Get tiff array time dimension:
    time_dimension = int(np.shape(Discharge_dict_CR3[0])[0])

    # create an empty array
    Result = np.zeros([time_dimension, size_Y, size_X])

    for river_part in range(0,len(River_dict_CR2)):
        for river_pixel in range(1,len(River_dict_CR2[river_part])):
            river_pixel_ID = River_dict_CR2[river_part][river_pixel]
            if len(np.argwhere(ID_Matrix == river_pixel_ID))>0:
                row, col = np.argwhere(ID_Matrix == river_pixel_ID)[0][:]
                Result[:,row,col] = Discharge_dict_CR3[river_part][:,river_pixel]
        print(river_part)


    Outflow = Discharge_dict_CR3[0][:,1]

    for i in range(0,time_dimension):
        output_name = r'C:/testmap/rtest_%s.tif' %i
        Result_one = Result[i, :, :]
        DC.Save_as_tiff(output_name, Result_one, geo_out, "WGS84")

    import os

    # Get environmental variable for the Home folder
    WA_env_paths = os.environ["WA_HOME"].split(';')
    Dir_Home = WA_env_paths[0]

    # Create the Basin folder
    Dir_Basin = os.path.join(Dir_Home, Basin)
    info = ['monthly','m3-month-1', ''.join([Startdate[5:7], Startdate[0:4]]) , ''.join([Enddate[5:7], Enddate[0:4]])]
    Name_Result = DC.Create_NC_name('DischargeEnd', Simulation, Dir_Basin, 5, info)
    Result[np.logical_and(Result == 0.0, Rivers == 0.0)] = np.nan

    DC.Save_as_NC(Name_Result, Result, 'DischargeEnd', Example_dataset, Startdate, Enddate, 'monthly')



    '''

    return ()
Esempio n. 3
0
def Calculate(WA_HOME_folder, Basin, P_Product, ET_Product, LAI_Product,
              ETref_Product, Runoff_Product, Startdate, Enddate, Simulation):
    """
    This functions is the main framework for calculating sheet 4.

    Parameters
    ----------
    Basin : str
        Name of the basin
    P_Product : str
        Name of the rainfall product that will be used
    ET_Product : str
        Name of the evapotranspiration product that will be used
    LAI_Product : str
        Name of the LAI product that will be used
    Runoff_Product : str
        Name of the Runoff product that will be used
    Moving_Averiging_Length, int
        Defines the length of the moving average
    Startdate : str
        Contains the start date of the model 'yyyy-mm-dd'
    Enddate : str
        Contains the end date of the model 'yyyy-mm-dd'
    Simulation : int
        Defines the simulation

    """
    ######################### Import WA modules ###################################

    from watools.General import raster_conversions as RC
    from watools.General import data_conversions as DC
    import watools.Functions.Four as Four
    import watools.Functions.Start as Start
    import watools.Generator.Sheet4 as Generate
    import watools.Functions.Start.Get_Dictionaries as GD

    ######################### Set General Parameters ##############################

    # Get environmental variable for the Home folder
    if WA_HOME_folder == '':
        WA_env_paths = os.environ["WA_HOME"].split(';')
        Dir_Home = WA_env_paths[0]
    else:
        Dir_Home = WA_HOME_folder

    # Create the Basin folder
    Dir_Basin = os.path.join(Dir_Home, Basin)
    output_dir = os.path.join(Dir_Basin, "Simulations",
                              "Simulation_%d" % Simulation)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # Get the boundaries of the basin based on the shapefile of the watershed
    # Boundaries, Shape_file_name_shp = Start.Boundaries.Determine(Basin)
    Boundaries, Example_dataset = Start.Boundaries.Determine_LU_Based(
        Basin, Dir_Home)

    # Find the maximum moving window value
    ET_Blue_Green_Classes_dict, Moving_Window_Per_Class_dict = GD.get_bluegreen_classes(
        version='1.0')
    Additional_Months_tail = np.max(list(
        Moving_Window_Per_Class_dict.values()))

    ############## Cut dates into pieces if it is needed ######################

    # Check the years that needs to be calculated
    years = list(
        range(int(Startdate.split('-')[0]),
              int(Enddate.split('-')[0]) + 1))

    for year in years:

        # Create .nc file if not exists
        nc_outname = os.path.join(output_dir, "%d.nc" % year)
        if not os.path.exists(nc_outname):
            DC.Create_new_NC_file(nc_outname, Example_dataset, Basin)

        # Open variables in netcdf
        fh = Dataset(nc_outname)
        Variables_NC = [var for var in list(fh.variables)]
        fh.close()

        # Create Start and End date for time chunk
        Startdate_part = '%d-01-01' % int(year)
        Enddate_part = '%s-12-31' % int(year)

        if int(year) == int(years[0]):
            Startdate_Moving_Average = pd.Timestamp(Startdate) - pd.DateOffset(
                months=Additional_Months_tail)
            Startdate_Moving_Average_String = Startdate_Moving_Average.strftime(
                '%Y-%m-%d')
        else:
            Startdate_Moving_Average_String = Startdate_part

        ############################# Download Data ###################################

        # Download data
        if not "Precipitation" in Variables_NC:
            Data_Path_P_Monthly = Start.Download_Data.Precipitation(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, P_Product)

        if not "Actual_Evapotranspiration" in Variables_NC:
            Data_Path_ET = Start.Download_Data.Evapotranspiration(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, ET_Product)

        if not "Reference_Evapotranspiration" in Variables_NC:
            Data_Path_ETref = Start.Download_Data.ETreference(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']],
                Startdate_Moving_Average_String, Enddate_part, ETref_Product)

        if not "Grey_Water_Footprint" in Variables_NC:
            Data_Path_GWF = Start.Download_Data.GWF(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']])

        if not "Theta_Saturated_Topsoil" in Variables_NC:
            Data_Path_ThetaSat_topsoil = Start.Download_Data.Soil_Properties(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']],
                Para='ThetaSat_TopSoil')

        ###################### Save Data as netCDF files ##############################

        #______________________________Precipitation_______________________________

        # 1.) Precipitation data
        if not "Precipitation" in Variables_NC:
            # Get the data of Precipitation and save as nc
            DataCube_Prec = RC.Get3Darray_time_series_monthly(
                Data_Path_P_Monthly,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Prec,
                                     "Precipitation", "mm/month", 0.01)
            del DataCube_Prec

    #_______________________Reference Evaporation______________________________

    # 2.) Reference Evapotranspiration data
        if not "Reference_Evapotranspiration" in Variables_NC:
            # Get the data of Precipitation and save as nc
            DataCube_ETref = RC.Get3Darray_time_series_monthly(
                Data_Path_ETref,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ETref,
                                     "Reference_Evapotranspiration",
                                     "mm/month", 0.01)
            del DataCube_ETref

        #_______________________________Evaporation________________________________

        # 3.) Evapotranspiration data
        if not "Actual_Evapotranspiration" in Variables_NC:
            # Get the data of Evaporation and save as nc
            DataCube_ET = RC.Get3Darray_time_series_monthly(
                Data_Path_ET,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ET,
                                     "Actual_Evapotranspiration", "mm/month",
                                     0.01)
            del DataCube_ET

        #_____________________________________GWF__________________________________

        # 4.) Grey Water Footprint data
        if not "Grey_Water_Footprint" in Variables_NC:
            # Get the data of grey water footprint and save as nc
            GWF_Filepath = os.path.join(Dir_Basin, Data_Path_GWF,
                                        "Gray_Water_Footprint_Fraction.tif")
            dest_GWF = RC.reproject_dataset_example(GWF_Filepath,
                                                    Example_dataset,
                                                    method=1)
            DataCube_GWF = dest_GWF.GetRasterBand(1).ReadAsArray()
            DC.Add_NC_Array_Static(nc_outname, DataCube_GWF,
                                   "Grey_Water_Footprint", "fraction", 0.0001)
            del DataCube_GWF
        #________________________Theta_Saturated_Topsoil___________________________

        # 5.) Grey Water Footprint data
        if not "Theta_Saturated_Topsoil" in Variables_NC:
            # Get the data of grey water footprint and save as nc
            ThetaSat_Filepath = os.path.join(
                Dir_Basin, Data_Path_ThetaSat_topsoil,
                "Theta_Saturated_Topsoil_HiHydroSoil.tif")
            dest_ThetaSat = RC.reproject_dataset_example(ThetaSat_Filepath,
                                                         Example_dataset,
                                                         method=1)
            DataCube_ThetaSat = dest_ThetaSat.GetRasterBand(1).ReadAsArray()
            DC.Add_NC_Array_Static(nc_outname, DataCube_ThetaSat,
                                   "Theta_Saturated_Topsoil", "fraction",
                                   0.0001)
            del DataCube_ThetaSat

    ####################### Calculations Sheet 4 ##############################

    ############## Cut dates into pieces if it is needed ######################

    years = list(
        range(int(Startdate.split('-')[0]),
              int(Enddate.split('-')[0]) + 1))

    for year in years:

        if len(years) > 1.0:

            if year is years[0]:
                Startdate_part = Startdate
                Enddate_part = '%s-12-31' % year
            if year is years[-1]:
                Startdate_part = '%s-01-01' % year
                Enddate_part = Enddate

        else:
            Startdate_part = Startdate
            Enddate_part = Enddate

        #____________ Evapotranspiration data split in ETblue and ETgreen ____________

        if not ("Blue_Evapotranspiration" in Variables_NC
                or "Green_Evapotranspiration" in Variables_NC):

            # Calculate Blue and Green ET
            DataCube_ETblue, DataCube_ETgreen = Four.SplitET.Blue_Green(
                Dir_Basin, nc_outname, ETref_Product, P_Product, Startdate,
                Enddate)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ETblue,
                                     "Blue_Evapotranspiration", "mm/month",
                                     0.01)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ETgreen,
                                     "Green_Evapotranspiration", "mm/month",
                                     0.01)
            del DataCube_ETblue, DataCube_ETgreen

        #____________ Calculate non-consumend and Total supply maps by using fractions and consumed maps (blue ET) ____________

        if not ("Total_Supply" in Variables_NC
                or "Non_Consumed_Water" in Variables_NC):

            # Do the calculations
            DataCube_Total_Supply, DataCube_Non_Consumed = Four.Total_Supply.Fraction_Based(
                nc_outname, Startdate_part, Enddate_part)

            # Save the Total Supply and non consumed data as NetCDF files
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Total_Supply,
                                     "Total_Supply", "mm/month", 0.01)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Non_Consumed,
                                     "Non_Consumed_Water", "mm/month", 0.01)
            del DataCube_Total_Supply, DataCube_Non_Consumed

        #____________ Apply fractions over total supply to calculate gw and sw supply ____________

        if not ("Total_Supply_Surface_Water" in Variables_NC
                or "Total_Supply_Ground_Water" in Variables_NC):

            # Do the calculations
            DataCube_Total_Supply_SW, DataCube_Total_Supply_GW = Four.SplitGW_SW_Supply.Fraction_Based(
                nc_outname, Startdate_part, Enddate_part)

            # Save the Total Supply surface water and Total Supply ground water data as NetCDF files
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Total_Supply_SW,
                                     "Total_Supply_Surface_Water", "mm/month",
                                     0.01)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Total_Supply_GW,
                                     "Total_Supply_Ground_Water", "mm/month",
                                     0.01)
            del DataCube_Total_Supply_SW, DataCube_Total_Supply_GW

        #____________ Apply gray water footprint fractions to calculated non recoverable flow based on the non consumed flow ____________

        if not ("Non_Recovable_Flow" in Variables_NC
                or "Recovable_Flow" in Variables_NC):

            # Calculate the non recovable flow and recovable flow by using Grey Water Footprint values
            DataCube_NonRecovableFlow, Datacube_RecovableFlow = Four.SplitNonConsumed_NonRecov.GWF_Based(
                nc_outname, Startdate_part, Enddate_part)

            # Get the data of Evaporation and save as nc
            DC.Add_NC_Array_Variable(nc_outname, DataCube_NonRecovableFlow,
                                     "Non_Recovable_Flow", "mm/month", 0.01)
            DC.Add_NC_Array_Variable(nc_outname, Datacube_RecovableFlow,
                                     "Recovable_Flow", "mm/month", 0.01)
            del DataCube_NonRecovableFlow, Datacube_RecovableFlow

        #____________Apply fractions to calculate the non recovarable SW/GW and recovarable SW/GW ____________

        # 1. Non recovarable flow
        if not ("Non_Recovable_Flow_Ground_Water" in Variables_NC
                or "Non_Recovable_Flow_Surface_Water" in Variables_NC):

            # Calculate the non recovable return flow to ground and surface water
            DataCube_NonRecovableFlow_Return_GW, Datacube_NonRecovableFlow_Return_SW = Four.SplitGW_SW_Return.Fraction_Based(
                nc_outname, "Non_Recovable_Flow", Startdate_part, Enddate_part)

            # Get the data of Evaporation and save as nc
            DC.Add_NC_Array_Variable(nc_outname,
                                     DataCube_NonRecovableFlow_Return_GW,
                                     "Non_Recovable_Flow_Ground_Water",
                                     "mm/month", 0.01)
            DC.Add_NC_Array_Variable(nc_outname,
                                     Datacube_NonRecovableFlow_Return_SW,
                                     "Non_Recovable_Flow_Surface_Water",
                                     "mm/month", 0.01)
            del DataCube_NonRecovableFlow_Return_GW, Datacube_NonRecovableFlow_Return_SW

        # 2. Recovarable flow
        if not ("Recovable_Flow_Ground_Water" in Variables_NC
                or "Recovable_Flow_Surface_Water" in Variables_NC):

            # Calculate the non recovable return flow to ground and surface water
            DataCube_RecovableFlow_Return_GW, Datacube_RecovableFlow_Return_SW = Four.SplitGW_SW_Return.Fraction_Based(
                nc_outname, "Recovable_Flow", Startdate_part, Enddate_part)

            # Get the data of Evaporation and save as nc
            DC.Add_NC_Array_Variable(nc_outname,
                                     DataCube_RecovableFlow_Return_GW,
                                     "Recovable_Flow_Ground_Water", "mm/month",
                                     0.01)
            DC.Add_NC_Array_Variable(nc_outname,
                                     Datacube_RecovableFlow_Return_SW,
                                     "Recovable_Flow_Surface_Water",
                                     "mm/month", 0.01)
            del DataCube_RecovableFlow_Return_GW, Datacube_RecovableFlow_Return_SW

        ############################ Create CSV 4 #################################

        Dir_Basin_CSV, Unit_front = Generate.CSV.Create(
            Dir_Basin, Simulation, Basin, Startdate_part, Enddate_part,
            nc_outname)

    ############################ Create Sheet 4 ###############################

    Generate.PDF.Create(Dir_Basin, Basin, Simulation, Dir_Basin_CSV,
                        Unit_front)

    return ()
Esempio n. 4
0
def Calculate(WA_HOME_folder, Basin, P_Product, ET_Product, LAI_Product,
              NDM_Product, NDVI_Product, ETref_Product, dict_crops,
              dict_non_crops, Startdate, Enddate, Simulation):
    """
    This functions is the main framework for calculating sheet 3.

    Parameters
    ----------
    Basin : str
        Name of the basin
    P_Product : str
        Name of the rainfall product that will be used
    ET_Product : str
        Name of the evapotranspiration product that will be used
    LAI_Product : str
        Name of the LAI product that will be used
    NDM_Product : str
        Name of the NDM product that will be used
    Startdate : str
        Contains the start date of the model 'yyyy-mm-dd'
    Enddate : str
        Contains the end date of the model 'yyyy-mm-dd'
    Simulation : int
        Defines the simulation

    """
    ######################### Import WA modules ###################################

    from watools.General import raster_conversions as RC
    from watools.General import data_conversions as DC
    import watools.Functions.Three as Three
    import watools.Functions.Two as Two
    import watools.Functions.Start as Start
    import watools.Functions.Four as Four
    import watools.Generator.Sheet3 as Generate
    import watools.Functions.Start.Get_Dictionaries as GD

    ######################### Set General Parameters ##############################

    # Check if there is a full year selected  between Startdate and Enddate, otherwise Sheet 3 cannot be produced
    try:
        years_end = pd.date_range(Startdate, Enddate, freq="A").year
        years_start = pd.date_range(Startdate, Enddate, freq="AS").year
        if (len(years_start) == 0 or len(years_end) == 0):
            print(
                "Calculation period is less than a year, which is not possible for sheet 3"
            )
            quit
        years = np.unique(np.append(years_end, years_start))
    except:
        print(
            "Calculation period is less than a year, which is not possible for sheet 3"
        )
        quit

    # Get environmental variable for the Home folder
    if WA_HOME_folder == '':
        WA_env_paths = os.environ["WA_HOME"].split(';')
        Dir_Home = WA_env_paths[0]
    else:
        Dir_Home = WA_HOME_folder

    # Create the Basin folder
    Dir_Basin = os.path.join(Dir_Home, Basin)
    output_dir = os.path.join(Dir_Basin, "Simulations",
                              "Simulation_%d" % Simulation)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # Get the boundaries of the basin based on the shapefile of the watershed
    # Boundaries, Shape_file_name_shp = Start.Boundaries.Determine(Basin)
    Boundaries, Example_dataset = Start.Boundaries.Determine_LU_Based(
        Basin, Dir_Home)

    ############################# Download Data ###################################
    # Check the years that needs to be calculated
    years = range(int(Startdate.split('-')[0]), int(Enddate.split('-')[0]) + 1)

    # Find the maximum moving window value
    ET_Blue_Green_Classes_dict, Moving_Window_Per_Class_dict = GD.get_bluegreen_classes(
        version='1.0')
    Additional_Months_tail = np.max(list(
        Moving_Window_Per_Class_dict.values()))

    for year in years:

        # Create Start and End date for time chunk
        Startdate_part = '%d-01-01' % int(year)
        Enddate_part = '%s-12-31' % year

        # Create .nc file if not exists
        nc_outname = os.path.join(output_dir, "%d.nc" % year)
        if not os.path.exists(nc_outname):
            DC.Create_new_NC_file(nc_outname, Example_dataset, Basin)

        #Set Startdate for moving average
        if int(year) == int(years[0]):
            Startdate_Moving_Average = pd.Timestamp(Startdate) - pd.DateOffset(
                months=Additional_Months_tail)
            Startdate_Moving_Average_String = Startdate_Moving_Average.strftime(
                '%Y-%m-%d')
        else:
            Startdate_Moving_Average_String = Startdate_part

        # Open variables in netcdf
        fh = netCDF4.Dataset(nc_outname)
        Variables_NC = [var for var in fh.variables]
        fh.close()

        # Download data
        if not "Precipitation" in Variables_NC:
            Data_Path_P_Monthly = Start.Download_Data.Precipitation(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, P_Product)

        if not "Actual_Evapotransporation" in Variables_NC:
            Data_Path_ET = Start.Download_Data.Evapotranspiration(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, ET_Product)

        if not "Reference_Evapotranspiration" in Variables_NC:
            Data_Path_ETref = Start.Download_Data.ETreference(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, ETref_Product)

        if not "NDVI" in Variables_NC:
            Data_Path_NDVI = Start.Download_Data.NDVI(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part)

        if not "Normalized_Dry_Matter" in Variables_NC:
            Data_Path_NPP = Start.Download_Data.NPP(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, NDM_Product)
            Data_Path_GPP = Start.Download_Data.GPP(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_part,
                Enddate_part, NDM_Product)

        # Download additional time before the research time frame
        if year == years[0]:
            Data_Path_P_Monthly = Start.Download_Data.Precipitation(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']],
                Startdate_Moving_Average_String, "%d-01-01" % year, P_Product)

            Data_Path_ETref = Start.Download_Data.ETreference(
                Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
                [Boundaries['Lonmin'], Boundaries['Lonmax']],
                Startdate_Moving_Average_String, "%d-01-01" % year,
                ETref_Product)

        ########################### Create input data #################################

        if not "Normalized_Dry_Matter" in Variables_NC:
            # Create NDM based on MOD17
            if NDM_Product == 'MOD17':
                # Create monthly GPP
                Start.Eightdaily_to_monthly_state.Nearest_Interpolate(
                    Data_Path_GPP, Startdate_part, Enddate_part)
                Data_Path_NDM = Two.Calc_NDM.NPP_GPP_Based(
                    Dir_Basin, Data_Path_GPP, Data_Path_NPP, Startdate_part,
                    Enddate_part)

        if not "NDVI" in Variables_NC:
            # Create monthly NDVI based on MOD13
            if NDVI_Product == 'MOD13':
                Start.Sixteendaily_to_monthly_state.Nearest_Interpolate(
                    Data_Path_NDVI, Startdate_part, Enddate_part)

        ###################### Save Data as netCDF files ##############################
        #______________________________Precipitation_______________________________

        # 1.) Precipitation data
        if not "Precipitation" in Variables_NC:
            # Get the data of Precipitation and save as nc
            DataCube_Prec = RC.Get3Darray_time_series_monthly(
                Data_Path_P_Monthly,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_Prec,
                                     "Precipitation", "mm/month", 0.01)
            del DataCube_Prec

        #_______________________________Evaporation________________________________

        # 2.) Evapotranspiration data
        if not "Actual_Evapotranspiration" in Variables_NC:
            # Get the data of Evaporation and save as nc
            DataCube_ET = RC.Get3Darray_time_series_monthly(
                Data_Path_ET,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ET,
                                     "Actual_Evapotranspiration", "mm/month",
                                     0.01)
            del DataCube_ET

        #___________________________Normalized Dry Matter__________________________

        # 3.) Normalized Dry Matter
        if not "Normalized_Dry_Matter" in Variables_NC:
            # Get the data of Evaporation and save as nc
            DataCube_NDM = RC.Get3Darray_time_series_monthly(
                Data_Path_NDM,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_NDM,
                                     "Normalized_Dry_Matter", "kg_ha", 0.01)
            del DataCube_NDM

        #_______________________Reference Evaporation______________________________

        # 4.) Reference Evapotranspiration data
        if not "Reference_Evapotranspiration" in Variables_NC:
            # Get the data of Precipitation and save as nc
            DataCube_ETref = RC.Get3Darray_time_series_monthly(
                Data_Path_ETref,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ETref,
                                     "Reference_Evapotranspiration",
                                     "mm/month", 0.01)
            del DataCube_ETref

        #____________________________________NDVI__________________________________

        # 4.) Reference Evapotranspiration data
        if not "NDVI" in Variables_NC:
            # Get the data of Precipitation and save as nc
            DataCube_NDVI = RC.Get3Darray_time_series_monthly(
                Data_Path_NDVI,
                Startdate_part,
                Enddate_part,
                Example_data=Example_dataset)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_NDVI, "NDVI",
                                     "Fraction", 0.0001)
            del DataCube_NDVI

        ############################# Calculate Sheet 3 ###########################

        #____________ Evapotranspiration data split in ETblue and ETgreen ____________

        if not ("Blue_Evapotranspiration" in Variables_NC
                or "Green_Evapotranspiration" in Variables_NC):

            # Calculate Blue and Green ET
            DataCube_ETblue, DataCube_ETgreen = Four.SplitET.Blue_Green(
                Dir_Basin, nc_outname, ETref_Product, P_Product, Startdate,
                Enddate)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ETblue,
                                     "Blue_Evapotranspiration", "mm/month",
                                     0.01)
            DC.Add_NC_Array_Variable(nc_outname, DataCube_ETgreen,
                                     "Green_Evapotranspiration", "mm/month",
                                     0.01)
            del DataCube_ETblue, DataCube_ETgreen

    #____________________________ Create the empty dictionaries ____________________________

    # Create the dictionaries that are required for sheet 3
    wp_y_irrigated_dictionary, wp_y_rainfed_dictionary, wp_y_non_crop_dictionary = GD.get_sheet3_empties(
    )

    #____________________________________ Fill in the dictionaries ________________________

    # Fill in the crops dictionaries
    wp_y_irrigated_dictionary, wp_y_rainfed_dictionary = Three.Fill_Dicts.Crop_Dictionaries(
        wp_y_irrigated_dictionary, wp_y_rainfed_dictionary, dict_crops,
        nc_outname, Dir_Basin)

    # Fill in the non crops dictionaries
    wp_y_non_crop_dictionary = Three.Fill_Dicts.Non_Crop_Dictionaries(
        wp_y_non_crop_dictionary, dict_non_crops)

    ############################ Create CSV 3 #################################

    csv_fh_a, csv_fh_b = Generate.CSV.Create(wp_y_irrigated_dictionary,
                                             wp_y_rainfed_dictionary,
                                             wp_y_non_crop_dictionary, Basin,
                                             Simulation, year, Dir_Basin)

    ############################ Create Sheet 3 ###############################

    Generate.PDF.Create(Dir_Basin, Basin, Simulation, csv_fh_a, csv_fh_b)

    return ()