示例#1
0
def reproject_dataset_example(dataset, dataset_example, method=1):
    """
    A sample function to reproject and resample a GDAL dataset from within
    Python. The user can define the wanted projection and shape by defining an example dataset.

    Keywords arguments:
    dataset -- 'C:/file/to/path/file.tif' or a gdal file (gdal.Open(filename))
        string that defines the input tiff file or gdal file
    dataset_example -- 'C:/file/to/path/file.tif' or a gdal file (gdal.Open(filename))
        string that defines the input tiff file or gdal file
    method -- 1,2,3,4 default = 1
        1 = Nearest Neighbour, 2 = Bilinear, 3 = lanzcos, 4 = average
    """
    # open dataset that must be transformed
    try:
        if os.path.splitext(dataset)[-1] == '.tif':
            g = gdal.Open(dataset)
        else:
            g = dataset
    except:
        g = dataset
    epsg_from = Get_epsg(g)

    #exceptions
    if epsg_from == 9001:
        epsg_from = 5070

    # open dataset that is used for transforming the dataset
    try:
        if os.path.splitext(dataset_example)[-1] == '.tif':
            gland = gdal.Open(dataset_example)
            epsg_to = Get_epsg(gland)
        elif os.path.splitext(dataset_example)[-1] == '.nc':
            import wa.General.data_conversions as DC
            geo_out, epsg_to, size_X, size_Y, size_Z, Time = Open_nc_info(
                dataset_example)
            data = np.zeros([size_Y, size_X])
            gland = DC.Save_as_MEM(data, geo_out, str(epsg_to))
        else:
            gland = dataset_example
            epsg_to = Get_epsg(gland)
    except:
        gland = dataset_example
        epsg_to = Get_epsg(gland)

    # Set the EPSG codes
    osng = osr.SpatialReference()
    osng.ImportFromEPSG(epsg_to)
    wgs84 = osr.SpatialReference()
    wgs84.ImportFromEPSG(epsg_from)

    # Get shape and geo transform from example
    geo_land = gland.GetGeoTransform()
    col = gland.RasterXSize
    rows = gland.RasterYSize

    # Create new raster
    mem_drv = gdal.GetDriverByName('MEM')
    dest1 = mem_drv.Create('', col, rows, 1, gdal.GDT_Float32)
    dest1.SetGeoTransform(geo_land)
    dest1.SetProjection(osng.ExportToWkt())

    # Perform the projection/resampling
    if method is 1:
        gdal.ReprojectImage(g, dest1, wgs84.ExportToWkt(), osng.ExportToWkt(),
                            gdal.GRA_NearestNeighbour)
    if method is 2:
        gdal.ReprojectImage(g, dest1, wgs84.ExportToWkt(), osng.ExportToWkt(),
                            gdal.GRA_Bilinear)
    if method is 3:
        gdal.ReprojectImage(g, dest1, wgs84.ExportToWkt(), osng.ExportToWkt(),
                            gdal.GRA_Lanczos)
    if method is 4:
        gdal.ReprojectImage(g, dest1, wgs84.ExportToWkt(), osng.ExportToWkt(),
                            gdal.GRA_Average)
    return (dest1)
