-
Notifications
You must be signed in to change notification settings - Fork 3
/
gnssIR_api
executable file
·308 lines (280 loc) · 8.69 KB
/
gnssIR_api
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
#!/bin/bash
#
if [ "$#" -eq 0 ]; then
echo please see https://github.com/kristinemlarson/gnssIR_api
echo
exit
fi
running_local=true
running_local=false
if [ "$running_local" = true ]; then
echo running local
heroku="http://127.0.0.1:5000/api"
heroku0="http://127.0.0.1:5000/api"
else
echo not running local
heroku="https://gnss-reflections.org/api"
heroku0="http://trywithgps2.herokuapp.com/api"
fi
#
# define a new output directory if you like. This one assumes you will put the results in
# the directory below where you are running this code.
outputdir='./solutions'
#
# defaults for elevation angles (degrees), Reflector Heights (meters) and frequency
# h1 should never be less than 0.5
e1=5
e2=25
h1=0.5
h2=7
freq=L1
# these are defaults
pk2noise=4.0
amp=5
rinex=None
# declare these variables so they are integers
declare -i azim1=0
declare -i azim2=360
declare -i doy_end=0
declare -i doy=0
# This variable just sends output to the screen. Even if you don't set it there will be a daily
# average file created
snow=False
#
# I am new to bash scripts - so ....
# As long as there is at least one more argument, keep looping
declare -i k=0
while [[ $# -gt 0 ]]; do
key="$1"
k+=1
#echo key is $k $key
if [ "$k" -eq 1 ]; then
station=$key
shift
elif [ "$k" -eq 2 ]; then
year=$key
shift
elif [ "$k" -eq 3 ]; then
doy=$key
shift
else
case "$key" in
# This is a flag type option. Will catch either -f or --foo
-f|--foo)
FOO=1
;;
# This is an arg value type option. Will catch -o value or --output-file value
-o|--output-file)
shift # past the key and to the value
OUTPUTFILE="$1"
;;
-e1|--min-elev-file)
shift # past the key and to the value
e1="$1"
;;
-e2|--max-elev-file)
shift # past the key and to the value
e2="$1"
;;
-h1|--min-refl-ht)
shift # past the key and to the value
h1="$1"
;;
-h2|--max-refl-ht)
shift # past the key and to the value
h2="$1"
;;
-freq|--frequency)
shift # past the key and to the value
freq="$1"
;;
-amp|--amplitude)
shift # past the key and to the value
amp="$1"
;;
-azim1|--min-azimuth)
shift # past the key and to the value
azim1="$1"
;;
-azim2|--max-azimuth)
shift # past the key and to the value
azim2="$1"
;;
-doy_end|--doy_end)
shift # past the key and to the value
doy_end="$1"
;;
-rinex |--rinex)
shift # past the key and to the value
rinex="$1"
;;
-pk2noise |--pk2noise)
shift # past the key and to the value
pk2noise="$1"
;;
-snow|--snow)
shift # past the key and to the value
snow="$1"
;;
*)
# Do whatever you want with extra options
echo "Unknown option '$key'"
;;
esac
# Shift after checking all the cases to get the next option
shift
fi
done
echo rinex is $rinex
ok=0
if [ "$freq" == "L1" ] || [ "$freq" == "L2" ] ; then
ok=1
echo You have chosen a legal frequency, $freq
elif [ "$freq" == "L5" ] ; then
ok=1
echo You have chosen a legal frequency, $freq
elif [ "$freq" == "L1L2" ] || [ "$freq" == "L2C" ] || [ "$freq" == "L1L2CL5" ]; then
ok=1
echo You have chosen a legal frequency, $freq
fi
if [ $ok -eq 0 ]; then
echo $freq illegal frequency input, exiting
exit
fi
# check doy
if [ $doy -gt 0 ] && [ $doy -lt 367 ]; then
echo This is a legal day of year
else
echo This is an illegal day of year.
exit
fi
# check year
if [ $year -ge 2000 ] && [ $year -lt 2025 ]; then
echo This is a legal year although the year you chose might be in the future
else
echo This is an illegal year.
exit
fi
# check h2
if [ $h2 -gt 25 ] ; then
echo The max reflector height you have chosen is too big for the API.
echo It must be less than 25 meters. For a typical unobstructed site the
echo reflector height minimum should be 0.5 meters. For other sites you
echo might restrict the choices to eliminate soil, e.g. you want to only
echo see water reflections.
exit
fi
# make sure the output directories are there
if [ ! -e "${outputdir}" ]; then
echo "making output directory "
mkdir "${outputdir}"
fi
# make sure the output directories are there
if [ ! -e "${outputdir}/${station}" ]; then
echo "making station output directory "
mkdir "${outputdir}/$station"
fi
if [ ! -e "${outputdir}/${station}/${year}" ]; then
echo "making station year output directory "
mkdir "${outputdir}/${station}/${year}"
fi
# if not set, have doy_end be the same of doy,
if [ $doy_end -eq 0 ]; then
doy_end=$doy
fi
# output for daily averages
comboFile=${outputdir}/${station}_RH_all.csv
echo Combination File $comboFile
echo Day of Year End is $doy_end
echo Look for solutions in the $outputdir directory
# name for the script that calls the API
scriptname=xit.${year}.${station}
# name for the temporary output
echo CHOICES THAT WILL BE SENT TO THE GNSS-IR API
echo Station $station
#echo Year $year
#echo Day of Year $doy
echo Elevation Angle choices $e1 $e2
echo Azimuth Angle choices $azim1 $azim2
echo Reflector Height restrictions $h1 $h2
echo Frequency choice $freq
echo Amplitude $amp
echo Peak to Noise $pk2noise
#echo End day of year $doy_end
while [ $doy -le $doy_end ] ; do
rm -f out.json
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
echo Analyzing Station $station Year $year Day of Year $doy
dailyname=test.${year}.${station}
# calculate string doy with exactly three characters
cdoy=$( printf '%03d' $doy )
if [ $rinex = None ]; then
echo You are using the archive section of the API
curl ${heroku}"?station=${station}&year=${year}&doy=${doy}&freq=${freq}&=${amp}&h1=${h1}&h2=${h2}&e1=${e1}&e2=${e2}&pk2noise=${pk2noise}&azim1=${azim1}&azim2=${azim2}&jsononly=True" -o out.json --silent
# this is where the results will be
cat out.json
resultnow=`cat out.json | grep fileAddress | awk '{print substr($2,2,86)}'`
# this is the status information
statusnow=`cat out.json | grep status | awk '{print $2}' | tr "," " " | sed 's/"//g' `
finished=false
x=20
while [ "$finished" = false ]
do
echo Checking job status.
sleep 2
x=$(( $x - 2 ))
if [ $x -eq 0 ]; then
finished=true
echo time has run out, exiting
fi
rm -f tmp.json
curl ${statusnow} --silent > tmp.json
cat tmp.json
var=`grep "finished" tmp.json | wc -l`
if [ $var -eq 1 ]; then
finished=true
echo the job has finished
#echo moving daily results to ${station}_${year}_${cdoy}.txt in the output directory
echo ${resultnow}
curl --silent ${resultnow} > tmpfile
# look for error messages which means data were not found
var2=`grep "Error" tmpfile | wc -l`
if [ $var2 -eq 0 ]; then
echo API for year $year and day of year $doy seems to have worked, ${outputdir}/${station}/${year}/${station}_${year}_${cdoy}.txt
mv tmpfile ${outputdir}/${station}/${year}/${station}_${year}_${cdoy}.txt
# make a new combination file
fi
rm -f tmpfile
fi
done
else
if test -f "$rinex"; then
echo "The RINEX file exists. Uploading."
else
echo "The RINEX file you submitted does not exist. Exiting."
exit
fi
curl --silent -F "file=@${rinex}" -F "jsononly=True" -F "azim1=${azim1}" -F "azim2=${azim2}" -F "freq=${freq}" -F "h1=${h1}" -F "h2=${h2}" -F "e1=${e1}" -F "e2=${e2}" -F "pk2noise=${pk2noise}" -F "amp=${amp}" ${heroku0} -o out.json
error3=`grep errormessage out.json | wc -l`
if [ $error3 -eq 1 ]; then
echo problem with your file
cat out.json
exit
fi
resultnow=`cat out.json | grep fileAddress | awk '{print substr($2,2,86)}'`
curl --silent $resultnow > tmpfile
mv tmpfile ${outputdir}/${station}/${year}/${station}_${year}_${cdoy}.txt
echo Results stored in ${outputdir}/${station}/${year}/${station}_${year}_${cdoy}.txt
fi
doy=$(( $doy + 1 ))
done # end of doy while loop
rm -f ${comboFile}
echo Combine the results into one file
echo \# https://gnss-reflections.org > ${comboFile}
echo \# Year,DayOfYear,ReflectorHeight\(m\),Month,Day,RHsigma >> ${comboFile}
cat ${outputdir}/${station}/????/*.txt | grep DailyAverage | awk '{print $3"," $4"," $5"," $6"," $7",0"}' >> ${comboFile}
echo Daily averages are stored here: ${comboFile}
echo Output directory ${outputdir}
# clean up
rm -f out.json
rm -f tmp.json