#!/bin/bash
#####################################################################
# 
# s09_mk_leosp3.bash 
#
# Purpose:
# This shell script is used to combine leoorb and leoclk into
# leo sp3.
#
#
# History:  
#       Created by: Ming Chen 2025/12/02/
#                   ming.chen@noaa.gov
# 
# Usage: s09_mk_leosp3.bash  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(yyyyddd or yyyymndd) [ending-date]"

PARSED_ARGUMENTS=$(getopt -o m:a: -- "$@")
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" 
sdate=$4 ; edate=${5:-$sdate}
sdate=$(mkdate $sdate) ; edate=$(mkdate $edate)
 
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 "

arch_home=${archive_home}/$mission/$archive
arch_leoOrb_home=${arch_home}/leoOrb
arch_leoClk_home=${arch_home}/leoClk

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

# loop over dates
echo -e "\nCopying data from Bernese SaveDisk to the archive"
cdate=$sdate
mypython=${MYPYTHON/:-python}
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`

    mk_leosp3(){
      arch_leoOrb=${arch_leoOrb_home}/${yyyy}.${doy}
      arch_leoClk=${arch_leoClk_home}/${yyyy}.${doy}
      fin_orb=FIN_${yyyy}${doy}0_${bern_antid}.PRE
      kin_orb=KIN_${yyyy}${doy}0_${bern_antid}.PRE
      fin_sp3=FIN_${yyyy}${doy}0_${bern_antid}.SP3

      orbit_file="${arch_leoOrb}/${fin_orb}"
      clock_file="${arch_leoClk}/${bern_antid}_${yyyy}${doy}0.CLK"
      sp3_file="${arch_leoOrb}/${fin_sp3}"
      ${mypython} ${utils_home}/leosp3.py ${orbit_file} ${clock_file} ${sp3_file}
   }
    
     

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




