-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntk_extractPolarHour.py.bak
312 lines (275 loc) · 11.3 KB
/
ntk_extractPolarHour.py.bak
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import sys
version = "R 0.5.0"
################################################################################################
#
# outout usage message
#
################################################################################################
#
def usage():
print "\n\nUSAGE:\n\n"
print " configuration file name net sta loc channel directory start date-time end date-time x-axis type mode = verbose -- run in verbose mode"
print " | | | | | | | | | 0 -- run with minimum message output"
print " | | | | | | | | |"
print " python ntk_extractPolarHour.py param=extractPolarHour net=NM sta=SLM loc=DASH chandir=BHZ_BHE_BHN start=2009-11-01T11:00:00 end=2009-11-06T00:00:00 type=frequency mode=0"
print " "
print " chandir is the ichannel directory under which the polarization output files are stored (under polarDb)"
print " "
print " hourly polarization values are extracted for each variable as defined by 'variables' in the computePolarization parameter file and the param file above ('variables' must have the same value in both parameter files)"
print " available variables are: powerUD, powerEW, powerNS, powerLambda, betaSquare, thetaH, thetaV, phiVH, phiHH"
print "\n\nOutput file name provided during the run"
print "\n\n\n\n"
################################################################################################
#
# get run arguments
#
################################################################################################
#
def getArgs(argList):
args = {}
for i in xrange(1,len(argList)):
if '=' not in argList[i]:
print '[ERROR] bad parameter definition',argList[i]
usage()
sys.exit()
key,value = argList[i].split('=')
args[key] = value
return args
################################################################################################
#
# get a run argument for the given key
#
################################################################################################
#
def getParam(args,key,msgLib,value):
if key in args.keys():
return args[key]
elif value is not None:
return value
else:
print '[ERROR] missing parameter ',key
usage()
sys.exit()
################################################################################################
#
# Main
#
################################################################################################
#
# NAME: # ntk_extractPolarHour.py - a Python script to extract polarization parameters for the given channelis and bounding
# parameters. The output is similar to PQLX's exPSDhour script
#
# Copyright (C) 2015 Product Team, IRIS Data Management Center
#
# This is a free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This script is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License (GNU-LGPL) for more details. The
# GNU-LGPL and further information can be found here:
# http://www.gnu.org/
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# INPUT:
#
# hourly polarization files
#
# HISTORY:
#
# 2015-09-15 R0.5.0: Beta release
# 2013-06-07 IRIS DMC Product Team (MB): created
#
################################################################################################
#
#
# PACKAGES:
#
import glob,re,os,math
import numpy as np
from obspy.core import UTCDateTime
from datetime import date, timedelta as td
#
# import the Noise Toolkit libraries
#
libraryPath = os.path.join(os.path.dirname(__file__), '..', 'lib')
sys.path.append(libraryPath)
import msgLib as msgLib
import fileLib as fileLib
import staLib as staLib
args = getArgs(sys.argv)
#
# see if user has provided the run arguments
#
if len(args) < 8:
msgLib.error("missing argument(s)",1)
usage()
sys.exit()
#
# import the user-provided parameter file
#
# os.path.dirname(__file__) gives the current directory
#
paramFile = getParam(args,'param',msgLib,None)
import importlib
paramPath = os.path.join(os.path.dirname(__file__), '..', 'param')
#
# check to see if param file exists
#
if os.path.isfile(os.path.join(paramPath,paramFile+".py")):
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'param'))
param = importlib.import_module(paramFile)
else:
msgLib.error("bad parameter file name ["+paramFile+"]",2)
usage()
sys.exit()
VERBOSE = msgLib.param(param,'VERBOSE').VERBOSE
VERBOSE = 0
arg = getParam(args,'mode',msgLib,'verbose')
print "MODE",arg
if arg == 'verbose':
msgLib.message("VERBOSE RUN")
VERBOSE = 1
if VERBOSE >0 :
print "\n\n[INFO] script: %s" % script
print "[INFO] ARG#",len(sys.argv)
print "[INFO] ARGS",sys.argv
if len(sys.argv) < 9:
msgLib.error("missing argument(s)",1)
usage()
sys.exit()
script = sys.argv[0]
#
# RUN ARGUMENTS:
#
print "\n"
msgLib.message("START")
network = getParam(args,'net',msgLib,None)
station = getParam(args,'sta',msgLib,None)
location = staLib.getLocation(getParam(args,'loc',msgLib,None))
channelDir = getParam(args,'chandir',msgLib,None)
#
# Polarization files are all HOURLY files with 50% overlap computed as part of the polarization product
# date parameter of the hourly Polarizations to start, it starts at hour 00:00:00
# - HOURLY files YYYY-MM-DD
#
variableList = param.variables
dataDirectory = param.dataDirectory
startDateTime = getParam(args,'start',msgLib,None)
#
# processing parameters
#
xType = getParam(args,'type',msgLib,'period') # what the x-axis should represent
tStart = UTCDateTime(startDateTime)
startYear = tStart.strftime("%Y")
startMonth = tStart.strftime("%m")
startDay = tStart.strftime("%d")
startDOY = tStart.strftime("%j")
endDateTime = getParam(args,'end',msgLib,None)
tEnd = UTCDateTime(endDateTime)
endYear = tEnd.strftime("%Y")
endMonth = tEnd.strftime("%m")
endDay = tEnd.strftime("%d")
endDOY = tEnd.strftime("%j")
duration = tEnd - tStart
dEnd = date(int(endYear),int(endMonth),int(endDay))
dStart = date(int(startYear),int(startMonth),int(startDay))
delta = dEnd - dStart
dataDaysList = []
for i in xrange(delta.days +1):
thisDay = dStart + td(days=i)
dataDaysList.append(thisDay.strftime("%Y/%j"))
if duration <=0 or len(dataDaysList) <= 0:
msgLib.error('bad start/end times [' + startDateTime + ', ' + endDateTime + ']', 2)
usage()
sys.exit()
#####################################
# find and start reading the polarization files
#####################################
#
# build the file tag for the polarization files to read, example:
# NM_SLM_--_BH_2009-01-06
#
polarDbDirTag, polarDbFileTag = fileLib.getDir(param.dataDirectory,param.polarDbDirectory,network,station,location,channelDir)
print "\n[INFO] polarization DIR TAG: " + polarDbDirTag + "\n"
if VERBOSE >0 :
print "\n[INFO] polarization FILE TAG: " + polarDbFileTag
#####################################
# open the output file for each parameter
#####################################
#
thisPolarDirTag, polarFileTag = fileLib.getDir(param.dataDirectory,param.polarDirectory,network,station,location,channelDir)
for pn in xrange(len(variableList)):
variable = variableList[pn]
print "[INFO] variable:",variable+"\n"
polarDirTag = os.path.join(thisPolarDirTag,variable)
fileLib.makePath(polarDirTag)
tagList = [polarFileTag,startDateTime.split('.')[0],endDateTime.split('.')[0],xType]
outputFileName = fileLib.getFileName(param.namingConvention,polarDirTag,tagList)
try:
with open(outputFileName, 'w') as outputFile:
#####################################
# loop through the windows
#####################################
#
for n in xrange(len(dataDaysList)):
thisFile = os.path.join(polarDbDirTag, dataDaysList[n], polarDbFileTag+'*'+xType+'.txt')
print "[INFO] Day:", dataDaysList[n] ,thisFile
thisFileList = sorted(glob.glob(thisFile))
if len(thisFileList)<=0:
msgLib.warning('Main','No files found!')
if VERBOSE:
print "skip"
continue
elif len(thisFileList)>1:
if VERBOSE:
print len(thisFileList), "files found!"
#####################################
# found the file, open it and read it
#####################################
#
for thisPolarFile in thisFileList:
if VERBOSE >0 :
print "[INFO] polarization FILE: " ,thisPolarFile
#print "polarDbFileTag",polarDbFileTag
thisFileTimeLabel = thisPolarFile.split(polarDbFileTag+'.')[1].split('.')[0]
#print "thisFileTimeLabel",thisFileTimeLabel
#thisFileTimeLabel = thisPolarFile.split(channelDir+'_')[1].split('.')[0]
thisFileTime = UTCDateTime(thisFileTimeLabel)
if thisFileTime >= tStart and thisFileTime < tEnd:
with open(thisPolarFile) as file:
if VERBOSE >0 :
print "OK, working on ..." + thisPolarFile
#
# skip the header lines
#
for i in (1,2):
next(file)
#
# go through individual periods/frequencies
#
for line in file:
#
# each row, split column values
#
values = re.split('\s+',line.strip('.').strip())
X = values[0]
#print "values[pn+1]",values[pn+1]
V = str(round(float(values[pn+1]),param.decimalPlaces[variable]))
#print "V",V
#
# save the period/frequency and the value of interest
#
thisOutDate,thisOutTime = thisFileTimeLabel.split('T')
outputFile.write("%s%s%s%s%s%s%s\n" % (thisOutDate,param.separator,thisOutTime.split('.')[0],param.separator,X,param.separator,V))
file.close()
except:
msgLib. error("failed to open "+outFileName+"\nis 'namingConvention' "+msgLib.param(param,'namingConvention').namingConvention+" set correctly?" +"\n",0)
sys.exit()
print "[INFO] OUTPUT FILE: " + outputFileName + "\n"
outputFile.close()