#!/bin/bash
# -------------------------------------------------------
# RPC Project :
# RPC_download_data_CLUFGM : get FGM data from CAA
# P. Robert, LPP, 2012-08-25
# -------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : get FGM data from CAA for several days" 
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   SatNum date_iso1 date_iso2 mode"
   echo "ex:"
   echo "$appli 3 2001-09-01T09:00:00.OOOZ  2001-09-01T09:00:00.OOOZ  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`

SatNum=$1
datim_deb=$2
datim_fin=$3
Mode=$4


# Spacecraft test
# ---------------

if ((test $SatNum -lt 1 ) || (test $SatNum -gt 4 ))
   then 
        echo "S/C # must be >=1 and =< 4"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 2
fi

# 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 3
fi

# input parameters for downloading data

mission=CLUSTER
experi=FGM

echo " -------------------------------------------------------------------"
echo " RPC_CLUFGM_download_data_oneday: dowload t1-t2 in a day of $mission/$experi"
echo "                                  data for time resolution=$Mode"
echo "                                  Span time choosen : $datim_deb - $datim_fin  for S/C#$SatNum"
echo ""

# build wget command

miss1=`echo $mission | cut -c1`
dataset_id=$miss1$SatNum"_CP_"$experi"_"$Mode
format='CEF'

echo "dataset_id=$dataset_id"
echo "datim_deb=$datim_deb"
echo "datim_fin=$datim_fin"


date1=`echo $datim_deb | cut -c1-4``echo $datim_deb | cut -c6-7``echo $datim_deb | cut -c9-10`
date2=`echo $datim_fin | cut -c1-4``echo $datim_fin | cut -c6-7``echo $datim_fin | cut -c9-10`

time1=`echo $datim_deb | cut -c12-13``echo $datim_deb | cut -c15-16``echo $datim_deb | cut -c18-19`
time2=`echo $datim_fin | cut -c12-13``echo $datim_fin | cut -c15-16``echo $datim_fin | cut -c18-19`

main_name=$dataset_id"__"$date1"_"$time1"_"$date2"_"$time2

echo " main_name=$main_name"
echo
echo " Run wget command..."

csacookie=`cat $RPC_DIR/csa_cookie.txt`

#wget --no-check-certificate -O ./$main_name.cef "https://csa.esac.esa.int/csa/aio/streaming-action?DATASET_ID=$dataset_id&START_DATE=$datim_deb&END_DATE=$datim_fin&NON_BROWSER&CSACOOKIE=$csacookie" 2>&1
wget --no-check-certificate -O ./$main_name.cef "https://csa.esac.esa.int/csa-sl-tap/data?RETRIEVAL_TYPE=product&RETRIEVAL_ACCESS=streamed&DATASET_ID=$dataset_id&START_DATE=$datim_deb&END_DATE=$datim_fin&NON_BROWSER&CSACOOKIE=$csacookie" 2>&1

err=$?
if test $err = 0
   then
   echo " wget done !"
   echo " main_name=$main_name"
   else
   echo " *** wget failed!"
   exit 1
fi


# 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.)"

appli=`echo $appli | cut -c5-`
file_out=$appli.out

if test -f $main_name.cef
   then
       echo " "
       echo "$main_name is ready in the current directory !"
       echo
       echo $main_name > $file_out
       echo " file name is copied in $file_out"
       echo    
       echo " $appli36 : NORMAL TERMINATION - time exe= $diff s." >&2 
   else

if test -f $main_name.tar.gz
   then
       echo " "
       echo "extract $main_name.tar.gz ..."
       echo " "
       tar xvf $main_name.tar.gz
       find . -type f -name "$main_name*.cef"  -exec mv {} . \;
       error=$?
       echo " "
       if test $error = 0
          then 
               mv $main_name"_"V*.cef $main_name.cef
               echo "$main_name.cef created "

#              delete possible caveats
               rm -fr CSA_Download*
               rm $main_name.tar.gz
               echo $main_name > $file_out
               echo " file name is copied in $file_out"
               echo    
               echo " $appli36 : NORMAL TERMINATION - time exe= $diff s." >&2 
       
          else 
               echo "**** ERROR in tar xvf !  $main_name.cef not created"
               echo "     but $main_name.tar.gz exists"
               echo " $appli36 : *** ABORTED !!!" >&2
               exit 4
       fi
   else
      echo " "
      echo "*** file $main_name.tar.gz not created"
      echo " $appli36 : *** ABORTED !!!"
      exit 5
      echo 
fi
fi

echo " -------------------------------------------------------------------"

