Facilitate – Collaborate – Educate Emily Wolin Northwestern University.

Slides:



Advertisements
Similar presentations
Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party.
Advertisements

Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Introduction to a Programming Environment
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
By. What advantages has it? The Reasons for Choosing Python  Python is free  It is object-oriented  It is interpreted  It is operating-system independent.
Python plotting for lab folk Only the stuff you need to know to make publishable figures of your data. For all else: ask Sourish.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
Prof. R. Willingale Department of Physics and Astronomy 2nd Year C+R 2 nd Year C and R Workshop Part of module PA2930 – 2.5 credits Venue: Computer terminal.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter Introduction to Computers and Programming 1.
1 Programming Concepts Module Code : CMV6107 Class Contact Hours: 45 hours (Lecture 15 hours) (Laboratory/Tutorial 30 hours) Module Value: 1 Textbook:
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Array Response Functions with ArrayGUI
Introduction to M ATLAB EE 100 – EE Dept. - JUST.
CIS Computer Programming Logic
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
BIT 1003 – Presentation 7. Contents GENERATIONS OF LANGUAGES COMPILERS AND INTERPRETERS VIRTUAL MACHINES OBJECT-ORIENTED PROGRAMMING SCRIPTING LANGUAGES.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Scientific Computing Beyond Matlab Nov 19, 2012 Jason Su.
Programming With C.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Eng Ship Structures 1 Introduction to Matlab.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
CS4710 Why Progam?. Why learn to program? Utility of programming skills: understand tools modify tools create your own automate repetitive tasks automate.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
G.I.FEFFIT: A Graphical Interface to IFEFFIT IFEFFIT: Interactive XAFS Analysis and FEFF Fitting IFEFFIT is an interactive program and scriptable library.
XP Tutorial 8 Adding Interactivity with ActionScript.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
ECET – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.
CIS 601 Fall 2003 Introduction to MATLAB Longin Jan Latecki Based on the lectures of Rolf Lakaemper and David Young.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
Practical Kinetics Exercise 0: Getting Started Objectives: 1.Install Python and IPython Notebook 2.print “Hello World!”
SAC: An Overview SAC ( “Seismic Analysis Code” ) was originally developed by LLNL An interactive program for time-series data with emphasis placed on.
Matplotlib SANTHOSH Boggarapu.
Python & NetworkX Youn-Hee Han
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Hello world !!! ASCII representation of hello.c.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Linux Administration Working with the BASH Shell.
SAC: An Overview SAC ( “Seismic Analysis Code” ) was originally developed by LLNL SAC ( “Seismic Analysis Code” ) was originally developed by LLNL An interactive.
Operating Systems A Biswas, Dept. of Information Technology.
Python Scripting for Computational Science CPS 5401 Fall 2014 Shirley Moore, Instructor October 6,
Introduction to python programming
MET4750 Techniques for Earth System Modeling
Introduction to Python
Introduction to Python
Prepared by Kimberly Sayre and Jinbo Bi
Administrative things
Introduction to MATLAB
Python for Scientific Computing
Automating reports with Python
Introduction to Python
Simulation And Modeling
Chapter 1: Programming Basics, Python History and Program Components
Introduction to Python
Presentation transcript:

Facilitate – Collaborate – Educate Emily Wolin Northwestern University

Facilitate – Collaborate – Educate This presentation was developed by Emily Wolin of Northwestern University, and adapted for online use by Danielle Sumy of the IRIS Consortium. Any mistake is purely the fault of D. Sumy. Acknowledgements

Facilitate – Collaborate – Educate You will need: To install python: Python is a freely available software Download: Open up a terminal window Make sure you have a text editor, ‘vi’ is usually standard You also need to have a very basic understanding of SAC (Seismic Analysis Code): SAC info hereSAC info here Important Information Before You Begin

