#!/usr/bin/env python
import os, sys, glob, subprocess, shutil
from datetime import datetime, timedelta
import re
import numpy as np

__authors__ = ["Ming Chen"]
__license__ = "Public Domain"
__maintainer__ = "Ming Chen"
__email__ = "ming.chen@noaa.gov"
__date__  = "Mar 24, 2023"

'''
#####################################################################
# 
# podgen_config.py
#
# Purpose:
# This python script is used to set and get the LEO satellite/antenna 
# name and svn information.
##
# History:  
#       Created by: Ming Chen 2023/03/24
#                   ming.chen@noaa.gov
# 
# Usage: import podgen_config  
#
#
#####################################################################

'''

env_util_exists = 'utils_home' in os.environ
env_umd_exists  = 'umd_data'  in os.environ
env_ucar_exists = 'ucar_data' in os.environ
env_tmp_exists  = 'tmp_data'  in os.environ

# COSMIC2 satellite and antenna 
def env_paths():
    home = os.environ['HOME']
    paths={'ucar': os.path.join(home,'POD_DATA','ucar'),
           'umd':  os.path.join(home,'POD_DATA','umd'),
           'aiub': os.path.join(home,'POD_DATA','aiub'),
           'nasa': os.path.join(home,'POD_DATA','nasa'),
           'tmp':  os.path.join(home,'POD_DATA','TMP'),
           'util': os.path.join(home,'Utility'),
	  }
    if env_util_exists: 
       paths['util']=os.environ['utils_home']
    else:
       os.environ['utils_home'] = paths['util']
    if env_umd_exists:  
       paths['umd']=os.environ['umd_data']
    else:
       os.environ['umd_data'] = paths['umd']
    if env_ucar_exists: 
       paths['ucar']=os.environ['ucar_data'] 
    else:
       os.environ['ucar_data'] = paths['ucar']
    if env_tmp_exists:  
       paths['tmp']=os.environ['tmp_data'] 
    else:
       os.environ['tmp_data'] = paths['tmp']
    return paths

def sat_ant_info(mission, satidx=None, antidx=None):
    antidx = antidx
    if mission.lower() == "cosmic2":
       satinfo = cosmic2_info(satidx, antidx)
    elif mission.lower() == "spire":
       satinfo = spire_info(satidx, antidx)
    elif mission.lower() == "planetiq":
       satinfo = planetiq_info(satidx, antidx)
    elif mission.lower() == "cosmic1":
       satinfo = cosmic1_info(satidx, antidx)
    else:
       print('mission must be cosmic1 or cosmic2 ....')
       return None
    return satinfo
    
# COSMIC2 satellite and antenna 
def cosmic2_info(satidx=None, antidx=None):
    satsvn  = ['001','002','003','004','005','006']
    antsvn  = ['01','02']
    satname = ['C2E1','C2E2','C2E3','C2E4','C2E5','C2E6']
    bernid  = [['C211','C221','C231','C241','C251','C261'],
               ['C212','C222','C232','C242','C252','C262']]
    bernid=np.array(bernid).flatten()
    leonum=np.arange(len(satsvn))+923
    leoprns=np.array(list(map(str,leonum)))

    if('COSMIC2_SATNAME' in os.environ) and \
      ('COSMIC2_SATSVN'  in os.environ) and \
      ('COSMIC2_ANTSVN'  in os.environ) and \
      ('COSMIC2_LEOPRN'  in os.environ) and \
      ('COSMIC2_BERNID'  in os.environ) : 
       satname = (os.environ['COSMIC2_SATNAME']).split()
       satsvn  = (os.environ['COSMIC2_SATSVN']).split()
       antsvn  = (os.environ['COSMIC2_ANTSVN']).split()
       bernid  = (os.environ['COSMIC2_BERNID']).split()
       leoprns  = (os.environ['COSMIC2_LEOPRN']).split()
       
    bernids = [] 
    n_sat = len(satsvn) ; n_ant = len(antsvn)
    for j in range(n_ant):
        bernids.append( \
	[ bernid[i+j*n_sat] for i in range(n_sat)])
 
    if satidx is None and antidx is None:
       satinfo={'nsats':len(satsvn) , 'nant':len(antsvn)}  
       return (satinfo)

    if satidx > n_sat:
       print("error: satidx {} larger than valid range 1-{}".format(satidx,n_sat))
       return None
    sname = satname[satidx-1] ; ssvn= satsvn[satidx-1] ; leoprn = leoprns[satidx-1]
    attsvn = None ; bernid = None  
    if antidx != None and antidx <= n_ant:
       attsvn = antsvn[antidx-1]
       bernid = bernids[antidx-1][satidx-1]
        
    satinfo={'satsvn':ssvn , 'antsvn':attsvn, 
             'satname':sname,'bernid':bernid,
             'leoprn':leoprn,
	     }  
    return (satinfo)
    
# SPIRE satellite and antenna 
# Note: not verified or tested 
def spire_info(satidx, antidx=None):
    satsvn = ['099','100','101','102','103','104','105',
              '106','107','108','115','116','117','118',
	      '119','120','122','123','124','128','129']
    antsvn  = ['00']
    satname = [ 'S'+ i for i in satsvn ]
    bernid  = [ 'S'+ i for i in satsvn ]
    leonum  = np.arange(len(satsvn))+923
    leoprns = np.array(list(map(str,leonum)))

    if('SPIRE_SATNAME' in os.environ and \
       'SPIRE_SATSVN'  in os.environ and \
       'SPIRE_ANTSVN'  in os.environ and \
       'SPIRE_LEOPRN'  in os.environ and \
       'SPIRE_BERNID'  in os.environ ): 
       satname = (os.environ['SPIRE_SATNAME']).split()
       satsvn  = (os.environ['SPIRE_SATSVN']).split()
       antsvn  = (os.environ['SPIRE_ANTSVN']).split()
       bernid  = (os.environ['SPIRE_BERNID']).split()
       leoprns = (os.environ['SPIRE_LEOPRN']).split()
    bernids = [bernid]  
    n_sat = len(satsvn) ; n_ant = len(antsvn)
    for j in range(n_ant):
        bernids.append( \
	[ bernid[i+j*n_sat] for i in range(n_sat)])
 
    if satidx > n_sat:
       print("error: satidx {} larger than valid range 1-{}".format(satidx,n_sat))
       return None
    sname = satname[satidx-1] ; ssvn = satsvn[satidx-1] 
    attsvn = None ; bernid = None ; leoprn=leoprns[satidx-1]
    if antidx != None and antidx <= n_ant:
       attsvn = antsvn[antidx-1]
       bernid = bernids[antidx-1][satidx-1]

    satinfo={'satsvn':ssvn , 'antsvn':attsvn,
             'satname':sname,'bernid':bernid,
             'leoprn':leoprn,
	     }  
    return (satinfo)

#COSMIC1 satellite and antenna 
# Note: not verified or tested 
def cosmic1_info(satidx, antidx=None):
    satsvn  = ['001','002','003','004','005','006']
    antsvn  = ['01','02']
    satname = ['C001','C002','C003','C004','C005','C006']
    bernid  = [['C111','C121','C131','C141','C151','C161'],
               ['C112','C122','C132','C142','C152','C162']]
    bernid  = np.array(bernid).flatten()
    leonum  = np.arange(len(bernid))+923
    leoprns = np.array(map(str,leonum))

    if('COSMIC1_SATNAME' in os.environ and \
       'COSMIC1_SATSVN'  in os.environ and \
       'COSMIC1_ANTSVN'  in os.environ and \
       'COSMIC1_LEOPRN'  in os.environ and \
       'COSMIC1_BERNID'  in os.environ) : 
       satname = (os.environ['COSMIC1_SATNAME']).split()
       satsvn  = (os.environ['COSMIC1_SATSVN']).split()
       antsvn  = (os.environ['COSMIC1_ANTSVN']).split()
       bernid  = (os.environ['COSMIC1_BERNID']).split()
       leoprns = (os.environ['COSMIC1_LEOPRN']).split()

    bernids = [] 
    n_sat = len(satsvn) ; n_ant = len(antsvn)
    for j in range(n_ant):
        bernids.append( \
	[ bernid[i+j*n_sat] for i in range(n_sat)])

    if satidx > n_sat:
       print("error: satidx {} larger than valid range 1-{}".format(satidx,n_sat))
       return None
    sname = satname[satidx-1] ; ssvn = satsvn[satidx-1] 
    attsvn = None ; bernid = None; leoprn=leoprns[satidx-1]
    if antidx != None and antidx <= n_ant:
       attsvn = antsvn[antidx-1]
       bernid = bernids[antidx-1][satidx-1]

    satinfo={'satsvn':ssvn , 'antsvn':attsvn, 
             'satname':sname,'bernid':bernid,
             'leoprn':leoprn,
	     }  
    return (satinfo)

# PLANETIQ satellite and antenna 
def planetiq_info(satidx, antidx=None):
    satsvn  = ['002','003']
    antsvn  = ['00','01']
    satname = [ 'G'+ i for i in satsvn ]
    bernid  = [['G021','G031'],
               ['G022','C032']]
    bernid  = np.array(bernid).flatten()
    leonum  = np.arange(len(bernid))+923
    leoprns = np.array(map(str,leonum))

    if('PLANETIQ_SATNAME' in os.environ) and \
      ('PLANETIQ_SATSVN'  in os.environ) and \
      ('PLANETIQ_ANTSVN'  in os.environ) and \
      ('PLANETIQ_LEOPRN'  in os.environ) and \
      ('PLANETIQ_BERNID'  in os.environ) : 
       satname = (os.environ['PLANETIQ_SATNAME']).split()
       satsvn  = (os.environ['PLANETIQ_SATSVN']).split()
       antsvn  = (os.environ['PLANETIQ_ANTSVN']).split()
       bernid  = (os.environ['PLANETIQ_BERNID']).split()
       leoprns = (os.environ['PLANETIQ_LEOPRN']).split()

    bernids = []
    n_sat = len(satsvn) ; n_ant = len(antsvn)
    for j in range(n_ant):
        bernids.append( \
	[ bernid[i+j*n_sat] for i in range(n_sat)])

    if satidx > n_sat:
       print("error: satidx {} larger than valid range 1-{}".format(satidx,n_sat))
       return None
    sname = satname[satidx-1] ; ssvn = satsvn[satidx-1] 
    attsvn = None ; bernid = None; leoprn=leoprns[satidx-1]
    if antidx != None and antidx <= n_ant:
       attsvn = antsvn[antidx-1]
       bernid = bernids[antidx-1][satidx-1]

    satinfo={'satsvn':ssvn , 'antsvn':attsvn, 
             'satname':sname,'bernid':bernid,
             'leoprn':leoprn,
	     }  
    return (satinfo)
    

if __name__ == "__main__":
   import argparse
