示例#1
0
def Convert_dict_to_array(River_dict, Array_dict, Reference_data):

    import numpy as np
    import os
    import wa.General.raster_conversions as RC

    if os.path.splitext(Reference_data)[-1] == '.nc':
        # Get raster information
        geo_out, proj, size_X, size_Y, size_Z, Time = RC.Open_nc_info(
            Reference_data)
    else:
        # Get raster information
        geo_out, proj, size_X, size_Y = RC.Open_array_info(Reference_data)

    # 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(Array_dict[0])[0])

    # create an empty array
    DataCube = np.ones([time_dimension, size_Y, size_X]) * np.nan

    for river_part in range(0, len(River_dict)):
        for river_pixel in range(1, len(River_dict[river_part])):
            river_pixel_ID = River_dict[river_part][river_pixel]
            if len(np.argwhere(ID_Matrix == river_pixel_ID)) > 0:
                row, col = np.argwhere(ID_Matrix == river_pixel_ID)[0][:]
                DataCube[:, row, col] = Array_dict[river_part][:, river_pixel]

    return (DataCube)
示例#2
0
def Degrees_to_m2(Reference_data):
    """
    This functions calculated the area of each pixel in squared meter.

    Parameters
    ----------
    Reference_data: str
        Path to a tiff file or nc file of which the pixel area must be defined
        
    Returns
    -------
    area_in_m2: array
        Array containing the area of each pixel in squared meters

    """
    # Get the extension of the example data
    filename, file_extension = os.path.splitext(Reference_data)

    # Get raster information
    if str(file_extension) == '.tif':
        geo_out, proj, size_X, size_Y = RC.Open_array_info(Reference_data)
    if str(file_extension) == '.nc':
        geo_out, epsg, size_X, size_Y, size_Z, Time = RC.Open_nc_info(
            Reference_data)

    # Calculate the difference in latitude and longitude in meters
    dlat, dlon = Calc_dlat_dlon(geo_out, size_X, size_Y)

    # Calculate the area in squared meters
    area_in_m2 = dlat * dlon

    return (area_in_m2)
示例#3
0
def Add_Inlets(Name_NC_Runoff, Inflow_Text_Files):
    '''
    This functions add inflow to the runoff dataset before the channel routing.
    The inflow must be a text file with a certain format. The first line of this format are the latitude and longitude.
    Hereafter for each line the time (ordinal time) and the inflow (m3/month) seperated with one space is defined. See example below:
        
    [lat lon]
    733042 156225.12
    733073 32511321.2
    733102 212315.25
    733133 2313266.554
    '''
    # General modules
    import numpy as np

    # Water Accounting modules
    import wa.General.raster_conversions as RC
    import wa.Functions.Start.Area_converter as Area

    # Open information and open the Runoff array
    Runoff_dataCube = RC.Open_nc_array(Name_NC_Runoff)
    geo_out, epsg, size_X, size_Y, size_Z, Time = RC.Open_nc_info(
        Name_NC_Runoff)

    # Calculate the surface area of every pixel
    dlat, dlon = Area.Calc_dlat_dlon(geo_out, size_X, size_Y)
    area_in_m2 = dlat * dlon

    for Inflow_Text_File in Inflow_Text_Files:

        # Open the inlet text data
        Inlet = np.genfromtxt(Inflow_Text_File, dtype=None, delimiter=" ")

        # Read out the coordinates
        Coord = Inlet[0, :]
        Lon_coord = Coord[0][1:]
        Lat_coord = Coord[1][:-1]

        # Search for the pixel
        lon_pix = int(np.ceil((float(Lon_coord) - geo_out[0]) / geo_out[1]))
        lat_pix = int(np.ceil((float(Lat_coord) - geo_out[3]) / geo_out[5]))

        # Add the value on top of the Runoff array
        for i in range(1, len(Inlet)):
            time = float(Inlet[i, 0])
            time_step = np.argwhere(np.logical_and(Time >= time, Time <= time))
            if len(time_step) > 0:
                time_step_array = int(time_step[0][0])
                value_m3_month = float(Inlet[i, 1])
                area_in_m2_pixel = area_in_m2[lat_pix, lon_pix]
                value_mm = (value_m3_month / area_in_m2_pixel) * 1000
                Runoff_dataCube[time_step_array, lat_pix,
                                lon_pix] = Runoff_dataCube[time_step_array,
                                                           lat_pix,
                                                           lon_pix] + value_mm

    return (Runoff_dataCube)