Facilitate – Collaborate – Educate According to the ‘What is Python? Executive Summary ( ‘Python is an interpreted, object-oriented, high- level programming language with dynamic semantics’ In the next slide, we’ll go through the meaning of these words, and what they mean for Python What is Python?

Facilitate – Collaborate – Educate Interpreted: executes a program directly, without the need to compile –Pro: platform independent –Con: can result in slower speed Object-oriented: modular software system, where each ‘object’ has data fields –For example: What is Python? Object: Contact Information Object: Contact Information Name Address Phone Name Address Phone

Facilitate – Collaborate – Educate High-level: uses natural language elements, and automates or even hides significant areas of the computing system –Pro: simple and easy to understand! Dynamic Semantics: the variable type (integer, character, float, etc.) is based on use, and does not need specified. The variable can be reused in the program and the type changes based on the current need. –Pro: eliminates data type mismatches –Con: may cause unexpected behavior when reusing a variable, like squaring text (e.g. Bob^2) What is Python?

Facilitate – Collaborate – Educate Why do I use Python? “How good are current tomographic models of North America?”

Facilitate – Collaborate – Educate Why do I use Python? “How good are current tomographic models of North America?” “We can use current tomographic models to predict S and Rayleigh wavefronts from earthquakes within North America. Do these synthetic seismograms agree with Observations at SPREE and the TA?”

Facilitate – Collaborate – Educate Why do I use Python? Things I need to know to compare two waveforms: Time series of ground motion (of course) Station, network, component, location Latitude, longitude, depth of station Start and end time of traces Sampling rate Event hypocenter and origin time Phase picks All of this information could be stored in an object called ‘mytrace’, for example

Facilitate – Collaborate – Educate Why do I use Python? Goal: Measure misfit between observed and synthetic seismograms

Facilitate – Collaborate – Educate Why do I use Python? Method: Calculate time-frequency misfit

Facilitate – Collaborate – Educate Why do I use Python? Method: Calculate time-frequency misfit How can I get my SAC files into Python and preserve necessary metadata?

Facilitate – Collaborate – Educate Why do I use Python? I could build an object in Python that contains: header information: mytrace.header.stnm mytrace.header.stla time series: mytrace.data Note: this is all pseudocode to serve as an example

Facilitate – Collaborate – Educate Why do I use Python? Then, I could define functions that modify these attributes: mytrace.trim(starttime=t1, endtime=t2) mytrace.filter(“highpass”, freq=0.02) mytrace.remove_response(output=“DISP”) Note: this is all pseudocode to serve as an example

Facilitate – Collaborate – Educate Why do I use Python? Good News! Seismology-friendly data structures already exist in the ObsPy package ( Even better news!! Powerful numerical and scientific libraries exist to process and visualize data

Facilitate – Collaborate – Educate After Python is installed, type in terminal window: ipython - -pylab (Note: make sure to have the double hyphen) Getting started Now type on the command line: print ‘Hello, World!’

Facilitate – Collaborate – Educate Does your screen look something like this?

Facilitate – Collaborate – Educate a = ‘1’string a = 1integer a = 1.1float a = 1+2jcomplex Getting Started: Variables

Facilitate – Collaborate – Educate a = [42,17,6]a list of integers a[1]answer? 15 Note: a list starts from zero a. a? Getting Started: Lists In IPython, use and ? to explore attributes/methods of an object

Facilitate – Collaborate – Educate s = ‘hello there’s is a string s[1]What’s the answer here? Getting Started: Strings Did you think that it would be ‘there’? In this case, the list took the second character ‘e’ Answer:

Facilitate – Collaborate – Educate if answer != 42:need a colon at the end print ‘that is not correct’ for i in range(5): print ‘Hello, world!’ Getting Started: Spaces Reminder: User a colon at the end of ‘if’ and ‘for’ statements Four spaces are needed when in if/for statement

Facilitate – Collaborate – Educate Getting Started: Modules Python doesn’t load modules unless you ask for them import os

Facilitate – Collaborate – Educate Getting Started: Modules Once loaded, can type ‘help(os)’ which produces…

Facilitate – Collaborate – Educate Getting Started: Modules Once loaded, can type ‘os.environ’ which produces… A dictionary Dictionaries are stored with keys, and the values of those keys (

Facilitate – Collaborate – Educate Getting Started: Modules Within the os.environ dictionary, find value for ‘USER’:

Facilitate – Collaborate – Educate Exercise: Write Your Own Module Create a file called mymodule.py using your preferred text editor (like ‘vi’), and type in the following:

Facilitate – Collaborate – Educate Exercise: Use Your Own Module In the Python shell: import mymodule mymodule.sayhello() a=mymodule.addthese(3.1,2.7) Or if your fingers are getting tired: import mymodule as mm mm.sayhello() a=mm.addthese(3.1,2.7)

Facilitate – Collaborate – Educate Is this what you see (or something like this)?

Facilitate – Collaborate – Educate Make sure that you have the file ‘birdclasses.py’ available with this material before moving on. Before moving on…

Facilitate – Collaborate – Educate We can define a class (an object) that has its own attributes and methods. from birdclasses import Swallow bird=Swallow(species=‘African’, loadstatus=‘unladen’)bird.loadstatus bird.velocity Using Classes

Facilitate – Collaborate – Educate Try giving the bird a coconut: bird.giveCoconut() bird.loadstatus What has changed? What is the airspeed velocity of an unladen swallow? Now try…

Facilitate – Collaborate – Educate NumPy: “the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities” Useful modules

Facilitate – Collaborate – Educate SciPy: “a collection of numerical algorithms and domain-specific toolboxes, including signal processing, optimization, statistics and much more” Useful modules

Facilitate – Collaborate – Educate Matplotlib: “matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms” Useful modules PyLab indepedently or with Matplotlib helps give MATLAB-like syntax

Facilitate – Collaborate – Educate Matplotlib: “matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms” Useful modules PyLab indepedently or with Matplotlib helps give MATLAB-like syntax

Facilitate – Collaborate – Educate ObsPy: “an open-source project dedicated to provide a Python framework for processing seismological data. It provides parsers for common file formats and seismological signal processing routines which allow the manipulation of seismological time series” Useful modules

Facilitate – Collaborate – Educate Now you know a little about: Data Types Object-oriented: attributes and methods Flow control statements (if/while/for): –Mandatory colons and indentations Importing modules and writing simple functions Python Basics

Facilitate – Collaborate – Educate Now for something different…

Facilitate – Collaborate – Educate You will need the sample code: mylinefit.py With the code, you can: Generates a linearly-spaced array of x values Calculate a function y(x) at each point Add random noise to y(x) Fit a line through the noisy data Plot the noisy data, original function, and best-fit line Scripting with Python

Facilitate – Collaborate – Educate Run script from iPython: run mylinefit.py Run in Terminal: chmod +x mylinefit.py./mylinefit.py Make sure that the first line of mylinefit.py is: #!/usr/bin/env python Scripting with Python

Facilitate – Collaborate – Educate Fit a line in two ways: SciPy: stats.linregress module “By hand” with NumPy matrices and some inverse theory Scripting with Python

Facilitate – Collaborate – Educate from obspy.core import read st = read(‘TA.SPMN..LHZ.disp’) st.plot() OR: print st len(st) tr = st[0] print tr print tr.stats print tr.stats[‘station’] tr.data Read and Plot a Waveform

Facilitate – Collaborate – Educate from obspy.imaging.beachball import Beachball mt = [180, 80, 90] Beachball(mt, size=500) mt2 = [-0.463, 4.61, -4.15, , , -1.49] Beachball(mt2, size=500) Plot a focal mechanism

Facilitate – Collaborate – Educate Plot any two-column ASCII file from Terminal: chmod +x plotanything.py./plotanything.py vs.ak135./plotanything.py vs.* Note again that you need: #!/usr/bin/env python as the first line of your script Scripting with Python

Facilitate – Collaborate – Educate iPython notebooks Then run the notebook (will open in a browser): ipython notebook python_introduction.ipynb --pylab inline

Facilitate – Collaborate – Educate A Byte of Python: ObsPy tutorials: Python Scripting for Computational Science Python Scientific Lecture Notes Want to learn more?

Facilitate – Collaborate – Educate Thank you!