#!/bin/bash

# ----------------------------------------------------------------------
# RPC Project :
# RPC_download_data_onemonth_nolog_CLUFGM : download FGM data from CAA
#                                   for one entire month and 4 S/C
#                                   (no log file produced)
#
# usage: RPC_download_data_onemonth_nolog_CLUFGM  year month  Mode
# ex   : RPC_download_data_onemonth_nolog_CLUFGM  2009 12 SPIN"
# or   : RPC_download_data_onemonth_nolog_CLUFGM  2009 12 5VPS"
#
# P. Robert, LPP, 2012-09-11
#
# ----------------------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36`
Narg=3

if( (test $# = 1 ) && (test $1 = -h ) ) ; then hh=1 ; else hh=0 ; fi

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : get entire day of FGM data SPIN resolution from CAA"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   year month Mode"
   echo "    ex: $appli 2001 09 SPIN"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo "$appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

datim1=`date +%F'  '%H':'%M':'%S `
julsec1=`date +%s`

Mode=$3

# Mode test
# ---------

if ((test $Mode != SPIN ) && (test $Mode != 5VPS) && (test $Mode != FULL))
   then
     echo "Mode must be only SPIN, 5VPS or FULL"
     echo "$appli36 : *** ERROR ! Command aborted." >&2
     exit 2
fi


year=$1
month=$2


echo " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo " RPC_download_data_onemonth_CLUFGM_nolog :product $year"_"$month of CLUSTER/FGM $Mode"
echo "                                    from  CAA data base for 4 S/C, convert in RFF"
echo "                                    and put it in Big_DataBase_CLUSTER"
echo ""

# Check existence of directories for files destination

dirCEF=./Big_CEF_database_CLUSTER/FGM/$Mode/$year/$year"_"$month
dirRFF=./Big_RFF_database_CLUSTER/FGM/$Mode/$year/$year"_"$month

if ! test -d     $dirCEF
   then 
        mkdir -p $dirCEF
   else 
   #    C1_CP_FGM_SPIN__20100101.cef
        ls *_CP_FGM_$Mode*.cef 2> /dev/null > /dev/null
        err=$?
        if test $err = 0
           then
               echo "Directory $dirCEF" >&2
               echo "is already existing and contains files as *_CP_FGM_$Mode*.cef" >&2
               echo "Do you want to erase all these files ? (y/n)" >&2
               read yeye
               if test $yeye = y
                  then rm -f  $dirCEF/*_CP_FGM_$Mode*.cef
                      echo "Files destroyed !" >&2
                      echo
                  else
                      echo "So, manage your files & directories an try again..." >&2
                      echo "$appli36 : ***  Command aborted by user" >&2
                      exit 3
               fi
        fi
fi

if ! test -d     $dirRFF
   then 
        mkdir -p $dirRFF
   else 
   #    CLU4_FGM_VTL2_SPIN_20100102.rff
        ls *_FGM_VTL2_$Mode*.rff 2> /dev/null > /dev/null
        err=$?
        if test $err = 0
           then
               echo "Directory $dirRFF" >&2
               echo "is already existing and contains files as *_FGM_VTL2_$Mode*.rff" >&2
               echo "Do you want to erase all these files ? (y/n)" >&2
               read yeye
               if test $yeye = y
                  then rm -f  $dirRFF/*_FGM_VTL2_$Mode*.rff
                      echo "Files destroyed !" >&2
                      echo
                  else
                      echo "So, manage your files & directories an try again..." >&2
                      echo "$appli36 : ***  Command aborted by user" >&2
                      exit 3
               fi
        fi
fi

if ! test -d     $dirOUT
   then 
        mkdir -p $dirOUT
   else 
        rm -f    $dirOUT/*.out
fi

out_onemonth=download_data_onemonth_CLUFGM_$Mode"_"$year"_"$month.out

if test -f $out_onemonth ; then rm $out_onemonth ; fi

# loop on the day in the given month
# ----------------------------------

list=`RPC_list_days_of_month $year $month`

for day in $list
    do
    for SatNum in 1 2 3 4
        do
        RPC_download_data_oneday_CLUFGM $SatNum $year $month $day $Mode >> $out_onemonth
  
        if test $? = 0 
           then
           # C1_CP_FGM_SPIN__20100101.cef
           # CLU4_FGM_VTL2_SPIN_20100102.rff
           cefname=C$SatNum"_CP_FGM_"$Mode"__"$year$month$day.cef
           rffname=CLU$SatNum"_FGM_VTL2_"$Mode"_"$year$month$day.rff
           if test -f $cefname
              then
              echo "$cefname created"
#             conversion in rff
              RPC_cef_to_rff_CLUFGM $cefname
              if test $? = 0
                 then
#                put rff and cef in right directories
                 mv $cefname    $dirCEF
                 echo "cef file put in $dirCEF"
                 mv $rffname    $dirRFF
                 echo "rff file put in $dirRFF"
                 rm cef_to_rff_CLUFGM.*
                 
                 else
                 echo "*** $rffname not created !!"
              fi

              else
              echo "*** $cefname not created !"
           fi
           else
           echo "$appli36 : *   WARNING !!"
           echo "$appli36 $* failed !!" >&2
        fi

    done
done

# files clean up and computing time
# ---------------------------------

datim2=`date +%F'  '%H':'%M':'%S `
julsec2=`date +%s`
diff=`expr $julsec2 - $julsec1`
diffm=`echo "scale=2; $diff/60." | bc `

echo ""
echo " Starting time $appli : $datim1"
echo " Ending   time $appli : $datim2"
echo " Duration      $appli : $diff sec.  ($diffm mn.)"

echo " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

