#!/bin/tcsh -f

mkdir zip

### download raw data:

# wget ftp://ftp.ncdc.noaa.gov/pub/data/globaldatabank/monthly/stage3/recommended/results/recommended-isti_format.monthly.stage3.v1.0.0.20140630.tar.gz
# 
# tar zxvf recommended-isti_format.monthly.stage3.v1.0.0.20140630.tar.gz
# 
# mv recommended-isti_format.monthly.stage3.v1.0.0.20140630.tar.gz zip

set indir = results_merged


### download and rename station networks:

# wget http://tinyurl.com/samsi-workshop-cases
# unzip samsi-workshop-cases.zip
# mv samsi-workshop-cases.zip zip
# 
# mv CA002400600.txt network.canada1
# mv CA003031093.txt network.canada2
# mv CA005063075.txt network.canada3
# mv MA000067083.txt network.madagascar
# mv SZ000001940.txt network.switzerland
# mv USW00023185.txt network.us1
# mv USW00024157.txt network.us2


echo "id              lat       lon       elev    fyr  fm lyr  lm" > header.sd
echo "year mo  tmax   tmin   tavg"                                 > header.ts
echo "id           tmax   tmin   tavg"                             > header.syn


#foreach region (switzerland madagascar canada1 canada1 canada3 us1 us2)
foreach region (madagascar switzerland)

  echo "########## $region"

  mkdir -p $region/raw-isti $region/synoptic $region/timeseries

  cut -f 2 -d ' ' network.$region | grep -v NEIGHBORS | grep -v TARGET > stationlist

  echo "#### converting timeseries from ISTI format to plain-text format"

  foreach station (`cat stationlist`)

    echo $station
    set i = $indir/merge_${station}_stage3    

    cut -b 63-89 $i > temp
    cut -b 1-4 temp > temp.1
    cut -b 5-6 temp > temp.2
    cut -b 7-9 temp > temp.junk
    cut -b 10-12 temp > temp.3
    cut -b 13-18 temp > temp.4
    cut -b 19-24 temp > temp.5
    cut -b 25- temp > temp.6

    paste -d ' ' temp.1 temp.2 > temp.a
    paste -d '.' temp.3 temp.4 temp.5 temp.6 > temp.b
    paste -d ' ' temp.a temp.b > temp.c

    sed -e 's/ \. /0.0/g' temp.c > temp.d
    sed -e 's/   \.-/ -0.0/g' temp.d > temp.e
    sed -e 's/ \./0./g' temp.e > temp.f

    cp header.ts $region/timeseries/$station
    cat temp.f >> $region/timeseries/$station

    rm temp*
  end



  echo "#### collating static station info"

  cp header.sd $region/station.data

  foreach station (`cat stationlist`)
    echo $station
    set i = $indir/merge_${station}_stage3    
    set j = $region/timeseries/$station

    echo $station > temp.id
    head -1 $i | cut -b 31-61 > temp.loc

    sed '2q;d' $j | cut -b 1-7  > temp.first
    tail -n 1 $j  | cut -b 1-7  > temp.last

    paste -d ' ' temp.id temp.loc temp.first temp.last >> $region/station.data

    rm temp*
  end


  rm stationlist



  echo "#### transposing timeseries to synoptic"

  set first = `tail -n +2 $region/station.data | cut -b 45-48 | sort -n | sed '2q;d'`
  set  last = `tail -n +2 $region/station.data | cut -b 53-56 | sort -n | tail -1`


  foreach year (`seq $first $last`)
    foreach month (01 02 03 04 05 06 07 08 09 10 11 12)
      echo $year $month

      grep "$year $month" $region/timeseries/* | sed 's/:/ /' | cut -f 3- -d / | cut -f 1,4- -d ' ' > temp

      if (! -z temp) then
        cat header.syn temp > $region/synoptic/$year$month
      endif
      rm temp
    end
  end


  echo "NOTE!  Data before 1950 moved to pre-1950-synoptic"
  mkdir $region/pre-1950-synoptic
  mv $region/synoptic/1[78]* $region/synoptic/19[01234]* $region/pre-1950-synoptic
  
end