示例#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 Find_Area_Volume_Relation(region, input_JRC, DEM_dataset):

    # Find relation between V and A

    import numpy as np
    import wa.General.raster_conversions as RC
    import wa.General.data_conversions as DC
    from scipy.optimize import curve_fit
    import matplotlib.pyplot as plt

    def func(x, a, b):
        """
        This function is used for finding relation area and volume
        
        """
        return (a * x**b)

    def func3(x, a, b, c, d):
        """
        This function is used for finding relation area and volume
        
        """
        return (a * (x - c)**b + d)

    #Array, Geo_out = RC.clip_data(input_JRC,latlim=[14.528,14.985],lonlim =[35.810,36.005])
    Array, Geo_out = RC.clip_data(
        input_JRC,
        latlim=[region[2], region[3]],
        lonlim=[region[0], region[1]
                ])  # This reservoir was not filled when SRTM was taken
    size_Y = int(np.shape([Array])[-2])
    size_X = int(np.shape([Array])[-1])

    Water_array = np.zeros(np.shape(Array))
    buffer_zone = 4
    Array[Array > 0] = 1
    for i in range(0, size_Y):
        for j in range(0, size_X):
            Water_array[i, j] = np.max(Array[
                np.maximum(0, i -
                           buffer_zone):np.minimum(size_Y, i + buffer_zone +
                                                   1),
                np.maximum(0, j -
                           buffer_zone):np.minimum(size_X, j + buffer_zone +
                                                   1)])
    del Array

    # Open DEM and reproject

    # Save Example as memory file
    dest_example = DC.Save_as_MEM(Water_array, Geo_out, projection='WGS84')

    # reproject DEM by using example
    dest_out = RC.reproject_dataset_example(DEM_dataset,
                                            dest_example,
                                            method=2)
    DEM = dest_out.GetRasterBand(1).ReadAsArray()

    # find DEM water heights
    DEM_water = np.zeros(np.shape(Water_array))
    DEM_water[Water_array != 1] = np.nan
    DEM_water[Water_array == 1.] = DEM[Water_array == 1.]

    # Get array with areas
    import wa.Functions.Start.Area_converter as Area
    dlat, dlon = Area.Calc_dlat_dlon(Geo_out, size_X, size_Y)
    area_in_m2 = dlat * dlon

    # find volume and Area
    min_DEM_water = int(np.round(np.nanmin(DEM_water)))
    max_DEM_water = int(np.round(np.nanmax(DEM_water)))

    Reservoir_characteristics = np.zeros([1, 5])
    i = 0

    for height in range(min_DEM_water + 1, max_DEM_water):
        DEM_water_below_height = np.zeros(np.shape(DEM_water))
        DEM_water[np.isnan(DEM_water)] = 1000000
        DEM_water_below_height[DEM_water < height] = 1
        pixels = np.sum(DEM_water_below_height)

        area = np.sum(DEM_water_below_height * area_in_m2)
        if height == min_DEM_water + 1:
            volume = 0.5 * area
            histogram = pixels
            Reservoir_characteristics[:] = [
                height, pixels, area, volume, histogram
            ]
        else:
            area_previous = Reservoir_characteristics[i, 2]
            volume_previous = Reservoir_characteristics[i, 3]
            volume = volume_previous + 0.5 * (
                area - area_previous) + 1 * area_previous
            histogram_previous = Reservoir_characteristics[i, 1]
            histogram = pixels - histogram_previous
            Reservoir_characteristics_one = [
                height, pixels, area, volume, histogram
            ]
            Reservoir_characteristics = np.append(
                Reservoir_characteristics, Reservoir_characteristics_one)
            i += 1
            Reservoir_characteristics = np.resize(Reservoir_characteristics,
                                                  (i + 1, 5))

    maxi = int(len(Reservoir_characteristics[:, 3]))

    # find minimum value for reservoirs height (DEM is same value if reservoir was already filled whe SRTM was created)
    Historgram = Reservoir_characteristics[:, 4]
    hist_mean = np.mean(Historgram)
    hist_std = np.std(Historgram)

    mini_tresh = hist_std * 5 + hist_mean

    Check_hist = np.zeros([len(Historgram)])
    Check_hist[Historgram > mini_tresh] = Historgram[Historgram > mini_tresh]
    if np.max(Check_hist) != 0.0:
        col = np.argwhere(Historgram == np.max(Check_hist))[0][0]
        mini = col + 1
    else:
        mini = 0

    fitted = 0

    # find starting point reservoirs
    V0 = Reservoir_characteristics[mini, 3]
    A0 = Reservoir_characteristics[mini, 2]

    # Calculate the best maxi reservoir characteristics, based on the normal V = a*x**b relation
    while fitted == 0:
        try:
            if mini == 0:
                popt1, pcov1 = curve_fit(
                    func, Reservoir_characteristics[mini:maxi, 2],
                    Reservoir_characteristics[mini:maxi, 3])
            else:
                popt1, pcov1 = curve_fit(
                    func, Reservoir_characteristics[mini:maxi, 2] - A0,
                    Reservoir_characteristics[mini:maxi, 3] - V0)
            fitted = 1
        except:
            maxi -= 1

        if maxi < mini:
            print 'ERROR: was not able to find optimal fit'
            fitted = 1

    # Remove last couple of pixels of maxi
    maxi_end = int(np.round(maxi - 0.2 * (maxi - mini)))

    done = 0
    times = 0

    while done == 0 and times > 20 and maxi_end < mini:
        try:
            if mini == 0:
                popt, pcov = curve_fit(
                    func, Reservoir_characteristics[mini:maxi_end, 2],
                    Reservoir_characteristics[mini:maxi_end, 3])
            else:
                popt, pcov = curve_fit(
                    func3, Reservoir_characteristics[mini:maxi_end, 2],
                    Reservoir_characteristics[mini:maxi_end, 3])

        except:
            maxi_end = int(maxi)
            if mini == 0:
                popt, pcov = curve_fit(
                    func, Reservoir_characteristics[mini:maxi_end, 2],
                    Reservoir_characteristics[mini:maxi_end, 3])
            else:
                popt, pcov = curve_fit(
                    func3, Reservoir_characteristics[mini:maxi_end, 2],
                    Reservoir_characteristics[mini:maxi_end, 3])

        if mini == 0:
            plt.plot(Reservoir_characteristics[mini:maxi_end, 2],
                     Reservoir_characteristics[mini:maxi_end, 3], 'ro')
            t = np.arange(0., np.max(Reservoir_characteristics[:, 2]), 1000)
            plt.plot(t, popt[0] * (t)**popt[1], 'g--')
            plt.axis([
                0,
                np.max(Reservoir_characteristics[mini:maxi_end, 2]), 0,
                np.max(Reservoir_characteristics[mini:maxi_end, 3])
            ])
            plt.show()
            done = 1

        else:
            plt.plot(Reservoir_characteristics[mini:maxi_end, 2],
                     Reservoir_characteristics[mini:maxi_end, 3], 'ro')
            t = np.arange(0., np.max(Reservoir_characteristics[:, 2]), 1000)
            plt.plot(t, popt[0] * (t - popt[2])**popt[1] + popt[3], 'g--')
            plt.axis([
                0,
                np.max(Reservoir_characteristics[mini:maxi_end, 2]), 0,
                np.max(Reservoir_characteristics[mini:maxi_end, 3])
            ])
            plt.show()
            Volume_error = popt[3] / V0 * 100 - 100
            print 'error Volume = %s percent' % Volume_error
            print 'error Area = %s percent' % (A0 / popt[2] * 100 - 100)

            if Volume_error < 30 and Volume_error > -30:
                done = 1
            else:
                times += 1
                maxi_end -= 1
                print 'Another run is done in order to improve the result'

    if done == 0:
        popt = np.append(popt1, [A0, V0])

    if len(popt) == 2:
        popt = np.append(popt, [0, 0])

    return (popt)
