Python in Hydrology In
Total Page:16
File Type:pdf, Size:1020Kb
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 Chapter 0. Preface Acknowledgements 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 [email protected]. 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 v 1 Getting started 1 1.1 Why Python? . .1 1.2 Python Installation . .1 1.3 Install additional packages . .2 1.4 Interactive Development Environment . .3 1.5 Execute the program . .4 1.6 Type of errors . .4 1.7 The first program . .5 2 A bit of Python 7 2.1 Data type . .7 2.2 Data structures . .9 2.3 Data structures . .9 2.4 Choosing the name of variable . 12 2.5 Operators and operands . 13 2.6 Expressions . 15 2.7 Control Flow . 15 2.8 Function . 18 2.9 Plotting . 20 3 Array 21 3.1 Generating sequential arrays . 21 3.2 Useful attributes and methods . 23 3.3 Indexing . 25 3.4 Array Manipulation . 26 viii Contents 4 Basic applications in Hydrology 29 4.1 Introdution . 29 4.2 Water Vapor . 29 4.3 Precipitation . 30 4.4 Rainfall . 31 4.5 Evaporation . 34 4.6 Infiltration . 38 4.7 Surface water . 39 4.8 River Routing–Muskingum method . 41 5 Statistics 43 5.1 Empirical distributions . 43 5.2 Theoretical distributions . 48 5.3 The t-test . 54 5.4 KS test . 55 5.5 The chi square test . 55 5.6 Measure of statistical dependence . 56 5.7 Linear regression . 59 5.8 Polynomial regression . 60 5.9 Interpolation . 61 5.10 Autocorrelation . 63 5.11 Uncertainty Intervals . 66 6 Spatial Data 69 6.1 Types of spatial data . 69 6.2 Geoinformation . 72 6.3 Writing Raster . 72 6.4 Writing Vector . 73 6.5 Reading the raster . 74 6.6 Read the vector . 75 6.7 Filtering . 75 6.8 NDVI . 77 Contents ix 7 Plotting 81 7.1 Date axis . 81 7.2 Bar charts . 82 7.3 Pie charts . 83 7.4 2 D plots . 84 7.5 3 D plots . 88 7.6 Box-plot . 88 7.7 Q-Q plot . 89 7.8 plotyy . 90 7.9 Annotation . 92 7.10 Basemap . 92 7.11 Shared axis . 94 7.12 Subplot . 95 8 Input-Output 99 8.1 xls . 99 8.2 Text file . 101 8.3 NetCDF . 102 8.4 Pickle . 104 9 Numerical Modelling 105 9.1 Integration . 105 9.2 ODE . 106 9.3 Parameter Estimation . 107 10 Advance statistics 111 10.1 copula . 111 10.2 Multivariate distribution . 113 A Install library 115 A.1 Basemap . 115 x Contents Chapter 1 Getting started 1.1 Why Python? Python is a simple and powerful programming language. By simple I mean, that it is much more forgiving than languages like C though slow also. By powerful, I mean it can glue many existing code which were written in C, C++, Fortran etc. easily. This has a growing user community which makes many tools easily available. Python Package Index which is a major host of the Python code, has more than 15,000 packages listed, which speaks about it popularity. Use of Python in hydrol- ogy community is not so fast as compared to other field, but now a days many new hydrological packages are being developed. Python provides access to a nice combination of GIS tools, Mathe- matics, Statistics etc., which make it a useful language for the hydrologist. Following are the major advantages of Python for the hydrologist: 1. A clear and.