196c51582a08efff4770b89924d4dac4bd9286bc
[jendabot] / sonde.sh
1 #!/bin/bash
2
3 shour=$1
4 smin=$2
5 nocz=$3
6
7 export LANG=C
8
9 towncoords=sonde/towncoords.txt
10 towncoords_l2=sonde/towncoords-trans.txt
11
12 latbrm=50.0986739
13 lonbrm=14.435838
14 maxdist=25
15 maxreportdist_prague=300
16 maxreportdist_other=50
17
18
19 predict() {
20 local baselat=$1
21 local baselon=$2
22 local basealt=$3
23 local drag=$4
24
25 local outtries=5
26
27 local uuid=
28 local lat=
29 local lon=
30
31 while [ -z "$lat" ]; do
32 read day month year <<<$(TZ=UTC date +'%d %m %Y')
33 local uuid=$(wget -q -O- --post-data="lat=$baselat&lon=$baselon&initial_alt=$basealt&hour=$shour&min=$smin&day=$day&month=$month&year=$year&ascent=5&burst=35000&drag=$drag&submit=1" http://predict.habhub.org/ajax.php?action=submitForm | sed -r 's/.*uuid":"([^"]*)".*/\1/')
34 if grep -q '{' <<<"$uuid"; then
35 echo "$* prediction error: $(tr '\n' ' ' <<<"$uuid")"
36 exit 1
37 fi
38 local intries=5
39 while [ -z "$lat" ]; do
40 sleep 2 # Give some time for server predictor to run; just to be on the safe side ;)
41 read x lat lon x <<<$(curl -sS http://predict.habhub.org/preds/$uuid/flight_path.csv | tail -n 1 | tr , ' ')
42 intries=$((intries - 1))
43 if [ "$intries" -eq 0 ]; then
44 break
45 fi
46 done
47 if [ -n "$lat" ]; then
48 echo "$uuid $lat $lon"
49 echo "*** $uuid $lat $lon" >>/tmp/sonde.bclog
50 break
51 fi
52 outtries=$((outtries - 1))
53 if [ "$outtries" -eq 0 ]; then
54 echo "$uuid 0 0"
55 break
56 fi
57 done
58 }
59
60
61 # Haversine formula in bash / bc
62 # Adapted from http://www.movable-type.co.uk/scripts/latlong.html
63 DISTFUN="
64 define hdist(lat1,lon1,lat2,lon2) {
65 pi=4*a(1)
66 dlat = (lat2-lat1)*pi/180
67 dlon = (lon2-lon1)*pi/180
68 lat1 = lat1*pi/180
69 lat2 = lat2*pi/180
70
71 a = s(dlat/2) * s(dlat/2) + s(dlon/2) * s(dlon/2) * c(lat1) * c(lat2)
72 c = 2 * a(sqrt(a) / sqrt(1-a))
73 d = 6371 * c
74 return (d)
75 }"
76
77 dist() {
78 cat <<EOT | tee -a /tmp/sonde.bclog | bc -l
79 # <$*> $latbrm $lonbrm #
80 $DISTFUN
81
82 d = hdist($1,$2, $3,$4)
83 d
84 d <= $maxdist
85 d <= $maxreportdist
86 EOT
87 }
88
89 nearest_town() {
90 cat <<EOT | tee -a /tmp/sonde.bclog | bc -l
91 $DISTFUN
92 min=1000
93 mini=0
94 $(cat "$3" | nl -s',' -w1 | cut -d, -f1,3,4 | sed -r 's/^(.*),(.*),(.*)$/ \
95 d = hdist(\2, \3, '$1', '$2'); \
96 if (d < min) { \
97 mini = \1; \
98 min = d; \
99 } \
100 /')
101 mini
102 min
103 EOT
104 }
105
106 sondecheck() {
107 local origin=$1
108 local baselat=$2
109 local baselon=$3
110 local basealt=$4
111 local drag=$5
112 maxreportdist=$6
113
114 read uuid lat lon <<<$(predict "$baselat" "$baselon" "$basealt" "$drag")
115 read dist isnear willreport <<<$(dist "$lat" "$lon" "$latbrm" "$lonbrm")
116 dist=$(printf '%.1f' "$dist")
117
118 read nrnt nrnt_d <<<$(nearest_town "$lat" "$lon" "$towncoords")
119 nearest=$(cat "$towncoords" | tail -n +$nrnt | head -n 1 | cut -d, -f1)
120
121 read nrnt2 nrnt2_d <<<$(nearest_town "$lat" "$lon" "$towncoords_l2")
122 nearest_l2=$(cat "$towncoords_l2" | tail -n +$nrnt2 | head -n 1 | cut -d, -f1)
123
124 if [ "$nearest_l2" != "$nearest" ]; then
125 nearest="$nearest, $nearest_l2"
126 fi
127
128 echo "$origin $uuid $lat,$lon : $dist,$isnear" >>/tmp/sonde.bclog
129
130 if [ $isnear = 1 ]; then
131 echo "The $shour:$smin $origin sonde will land only $dist km away from brmlab! (Near $nearest) http://predict.habhub.org/#!/uuid=$uuid"
132 elif [ $willreport = 1 ]; then
133 echo "The $shour:$smin $origin sonde will land $dist km away from brmlab (near $nearest) http://predict.habhub.org/#!/uuid=$uuid"
134 fi
135 }
136
137 echo "===== `date`" >>/tmp/sonde.bclog
138 [ -n "$nocz" ] || sondecheck Prague 50.0078294 14.4464886 270 15 $maxreportdist_prague
139 sondecheck Meiningen 50.562500 10.377222 453 4 $maxreportdist_other
140 sondecheck Kuemmersbruck 49.428889 11.902222 418 4 $maxreportdist_other
141 [ -n "$nocz" ] || sondecheck Prostejov 49.450000 17.130000 216 15 $maxreportdist_other