-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathss_folder_ampHist.py
executable file
·181 lines (157 loc) · 7.05 KB
/
ss_folder_ampHist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/Python
# -*- coding: utf-8 -*-
__author__ = 'toopazo'
import subprocess
import os
# from os import listdir
# from os.path import isfile, join
import numpy as np
from obspy.core import read, Stream, Trace
import matplotlib.pyplot as plt
# import plotly.plotly as py
import ss_utilities
class ApplyToFolder():
def __init__(self):
pass
@staticmethod
def apply_to_folder(infolder, bins_width, bins_max):
print "[apply_to_folder] infolder %s " % infolder
print "**********************************************************"
# 1) uncompress ".xxx.gz" files
xxxgz_files = ss_utilities.ParserUtilities.get_xxx_files(folderpath=infolder, extension=".MSEED.gz")
for path_i in xxxgz_files:
gz_path_i = os.path.abspath(path_i)
print "[apply_to_folder] Uncompressing gz_path_i %s .." % gz_path_i
cmdline = "gzip -d %s" % gz_path_i
subprocess.call(cmdline, shell=True) # resp = str(subprocess.call(cmdline, shell=True))
# arg = "[convert_slist2mseed] cmdline %s, resp %s" % (cmdline, resp)
# #>print arg
print "**********************************************************"
# 2) get ".xxx" files and apply "apply_to_file"
xxx_files = ss_utilities.ParserUtilities.get_xxx_files(folderpath=infolder, extension=".MSEED")
for i in range(0, len(xxx_files)):
try:
infile_i = os.path.abspath(xxx_files[i])
print "[apply_to_folder] Processing infile_i %s .." % infile_i
ApplyToFolder.apply_to_file(infile=infile_i, bins_width=bins_width, bins_max=bins_max)
print "Next file >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" # separate infile_i
except IndexError:
pass
print "**********************************************************"
# 3) Done
#>print "Done"
@staticmethod
def apply_to_file(infile, bins_width, bins_max):
amp_hist(infile, bins_width, bins_max)
def amp_hist(infile, bins_width, bins_max):
# 1) Make sure user inputs are correct
infile = os.path.normcase(infile)
infile = os.path.normpath(infile)
infile = os.path.realpath(infile)
#>print(infile)
bins_width = int(bins_width)
bins_max = int(bins_max)
# 2) Construct Stream object
st = read(infile)
# st = st.sort()
# tr_i) Histogram
bins_array = []
bins_i = -bins_max + bins_width/2
while bins_i <= bins_max:
bins_array.append(bins_i)
bins_i += bins_width
# #>print(bins_array)
for tr_i in range(0, len(st)):
arg = 'st[tr_i].stats.channel %s' % st[tr_i].stats.channel
# #>print(arg)
print(st[tr_i].stats)
samples = st[tr_i].data
channel = st[tr_i].stats['channel']
samples = np.int32(samples)
# #>print(st[tr_i])
# #>print(st[tr_i].data)
# Histogram example from https://plot.ly/matplotlib/histograms/
freq_of_bins_normed, bins, patches = plt.hist(samples, bins=bins_array, normed=True)
# arg = "len(freq_of_bins_normed) %s freq_of_bins_normed %s"\
# % (len(freq_of_bins_normed), freq_of_bins_normed)
# #>print(arg)
# arg = "len(bins) %s bins %s" % (len(bins), bins)
# #>print(arg)
# arg = "len(patches) %s patches %s" % (len(patches), patches)
# #>print(arg)
# get statistical info about samples
samples_mean = np.mean(samples)
arg = 'samples_mean %s' % samples_mean
print(arg)
samples_std = np.std(samples)
arg = 'samples_std %s' % samples_std
print(arg)
samples_max = np.max(samples)
arg = 'samples_max %s' % samples_max
print(arg)
samples_min = np.min(samples)
arg = 'samples_min %s' % samples_min
print(arg)
pseudo_signal = 0
pseudo_noise = 0
for sample_i in range(0, len(samples)):
if abs(samples[sample_i]) <= 3.0*samples_std + samples_mean:
pseudo_noise += abs(samples[sample_i])
else:
pseudo_signal += abs(samples[sample_i])
try:
pseudo_snr = 1.0*pseudo_signal/pseudo_noise
except ZeroDivisionError:
pseudo_snr = "ZeroDivisionError"
# arg = 'pseudo_snr %s' % pseudo_snr
# print(arg)
# arg = "Histogram \n Mean %s stdDev %s \n pseudo_snr %s" % (samples_mean, samples_std, pseudo_snr)
arg = "Histogram \n Mean %s stdDev %s" % (samples_mean, samples_std)
plt.title(arg)
# get statistical info about bins
# try:
# plt.yscale('log', basey=10, nonposy='clip')
# plt.ylabel("Normailzed Frequency [log10 scale]")
# except ValueError:
# plt.yscale('linear')
# plt.ylabel("Normailzed Frequency [linear scale]")
plt.xlabel("Amplitude intervals ") # [linear scale]")
plt.ylabel("Normailzed Frequency ") # [linear scale]")
# x_axis = np.float64(bins_array)
# y_axis = np.float64(freq_of_bins_normed)
# y_axis_argmax = y_axis.argmax()
# y_axis_max = y_axis.max()
#>print('y_axis_argmax = %s' % y_axis_argmax)
#>print('y_axis[%s] = %s' % (y_axis_argmax, y_axis[y_axis_argmax]))
#>print('y_axis_max = %s' % y_axis_max)
# arg_freqinfo = 'Maximum: \n%s [Interval]\n %s [Frequency]' % (x_axis[y_axis_argmax], y_axis_max)
# plt.annotate(arg_freqinfo, xy=(x_axis[y_axis_argmax]+0.0, y_axis_max+0.0),
# xytext=(0.5, 0.5),
# arrowprops=dict(facecolor='black', shrink=0.05),
# textcoords='figure fraction'
# )
# plt.annotate(arg_freqinfo, xy=(x_axis[y_axis_argmax]+10, y_axis_max+0.0),
# xytext=(x_axis[y_axis_argmax]+100, y_axis_max),
# #arrowprops=dict(facecolor='black', shrink=0.05),
# )
# outfile_channel_ext = "_" + st[tr_i].stats.channel + "_ampHisto.png"
# repl = "_%s_ampHisto_%sbinswidth_%sbinmax.png" % (channel, bins_width, bins_max)
repl = "_ampHisto_%sbinswidth_%sbinmax.png" % (bins_width, bins_max)
outfile = infile.replace(".MSEED", repl)
plt.savefig(outfile)
# plt.show()
plt.clf()
print("")
# exit(0)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Apply function xxx to corresponding files .yyy in the given folder')
parser.add_argument('directory', help='directory to use', action='store')
parser.add_argument('--bins_width', action='store', help='bins_width', default='1') # , required=True)
parser.add_argument('--bins_max', action='store', help='bins_max', default='1000') # , required=True)
args = parser.parse_args()
uinfolder = args.directory
uinfolder = os.path.normcase(uinfolder)
uinfolder = os.path.normpath(uinfolder)
uinfolder = os.path.realpath(uinfolder)
ApplyToFolder.apply_to_folder(uinfolder, bins_width=args.bins_width, bins_max=args.bins_max)