示例#4
0
def Convert_nc_to_tiff(input_nc, output_folder):
    """
    This function converts the nc file into tiff files

    Keyword Arguments:
    input_nc -- name, name of the adf file
    output_folder -- Name of the output tiff file
    """
    from datetime import date
    import wa.General.raster_conversions as RC

    #All_Data = RC.Open_nc_array(input_nc)

    if type(input_nc) == str:
        nc = netCDF4.Dataset(input_nc)
    elif type(input_nc) == list:
        nc = netCDF4.MFDataset(input_nc)

    Var = nc.variables.keys()[-1]
    All_Data = nc[Var]

    geo_out, epsg, size_X, size_Y, size_Z, Time = RC.Open_nc_info(input_nc)

    if epsg == 4326:
        epsg = 'WGS84'

    # Create output folder if needed
    if not os.path.exists(output_folder):
        os.mkdir(output_folder)

    for i in range(0, size_Z):
        if not Time == -9999:
            time_one = Time[i]
            d = date.fromordinal(time_one)
            name = os.path.splitext(os.path.basename(input_nc))[0]
            nameparts = name.split('_')[0:-2]
            name_out = os.path.join(
                output_folder, '_'.join(nameparts) + '_%d.%02d.%02d.tif' %
                (d.year, d.month, d.day))
            Data_one = All_Data[i, :, :]
        else:
            name = os.path.splitext(os.path.basename(input_nc))[0]
            name_out = os.path.join(output_folder, name + '.tif')
            Data_one = All_Data[:, :]

        Save_as_tiff(name_out, Data_one, geo_out, epsg)

    return ()
示例#5
0
def Run(input_nc, output_nc):

    # Extract flow direction data from NetCDF file
    flow_directions = RC.Open_nc_array(input_nc, Var = 'demdir')

    # Open River Array
    Rivers = RC.Open_nc_array(output_nc, Var = 'rivers')

    # Open Accumulated Pixel Array
    Accumulated_Pixels = RC.Open_nc_array(output_nc, Var = 'accpix')

    # Open Routed discharge Array
    Routed_Array = RC.Open_nc_array(output_nc, Var = 'discharge_natural')

    # Get the raster shape
    geo_out_example, epsg_example, size_X_example, size_Y_example, size_Z_example, Time_example = RC.Open_nc_info(input_nc)
    geo_out_example = np.array(geo_out_example)

    # Create a river array with a boundary of 1 pixel
    Rivers_bounds = np.zeros([size_Y_example+2, size_X_example+2])
    Rivers_bounds[1:-1,1:-1] = Rivers

    # Create a flow direction array with a boundary of 1 pixel
    flow_directions[flow_directions==0]=-32768
    flow_directions_bound = np.ones([size_Y_example+2, size_X_example+2]) * -32768
    flow_directions_bound[1:-1,1:-1] = flow_directions

    # Create ID Matrix
    y,x = np.indices((size_Y_example, size_X_example))
    ID_Matrix = np.int32(np.ravel_multi_index(np.vstack((y.ravel(),x.ravel())),(size_Y_example,size_X_example),mode='clip').reshape(x.shape))
    ID_Matrix_bound = np.ones([size_Y_example+2, size_X_example+2]) * -32768
    ID_Matrix_bound[1:-1,1:-1] = ID_Matrix + 1
    ID_Matrix_bound[flow_directions_bound==-32768]=-32768
    del  x, y

    # Empty total from and to arrays
    ID_to_total=np.array([])
    ID_from_total=np.array([])

    # The flow directions parameters of HydroSHED
    Directions = [1, 2, 4, 8, 16, 32, 64, 128]

    # Loop over the directions
    for Direction in Directions:

        # empty from and to arrays for 1 direction
        data_flow_to = np.zeros([size_Y_example + 2, size_X_example + 2])
        data_flow_from = np.zeros([size_Y_example + 2, size_X_example + 2])

        # Get the ID of only the rivers
        data_flow_to_ID = np.zeros([size_Y_example + 2, size_X_example + 2])
        data_flow_in = np.ones([size_Y_example + 2, size_X_example + 2])	* Rivers_bounds

        # Mask only one direction
        data_flow_from[flow_directions_bound == Direction] = data_flow_in[flow_directions_bound == Direction] * ID_Matrix_bound[flow_directions_bound == Direction]

        # Add the data flow to ID
        if Direction == 4:
            data_flow_to[1:,:] = data_flow_from[:-1,:]
        if Direction == 2:
            data_flow_to[1:,1:] = data_flow_from[:-1,:-1]
        if Direction == 1:
            data_flow_to[:,1:] = data_flow_from[:,:-1]
        if Direction == 128:
            data_flow_to[:-1,1:] = data_flow_from[1:,:-1]
        if Direction == 64:
            data_flow_to[:-1,:] = data_flow_from[1:,:]
        if Direction == 32:
            data_flow_to[:-1,:-1] = data_flow_from[1:,1:]
        if Direction == 16:
            data_flow_to[:,:-1] = data_flow_from[:,1:]
        if Direction == 8:
            data_flow_to[1:,:-1] = data_flow_from[:-1,1:]

        # mask out the no river pixels
        data_flow_to_ID[data_flow_to>0] = ID_Matrix_bound[data_flow_to>0]

        # Collect to and from arrays
        ID_from_total = np.append(ID_from_total,data_flow_from[data_flow_from!=0].ravel())
        ID_to_total = np.append(ID_to_total,data_flow_to_ID[data_flow_to_ID!=0].ravel())


    ######################## Define the starting point ############################

    # Open Basin area
    Basin = RC.Open_nc_array(input_nc, Var = 'basin')
    Basin = -1 * (Basin - 1)
    Basin_Buffer = RC.Create_Buffer(Basin, 8)
    Possible_End_Points = np.zeros(Basin.shape)
    Possible_End_Points[(Basin_Buffer + Rivers) == 2] = 1
    End_Points = [[0,0]]

    rows_col_possible_end_pixels = np.argwhere(Possible_End_Points == 1)
    #  Accumulated_Pixels_possible = ID_Matrix * Possible_End_Points

    for PosPix in rows_col_possible_end_pixels:
        Accumulated_Pixels_possible_Area = Accumulated_Pixels[PosPix[0]-1:PosPix[0]+2, PosPix[1]-1:PosPix[1]+2]
        Max_acc_possible_area = np.max(Accumulated_Pixels_possible_Area)
        middle_pixel = Accumulated_Pixels_possible_Area[1,1]
        if Max_acc_possible_area == middle_pixel:
            if flow_directions[PosPix[0],PosPix[1]] == -32768:
                acc_aux = np.copy(Accumulated_Pixels_possible_Area)
                acc_aux[1,1] = 0
                off_y = np.where(acc_aux == np.max(acc_aux))[1][0] - 1
                off_x = np.where(acc_aux == np.max(acc_aux))[0][0] - 1
                PosPix[0] = PosPix[0] + off_x
                PosPix[1] = PosPix[1] + off_y
            if End_Points == []:
                End_Points = PosPix
            else:
                End_Points = np.vstack([End_Points, PosPix])

    # Create an empty dictionary for the rivers
    River_dict = dict()

    # Create empty array for the loop
    ID_starts_next = []
    i = 0

    for End_Point in End_Points[1:]:

    # Define starting point
    # Max_Acc_Pix = np.nanmax(Accumulated_Pixels[ID_Matrix_bound[1:-1,1:-1]>0])
    # ncol, nrow = np.argwhere(Accumulated_Pixels==Max_Acc_Pix)[0]

    # Add Bounds
    # col = ncol + 1
    # row = nrow + 1

        col = End_Point[0] + 1
        row = End_Point[1] + 1

        ############################ Route the river ##################################

        # Get the ID of the starting point
        ID_starts = [ID_Matrix_bound[col,row]]


        # Keep going on till all the branches are looped
        while len(ID_starts) > 0:
            for ID_start in ID_starts:
                ID_start = int(ID_start)

                # Empty parameters for new starting point
                new = 0
                IDs = []

                # Add starting point
                Arrays_from = np.argwhere(ID_from_total[:] == ID_start)
                ID_from = ID_to_total[int(Arrays_from[0])]
                IDs = np.array([ID_from, ID_start])
                ID_start_now = ID_start

                # Keep going till the branch ends
                while new == 0:

                    Arrays_to = np.argwhere(ID_to_total[:] == ID_start)

                    # Add IDs to the river dictionary
                    if len(Arrays_to)>1 or len(Arrays_to) == 0:
                        River_dict[i] = IDs
                        i += 1
                        new = 1

                        # Define the next loop for the new branches
                        for j in range(0, len(Arrays_to)):
                            ID_starts_next = np.append(ID_starts_next,ID_from_total[int(Arrays_to[j])])

                        # If it was the last one then empty ID_start_next
                        if ID_start_now == ID_starts[-1]:
                            ID_starts = ID_starts_next
                            ID_starts_next = []

                    # Add pixel to tree for river dictionary
                    else:
                        ID_start = ID_from_total[Arrays_to[0]]
                        IDs = np.append(IDs, ID_start)

    ######################## Create dict distance and dict dem ####################

    # Extract DEM data from NetCDF file
    DEM = RC.Open_nc_array(input_nc, Var = 'dem')

    # Get the distance of a horizontal and vertical flow pixel (assuming it flows in a straight line)
    import wa.Functions.Start.Area_converter as AC
    vertical, horizontal = AC.Calc_dlat_dlon(geo_out_example,size_X_example, size_Y_example)

    # Calculate a diagonal flowing pixel (assuming it flos in a straight line)
    diagonal = np.power((np.square(vertical) + np.square(horizontal)),0.5)

    # Create empty distance array
    Distance = np.zeros([size_Y_example, size_X_example])

    # Fill in the distance array
    Distance[np.logical_or(flow_directions == 1,flow_directions == 16)] = horizontal[np.logical_or(flow_directions == 1,flow_directions == 16)]
    Distance[np.logical_or(flow_directions == 64,flow_directions == 4)] = vertical[np.logical_or(flow_directions == 64,flow_directions == 4)]
    Distance[np.logical_or(np.logical_or(np.logical_or(flow_directions == 32,flow_directions == 8),flow_directions == 128),flow_directions == 2)] = diagonal[np.logical_or(np.logical_or(np.logical_or(flow_directions == 32,flow_directions == 8),flow_directions == 128),flow_directions == 2)]

    # Create empty dicionaries for discharge, distance, and DEM
    Discharge_dict = dict()
    Distance_dict = dict()
    DEM_dict = dict()

    # Create empty arrays needed for the loop
    River_end = []
    River_ends = np.zeros([2,3])


    # Loop over the branches
    for River_number in range(0,len(River_dict)):

        # Get the pixels associated with the river section
        River = River_dict[River_number]
        i=1

        # Create empty arrays
        Distances_river = np.zeros([len(River)])
        DEM_river = np.zeros([len(River)])
        Discharge_river = np.zeros([len(River)])

        # for the first pixel get the previous pixel value from another branche
        row_start = np.argwhere(River_ends[:,0] == River[0])
        if len(row_start) < 1:
            Distances_river[0] = 0
            row, col = np.argwhere(ID_Matrix_bound == River[0])[0][:]
            DEM_river[0] = DEM[row - 1, col - 1]
            Discharge_river[0] = -9999

        else:
            Distances_river[0] = River_ends[row_start, 1]
            DEM_river[0] = River_ends[row_start, 2]
            row, col = np.argwhere(ID_Matrix_bound == River[0])[0][:]
            #Discharge_river[0] = Routed_Discharge[timestep, row - 1, col - 1]

        # For the other pixels get the value of the River ID pixel
        for River_part in River[1:]:
            row, col = np.argwhere(ID_Matrix_bound == River_part)[0][:]
            Distances_river[i] = Distance[row - 1, col - 1]
            DEM_river[i] = np.max([DEM_river[i-1],DEM[row - 1, col - 1]])
            #Discharge_river[i] = Routed_Discharge[timestep, row - 1, col - 1]

            if River_part == River[1] and Discharge_river[i-1] == -9999:
                Discharge_river[i - 1] = Discharge_river[i]

            i += 1

        # Write array in dictionary
        DEM_dict[River_number] = DEM_river
        Discharge_dict[River_number] = Discharge_river
        Distance_dict[River_number] = np.cumsum(Distances_river)

        # Save the last pixel value
        River_end[:] = [River_part , np.cumsum(Distances_river)[-1], DEM_river[-1]]
        River_ends = np.vstack((River_ends, River_end))


    ########################## Discharge Dictionary ###############################

    # Create ID Matrix
    y,x = np.indices((size_Y_example, size_X_example))
    ID_Matrix = np.int32(np.ravel_multi_index(np.vstack((y.ravel(),x.ravel())),(size_Y_example,size_X_example),mode='clip').reshape(x.shape))
    ID_Matrix_bound = np.ones([size_Y_example+2, size_X_example+2]) * -32768
    ID_Matrix_bound[1:-1,1:-1] = ID_Matrix + 1
    del  x, y

    # Create empty dicionaries for discharge, distance, and DEM
    Discharge_dict = dict()

    Amount_months = len(RC.Open_nc_array(input_nc, Var = 'time'))

    # Loop over the branches
    for River_number in range(0,len(River_dict)):

        # Get the pixels associated with the river section
        River = River_dict[River_number]
        i=0

        # Create empty arrays
        Discharge_river = np.zeros([Amount_months, len(River)])

        # For the other pixels get the value of the River ID pixel
        for River_part in River[:]:
            row, col = np.argwhere(ID_Matrix_bound == River_part)[0][:]
            Discharge_river[:,i] = Routed_Array[:, row - 1, col - 1]
            i += 1

        # Write array in dictionary
        Discharge_dict[River_number] = Discharge_river
        print(River_number)

    return(DEM_dict, River_dict, Distance_dict, Discharge_dict)
