DATA ANALYSIS USING PYTHON-I Baburao Kamble and Ayse

DATA ANALYSIS USING PYTHON-I Baburao Kamble and Ayse

DATA ANALYSIS USING PYTHON-I Baburao Kamble and Ayse Kilic University of Nebraska-Lincoln University of Nebraska-Lincoln GIS in Water Resources Lecture, 2014 03/01/2020 Objectives To be able to understand and write Python scripts for Numerical, Text (Weather) & Geospatial (Environmental) data manipulation

03/01/2020 Agenda Part I: Basic Python Programming Python Syntax, Strings, Array, Conditional and Control flow, Functions File Input/Output and Text Data Processing Part II: Geospatial Data Analysis Installation of GDAL bindings on windows operating system Reading Raster (Landsat and DEM) data Using GDAL function from command line (interpolation, translation) Raster Processing.

NDVI calculation DN2Rad2Ref 03/01/2020 What is Python? 03/01/2020 Why Python? Natural Language ToolKit Ease of use; interpreter Processing: Symbolic Pythons built-in data-types for strings, lists, and more.

Java or C++ require the use of special classes for this. Processing: Statistical Python has strong numeric processing capabilities: matrix operations, etc. Suitable for probability and machine learning code. 03/01/2020 Writing a Program in Python A program is a collection of Python statements, functions, etc. A script is a list of commands which are run by a program written in a specific language such as Python Scripts can be edited by text editors or preferably by a

specialized editor that also allows the script to run We can use Python command line in Windows However, the use of the command line is limited (it does not support testing scripts) So, instead we use the Python script editor (an IDE) that has a better interface than the command line mode 03/01/2020 Installing Python Python is installed on Lab Computers. Python for Win/Mac/Unix/Linux is available from

www.python.org. Generally an easy install. On Mac, already part of OS X. For GDAL you need Python 2.6 or 2.7. Python 3.X is little-bit different GUI development environment: IDLE (Integrated DeveLopment Environment). 03/01/2020 IDLE

We will also continue using the IDLE environment for Python scripting IDLE is available with Python download Using the interactive Python interpreter is great for writing small code However, it is not meant to be saved! For larger, more complex multi-line code, it is better to write it as a script (rather than using the interactive Python interpreter, e.g., IDLE) or configure ECLIPSE for Python

03/01/2020 Elements of Programs Simpler expressions can be combined using operators. +, -, *, /, ** Spaces are irrelevant within an expression. The normal mathematical precedence applies. 2/4 2*3 03/01/2020 Variable

Variables in python are really object references. Can contain letters, numbers, and underscores Must begin with a letter = 3.1415926 Cannot be one of the reserved Python pi HW = "Hello, world" i = 2+7 print type(pi) print type(HW)

print type(i) Output: 03/01/2020 Operators 03/01/2020 Simple Sample of Python code

Sample.py 03/01/2020 Array & Lists How to Define array myArray=numpy.array([2,3,4,5,6,7]) myArray myArray[0] myArray[4] How to define list myList=[2,3,4,5,6,7]

myList myList[1] myList[4]=99 myList 03/01/2020 Modules (Library) & Packages in Python A module is a file containing Python definitions and statements. A module is a single file (or files) that are imported under one import and used. e.g.

