#!/bin/bash

# This code is to be run at 11:50 every night. 

ST="SBBE"
NH="24"
LP="/gea/data/towers/sbbe"
LB=`/gea/data/towers/sbbe/Date`

#echo wget -O $LP/NOAA_"$LB".xml https://www.wrh.noaa.gov/mesowest/getobextXml.php?sid=$ST'&num='$NH

# It will retry for 9 times (one per minute) before giving up.
n=0
until [ $n -ge 10 ]
do
  opf="$LP/NOAA_""$LB"".xml"
  lof="$LP/logfile"
  wget -o $lof -O $opf https://www.wrh.noaa.gov/mesowest/getobextXml.php?sid=$ST'&num='$NH  
  errc="$?"
  if [[ "$errc" -eq 0 ]]; then
    chmod -w $opf
    break
  fi
  n=$[$n+1]
  sleep 60
done

if [[ "$errc" -ne 0 ]]; then 
  echo ERROR CODE: $errc
  cat $LP/logfile
fi