示例#4
0
def Add_Reservoirs(Name_NC_Rivers, Name_NC_Acc_Pixels, Diff_Water_Volume,
                   River_dict, Discharge_dict, DEM_dict, Distance_dict,
                   Regions, Example_dataset):

    import numpy as np

    import wa.General.raster_conversions as RC
    import wa.General.data_conversions as DC

    # Extract Rivers data from NetCDF file
    Rivers = RC.Open_nc_array(Name_NC_Rivers)

    # Open data array info based on example data
    geo_out, epsg, size_X, size_Y = RC.Open_array_info(Example_dataset)

    # Extract flow direction data from NetCDF file
    acc_pixels = RC.Open_nc_array(Name_NC_Acc_Pixels)

    # 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
    del x, y

    Acc_Pixels_Rivers = Rivers * acc_pixels
    ID_Rivers = Rivers * ID_Matrix

    Amount_of_Reservoirs = len(Regions)

    Reservoir_is_in_River = np.ones([len(Regions), 3]) * -9999

    for reservoir in range(0, Amount_of_Reservoirs):

        region = Regions[reservoir, :]

        dest = DC.Save_as_MEM(Acc_Pixels_Rivers, geo_out, projection='WGS84')
        Rivers_Acc_Pixels_reservoir, Geo_out = RC.clip_data(
            dest, latlim=[region[2], region[3]], lonlim=[region[0], region[1]])

        dest = DC.Save_as_MEM(ID_Rivers, geo_out, projection='WGS84')
        Rivers_ID_reservoir, Geo_out = RC.clip_data(
            dest, latlim=[region[2], region[3]], lonlim=[region[0], region[1]])

        size_Y_reservoir, size_X_reservoir = np.shape(
            Rivers_Acc_Pixels_reservoir)
        IDs_Edges = []
        IDs_Edges = np.append(IDs_Edges, Rivers_Acc_Pixels_reservoir[0, :])
        IDs_Edges = np.append(IDs_Edges, Rivers_Acc_Pixels_reservoir[:, 0])
        IDs_Edges = np.append(
            IDs_Edges,
            Rivers_Acc_Pixels_reservoir[int(size_Y_reservoir) - 1, :])
        IDs_Edges = np.append(
            IDs_Edges, Rivers_Acc_Pixels_reservoir[:,
                                                   int(size_X_reservoir) - 1])
        Value_Reservoir = np.max(np.unique(IDs_Edges))

        y_pix_res, x_pix_res = np.argwhere(
            Rivers_Acc_Pixels_reservoir == Value_Reservoir)[0]
        ID_reservoir = Rivers_ID_reservoir[y_pix_res, x_pix_res]

        # Find exact reservoir area in river directory
        for River_part in River_dict.iteritems():
            if len(np.argwhere(River_part[1] == ID_reservoir)) > 0:
                Reservoir_is_in_River[reservoir, 0] = np.argwhere(
                    River_part[1] == ID_reservoir)  #River_part_good
                Reservoir_is_in_River[reservoir,
                                      1] = River_part[0]  #River_Add_Reservoir
                Reservoir_is_in_River[reservoir, 2] = 1  #Reservoir_is_in_River

    numbers = abs(Reservoir_is_in_River[:, 1].argsort() -
                  len(Reservoir_is_in_River) + 1)

    for number in range(0, len(Reservoir_is_in_River)):

        row_reservoir = np.argwhere(numbers == number)[0][0]

        if not Reservoir_is_in_River[row_reservoir, 2] == -9999:

            # Get discharge into the reservoir:
            Flow_in_res_m3 = Discharge_dict[int(Reservoir_is_in_River[
                row_reservoir, 1])][:,
                                    int(Reservoir_is_in_River[row_reservoir,
                                                              0])]

            # Get difference reservoir
            Change_Reservoir_m3 = Diff_Water_Volume[row_reservoir, :, 2]

            # Total Change outflow
            Change_outflow_m3 = np.minimum(Flow_in_res_m3, Change_Reservoir_m3)

            Difference = Change_outflow_m3 - Change_Reservoir_m3
            if abs(np.sum(Difference)) > 10000 and np.sum(
                    Change_Reservoir_m3[Change_outflow_m3 > 0]) > 0:
                Change_outflow_m3[Change_outflow_m3 < 0] = Change_outflow_m3[
                    Change_outflow_m3 < 0] * np.sum(
                        Change_outflow_m3[Change_outflow_m3 > 0]) / np.sum(
                            Change_Reservoir_m3[Change_outflow_m3 > 0])

            # Find key name (which is also the lenght of the river dictionary)
            i = len(River_dict)

            #River_with_reservoirs_dict[i]=list((River_dict[River_Add_Reservoir][River_part_good[0][0]:]).flat) < MAAK DIRECTORIES ARRAYS OP DEZE MANIER DAN IS DE ARRAY 1D
            River_dict[i] = River_dict[int(Reservoir_is_in_River[
                row_reservoir, 1])][int(Reservoir_is_in_River[row_reservoir,
                                                              0]):]
            River_dict[int(
                Reservoir_is_in_River[row_reservoir, 1])] = River_dict[int(
                    Reservoir_is_in_River[
                        row_reservoir,
                        1])][:int(Reservoir_is_in_River[row_reservoir, 0]) + 1]

            DEM_dict[i] = DEM_dict[int(Reservoir_is_in_River[
                row_reservoir, 1])][int(Reservoir_is_in_River[row_reservoir,
                                                              0]):]
            DEM_dict[int(
                Reservoir_is_in_River[row_reservoir, 1])] = DEM_dict[int(
                    Reservoir_is_in_River[
                        row_reservoir,
                        1])][:int(Reservoir_is_in_River[row_reservoir, 0]) + 1]

            Distance_dict[i] = Distance_dict[int(Reservoir_is_in_River[
                row_reservoir, 1])][int(Reservoir_is_in_River[row_reservoir,
                                                              0]):]
            Distance_dict[int(
                Reservoir_is_in_River[row_reservoir, 1])] = Distance_dict[int(
                    Reservoir_is_in_River[
                        row_reservoir,
                        1])][:int(Reservoir_is_in_River[row_reservoir, 0]) + 1]

            Discharge_dict[i] = Discharge_dict[int(Reservoir_is_in_River[
                row_reservoir, 1])][:,
                                    int(Reservoir_is_in_River[row_reservoir,
                                                              0]):]
            Discharge_dict[int(
                Reservoir_is_in_River[row_reservoir, 1])] = Discharge_dict[int(
                    Reservoir_is_in_River[
                        row_reservoir,
                        1])][:, :int(Reservoir_is_in_River[row_reservoir, 0]) +
                             1]
            Discharge_dict[int(Reservoir_is_in_River[
                row_reservoir,
                1])][:, 1:int(Reservoir_is_in_River[row_reservoir, 0]) +
                     1] = Discharge_dict[int(
                         Reservoir_is_in_River[row_reservoir, 1]
                     )][:, 1:int(Reservoir_is_in_River[row_reservoir, 0]) +
                        1] - Change_outflow_m3[:, None]
            Next_ID = River_dict[int(Reservoir_is_in_River[row_reservoir,
                                                           1])][0]

            times = 0
            while len(River_dict) > times:
                for River_part in River_dict.iteritems():
                    if River_part[-1][-1] == Next_ID:
                        Next_ID = River_part[-1][0]
                        item = River_part[0]
                        #Always 10 procent of the incoming discharge will pass the dam
                        Change_outflow_m3[:, None] = np.minimum(
                            0.9 * Discharge_dict[item][:, -1:],
                            Change_outflow_m3[:, None])

                        Discharge_dict[item][:, 1:] = Discharge_dict[
                            item][:, 1:] - Change_outflow_m3[:, None]
                        print(item)
                        times = 0
                    times += 1

    return (Discharge_dict, River_dict, DEM_dict, Distance_dict)
示例#5
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 ()