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

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

    # import WA plus modules
    from 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()
Esempio n. 3
0
def Calculate(WA_HOME_folder, Basin, P_Product, ET_Product, LAI_Product, ETref_Product, Runoff_Product, Startdate, Enddate, Simulation):
    """
    This functions is the main framework for calculating sheet 4.

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

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

    from 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()
Esempio n. 4
0
def Calculate(WA_HOME_folder, Basin, P_Product, ET_Product, LAI_Product, NDM_Product, NDVI_Product, ETref_Product, dict_crops, dict_non_crops, Startdate, Enddate, Simulation):
    """
    This functions is the main framework for calculating sheet 3.

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

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

    from 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()
Esempio n. 5
0
def Blue_Green(Dir_Basin, nc_outname, ETref_Product, P_Product, Startdate,
               Enddate):
    """
    This functions split the evapotranspiration into green and blue evapotranspiration.
    Parameters
    ----------
    nc_outname : str
        Path to the .nc file containing data
    Moving_Averaging_Length: integer
        Number defines the amount of months that are taken into account

    Returns
    -------
    ET_Blue : array
              Array[time, lat, lon] contains Blue Evapotranspiration
    ET_Green : array
              Array[time, lat, lon] contains Green Evapotranspiration
    """
    import wa.General.raster_conversions as RC
    import wa.Functions.Start.Get_Dictionaries as GD

    # Input Parameters functions
    scale = 1.1

    # Open LU map for example
    LU = RC.Open_nc_array(nc_outname, "Landuse")

    # Define monthly dates
    Dates = pd.date_range(Startdate, Enddate, freq='MS')

    # Get moving window period

    # Get dictionaries and keys for the moving average
    ET_Blue_Green_Classes_dict, Moving_Window_Per_Class_dict = GD.get_bluegreen_classes(
        version='1.0')
    Classes = ET_Blue_Green_Classes_dict.keys()
    Moving_Averages_Values_Array = np.ones(LU.shape) * np.nan

    # Create array based on the dictionary that gives the Moving average tail for every pixel
    for Class in Classes:
        Values_Moving_Window_Class = Moving_Window_Per_Class_dict[Class]
        for Values_Class in ET_Blue_Green_Classes_dict[Class]:
            Moving_Averages_Values_Array[
                LU == Values_Class] = Values_Moving_Window_Class

    Additional_Months_front = int(np.nanmax(Moving_Averages_Values_Array))
    Additional_Months_tail = 0
    Start_period = Additional_Months_front
    End_period = Additional_Months_tail * -1

    ########################### Extract ETref data #################################

    if ETref_Product is 'WA_ETref':
        # Define data path
        Data_Path_ETref = os.path.join(Dir_Basin, 'ETref', 'Monthly')
    else:
        Data_Path_ETref = ETref_Product

    ETref = Complete_3D_Array(nc_outname, 'Reference_Evapotranspiration',
                              Startdate, Enddate, Additional_Months_front,
                              Additional_Months_tail, Data_Path_ETref)

    ######################## Extract Precipitation data ########################

    if (P_Product == "CHIRPS" or P_Product == "RFE" or P_Product == "TRMM"):
        # Define data path
        Data_Path_P = os.path.join(Dir_Basin, 'Precipitation', P_Product,
                                   'Monthly')
    else:
        Data_Path_P = P_Product

    P = Complete_3D_Array(nc_outname, 'Precipitation', Startdate, Enddate,
                          Additional_Months_front, Additional_Months_tail,
                          Data_Path_P)

    ########################## Extract actET data ##############################
    ET = RC.Open_nc_array(nc_outname, "Actual_Evapotranspiration", Startdate,
                          Enddate)

    ############ Create average ETref and P using moving window ################
    ETref_Ave = np.ones([len(Dates),
                         int(LU.shape[0]),
                         int(LU.shape[1])]) * np.nan
    P_Ave = np.ones([len(Dates), int(LU.shape[0]), int(LU.shape[1])]) * np.nan
    if End_period == 0:
        P_period = P[Start_period:, :, :]
        ETref_period = ETref[Start_period:, :, :]
    else:
        P_period = P[Start_period:End_period, :, :]
        ETref_period = ETref[Start_period:End_period, :, :]

    # Loop over the different moving average tails
    for One_Value in np.unique(Moving_Window_Per_Class_dict.values()):

        # If there is no moving average is 1 than use the value of the original ETref or P
        if One_Value == 1:
            Values_Ave_ETref = ETref[int(ETref.shape[0]) - len(Dates):, :, :]
            Values_Ave_P = P[int(ETref.shape[0]) - len(Dates):, :, :]

        # If there is tail, apply moving average over the whole datacube
        else:
            Values_Ave_ETref_tot = RC.Moving_average(ETref, One_Value - 1, 0)
            Values_Ave_P_tot = RC.Moving_average(P, One_Value - 1, 0)
            Values_Ave_ETref = Values_Ave_ETref_tot[
                int(Values_Ave_ETref_tot.shape[0]) - len(Dates):, :, :]
            Values_Ave_P = Values_Ave_P_tot[int(Values_Ave_P_tot.shape[0]) -
                                            len(Dates):, :, :]

        # Only add the data where the corresponding tail corresponds with the one_value
        ETref_Ave[:, Moving_Averages_Values_Array ==
                  One_Value] = Values_Ave_ETref[:,
                                                Moving_Averages_Values_Array ==
                                                One_Value]
        P_Ave[:, Moving_Averages_Values_Array ==
              One_Value] = Values_Ave_P[:, Moving_Averages_Values_Array ==
                                        One_Value]

    ##################### Calculate ET blue and green ###########################

    # Mask out the nan values(if one of the parameters is nan, then they are all nan)
    mask = np.any([
        np.isnan(LU) *
        np.ones([len(Dates), int(LU.shape[0]),
                 int(LU.shape[1])]) == 1,
        np.isnan(ET),
        np.isnan(ETref[int(ETref.shape[0]) - len(Dates):, :, :]),
        np.isnan(P[int(ETref.shape[0]) - len(Dates):, :, :]),
        np.isnan(P_Ave),
        np.isnan(ETref_Ave)
    ],
                  axis=0)
    ETref_period[mask] = ETref_Ave[mask] = ET[mask] = P_period[mask] = P_Ave[
        mask] = np.nan

    phi = ETref_Ave / P_Ave

    # Calculate Budyko-index
    Budyko = scale * np.sqrt(phi * np.tanh(1 / phi) * (1 - np.exp(-phi)))

    # Calculate ET green
    ETgreen_DataCube = np.minimum(
        Budyko * P[int(ETref.shape[0]) - len(Dates):, :, :], ET)

    # Calculate ET blue
    ETblue_DataCube = ET - ETgreen_DataCube

    return (np.array(ETblue_DataCube), np.array(ETgreen_DataCube))
