Python
In Hydrology
Version 0.0.0
Sat Kumar Tomer
Copyright © 2011 Sat Kumar Tomer.
Printing history:
November 2011: First edition of Python in Hydrology.
Permission is granted to copy, distribute, and/or modify this document under the terms of the GNU Free Doc-
umentation License, Version 1.1 or any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and with no Back-Cover Texts.
The GNU Free Documentation License is available from www.gnu.org or by writing to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
The original form of this book is LATEX source code. Compiling this LATEX source has the effect of generating
a device-independent representation of a textbook, which can be converted to other formats and printed.
iv
Preface
History
I started using Python in July 2010.
I was looking for a programming language which is open
source, and can combine many codes/modules/software. I came across Python and Perl, though
there might be many more options available. I googled the use of Python and Perl in the field of
general scientific usage, hydrology, Geographic Information System (GIS), statistics, and somehow
found Python to be the language of my need. I do not know if my conclusions about the Python
versus Perl were true or not? But I feel happy for Python being my choice, and it is fulfilling my
requirements.
After using Python for two-three months, I become fond of open source, and started using only
open source software, I said good-bye to Windows, ArcGis, MATLAB. And even after one year, I
do not miss them. I felt that I should also make a small contribution into the free world. I tried to
contribute in few areas, but could not go ahead because of my less knowledge in those areas. After
spending extensive one year with Python, I thought to make some contribution into Python world. I
wrote few small packages for the Python, and started writing this book.
I always have been scared of reading books, especially those having more than 200 pages. I do not
remember if I have read any book completely which had more than 200 pages. Though the list of
books, that I have read is very small, even for the books which had pages less than 200. I do not
like much of the text in the book, and like to learn from examples in the book. I am a student, not
a professor, so does not have idea about what students like except my own feeling which I know
many of my fellow students do not like.
I hope that you will find this book helpful and enjoyable.
Sat Kumar Tomer
Bangalore, India
Sat Kumar Tomer is a Phd Student in the department of civil engineering at Indian Institute of
Science, Bangalore, India.
vi
Acknowledgements
Chapter 0. Preface
I am thankful to you for reading the book, and hope to receive feedback from you.
I am thankful to Allen B. Downey who provided the latex source code of his books, which helped
me in formatting the book in better way. Apart from it, he has written a nice history for his book
titled ‘Think Python: How to Think Like a Computer Scientist’, which encouraged me for writing
this book. And, I copied many sentences from his book, mainly the basic about Python.
I am thankful to the Free Software Foundation for developing the GNU Free Documentation License.
I am thankful to Green Tea Press.
I am thankful to my friends Aditya Singh, Ganjendara Yadav, Himanshu Joshi, Katarina Turcekova,
Shadi Davarian, and Shobhit Khatyan, for their continuous support.
Contributor List
If you have a suggestion or correction, please send email to satkumartomer@gmail.com. If I make
a change based on your feedback, I will add you to the contributor list (unless you ask to be omitted).
If you include at least part of the sentence the error appears in, that makes it easy for me to search.
Page and section numbers are fine, too, but not quite as easy to work with. Thanks!
Contents
Preface
1 Getting started
1.1 Why Python? .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2
1.3
1.4
1.5
1.6
1.7
Python Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Install additional packages
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interactive Development Environment
. . . . . . . . . . . . . . . . . . . . . . .
Execute the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Type of errors .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The first program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 A bit of Python
Data type
2.1
.
.
.
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3 Array
Data structures
Data structures
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Choosing the name of variable . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operators and operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Expressions .
.
Control Flow .
Function .
Plotting .
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1
3.2
3.3
3.4
Generating sequential arrays
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Useful attributes and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Indexing .
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Array Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
v
1
1
1
2
3
4
4
5
7
7
9
9
12
13
15
15
18
20
21
21
23
25
26
viii
4 Basic applications in Hydrology
4.1
Introdution .
4.2 Water Vapor .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Precipitation .
Rainfall
.
.
.
Evaporation .
Infiltration .
.
Surface water
River Routing–Muskingum method . . . . . . . . . . . . . . . . . . . . . . . . .
4.3
4.4
4.5
4.6
4.7
4.8
Contents
29
29
29
30
31
34
38
39
41
43
43
48
54
55
55
56
59
60
61
63
66
69
69
72
72
73
74
75
75
77
5 Statistics
5.1
5.2
5.3
5.4
5.5
Empirical distributions
Theoretical distributions
The t-test
KS test .
.
.
.
.
.
.
.
.
.
.
.
The chi square test
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.6 Measure of statistical dependence
. . . . . . . . . . . . . . . . . . . . . . . . .
5.7
5.8
5.9
Linear regression .
.
.
Polynomial regression .
Interpolation .
.
.
5.10 Autocorrelation .
5.11 Uncertainty Intervals
6 Spatial Data
6.1
6.2
Types of spatial data .
Geoinformation .
6.3 Writing Raster .
6.4 Writing Vector .
.
.
6.5
6.6
6.7
6.8
Reading the raster .
Read the vector
Filtering .
NDVI
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.