User Manual: IHEWA Python Tools (v0.1)
User Manual: IHEWA Python Tools (v0.1)
1)
Contents
1. Introduction...................................................................................................................................1
2. Installation.....................................................................................................................................3
2.2.1. IHEWAcollect.................................................................................................................9
3. Usage...........................................................................................................................................13
3.1. IHEWAcollect.......................................................................................................................13
3.2. IHEWAdataanalysis..............................................................................................................16
3.3. IHEWAengine.......................................................................................................................16
1. Introduction
IHEWA stands for IHE Delft Water Accounting, a cluster of open-source tools in Python programming
language for Water Accounting plus (WA+) framework, which is developed by Water Accounting
group at IHE Delft Institute for Water Education. The WA+ process involves collecting data and
information (Inputs), analyzing and assessing data quality, calculating water accounts, and reporting
results in sheets, maps, indicators… (Outputs) (Figure 1).
Figure 1: WA+ framework and the link with the water accounting analysts (users)
IHEWA Python tools are developed to assist the water accounting analysts (users) in implementing
the WA+ framework. These tools are developed in separate packages so it can also be used for
purposes other than water accounting. For example, the IHEWA package for collecting remote
sensing data can be installed and used without installation of other IHEWA packages. Below is the
overview of the current IHEWA packages:
IHEWAcollect: The first step in WA+ process is collecting inputs which includes remote
sensing data. Many remote sensing products are open-access but often provided using
different protocols (HTTPS, FTP, API, …). The IHEWAcollect package is developed to simplify
the process of downloading several datasets in bulk.
o Github repository: https://github.com/wateraccounting/IHEWAcollect
IHEWAdataanalysis: The next step after collecting remote sensing data is to analyze the data
and select suitable products for WA+ report. For example, water balance analysis can be
done to assess the quality of the available datasets for the main water balance components
such as precipitation (P), actual evapotranspiration (ET) and total water storage change (dS),
each of which can be estimated using different products. The IHEWAdataanalysis package
provides template scripts for such data analyses. At the moment, IHEWAdataanalysis has
one template to automatically generate basin average time-series, scatterplot, correlation
heat map of possible P-ET-dS combinations from available datasets. Comparing different
combinations of P-ET-dS will help water accounting analysts decide better which
combination of datasets is suitable for the region of interest. IHEWAdatanalysis can be
further developed to incorporate other data analyses.
o Github repository: https://github.com/wateraccounting/IHEWAdataanalysis
IHEWAengine: The last step in WA+ process is to estimate other variables that are not
available from remote sensing products, and then synthesize water accounts. For this
purpose, many different hydrological models can be potentially used and adapted to WA+
requirements. IHEWAengine package is developed with flexible modular structure which
provides the WA+ ‘engine’ with a protocol to communicate with different built-in models
(developed for WA+) or plug-ins of other common hydrological models (e.g. SWAT+, WEAP,
MODFLOW).
o Github repository: https://github.com/wateraccounting/IHEWAengine
This manual guides the users (water accounting analysts) on how to install Python environment
(Anaconda distribution, core packages, projects version control) and IHEWA packages. For each
package, an example dataset (test case) is available and users will be guided on how to use the
package with the example dataset.
This manual is for Windows users (recommended Operating System: Windows 10 64-bit). For Linux
user, all the steps can be found in the Dockerfile in the Github repository.
2. Installation
2.1. Python set up
IHEWA Python tools is developed in Python 3.7 environment. We recommend Anaconda3
distribution of Python for simple Python environment and packages manager.
2.1.1. Install Anaconda3
We suggest to install Anaconda3 (https://www.anaconda.com/). It is a free and open-source
distribution of the Python and R programming languages for scientific computing (data science,
machine learning applications, large-scale data processing, predictive analytics, etc.), that aims to
simplify package management and deployment.
After installing Anaconda3, a Python 3.7 environment must be created since the base environment
of the latest Anaconda release is 3.8. First, start Anaconda prompt with ‘Run as administrator’
(Figure 2).
https://github.com/OSGeo/gdal.
https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal.
http://www.gisinternals.com/release.php.
The following steps are to install GDAL from the binaries downloaded from Tamas Szekeres’
window binaries solution.
i. Before download the binary files, check which version you need. This is concerned with the
MSVC version used to compile python and the windows version.
The version number from the screenshot is v.1916 and the operating system is 64 bit
(AMD64). MSC v.1916 corresponds to MSVC 2017 (see
https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B).
ii. Find the corresponding binaries from the link http://www.gisinternals.com/release.php. For
the above case, the corresponding link is as shown below in the screenshot.
Selecting a release will take you to a new screen promoting a handful of EXE and MSI files.
First, download and install the core components of GDAL which is the MSI file corresponding
to your python setup. For the above case, the corresponding MSI file is as shown in the
screenshot below.
After downloaded the installation msi file, double click it and the installation will start.
Figure 6: GDAL installation window1
After installing GDAL, the next step is to set system path of GDAL, where the system can find
the executable files and the data.
If you install GDAl from windows binaries, the default installation path is “C:\Program
Files\GDAL”
i. Open “Control Panel”,
ii. go to “System”,
iii. find the “Advanced system settings” on the left side panel,
iv. click it then a system properties window will pop up
v. Under “Advanced” tab, click the “Environment Variables” button to open the
“Environmental Variables” window
vi. Select “path” in the users variable and click on ‘Edit…” to open the “Edit environmental
variable” window
vii. Click new and add the GDAL path “C:\Program Files\GDAL”
viii. Close the above window and click “Edit…” under the ‘System Variable” to open the “Edit
System variable” window
ix. Add a path variable called “GDAL_DATA” and set the value to “C:\Program
Files\GDAL\gdal-data”
iii. Validate gdal import. Open the cmd or Anaconda prompt and start python. Inside the
python running environment import gdal.
If this failed please look at this and try to correct the paths
https://stackoverflow.com/questions/35500176/importerror-no-module-named-gdal
IHEWAcollect is hosted on PyPi as a python package. To install it from PyPi, type “pip install
IHEWAcollect”. All the dependencies will be installed automatically.
First clone the source code from github repository. Under the root folder of the repository
type “python setup.py install”.
2.2.2. IHEWAdataanalysis and IHEWAengine
IHEWAdataanalysis and IHEWAengine packages are not yet available on the Python Package Index
(PyPI) repository. These packages need to be installed using a setup Python scripts in the package
repository. The package repository, therefore, needs to be downloaded first following these steps:
Git is a free and open source distributed version control system designed to handle everything from
small to very large projects with speed and efficiency. Git setup file can be downloaded from
https://git-scm.com/downloads. Select the latest source release to download and run the setup file
with all default options.
The HTTPS URL can also be found on the Github repository of the package (Figure 16):
First, right-click in a folder to download the repository, then select ‘Git Bash Here’ (Figure 17)
In the Git Bash prompt, enter this command: git clone [HTTPS URL of the package] (Figure 18)
First, run Anaconda prompt as administrator. In Anaconda Prompt window, change working
directory to the folder cloned from package repository using cd command. For example, in Figure 18,
IHEWAdataanalysis package is cloned in ‘D:\Projects’ folder, then the directory should be changed to
‘D:\Projects\IHEWAdataanalysis’ (Figure 19) using commands:
D:
cd .\Projects\IHEWAdataanalysis\
Figure 19: Screen capture of installing IHEWAdataanlysis package using setup file
After that, activate a Python 3.7 environment (set up before). Then, enter the command:
The package is successfully installed once a message ‘Finished processing…’ is shown (Figure 20).
3. Usage
3.1. IHEWAcollect
IHEWAcollect is a collection of tools to download remote sensing data. Most of the remote sensing
data require to create an account to be able to access the datasets. Product names, their source and
account type for supported products are given in Table 2. The parameters of all the products can be
found from https://github.com/wateraccounting/IHEWAcollect/blob/master/docs/products.rst.
For example, to use IHEWA_GUESS account we need to have the following in the “accounts.yml”
file.
IHEWA_GUESS:
username:
'wateraccountingguest'
password: 'W@t3r@ccounting'
apitoken: null
Create a folder called “Data” where you would like to save the downloaded data and save the
“accounts.yml” file in the folder.
Create a python script, and save in the data folder together with the “accounts.yml” file.
i. Import packages
# -*- coding: utf-8 -*-
import inspect
import os
import IHEWAcollect
ii. Write main function. To use IHEWAcollect download datasets, it’s easily to call
method “Download”, and pass parameters to the method.
def main(path, test_args):
__
for key, value in test_args.items():
print('\n{:>4s}'
'{:>20s}{:>6s}{:>20s}{:>20s}{:>20s}\n'
'{:->90s}'.format(key,
value['product'],
value['version'],
value['parameter'],
value['resolution'],
value['variable'],
'-'))
IHEWAcollect.Download(workspace=path,
product=value['product'],
version=value['version'],
parameter=value['parameter'],
resolution=value['resolution'],
variable=value['variable'],
bbox=value['bbox'],
period=value['period'],
nodata=value['nodata'],
is_status=False,
is_save_temp=True,
is_save_remote=True)
main(path, test_args)
To download a product, you need to specify the name of the product, name of the
variable, frequency (time resolution of the data), the bounding box of the area in
degrees of latitude and longitude and the start and end of the time period for which
the data need to be downloaded. These need to be specified as arguments to the
main function as shown in the above example.
Once the script Run the example and the accounts file are save in a data folder, then
you can run the script in an IDE like Spyder or in the command window.
To run the script in the command window, start python and type “python scriptname.py”,
where the “scriptname” is the name of the script.
After finish the download data, a “IHEWAcollect” folder will be created UNDER THE
DATA FOLDER. Inside it contains 3 sub-folders. The final data is stored inside
download folder. The remote folder keeps the data downloaded from server. The
temporary folder stores the temporary files during the data processing.
3.2. IHEWAdataanalysis
IHEWAdataanalysis can have many templates for data analyses. Currently, the package has one
template for comparing main components of water balance (precipitation, actual
evapotranspiration, and total water storage change) from different remote sensing products. First,
monthly raster data of each product need to be collected, which can be done using IHEWAcollect.
Then, a monthly basin-wide average time-series and a mean annual raster of each product needs to
be prepared in Step 1. After that, in Step 2 and Step 3, a configuration file was created to list all
available products with the path to the prepared data. In Step 4, the information and data from the
configuration file (.yml) are passed to the analysis template scripts. All possible combination of P, ET,
dS will be generated and compared in heatmap, scatterplot, and maps.
cmd2 = 'gdalwarp -of GTiff -overwrite -s_srs epsg:4326 -t_srs epsg:4326 -r near -tr 0.05 0.05 -cutline {shp}
-crop_to_cutline {fi} {fo}'
import IHEWAdataanalysis
Second, write main function. To use IHEWAdataanalysis analysis datasets, it’s easily to call method
“Analysis”, and pass yaml file to the method.
if __name__ == "__main__":
print('\nAnalysis\n=====')
path = os.path.join(
os.getcwd(),
os.path.dirname(
inspect.getfile(
inspect.currentframe()))
)
Create a YAML file, and name it as “example.yml”, which should be placed in the same folder with
the python script. Define basin name, example “area1”. Under basin
“hydrology” block defines the basin area, hydrological year, analysis period and area
shapefile.
“directory” block defines the data path.
areas:
area1:
hydrology:
# km2 / 1000.0 = Mm2
area: 21.503
year: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]
# csv column
date:
- start: '2005-01-01'
- end: '2012-12-31'
basin:
shp:
folder: 'data/Shapefile'
fname: 'Mindanao-RiverBasin.shp'
directory:
ETA:
ALEXI:
tif:
folder: 'data/Output/tif'
fname: 'ALEXI_yearly.tif'
csv:
folder: 'data/Output/csv'
fname: 'ETA-ALEXI.csv'
column: 'mean_1'
index: 'date'
Step 4: Run the analysis script
In Anaconda prompt window, activate python environment and change working directory to where
example.py python script is saved. Then, run the command:
python example.py
After that, the output graphs and csv files will be saved in the folder IHEWAdatanalysis under the
working directory (Annex 1).
3.3. IHEWAengine
IHEWAengine packages has 3 main modules: (1) Engine 1 includes pixel-based water balance and
routing model, which are used to estimate variables not available from input data (e.g. incremental
ET, rainfall ET, runoff, return flow, discharge…), (2) Engine 2 module computes the indicators and
values in WA+ sheets from both input and output data of Engine 1, and (3) plugins module is
developed to incorporate the results from other hydrological models to WA+ sheets (e.g. SWAT+,
MODFLOW, WEAP…).
The WA_Hyperloop 1package has been integrated in IHEWAengine.engine2 module. This example
below shows how to run Hyperloop in IHEWAengine.
The example input data for Hyperloop can be found in the project repository:
https://github.com/wateraccounting/IHEWAengine/tree/master/examples/data/engine2/Hyperloop
Create a python script, and save it as “example.py”. The codes in the examples folder
“ex_engine2_Hyperloop.py” is the details steps. It can be simplified (or replaced) by the follow
codes.
if __name__ == "__main__":
print('\nEngine\n=====')
path = os.path.join(
os.getcwd(),
os.path.dirname(
inspect.getfile(
inspect.currentframe()))
)
Create a YAML file, and name it as “example.yml” and save in the same folder with ‘example.py’.
Define basin name, example “area1”. Under basin, the following configuration parameters need to
be defined:
bf: 'Baseflow'
sr: 'SurfaceRunoff'
tr: 'TotalRunoff'
perc: 'Percolation'
dperc: 'IncrementalPercolation'
supply_total: 'Supply'
dro: 'IncrementalRunoff'
etb: 'ETblue'
etg: 'ETgreen'
rzsm: 'RootDepthSoilMoisture'
In Anaconda prompt window, activate python environment and change working directory to where
example.py python script is saved. Then, run the command:
python example.py
After that, the output graphs and csv files will be saved in the folder IHEWAengine under the
working directory (Annex 2)
Scatter chart
Bar chart
P-ET-dS map chart
Heat map chart
2. Folder tree of IHEWAengine.engine2 working directory
2.1. Input folder
+---hydsim
| |
| +---Baseflow
| |
| +---ETblue
| |
| +---ETgreen
| |
| +---IncrementalPercolation
| |
| +---IncrementalRunoff
| |
| +---Percolation
| |
| +---RootDepthSoilMoisture
| |
| +---Supply
| |
| +---SurfaceRunoff
| |
| \---TotalRunoff
|
+---remote
| +---ETref
| |
| +---Evaporation
| |
| +---LAI
| |
| +---NDM
| |
| +---Precipitation
| |
| \---RainyDays
|
+---series
| +---GRACE
| | GSFC-average_mmwe.csv
| |
| \---Growing Seasons
| palm_perennial.txt
| rice_irrigated_java.txt
| rice_rainfed_java.txt
|
+---static
| Cattle.tif
| DEM-HydroShed_m_3s.tif
| EWR.tif
| GMIA-aeisw_pct_aei_v5.asc
| LU.tif
| Population.tif
| SubBasin.tif
| WPL.tif
2.2. Output folder
| \---Example0
| |
| +---data
| | |
| | +---demand
| | |
| | +---fractions
| | | |
| | | +---fractions
| | | |
| | | \---fractions_dryness
| | |
| | +---i
| | |
| | +---return_fractions
| | |
| | +---return_gwgw
| | |
| | +---return_gwsw
| | |
| | +---return_swgw
| | |
| | +---return_swsw
| | |
| | +---supply_gw
| | |
| | +---supply_sw
| | |
| | +---supply_swa
| | |
| | \---t
| |
| +---data_maps
| | +---bf
| | |
| | +---diff
| | |
| | +---dperc
| | |
| | +---dperc_corr
| | |
| | +---dro
| | |
| | +---dro_corr
| | |
| | +---et
| | |
| | +---etb
| | |
| | +---etg
| | |
| | +---etref
| | |
| | +---lai
| | |
| | +---n
| | |
| | +---ndm
| | |
| | +---p
| | |
| | +---perc
| | |
| | +---r
| | |
| | +---rzsm
| | |
| | +---sr
| | |
| | +---sr_corr
| | |
| | +---supply_total
| | |
| | +---supply_total_corr
| | |
| | +---tr
| | |
| | \---tr_corr
| |
| +---sheet1
| | |
| | +---sheet1_monthly
| | |
| | \---sheet1_yearly
| |
| +---sheet2
| | +---sheet2_monthly
| | |
| | \---sheet2_yearly
| |
| +---sheet3
| | +---WP_Y_Seasonly_csvs
| | |
| | +---WP_Y_Seasonly_graphs
| | |
| | +---WP_Y_Yearly_csvs
| | |
| | \---WP_Y_Yearly_graphs
| |
| +---sheet4
| | |
| | +---sheet4_monthly
| | |
| | \---sheet4_yearly
| |
| +---sheet5
| | +---sheet5_monthly
| | |
| | \---sheet5_yearly
| |
| +---sheet6
| | +---sheet6_monthly
| | |
| | \---sheet6_yearly
| |
| \---sheet7
| +---Feed
| |
| +---Fuel
| |
| +---RZstor
| |
| +---sheet7_monthly
| |
| +---sheet7_yearly
| |
| +---split_y
| |
| +---temp_cattle
| |
| \---temp_et_recycle