#!/bin/bash
#####################################################################
# 
# set_env.bash
#
# Purpose:
# This shell script collects satellite/antenna information, and
# set up the shell ENV variables so that the satellite/antenna parameters
# may be consistently used throughout the podgen package.
#
# Note: "xxx_bernid" refers to the satellite antenna 4-char ID defined in
# the BERNESE LEO satellite configuration file, e.g. UMD_LEO.STA
# This may be different from satname if a satellite has multiple POD
# antennas. 
#
# History:  
#       Created by: Ming Chen 2023/04/08
#                   ming.chen@noaa.gov
# 
# Usage: source satellite.info
#
#####################################################################

#LEOs

cosmic2_info(){
   # COSMIC2 satellite and antenna 
   local mission_tag=C2
   local satname satsvn bernid leoprn 
   declare -a satname ; declare -a satsvn 
   declare -a bernid ; declare -a leoprn

   # the satellites implemented in BERNESE SATELLITE configuration file
   satname=(C2E1 C2E2 C2E3 C2E4 C2E5 C2E6)
   satsvn=(001 002 003 004 005 006)
   antsvn=(01 02)
   bernid=(C211 C221 C231 C241 C251 C261)
   bernid+=(C212 C222 C232 C242 C252 C262)

   for i in "${!satsvn[@]}"; do 
      local id=$(printf "%02d" $((10#$(( i + 923 )) )))
      #local id=$(printf "%02d" $((10#$(( 922 )) )))
      leoprn+=($id)
   done

   export COSMIC2_ID=$mission_tag
   export COSMIC2_SATNAME="${satname[@]}"
   export COSMIC2_SATSVN="${satsvn[@]}"
   export COSMIC2_ANTSVN="${antsvn[@]}"
   export COSMIC2_BERNID="${bernid[@]}"
   export COSMIC2_LEOPRN="${leoprn[@]}"
}

spire_info(){

  # SPIRE satellite and antenna 
   local mission_tag=SP
   local satname satsvn bernid leoprn
   declare -a satname ; declare -a satsvn 
   declare -a bernid ; declare -a leoprn

   satsvn=(099 100 101 102 103 104 105 106 107 108 113)
   satsvn+=(115 116 117 118 119 120 121 122 123 124 125 126 127 128 129)
   satsvn+=(130 131 132 133 134 135 136 137 138 141 142 143 144 145 146 147 148 149)
   satsvn+=(150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169)
   
   # the satellites implemented in BERNESE SATELLITE configuration file
   satsvn=(099 102 103 104 113 115 117 118 119 120 122 124 125 126 127 128 129 132 133 134 135 141 143 148 149 150 162 163 166 167 168 169)
   satsvn=(126 173 174 196 197 199 201 202 206 225 233 236)
   satname=() ; antsvn=(00)
   for i in "${!satsvn[@]}"; do 
     local id=$(printf "%02d" $((10#$(( i + 923 )) )))
     #local id=$(printf "%02d" $((10#$(( 923)) )))
     leoprn+=($id)
     satname+=(S${satsvn[$i]})
   done
   bernid=($(echo "${satname[@]}"))

   export SPIRE_ID=$mission_tag
   export SPIRE_SATNAME="${satname[@]}"
   export SPIRE_SATSVN="${satsvn[@]}"
   export SPIRE_ANTSVN="${antsvn[@]}"
   export SPIRE_BERNID="${bernid[@]}"
   export SPIRE_LEOPRN="${leoprn[@]}"
}

cosmic1_info(){

   # COSMIC1 satellite and antenna 
   local mission_tag=C1
   local satname satsvn bernid leoprn
   declare -a satname ; declare -a satsvn 
   declare -a bernid ; declare -a leoprn
   satsvn=(001 002 003 004 005 006)
   antsvn=(01 02)
   satname=(C001 C002 C003 C004 C005 C006)
   bernid=(C111 C121 C131 C141 C151 C161)
   bernid+=(C12 C122 C132 C142 C152 C162)
 
   for i in "${!satsvn[@]}"; do 
      local id=$(printf "%02d" $((10#$(( i + 923 )) )))
      leoprn+=($id)
   done

   export COSMIC1_ID=$mission_tag
   export COSMIC1_SATNAME="${satname[@]}"
   export COSMIC1_SATSVN="${satsvn[@]}"
   export COSMIC1_ANTSVN="${antsvn[@]}"
   export COSMIC1_BERNID="${bernid[@]}"
   export COSMIC1_LEOPRN="${leoprn[@]}"
}

planetiq_info(){
   # planetiq satellite and antenna 
   local mission_tag=GN
   local satname satsvn bernid leoprn
   declare -a satname ; declare -a satsvn 
   declare -a bernid ; declare -a leoprn
 
   satsvn=(004 005)
   antsvn=(00)
   satname=(GN04 GN05)
   bernid=(GN04 GN05)

   for i in "${!satsvn[@]}"; do 
      local id=$(printf "%02d" $((10#$(( i + 923 )) )))
      leoprn+=($id)
  done
 
  export PLANETIQ_ID=$mission_tag
  export PLANETIQ_SATNAME="${satname[@]}"
  export PLANETIQ_SATSVN="${satsvn[@]}"
  export PLANETIQ_ANTSVN="${antsvn[@]}"
  export PLANETIQ_BERNID="${bernid[@]}"
  export PLANETIQ_LEOPRN=${leoprn[@]}

}

metopb_info(){
   # metopb satellite and antenna 
   local mission_tag=MT
   local satname satsvn bernid leoprn
   declare -a satname ; declare -a satsvn 
   declare -a bernid ; declare -a leoprn
 
   satsvn=(001)
   antsvn=(00)
   satname=(MTPB)
   bernid=(MTPB)
   leoprn=(923)

 
  export METOPB_ID=$mission_tag
  export METOPB_SATNAME="${satname[@]}"
  export METOPB_SATSVN="${satsvn[@]}"
  export METOPB_ANTSVN="${antsvn[@]}"
  export METOPB_BERNID="${bernid[@]}"
  export METOPB_LEOPRN=${leoprn[@]}

}
metopc_info(){
   # metopc satellite and antenna 
   local mission_tag=MT
   local satname satsvn bernid leoprn
   declare -a satname ; declare -a satsvn 
   declare -a bernid ; declare -a leoprn
 
   satsvn=(001)
   antsvn=(00)
   satname=(MTPC)
   bernid=(MTPC)
   leoprn=(924)

  export METOPC_ID=$mission_tag
  export METOPC_SATNAME="${satname[@]}"
  export METOPC_SATSVN="${satsvn[@]}"
  export METOPC_ANTSVN="${antsvn[@]}"
  export METOPC_BERNID="${bernid[@]}"
  export METOPC_LEOPRN=${leoprn[@]}

}

cosmic2_info ; cosmic1_info ; spire_info ; planetiq_info; metopb_info ; metopc_info

valid_mission(){
  local missions=(COSMIC2 SPIRE PLANETIQ COSMIC1 METOPB METOPC)
  local mission="$1"
  local match=0
  for m in "${missions[@]}"; do
    if [[ $m = "$mission" ]]; then
        match=1
        break  
    fi
  done
  
  if  [[ ${match} = 0 ]]; then
      echo  "unknown mission name  ... $mission"
      return 2
  else 
      return 0
  fi

}

sat_ant_info(){

  local mission satidx antidx 
  local m_satname m_satsvn m_antsvn m_bernid m_leoprn
  local satname satsvn antsvn bernid leoprn
  local missionid satinfo
  local n_sat n_ant
  local usage="$0 mission[osmic2|cosmic1|spire) satidx antidx "
  if [ $# -lt 2 ]; then
    echo "${usage} "
    exit 1
  fi

  
  mission="${1^^}"; satidx="$2"; antidx="${3:-1}"
  missionid=${mission}_ID    ; missionid=${!missionid}
  m_satname=${mission}_SATNAME ; m_satname=(${!m_satname})
  m_satsvn=${mission}_SATSVN ; m_satsvn=(${!m_satsvn})
  m_antsvn=${mission}_ANTSVN ; m_antsvn=(${!m_antsvn})
  m_bernid=${mission}_BERNID ; m_bernid=(${!m_bernid})
  m_leoprn=${mission}_LEOPRN ; m_leoprn=(${!m_leoprn})

  valid_mission "$mission"  
  if [ $? -gt 0 ]; then return 2 ; fi
  
  n_sat=${#m_satsvn[@]} ; n_ant=${#m_antsvn[@]}
  if  [[ $satidx -gt n_sat  || \
         $antidx -gt n_ant ]]; then
        error_msg1="---satidx or antidx error---  "
        error_msg2="$mission  "
        error_msg3="Valid satidx:  1 to ${n_sat}, "
        error_msg4="Valid antidx:  1 to ${n_ant} \n"
        error="${error_msg1}${error_msg2}${error_msg3}${error_msg4}"
        echo -e "$error"
        return 2
  fi
  satidx=$((satidx-1)) ; antidx=$((antidx-1))
  satsvn=${m_satsvn[$satidx]}
  satname=${m_satname[$satidx]}
  antsvn=${m_antsvn[$antidx]}
  #bernid=(${m_bernid[$antidx]})
  ss=$((satidx + n_sat * antidx))
  bernid=(${m_bernid[$ss]})
  leoprn=(${m_leoprn[$satidx]})
  
  satinfo=($satsvn $satname $antsvn ${bernid} ${missionid} ${leoprn})
  echo "${satinfo[@]}"
  return 0

}

sat_ant_nums(){

  local mission 
  local usage="$0 mission[osmic2|cosmic1|spire " 
  if [ $# -lt 1 ]; then
    echo "${usage} "
    exit 1
  fi
  mission="${1^^}" 
  m_satsvn=${mission}_SATSVN ; m_satsvn=(${!m_satsvn})
  m_antsvn=${mission}_ANTSVN ; m_antsvn=(${!m_antsvn})

  valid_mission "$mission"
  if [ $? -eq 0 ]; then 
    echo "${#m_satsvn[@]} ${#m_antsvn[@]}"
  else
   echo "0 0"
  fi
}
