[cig-commits] [commit] master: Use argparse in Python program. (6f8d78d)
cig_noreply at geodynamics.org
cig_noreply at geodynamics.org
Tue Jan 6 08:44:29 PST 2015
Repository : https://github.com/geodynamics/specfem1d
On branch : master
Link : https://github.com/geodynamics/specfem1d/compare/35bd59c099344f3bf7aa0e5570daa5ec1eca3c07...5ac1833074897dbc9cc286395ddae214ea0452e3
>---------------------------------------------------------------
commit 6f8d78d7a006eb2016dac9aa9527ca3d2da8ed77
Author: Elliott Sales de Andrade <esalesde at physics.utoronto.ca>
Date: Tue Jan 6 03:17:31 2015 -0500
Use argparse in Python program.
>---------------------------------------------------------------
6f8d78d7a006eb2016dac9aa9527ca3d2da8ed77
Fortran_version/plot_script_using_python.py | 50 +++++++++++------------------
1 file changed, 18 insertions(+), 32 deletions(-)
diff --git a/Fortran_version/plot_script_using_python.py b/Fortran_version/plot_script_using_python.py
index a84bfc6..26f1c44 100755
--- a/Fortran_version/plot_script_using_python.py
+++ b/Fortran_version/plot_script_using_python.py
@@ -6,56 +6,42 @@ Created on Wed Jan 22 15:52:24 2014
Script to plot the seismograms generated by SPECFEM2D.
The arguments must be correct paths to existing 2D seismogram files or
-an existing option (-hold,-grid)
+an existing option (--hold, --grid)
@author: Alexis Bottero (alexis.bottero at gmail.com)
"""
### --- MODULES AND PACKAGES --- ###
+import argparse
+
import numpy as np # NumPy (multidimensional arrays, linear algebra, ...)
import matplotlib as mpl # Matplotlib (2D/3D plotting library)
import matplotlib.pyplot as plt # Matplotlib's pyplot: MATLAB-like syntax
from pylab import * # Matplotlib's pylab interface
-from os.path import exists
-import sys
-
-# We could create a dictionnary that associate each option (-hold for ex) to
-# its meaning
-message="The arguments must be correct paths to existing 2D seismogram files \
-or an existing option (-hold,-grid)"
-ArgumentList=sys.argv
-FileList=[]
-correctCall,hold,grid=False,False,False
-for arg in ArgumentList[1:]: # The first argument is the name of the program
- if arg == '-hold':
- hold=True
- correctCall=True
- elif arg == '-grid':
- grid=True
- correctCall=True
- elif exists(arg):
- FileList.append(arg)
- correctCall=True
- else:
- print 'Argument : \"',arg,'\" is not a correct path to an existing seismogram \
-file or an existing option (-hold,-grid). It will be ignored...'
-if correctCall is False:
- print 'None of the arguments given is correct. ',message
- sys.exit(0)
-for seismo in FileList:
+parser = argparse.ArgumentParser(
+ description='Plot seismograms generated by SPECFEM2D')
+parser.add_argument('--hold', action='store_true',
+ help='Plot all seismograms on the same figure')
+parser.add_argument('--grid', action='store_true',
+ help='Show a grid on the plot')
+parser.add_argument('files', nargs='+', type=argparse.FileType('r'),
+ help='Files to be plotted')
+args = parser.parse_args()
+
+for seismo in args.files:
data = np.loadtxt(seismo)
t,u = data[:,0],data[:,1]
- if hold is not True:
+ if not args.hold:
figure()
plt.plot(t,u)
- if grid:
+ if args.grid:
plt.grid(True)
- if hold:
+ if args.hold:
plt.hold(True)
else:
plt.hold(False)
- plt.title(seismo)
+ plt.title(seismo.name)
plt.xlim([t[0],t[-1]])
plt.show()
More information about the CIG-COMMITS
mailing list