Пример #1
0
def DeleteBasins(basins, pathout):
    """
    ===========================================================
         DeleteBasins(basins,pathout)
    ===========================================================
    this function deletes all the basins in a basin raster created when delineating
    a catchment and leave only the first basin which is the biggest basin in the raster
    
    Inputs:
    ----------
        1- basins:
            [gdal.dataset] raster you create during delineation of a catchment 
            values of its cells are the number of the basin it belongs to
        2- pathout:
            [String] path you want to save the resulted raster to it should include
            the extension ".tif"
    Outputs:
    ----------
        1- raster with only one basin (the basin that its name is 1 )
    
    Example:
    ----------
        basins=gdal.Open("Data/basins.tif")    
        pathout="mask.tif"
        DeleteBasins(basins,pathout)
    """
    # input data validation
    # data type
    assert type(pathout) == str, "A_path input should be string type"
    assert type(
        basins
    ) == gdal.Dataset, "basins raster should be read using gdal (gdal dataset please read it using gdal library) "

    # input values
    # check wether the user wrote the extension of the raster or not
    ext = pathout[-4:]
    assert ext == ".tif", "please add the extension at the end of the path input"

    # get number of rows
    rows = basins.RasterYSize
    # get number of columns
    cols = basins.RasterXSize
    # array
    basins_A = basins.ReadAsArray()
    # no data value
    no_val = np.float32(basins.GetRasterBand(1).GetNoDataValue())
    # get number of basins and there names
    basins_val = list(
        set([
            int(basins_A[i, j]) for i in range(rows) for j in range(cols)
            if basins_A[i, j] != no_val
        ]))

    # keep the first basin and delete the others by filling their cells by nodata value
    for i in range(rows):
        for j in range(cols):
            if basins_A[i, j] != no_val and basins_A[i, j] != basins_val[0]:
                basins_A[i, j] = no_val

    GIS.RasterLike(basins, basins_A, pathout)
Пример #2
0
def SaveParameters(DistParFn, Raster, Par, No_parameters, snow, kub, klb, 
                   Path=None):
    """
    ============================================================
        SaveParameters(DistParFn, Raster, Par, No_parameters, snow, kub, klb, Path=None)
    ============================================================
    this function takes generated parameters by the calibration algorithm, 
    distributed them with a given function and save them as a rasters
    
    Inputs:
    ----------
        1-DistParFn:
            [function] function to distribute the parameters (all functions are
            in Hapi.DistParameters )
        2-Raster:
            [gdal.dataset] raster to get the spatial information
        3-Par
            [list or numpy ndarray] parameters as 1D array or list
        4-no_parameters:
            [int] number of the parameters in the conceptual model
        5-snow:
            [integer] number to define whether to take parameters of 
            the conceptual model with snow subroutine or without
        5-kub:
            [numeric] upper bound for k parameter in muskingum function
        6-klb:
            [numeric] lower bound for k parameter in muskingum function
         7-Path:
             [string] path to the folder you want to save the parameters in
             default value is None (parameters are going to be saved in the
             current directory)
     
    Outputs:
    ----------
         Rasters for parameters of the distributed model
     
   Examples:     
   ----------
        DemPath = path+"GIS/4000/dem4000.tif"
        Raster=gdal.Open(DemPath)
        ParPath = "par15_7_2018.txt"
        par=np.loadtxt(ParPath)
        klb=0.5
        kub=1
        no_parameters=12
        DistParFn=DP.par3dLumped
        Path="parameters/"
        snow=0
        
        SaveParameters(DistParFn, Raster, par, no_parameters,snow ,kub, klb,Path)
    """
    assert callable(DistParFn), " please check the function to distribute your parameters"
    assert type(Raster)==gdal.Dataset, "raster should be read using gdal (gdal dataset please read it using gdal library) "
    assert type(Par)==np.ndarray or type(Par)==list, "par_g should be of type 1d array or list"
    assert type(No_parameters) == int, "No of parameters should be integer"
    assert isinstance(kub,numbers.Number) , " kub should be a number"
    assert isinstance(klb,numbers.Number) , " klb should be a number"
    assert type(Path) == str, "path should be of type string"
    assert os.path.exists(Path), Path + " you have provided does not exist"
    
    par2d=DistParFn(Par,Raster,No_parameters,kub,klb)
    # save 
    if snow == 0: # now snow subroutine
        pnme=["01rfcf.tif","02FC.tif", "03BETA.tif", "04ETF.tif", "05LP.tif", "06CFLUX.tif", "07K.tif",
              "08K1.tif","09ALPHA.tif", "10PERC.tif", "11Kmuskingum.tif", "12Xmuskingum.tif"]
    else: # there is snow subtoutine 
        pnme=["01ltt.tif", "02utt.tif", "03rfcf.tif", "04sfcf.tif", "05ttm.tif", "06cfmax.tif", "07cwh.tif",
              "08cfr.tif", "09fc.tif", "10fc.tif", "11beta.tif","12etf.tif","13lp.tif","14cflux.tif",
              "15k.tif","16k1.tif","17alpha.tif","18perc.tif"]
        
    if Path != None:
        pnme=[Path+i for i in pnme]

    for i in range(np.shape(par2d)[2]):
        GIS.RasterLike(Raster,par2d[:,:,i],pnme[i])