#!/bin/bash

# get just once - this is not a production script.


getdata()
{
  yyyy=$1
  mm=$2
  dd=$3
  station=$4
  hh=$5

  dfile=UWYO_S_$yyyy.$mm.$dd.$hh.txt
  rm -f tmp.html
  #wget --no-check-certificate -nv -O tmp.html "https://weather.uwyo.edu/cgi-bin/sounding?region=samer&TYPE=TEXT%3ALIST&YEAR=$yyyy&MONTH=$mm&FROM=$dd$hh&TO=$dd$hh&STNM=$station"
  curl -k -o tmp.html -sS "https://weather.uwyo.edu/cgi-bin/sounding?region=samer&TYPE=TEXT%3ALIST&YEAR=$yyyy&MONTH=$mm&FROM=$dd$hh&TO=$dd$hh&STNM=$station"
  html2text -o $dfile tmp.html 
}


if [[ $# -eq 0 ]]
then
  echo get_sounding_uwyo.sh yyyy0 mm0 dd0 yyyy1 mm1 dd1 SSSS
  echo Fetch sounding data for station 'SSSS' for all days in between 
  echo yyyy0/mm0/dd0  and  yyyy1/mm1/dd1.
  echo 'Soundings are made every 12 hours (00Z and 12Z) both are retrieved'
  echo 
  echo get_sounding_uwyo.sh 2020 01 01 2020 01 31 SBBE
  exit 1
fi

y1=$1
m1=$2
d1=$3
y2=$4
m2=$5
d2=$6
station=$7

./gendate.sh $y1 $m1 $d1 $y2 $m2 $d2 > tmp.dat

nl=`cat tmp.dat | wc -l `

for il in `seq 1 $nl`
do 
  getdata ` cat tmp.dat | head -n $il | tail -n 1 ` $station 00
  sleep 2
  getdata ` cat tmp.dat | head -n $il | tail -n 1 ` $station 12
  sleep 2

done