示例#6
0
def Add_Reservoirs(output_nc, Diff_Water_Volume, Regions):
 
    import numpy as np
    
    import wa.General.raster_conversions as RC
    import wa.General.data_conversions as DC

    # Extract data from NetCDF file
    Discharge_dict = RC.Open_nc_dict(output_nc, "dischargedict_dynamic")    
    River_dict = RC.Open_nc_dict(output_nc, "riverdict_static")
    DEM_dict = RC.Open_nc_dict(output_nc, "demdict_static")
    Distance_dict = RC.Open_nc_dict(output_nc, "distancedict_static")
    Rivers = RC.Open_nc_array(output_nc, "rivers")
    acc_pixels = RC.Open_nc_array(output_nc, "accpix")


    # Open data array info based on example data
    geo_out, epsg, size_X, size_Y, size_Z, time = RC.Open_nc_info(output_nc)

    # 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)        
示例#7
0
def Find_Area_Volume_Relation(region, input_JRC, input_nc):
  
    # 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   
    DEM_Array = RC.Open_nc_array(input_nc, "dem" )
    Geo_out_dem, proj_dem, size_X_dem, size_Y_dem, size_Z_dem, time = RC.Open_nc_info(input_nc)
    
    # Save Example as memory file
    dest_example = DC.Save_as_MEM(Water_array, Geo_out, projection='WGS84')   
    dest_dem = DC.Save_as_MEM(DEM_Array, Geo_out_dem, projection='WGS84')   

    # reproject DEM by using example
    dest_out=RC.reproject_dataset_example(dest_dem, 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)
