logo资料库

a_hands_on_introduction_to_using_python_in_the_atmos.pdf

第1页 / 共209页
第2页 / 共209页
第3页 / 共209页
第4页 / 共209页
第5页 / 共209页
第6页 / 共209页
第7页 / 共209页
第8页 / 共209页
资料共209页,剩余部分请下载后查看
Preface
Notices
How to Use This Book
What Is and Why Python?
Python: The good and the bad
Examples of AOS uses for Python
Using the Python Interpreter and Interactive Development Environment
Getting and installing Python
The easiest way: EPD
The mostly easy way, but for Ubuntu 12.04
The not as easy way, but it's still free
Getting and installing the course files
The Python interpreter
The command-line environment
The IDLE environment
Exercises using Python programming environments
Basic Data and Control Structures
Overview of basic variables and operators
Strings
Booleans
NoneType
Lists and tuples
Exercises with lists and tuples
Dictionaries
Exercises with dictionaries
Functions
Logical constructs
Looping
Looping a definite number of times
Looping an indefinite number of times
Exercises on functions, logical constructs, and looping
Modules
A brief introduction to object syntax
Exercise that includes using a module
Exception handling
Summary
Array Operations
What is an array and the NumPy package
Creating arrays
Array indexing
Exercises in creating and indexing arrays
Array inquiry
Array manipulation
General array operations
General array operations: Method 1 (loops)
General array operations: Method 2 (array syntax)
Exercise on general array operations
Testing inside an array
Testing inside an array: Method 1 (loops)
Testing inside an array: Method 2 (array syntax)
Exercise on testing inside an array
Additional array functions
Summary
File Input and Output
File objects
Text input/output
Text input/output: Reading a file
Text input/output: Writing a file
Text input/output: Processing file contents
Exercise to read a multi-column text file
NetCDF input/output
NetCDF input/output: Reading a file
NetCDF input/output: Writing a file
Exercise to read and write a netCDF file
Summary
A ``Real'' AOS Project: Putting Together a Basic Data Analysis Routine
The assignment
Solution One: Fortran-like structure
Solution Two: Store results in arrays
Solution Three: Store results in dictionaries
Solution Four: Store results and functions in dictionaries
Exercises on using dictionaries and extending your basic data analysis routine
Summary
An Introduction to OOP Using Python: Part I—Basic Principles and Syntax
What is object-oriented programming
Procedural vs. object-oriented programming
The nuts and bolts of objects
Example of how objects work: Strings
Exercise on how objects work: Strings
Example of how objects work: Arrays
Exercise on how objects work: Arrays
Defining your own class
Exercise on defining your own class
Making classes work together to make complex programming easier
Case study 1: The bibliography example
Structuring the Bibliography class
What sort_entries_alpha illustrates about OOP
Exercise in extending the Bibliography class
What the write_bibliog_alpha method illustrates about OOP
Case study 2: Creating a class for geosciences work—Surface domain management
Summary
An Introduction to OOP Using Python: Part II—Application to Atmospheric Sciences Problems
Managing metadata and missing values
What are masked arrays and masked variables?
Constructing and deconstructing masked arrays
Exercise using masked arrays
Managing related but unknown data: Seeing if attributes are defined
Exercise to add to the Atmosphere class
Dynamically changing subroutine execution order (optional)
Summary
Visualization: Basic Line and Contour Plots
What is matplotlib?
Basic line plots
Controlling line and marker formatting
Annotation and adjusting the font size of labels
Plotting multiple figures and curves
Adjusting the plot size
Saving figures to a file
Exercise on basic line plots
Basic contour plots
Exercise on basic contour plots
Superimposing a map
Exercise on superimposing a map
Summary
What Next?
What Python topics would be good to cover next?
Some packages of interest to AOS users
Additional references
A parting invitation
Glossary
Acronyms
Bibliography
Index
Johnny Wei-Bing Lin A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences http://www.johnny-lin.com/pyintro 2012
c 2012 Johnny Wei-Bing Lin. Some rights reserved. Printed version: ISBN 978-1-300-07616-2. PDF ver- sions: No ISBNs are assigned. This work is licensed under the Creative Commons Attribution-Noncom- mercial-Share Alike 3.0 United States License (CC BY-NC-SA). To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ us or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Who would not want to pay money for this book?: if you do not need a black-and-white paper copy of the book, a color PDF copy with functional hyperlinks, have limited funds, or are interested in such a small portion of the book that it makes no sense to buy the whole thing. The book’s web site (http://www.johnny-lin.com/pyintro) has available, for free, PDFs of every chapter as separate files. Who would want to pay money for this book?: if you want a black- and-white paper copy of the book, a color PDF copy with functional hy- perlinks, or you want to help support the author financially. You can buy a black-and-white paper copy of the book at http://www.johnny-lin.com/ pyintro/buypaper.shtml and a hyperlink-enabled color PDF copy of the book at http://www.johnny-lin.com/pyintro/buypdf.shtml. A special appeal to instructors: Instruction at for-profit institutions, as a commercial use, is not covered under the terms of the CC BY-NC-SA, and so instructors at those institutions should not make copies of the book for students beyond copying permitted under Fair Use. Instruction at not-for- profit institutions is not a commercial use, so instructors may legally make copies of this book for the students in their classes, under the terms of the CC BY-NC-SA, so long as no profit is made through the copy and sale (or Fair Use is not exceeded). However, most instruction at not-for-profit institutions still involves payment of tuition: lots of people are getting paid for their contributions. Please consider also paying the author of this book something for his contribution. Regardless of whether or not you paid money for your copy of the book, you are free to use any and all parts of the book under the terms of the CC BY-NC-SA.
Contents Preface Notices How to Use This Book 1 What Is and Why Python? 1.1 Python: The good and the bad . . . . . . . . . . . . . . . . 1.2 Examples of AOS uses for Python . . . . . . . . . . . . . . 2 Using the Python Interpreter and Interactive Development Envi- ronment 2.1 Getting and installing Python . . . . . . . . . . . . . . . . . 2.1.1 The easiest way: EPD . . . . . . . . . . . . . . . . 2.1.2 The mostly easy way, but for Ubuntu 12.04 . . . . . 2.1.3 The not as easy way, but it’s still free . . . . . . . . 2.2 Getting and installing the course files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 The Python interpreter . . . . . . . . . . . . . . . 2.4 The command-line environment 2.5 The IDLE environment . . . . . . . . . . . . . . . . . . . . 2.6 Exercises using Python programming environments . . . . . 3 Basic Data and Control Structures . . . . . . . 3.1 Overview of basic variables and operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Strings . . . . . . . . . . . . . . . . . . . . . . . 3.3 Booleans . . . 3.4 NoneType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5 Lists and tuples . . . . . . . . . . . . . . . . . . 3.6 Exercises with lists and tuples 3.7 Dictionaries . . . . . . . . . . . . . . . . . . . . . 3.8 Exercises with dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v xi xiii 1 1 2 7 7 8 8 9 9 9 11 12 13 17 17 19 20 21 22 25 26 28 i
. . . . . . . . . . . 3.9 Functions . . 3.10 Logical constructs . . . 3.11 Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.11.1 Looping a definite number of times . . . . . . . . . 3.11.2 Looping an indefinite number of times . . . . . . . . 3.12 Exercises on functions, logical constructs, and looping . . . 3.13 Modules . . . . . . . . . . . . . . . . . . . . . . 3.14 A brief introduction to object syntax . . . . . . . . . . . . . 3.15 Exercise that includes using a module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.16 Exception handling . 3.17 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Array Operations . . . . . . 4.1 What is an array and the NumPy package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Creating arrays . . 4.3 Array indexing . . . . . . . . . . . . . . . . . . . . . . 4.4 Exercises in creating and indexing arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.5 Array inquiry . . . . . . . . . . . . . . . . . . . . . . 4.6 Array manipulation . . 4.7 General array operations . . . . . . . . . . . . . . . . . . . 4.7.1 General array operations: Method 1 (loops) . . . . . 4.7.2 General array operations: Method 2 (array syntax) . 4.7.3 Exercise on general array operations . . . . . . . . . 4.8 Testing inside an array . . . . . . . . . . . . . . . . . . . . 4.8.1 Testing inside an array: Method 1 (loops) . . . . . . 4.8.2 Testing inside an array: Method 2 (array syntax) . . 4.8.3 Exercise on testing inside an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.9 Additional array functions 4.10 Summary . . . . . . . 5 File Input and Output . 5.1 File objects . 5.2 Text input/output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.1 Text input/output: Reading a file . . . . . . . . . . . 5.2.2 Text input/output: Writing a file . . . . . . . . . . . 5.2.3 Text input/output: Processing file contents . . . . . . 5.2.4 Exercise to read a multi-column text file . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.1 NetCDF input/output: Reading a file . . . . . . . . . 5.3.2 NetCDF input/output: Writing a file . . . . . . . . . 5.3.3 Exercise to read and write a netCDF file . . . . . . . . 5.3 NetCDF input/output 29 33 34 34 36 37 39 41 42 43 45 47 47 47 50 52 53 54 58 58 59 62 63 63 64 70 71 72 73 74 74 75 75 76 79 79 80 84 86 ii
5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 6 A “Real” AOS Project: Putting Together a Basic Data Analysis . . . . Routine 6.1 The assignment . . . . . . . . . . . . . . . . . . . . 6.2 Solution One: Fortran-like structure . . . . . . . . . . . . . 6.3 Solution Two: Store results in arrays . . . . . . . . . . . . . 6.4 Solution Three: Store results in dictionaries . . . . . . . . . 6.5 Solution Four: Store results and functions in dictionaries . . 6.6 Exercises on using dictionaries and extending your basic da- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ta analysis routine . . 6.7 Summary . . . . . . . 89 89 90 92 93 94 95 96 7 An Introduction to OOP Using Python: Part I—Basic Principles 97 and Syntax 97 7.1 What is object-oriented programming . . . . . . . . . . . . 98 7.1.1 Procedural vs. object-oriented programming . . . . . 98 7.1.2 The nuts and bolts of objects . . . . . . . . . . . . . 7.2 Example of how objects work: Strings . . . . . . . . . . . . 99 7.3 Exercise on how objects work: Strings . . . . . . . . . . . . 100 7.4 Example of how objects work: Arrays . . . . . . . . . . . . 101 7.5 Exercise on how objects work: Arrays . . . . . . . . . . . . 103 7.6 Defining your own class . . . . . . . . . . . . . . . . . . . 104 7.7 Exercise on defining your own class . . . . . . . . . . . . . 107 7.8 Making classes work together to make complex program- . . . . . . ming easier . . . . . . . . . . . . . . . . . . . . 110 7.9 Case study 1: The bibliography example . . . . . . . . . . . 110 7.9.1 Structuring the Bibliography class . . . . . . . . . 110 7.9.2 What sort entries alpha illustrates about OOP . 112 7.9.3 Exercise in extending the Bibliography class . . . 113 7.9.4 What the write bibliog alpha method illustrates about OOP . . . . . . . . . . . . . . . . . . . . . . 115 7.10 Case study 2: Creating a class for geosciences work—Sur- face domain management . . . . . . . . . . . . . . . . . . . 115 . . . . . . . . . . . . . . . . . . . . 119 7.11 Summary . . . . . . . 8 An Introduction to OOP Using Python: Part II—Application to Atmospheric Sciences Problems 8.1 Managing metadata and missing values 121 . . . . . . . . . . . 121 8.1.1 What are masked arrays and masked variables? . . . 122 8.1.2 Constructing and deconstructing masked arrays . . . 126 iii
8.1.3 Exercise using masked arrays . . . . . . . . . . . . 129 8.2 Managing related but unknown data: Seeing if attributes are . . . . . . . . defined . . . . . . . . . . . . . . . . . . . . . 130 8.3 Exercise to add to the Atmosphere class . . . . . . . . . . . 135 8.4 Dynamically changing subroutine execution order (optional) 137 8.5 Summary . . . . . . . . . . . . . . . . . . . . . 141 . . . . . . . . . . 9 Visualization: Basic Line and Contour Plots 9.1 What is matplotlib? . 9.2 Basic line plots . 143 . . . . . . . . . . . . . . . . . . . . 143 . . . . . . . . . . . . . . . . . . . . 144 9.2.1 Controlling line and marker formatting . . . . . . . 145 9.2.2 Annotation and adjusting the font size of labels . . . 146 9.2.3 Plotting multiple figures and curves . . . . . . . . . 150 9.2.4 Adjusting the plot size . . . . . . . . . . . . . . . . 152 9.2.5 Saving figures to a file . . . . . . . . . . . . . . . . 152 9.3 Exercise on basic line plots . . . . . . . . . . . . . . . . . . 153 9.4 Basic contour plots . . . . . . . . . . . . . . . . . . . . . . 154 9.5 Exercise on basic contour plots . . . . . . . . . . . . . . . . 156 9.6 Superimposing a map . . . . . . . . . . . . . . . . . . . . . 158 9.7 Exercise on superimposing a map . . . . . . . . . . . . . . 161 . . . . . . . . . . . . . . . . . . . . 162 9.8 Summary . . . . . . . 10 What Next? 165 10.1 What Python topics would be good to cover next? . . . . . . 165 10.2 Some packages of interest to AOS users . . . . . . . . . . . 167 . . . . . . . . . . . . . . . . . . . . 168 10.3 Additional references . 10.4 A parting invitation . . . . . . . . . . . . . . . . . . . . . . 169 Glossary Acronyms Bibliography Index 171 175 177 179 iv
分享到:
收藏