#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# RPC_get_data_GEOMAG : get MAG data in RFF database
#                            search also .gz files
# P. Robert, LPP, 2014-11-14, rev Nov 2020
# -------------------------------------------------------

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 GEOS/MAG data"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   SatNum year month day"
   echo "    ex: $appli 1 1977 07 13"
   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
year=$2
month=$3
day=$4


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

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


dir=$R_RFF_data_GEO/MAG/$year/$year"_"$month

echo " -------------------------------------------------------------------"
echo " RPC_get_data_GEOMAG : get MAG data from RFF GEOS data base "
echo "                            Day choosen : $*"
echo " search files in :"
echo " $dir"
echo ""

# cp MAG file from data base to current directory
# ------------------------------------------------

if test ! -d $dir
   then echo " *** non-existing directory in RFF database, so no files found"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 4
fi

gz=0

ls $dir/GEOS$SatNum*MAG*$year$month$day*.rff > toto1_$$.tmp 2> /dev/null

if test $? != 0 
   then echo " *** No rff files found ! search for gz files..."
   ls $dir/GEOS$SatNum*MAG*$year$month$day*.rff.gz > toto1_$$.tmp 2> /dev/null
   if test $? != 0 
   then echo " *** No rff.gz files found ! "
        echo
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        rm toto1_$$.tmp
        exit 5
   else gz=1
   fi
fi

if test -f get_data_GEOMAG.out ; then rm get_data_GEOMAG.out ; fi
touch get_data_GEOMAG.out    
   
# temporary exec shell files   
sed "s/^/basename /" toto1_$$.tmp > toto2_$$.tmp
sed -e "s/basename/cp/" -e "s/$/ ./" toto2_$$.tmp > toto3_$$.tmp
sh toto2_$$.tmp | sed "s/^/ls -l /" > toto4_$$.tmp

echo " files found: " 
sh toto2_$$.tmp | sed "s/^/ /"

echo
echo " copy in current directory"
echo " Please wait..."
echo
sh toto3_$$.tmp

echo " files copied:"
sh toto4_$$.tmp 2> /dev/null

if test $? = 0
   then echo " file copied in current directory"
        if test $gz = 1 ; then echo " you have to gunzip file by 'gunzip -c file_name.gz > ./file_name'" ; fi
        sh toto2_$$.tmp >> get_data_GEOMAG.out
   else echo " error on file copy"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        rm toto1_$$.tmp toto2_$$.tmp toto3_$$.tmp toto4_$$.tmp
        exit 6
fi 

# change last access date
sed "s/^/touch -a /" toto1_$$.tmp > toto5_$$.tmp 
sh toto5_$$.tmp 2> /dev/null

rm toto*_$$.tmp 

echo "name of copied files are in get_data_GEOMAG.out"
   
# end of command
# --------------

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
echo " $appli36 : NORMAL TERMINATION - time exe= $diff s." >&2

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