示例#8
0
def Run(input_nc, output_nc):

    # Open discharge dict
    Discharge_dict = RC.Open_nc_dict(output_nc, 'dischargedict_dynamic')

    # Open River dict
    River_dict = RC.Open_nc_dict(output_nc, 'riverdict_static')

    # Open River Array
    Rivers = RC.Open_nc_array(output_nc, Var='rivers')

    # Open Supply Array
    DataCube_surface_withdrawal_mm = RC.Open_nc_array(input_nc,
                                                      Var='Extraction_M')
    Areas_in_m2 = RC.Open_nc_array(input_nc, Var='area')
    DataCube_surface_withdrawal_m3 = ((DataCube_surface_withdrawal_mm / 1000) *
                                      Areas_in_m2)

    # Open Basin Array
    Basin = RC.Open_nc_array(input_nc, Var='basin')

    # Copy dicts as starting adding reservoir
    Discharge_dict_new = copy.deepcopy(Discharge_dict)

    # Open data array info based on example data
    geo_out_example, epsg_example, size_X_example, size_Y_example, size_Z_example, Time_example = RC.Open_nc_info(
        input_nc)

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

    # Find IDs
    ID_Rivers = Rivers * ID_Matrix

    # find IDs drainage for only the basin
    ID_Rivers_flow = RC.gap_filling(ID_Rivers, NoDataValue=0.) * Basin
    Water_Error = 0
    Count = 0

    for i in np.unique(ID_Rivers_flow)[1:]:
        Count += 1
        if np.nansum(DataCube_surface_withdrawal_m3[:,
                                                    ID_Rivers_flow == i]) > 0:
            sys.stdout.write(
                "\r%s Procent of adding irrigation completed with %.2f x 10^9 m3 Water Error       "
                % (np.int(
                    np.ceil((np.float(Count) / len(np.unique(ID_Rivers_flow)) *
                             100))), Water_Error / 1e9))
            sys.stdout.flush()
            #print('%s Procent of adding irrigation completed' %np.int(i/np.unique(ID_Rivers_flow)[-1]*100))
            total_surface_withdrawal = np.nansum(
                DataCube_surface_withdrawal_m3[:, ID_Rivers_flow == i], 1)

            # Find exact area in river directory
            for River_part in River_dict.iteritems():
                if len(np.argwhere(River_part[1] == i)) > 0:

                    # Find the river part in the dictionery
                    row_discharge = np.argwhere(River_part[1] == i)[0][0]

                    # Subtract the withdrawal from that specific riverpart
                    Real_Surface_Withdrawal = np.minimum(
                        Discharge_dict_new[
                            River_part[0]][:, row_discharge].flatten(),
                        total_surface_withdrawal[:, None].flatten())

                    Water_Error += np.maximum(
                        np.nansum(total_surface_withdrawal[:, None].flatten() -
                                  Real_Surface_Withdrawal), 0)
                    Discharge_dict_new[River_part[
                        0]][:, 0:row_discharge] = Discharge_dict_new[River_part[
                            0]][:, 0:
                                row_discharge] - Real_Surface_Withdrawal[:,
                                                                         None]

                    # Subtract the withdrawal from the part downstream of the riverpart within the same dictionary
                    Discharge_dict_new[River_part[0]][np.logical_and(
                        Discharge_dict_new[River_part[0]] <= 0,
                        Discharge_dict[River_part[0]] >= 0)] = 0
                    End_river = River_dict[River_part[0]][0]
                    times = 0

                    # Subtract the withdrawal from all the other downstream dictionaries
                    while len(River_dict) > times:
                        for River_part_downstream in River_dict.iteritems():
                            if River_part_downstream[1][-1] == End_river:

                                Discharge_dict_new[River_part_downstream[
                                    0]][:, :] = Discharge_dict_new[
                                        River_part_downstream[
                                            0]][:, :] - Real_Surface_Withdrawal[:,
                                                                                None]
                                #Discharge_dict_new[River_part_downstream[0]][:,1:] = Discharge_dict_new[River_part_downstream[0]][:,1:] - total_surface_withdrawal[:,None]

                                Discharge_dict_new[River_part_downstream[0]][
                                    np.logical_and(
                                        Discharge_dict_new[
                                            River_part_downstream[0]] <= 0,
                                        Discharge_dict[
                                            River_part_downstream[0]] >=
                                        0)] = 0
                                End_river = River_dict[
                                    River_part_downstream[0]][0]
                                times = 0
                            times += 1

    return (Discharge_dict_new)
