#!/bin/bash

set -x

pwd=/ftp/katabata

for station in 300234067596640 300534060460680 300234067590460 ; do

 cd $pwd

 i=0
 for file in ${station}*.sbd ; do
  if [ -f $file ] ; then
   [ $i -eq 0 ] && sleep 5
   mkdir -p ${station:11:4} &>/dev/null
   mv $file ${station:11:4}
   i=1
  fi
 done

 cd $pwd/${station:11:4}

 chmod go+r      *.sbd
 chown root:root *.sbd

 start=`ls -1 ${station}_*.sbd | head -n 1`
   end=`ls -1 ${station}_*.sbd | tail -n 1`

 start=${start%.sbd} ; start=${start#*\_} ; start=${start#*0} ; start=${start#*0} ; start=${start#*0}
 end=${end%.sbd} ; end=${end#*\_} ; end=${end#*0} ; end=${end#*0} ; end=${end#*0}

 for i in `seq $start $end` ; do
 
                     ii=$i
  [ $i -le 9999 ] && ii="00$i"
  [ $i -le  999 ] && ii="000$i"
  [ $i -le   99 ] && ii="0000$i"
  [ $i -le    9 ] && ii="00000$i"
  [ ! -f ${station}_$ii.sbd ] && echo "${station}_$ii.sbd is missing" 
 done


 cd $pwd/dat

# File data

 cat $pwd/${station:11:4}/${station}_00*.sbd | grep MAWS  > data.txt
 cat $pwd/${station:11:4}/${station}_01*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_02*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_03*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_04*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_05*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_06*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_07*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_08*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_09*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_10*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_11*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_12*.sbd | grep MAWS >> data.txt
 cat $pwd/${station:11:4}/${station}_13*.sbd | grep MAWS >> data.txt


 sed "s/20\-/20\ /g" data.txt   > data1.txt
 sed "s/21\-/21\ /g" data1.txt  > data2.txt
 sed "s/22\-/22\ /g" data2.txt  > data3.txt
 sed "s/23\-/23\ /g" data3.txt  > data4.txt
 sed "s/24\-/24\ /g" data4.txt  > data.txt

 sed "s/01\-/01\ /g" data.txt   > data1.txt
 sed "s/02\-/02\ /g" data1.txt  > data2.txt
 sed "s/03\-/03\ /g" data2.txt  > data3.txt
 sed "s/04\-/04\ /g" data3.txt  > data4.txt
 sed "s/05\-/05\ /g" data4.txt  > data5.txt
 sed "s/06\-/06\ /g" data5.txt  > data6.txt
 sed "s/07\-/07\ /g" data6.txt  > data7.txt
 sed "s/08\-/08\ /g" data7.txt  > data8.txt
 sed "s/09\-/09\ /g" data8.txt  > data9.txt
 sed "s/10\-/10\ /g" data9.txt  > data10.txt
 sed "s/11\-/11\ /g" data10.txt > data11.txt
 sed "s/12\-/12\ /g" data11.txt > data.txt

 sed "s/\:/\ /g"     data.txt   > data3.txt
 sed "s/MAWS/\ /g"   data3.txt  > data4.txt
 mv data4.txt data-${station:11:4}.txt
 rm -f data.txt data1.txt data2.txt data3.txt data4.txt data5.txt data6.txt data7.txt data8.txt data9.txt data10.txt data11.txt

# Fortran

 echo "../dat/data-${station:11:4}.txt" > $pwd/f90/file.txt
 cd $pwd/f90
  ./a.out

 cd $pwd/dat
 mv data-${station:11:4}.txt  data-${station:11:4}-org.txt
 mv data-${station:11:4}.txt2 data-${station:11:4}.txt
 [ -d /srv/web/html/data ] && cp data-${station:11:4}.txt  /srv/web/html/data
done

