-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomasspos.sh
263 lines (251 loc) · 8.08 KB
/
domasspos.sh
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
#! /bin/sh
#usage: domasspos [-store xxxx yyyy | [-sn store#] yyyy mm [dd]]
# apollo server must be running on port 8080
# xxxx is the store file and yyyy is the station name
#by G. Helffrich/U. Bristol 31 Mar. 2008
lsfn=/usr/local/lib/leapseconds dir=/tmp
if [ "$1" = "-store" ]; then
pfx="http://localhost:8080/pages/central/storeSelector.page"
sfx="storeFile=$2" sta=$3
tmp=/tmp/tmp$$
curl -sS ${pfx}?${sfx} > $tmp 2>&1
if egrep -q 'Connection refused' $tmp ; then
echo "**Store selection failed -- apollo running?"
elif egrep -q 'id="message".*Improper file selection' $tmp ; then
echo "**Store selection failed."
elif egrep -q 'Connect failed' $tmp ; then
echo "**Store selection failed -- apollo running?"
else
echo "**Store selection succeeded."
sname=`basename $2`
sn=`echo $sname | sed -e 's/.*_\([0-9][0-9][0-9][0-9]\)_.*/\1/'`
if [ "$sn" != "$sname" ]; then
echo NMXSTORE=$sn NMXSTA=$sta > .nmxstore
fi
fi
/bin/rm -f $tmp
exit
fi
while [ $# -gt 0 ]; do
case "$1" in
-sn) sn=$2; shift 2;;
-dir) dir=$2; shift 2;;
*) [ -f .nmxstore ] && . .nmxstore
if [ -z "$NMXSTORE" ]; then
echo "**No store defined and no -sn given, quitting."
exit 1
fi
sn=$NMXSTORE sta=$NMXSTA
break
;;
esac
done
yy=$1; mm=`echo $2 | awk '{printf "%02d",$1}'`
y0=`echo $yy | awk '{printf "%02d",$1%100}'`
function ext() {
perl - $1 $2 $3 $4 $5 $6 << '__END__'
# =========================
package NMX::Scripts;
use Time::Local;
# =========================
sub timeInMillis
{
( $inputTime ) = @_;
$inputTime =~ s/(^\s*|\s*$)//g;
if ($inputTime =~ /([0-9][0-9][0-9][0-9])[\-\/\.]([0-9][0-9])[\-\/\.]([0-9][0-9])[ _T\.]([0-9][0-9])[:\-\.]([0-9][0-9])[:\-\.]([0-9][0-9])($|\.[0-9]+)/)
{
my $value = timegm($6, $5, $4, $3, $2 - 1, $1);
my $date = sprintf( "%04d-%02d-%02d_%02d:%02d:%02d", $1, $2, $3, $4, $5, $6 );
$fraction =~ s/0\.//;
$date = sprintf( "%s.%09d", $date, $fraction );
my $valueMs = sprintf ("%d%03d", $value, $fraction / 1000000);
return $valueMs
}
else
{
return 0;
}
}
# =========================
sub durationInMillis
{
( $duration ) = @_;
return $duration * 1000;
}
# =========================
sub getChannel
{
( $serialNumber, $channelnum ) = @_;
$channel = sprintf ("taurus_%04d/band/timeSeries%d/", $serialNumber, $channelnum);
return $channel;
}
# =========================
sub getURL
{
( $ip_address, $startTime, $duration, $serialNumber ) = @_;
my $urlString =
sprintf("http://%s/playback/download",$ip_address) . "?" .
sprintf("dataType=StateOfHealth&dataFormat=EnvironmentSOH(csv)&startMillis=%s&duration=%d_s", $startTime, $duration / 1000);
return $urlString;
}
# =========================
sub getTimeString
{
( $inputTime ) = @_;
$inputTime =~ s/(^\s*|\s*$)//g;
$seconds = $inputTime / 1000;
$fraction += $inputTime % 1000;
$fraction /= 1000;
$units = "ms";
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = gmtime($seconds);
$mon++;
$year += 1900;
my $date = sprintf( "%02d-%02d-%02d_%02d_%02d_%02d",
$year, $mon, $mday, $hour, $min, $sec );
return $date;
}
# =========================
sub getFiles
{
( $ip_address, $serialNumber, $startTime, $duration, $interval, $fn ) = @_;
$endTime = $startTime + $duration;
while($startTime < $endTime){
if ($startTime + $interval > $endTime) {
$interval = $endTime - $startTime;
}
my $url = getURL($ip_address, $startTime, $interval, $serialNumber);
my $ofn = $fn . ".csv";
print "URL: " . $url . "\n to $ofn\n";
print "cmd: " . " -o " . $ofn . " " . $url . "\n";
my @args = ("curl", "-sS", "-o", $ofn, "$url");
my $res = system(@args);
if ($res == 0) {
my $head, $size = (stat($ofn))[7];
if ($size < 4096) {
open(DF, $ofn) or $res = -1;
if ($res == 0) {
read(DF,$head,6) or $res = -1;
}
if ($res == 0 &&
$head =~ /<[hH][tT][mM][lL]>/) {
$res = -1;
unlink($ofn);
}
close(DF);
}
if ($res == 0) {
print sprintf("Got file: %s\n", $ofn);
}
}
if ($res != 0) {print "Error getting $ofn\n";}
$startTime = $startTime + $interval;
}
}
# =========================
( $ip_address, $serialNumber, $startTime, $duration, $interval, $fn ) = @ARGV;
my $startTimeInMs = timeInMillis($startTime);
my $durationInMs = durationInMillis($duration);
my $intervalInMs = durationInMillis($interval);
getFiles($ip_address, $serialNumber, $startTimeInMs, $durationInMs, $intervalInMs, $fn);
__END__
}
function duration() { ## Duration that accounts for leap second.
## 1: year 2: day 3: arg 4: file
if [ $2 -eq $3 -a -f $4 ]; then
awk "BEGIN{corr=0}
/^Leap[ ]*${1}[ ]*((Jun)|(Dec))[ ]*${3}[ ]/{"'
if ($6 == "+") corr=1; else corr=-1}
END{print 86400+corr}' $lsfn
else
echo 86400
fi
}
function split() { ## Split data out of .csv file
## 1: dir 2: file prefix 3: station name
f=${1}/${2} suffix='MPZ MPN MPE'
if [ -s $f.csv ]; then
awk -F, 'BEGIN{fn="'"${f}"'"; n=split("'"${suffix}"'",f," ")}
NR>1{for(i=1;i<=3;i++) {of=fn "." f[i]; print $1,$2,$(6+i) > of}}' \
$f.csv
for sfx in $suffix ; do
fn=`awk '{
nymd=split($2,ymd,"-");nhms=split(substr($3,1,length($3)-1),hms,":")
printf "%02d%02d%02d%02d%02d%02d",
ymd[1]%100,ymd[2],ymd[3],hms[1],hms[2],hms[3]
exit}' $f.$sfx`
echo ${3}$fn
done
# /bin/rm -f $f.csv
else
/bin/rm -f $f.csv ; echo "**No data for $yy $mm $dd" > /dev/tty
fi
}
if [ $# -eq 2 ]; then
case $mm in
01) nm=31 ls=0 ;;
02) nm=`echo $yy |
awk '{if($1%4 == 0 && $1%400 != 0) print 29; else print 28}'` ls=0 ;;
03) nm=31 ls=0 ;;
04) nm=30 ls=0 ;;
05) nm=31 ls=0 ;;
06) nm=30 ls=30 ;;
07) nm=30 ls=0 ;;
08) nm=31 ls=0 ;;
09) nm=30 ls=0 ;;
10) nm=31 ls=0 ;;
11) nm=30 ls=0 ;;
12) nm=31 ls=31 ;;
*) echo "$0: Invalid month $mm" ; exit 1 ;;
esac
for d in `echo $nm | awk '{for(i=1;i<=$1;i++) print i}'` ; do
dd=`echo $d | awk '{printf "%02d",$1}'`
dur=`duration $yy $dd $ls $lsfn`
pfx="${sta}${y0}${mm}${dd}000000"
ext localhost:8080 $sn \
$yy/$mm/${dd}_00:00:00 $dur $dur /tmp/$pfx
fnm=(`split /tmp $pfx`) sfx=(MPE MPN MPZ)
for f in 0 1 2 ; do
if=/tmp/${pfx}.${sfx[$f]}
if [ -s $if ]; then
masspos -sta ${sta} -time `awk '{print $2,$3; exit}' $if` \
-c ${sfx[$f]} -o ${dir}/${sta}${fnm[$f]}.${sfx[$f]} < $if
fi
/bin/rm -f $if
done
done
elif [ $# -eq 3 ]; then
dd=`echo $3 | awk '{printf "%02d",$1; exit}'`
case $3$4 in
0630|630) ls=30 ;;
1231) ls=31 ;;
*) ls=0 ;;
esac
declare -a fnm sfx
dur=`duration $yy $dd $ls $lsfn`
pfx="${sta}${y0}${mm}${dd}000000"
ext localhost:8080 $sn \
$yy/$mm/${dd}_00:00:00 $dur $dur /tmp/$pfx
nm=(`split /tmp $pfx`) sfx=(MPZ MPN MPE)
for f in 0 1 2 ; do
if=/tmp/${pfx}.${sfx[$f]}
if [ -s $if ]; then
masspos -sta ${sta} -time `awk '{print $2,$3; exit}' $if` \
-c ${sfx[$f]} -o ${dir}/${sta}${nm[$f]}.${sfx[$f]} < $if
fi
/bin/rm -f $if
done
else
echo "$0: usage: domasspos [-store xxxx zzzz |"
echo " [-sn store#] [ -dir aaa ] yyyy mm [dd]]"
echo " where -store xxxx zzzz sets the store file for station zzzz"
echo " to the file xxxx; must be set once to change store/station"
echo " combination. Only option allowed if used."
echo " -sn store# -- optional store number if not able to determine"
echo " from the store file name (see -store)"
echo " -dir aaa -- optional directory to place files (default: /tmp);"
echo " will be named 'aaa/zzzzyymmddhhmmss.MP[ENZ]' based on first"
echo " sample time"
echo " and yyyy mm [dd] - year, month and (optional) day for data"
echo " extraction; if no day, all days in month extracted."
echo " An Apollo Lite server must be running on port 8080"
fi