#!/bin/bash
#
# $1 = output synop CSV file
# $2 = output HTML map
# $3 = output JSON file
#
# https://www.ogimet.com/ultimos_synops2.php?lang=en&estado=Czec&fmt=html&Send=Send
# https://www.ogimet.com/getsynop_help.phtml.en
# https://www.ogimet.com/cgi-bin/getsynop?begin=201901281900&state=Cze&header=yes
# http://www.weather.org.uk/resource/syn_code.htm
# How far station is
function dalka()
{
local x=$(echo "$3-$1" | bc)
local y=$(echo "$4-$2" | bc)
echo "sqrt((($3-$1)^2)+(($4-$2)^2))" | bc -l| sed -e 's/^\./0./g' -e 's/^-\./-0./g'
}
# Angle to another station
function uhel ()
{
local x=$(echo "$3-$1" | bc)
local y=$(echo "$4-$2" | bc)
echo "$y/sqrt(($x*$x)+($y*$y))" | bc -l| sed -e 's/^\./0./g' -e 's/^-\./-0./g'
}
# Return close points $1=stanice $2=CSV
function blizke()
{
local id=$(echo "$1" | awk -F', ' '{print $4}' | grep -o -e'[0-9]*')
local x=$(echo "$1" | awk -F', ' '{print $2}' | sed -e 's/.$//g')
local y=$(echo "$1" | awk -F', ' '{print $3}' | sed -e 's/.$//g')
local m=$(echo "$1" | awk -F', ' '{print $1}')
local tab=""
while IFS='' read -r br || [[ -n "$br" ]]; do
local rid=$(echo "$br" | awk -F', ' '{print $4}' | grep -o -e'[0-9]*')
if [[ "$id" == "$rid" ]]; then
continue;
fi
local rx=$(echo "$br" | awk -F', ' '{print $2}' | sed -e 's/.$//g')
local ry=$(echo "$br" | awk -F', ' '{print $3}' | sed -e 's/.$//g')
local rmm=$(echo "$br" | awk -F', ' '{print $1}')
local rd=$(dalka "$x" "$y" "$rx" "$ry")
tab+="$rd $rid $m -> $rmm\n"
done <<< $(echo "$2")
tab=$(echo -e "$tab" | sort)
echo -e "$tab"
}
json="var data='{\"version\":\"$(date --rfc-3339=seconds)\",\"stanice\":["
#syno=$(wget "https://www.ogimet.com/cgi-bin/getsynop?begin=201901291800&state=Cze" -O - > /dev/null 2>&1
syno=$(cat /s/syno | cut -b 35- | sed -e 's/ /, /g' | sed -e 's/^11406/Cheb, 50.0683N, 12.3913E, 11406/g' -e 's/^11414/Karlovy Vary, 50.2016N, 12.9139E, 11414/g' -e 's/^11423/Primda, 49.6694N, 12.6779E, 11423/g' -e 's/^11438/Tusimice, 50.3765N, 13.3279E, 11438/g' -e 's/^11450/Plzen-Mikulka, 49.7645N, 13.3787E, 11450/g' -e 's/^11457/Churanov, 49.0683N, 13.615E, 11457/g' -e 's/^11464/Milesovka, 50.5549N, 13.9306E, 11464/g' -e 's/^11487/Kocelovice, 49.4672N, 13.8385E, 11487/g' -e 's/^11502/Usti n. Labem, 50.6833N, 14.0410E, 11502/g' -e 's/^11509/Doksany, 50.4587N, 14.1699E, 11509/g' -e 's/^11518/Praha-Ruzyne, 50.1003N, 14.2555E, 11518/g' -e 's/^11520/Praha-Libus, 50.0077N, 14.4467E, 11520/g' -e 's/^11538/Temelin, 49.1975N, 14.3421E, 11538/g' -e 's/^11546/C. Budejovice, 48.9519N, 14.4697E, 11546/g' -e 's/^11567/Praha-Kbely, 50.1232N, 14.538E, 11567/g' -e 's/^11603/Liberec, 50.7697N, 15.0238E, 11603/g' -e 's/^11624/Caslav, 49.9407N, 15.3863E, 11624/g' -e 's/^11628/Kramolin-Kosetice, 49.5735N, 15.0803E, 11628/g' -e 's/^11636/Kostelni Myslova, 49.159N, 15.4391E, 11636/g' -e 's/^11643/Pec p. Snezkou, 50.6918N, 15.7287E, 11643/g' -e 's/^11652/Pardubice, 50.1580N, 15.7402E, 11652/g' -e 's/^11653/Snezka, 50.68N, 15.44E, 11653/g' -e 's/^11659/Pribyslav, 49.5825N, 15.7623E, 11659/g' -e 's/^11669/Polom, 50.3503N, 16.3221E, 11669/g' -e 's/^11679/Usti n. Orlici, 49.9801N, 16.4221E, 11679/g' -e 's/^11683/Svratouch, 49.735N, 16.0342E, 11683/g' -e 's/^11692/Namest n. Oslavou, 49.1708N, 16.1205E, 11692/g' -e 's/^11693/Dukovany, 49.0954N, 16.1344E, 11693/g' -e 's/^11698/Kucharovice, 48.8809N, 16.0852E, 11698/g' -e 's/^11710/Luka, 49.6522N, 16.9533E, 11710/g' -e 's/^11723/Brno-Turany, 49.1530N, 16.6888E, 11723/g' -e 's/^11730/Serak, 50.1874N, 17.1082E, 11730/g' -e 's/^11747/Prostejov, 49.4525N, 17.1347E, 11747/g' -e 's/^11766/Cervená u Libavé, 49.7770N, 17.5418E, 11766/g' -e 's/^11774/Holesov, 49.3205N, 17.5699E, 11774/g' -e 's/^11782/Ostrava-Mosnov, 49.6918N, 18.1126E, 11782/g' -e 's/^11787/Lysa hora, 49.5459N, 18.4473E, 11787/g' -e 's/^11791/Maruska Hostalkova, 49.3650N, 17.8284E, 11791/g')
#echo "$syno" > "$1"
while IFS='' read -r radek || [[ -n "$radek" ]]; do
blizke "$radek" "$syno"
echo "---"
mesto=$(echo "$radek" | grep -o -e '^[^,]*')
gpsn=$(echo "$radek" | awk -F', ' '{print $2}'| sed -e 's/.$//g')
gpse=$(echo "$radek" | awk -F', ' '{print $3}'| sed -e 's/.$//g')
mestoid=$(echo "$radek" | awk -F', ' '{print $4}')
# SECTION 0
ir=$(echo "$radek" | awk -F', ' '{print $5}' | cut -b 1)
ix=$(echo "$radek" | awk -F', ' '{print $5}' | cut -b 2)
# vyska mraku
h=$(echo "$radek" | awk -F', ' '{print $5}' | cut -b 3)
# dohlednost
vv=$(echo "$radek" | awk -F', ' '{print $5}' | cut -b 4-5)
#echo "$ir $ix $h $vv"
# SECTION 1
n=$(echo "$radek" | awk -F', ' '{print $6}' | cut -b 1)
# smer vetru
dd=$(echo "$radek" | awk -F', ' '{print $6}' | cut -b 2-3)
# rychlost vetru v knotech
ff=$(echo "$radek" | awk -F', ' '{print $6}' | cut -b 4-5)
# rychlost vetru v m/s
ff=$(echo "0$ff*0.514" | bc | sed -e 's/^\./0\./g')
#echo "$n $dd $ff"
section1=$(echo "$radek" | grep -o -P -e ' (1[0-9/]{4}, )?(2[0-9/]{4}, )?(3[0-9/]{4}, )?(4[0-9/]{4}, )?(5[0-9/]{4}, )?(6[0-9/]{4}, )?(7[0-9/]{4}, )?(8[0-9/]{4}, )?333,' | cut -c 2- | sed -e 's/, 333,//g')
# znamenko teploty (0=+; 1=-)
s11sn=$(echo "$section1" | grep -o -P -e '1[0-9/]{4}' | cut -b 2 | tr '1' '-' | tr '0' '+')
# teplota v desetinach
s11ttt=$(echo "$section1" | grep -o -P -e '1[0-9/]{4}' | cut -b 3-5)
# teplota v °C
s11ttt=$(echo "$s11sn 0 $s11ttt*0.1" | tr -d ' ' | bc)
# rosny bod znamenko
s12sn=$(echo "$section1" | grep -o -P -e '2[0-9/]{4}' | cut -b 2)
# rosny bod teplota °C
s12ttt=$(echo "$section1" | grep -o -P -e '2[0-9/]{4}' | cut -b 3-5)
# tlak v mBar (stary ale v CR asi pouzivany)
s13tttt=$(echo "$section1" | grep -o -P -e '3[0-9/]{4}' | cut -b 2-5)
s13tttt=$(echo "((0$s13tttt)*100)+101325" | bc)
# tlak v mBar
s14pppp=$(echo "$section1" | grep -o -P -e '4[0-9/]{4}' | cut -b 2-5)
# tlak v Pa
s14pppp=$(echo "(0$s14pppp*0.1*100)+101325" | bc)
# zmena tlaku +-
s15a=$(echo "$section1" | grep -o -P -e '5[0-9/]{4}' | cut -b 2)
# zmena tlaku v desetinach mBar/3h
s15ppp=$(echo "$section1" | grep -o -P -e '5[0-9/]{4}' | cut -b 3-5)
# srazky mm/6h
s16rrr=$(echo "$section1" | grep -o -P -e '6[0-9/]{4}' | cut -b 2-4)
s16tr=$(echo "$section1" | grep -o -P -e '6[0-9/]{4}' | cut -b 5)
# aktualni pocasi
s17ww=$(echo "$section1" | grep -o -P -e '7[0-9/]{4}' | cut -b 2-3)
# minule pocasi
s17w1w2=$(echo "$section1" | grep -o -P -e '7[0-9/]{4}' | cut -b 4-5)
# oblacnost
s18nh=$(echo "$section1" | grep -o -P -e '8[0-9/]{4}' | cut -b 2)
# ...nizka
s18cl=$(echo "$section1" | grep -o -P -e '8[0-9/]{4}' | cut -b 3)
# ...stredni
s18cm=$(echo "$section1" | grep -o -P -e '8[0-9/]{4}' | cut -b 4)
# ...vysoka
s18ch=$(echo "$section1" | grep -o -P -e '8[0-9/]{4}' | cut -b 5)
#echo "$section1"
# SECTION 3
section3=$(echo "$radek" | grep -o -P -e ', 333, (1[0-9/]{4}((, )|(=+)))?(2[0-9/]{4}((, )|(=+)))?(3[0-9/]{4}((, )|(=+)))?(4[0-9/]{4}((, )|(=+)))?(5[0-9/]{4}((, )|(=+)))?(6[0-9/]{4}((, )|(=+)))?(7[0-9/]{4}((, )|(=+)))?(8[0-9/]{4}((, )|(=+)))*(9[0-9/]{4}((, )|(=+)))*((444)|(555))?' | cut -c 8- | sed -e 's/, 555$//g' -e 's/, 444$//g' | tr -d '=')
s31sn=$(echo "$section3" | grep -o -P -e '1[0-9/]{4}' | cut -b 2)
s31ttt=$(echo "$section3" | grep -o -P -e '1[0-9/]{4}' | cut -b 3-5)
s32sn=$(echo "$section3" | grep -o -P -e '2[0-9/]{4}' | cut -b 2)
s32ttt=$(echo "$section3" | grep -o -P -e '2[0-9/]{4}' | cut -b 3-5)
s33e=$(echo "$section3" | grep -o -P -e '3[0-9/]{4}' | cut -b 2)
s33sn=$(echo "$section3" | grep -o -P -e '3[0-9/]{4}' | cut -b 3)
s33sn=$(echo "$section3" | grep -o -P -e '3[0-9/]{4}' | cut -b 4-5)
s34e=$(echo "$section3" | grep -o -P -e '4[0-9/]{4}' | cut -b 2)
s34sss=$(echo "$section3" | grep -o -P -e '4[0-9/]{4}' | cut -b 3-5)
s38ns=$(echo "$section3" | grep -o -P -e '8[0-9/]{4}' | cut -b 2)
s38c=$(echo "$section3" | grep -o -P -e '8[0-9/]{4}' | cut -b 3)
s38hh=$(echo "$section3" | grep -o -P -e '8[0-9/]{4}' | cut -b 4-5)
s39=$(echo "$section3" | grep -o -P -e '9[0-9/]{4}' | cut -b 2-5)
#echo "$section3"
# SECTION 4
section4=$(echo "$radek" | grep -o -P -e ', 444, (1[0-9/]{4}((, )|(=+)))?(2[0-9/]{4}((, )|(=+)))?(3[0-9/]{4}((, )|(=+)))?(4[0-9/]{4}((, )|(=+)))?(5[0-9/]{4}((, )|(=+)))?(6[0-9/]{4}((, )|(=+)))?(7[0-9/]{4}((, )|(=+)))?(8[0-9/]{4}((, )|(=+)))*(9[0-9/]{4}((, 555,)|(=+)))*?' | cut -c 8- | sed -e 's/, 555,$//g' -e 's/, $//g' | tr -d '=')
#echo "$section4"
#SECTION 5
section5=$(echo "$radek" | grep -o -P -e ', 555, (1[0-9/]{4}((, )|(=+)))?(2[0-9/]{4}((, )|(=+)))?(3[0-9/]{4}((, )|(=+)))?(4[0-9/]{4}((, )|(=+)))?(5[0-9/]{4}((, )|(=+)))?(6[0-9/]{4}((, )|(=+)))?(7[0-9/]{4}((, )|(=+)))?(8[0-9/]{4}((, )|(=+)))*(9[0-9/]{4}((, )|(=+)))*' | cut -c 8- | tr -d '=')
s51vv=$(echo "$section5" | grep -o -P -e '1[0-9/]{4}' | cut -b 2-3)
s51ff=$(echo "$section5" | grep -o -P -e '1[0-9/]{4}' | cut -b 4-5)
s52sn=$(echo "$section5" | grep -o -P -e '2[0-9/]{4}' | cut -b 2)
s52ttt=$(echo "$section5" | grep -o -P -e '2[0-9/]{4}' | cut -b 3-5)
#echo "$section5"
# VYPIS
echo "$radek"
echo "$mesto $gpsn,$gpse $ff m/s $s11ttt °C $s13tttt Pa $s16rrr mm/6h";
json+="{"
json+="\"station\":\"$mesto\","
json+="\"gpse\":\"$gpse\","
json+="\"gpsn\":\"$gpsn\","
json+="\"wind\":\"$ff\","
json+="\"temp\":\"$s11ttt\","
json+="\"press\":\"$s13tttt\","
json+="\"rain\":\"$s16rrr\","
json+="\"raw\":\"$radek\"" # posledni bez ,
json+="},"
done <<< $(echo "$syno")
json+="{}]}'"
echo "$json" | sed -e "s/^var data='//g" -e "s/'$//g" > "$3"
# STATIC
js='<script>'
js+='map = new OpenLayers.Map("map");'
js+='map.addLayer(new OpenLayers.Layer.OSM());'
js+='epsg4326 = new OpenLayers.Projection("EPSG:4326");' # WGS 1984 projection
js+='projectTo = map.getProjectionObject();' # The map projection (Spherical Mercator)
js+='var lonLat = new OpenLayers.LonLat(15.478, 49.817).transform(epsg4326, projectTo);'
js+='var zoom=8;'
js+='map.setCenter (lonLat, zoom);'
js+="$json"
js+="</script>"
html='<!DOCTYPE html>'
html+='<html>'
html+='<head>'
html+='<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'
html+='<title>BrmMeteo</title>'
html+='<link rel="stylesheet" href="./style.css" type="text/css">'
html+='<link rel="favicon" href="./biohazard.svg">'
html+='<script src="./OpenLayers.js"></script>'
html+='</head>'
html+='<body>'
html+="<h1 id='title'>BrmMeteo - Let's make HAARP great again!</h1>"
html+="<div id='map' class='smallmap'></div>"
html+="<br><p>Welcome! You can download the <a href='./brmmeteo.json' target='_blank'>source data as JSON file</a><br>"
html+="This site is owned and governed by members of the <a href='https://brmlab.cz' target='_blank'>hackerspace brmlab</a>. See our wiki page for <a href='https://brmlab.cz/project/brmmeteo/start' target='_blank'>details and contact info</a><br>License: CC-BY-SA</p>"
html+="</body>"
html+="$js"
html+='<script src="./brmmeteo.js"></script>'
html+="</html>"
echo "$html" > "$2"