예제 #1
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 wa.General import raster_conversions as RC
    from wa.General import data_conversions as DC
    import wa.Functions.Three as Three
    import wa.Functions.Two as Two
    import wa.Functions.Start as Start
    import wa.Functions.Four as Four
    import wa.Generator.Sheet3 as Generate
    import wa.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(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_Moving_Average_String, 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)

        ########################### 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 ()
예제 #2
0
파일: main.py 프로젝트: jupaladin/wa
def Calculate(Basin, P_Product, ET_Product, Inflow_Text_Files,
              Reservoirs_Lakes_Calculations, 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. Create Mask based on LU map
    5. Calculate Runoff based on Budyko
    6. Add inflow in Runoff
    7. Calculate River flow
       7.1  Route Runoff
       7.2  Add Reservoirs
       7.3  Add surface water withdrawals
    '''
    # import General modules
    import os
    import gdal
    import numpy as np
    import pandas as pd
    import copy

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

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

    # 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)
    if not os.path.exists(Dir_Basin):
        os.makedirs(Dir_Basin)

    # Get the boundaries of the basin based on the shapefile of the watershed
    # Boundaries, Shape_file_name_shp = Start.Boundaries.Determine(Basin)
    Boundaries, LU_dataset = Start.Boundaries.Determine_LU_Based(Basin)
    LU_data = RC.Open_tiff_array(LU_dataset)
    geo_out_LU, proj_LU, size_X_LU, size_Y_LU = RC.Open_array_info(LU_dataset)

    # Define resolution of SRTM
    Resolution = '15s'

    # Get the amount of months
    Amount_months = len(pd.date_range(Startdate, Enddate, freq='MS'))
    Amount_months_reservoirs = Amount_months + 1

    # Startdate for moving window Budyko
    Startdate_2months_Timestamp = pd.Timestamp(Startdate) - pd.DateOffset(
        months=2)
    Startdate_2months = Startdate_2months_Timestamp.strftime('%Y-%m-%d')

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

    # Download data
    Data_Path_P = Start.Download_Data.Precipitation(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_2months,
        Enddate, P_Product)
    Data_Path_ET = Start.Download_Data.Evapotranspiration(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_2months,
        Enddate, ET_Product)
    Data_Path_DEM = Start.Download_Data.DEM(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Resolution)
    if Resolution is not '3s':
        Data_Path_DEM = Start.Download_Data.DEM(
            Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
            [Boundaries['Lonmin'], Boundaries['Lonmax']], Resolution)
    Data_Path_DEM_Dir = Start.Download_Data.DEM_Dir(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Resolution)
    Data_Path_ETref = Start.Download_Data.ETreference(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate_2months,
        Enddate)
    Data_Path_JRC_occurrence = Start.Download_Data.JRC_occurrence(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']])
    Data_Path_P_Monthly = os.path.join(Data_Path_P, 'Monthly')

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

    #_____________________________________DEM__________________________________
    # Get the data of DEM and save as nc, This dataset is also used as reference for others
    Example_dataset = os.path.join(Dir_Basin, Data_Path_DEM,
                                   'DEM_HydroShed_m_%s.tif' % Resolution)
    DEMdest = gdal.Open(Example_dataset)
    Xsize_CR = int(DEMdest.RasterXSize)
    Ysize_CR = int(DEMdest.RasterYSize)
    DataCube_DEM_CR = DEMdest.GetRasterBand(1).ReadAsArray()

    Name_NC_DEM_CR = DC.Create_NC_name('DEM_CR', Simulation, Dir_Basin, 5)
    if not os.path.exists(Name_NC_DEM_CR):
        DC.Save_as_NC(Name_NC_DEM_CR, DataCube_DEM_CR, 'DEM_CR',
                      Example_dataset)
    DEMdest = None

    #___________________________________DEM Dir________________________________
    # Get the data of flow direction and save as nc.
    Dir_dataset = os.path.join(Dir_Basin, Data_Path_DEM_Dir,
                               'DIR_HydroShed_-_%s.tif' % Resolution)
    DEMDirdest = gdal.Open(Dir_dataset)
    DataCube_DEM_Dir_CR = DEMDirdest.GetRasterBand(1).ReadAsArray()

    Name_NC_DEM_Dir_CR = DC.Create_NC_name('DEM_Dir_CR', Simulation, Dir_Basin,
                                           5)
    if not os.path.exists(Name_NC_DEM_Dir_CR):
        DC.Save_as_NC(Name_NC_DEM_Dir_CR, DataCube_DEM_Dir_CR, 'DEM_Dir_CR',
                      Example_dataset)
    DEMDirdest = None
    del DataCube_DEM_Dir_CR

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

    # Precipitation data
    Name_NC_Prec_CR = DC.Create_NC_name('Prec_CR', Simulation, Dir_Basin, 5,
                                        info)
    if not os.path.exists(Name_NC_Prec_CR):

        # Get the data of Precipitation and save as nc
        DataCube_Prec_CR = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_P_Monthly,
            Startdate_2months,
            Enddate,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_Prec_CR, DataCube_Prec_CR, 'Prec_CR',
                      Example_dataset, Startdate_2months, Enddate, 'monthly',
                      0.01)
        del DataCube_Prec_CR

    #____________________________ Evapotranspiration___________________________
    # Evapotranspiration data
    info = [
        'monthly', 'mm',
        ''.join([Startdate_2months[5:7], Startdate_2months[0:4]]),
        ''.join([Enddate[5:7], Enddate[0:4]])
    ]
    Name_NC_ET_CR = DC.Create_NC_name('ET_CR', Simulation, Dir_Basin, 5, info)
    if not os.path.exists(Name_NC_ET_CR):

        # Get the data of Evaporation and save as nc
        DataCube_ET_CR = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_ET,
            Startdate_2months,
            Enddate,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_ET_CR, DataCube_ET_CR, 'ET_CR', Example_dataset,
                      Startdate_2months, Enddate, 'monthly', 0.01)
        del DataCube_ET_CR

    #_______________________Reference Evapotranspiration_______________________
    # Reference Evapotranspiration data
    Name_NC_ETref_CR = DC.Create_NC_name('ETref_CR', Simulation, Dir_Basin, 5,
                                         info)
    if not os.path.exists(Name_NC_ETref_CR):

        # Get the data of Reference Evapotranspiration and save as nc
        DataCube_ETref_CR = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_ETref,
            Startdate_2months,
            Enddate,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_ETref_CR, DataCube_ETref_CR, 'ETref_CR',
                      Example_dataset, Startdate_2months, Enddate, 'monthly',
                      0.01)
        del DataCube_ETref_CR

    #_______________________fraction surface water _______________________

    Name_NC_frac_sw_CR = DC.Create_NC_name('Fraction_SW_CR', Simulation,
                                           Dir_Basin, 5)
    if not os.path.exists(Name_NC_frac_sw_CR):
        DataCube_frac_sw = np.ones_like(LU_data) * np.nan

        import wa.Functions.Start.Get_Dictionaries as GD

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

        for key in lulc_dict:
            Numbers = lulc[key]
            for LU_nmbr in Numbers:
                Mask = np.zeros_like(LU_data)
                Mask[LU_data == LU_nmbr] = 1
                DataCube_frac_sw[Mask == 1] = consumed_frac_dict[key]

        dest_frac_sw = DC.Save_as_MEM(DataCube_frac_sw, geo_out_LU, proj_LU)
        dest_frac_sw_CR = RC.reproject_dataset_example(dest_frac_sw,
                                                       Example_dataset)
        DataCube_frac_sw_CR = dest_frac_sw_CR.ReadAsArray()
        DataCube_frac_sw_CR[DataCube_frac_sw_CR == 0] = np.nan

        DC.Save_as_NC(Name_NC_frac_sw_CR,
                      DataCube_frac_sw_CR,
                      'Fraction_SW_CR',
                      Example_dataset,
                      Scaling_factor=0.01)
        del DataCube_frac_sw_CR

    del DataCube_DEM_CR
    ##################### 4. Create Mask based on LU map ###########################

    # Now a mask will be created to define the area of interest (pixels where there is a landuse defined)

    #_____________________________________LU___________________________________
    destLU = RC.reproject_dataset_example(LU_dataset,
                                          Example_dataset,
                                          method=1)
    DataCube_LU_CR = destLU.GetRasterBand(1).ReadAsArray()

    Raster_Basin_CR = np.zeros([Ysize_CR, Xsize_CR])
    Raster_Basin_CR[DataCube_LU_CR > 0] = 1
    Name_NC_Basin_CR = DC.Create_NC_name('Basin_CR', Simulation, Dir_Basin, 5)
    if not os.path.exists(Name_NC_Basin_CR):
        DC.Save_as_NC(Name_NC_Basin_CR, Raster_Basin_CR, 'Basin_CR',
                      Example_dataset)
        #del Raster_Basin
    '''
    Name_NC_Basin = DC.Create_NC_name('Basin_CR', Simulation, Dir_Basin, 5)
    if not os.path.exists(Name_NC_Basin):

        Raster_Basin = RC.Vector_to_Raster(Dir_Basin, Shape_file_name_shp, Example_dataset)
        Raster_Basin = np.clip(Raster_Basin, 0, 1)
        DC.Save_as_NC(Name_NC_Basin, Raster_Basin, 'Basin_CR', Example_dataset)
        #del Raster_Basin
    '''
    ###################### 5. Calculate Runoff based on Budyko ###########################

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

    # Define the output names of section 5 and 6
    Name_NC_Runoff_CR = DC.Create_NC_name('Runoff_CR', Simulation, Dir_Basin,
                                          5, info)
    Name_NC_Runoff_for_Routing_CR = Name_NC_Runoff_CR

    if not os.path.exists(Name_NC_Runoff_CR):

        # Calculate runoff based on Budyko
        DataCube_Runoff_CR = Five.Budyko.Calc_runoff(Name_NC_ETref_CR,
                                                     Name_NC_Prec_CR)

        # Save the runoff as netcdf
        DC.Save_as_NC(Name_NC_Runoff_CR, DataCube_Runoff_CR, 'Runoff_CR',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)
        del DataCube_Runoff_CR
    '''  
    ###################### Calculate Runoff with P min ET ###########################
  
    Name_NC_Runoff_CR = DC.Create_NC_name('Runoff_CR', Simulation, Dir_Basin, 5, info)
    if not os.path.exists(Name_NC_Runoff_CR):

        ET = RC.Open_nc_array(Name_NC_ET_CR)
        P = RC.Open_nc_array(Name_NC_Prec_CR) 
        DataCube_Runoff_CR = P - ET
        DataCube_Runoff_CR[:,:,:][DataCube_Runoff_CR<=0.1] = 0
        DataCube_Runoff_CR[:,:,:][np.isnan(DataCube_Runoff_CR)] = 0                          
        DC.Save_as_NC(Name_NC_Runoff_CR, DataCube_Runoff_CR, 'Runoff_CR', Example_dataset, Startdate, Enddate, 'monthly')
        del DataCube_Runoff_CR

     '''
    ############### 6. Add inflow in basin by using textfile #########################

    # add inlets if there are textfiles defined
    if len(Inflow_Text_Files) > 0:

        # Create name of the Runoff with inlets
        Name_NC_Runoff_with_Inlets_CR = DC.Create_NC_name(
            'Runoff_with_Inlets_CR', Simulation, Dir_Basin, 5, info)

        # Use this runoff name for the routing (it will overwrite the runoff without inlets)
        Name_NC_Runoff_for_Routing_CR = Name_NC_Runoff_with_Inlets_CR

        # Create the file if it not exists
        if not os.path.exists(Name_NC_Runoff_with_Inlets_CR):

            # Calculate the runoff that will be routed by including the inlets
            DataCube_Runoff_with_Inlets_CR = Five.Inlets.Add_Inlets(
                Name_NC_Runoff_CR, Inflow_Text_Files)

            # Save this runoff as netcdf
            DC.Save_as_NC(Name_NC_Runoff_with_Inlets_CR,
                          DataCube_Runoff_with_Inlets_CR,
                          'Runoff_with_Inlets_CR', Example_dataset, Startdate,
                          Enddate, 'monthly', 0.01)
            del DataCube_Runoff_with_Inlets_CR

    ######################### 7. Now the surface water is calculated #################

    # Names for dicionaries and nc files
    # CR1 = Natural_flow with only green water
    # CR2 = Natural_flow with only green water and reservoirs
    # CR3 = Flow with green, blue and reservoirs

    ######################### 7.1 Apply Channel Routing ###############################

    # Create the name for the netcdf outputs for section 7.1
    info = [
        'monthly', 'pixels', ''.join([Startdate[5:7], Startdate[0:4]]),
        ''.join([Enddate[5:7], Enddate[0:4]])
    ]
    Name_NC_Acc_Pixels_CR = DC.Create_NC_name('Acc_Pixels_CR', Simulation,
                                              Dir_Basin, 5)
    info = [
        'monthly', 'm3', ''.join([Startdate[5:7], Startdate[0:4]]),
        ''.join([Enddate[5:7], Enddate[0:4]])
    ]
    Name_NC_Discharge_CR1 = DC.Create_NC_name('Discharge_CR1', Simulation,
                                              Dir_Basin, 5, info)

    # If one of the outputs does not exists, run this part
    if not (os.path.exists(Name_NC_Acc_Pixels_CR)
            and os.path.exists(Name_NC_Discharge_CR1)):

        Accumulated_Pixels_CR, Discharge_CR1 = Five.Channel_Routing.Channel_Routing(
            Name_NC_DEM_Dir_CR,
            Name_NC_Runoff_for_Routing_CR,
            Name_NC_Basin_CR,
            Example_dataset,
            Degrees=1)

        # Save Results
        DC.Save_as_NC(Name_NC_Acc_Pixels_CR, Accumulated_Pixels_CR,
                      'Acc_Pixels_CR', Example_dataset)
        DC.Save_as_NC(Name_NC_Discharge_CR1, Discharge_CR1, 'Discharge_CR1',
                      Example_dataset, Startdate, Enddate, 'monthly')

    ################# Calculate the natural river and river zones #################

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

        # Open routed discharge array
        Discharge_CR1 = RC.Open_nc_array(Name_NC_Discharge_CR1)
        Raster_Basin = RC.Open_nc_array(Name_NC_Basin_CR)

        # Calculate mean average over the period
        if len(np.shape(Discharge_CR1)) > 2:
            Routed_Discharge_Ave = np.nanmean(Discharge_CR1, axis=0)
        else:
            Routed_Discharge_Ave = Discharge_CR1

        # Define the 2% highest pixels as rivers
        Rivers = np.zeros([
            np.size(Routed_Discharge_Ave, 0),
            np.size(Routed_Discharge_Ave, 1)
        ])
        Routed_Discharge_Ave[Raster_Basin != 1] = np.nan
        Routed_Discharge_Ave_number = np.nanpercentile(Routed_Discharge_Ave,
                                                       98)
        Rivers[
            Routed_Discharge_Ave >
            Routed_Discharge_Ave_number] = 1  # if yearly average is larger than 5000km3/month that it is a river

        # Save the river file as netcdf file
        DC.Save_as_NC(Name_NC_Rivers_CR, Rivers, 'Rivers_CR', Example_dataset)

    ########################## Create river directories ###########################

    Name_py_River_dict_CR1 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'River_dict_CR1_simulation%d.npy' % (Simulation))
    Name_py_DEM_dict_CR1 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'DEM_dict_CR1_simulation%d.npy' % (Simulation))
    Name_py_Distance_dict_CR1 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'Distance_dict_CR1_simulation%d.npy' % (Simulation))

    if not (os.path.exists(Name_py_River_dict_CR1)
            and os.path.exists(Name_py_DEM_dict_CR1)
            and os.path.exists(Name_py_Distance_dict_CR1)):

        # Get river and DEM dict
        River_dict_CR1, DEM_dict_CR1, Distance_dict_CR1 = Five.Create_Dict.Rivers_General(
            Name_NC_DEM_CR, Name_NC_DEM_Dir_CR, Name_NC_Acc_Pixels_CR,
            Name_NC_Rivers_CR, Example_dataset)
        np.save(Name_py_River_dict_CR1, River_dict_CR1)
        np.save(Name_py_DEM_dict_CR1, DEM_dict_CR1)
        np.save(Name_py_Distance_dict_CR1, Distance_dict_CR1)
    else:
        # Load
        River_dict_CR1 = np.load(Name_py_River_dict_CR1).item()
        DEM_dict_CR1 = np.load(Name_py_DEM_dict_CR1).item()
        Distance_dict_CR1 = np.load(Name_py_Distance_dict_CR1).item()

    Name_py_Discharge_dict_CR1 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'Discharge_dict_CR1_simulation%d.npy' % (Simulation))

    if not os.path.exists(Name_py_Discharge_dict_CR1):
        # Get discharge dict
        Discharge_dict_CR1 = Five.Create_Dict.Discharge(
            Name_NC_Discharge_CR1, River_dict_CR1, Amount_months,
            Example_dataset)
        np.save(Name_py_Discharge_dict_CR1, Discharge_dict_CR1)
    else:
        # Load
        Discharge_dict_CR1 = np.load(Name_py_Discharge_dict_CR1).item()

    ###################### 7.2 Calculate surface water storage characteristics ######################

    Name_py_Discharge_dict_CR2 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'Discharge_dict_CR2_simulation%d.npy' % (Simulation))
    Name_py_River_dict_CR2 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'River_dict_CR2_simulation%d.npy' % (Simulation))
    Name_py_DEM_dict_CR2 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'DEM_dict_CR2_simulation%d.npy' % (Simulation))
    Name_py_Distance_dict_CR2 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'Distance_dict_CR2_simulation%d.npy' % (Simulation))
    Name_py_Diff_Water_Volume = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'Diff_Water_Volume_CR2_simulation%d.npy' % (Simulation))
    Name_py_Regions = os.path.join(Dir_Basin, 'Simulations',
                                   'Simulation_%d' % Simulation, 'Sheet_5',
                                   'Regions_simulation%d.npy' % (Simulation))

    if not (os.path.exists(Name_py_Discharge_dict_CR2)
            and os.path.exists(Name_py_River_dict_CR2)
            and os.path.exists(Name_py_DEM_dict_CR2)
            and os.path.exists(Name_py_Distance_dict_CR2)):

        # Copy dicts as starting adding reservoir
        Discharge_dict_CR2 = copy.deepcopy(Discharge_dict_CR1)
        River_dict_CR2 = copy.deepcopy(River_dict_CR1)
        DEM_dict_CR2 = copy.deepcopy(DEM_dict_CR1)
        Distance_dict_CR2 = copy.deepcopy(Distance_dict_CR1)

        if Reservoirs_Lakes_Calculations == 1:

            # define input tiffs for surface water calculations
            input_JRC = os.path.join(Dir_Basin, Data_Path_JRC_occurrence,
                                     'JRC_Occurrence_percent.tif')
            DEM_dataset = os.path.join(Dir_Basin, Data_Path_DEM,
                                       'DEM_HydroShed_m_3s.tif')

            sensitivity = 700  # 900 is less sensitive 1 is very sensitive
            Regions = Five.Reservoirs.Calc_Regions(Name_NC_Basin_CR, input_JRC,
                                                   sensitivity, Boundaries)

            Diff_Water_Volume = np.zeros(
                [len(Regions), Amount_months_reservoirs - 1, 3])
            reservoir = 0

            for region in Regions:

                popt = Five.Reservoirs.Find_Area_Volume_Relation(
                    region, input_JRC, DEM_dataset)

                Area_Reservoir_Values = Five.Reservoirs.GEE_calc_reservoir_area(
                    region, Startdate, Enddate)

                Diff_Water_Volume[
                    reservoir, :, :] = Five.Reservoirs.Calc_Diff_Storage(
                        Area_Reservoir_Values, popt)
                reservoir += 1

            ################# 7.3 Add storage reservoirs and change outflows ##################
            Discharge_dict_CR2, River_dict_CR2, DEM_dict_CR2, Distance_dict_CR2 = Five.Reservoirs.Add_Reservoirs(
                Name_NC_Rivers_CR, Name_NC_Acc_Pixels_CR, Diff_Water_Volume,
                River_dict_CR2, Discharge_dict_CR2, DEM_dict_CR2,
                Distance_dict_CR2, Regions, Example_dataset)

            np.save(Name_py_Regions, Regions)
            np.save(Name_py_Diff_Water_Volume, Diff_Water_Volume)

        np.save(Name_py_Discharge_dict_CR2, Discharge_dict_CR2)
        np.save(Name_py_River_dict_CR2, River_dict_CR2)
        np.save(Name_py_DEM_dict_CR2, DEM_dict_CR2)
        np.save(Name_py_Distance_dict_CR2, Distance_dict_CR2)

    else:
        # Load
        Discharge_dict_CR2 = np.load(Name_py_Discharge_dict_CR2).item()
        River_dict_CR2 = np.load(Name_py_River_dict_CR2).item()
        DEM_dict_CR2 = np.load(Name_py_DEM_dict_CR2).item()
        Distance_dict_CR2 = np.load(Name_py_Distance_dict_CR2).item()

    ####################### 7.3 Add surface water withdrawals #############################

    Name_py_Discharge_dict_CR3 = os.path.join(
        Dir_Basin, 'Simulations', 'Simulation_%d' % Simulation, 'Sheet_5',
        'Discharge_dict_CR3_simulation%d.npy' % (Simulation))

    if not os.path.exists(Name_py_Discharge_dict_CR3):

        Discharge_dict_CR3, DataCube_ETblue_m3 = Five.Irrigation.Add_irrigation(
            Discharge_dict_CR2, River_dict_CR2, Name_NC_Rivers_CR,
            Name_NC_ET_CR, Name_NC_ETref_CR, Name_NC_Prec_CR, Name_NC_Basin_CR,
            Name_NC_frac_sw_CR, Startdate, Enddate, Example_dataset)
        np.save(Name_py_Discharge_dict_CR3, Discharge_dict_CR3)

        # save ETblue as nc
        info = [
            'monthly', 'm3-month-1', ''.join([Startdate[5:7], Startdate[0:4]]),
            ''.join([Enddate[5:7], Enddate[0:4]])
        ]
        Name_NC_ETblue = DC.Create_NC_name('ETblue', Simulation, Dir_Basin, 5,
                                           info)
        DC.Save_as_NC(Name_NC_ETblue, DataCube_ETblue_m3, 'ETblue',
                      Example_dataset, Startdate, Enddate, 'monthly')

    else:
        Discharge_dict_CR3 = np.load(Name_py_Discharge_dict_CR3).item()

    ################################# 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('Discharge', 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',
                      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 ()
예제 #3
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 wa.General import raster_conversions as RC
    from wa.General import data_conversions as DC
    import wa.Functions.Five as Five
    import wa.Functions.Start as Start
    import wa.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(Moving_Window_Per_Class_dict.values())

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

    # Check the years that needs to be calculated
    years = 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 _______________________

        DataCube_frac_sw = np.ones([size_Y, size_X]) * np.nan

        import wa.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 = 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 wa.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()
예제 #4
0
파일: main.py 프로젝트: gikon1/wa
def Calculate(Basin, 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 ###################################

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

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

    # 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)
    if not os.path.exists(Dir_Basin):
        os.makedirs(Dir_Basin)

    # 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)

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

    # Set the NPP and GPP data for the whole year
    StartYear = Startdate[:4]
    EndYear = Enddate[:4]
    StartdateNDM = '%d-01-01' % int(StartYear)
    EnddateNDM = '%d-12-31' % int(EndYear)

    # Download data
    Data_Path_P = Start.Download_Data.Precipitation(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate, Enddate,
        P_Product)
    Data_Path_ET = Start.Download_Data.Evapotranspiration(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate, Enddate,
        ET_Product)
    Data_Path_LAI = Start.Download_Data.LAI(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate, Enddate,
        LAI_Product)

    if NDM_Product == 'MOD17':
        Data_Path_NPP = Start.Download_Data.NPP(
            Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
            [Boundaries['Lonmin'], Boundaries['Lonmax']], StartdateNDM,
            EnddateNDM, NDM_Product)
        Data_Path_GPP = Start.Download_Data.GPP(
            Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
            [Boundaries['Lonmin'], Boundaries['Lonmax']], StartdateNDM,
            EnddateNDM, NDM_Product)

    Data_Path_P_Daily = os.path.join(Data_Path_P, 'Daily')
    Data_Path_P_Monthly = os.path.join(Data_Path_P, 'Monthly')

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

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

    # Create monthly LAI and GPP
    Dir_path_LAI = os.path.join(Dir_Basin, Data_Path_LAI)
    Start.Eightdaily_to_monthly_state.Nearest_Interpolate(
        Dir_path_LAI, Startdate, Enddate)
    Dir_path_GPP = os.path.join(Dir_Basin, Data_Path_GPP)
    Start.Eightdaily_to_monthly_state.Nearest_Interpolate(
        Dir_path_GPP, StartdateNDM, EnddateNDM)

    # Create NDM based on MOD17
    if NDM_Product == 'MOD17':
        Data_Path_NDM = Two.Calc_NDM.NPP_GPP_Based(Dir_Basin, Data_Path_GPP,
                                                   Data_Path_NPP, Startdate,
                                                   Enddate)

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

    #___________________________________Land Use_______________________________

    # Get the data of LU and save as nc, This dataset is also used as reference for others
    LUdest = gdal.Open(Example_dataset)
    DataCube_LU = LUdest.GetRasterBand(1).ReadAsArray()

    Name_NC_LU = DC.Create_NC_name('LU', Simulation, Dir_Basin, 2)
    if not os.path.exists(Name_NC_LU):
        DC.Save_as_NC(Name_NC_LU, DataCube_LU, 'LU', Example_dataset)

    LUdest = None
    del DataCube_LU

    #______________________________Precipitation_______________________________

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

    # Precipitation data
    Name_NC_P = DC.Create_NC_name('Prec', Simulation, Dir_Basin, 2, info)
    if not os.path.exists(Name_NC_P):

        # Get the data of Precipitation and save as nc
        DataCube_Prec = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_P_Monthly,
            Startdate,
            Enddate,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_P, DataCube_Prec, 'Prec', Example_dataset,
                      Startdate, Enddate, 'monthly', 0.01)
        del DataCube_Prec

    #_______________________________Evaporation________________________________

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

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

    #___________________________Normalized Dry Matter__________________________

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

    Name_NC_NDM = DC.Create_NC_name('NDM', Simulation, Dir_Basin, 2, info)
    if not os.path.exists(Name_NC_NDM):

        # Get the data of Evaporation and save as nc
        DataCube_NDM = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_NDM,
            Startdate,
            Enddate,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_NDM, DataCube_NDM, 'NDM', Example_dataset,
                      Startdate, Enddate, 'monthly', 100)
        del DataCube_NDM

    #_______________________________Rainy Days_________________________________

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

    Name_NC_RD = DC.Create_NC_name('RD', Simulation, Dir_Basin, 2, info)
    if not os.path.exists(Name_NC_RD):

        # Get the data of Evaporation and save as nc
        DataCube_RD = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_RD,
            Startdate,
            Enddate,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_RD, DataCube_RD, 'RD', Example_dataset,
                      Startdate, Enddate, 'monthly', 100)
        del DataCube_RD

    #_______________________________Leaf Area Index____________________________

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

    Name_NC_LAI = DC.Create_NC_name('LAI', Simulation, Dir_Basin, 2, info)
    if not os.path.exists(Name_NC_LAI):

        # Get the data of Evaporation and save as nc
        DataCube_LAI = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_LAI,
            Startdate,
            Enddate,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_LAI, DataCube_LAI, 'LAI', Example_dataset,
                      Startdate, Enddate, 'monthly', 1)
        del DataCube_LAI

    ####################### Calculations Sheet 2 ##############################

    DataCube_I, DataCube_T, DataCube_E = Two.SplitET.ITE(
        Dir_Basin, Name_NC_ET, Name_NC_LAI, Name_NC_P, Name_NC_RD, Name_NC_NDM,
        Name_NC_LU, Startdate, Enddate, Simulation)

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

    Dir_Basin_CSV = Generate.CSV.Create(Dir_Basin, Simulation, Basin,
                                        Startdate, Enddate, Name_NC_LU,
                                        DataCube_I, DataCube_T, DataCube_E,
                                        Example_dataset)

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

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

    return ()
예제 #5
0
파일: main.py 프로젝트: wateraccounting/wa
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 wa.General import raster_conversions as RC
    from wa.General import data_conversions as DC
    import wa.Functions.Four as Four
    import wa.Functions.Start as Start
    import wa.Generator.Sheet4 as Generate
    import wa.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(Moving_Window_Per_Class_dict.values())

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

    # Check the years that needs to be calculated
    years = 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

        ############################# 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

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

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

    years = 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()
예제 #6
0
파일: SplitET.py 프로젝트: ali1100/wa
def Complete_3D_Array(nc_outname, Var, Startdate, Enddate,
                      Additional_Months_front, Additional_Months_tail,
                      Data_Path):

    from netCDF4 import Dataset
    import wa.General.raster_conversions as RC

    # Define startdate and enddate with moving average
    Startdate_Moving_Average = pd.Timestamp(Startdate) - pd.DateOffset(
        months=Additional_Months_front)
    Enddate_Moving_Average = pd.Timestamp(Enddate) + pd.DateOffset(
        months=Additional_Months_tail)
    Startdate_Moving_Average_String = '%d-%02d-%02d' % (
        Startdate_Moving_Average.year, Startdate_Moving_Average.month,
        Startdate_Moving_Average.day)
    Enddate_Moving_Average_String = '%d-%02d-%02d' % (
        Enddate_Moving_Average.year, Enddate_Moving_Average.month,
        Enddate_Moving_Average.day)

    # Extract moving average period before
    Year_front = int(Startdate_Moving_Average.year)
    filename_front = os.path.join(os.path.dirname(nc_outname),
                                  "%d.nc" % Year_front)
    Enddate_Front = pd.Timestamp(Startdate) - pd.DateOffset(days=1)

    # Extract inside start and enddate
    Array_main = RC.Open_nc_array(nc_outname, Var, Startdate, Enddate)

    if Additional_Months_front > 0:

        # Extract moving average period before
        if os.path.exists(filename_front):

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

            if Var in Variables_NC:
                Array_front = RC.Open_nc_array(
                    filename_front, Var, Startdate_Moving_Average_String,
                    Enddate_Front)
            else:
                Array_front = RC.Get3Darray_time_series_monthly(
                    Data_Path, Startdate_Moving_Average_String, Enddate_Front,
                    nc_outname)

        else:
            Array_front = RC.Get3Darray_time_series_monthly(
                Data_Path, Startdate_Moving_Average_String, Enddate_Front,
                nc_outname)

        # Merge dataset
        Array_main = np.vstack([Array_front, Array_main])

    if Additional_Months_tail > 0:

        # Extract moving average period after
        Year_tail = int(Enddate_Moving_Average.year)
        filename_tail = os.path.join(os.path.dirname(nc_outname),
                                     "%d.nc" % Year_tail)
        Startdate_tail = pd.Timestamp(Enddate) + pd.DateOffset(days=1)

        # Extract moving average period after
        if os.path.exists(filename_tail):

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

            if Var in Variables_NC:
                Array_tail = RC.Open_nc_array(filename_tail, Var,
                                              Startdate_tail,
                                              Enddate_Moving_Average_String)
            else:
                Array_tail = RC.Get3Darray_time_series_monthly(
                    Data_Path, Startdate_tail, Enddate_Moving_Average_String,
                    nc_outname)

        else:
            Array_tail = RC.Get3Darray_time_series_monthly(
                Data_Path, Startdate_tail, Enddate_Moving_Average_String,
                nc_outname)

        # Merge dataset
        Array_main = np.vstack([Array_main, Array_tail])

    return (Array_main)
예제 #7
0
파일: main.py 프로젝트: wateraccounting/wa
def Calculate(WA_HOME_folder, Basin, 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 wa.General import raster_conversions as RC
    from wa.General import data_conversions as DC
    import wa.Functions.Two as Two
    import wa.Functions.Start as Start
    import wa.Generator.Sheet2 as Generate

    ######################### 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

    # 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)

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

    # Check the years that needs to be calculated
    years = 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()
예제 #8
0
def main(files_DEM_dir, files_DEM, files_Basin, files_Runoff, files_Extraction,
         startdate, enddate, input_nc, resolution, Format_DEM_dir, Format_DEM,
         Format_Basin, Format_Runoff, Format_Extraction):

    # Define a year to get the epsg and geo
    Startdate_timestamp = pd.Timestamp(startdate)
    year = Startdate_timestamp.year

    ############################## Drainage Direction #####################################

    # Open Array DEM dir as netCDF
    if Format_DEM_dir == "NetCDF":
        file_DEM_dir = os.path.join(files_DEM_dir, "%d.nc" % year)
        DataCube_DEM_dir = RC.Open_nc_array(file_DEM_dir, "Drainage_Direction")
        geo_out_example, epsg_example, size_X_example, size_Y_example, size_Z_example, Time_example = RC.Open_nc_info(
            files_DEM_dir)

        # Create memory file for reprojection
        gland = DC.Save_as_MEM(DataCube_DEM_dir, geo_out_example, epsg_example)
        dataset_example = file_name_DEM_dir = gland

    # Open Array DEM dir as TIFF
    if Format_DEM_dir == "TIFF":
        file_name_DEM_dir = os.path.join(files_DEM_dir,
                                         "DIR_HydroShed_-_%s.tif" % resolution)
        DataCube_DEM_dir = RC.Open_tiff_array(file_name_DEM_dir)
        geo_out_example, epsg_example, size_X_example, size_Y_example = RC.Open_array_info(
            file_name_DEM_dir)
        dataset_example = file_name_DEM_dir

    # Calculate Area per pixel in m2
    import wa.Functions.Start.Area_converter as AC
    DataCube_Area = AC.Degrees_to_m2(file_name_DEM_dir)

    ################################## DEM ##########################################

    # Open Array DEM as netCDF
    if Format_DEM == "NetCDF":
        file_DEM = os.path.join(files_DEM, "%d.nc" % year)
        DataCube_DEM = RC.Open_nc_array(file_DEM, "Elevation")

    # Open Array DEM as TIFF
    if Format_DEM == "TIFF":
        file_name_DEM = os.path.join(files_DEM,
                                     "DEM_HydroShed_m_%s.tif" % resolution)
        DataCube_DEM = RC.Open_tiff_array(file_name_DEM)

    ################################ Landuse ##########################################

    # Open Array Basin as netCDF
    if Format_Basin == "NetCDF":
        file_Basin = os.path.join(files_Basin, "%d.nc" % year)
        DataCube_Basin = RC.Open_nc_array(file_Basin, "Landuse")
        geo_out, epsg, size_X, size_Y, size_Z, Time = RC.Open_nc_info(
            file_Basin, "Landuse")
        dest_basin = DC.Save_as_MEM(DataCube_Basin, geo_out, str(epsg))
        destLU = RC.reproject_dataset_example(dest_basin,
                                              dataset_example,
                                              method=1)
        DataCube_LU_CR = destLU.GetRasterBand(1).ReadAsArray()
        DataCube_Basin = np.zeros([size_Y_example, size_X_example])
        DataCube_Basin[DataCube_LU_CR > 0] = 1

    # Open Array Basin as TIFF
    if Format_Basin == "TIFF":
        file_name_Basin = files_Basin
        destLU = RC.reproject_dataset_example(file_name_Basin,
                                              dataset_example,
                                              method=1)
        DataCube_LU_CR = destLU.GetRasterBand(1).ReadAsArray()
        DataCube_Basin = np.zeros([size_Y_example, size_X_example])
        DataCube_Basin[DataCube_LU_CR > 0] = 1

    ################################ Surface Runoff ##########################################

    # Open Array runoff as netCDF
    if Format_Runoff == "NetCDF":
        DataCube_Runoff = RC.Open_ncs_array(files_Runoff, "Surface_Runoff",
                                            startdate, enddate)
        size_Z_example = DataCube_Runoff.shape[0]
        file_Runoff = os.path.join(files_Runoff, "%d.nc" % year)
        geo_out, epsg, size_X, size_Y, size_Z, Time = RC.Open_nc_info(
            file_Runoff, "Surface_Runoff")
        DataCube_Runoff_CR = np.ones(
            [size_Z_example, size_Y_example, size_X_example]) * np.nan
        for i in range(0, size_Z):
            DataCube_Runoff_one = DataCube_Runoff[i, :, :]
            dest_Runoff_one = DC.Save_as_MEM(DataCube_Runoff_one, geo_out,
                                             str(epsg))
            dest_Runoff = RC.reproject_dataset_example(dest_Runoff_one,
                                                       dataset_example,
                                                       method=4)
            DataCube_Runoff_CR[i, :, :] = dest_Runoff.GetRasterBand(
                1).ReadAsArray()

        DataCube_Runoff_CR[:, DataCube_LU_CR == 0] = -9999
        DataCube_Runoff_CR[DataCube_Runoff_CR < 0] = -9999

    # Open Array runoff as TIFF
    if Format_Runoff == "TIFF":
        Data_Path = ''
        DataCube_Runoff = RC.Get3Darray_time_series_monthly(
            files_Runoff,
            Data_Path,
            startdate,
            enddate,
            Example_data=dataset_example)

    ################################ Surface Withdrawal ##########################################

    # Open Array Extraction as netCDF
    if Format_Extraction == "NetCDF":
        DataCube_Extraction = RC.Open_ncs_array(files_Extraction,
                                                "Surface_Withdrawal",
                                                startdate, enddate)
        size_Z_example = DataCube_Extraction.shape[0]
        file_Extraction = os.path.join(files_Extraction, "%d.nc" % year)
        geo_out, epsg, size_X, size_Y, size_Z, Time = RC.Open_nc_info(
            file_Extraction, "Surface_Withdrawal")
        DataCube_Extraction_CR = np.ones(
            [size_Z_example, size_Y_example, size_X_example]) * np.nan
        for i in range(0, size_Z):
            DataCube_Extraction_one = DataCube_Extraction[i, :, :]
            dest_Extraction_one = DC.Save_as_MEM(DataCube_Extraction_one,
                                                 geo_out, str(epsg))
            dest_Extraction = RC.reproject_dataset_example(dest_Extraction_one,
                                                           dataset_example,
                                                           method=4)
            DataCube_Extraction_CR[i, :, :] = dest_Extraction.GetRasterBand(
                1).ReadAsArray()

        DataCube_Extraction_CR[:, DataCube_LU_CR == 0] = -9999
        DataCube_Extraction_CR[DataCube_Extraction_CR < 0] = -9999

    # Open Array Extraction as TIFF
    if Format_Extraction == "TIFF":
        Data_Path = ''
        DataCube_Extraction = RC.Get3Darray_time_series_monthly(
            files_Extraction,
            Data_Path,
            startdate,
            enddate,
            Example_data=dataset_example)

    ################################ Create input netcdf ##########################################
    # Save data in one NetCDF file
    geo_out_example = np.array(geo_out_example)

    # Latitude and longitude
    lon_ls = np.arange(size_X_example) * geo_out_example[1] + geo_out_example[
        0] + 0.5 * geo_out_example[1]
    lat_ls = np.arange(size_Y_example) * geo_out_example[5] + geo_out_example[
        3] - 0.5 * geo_out_example[5]

    lat_n = len(lat_ls)
    lon_n = len(lon_ls)

    # Create NetCDF file
    nc_file = netCDF4.Dataset(input_nc, 'w')
    nc_file.set_fill_on()

    # Create dimensions
    lat_dim = nc_file.createDimension('latitude', lat_n)
    lon_dim = nc_file.createDimension('longitude', lon_n)

    # Create NetCDF variables
    crso = nc_file.createVariable('crs', 'i4')
    crso.long_name = 'Lon/Lat Coords in WGS84'
    crso.standard_name = 'crs'
    crso.grid_mapping_name = 'latitude_longitude'
    crso.projection = epsg_example
    crso.longitude_of_prime_meridian = 0.0
    crso.semi_major_axis = 6378137.0
    crso.inverse_flattening = 298.257223563
    crso.geo_reference = geo_out_example

    lat_var = nc_file.createVariable('latitude', 'f8', ('latitude', ))
    lat_var.units = 'degrees_north'
    lat_var.standard_name = 'latitude'
    lat_var.pixel_size = geo_out_example[5]

    lon_var = nc_file.createVariable('longitude', 'f8', ('longitude', ))
    lon_var.units = 'degrees_east'
    lon_var.standard_name = 'longitude'
    lon_var.pixel_size = geo_out_example[1]

    Dates = pd.date_range(startdate, enddate, freq='MS')
    time_or = np.zeros(len(Dates))
    i = 0
    for Date in Dates:
        time_or[i] = Date.toordinal()
        i += 1
    nc_file.createDimension('time', None)
    timeo = nc_file.createVariable('time', 'f4', ('time', ))
    timeo.units = 'Monthly'
    timeo.standard_name = 'time'

    # Variables
    demdir_var = nc_file.createVariable('demdir',
                                        'i', ('latitude', 'longitude'),
                                        fill_value=-9999)
    demdir_var.long_name = 'Flow Direction Map'
    demdir_var.grid_mapping = 'crs'

    dem_var = nc_file.createVariable('dem',
                                     'f8', ('latitude', 'longitude'),
                                     fill_value=-9999)
    dem_var.long_name = 'Altitude'
    dem_var.units = 'meters'
    dem_var.grid_mapping = 'crs'

    basin_var = nc_file.createVariable('basin',
                                       'i', ('latitude', 'longitude'),
                                       fill_value=-9999)
    basin_var.long_name = 'Altitude'
    basin_var.units = 'meters'
    basin_var.grid_mapping = 'crs'

    area_var = nc_file.createVariable('area',
                                      'f8', ('latitude', 'longitude'),
                                      fill_value=-9999)
    area_var.long_name = 'area in squared meters'
    area_var.units = 'squared_meters'
    area_var.grid_mapping = 'crs'

    runoff_var = nc_file.createVariable('Runoff_M',
                                        'f8',
                                        ('time', 'latitude', 'longitude'),
                                        fill_value=-9999)
    runoff_var.long_name = 'Runoff'
    runoff_var.units = 'm3/month'
    runoff_var.grid_mapping = 'crs'

    extraction_var = nc_file.createVariable('Extraction_M',
                                            'f8',
                                            ('time', 'latitude', 'longitude'),
                                            fill_value=-9999)
    extraction_var.long_name = 'Surface water Extraction'
    extraction_var.units = 'm3/month'
    extraction_var.grid_mapping = 'crs'

    # Load data
    lat_var[:] = lat_ls
    lon_var[:] = lon_ls
    timeo[:] = time_or

    # Static variables
    demdir_var[:, :] = DataCube_DEM_dir[:, :]
    dem_var[:, :] = DataCube_DEM[:, :]
    basin_var[:, :] = DataCube_Basin[:, :]
    area_var[:, :] = DataCube_Area[:, :]
    for i in range(len(Dates)):
        runoff_var[i, :, :] = DataCube_Runoff_CR[i, :, :]
    for i in range(len(Dates)):
        extraction_var[i, :, :] = DataCube_Extraction_CR[i, :, :]

    # Close file
    nc_file.close()
    return ()
예제 #9
0
파일: main.py 프로젝트: gikon1/wa
def Calculate(Basin, P_Product, ET_Product, Moving_Averaging_Length, 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 
    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 wa.General import raster_conversions as RC
    from wa.General import data_conversions as DC
    import wa.Functions.Four as Four
    import wa.Functions.Start as Start
    import wa.Generator.Sheet4 as Generate

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

    # 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)
    if not os.path.exists(Dir_Basin):
        os.makedirs(Dir_Basin)

    # 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)

    #Set Startdate and Enddate for moving average
    Additional_Months = (Moving_Averaging_Length - 1) / 2
    Startdate_Moving_Average = pd.Timestamp(Startdate) - pd.DateOffset(
        months=Additional_Months)
    Enddate_Moving_Average = pd.Timestamp(Enddate) + pd.DateOffset(
        months=Additional_Months)
    Startdate_Moving_Average_String = '%d-%02d-%02d' % (
        Startdate_Moving_Average.year, Startdate_Moving_Average.month,
        Startdate_Moving_Average.day)
    Enddate_Moving_Average_String = '%d-%02d-%02d' % (
        Enddate_Moving_Average.year, Enddate_Moving_Average.month,
        Enddate_Moving_Average.day)

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

    # Download data
    Data_Path_P = Start.Download_Data.Precipitation(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']],
        Startdate_Moving_Average_String,
        Enddate_Moving_Average_String,
        P_Product,
        Daily='n')
    Data_Path_ET = Start.Download_Data.Evapotranspiration(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']], Startdate, Enddate,
        ET_Product)
    Data_Path_ETref = Start.Download_Data.ETreference(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']],
        Startdate_Moving_Average_String, Enddate_Moving_Average_String)
    Data_Path_GWF = Start.Download_Data.GWF(
        Dir_Basin, [Boundaries['Latmin'], Boundaries['Latmax']],
        [Boundaries['Lonmin'], Boundaries['Lonmax']])

    Data_Path_P_Monthly = os.path.join(Data_Path_P, 'Monthly')

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

    #___________________________________Land Use_______________________________

    # Get the data of LU and save as nc, This dataset is also used as reference for others
    LUdest = gdal.Open(Example_dataset)
    DataCube_LU = LUdest.GetRasterBand(1).ReadAsArray()

    Name_NC_LU = DC.Create_NC_name('LU', Simulation, Dir_Basin, 4)
    if not os.path.exists(Name_NC_LU):
        DC.Save_as_NC(Name_NC_LU, DataCube_LU, 'LU', Example_dataset)

    LUdest = None
    del DataCube_LU

    #______________________________Precipitation_______________________________

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

    # Precipitation data
    Name_NC_P = DC.Create_NC_name('Prec', Simulation, Dir_Basin, 4, info)
    if not os.path.exists(Name_NC_P):

        # Get the data of Precipitation and save as nc
        DataCube_Prec = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_P_Monthly,
            Startdate_Moving_Average_String,
            Enddate_Moving_Average_String,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_P, DataCube_Prec, 'Prec', Example_dataset,
                      Startdate_Moving_Average_String,
                      Enddate_Moving_Average_String, 'monthly', 0.01)
        del DataCube_Prec

#_______________________Reference Evaporation______________________________

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

        # Get the data of Evaporation and save as nc
        DataCube_ETref = RC.Get3Darray_time_series_monthly(
            Dir_Basin,
            Data_Path_ETref,
            Startdate_Moving_Average_String,
            Enddate_Moving_Average_String,
            Example_data=Example_dataset)
        DC.Save_as_NC(Name_NC_ETref, DataCube_ETref, 'ETref', Example_dataset,
                      Startdate_Moving_Average_String,
                      Enddate_Moving_Average_String, 'monthly', 0.01)
        del DataCube_ETref

    #_______________________________Evaporation________________________________
    info = [
        'monthly', 'mm', ''.join([Startdate[5:7], Startdate[0:4]]),
        ''.join([Enddate[5:7], Enddate[0:4]])
    ]

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

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

    #_____________________________________GWF__________________________________

    # GWF data
    Name_NC_GWF = DC.Create_NC_name('GWF_Fraction', Simulation, Dir_Basin, 4)
    if not os.path.exists(Name_NC_GWF):

        # Get the data of GWF, reproject 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.Save_as_NC(Name_NC_GWF,
                      DataCube_GWF,
                      'GWF',
                      Example_dataset,
                      Scaling_factor=0.01)
        del DataCube_GWF

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

    #____________ Evapotranspiration data split in ETblue and ETgreen ____________

    Name_NC_ETgreen = DC.Create_NC_name('ETgreen', Simulation, Dir_Basin, 4,
                                        info)
    Name_NC_ETblue = DC.Create_NC_name('ETblue', Simulation, Dir_Basin, 4,
                                       info)

    if not (os.path.exists(Name_NC_ETgreen) or os.path.exists(Name_NC_ETblue)):

        # Calculate Blue and Green ET
        DataCube_ETblue, DataCube_ETgreen = Four.SplitET.Blue_Green(
            Name_NC_ET, Name_NC_P, Name_NC_ETref, Startdate, Enddate,
            Additional_Months)

        # Save the ETblue and ETgreen data as NetCDF files
        DC.Save_as_NC(Name_NC_ETblue, DataCube_ETblue, 'ETblue',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)
        DC.Save_as_NC(Name_NC_ETgreen, DataCube_ETgreen, 'ETgreen',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)

        del DataCube_ETblue, DataCube_ETgreen

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

    Name_NC_Total_Supply = DC.Create_NC_name('TotSup', Simulation, Dir_Basin,
                                             4, info)
    Name_NC_Non_Consumed = DC.Create_NC_name('NonCon', Simulation, Dir_Basin,
                                             4, info)

    if not (os.path.exists(Name_NC_Total_Supply)
            or os.path.exists(Name_NC_Non_Consumed)):

        # Do the calculations
        DataCube_Total_Supply, DataCube_Non_Consumed = Four.Total_Supply.Fraction_Based(
            Name_NC_ETblue, Name_NC_LU, Startdate, Enddate)

        # Save the Total Supply and non consumed data as NetCDF files
        DC.Save_as_NC(Name_NC_Total_Supply, DataCube_Total_Supply, 'TotSup',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)
        DC.Save_as_NC(Name_NC_Non_Consumed, DataCube_Non_Consumed, 'NonCon',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)
        del DataCube_Total_Supply, DataCube_Non_Consumed

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

    Name_NC_Total_Supply_SW = DC.Create_NC_name('TotSupSW', Simulation,
                                                Dir_Basin, 4, info)
    Name_NC_Total_Supply_GW = DC.Create_NC_name('TotSupGW', Simulation,
                                                Dir_Basin, 4, info)

    if not (os.path.exists(Name_NC_Total_Supply_SW)
            or os.path.exists(Name_NC_Total_Supply_GW)):

        # Do the calculations
        DataCube_Total_Supply_SW, DataCube_Total_Supply_GW = Four.SplitGW_SW_Supply.Fraction_Based(
            Name_NC_Total_Supply, Name_NC_LU, Startdate, Enddate)

        # Save the Total Supply surface water and Total Supply ground water data as NetCDF files
        DC.Save_as_NC(Name_NC_Total_Supply_SW, DataCube_Total_Supply_SW,
                      'TotSupSW', Example_dataset, Startdate, Enddate,
                      'monthly', 0.01)
        DC.Save_as_NC(Name_NC_Total_Supply_GW, DataCube_Total_Supply_GW,
                      'TotSupGW', Example_dataset, Startdate, Enddate,
                      'monthly', 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 ____________

    Name_NC_NonRecovableFlow = DC.Create_NC_name('NonRecov', Simulation,
                                                 Dir_Basin, 4, info)
    Name_NC_RecovableFlow = DC.Create_NC_name('Recov', Simulation, Dir_Basin,
                                              4, info)

    if not (os.path.exists(Name_NC_NonRecovableFlow)
            or os.path.exists(Name_NC_RecovableFlow)):

        # Calculate the non recovable flow and recovable flow by using Grey Water Footprint values
        DataCube_NonRecovableFlow, Datacube_RecovableFlow = Four.SplitNonConsumed_NonRecov.GWF_Based(
            Name_NC_Non_Consumed, Name_NC_GWF, Name_NC_LU, Startdate, Enddate)

        # Get the data of Evaporation and save as nc
        DC.Save_as_NC(Name_NC_NonRecovableFlow, DataCube_NonRecovableFlow,
                      'NonRecov', Example_dataset, Startdate, Enddate,
                      'monthly', 0.01)
        DC.Save_as_NC(Name_NC_RecovableFlow, Datacube_RecovableFlow, 'Recov',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)
        del DataCube_NonRecovableFlow, Datacube_RecovableFlow

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

    # 1. Non recovarable flow
    Name_NC_NonRecovableFlow_Return_GW = DC.Create_NC_name(
        'NonRecov_Return_GW', Simulation, Dir_Basin, 4, info)
    Name_NC_NonRecovableFlow_Return_SW = DC.Create_NC_name(
        'NonRecov_Return_SW', Simulation, Dir_Basin, 4, info)

    if not (os.path.exists(Name_NC_NonRecovableFlow_Return_GW)
            or os.path.exists(Name_NC_NonRecovableFlow_Return_SW)):

        # 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(
            Name_NC_NonRecovableFlow, Name_NC_LU, Startdate, Enddate)

        # Get the data of Evaporation and save as nc
        DC.Save_as_NC(Name_NC_NonRecovableFlow_Return_GW,
                      DataCube_NonRecovableFlow_Return_GW, 'NonRecovReturnGW',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)
        DC.Save_as_NC(Name_NC_NonRecovableFlow_Return_SW,
                      Datacube_NonRecovableFlow_Return_SW, 'NonRecovReturnSW',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)
        del DataCube_NonRecovableFlow_Return_GW, Datacube_NonRecovableFlow_Return_SW

    # 2. Recovarable flow
    Name_NC_RecovableFlow_Return_GW = DC.Create_NC_name(
        'Recov_Return_GW', Simulation, Dir_Basin, 4, info)
    Name_NC_RecovableFlow_Return_SW = DC.Create_NC_name(
        'Recov_Return_SW', Simulation, Dir_Basin, 4, info)

    if not (os.path.exists(Name_NC_RecovableFlow_Return_GW)
            or os.path.exists(Name_NC_RecovableFlow_Return_SW)):

        # 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(
            Name_NC_RecovableFlow, Name_NC_LU, Startdate, Enddate)

        # Get the data of Evaporation and save as nc
        DC.Save_as_NC(Name_NC_RecovableFlow_Return_GW,
                      DataCube_RecovableFlow_Return_GW, 'NonRecovReturnGW',
                      Example_dataset, Startdate, Enddate, 'monthly', 0.01)
        DC.Save_as_NC(Name_NC_RecovableFlow_Return_SW,
                      Datacube_RecovableFlow_Return_SW, 'NonRecovReturnSW',
                      Example_dataset, Startdate, Enddate, 'monthly', 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, Enddate, Name_NC_LU,
        Name_NC_Total_Supply_GW, Name_NC_Total_Supply_SW, Name_NC_Non_Consumed,
        Name_NC_ETblue, Name_NC_RecovableFlow_Return_GW,
        Name_NC_RecovableFlow_Return_SW, Name_NC_NonRecovableFlow_Return_GW,
        Name_NC_NonRecovableFlow_Return_SW)

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

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

    return ()