示例#9
0
def main(input_nc,
         output_nc,
         input_JRC,
         Inflow_Text_Files,
         include_reservoirs=1):

    import time
    import wa.General.raster_conversions as RC
    import wa.General.data_conversions as DC
    import numpy as np
    import netCDF4

    ####################### add inflow text files #################################

    if len(Inflow_Text_Files) > 0:

        import wa.Models.SurfWAT.Part0_Add_Inlets as Part0_Add_Inlets

        # Calculate the runoff that will be routed by including the inlets
        Runoff = Part0_Add_Inlets(input_nc, Inflow_Text_Files)

    else:
        # Extract runoff data from NetCDF file
        Runoff = RC.Open_nc_array(input_nc, Var='Runoff_M')

    ###############################################################################

    # Extract flow direction data from NetCDF file
    flow_directions = RC.Open_nc_array(input_nc, Var='demdir')

    # Extract basin data from NetCDF file
    Basin = RC.Open_nc_array(input_nc, Var='basin')
    Areas_in_m2 = RC.Open_nc_array(input_nc, Var='area')
    Runoff_in_m3_month = ((Runoff / 1000) * Areas_in_m2)

    ###############################################################################
    ############################### Run Part 1 ####################################
    ###############################################################################

    import wa.Models.SurfWAT.Part1_Channel_Routing as Part1_Channel_Routing
    Routed_Array, Accumulated_Pixels, Rivers = Part1_Channel_Routing.Run(
        Runoff_in_m3_month, flow_directions, Basin)

    ###############################################################################
    ################## Create NetCDF Part 1 results ###############################
    ###############################################################################

    ################### Get Example parameters for NetCDF #########################

    # Create NetCDF
    geo_out_example, epsg_example, size_X_example, size_Y_example, size_Z_example, Time_example = RC.Open_nc_info(
        input_nc)
    geo_out_example = np.array(geo_out_example)

    time_or = RC.Open_nc_array(input_nc, Var='time')

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

    ################################ Save NetCDF ##################################

    # Create NetCDF file
    nc_file = netCDF4.Dataset(output_nc, 'w', format='NETCDF4')
    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

    ######################### Save Rasters in NetCDF ##############################

    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]

    nc_file.createDimension('time', None)
    timeo = nc_file.createVariable('time', 'f4', ('time', ))
    timeo.units = 'Monthly'
    timeo.standard_name = 'time'

    # Variables
    rivers_var = nc_file.createVariable('rivers',
                                        'i', ('latitude', 'longitude'),
                                        fill_value=-9999)
    rivers_var.long_name = 'Rivers'
    rivers_var.grid_mapping = 'crs'

    accpix_var = nc_file.createVariable('accpix',
                                        'f8', ('latitude', 'longitude'),
                                        fill_value=-9999)
    accpix_var.long_name = 'Accumulated Pixels'
    accpix_var.units = 'AmountPixels'
    accpix_var.grid_mapping = 'crs'

    discharge_nat_var = nc_file.createVariable(
        'discharge_natural',
        'f8', ('time', 'latitude', 'longitude'),
        fill_value=-9999)
    discharge_nat_var.long_name = 'Natural Discharge'
    discharge_nat_var.units = 'm3/month'
    discharge_nat_var.grid_mapping = 'crs'

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

    # Static variables
    rivers_var[:, :] = Rivers[:, :]
    accpix_var[:, :] = Accumulated_Pixels[:, :]
    for i in range(len(time_or)):
        discharge_nat_var[i, :, :] = Routed_Array[i, :, :]

    time.sleep(1)
    nc_file.close()
    del Routed_Array, Accumulated_Pixels

    ###############################################################################
    ############################### Run Part 2 ####################################
    ###############################################################################

    import wa.Models.SurfWAT.Part2_Create_Dictionaries as Part2_Create_Dictionaries
    DEM_dict, River_dict, Distance_dict, Discharge_dict = Part2_Create_Dictionaries.Run(
        input_nc, output_nc)

    ###############################################################################
    ################## Create NetCDF Part 2 results ###############################
    ###############################################################################

    # Create NetCDF file
    nc_file = netCDF4.Dataset(output_nc, 'r+', format='NETCDF4')
    nc_file.set_fill_on()

    ###################### Save Dictionaries in NetCDF ############################

    parmsdem = nc_file.createGroup('demdict_static')
    for k, v in DEM_dict.items():
        setattr(parmsdem, str(k), str(v.tolist()))

    parmsriver = nc_file.createGroup('riverdict_static')
    for k, v in River_dict.items():
        setattr(parmsriver, str(k), str(v.tolist()))

    parmsdist = nc_file.createGroup('distancedict_static')
    for k, v in Distance_dict.items():
        setattr(parmsdist, str(k), str(v.tolist()))

    parmsdis = nc_file.createGroup('dischargedict_dynamic')
    for k, v in Discharge_dict.items():
        setattr(parmsdis, str(k), str(v.tolist()))

    # Close file
    time.sleep(1)
    nc_file.close()

    ###############################################################################
    ############################### Run Part 3 ####################################
    ###############################################################################

    if include_reservoirs == 1:
        import wa.Models.SurfWAT.Part3_Reservoirs as Part3_Reservoirs
        Discharge_dict_2, River_dict_2, DEM_dict_2, Distance_dict_2 = Part3_Reservoirs.Run(
            input_nc, output_nc, input_JRC)

    else:
        import copy
        Discharge_dict_2 = copy.deepcopy(Discharge_dict)
        River_dict_2 = copy.deepcopy(River_dict)
        DEM_dict_2 = copy.deepcopy(DEM_dict)
        Distance_dict_2 = copy.deepcopy(Distance_dict)

    ###############################################################################
    ################## Create NetCDF Part 3 results ###############################
    ###############################################################################

    # Create NetCDF file
    nc_file = netCDF4.Dataset(output_nc, 'r+', format='NETCDF4')
    nc_file.set_fill_on()

    ###################### Save Dictionaries in NetCDF ############################

    parmsdisres = nc_file.createGroup('dischargedictreservoirs_dynamic')
    for k, v in Discharge_dict_2.items():
        setattr(parmsdisres, str(k), str(v.tolist()))

    parmsrivresend = nc_file.createGroup('riverdictres_static')
    for k, v in River_dict_2.items():
        setattr(parmsrivresend, str(k), str(v.tolist()))

    parmsdemres = nc_file.createGroup('demdictres_static')
    for k, v in DEM_dict_2.items():
        setattr(parmsdemres, str(k), str(v.tolist()))

    parmsdistres = nc_file.createGroup('distancedictres_static')
    for k, v in Distance_dict_2.items():
        setattr(parmsdistres, str(k), str(v.tolist()))

    # Close file
    time.sleep(1)
    nc_file.close()

    del DEM_dict, River_dict, Distance_dict, Discharge_dict
    ###############################################################################
    ############################### Run Part 4 ####################################
    ###############################################################################

    import wa.Models.SurfWAT.Part4_Withdrawals as Part4_Withdrawals
    Discharge_dict_end = Part4_Withdrawals.Run(input_nc, output_nc)

    ###############################################################################
    ################## Create NetCDF Part 4 results ###############################
    ###############################################################################

    # Create NetCDF file
    nc_file = netCDF4.Dataset(output_nc, 'r+', format='NETCDF4')
    nc_file.set_fill_on()

    ###################### Save Dictionaries in NetCDF ############################

    parmsdisend = nc_file.createGroup('dischargedictend_dynamic')
    for k, v in Discharge_dict_end.items():
        setattr(parmsdisend, str(k), str(v.tolist()))

    # Close file
    time.sleep(1)
    nc_file.close()
    del Discharge_dict_end

    ###############################################################################
    ############### Part 5 Convert dictionaries to rasters ########################
    ###############################################################################

    River_dict = RC.Open_nc_dict(output_nc, 'riverdict_static')

    # End discharge dictionary to raster
    Discharge_dict_end = RC.Open_nc_dict(output_nc, 'dischargedictend_dynamic')
    DataCube_Discharge_end = DC.Convert_dict_to_array(River_dict,
                                                      Discharge_dict_end,
                                                      input_nc)

    ###################### Save Dictionaries in NetCDF ############################

    # Create NetCDF file
    nc_file = netCDF4.Dataset(output_nc, 'r+', format='NETCDF4')
    nc_file.set_fill_on()

    discharge_end_var = nc_file.createVariable(
        'discharge_end',
        'f8', ('time', 'latitude', 'longitude'),
        fill_value=-9999)
    discharge_end_var.long_name = 'End Discharge'
    discharge_end_var.units = 'm3/month'
    discharge_end_var.grid_mapping = 'crs'

    for i in range(len(time_or)):
        discharge_end_var[i, :, :] = DataCube_Discharge_end[i, :, :]

    # Close file
    nc_file.close()
    del DataCube_Discharge_end
示例#10
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 ()