-
Notifications
You must be signed in to change notification settings - Fork 0
/
sac2mt5
executable file
·340 lines (298 loc) · 10.3 KB
/
sac2mt5
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
rm temp
cat<<!
sac2mt5 v1.3
SAC to DSN format converter.
Please see help for all options and usage. Help can be accessed by -h or --help
!
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
blue=`tput setaf 4`
bold=$(tput bold)
normal=$(tput sgr0)
underline=$(tput smul)
help(){
cat<<!
---
${red}sac2mt5${normal} is an abstraction layer for the SACtoDSN.pl perl script written by McCaffrey available at ${blue}${underline}http://www.geology.cwu.edu/facstaff/walter/mt5/SACtoDSN.pl${normal}
${bold}${yellow}Usage:${normal}
sac2mt5 ${green}[-d/--directory<data directory>] [-o/--output<output file>] [-t/--date <YY/MM/DD/HH/mm/ss = date and time>] [-l/--latlong <LAT/LONG>] [-n/--depth <event depth>] [-h/--help]${normal}
${bold}${yellow}Options:${normal}
-d/--directory Specify the directory containing all the SAC files.
This directory must also contain a subdirectory called RESP containing all the instrument response files
-o/--output Specify the name of the output DSN file. This will be created inside the data directory. Do not include the filename extension.
-t/--date Specify the date and time in YY/MM/DD/HH/mm/ss format
-l/--latlong Specify the latitude and longitude of the event in LAT/LON format
-n/--depth Specify the depth of the event in km
-h/--help Display this help
${bold}${red}NOTE:${normal}
1. Please make the required libraries and make sure all the paths are specified correctly in the perl script SACtoDSN.pl
2. Before using this script please make sure to install the three libraries provided along with this
3. Additional paths might need to be changed based on your TauP installation. Check where your ".tvel" files are within your installation. Put in the required directory name in TauP.pm inside Taup-0.01
!
}
d_flag=0;
o_flag=0;
t_flag=0;
l_flag=0;
n_flag=0;
while getopts ":d:o:t:l:n:h-:" opt; do
case $opt in
-)
case "${OPTARG}" in
directory)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
if [ "${val}" == '' ]; then
printf "Option --directory requires an argument.\nExiting...\n"
exit 1;
else
echo "Data directory is: '${val}'" >&2;
dir=${val};
fi
d_flag=1;
;;
output)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
if [ "${val}" == '' ]; then
printf "Option --output requires an argument.\nExiting...\n"
exit 1;
else
echo "Output file basename is: '${val}'" >&2;
output=${val};
fi
o_flag=1;
;;
date)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
if [ "${val}" == '' ]; then
printf "Option --date requires an argument.\nExiting...\n"
exit 1;
else
echo "Event date and time is: $(echo $val | awk -F/ '{print $1"/"$2"/"$3" "$4":"$5":"$6}')" >&2;
otime=${val}
time=`echo ${val} | awk -F/ '{print $1$2$3$4$5$6}'`;
fi
t_flag=1;
;;
latlong)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
if [ "${val}" == '' ]; then
printf "Option --latlong requires an argument.\nExiting...\n"
exit 1;
else
echo "Event lat/long is: '${val}'" >&2;
olat=`echo ${OPTARG} | awk -F/ '{print $1}'`
olon=`echo ${OPTARG} | awk -F/ '{print $2}'`
lat=`echo ${val} | awk -F/ '{print $1}' | awk -F. '{print $1 substr($2,0,2)}'`;
lon=`echo ${val} | awk -F/ '{print $2}' | awk -F. '{print $1 substr($2,0,2)}'`;
fi
l_flag=1;
;;
depth)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
if [ "${val}" == '' ]; then
printf "Option --depth requires an argument.\nExiting...\n"
exit 1;
else
echo "Event depth is: '${val}'" >&2;
evdp=${val};
odp=$evdp;
evdp=`echo $evdp | awk '{print ($1-int($1)>=0.5)?int($1)+1:int($1)}'`
fi
n_flag=1;
;;
help)
help >&2
o_flag=1;
d_flag=1;
t_flag=1;
l_flag=1;
n_flag=1;
exit 1
;;
esac
;;
d)
echo "Data directory is: $OPTARG" >&2
dir=$OPTARG;
d_flag=1;
;;
o)
echo "Output file basename is: $OPTARG" >&2
output=$OPTARG;
o_flag=1;
;;
t)
echo "Event date and time is: $(echo $OPTARG | awk -F/ '{print $1"/"$2"/"$3" "$4":"$5":"$6}')" >&2;
otime=${OPTARG};
time=`echo ${OPTARG} | awk -F/ '{print $1$2$3$4$5$6}'`;
t_flag=1;
;;
l)
echo "Event lat/long is: '${OPTARG}'" >&2;
olat=`echo ${OPTARG} | awk -F/ '{print $1}'`
olon=`echo ${OPTARG} | awk -F/ '{print $2}'`
lat=`echo ${OPTARG} | awk -F/ '{print $1}' | awk -F. '{print $1 substr($2,0,2)}'`;
lon=`echo ${OPTARG} | awk -F/ '{print $2}' | awk -F. '{print $1 substr($2,0,2)}'`;
l_flag=1;
;;
n)
echo "Event depth is: '${OPTARG}'" >&2;
evdp=${OPTARG};
odp=$evdp
evdp=`echo $evdp | awk '{print ($1-int($1)>=0.5)?int($1)+1:int($1)}'`
n_flag=1;
;;
\?)
printf "Invalid option: -$OPTARG.\nExiting...\n" >&2
exit 1
;;
:)
printf "Option -$OPTARG requires an argument.\nExiting...\n" >&2
exit 1
;;
h)
help >&2
o_flag=1;
d_flag=1;
exit 1
;;
esac
done
if [ $# -eq 0 ]
then
help;
fi
if [ $d_flag -eq 0 ];
then
read -p "Enter data directory: " dir;
fi
if [ $o_flag -eq 0 ];
then
read -p "Enter output DSN file name (without extension): " output;
fi
if [ $t_flag -eq 0 ];
then
read -p "Enter event date and time (YY/MM/DD/hh/mm/ss): " time;
otime=$time
time=`echo ${time} | awk -F/ '{print $1$2$3$4$5$6}'`
fi
if [ $l_flag -eq 0 ];
then
read -p "Enter event latitude: " lat;
read -p "Enter event longitude: " lon;
olat=$lat
olon=$lon
lat=`echo ${lat} | awk -F. '{print $1 substr($2,0,2)}'`;
lon=`echo ${lon} | awk -F. '{print $1 substr($2,0,2)}'`;
fi
if [ $n_flag -eq 0 ];
then
read -p "Enter event depth: " evdp;
odp=$evdp
evdp=`echo $evdp | awk '{print ($1-int($1)>=0.5)?int($1)+1:int($1)}'`
fi
read -p "Input channel type (BH,LH,etc.): " chan
echo "Selecting usable files..."
echo "Output files will be created in ${dir}/selected_s2m/"
echo
echo "Selecting files with a P(T1) and S(T2) marker and with E>=60+T2"
echo
echo $time $lat $lon $evdp 0 0 > temp
orig=`date --date="$(echo $otime | awk -F/ '{print $1}')-$(echo $otime | awk -F/ '{print $2}')-$(echo $otime | awk -F/ '{print $3}') $(echo $otime | awk -F/ '{print $4}'):$(echo $otime | awk -F/ '{print $5}'):$(echo $otime | awk -F/ '{print $6}')" --utc +%4Y" "%3j" "%2H" "%2M" "%2S" "%3N`
echo $orig
copy_sac2mt5 $olat $olon $odp "$orig"<<!
$dir
!
dir1=$dir
dir=${dir1}/selected_s2m
cp $dir/*.SAC .
cp $dir/RESP.* .
echo
n_total_p=`ls *${chan}Z*.SAC | awk -F. '{print $8}' | sort | uniq | wc -l`
echo $(tput setaf 4)${n_total_p}$(tput sgr0)
if [ $n_total_p -gt 100 ]; then
n_total=`echo $n_total_p | awk '{print int($1/100)}'`
for i in $(seq 1 $n_total); do
j=$((i*100));
left=(`ls *${chan}Z*.SAC | awk -F. '{print $8}' | sort | uniq | head -n$j | tail -n100`);
str=""
for k in ${left[*]}; do
str=$str" "`ls *.$k.*${chan}Z*.SAC | head -n1`;
done
echo $str
echo "$(tput bold)Input string:$(tput sgr0) "$str
SACtoDSN.pl --input $str --output ${output}P${i}.DSN
#cat temp ${output}P$((i)).DSN > temp2
#mv temp2 ${output}P$((i)).DSN
cp ${output}P${i}.DSN $dir
done
lll=`ls *${chan}Z*.SAC | awk -F. '{print $8}' | sort | uniq | wc -l | awk -vn=$n_total '{print $1-(n*100)}'`
left=(`ls *${chan}Z*.SAC | awk -F. '{print $8}' | sort | uniq | tail -n$lll`)
str=""
for j in ${left[*]}; do
str=$str" "`ls *.$j.*${chan}Z*.SAC | head -n1`;
done
echo "$(tput bold)Input string:$(tput sgr0) "$str
SACtoDSN.pl --input $str --output ${output}P0.DSN
#cat temp ${output}P0.DSN > temp2
#mv temp2 ${output}P0.DSN
cp ${output}P0.DSN $dir
else
left=(`ls *${chan}Z*.SAC | awk -F. '{print $8}' | sort | uniq`);
str=""
for j in ${left[*]}; do
str=$str" "`ls *.$j.*${chan}Z*.SAC | head -n1`;
done
echo "$(tput bold)Input string:$(tput sgr0) "$str
SACtoDSN.pl --input $str --output ${output}P0.DSN
#cat temp ${output}P0.DSN > temp2
#mv temp2 ${output}P0.DSN
cp ${output}P0.DSN $dir
fi
total_s=(`ls *${chan}E*.SAC *${chan}N*.SAC | awk -F. '{print $8}' | sort | uniq`)
str=""
for i in ${total_s[*]}; do
ax=`ls *.${i}.*${ch}E*.SAC | wc -l`
bx=`ls *.${i}.*${ch}N*.SAC | wc -l`
if [ $ax -eq 1 ] && [ $bx -eq 1 ]; then
a=`ls *.${i}.*${ch}E*.SAC | head -n1`
b=`ls *.${i}.*${ch}N*.SAC | head -n1`
str=$str" "$a" "$b
fi
done
n_total_s=`echo $str | awk '{print NF/2}'`
if [ $n_total_s -gt 50 ]; then
n_total=`echo $str | awk '{print 100*int(NF/100)}'`
z=1;
for j in $(seq 1 100 $n_total); do
k=$((j+99))
n_str=`echo $str | cut -d " " -f ${j}-${k}`
echo "$(tput bold)Input string:$(tput sgr0) "$str
SACtoDSN.pl --input $n_str --output ${output}S${z}.DSN
#cat temp ${output}S${z}.DSN > temp2
#mv temp2 ${output}S${z}.DSN
cp ${output}S${z}.DSN $dir
z=$((z+1))
done
nnn=$((n_total+1))
n_str=`echo $str | cut -d " " -f $nnn-10000`
echo "$(tput bold)Input string:$(tput sgr0) "$str
SACtoDSN.pl --input $n_str --output ${output}S0.DSN
#cat temp ${output}S0.DSN > temp2
#mv temp2 ${output}S0.DSN
cp ${output}S0.DSN $dir
else
echo "$(tput bold)Input string:$(tput sgr0) "$str
SACtoDSN.pl --input $str --output ${output}S0.DSN
#cat temp ${output}S0.DSN > temp2
#mv temp2 ${output}S0.DSN
cp ${output}S0.DSN $dir
fi
a=$(cd $dir/..; pwd)
b=$(pwd)
if [[ "$a" != "$b" ]]; then rm *.SAC; fi
rm RESP.*
rm ${output}*
rm temp