100% found this document useful (3 votes)
30 views36 pages

Full Download Python For Water and Environment 2024th Edition Anil Kumar PDF

Kumar

Uploaded by

nechevarbin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
30 views36 pages

Full Download Python For Water and Environment 2024th Edition Anil Kumar PDF

Kumar

Uploaded by

nechevarbin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Download the full version of the textbook now at textbookfull.

com

Python for Water and Environment 2024th


Edition Anil Kumar

https://textbookfull.com/product/python-for-water-
and-environment-2024th-edition-anil-kumar/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Bionanotechnology: Principles and Applications 1st Edition


Anil Kumar Anal

https://textbookfull.com/product/bionanotechnology-principles-and-
applications-1st-edition-anil-kumar-anal/

textbookfull.com

Fuzzy Machine Learning Algorithms for Remote Sensing Image


Classification 1st Edition Anil Kumar

https://textbookfull.com/product/fuzzy-machine-learning-algorithms-
for-remote-sensing-image-classification-1st-edition-anil-kumar/

textbookfull.com

Data Structures with C Programming 2nd Edition Anil Kumar


Yadav

https://textbookfull.com/product/data-structures-with-c-
programming-2nd-edition-anil-kumar-yadav/

textbookfull.com

A Murder for the Books Blue Ridge Library Mystery 1 1st


Edition Victoria Gilbert

https://textbookfull.com/product/a-murder-for-the-books-blue-ridge-
library-mystery-1-1st-edition-victoria-gilbert/

textbookfull.com
Media and Conflict in the Social Media Era in China 1st
Edition Shixin Ivy Zhang

https://textbookfull.com/product/media-and-conflict-in-the-social-
media-era-in-china-1st-edition-shixin-ivy-zhang/

textbookfull.com

Problems in Classical and Quantum Mechanics Extracting the


Underlying Concepts 1st Edition J. Daniel Kelley

https://textbookfull.com/product/problems-in-classical-and-quantum-
mechanics-extracting-the-underlying-concepts-1st-edition-j-daniel-
kelley/
textbookfull.com

Integrated Water Resource Management: An Interdisciplinary


Approach 1st Edition Neil S. Grigg (Auth.)

https://textbookfull.com/product/integrated-water-resource-management-
an-interdisciplinary-approach-1st-edition-neil-s-grigg-auth/

textbookfull.com

Nonviolence The History of a Dangerous Idea Modern Library


Chronicles Kurlansky

https://textbookfull.com/product/nonviolence-the-history-of-a-
dangerous-idea-modern-library-chronicles-kurlansky/

textbookfull.com

Microbiology for Dummies 1st Edition Jennifer C. Stearns

https://textbookfull.com/product/microbiology-for-dummies-1st-edition-
jennifer-c-stearns/

textbookfull.com
Between Sanity and Madness: Mental Illness from Ancient
Greece to the Neuroscientific Era 1st Edition Allan V.
Horwitz
https://textbookfull.com/product/between-sanity-and-madness-mental-
illness-from-ancient-greece-to-the-neuroscientific-era-1st-edition-
allan-v-horwitz/
textbookfull.com
Innovations in Sustainable Technologies and Computing

Anil Kumar · Manabendra Saharia

Python for
Water and
Environment
Innovations in Sustainable Technologies
and Computing

Series Editors
Jagdish Chand Bansal, Department of Mathematics, South Asian University,
New Delhi, India
Joong Hoon Kim, School of Civil, Environmental and Architectural Engineering,
Korea University, Seoul, Korea (Republic of)
Atulya K. Nagar, Liverpool Hope University, Liverpool, UK
The book series aims to publish research on the analysis and development of techno-
logical innovations that consider natural resources and nurture economic and social
development. Innovative sustainable technologies are expected to develop sustain-
able production planning and tools, which reduces environmental and ecological
risks drastically.
The series covers research related to innovative solutions in the field of sustainable
technology, computing, and communication. Computational methodologies in the
field of computer science and engineering, cybersecurity, data science, information
systems and software engineering, algorithms for communication, smart transport
system, smart city planning, e-waste management system, and other such sustainable
technological solutions within the scope of this series.
The series will publish monographs, edited volumes, textbooks and proceedings of
important conferences, symposia and meetings in the field of sustainable technology
and computing.
Anil Kumar · Manabendra Saharia

Python for Water


and Environment
Anil Kumar Manabendra Saharia
Department of Civil Engineering Department of Civil Engineering
Indian Institute of Technology Delhi Indian Institute of Technology Delhi
New Delhi, Delhi, India New Delhi, Delhi, India

ISSN 2731-880X ISSN 2731-8818 (electronic)


Innovations in Sustainable Technologies and Computing
ISBN 978-981-99-9407-6 ISBN 978-981-99-9408-3 (eBook)
https://doi.org/10.1007/978-981-99-9408-3

© The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature
Singapore Pte Ltd. 2024

This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether
the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse
of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and
transmission or information storage and retrieval, electronic adaptation, computer software, or by similar
or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication
does not imply, even in the absence of a specific statement, that such names are exempt from the relevant
protective laws and regulations and therefore free for general use.
The publisher, the authors, and the editors are safe to assume that the advice and information in this book
are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or
the editors give a warranty, expressed or implied, with respect to the material contained herein or for any
errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.

This Springer imprint is published by the registered company Springer Nature Singapore Pte Ltd.
The registered company address is: 152 Beach Road, #21-01/04 Gateway East, Singapore 189721,
Singapore

Paper in this product is recyclable.


To my family,
Late Nilakhi Saharia, Prof. Shrutidhara
Sarma, Ramesh Chandra Saharia, Dhiraj
Saharia, Gunindra Nath Sarma, Pranita
Devi, and others.
—Manabendra Saharia

To my family,
Radhamani Devi, Shukram Puran, Sunil
Kumar, Suneeta Kumari, Anita Kumari,
Sneha Mishra
—Anil Kumar
Foreword by Dr. Martyn Clark

Data science has emerged as a powerful tool to understand changes in the Earth’s
climate. The use of large amounts of data to produce transformative insights requires
a new set of tools and skills. This textbook, “Python for Water and Environment”, fills
a critical gap between theory, computation, and applications in the field, by enabling
readers to implement theoretical concepts and develop an in-depth understanding of
water and environment problems.
The objective of the book is not only to serve as a programming guide but also to
expose the reader to the unique challenges prevalent in the water and environment
sciences. The book uses the versatile Python programming language, which provides
a straightforward implementation of models and rapid testing of algorithms. Starting
from the basics, the book gradually takes the readers from a basic to an advanced
level of programming that is relevant to hydrologic and environmental modeling. The
book deals with a wide range of topics such as exploratory data analysis, statistical
data modeling, and numerical modeling, all organized into well-defined chapters.
I congratulate the authors for writing this book which can serve as an important
resource for researchers and water professionals who wish to include Python in their
day-to-day work.

Dr. Martyn Clark


Former Editor-in-Chief (Water Resources Research)
Professor of Hydrology and Schulich Research Chair in Environmental
Prediction
Department of Civil Engineering
Schulich School of Engineering
University of Calgary
Calgary, Alberta, Canada

vii
Foreword by Dr. Rangan Banerjee

Water is a critical and scarce resource in India and the world. The challenges of
environmental management and sustainable development need new and innovative
approaches and new tools and techniques. I have great pleasure in writing the fore-
word for the textbook “Python for Water and Environment” written by my colleague
Manabendra Saharia.
Python has emerged as a language of choice for programmers and researchers.
This book demonstrates how Python provides an efficient platform for modeling,
analyzing and interpreting data, data analysis, and creating predictive models. This
book provides an easy to understand introduction to the use of Python for environ-
mental problems. The author delves into his rich experience in developing models
for water and environment problems and presents well-annotated code that can help
beginners, practitioners, and experts.
I hope this textbook helps propel the next generation of researchers and future
professionals to develop software and analytical tools and techniques for solving the
problems of water and the environment.
We at IIT Delhi hope that this textbook will serve as a bridge between theory and
practice and serve to propel new interdisciplinary research in this important area. The
author will be happy to get your feedback and suggestions on further enhancements
in this book and the domain.

Dr. Rangan Banerjee


Director
Indian Institute of Technology Delhi
Forbes Marshall Chair Professor in the Department of Energy Science
and Engineering (IIT Bombay)
Fellow of the Indian National Academy of Engineering

ix
Preface

While teaching graduate students of Civil Engineering at IIT Delhi, we felt the
need for a textbook that focuses more on the practical implementation side of Water
Resources Engineering using a modern programming language, which could supple-
ment the excellent theoretical textbooks that already exist. In an era where data
science and machine learning have revolutionized all fields, students continue to
struggle with breaking into specialized domains that require increasingly advanced
computational skills. Thus, this book focuses on code examples that readers can
directly benefit from. By providing concrete examples, the book equips readers with
the skills needed to address the complex challenges faced by water and environmental
professionals in today’s rapidly changing world.
“Python for Water and Environment” is conceived as a practical guide for profes-
sionals, researchers, and students who are working in sectors of water and environ-
ment. This preface outlines our journey through the realm of Python programming,
where we venture into the science of water resources and environmental management.
The essence of this book lies in the seamless integration of theoretical principles with
computational prowess, harnessing the power of Python to model, analyze, and solve
real-world problems.
Our aim is to illuminate the potential of Python as a robust and versatile tool for
dealing with complex challenges in the domain of water and environment. We aim to
break down the barriers to entry that have traditionally existed for non-programmers.
We believe that being an open-source language Python aligns well with the shared
global responsibility of water and environmental management. This book, therefore,
goes beyond being a mere guide to Python programming. Whether you are a seasoned

xi
xii Preface

professional or a passionate beginner in this domain, “Python for Water and Envi-
ronment” is designed to be your companion in this exciting journey of discovery and
problem-solving.

July 2023 Dr. Manabendra Saharia


Assistant Professor
Indian Institute of Technology Delhi
New Delhi, India
Dr. Anil Kumar
Principal Project Scientist
Indian Institute of Technology Delhi
New Delhi, India
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Acknowledgements

We extend our heartfelt gratitude to the Indian Institute of Technology Delhi commu-
nity, for providing us with an intellectually challenging academic environment.
The Department of Civil Engineering deserves a special mention for its continual
support and encouragement, enabling us to explore and expand the horizons of our
professional expertise.
Manabendra Saharia dedicates this book to his mother, Late Mrs. Nilakhi Saharia,
whose life of hardwork and kindness he aspires to live up to. He would also like
to acknowledge the support of his loving wife (Prof. Shrutidhara Sarma), father
(Ramesh Chandra Saharia), brother (Dhiraj Saharia), father-in-law (Gunindra Nath
Sarma), and mother-in-law (Pranita Devi). He acknowledges the extraordinary debt
he owes to all his well-wishers over the years: Prof. Rajib Bhattacharjya, Prof. Sharad
K. Jain, Prof. Parthajit Roy, Prof. Parthasarathi Choudhury, Prof. G. V. Ramana, Prof.
Sumedha Chakma, Prof. D. R. Kaushal, Prof. B. R. Chahar, Prof. Pierre Kirstetter,
Dr. Jonathan J. Gourley, Prof. Yang Hong, Dr. Sujay Kumar, Dr. Augusto Getirana,
Dr. Andy Wood, Dr. Andy Newman, Prof. Martyn Clark, and many more. He also
acknowledges the friendships that have sustained him over the years.
Anil Kumar would like to dedicate this book to his mother (Mrs. Radhamani
Devi), father (Shukram Puran), brother (Sunil Kumar), and sisters (Suneeta Dhungia
and Anita Kumari). He is grateful to his well-wishers: Prof. Kumar Hemant Singh,
Prof. Mohan Yellishetty, Prof. Trilok Nath Singh, and Prof. Stuart D. C. Walsh. He
acknowledges his friendship with Dr. Sneha Mishra and Dr. Rohit Kumar Shrivastava
for their constant support.
We specially acknowledge the encouragement and leadership of the Director of
the institute, Prof. Rangan Banerjee, and the Head of the Department of Civil Engi-
neering. Prof. Arvind K. Nema. Without their steadfast support, this book wouldn’t
see the light of day. Finally, we would also like to express our sincere thanks to the
many reviewers (Dr. Aatish Anshuman, Prof. B. R. Chahar, Ms. Reetumoni, etc.)
who took the time to meticulously scrutinize our work. Their invaluable insights

xiii
xiv Acknowledgements

and constructive feedback played an instrumental role in shaping this book. They
challenged us to refine our ideas, improve our methodology, and ensure standards of
quality and accuracy in writing.
Contents

Part I Practical Python for a Water and Environment


Professional
1 Data Analysis in the Water and Environment . . . . . . . . . . . . . . . . . . . . 3
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Types of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Python Environment and Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 Integrated Development Environment (IDE) . . . . . . . . . . . . . . . . . . 7
2.2 Why Virtual Environments? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 The Anaconda Package Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 The Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5 Installing External Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Python Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1 Getting Started with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Setting Up Python Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.3 My First Python Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4 Python Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4.1 Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4.3 List and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.4.4 Dictionaries and Dataframes . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4.5 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.4.6 Conditional Statements in Python . . . . . . . . . . . . . . . . . . . 21
3.4.7 File Operations in Python . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4 Exploratory Analysis of Hydrological Data . . . . . . . . . . . . . . . . . . . . . . 23
4.1 Examining a Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.1.1 Types of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.1.2 Basic Data Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.1.3 Common Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

xv
xvi Contents

4.2 Summarizing a Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27


4.2.1 Theoretical Probability Distributions
and Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2.2 Summarizing Numerical Data . . . . . . . . . . . . . . . . . . . . . . 30
4.2.3 Gaussianity in Numerical Data . . . . . . . . . . . . . . . . . . . . . . 34
4.2.4 Limitations of Summary Statistics . . . . . . . . . . . . . . . . . . . 36
4.2.5 Fitting a Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.2.6 Inliers and Outliers in Hydrologic Data . . . . . . . . . . . . . . 40
4.2.7 Missing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.2.8 Q-Q Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5 Graphical Hydrological Data Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.1 For a Single Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.1.1 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.1.2 Boxplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.1.3 Quantile Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.2 For Multivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.2.1 Scatter Matrix Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.2.2 Parallel Coordinate Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.3 Publication-Ready Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.4 Misleading Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

Part II Statistical Modeling in Hydrology


6 Curve Fitting and Regression Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 61
6.1 Simple Linear Regression of Flow . . . . . . . . . . . . . . . . . . . . . . . . . . 62
6.2 Multiple Linear Regression of Flow . . . . . . . . . . . . . . . . . . . . . . . . . 66
6.3 Nonlinear Regression of Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
7 Hydrological Time Series Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
7.1 Stationarity, Trend, and Periodicity . . . . . . . . . . . . . . . . . . . . . . . . . 76
7.2 Common Forecasting Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.2.1 Autoregression (AR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.2.2 Autoregressive Moving Average (ARMA) . . . . . . . . . . . . 88
7.2.3 Autoregressive Integrated Moving Average
(ARIMA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.2.4 Simple Exponential Smoothing (SES) . . . . . . . . . . . . . . . 101
8 Common Hypothesis Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
8.1 One-Way Analysis of Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
8.2 Two-Way Analysis of Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
8.3 t-Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
8.4 F-Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
8.5 The Kolmogorov-Smirnov Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.6 Mann-Whitney Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Contents xvii

9 Uncertainty Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135


9.1 Interval Estimates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
9.1.1 Non-parametric Interval Estimate . . . . . . . . . . . . . . . . . . . 136
9.2 Confidence Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
9.2.1 For Median . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
9.2.2 For Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
9.2.3 For Quantiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
9.3 Prediction Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
9.3.1 Non-parametric Prediction Interval . . . . . . . . . . . . . . . . . . 152
9.3.2 One-Sided Non-parametric Prediction Interval . . . . . . . . 154
9.3.3 Two-Sided Parametric Prediction Interval . . . . . . . . . . . . 157
9.3.4 Asymmetric Prediction Interval . . . . . . . . . . . . . . . . . . . . . 159
9.4 Quantile Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
9.5 Maximum Likelihood Estimation (MLE) . . . . . . . . . . . . . . . . . . . . 164
9.6 Monte Carlo Uncertainty Propagation . . . . . . . . . . . . . . . . . . . . . . . 167

Part III Surface and Subsurface Water


10 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
10.1 Numerical Modeling Using Finite Elements . . . . . . . . . . . . . . . . . . 177
10.2 Weak Form of the Steady State Darcy Flow Equation . . . . . . . . . . 178
10.3 Integration of Transient PDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
11 Surface Flow Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
11.1 Rectangular Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
11.1.1 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
11.2 Triangular Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
11.2.1 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
11.3 Circular Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
11.3.1 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
11.3.2 Discussion of Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
11.4 2D Shallow Water Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
11.4.1 Governing Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
11.4.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
11.4.3 Discussion of the Results . . . . . . . . . . . . . . . . . . . . . . . . . . 204
12 Subsurface Flow Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
12.1 Seepage Flow Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
12.1.1 Variational Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
12.1.2 GMSH Code to Generate the Computational
Domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
12.1.3 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
12.1.4 Discussion of the Results . . . . . . . . . . . . . . . . . . . . . . . . . . 218
12.2 Groundwater Flow Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
12.2.1 Variational Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
12.2.2 GMSH Code to Create the Computational Domain . . . . 219
xviii Contents

12.2.3 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221


12.2.4 Discussion of the Results . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Part IV Environmental Applications


13 Transport Phenomena . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
13.1 Contaminant Transport Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
13.2 1D Diffusion Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
13.3 1D Diffusion-Reaction Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
13.4 1D Advection-Diffusion Equation . . . . . . . . . . . . . . . . . . . . . . . . . . 236
13.5 A 2D Simulation Using Navier-Stokes Equations . . . . . . . . . . . . . 237
13.5.1 Construction of the Computational Domain . . . . . . . . . . . 239
13.5.2 GMSH Code to Generate the Computational
Domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
13.5.3 Variational Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
13.5.4 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
13.5.5 Post-processing of the Results . . . . . . . . . . . . . . . . . . . . . . 256
13.5.6 Discussion of the Results . . . . . . . . . . . . . . . . . . . . . . . . . . 256
14 Contaminant Transport Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
14.1 A 2D Diffusion Reaction Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
14.1.1 Variational Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
14.1.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
14.1.3 Post-processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
14.1.4 Discussion of the Results . . . . . . . . . . . . . . . . . . . . . . . . . . 266
14.2 A 2D Diffusion Advection Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
14.2.1 Variational Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
14.2.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
14.2.3 Post-processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
14.2.4 Discussion of the Results . . . . . . . . . . . . . . . . . . . . . . . . . . 273
14.3 Generalized 2D Advection, Diffusion, Reaction Model . . . . . . . . 275
14.3.1 Variational Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
14.3.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
14.3.3 Post-processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
14.3.4 Discussion of the Results . . . . . . . . . . . . . . . . . . . . . . . . . . 282
15 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
About the Authors

Dr. Anil Kumar is a senior project scientist in the Department of Civil Engineering
at the Indian Institute of Technology Delhi. He received his Ph.D. in Computational
Geosciences jointly from Monash University (Australia) and the Indian Institute of
Technology Bombay (India). He received a B.Tech. in Geophysical Technology from
the Indian Institute of Technology Roorkee. He has been working as a researcher
in the field of machine learning and numerical modeling and has helped develop
innovative solutions for the oil, gas, and mining industry.

Dr. Manabendra Saharia is an assistant professor in the Department of Civil Engi-


neering and an associate faculty member of the Yardi School of Artificial Intelli-
gence at the Indian Institute of Technology Delhi. Prior to joining IIT Delhi, he
held positions in the hydrology labs of the NASA Goddard Space Flight Center and
the National Center for Atmospheric Research (NCAR). He received his Ph.D. in
Water Resources Engineering from the University of Oklahoma. At IIT Delhi, his
HydroSense research lab focuses on utilizing physics and data-driven techniques
to monitor and mitigate natural hazards such as floods and landslides. He has been
recognized for his scientific contributions, having received Young Scientist awards
from both the National Academy of Sciences, India (NASI), and the International
Society for Energy, Environment and Sustainability (ISEES).

xix
Part I
Practical Python for a Water
and Environment Professional
Chapter 1
Data Analysis in the Water and
Environment

1.1 Introduction

Water security is increasingly in jeopardy throughout the world. Too much water
causing floods, too little water causing droughts, or poor water quality affecting health
can endanger life, economy, and ecosystems. In order to detect, monitor, and mitigate
these diverse problems in water and environment, we require actionable intelligence
based on data. Data analysis is an important part of understanding the complex
and multidimensional relationships between water systems and their surrounding
environments. We investigate these relationships through a combination of science,
engineering, and technology, which will help in discovering new information relevant
to the impact of water and the environment on human life. The efficacy of our
strategies in managing water resources, preserving aquatic life, combating pollution,
and preparing for climate change rests on our ability to monitor, model, and mitigate
various types of water hazards.
Water sustains life and is one of our planet’s most precious resources. It acts as
an integral link in the vast chain of ecosystems that allows life to prosper. Various
environmental factors such as geological formations and anthropogenic activities
impact the quality, availability, and distribution of water. These exchanges result in
a perpetually evolving ecosystem, making it necessary to constantly monitor this
water-environment interface.
Data has been dubbed as the new oil. And just like crude oil, raw data has to
be refined and analyzed to extract valuable and meaningful insights. Understanding
patterns, trends, and relationships in data can help us in making inferences about the
state and performance of water and environment systems, their interactions, and the
potential effects of changes in one system on the other. Data analysis helps us assess
the impacts of an oil spill on coastal waters, study seasonal variations in river flow,
or model future scenarios of sea-level rise due to global warming.
Data analysis in the water and environment sector involves a combination of
methodologies and technologies, ranging from traditional statistical techniques to
advanced machine learning algorithms. It relies on data acquired from a variety
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2024 3
A. Kumar and M. Saharia, Python for Water and Environment, Innovations in Sustainable
Technologies and Computing, https://doi.org/10.1007/978-981-99-9408-3_1
4 1 Data Analysis in the Water and Environment

of sources, such as satellite images, weather stations, sensor networks, and socio-
economic databases, to explore and interpret complex phenomena related to water
and the environment. By leveraging computational power and algorithms, vast
amounts of data can be processed and analyzed, resulting in actionable insights
for scientists, policymakers, and stakeholders. Data analysis helps us understand the
impact of human activities such as industrial pollution, deforestation, overfishing,
and uncontrolled urbanization on water and the environment. Such insights help in the
formulation of better policies and effective strategies for sustainable development,
water management, and environmental conservation. Data analysis helps in the opti-
mization of resources and predictive modeling for future scenarios. Data analysis
thus enables us to anticipate and mitigate risks, harness opportunities for sustain-
able growth, and create a better balance between human needs and environmental
preservation.
However, data analysis in the water and environment sector is not without its chal-
lenges. The quality and integrity of data, the complexity of environmental systems,
the inherent uncertainty in many types of environmental data, and the need for multi-
disciplinary approaches are among the many issues that analysts must grapple with.
These challenges necessitate a continuous refinement of methods and techniques,
emphasizing the field’s dynamic and evolving nature.
Data analysis in the water and environment sector integrates scientific exploration,
use of technology, and consideration of natural phenomena. The process involves
diving into challenging datasets, and uncovering details of environmental processes.
It is a journey of exploration, problem-solving, and creating impact, which is crucial
in an era marked by environmental shifts, climate change, depleting resources, and
rising human needs. In this chapter, we shall investigate the techniques, significance,
and function of data analysis in the water and environment domain.

1.2 Types of Data

Hydrologists study the distribution, movement, and storage of water in the environ-
ment, which requires large amounts of data across diverse scientific and engineering
disciplines. The data collected in hydrology is generally of three types: spatial, tem-
poral, and attribute data.
Spatial data consists of the geographic and physical properties of an area of inter-
est. In hydrology, examples of spatial data are soil and rock type, topography, the
physical features of water bodies, and vegetation. These attributes influence of move-
ment of water within the water cycle. For example, how water moves across the land
surface is influenced by topographical attributes such as elevation, slope, and aspect.
Geographic Information System (GIS) tools are widely used for managing and ana-
lyzing spatial data, which can be collected using different means such as satellite
remote sensing, LiDAR, or ground observations.
When numerical data is tracked over time, it is called temporal data. Extracting
valuable insights about the trends, patterns, and shifts in temporal attributes is known
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
1.2 Types of Data 5

as temporal data mining. In hydrology, examples of temporal data include tempera-


ture, snowpack thickness, groundwater levels, station precipitation, and evapotran-
spiration. Ground-based measurements, weather stations, and satellites can collect
these hydrological measurements. Time series data are crucial for identifying annual
and seasonal patterns, understanding the impact of climate change, and predicting
hydrological scenarios in the future.
Attribute data, on the other hand, is any qualitative description that provides addi-
tional context regarding the spatial and temporal data. For example, it may involve
data on population density and socioeconomic indicators in the study area, which can
provide insights into human impacts on the hydrological system. Typically, attribute
data is collected using field measurements, laboratory analysis, public databases, and
direct surveys.
These categories of data often overlap and lead to rich and multilayered datasets
which can be explored from different perspectives. For instance, water quality param-
eters (attribute data) may be measured over time (temporal data), and across a variety
of locations (spatial data) within a watershed. Such a dataset can be explored to under-
stand how water quality in an area changes spatially as well as temporally. Increas-
ingly, in hydrology, the problems have become complex enough that it requires har-
nessing big data and machine learning to develop deeper insights into large, complex,
and multidimensional datasets. By integrating data from diverse sources, hydrolo-
gists can unravel the complex dynamics of water in the environment, and aid in the
formulation of effective water management strategies and sustainable practices.
In the later chapters, we delve into hydrological data modeling using Python.
We explore time series analysis and numerical modeling of water variables such as
streamflow, water level, and atmospheric pressure. We will show how Python’s pow-
erful libraries can be leveraged for stochastic and deterministic modeling and trend
detection. We will also demonstrate the use of Python for solving partial differential
equations in the context of groundwater flow, and solute transport modeling. These
steps and techniques will equip the reader with the tools necessary for the analysis
of complex water systems.
Chapter 2
Python Environment and Basics

2.1 Integrated Development Environment (IDE)

Integrated Development Environment (IDE) and virtual environments are two crucial
components of Python programming. They are key tools in a Python developer’s
toolkit, aiding in streamlining the development process, enhancing productivity, and
ensuring code reliability and reproducibility.
An IDE is a software application that provides comprehensive facilities to pro-
grammers for software development. For Python developers, using an IDE like
PyCharm, Jupyter Notebook, or Visual Studio Code has numerous benefits. One
of the key advantages is that it combines several tools and features needed for cod-
ing into a single interface. This includes text editors for writing and editing code,
debuggers for finding and fixing errors, syntax highlighting for better readability,
and auto-completion features that save time by suggesting completions for names of
functions, keywords, and variables.
Furthermore, many IDEs provide built-in support for version control systems like
Git, allowing developers to track changes, revert to previous versions of code, and
efficiently collaborate with other developers. IDEs are built with functions for code
refactoring, testing, and profiling, which are essential for writing clean, error-free,
and efficient code.

2.2 Why Virtual Environments?

Virtual Environments are isolated environments where python packages can be


installed without interfering with other projects and installations. This allows the
developer to create isolated environments for different projects, which can be com-
municated to other developers who can recreate the exact development environment
for those projects. Without virtual environments, installing different versions of the

© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2024 7
A. Kumar and M. Saharia, Python for Water and Environment, Innovations in Sustainable
Technologies and Computing, https://doi.org/10.1007/978-981-99-9408-3_2
8 2 Python Environment and Basics

same package for different projects can lead to conflicts and inconsistencies, mak-
ing it difficult to share or deploy code. Virtual environments solve this problem by
creating isolated spaces for each project, where the necessary dependencies can be
installed without the risk of interference.
Virtual environments also contribute to the reproducibility of Python code. Main-
taining a record of the exact versions of the packages used in a project, allows other
programmers and systems to recreate the environment and execute the code under
identical conditions. This is particularly important in scientific computation and data
analytics, where package dependency is a basic requirement.
Both the Integrated Development Environments (IDEs) and virtual environments
play crucial roles in Python programming. While IDEs increase efficiency and code
quality by integrating utilities and features into a unified interface, virtual environ-
ments ensure the reliability and replicability of Python code by ensuring project
dependencies and isolation. Together, they simplify the coding process by providing
a versatile framework for Python programming and making it more enjoyable.

2.3 The Anaconda Package Manager

The anaconda package manager (https://www.anaconda.com/) provides an open-


source distribution of the Python and R programming languages for scientific com-
puting, designed for simplified package management and deployment. It is loaded
with a collection of over 1,500 packages for computation, data analysis, visualiza-
tion, and machine learning, and incorporates fundamental libraries such as NumPy,
Pandas, and Matplotlib. In addition, it is equipped with a package manager, Conda,
that assists in managing environments, making the installation and maintenance of
different versions of software packages hassle-free.

macOS Installation: To install Anaconda on your operating system, start by down-


loading the Anaconda installer for your operating system from the official Ana-
conda website. Python 3.8 version or later is the preferred choice unless a specific
earlier version is required. Upon downloading, double-click the downloaded file to
launch the installer. This program guides the user through the installation process,
possibly suggesting the installation of the Anaconda3 version in the home user
directory. After completing the steps, the installation is verified by opening a new
terminal window and typing the command: “conda list”. If Anaconda is installed
correctly, a list of installed packages appears.
Anaconda’s popularity is due to the convenience it offers. Operating systems, like
MacOS and Windows Subsystem for Linux, come preinstalled with Python, which
might not be suitable for scientific computing due to its potential incompatibil-
ity with certain modules. Thus, having a separate, controlled environment like
Anaconda ensures compatibility between Python and other packages.
Linux Installation: The installation process for Anaconda on a Linux system
begins with downloading the installer script from the Anaconda website. After
2.4 The Jupyter Notebook 9

downloading, the user needs to verify the data integrity of the installer with cryp-
tographic hash verification through the SHA-256 checksum. A terminal is opened
and the bash command is run on the downloaded file, like this: “bash Anaconda3-
2020.02-Linux-x86_64.sh”. “2020.02” would be the version downloaded. Read
the license agreement by scrolling with the help of the “Enter” key and accept it by
typing “yes”. Next, choose an installation location or accept the default location.
Once the installation process is complete, the terminal is closed and a new one is
opened to ensure the changes take effect.
Anaconda’s ability to handle dependencies and environments on Linux systems
provides a significant advantage as Linux distributions comprise many interde-
pendent packages. Anaconda offers a reliable and easy-to-use interface to manage
the complexity of these packages.
Windows Installation: On Windows, the installation process begins by down-
loading the Anaconda installer .exe file from the official Anaconda website. Once
the installer is downloaded, it is run and the instructions are followed. During
installation, the user will be asked if they want to add Anaconda to the PATH
environment variable—it is recommended not to check this option, as it can inter-
fere with other Python installations or software. Rather, one can use Anaconda
software by launching Anaconda Navigator or the Anaconda Command Prompt
from the Start Menu.
Windows lacks preinstalled Python, so installing Anaconda is the simplest way to
get started with Python, especially for beginners. It can seamlessly handle com-
plex Windows environments, making it effortless to manage and distribute a large
number of third-party libraries which frequently pose difficulties during installa-
tion and maintenance in the Windows ecosystem.

2.4 The Jupyter Notebook

The Jupyter Notebook is an open-source web-based application that has emerged


as a preferred tool among data scientists, academicians, and researchers worldwide.
Its simple but powerful user interface allows users to produce and share documents
enriched with live code, mathematical equations, data visualizations, and explanatory
text.
One of the key features of a Jupyter Notebook is its flexibility in accommodating
more than 40 programming languages, including Python, R, Julia, and Scala mak-
ing it highly adaptable to diverse computational requirements and projects. Users
can toggle between these languages inside the notebook, making it an exceptional
medium for polyglot data analysis.
Jupyter Notebooks can embed multimedia elements like images, videos, LaTeX
scripts, and JavaScript widgets, making it easy to write narrative documents inter-
spersed with code and results. This feature is also suitable for crafting presentations,
10 2 Python Environment and Basics

dashboards, and interactive tutorials. It also allows connections to multiple data


sources, allowing users to pull data from databases, APIs, or cloud storage.
For those already proficient in Python or other compatible programming lan-
guages, gaining expertise in Jupyter Notebook is simple. The user interface is quite
beginner-friendly, helping users swiftly learn how to execute cells, tweak code, and
visualize data. A wide array of tutorials and resources is available online to assist
newcomers.
A notable feature of a Jupyter Notebook is that it allows executing code in “cells".
This enables users to efficiently segment their work into manageable units, beneficial
for code debugging and revision. Each cell operates independently, allowing for
iterative code development, testing, and validation of algorithms and functions.
The coding style in Jupyter Notebook fosters an investigative and iterative coding
methodology. As it encourages the concept of “literate programming”, users can
include with their code, notes ,and comments, leading to improved legibility and
sustainability. This style is apt for structured and lucid data analysis workflows.
Jupyter Notebook’s compatibility with Git and GitHub also extends its accessi-
bility. Users can set version control for their notebooks, share them among peers,
and even publish them right away as web pages via GitHub.
To summarize, the Jupyter Notebook’s versatility, user-centric design, and support
for collaborative and investigative programming make it an invaluable tool for a
diverse user base, from researchers involved in complex data analyses to educators
instructing in programming or data science disciplines.

2.5 Installing External Packages

Installing external packages in an Anaconda virtual environment is a process that


involves several steps, ensuring that the necessary software tools are correctly added
to the user’s workspace. The Conda package and environment manager included in
Anaconda are used to handle these installations.
1. Creation of a new Conda environment: Before installing external packages, it
is good practice to create a new Conda environment. This isolated environment
provides a separate space where packages can be installed without interfering
with the system’s base Python installation or other Conda environments. This
can be executed using the command:
Random documents with unrelated
content Scribd suggests to you:
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.

You might also like