import my_module import numpy A package is a collection of modules in directories that give a package hierarchy. from my_package.timing.danger.internets import my_module 03/01/2020 Using the numpy Library Besides (+, -, *, /, //, **, %, abs), we have

lots of other math functions available in a math library. A library is a module with some useful definitions/functions. To use a library, we need to make sure this line is in our program: import numpy Offers Matlab-ish capabilities within Python 03/01/2020 Functions

A function is like a subprogram, a small program inside of a program. The basic idea we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name. The part of the program that creates a function is called a function definition. When the function is used in a program, we say the definition is called or invoked. 03/01/2020 Functions

Define them in the file above the point they're used Body of the function should be indented consistently (4 spaces is typical in Python) Example: square.py def square(n): return n*n

print "The square of 10 is ", print square(10) Output: The square of 3 is 9 03/01/2020 Functions That Return Values Sometimes a function needs to return one or more values. 03/01/2020 Lets Consider Celsius to Fahrenheit temperature conversion program

def main(): celsius = input("Input Celsius temperature:") fahrenheit = 9/5 * celsius + 32 print "The temperature is", fahrenheit, "degrees Fahrenheit." main() ConvertTemp.py 03/01/2020 Python Program: Quadratic equation/Numerical Calculations Lets write a program to compute the roots of a quadratic

equation! b b2 4ac x 2a The only part of this we dont know how to do is find a square root but its in the mathematical library! Lets get mathematical library inside python. quadratic.py 03/01/2020

Lets Consider Celsius to Fahrenheit temperature conversion program Lets say we want to modify that program to print a warning when the weather is extreme. Any temperature over 90 degrees Fahrenheit and lower than 30 degrees Fahrenheit will cause a hot and cold weather warning, respectively. 03/01/2020 Three major control constructs of programming

Sequential: Simply do steps one after the other in order they are listed. Conditional: Decide which statement to do next based on some true/false test. Iterative: A set of statements is repeated over and over until some condition is met. 03/01/2020

Multi-Way Decisions if : elif : elif : else: 03/01/2020

Forming Simple Conditions Python < <= == >= Mathemati cs < = > != > Meaning Less than Less than or equal to Equal to Greater than or equal to Greater than Not equal to

ControlStruct_Demo1-3.py 03/01/2020 Text Data Processing file = open("test.txt") # Read everything into single string: content = file.read() # file.read(20) reads (at most) 20 bytes print (len(content)) print content # close file file.close()

#CSV file: #import csv library import csv #open csv file myfile = open('weather.csv', "r") #read content of file reader = csv.reader(myfile) # iterate over the rows for row in reader: print row, myfile.close() File_IO.py

03/01/2020 CSV file read/write/manipulation Estimating daily evapotranspiration (ET) using the California Irrigation Management Information System (CIMIS) Weather Data CIMIS_ETr.py Please refer ASCE manual for Evapotranspiration equations http://www.kimberly.uidaho.edu/water/asceewri/ascestzdetmain2005.pdf (Allen et al, 2005)

Recently Viewed Presentations

  • Middle School SAT Vocabulary List 2 List 2

    Middle School SAT Vocabulary List 2 List 2

    List 2 Words Abate Deliberate Egalitarian Flippant Jubilant Morose Novice Obscure Scrutinize Tenacious Study the definitions, synonyms, & antonyms, and know how to use them in a sentence. Abate (v) to become less strong or decrease The hurricane began to...
  • Ferdinand Magellan - Jefferson County Public Schools

    Ferdinand Magellan - Jefferson County Public Schools

    Ferdinand Magellan World Explorer Pgs. 142-144 Ferdinand Magellan Ferdinand Magellan (1480-1521) was a Portuguese explorer who led the first expedition that sailed around the Earth (1519-1522). Magellan also named the Pacific Ocean. The name means a calm, peaceful ocean. Early...
  • The Effect of Casino Proximity on Lottery Sales: Zip Code ...

    The Effect of Casino Proximity on Lottery Sales: Zip Code ...

    The Effect of Casino Proximity on Lottery Sales: Zip Code-Level Evidence from Maryland. 10th European Conference on Gambling Studies and Policy Issues. Helsinki, Finland. 11 September 2014. Douglas M. Walker and Will E. Cummings
  • Cumulative carbon emissions and economic policy: in search of ...

    Cumulative carbon emissions and economic policy: in search of ...

    Cumulative carbon emissions and economic policy: in search of general principles. Simon Dietz (LSE & Oxford) Frank Venmans (U. Mons & LSE) Strategy will be not to bore you with the details of the paper particularly, rather to try to...
  • Monday 5/8. Dr. Michael McIntosh will introduce psychiatric

    Monday 5/8. Dr. Michael McIntosh will introduce psychiatric

    Monday 5/8. Dr. Michael McIntosh will introduce psychiatric disease. Bi 1 "Drugs and the Brain" Lecture 18 Thursday, May 4, 2006 from mRNA to Protein
  • Term Logic - Shandong University

    Term Logic - Shandong University

    The classical logic of categorical syllogisms was originated by the ancient Greek philosopher Aristotle (384-322 BC). Modern deductive logic (symbolic logic) was developed by Frege (1848-1925 AD). There seems to be a tradeoff between the greater naturalness of the classical...
  • Induction - University of Waikato

    Induction - University of Waikato

    ACHIEVEMENTS IN ONE One Slide - One Minute Presentations Combined Chairs and Managers Leadership Forums 2011
  • Unit 3 CHAPTER 11: UNDERSTANDING RANDOMNESS (SIMULATIONS) A

    Unit 3 CHAPTER 11: UNDERSTANDING RANDOMNESS (SIMULATIONS) A

    Produces 5 random integers that might represent the pictures on the cereal box (0-9 had different meanings) randInt (0,56,3) Produces three random integers from 0 to 56. Also know how to use Appendix G in book when technology isn't available.