#!/bin/ksh

#This is a near-realtime script for 

# -------------------------- BEGIN PROCESSING -----------------------------
export PATH=$PATH:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/mcidas/bin:$HOME/bin:
export MCPATH=$HOME/mcidas/data:/home/mcidas/data
export mcworkdir=$HOME/mcidas/data
#export MCTRACK;MCTRACK=NO

#Each user needs to specify where their scratch folders are located
TMPDIR=/data1/Satellite_Input/clavrx_ops/scratch/IODC/

OUTPUT_DIR=/data1/Satellite_Input/IODC/
curdir=`pwd`

cnt=$(ps -ef|grep -v grep|grep ops|grep mcenv|wc -l)

while [ $cnt -gt 2 ] ; do
  cnt=$(ps -ef|grep ops|grep -v grep|grep mcenv|wc -l)
  echo $cnt >/dev/null
  sleep 120
done

export OUTPUT_DIR
export mcworkdir=$TMPDIR
mkdir $mcworkdir
export MCPATH=$mcworkdir:/home/mcidas/data

mcenv -f 1@700x700 <<'EOF'

logon.k STW 2705
dataloc.k ADD IODCEND INDOEX.SSEC.WISC.EDU
dsserve.k ADD MET/AREA AREA 3000 3005

#initialize status checks
ret_file_copy=1
status_time=1

jday=$(date +%Y%j -u --date="18 minutes ago")
hour_min=$(date +%H -u --date="17 minutes ago"):$(date +%M -u --date="17 minutes ago")

hour_min_new=$(date +%H -u --date="19 minutes ago"):$(date +%M -u --date="19 minutes ago")

echo $hour_min $hour_min_new $jday

#imglist.k IODC/FD.ALL DAY=$jday TIME=$hour_min_new $hour_min DEV=T $mcworkdir/tempiodc.done
imglist.k IODCEND/FD.0 DEV=T $mcworkdir/tempiodc.done

nt=`wc $mcworkdir/tempiodc.done| awk '{print $1;}'`
echo $nt
nt=`expr ${nt} - 4`

num=1
count=5 # line to start on, which is the 5th line

while [ ${num} -le ${nt} ] ; do

  pos=`cat $mcworkdir/tempiodc.done|awk -v jl=${count} '{if (NR==jl) print $1}'`

#---stw  imgcopy.k IODC/FD.${pos} MET/AREA.1 SIZE=SAME BAND=1 DOC=YES
  imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=1 DOC=YES
  ret_file_copy=$?

# Now, if the file is copied correctly, continue with processing of file.

if [[ ret_file_copy -eq 0 ]]; then
#Seperate out each band
#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=1 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=1 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      echo $newname
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area
      first_file=${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=2 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=2 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=3 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=3 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=4 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=4 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=5 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=5 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=6 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=6 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=7 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=7 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=8 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=8 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=9 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=9 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=10 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=10 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area

#      imgcopy.k MSG/FD.${pos} MET/AREA.1 SIZE=SAME BAND=11 DOC=YES
      imgcopy.k IODCEND/FD.0 MET/AREA.1 SIZE=SAME BAND=11 DOC=YES
      imgcha.k MET/AREA.1 LINE=1 ELE=1
      newname=$(charea $mcworkdir/AREA3000)
      cp $mcworkdir/AREA3000 $mcworkdir/${newname}.area


      ret=$?

      mv $mcworkdir/*.area $OUTPUT_DIR

      touch /data1/Satellite_Input/event/IODC/${first_file}.event
      echo "iodc event : /data1/Satellite_Input/event/IODC/${first_file}.event"
      touch /data1/Satellite_Input/IODC_events/${first_file}.event

fi


count=`expr ${count} + 1`
num=`expr ${num} + 1`

done


exit
EOF


#rm -rf $TMPDIR
exit
