Open In Colab

View Source on GitHub

HRU discretization for Laos

This notebook shows hru discretization using the land use, soil and sub-bains shapefiles of Houay Pano catchment, Laos.

[1]:

import os from ai4water.utils.utils import get_version_info from ai4water.preprocessing.spatial_processing import MakeHRUs from ai4water.datasets import MtropicsLaos
D:\C\Anaconda3\envs\tfcpu27_py39\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
D:\C\Anaconda3\envs\tfcpu27_py39\lib\site-packages\numpy\.libs\libopenblas.EL2C6PLE4ZYW3ECEVIV3OXXGRN2NRFM2.gfortran-win_amd64.dll
D:\C\Anaconda3\envs\tfcpu27_py39\lib\site-packages\numpy\.libs\libopenblas.GK7GX5KEQ4F6UYO3P26ULGBQYHGQO7J4.gfortran-win_amd64.dll
  warnings.warn("loaded more than 1 DLL from .libs:"
D:\C\Anaconda3\envs\tfcpu27_py39\lib\site-packages\sklearn\experimental\enable_hist_gradient_boosting.py:16: UserWarning: Since version 1.0, it is not needed to import enable_hist_gradient_boosting anymore. HistGradientBoostingClassifier and HistGradientBoostingRegressor are now stable and can be normally imported from sklearn.ensemble.
  warnings.warn(
[2]:
for lib, ver in get_version_info().items():
    print(lib, ver)
python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:20:16) [MSC v.1916 64 bit (AMD64)]
os nt
ai4water 1.06
lightgbm 3.3.1
tcn 3.4.0
catboost 0.26
xgboost 1.5.0
easy_mpl 0.21.3
SeqMetrics 1.3.3
tensorflow 2.7.0
keras.api._v2.keras 2.7.0
numpy 1.21.0
pandas 1.3.4
matplotlib 3.4.3
h5py 3.5.0
sklearn 1.0.1
shapefile 2.3.0
fiona 1.8.22
xarray 0.20.1
netCDF4 1.5.7
optuna 2.10.1
skopt 0.9.0
hyperopt 0.2.7
plotly 5.3.1
lime NotDefined
seaborn 0.11.2

if the above cell does not show `fiona <https://github.com/Toblerity/Fiona>`__ as installed, we can not proceed because fiona is required for processing of shapefiles of Laos. See notebook `hru_discretization.ipynb <https://github.com/AtrCheema/AI4Water/blob/master/examples/paper/hru_discretization.ipynb>`__ for results with hypothetical catchment. If you are on windows, you may try installing fiona from wheels

[3]:
laos = MtropicsLaos(path="F:\\data\\MtropicsLaos")
shapefiles_path = laos.ds_dir
print(f'directory where Laos data is saved is: {shapefiles_path}')

    Not downloading the data since the directory
    F:\data\MtropicsLaos already exists.
    Use overwrite=True to remove previously saved files and download again
directory where Laos data is saved is: F:\data\MtropicsLaos
[4]:
Soil_shp = os.path.join(shapefiles_path, 'soilmap', 'soilmap.shp')
SubBasin_shp = os.path.join(shapefiles_path, 'subs1', 'subs1.shp')

years = {2011: {'shapefile': os.path.join(shapefiles_path, 'lu1', 'LU2011.shp'), 'feature': 'NAME'},
         2012: {'shapefile': os.path.join(shapefiles_path, 'lu1', 'LU2012.shp'), 'feature': 'NAME'},
         2013: {'shapefile': os.path.join(shapefiles_path,'lu1', 'LU2013.shp'), 'feature': 'NAME'},
         2014: {'shapefile': os.path.join(shapefiles_path,'lu1', 'LU2014.shp'), 'feature': 'NAME'},
         }
[5]:
def run_and_plot(index):


    for yr in index:
        hru_object.draw_pie(yr, n_merge=4, title=False, textprops={'fontsize': '12'})

    hru_object.plot_as_ts(min_xticks=3, max_xticks=4)
[6]:
hru_object = MakeHRUs(
    'unique_lu_soil',
    index=years,
    soil_shape={'shapefile': Soil_shp, 'feature': 'SOIL_GROUP'}
)

hru_object.call()
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
__len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.
__len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.
__getitem__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.
../../_images/_notebooks_preprocessing_hru_discretization_laos_7_2.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_7_4.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_7_6.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_7_8.png
[7]:
for yr in years:
    hru_object.draw_pie(yr, n_merge=4, title=False, textprops={'fontsize': '12'})
../../_images/_notebooks_preprocessing_hru_discretization_laos_8_0.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_8_1.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_8_2.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_8_3.png
[8]:
_ = hru_object.plot_as_ts(min_xticks=3, max_xticks=4)
AutoDateLocator was unable to pick an appropriate interval for this date range. It may be necessary to add an interval value to the AutoDateLocator's intervald dictionary. Defaulting to 6.
AutoDateLocator was unable to pick an appropriate interval for this date range. It may be necessary to add an interval value to the AutoDateLocator's intervald dictionary. Defaulting to 6.
AutoDateLocator was unable to pick an appropriate interval for this date range. It may be necessary to add an interval value to the AutoDateLocator's intervald dictionary. Defaulting to 6.
../../_images/_notebooks_preprocessing_hru_discretization_laos_9_1.png
[9]:
years = {}
for yr in range(1998, 2020):
    years[yr] = {'shapefile': os.path.join(shapefiles_path, 'lu1', f'LU{yr}.shp'), 'feature': 'NAME'}
[10]:
hru_object = MakeHRUs(
    'unique_lu_soil',
    index=years,
    soil_shape={'shapefile': Soil_shp, 'feature': 'SOIL_GROUP'}
)

hru_object.call()
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_1.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_3.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_5.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_7.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_9.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_11.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_13.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_15.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_17.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_19.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_21.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_23.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_25.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_27.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_29.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_31.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_33.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_35.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_37.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_39.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_41.png
Checking validity of landuse shapefile
soil 0 is valid now
soil 1 is valid now
soil 2 is valid now
../../_images/_notebooks_preprocessing_hru_discretization_laos_11_43.png
[11]:
for yr in years:
    hru_object.draw_pie(yr, n_merge=4, title=False, textprops={'fontsize': '12'})
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_0.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_1.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_2.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_3.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_4.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_5.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_6.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_7.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_8.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_9.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_10.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_11.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_12.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_13.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_14.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_15.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_16.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_17.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_18.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_19.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_20.png
../../_images/_notebooks_preprocessing_hru_discretization_laos_12_21.png
[12]:
_ = hru_object.plot_as_ts(min_xticks=3, max_xticks=4)
../../_images/_notebooks_preprocessing_hru_discretization_laos_13_0.png
[ ]: