#!/bin/bash
#####################################################################
# 
# s09_archive_rt_pod.bach
#
# Purpose:
# This shell script is used to save the Bernese leopod outputs to
# local archive.
#
# Each archive may be tagged to separate different Bernese 
# experiments (e.g., different configurations) for a same specific case.
# 
#
# History:  
#       Created by: Ming Chen 2023/04/21
#                   ming.chen@noaa.gov
# 
# Usage: s09_archive_rt_pod.bach  Args
#
# Args:  mission[cosmic2] satidx[1-6]  antidx[1-2]
#        start-date[yyyyddd|yyyymndd] [end-date] archive 
#
#####################################################################

if [ -z "${podgen_home}" ]; then 
   echo "env is not set up, source the set_env.bash file first .."
   exit
fi
source ${utils_home}/gpsdate.bash
source ${podgen_home}/satellite.info

usage="Usage: $0  -m [cod|com] -a [archive] 
  mission[cosmic2|cosmic1] satidx antidx  starting-date(yyyymndd HHMM) [ending-date]"

PARSED_ARGUMENTS=$(getopt -o m:a:h: -- "$@")
VALID_ARGUMENTS=$?
if [ "$VALID_ARGUMENTS" != "0" ]; then
  echo -e "$usage" ; exit 2
fi
eval set -- "$PARSED_ARGUMENTS"

codmgex='cod'; archive='LEOPOD'
while :
do
  case "$1" in
    -m ) codmgex="$2" ; shift 2 ;;
    -a ) archive="$2" ; shift 2 ;;
    --) shift; break ;;
    *) echo "Unexpected option: $1 "
       echo "$usage" ; exit 2 ;;
  esac
done
echo "$codmgex $archive"

args=("$@")
if [ ${#args[@]} -lt 4 ]; then echo -e "$usage" ; exit 2; fi

if [ $# -lt 4 ];  then
    echo "$0 $usage "
    exit
fi

mission="$1" ; satidx="$2" ; antidx="$3" 

# starting and ending dates
stime="$4" ; etime="${5:-$stime}"

sdate=`date -d "$stime" +%Y%m%d` ; edate=`date -d "$etime" +%Y%m%d`
echo "${4} $sdate $edate"
HM=`date -d "$etime" +%H%M` 

etime2=`date -d "$etime +60 minutes" +%Y%m%dT%H%M%S`
stime=`date -d "$stime" +%Y%m%dT%H%M%S` ; etime=`date -d "$etime" +%Y%m%dT%H%M%S`
t1=`date -d "$t2 -${sliding_interval} minutes" +"%Y%m%d %H%M"`
echo "starting-time: $stime, ending-time: $etime"
 
echo $mission $satidx $antidx $sdate $edate
satinfo=( $(sat_ant_info $mission $satidx $antidx) )
if [ "$?" -ne 0 ]; then 
   echo -e "${satinfo[@]}"
   exit 2 
fi
echo "satinfo ${satinfo[@]}"
satsvn=${satinfo[0]} ; satname=${satinfo[1]} ; antsvn=${satinfo[2]}
bern_antid=${satinfo[3]}; 

echo "SatelliteSVN:$satsvn  SatelliteName:$satname  AntennaSVN:$antsvn "
echo "AntennaID in Bernese LEO: $bern_antid "

#Bernese env
echo "source ${HOME}/LOADGPS.setvar"
source ${HOME}/LOADGPS.setvar
if [ ! -d ${S} ]; then exit 2 ; fi

#bern_leopod=${S}/$archive
bern_leopod=${S}/LEOPOD
bern_gnsspod=${S}/GNSS
bern_leopod=${P}/${bern_antid}
arch_home=${archive_home}/$mission/$archive
arch_leoOrb_home=${arch_home}/leoOrb
arch_leoClk_home=${arch_home}/leoClk
arch_leoAtt_home=${arch_home}/leoAtt
arch_podRnx_home=${arch_home}/podRnx
arch_gnss_home=${arch_home}/gnssOrb

echo "Bernese SaveDisk: ${bern_leopod} "
echo "archive home: ${arch_home} "
echo "archive home: ${arch_leoOrb} "
echo "starting-date: $sdate ending-date: $edate "

copy='cp ' # or "mv "
# loop over dates
echo -e "\nCopying data from Bernese SaveDisk to the archive"
cdate=$edate
while [ $cdate -le $edate ];  do
    gpswd=$(date2gpsw $cdate)
    gweek=${gpswd:0:4} ; gday=${gpswd:4:1}
    echo "cdate, GWEEK, GDAY: " $cdate $gweek $gday $gpswd $edate

    yyyy=`date -u -d "$cdate " +%Y`
    yy=`date -u -d "$cdate " +%y`
    mm=`date -u -d "$cdate " +%m`
    dd=`date -u -d "$cdate " +%d`	
    doy=`date -u -d "$cdate" +%j`
    
    campname=${bern_antid}${yy}${doy}${HM}
    bern_leopod=${P}/${campname}
    datapool_subdir=${D}/${codmgex^^}${satname}/${yyyy}${doy}
    #stime=`date -d "$ymd $HH$MM" +%Y%m%dT%H%M00`
    #etime=`date -d "$ymd $HH$MM + 2 hours" +%Y%m%dT%H%M00`



    archive_leoOrb(){
      bern_leoOrb=${bern_leopod}/ORB
      bern_out=${bern_leopod}/OUT
      arch_leoOrb=${arch_leoOrb_home}/${yyyy}.${doy}
 
      fin_orb=FIN_${yyyy}${doy}0_${bern_antid}.PRE
      fin_apr=APR_${yyyy}${doy}0_${bern_antid}.PRE
      #fin_orb_arch=${bern_antid}_${stime}_${etime}_${bern_antid}.FIN
      fin_orb_arch=leoOrb_${etime}_${bern_antid}.FIN
      fin_orb_apr=leoOrb_${etime}_${bern_antid}.APR
      fin_orb_sp3=leoOrb_${etime}_${bern_antid}.SP3
      kin_orb=KIN_${yyyy}${doy}0_${bern_antid}.PRE
      #kin_orb_arch=${bern_antid}_${stime}_${etime}_${bern_antid}.KIN
      kin_orb_arch=leoOrb_${etime}_${bern_antid}.KIN
      kin_mp0=MP0_${yyyy}${doy}0_${bern_antid}.PRE
      if [ ! -d ${arch_leoOrb} ]; then mkdir -p ${arch_leoOrb} ; fi
      echo "Bernese leoOrb Path: " ${bern_leoOrb}

      echo "leoOrb Data Archive Path: " ${arch_leoOrb}

      if [ -f ${bern_leoOrb}/${kin_orb} ] ; then
         cmd="$(cp -f ${bern_leoOrb}/${kin_orb}  ${arch_leoOrb}/${kin_orb_arch})"
         echo $cmd ; $cmd
         KINSTATUS="KIN:KIN"
      elif [ -f ${bern_leoOrb}/${kin_mp0} ] ; then
         cmd="$(cp -f ${bern_leoOrb}/${kin_mp0}  ${arch_leoOrb}/${kin_orb_arch})"
         echo $cmd ; $cmd
         KINSTATUS="KIN:MP0"
      fi
      if [ -f ${bern_leoOrb}/${fin_orb} ] ; then
         cmd="$(cp -f ${bern_leoOrb}/${fin_orb}  ${arch_leoOrb}/${fin_orb_arch})"
         echo $cmd ; $cmd
         FINSTATUS="FIN:FIN"
      elif [ -f ${bern_leoOrb}/${fin_apr} ] ; then
         cmd="$(cp -f ${bern_leoOrb}/${fin_apr}  ${arch_leoOrb}/${fin_orb_arch})"
         echo $cmd ; $cmd
         FINSTATUS="FIN:APR"
      fi
      
      bern_leoClk=${bern_leopod}/OUT
      arch_leoClk=${arch_leoClk_home}/${yyyy}.${doy}
      leoclk_spp=SPP_${yyyy}${doy}0_${bern_antid}.CLK
      leoclk=KIN_${yyyy}${doy}0_${bern_antid}.CLK
      #fclka=${bern_antid}_${stime}_${etime}.CLK
      fclka=leoClk_${etime}_${bern_antid}.CLK
      if [ ! -d ${arch_leoClk} ]; then mkdir -p ${arch_leoClk} ; fi
      echo "Bernese leoOrb Path: " ${bern_leoClk}
      echo "leoOrb Data Archive Path: " ${arch_leoClk}

      if [ -f ${bern_leoClk}/$leoclk ] ; then
	 cp ${bern_leoClk}/$leoclk ${arch_leoClk}/$fclka
         CLKSTATUS="CLK:CLK"
      elif [ -f ${bern_leoClk}/${leoclk_spp} ] ; then
	 cp ${bern_leoClk}/${leoclk_spp} ${arch_leoClk}/$fclka
         CLKSTATUS="CLK:SPP"

      fi
      LEOSTATUS="${KINSTATUS} ${FINSTATUS} ${CLKSTATUS}" 
      if [ -f ${arch_leoOrb}/${fin_orb_arch} ] && [ -f ${arch_leoClk}/$fclka ]; then
 	 #cmd="python ${utils_home}/leopod_sp3.py ${arch_leoOrb}/${fin_orb_arch} ${arch_leoClk}/$fclka  ${arch_leoOrb}/${fin_orb_sp3} -start ${stime} -end ${etime2} -status \"${LEOSTATUS}\""
 	 cmd="python ${utils_home}/leopod_sp3.py ${arch_leoOrb}/${fin_orb_arch} ${arch_leoClk}/$fclka  ${arch_leoOrb}/${fin_orb_sp3} \
	      -start ${stime} -end ${etime2}  -s '${LEOSTATUS}'"
         echo $cmd 
	 python ${utils_home}/leopod_sp3.py ${arch_leoOrb}/${fin_orb_arch} ${arch_leoClk}/$fclka  ${arch_leoOrb}/${fin_orb_sp3} -start ${stime} -end ${etime2}  -status "${LEOSTATUS}"

      fi

   }
    
    archive_leoOrb
    
    
 
   arch_link(){
    ftypes=(leoAtt podRnx ATTIT  RINEX)
    inpath=${D}/${mission^^}/ATTIT
    inpath=${datapool_subdir}
    outpath=${arch_leoAtt_home}/${yyyy}.${doy}
    echo "outpath $outpath"
    if [ ! -d $outpath ]; then  mkdir -p ${outpath} ;fi
    attfile=${bern_antid}_${etime}.att
    attlink=${satname^^}${yy}${doy}0_${HM}.ATT
    #echo "cp ${inpath}/${attlink} ${outpath}/${attfile}"
    #cp ${inpath}/${attlink} ${outpath}/${attfile}
    cmd="python ${utils_home}/extract_att.py ${inpath}/${attlink}  ${outpath}/${attfile} -start ${stime} -end ${etime2} "
    echo $cmd ; $cmd


    inpath=${D}/${mission^^}/RINEX
    inpath=${datapool_subdir}
    outpath=${arch_podRnx_home}/${yyyy}.${doy}
    echo "outpath $outpath"
    if [ ! -d $outpath ]; then  mkdir -p ${outpath} ;fi
    rnxfile=${bern_antid}_${etime}.rnx
    rnxlink=${satname^^}${doy}0.${yy}O_${HM}_A$antsvn
    #echo "cp ${inpath}/${rnxlink} ${outpath}/${rnxfile}"
    #cp ${inpath}/${rnxlink} ${outpath}/${rnxfile}
    cmd="python ${utils_home}/extract_rnx2.py  ${inpath}/${rnxlink}  ${outpath}/${rnxfile} -start ${stime} -end ${etime2} "
    echo $cmd ; $cmd
   }
   arch_link

   rm -fr ${P}/${campname}
   rm -fr ${T}/BPE_${campname}_*

   cdate=`date -d "$cdate +1 day" +%Y%m%d`   
done