Esempio n. 6
0
def Blue_Green(Dir_Basin, nc_outname, ETref_Product, P_Product, Startdate, Enddate):
    """
    This functions split the evapotranspiration into green and blue evapotranspiration.
    Parameters
    ----------
    nc_outname : str
        Path to the .nc file containing data
    Moving_Averaging_Length: integer
        Number defines the amount of months that are taken into account

    Returns
    -------
    ET_Blue : array
              Array[time, lat, lon] contains Blue Evapotranspiration
    ET_Green : array
              Array[time, lat, lon] contains Green Evapotranspiration
    """
    import wa.General.raster_conversions as RC
    import wa.Functions.Start.Get_Dictionaries as GD


    # Input Parameters functions
    scale = 1.1

    # Open LU map for example
    LU = RC.Open_nc_array(nc_outname, "Landuse")

    # Define monthly dates
    Dates = pd.date_range(Startdate, Enddate, freq = 'MS')

    # Get moving window period

    # Get dictionaries and keys for the moving average
    ET_Blue_Green_Classes_dict, Moving_Window_Per_Class_dict = GD.get_bluegreen_classes(version = '1.0')
    Classes = ET_Blue_Green_Classes_dict.keys()
    Moving_Averages_Values_Array = np.ones(LU.shape) * np.nan

    # Create array based on the dictionary that gives the Moving average tail for every pixel
    for Class in Classes:
        Values_Moving_Window_Class = Moving_Window_Per_Class_dict[Class]
        for Values_Class in ET_Blue_Green_Classes_dict[Class]:
            Moving_Averages_Values_Array[LU == Values_Class] = Values_Moving_Window_Class

    Additional_Months_front = int(np.nanmax(Moving_Averages_Values_Array))
    Additional_Months_tail = 0
    Start_period = Additional_Months_front
    End_period = Additional_Months_tail * -1

     ########################### Extract ETref data #################################

    if ETref_Product is 'WA_ETref':
        # Define data path
        Data_Path_ETref = os.path.join(Dir_Basin, 'ETref', 'Monthly')
    else:
        Data_Path_ETref = ETref_Product

    ETref = Complete_3D_Array(nc_outname, 'Reference_Evapotranspiration', Startdate, Enddate, Additional_Months_front, Additional_Months_tail, Data_Path_ETref)

    ######################## Extract Precipitation data ########################

    if (P_Product == "CHIRPS" or P_Product == "RFE"):
        # Define data path
        Data_Path_P = os.path.join(Dir_Basin, 'Precipitation', P_Product, 'Monthly')
    else:
        Data_Path_P = P_Product

    P = Complete_3D_Array(nc_outname, 'Precipitation', Startdate, Enddate, Additional_Months_front, Additional_Months_tail, Data_Path_P)

    ########################## Extract actET data ##############################
    ET = RC.Open_nc_array(nc_outname, "Actual_Evapotranspiration", Startdate, Enddate)

    ############ Create average ETref and P using moving window ################
    ETref_Ave = np.ones([len(Dates),int(LU.shape[0]),int(LU.shape[1])]) * np.nan
    P_Ave = np.ones([len(Dates),int(LU.shape[0]),int(LU.shape[1])]) * np.nan
    if End_period == 0:
        P_period = P[Start_period:,:,:]
        ETref_period = ETref[Start_period:,:,:]
    else:
        P_period = P[Start_period:End_period,:,:]
        ETref_period = ETref[Start_period:End_period,:,:]

     # Loop over the different moving average tails
    for One_Value in np.unique(Moving_Window_Per_Class_dict.values()):

        # If there is no moving average is 1 than use the value of the original ETref or P
        if One_Value == 1:
            Values_Ave_ETref = ETref[int(ETref.shape[0])-len(Dates):,:,:]
            Values_Ave_P = P[int(ETref.shape[0])-len(Dates):,:,:]

        # If there is tail, apply moving average over the whole datacube
        else:
            Values_Ave_ETref_tot = RC.Moving_average(ETref, One_Value - 1, 0)
            Values_Ave_P_tot = RC.Moving_average(P, One_Value - 1, 0)
            Values_Ave_ETref = Values_Ave_ETref_tot[int(Values_Ave_ETref_tot.shape[0])-len(Dates):,:,:]
            Values_Ave_P = Values_Ave_P_tot[int(Values_Ave_P_tot.shape[0])-len(Dates):,:,:]

        # Only add the data where the corresponding tail corresponds with the one_value
        ETref_Ave[:,Moving_Averages_Values_Array == One_Value] = Values_Ave_ETref[:,Moving_Averages_Values_Array == One_Value]
        P_Ave[:,Moving_Averages_Values_Array == One_Value] = Values_Ave_P[:,Moving_Averages_Values_Array == One_Value]

    ##################### Calculate ET blue and green ###########################

    # Mask out the nan values(if one of the parameters is nan, then they are all nan)
    mask = np.any([np.isnan(LU)*np.ones([len(Dates),int(LU.shape[0]),int(LU.shape[1])])==1, np.isnan(ET), np.isnan(ETref[int(ETref.shape[0])-len(Dates):,:,:]), np.isnan(P[int(ETref.shape[0])-len(Dates):,:,:]), np.isnan(P_Ave), np.isnan(ETref_Ave)],axis=0)
    ETref_period[mask] = ETref_Ave[mask] = ET[mask] = P_period[mask] = P_Ave[mask] = np.nan

    phi = ETref_Ave / P_Ave

    # Calculate Budyko-index
    Budyko = scale * np.sqrt(phi*np.tanh(1/phi)*(1-np.exp(-phi)))

    # Calculate ET green
    ETgreen_DataCube = np.minimum(Budyko*P[int(ETref.shape[0])-len(Dates):,:,:],ET)

    # Calculate ET blue
    ETblue_DataCube = ET - ETgreen_DataCube

    return(np.array(ETblue_DataCube), np.array(ETgreen_DataCube))