Python for Climate Data Analysis

Python for Climate Data Analysis

Capacity Building Workshop Python for Climate Data Analysis Christoph Menz November 26, 2019 Module Overview • Lecture: Introduction to Python • Hands-On: Python for Beginners • Exercise: Python • Lecture: Introduction to Python Libraries • Hands-On: Access and analysis of netCDF Data with Python • Lecture: Python Libraries for Data Visualization • Hands-On: Visualization of Scientific Data with matplotlib • Hands-On: Visualization of Geospatial Data with cartopy • Exercise: Analysis and Visualization of netCDF Data with python , Christoph Menz RD II: Climate Resilience 2 Introduction to Python , Christoph Menz RD II: Climate Resilience 3 Introduction to Python • High-level general-purpose programming language • Emerges in the late 80s and early 90s (first release 1991) • Based on teaching/prototyping language ABC • Freely available under Python Software Foundation License • Major design philosophy: readability and performance • Important features: • Dynamic types (type automatically declared and checked at runtime) • Automatized memory management • Objects, Loops, Functions • Easily extendible by various libraries (numpy, netCDF4, scikit-learn, ...) , Christoph Menz RD II: Climate Resilience 4 Scope of this Course • Basic programming and scripting with python • Read, preparation, statistics and visualization of netCDF based data • Focus on python version 3.x using Anaconda platform • Online Tutorials: Anaconda Tutorials https://docs.python.org/3/tutorial https://www.tutorialspoint.com/python3 https://scipy.org & http://scikit-learn.org https://matplotlib.org/ http://scitools.org.uk/cartopy , Christoph Menz RD II: Climate Resilience 5 Anaconda - Data Science Platform , Christoph Menz RD II: Climate Resilience 6 Jupyter - Interactive Computing Notebook , Christoph Menz RD II: Climate Resilience 7 Spyder - Interactive Computing Notebook , Christoph Menz RD II: Climate Resilience 8 Variables in python • Variable types are automatically defined at runtime variable_name = value • Uses dynamic and static type casting: • 5*5.0 is a float and "Hello world" is a string • str(5) is a string and int("9") is a integer • python got 13 different built-in types: • bool, int, float, str, list, tuple, dict, bytearray, bytes, complex, ellipsis, frozenset, set • Possibility to create your own type for object-oriented programming ( class statement) , Christoph Menz RD II: Climate Resilience 9 Variable Types • Basic variable types Boolean - bool Int, Float and Complex - int, float, complex In [1]: x = True In [1]: x = 5 In [2]: y = False In [2]: y = 5.0 In [3]: Y = True In [3]: z = 5.0+2.0j Characters and Strings - str In [1]: char = "a" In [2]: string = 'python' In [3]: SeNtEnCe = "This is a sentence." In [4]: x = """This is a sentence ...: across multiple lines""" In [5]: string[0:2] In [5]: py , Christoph Menz RD II: Climate Resilience 10 Variable Types - Lists • Lists are sequences of variables of arbitrary type (also lists of lists of lists ... possible) • Lists are mutable • Single elements of lists can be accessed by indexing (from 0 to length - 1) List In [1]: List = [2.0, 5.0, True, 7.0, "text"] In [2]: ListList = [[2.0, 5.0], [True, 7.0, "more text"]] In [3]: ListList[0] = List[4] In [4]: ListList Out [4]: ["text", [True, 7.0, "more text"]] , Christoph Menz RD II: Climate Resilience 11 Variable Types - Tuples • Tuples are similar to lists • But tuples are immutable Tuple In [1]: Tuple = (2.0, 5.0, True, 7.0, "text") In [2]: TupleTuple = ((2.0, 5.0), (True, 7.0, "more text")) In [3]: TupleTuple[0] = Tuple[4] ---------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-52-862d5dc2e8bb> in <module> () ----> 1 TupleTuple [0] = Tuple [4] TypeError : 'tuple' object does not support item assignment , Christoph Menz RD II: Climate Resilience 12 Variable Types - Dictionaries • Dictionaries are unordered collections of arbitrary variables • Dictionaries are mutable • Elements of dictionaries get accessed by keys instead of indices • Keys in dictionaries are unique Dictionary In [1]: my_dict = {"a":2.0, "b":5.0, "zee":[True, True]} In [2]: my_dict["b"] = 23.0 In [3]: my_dict Out [3]: {'a':2.0, 'b':23, 'zee':[True,True]} In [4]: {"a":2.0, "b":5.0, "zee":[True, True], "a":7} Out [4]: {'a':7, 'b':5.0, 'zee':[True,True]} , Christoph Menz RD II: Climate Resilience 13 Operations Addition & Subtraction • python supports the usual In [1]: 3 + 5.0 mathematical operations on Out [1]: 8.0 float, int and complex In [2]: 3 - 5 • Out [2]: -2 Dynamic casting depends on operator and variable type Multiplication & Division In [1]: 4 * 4 Power & Root Out [1]: 16 In [2]: 8 / 2 In [1]: 4**2 Out [2]: 4.0 Out [1]: 16 In [3]: 7 // 3 In [2]: 4**2.5 Out [3]: 2 Out [2]: 32.0 In [4]: 7 % 3 In [3]: 16**0.5 Out [4]: 1 Out [3]: 4.0 , Christoph Menz RD II: Climate Resilience 14 • in -Operator permits an easy search functionality in-Operator In [7]: 7 in [1, 2, 3, 4, 5] Out [7]: False In [8]: "b" in {"a":4, "b":6, "c":8} Out [8]: True Boolean Operations Comparisons • Python uses usual comparison In [1]: 5 > 3 operations Out [1]: True In [2]: 5 >= 3 Out [2]: True In [3]: 5 < 3 Out [3]: False In [4]: 5 <= 3 Out [4]: False In [5]: 5 == 3 Out [5]: False In [6]: 5 != 3 Out [6]: True , Christoph Menz RD II: Climate Resilience 15 Boolean Operations Comparisons • Python uses usual comparison In [1]: 5 > 3 operations Out [1]: True • In [2]: 5 >= 3 in -Operator permits an easy search Out [2]: True functionality In [3]: 5 < 3 Out [3]: False In [4]: 5 <= 3 in-Operator Out [4]: False In [7]: 7 in [1, 2, 3, 4, 5] In [5]: 5 == 3 Out [7]: False Out [5]: False In [8]: "b" in {"a":4, "b":6, "c":8} In [6]: 5 != 3 Out [8]: True Out [6]: True , Christoph Menz RD II: Climate Resilience 15 Boolean Operators Logical NOT • python supports the basic logical Operator Results operators to combine booleans not True False not False True Logical AND Logical OR x Operator y Results x Operator y Results True and True True True or True True True and False False True or False True False and True False False or True True False and False False False or False False , Christoph Menz RD II: Climate Resilience 16 Methods of Objects/Variables Object methods • Python variables are not In [1]: x = [] just atomic variables In [2]: x.append(3) • Python variables are In [3]: x.append(5) objects by themself In [4]: print(x) [3,5] • Each variable already In [5]: y = {"a":1,"b":2,"c":3} comes with associated In [6]: print(y.keys()) methods dict_keys(['a','b','c']) In [7]: "This is a sentence".split("") • Syntax: In [7]: ['This','is','a','sentence'] variable.method In [8]: "".join(["This","is","a","list"]) In [8]: 'This is a list' You can use the dir() function to get an overview of all methods available for a given variable. , Christoph Menz RD II: Climate Resilience 17 • Python uses indentation (leading whitespaces) instead of bracketsINDENTATION to seperate code blocks INDENTATION Condition and Indentation • Condition start with if and ends with : (equivalent to ”then” in other languages) • Syntax: if expression : statement if In [1]: x = 7 In [2]: if x >= 5 and x <= 10: ...: print("x is above 5") ...: print("x is below 10") ...: x is above 5 x is below 10 , Christoph Menz RD II: Climate Resilience 18 INDENTATION INDENTATION Condition and Indentation • Condition start with if and ends with : (equivalent to ”then” in other languages) • Syntax: if expression : statement • Python uses indentation (leading whitespaces) instead of brackets to seperate code blocks if In [1]: x = 7 In [2]: if x >= 5 and x <= 10: ...: print("x is above 5") ...: print("x is below 10") ...: x is above 5 x is below 10 , Christoph Menz RD II: Climate Resilience 18 Condition and Indentation • Condition start with if and ends with : (equivalent to ”then” in other languages) • Syntax: if expression : statement • Python uses indentation (leading whitespaces) instead of bracketsINDENTATION to seperate code blocks if In [INDENTATION1]: x = 7 In [2]: if x >= 5 and x <= 10: ...: print("x is above 5") ...: print("x is below 10") ...: x is above 5 x is below 10 , Christoph Menz RD II: Climate Resilience 18 Condition and Indentation • if conditions support arbitrary number of elif conditions and one possible else condition if ... elif ... else In [1]: x = 20 In [2]: if x >= 5 and x <= 10: ...: print("x is between 5 and 10") ...: elif x < 5: ...: print("x is below 5") ...: elif x in [15,20,25]: ...: print("x is 15, 20 or 25") ...: else: ...: print("x is out of bound") ...: x is 15, 20 or 25 , Christoph Menz RD II: Climate Resilience 19 Loops • For loops iterate only a specific number of times • Syntax: for variable in iterable : statement • Iterable are objects you can iterate over (list, tuple, dict, iterators, etc.) for-Loop In [1]: for x in [2,4,6,8]: ...: print(x*2) ...: 4 8 12 16 , Christoph Menz RD II: Climate Resilience 20 Built-In Functions • Python ships with several built-in functions for daily usage • Syntax: function(arguments) • Function arguments are comma seperated values print() Function len() Function In [1]: print("123") In [1]: len("123456") 123 Out [1]: 6 In [2]: print(123) In [2]: len([3, 5, 8]) 123 Out [2]: 3 In [3]: print(1,2,3,"123") In [3]: len({"a":13,"b":21}) 1 2 3 123 Out [3]: 2 , Christoph Menz RD II: Climate Resilience 21 Type Related Built-In Functions • Use the type() function to type() Function get the type of any variable In [1]: type("PyThOn") • Out [1]: str Type conversion can be In [2]: type(3) done using one of the Out [2]: int following functions: In [3]: type(3.0)

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    87 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us