Skip to content

Instantly share code, notes, and snippets.

@jreyes1108
Forked from mattkoes/gfplot.py
Created February 2, 2012 18:29
Show Gist options
  • Save jreyes1108/1724995 to your computer and use it in GitHub Desktop.
Save jreyes1108/1724995 to your computer and use it in GitHub Desktop.
plot greens functions
# Read local GreensFunction file
# and plot the traces.
#
# Original: Matthew Koessler
# Updated 2/1/12: Juan Reyes jreyes1108 _ at _ gmail.com
#
# To run: % python gfplot.py ~/path_to_file/GreensFunctionFile.gn [GFF2.gn] [GFF3.gn]
from string import *
import os
import sys
import matplotlib.pyplot as plt
# Get command line arguments. They should be filenames
for arg in sys.argv:
if arg == 'gfplot.py': continue
print "\nNow [%s]" % arg
# Get the file
if not os.path.isfile(arg):
print "\n\tERROR: Cannot find file: [%s]\n" % arg
continue
else:
try:
gf = open(arg,"r")
except Exception, e:
print "\n\tERROR: Cannot open file: [%s]\n" % arg
print "\n\tException: %s => [%s]\n" % (Exception,e)
continue
# Read first line
hdr = split(gf.readline())
print "\tHDR: %s" % hdr
print "\tPoints for each block: [%s]" % hdr[0]
print "\tPeriod: [%s]" % hdr[1]
# Read data from file and split into array
data = split(gf.read())
# Remove last element
print "\tRemove last element: [%s]" % data.pop()
# Print total number of elements in file
print "\tGot [%s] elements in the file" % len(data)
# Count blocks of data
blocks = int(len(data)/int(hdr[0]))
print "\tSections in file: [%s]" % blocks
tiles = int(blocks/2)
# Init the plot
plt.hold(1)
plt.figure(1)
for b in range(blocks):
print "\tPlot section: [%s]" % b
plt.subplot(tiles,2,b+1)
start = b * int(hdr[0])
end = (b * int(hdr[0])) + 150
print "\tStart: [%s] End: [%s]" % (start,end)
p = plt.plot(data[start:end])
if b == 0:
plt.legend(['ZDD'])
elif b == 1:
plt.legend(['RDD'])
elif b == 2:
plt.legend(['TDD'])
elif b == 3:
plt.legend(['ZDS'])
elif b == 4:
plt.legend(['RDS'])
elif b == 5:
plt.legend(['TDS'])
elif b == 6:
plt.legend(['ZSS'])
elif b == 7:
plt.legend(['RSS'])
elif b == 8:
plt.legend(['TSS'])
elif b == 9:
plt.legend(['ZEX'])
elif b == 10:
plt.legend(['REX'])
elif b == 11:
plt.legend(['TEX'])
else:
plt.legend(['-NONE-'])
try:
plt.show()
except KeyboardInterrupt:
print "Done.\n"
except Exception, e:
print "\nERROR: %s => [%s]\n" % (Exception,e)
exit()
@jreyes1108
Copy link
Author

Python script to read a GreensFunction file with multiple blocks of fundamental functions in ascii and plots them. Multiple files can be specified. The script will read the header of the file (first line) to get the number of points for each block and the period or the data. Original script by Matthew Koessler is https://gist.github.com/1719577

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment