###############################################################################
# Copyright (c) 2010-2025 Trimble Inc.
# $Id: NoPiDI_Utils.py,v 1.15 2024/06/18 21:35:49 acartmel Exp $
###############################################################################
#
# NoPiDI_Utils.py
#
# Miscellaneous collection of basic utility functions
#
###############################################################################

import os
import sys

###############################################################################
# Function to include NoPi Utils directory in python's module search path
###############################################################################

def add_utils_dir_to_path() :

  file_path = os.path.abspath(sys.argv[0])
  file_dir = os.path.dirname(file_path)
  top_dir = os.path.dirname(file_dir)
  new_dir = os.path.join(top_dir, 'NoPi_Utils')
  sys.path.insert(0, new_dir)

  return()


###############################################################################
# Additional function imports
# Need to wait for the add_utils_dir_to_path() function to be defined before
# using
###############################################################################

add_utils_dir_to_path()

from numpy import where

from NoPiUT_Common_Meas import convert_sat_type, convert_sub_type
from NoPiUT_Const import *


###############################################################################
# Matplotlib deprecated find() in version 2.2
# Provide a replacement
###############################################################################
def find( x ) :
  return where(x)[0]


###############################################################################
# Create a summary file of the overall statistics for each combo
# This should be the same data that's loaded into the web page
###############################################################################

def create_stats_summary( diffs_summ, meas_stats, doppler_data ) :
  f_summ = open( 'NoPiDI_Stats_Summary.txt', 'wt' )

  f_summ.write( '%% Combo# ComboType Valid ' )
  f_summ.write( 'DDCa_#epochs DDCa_min DDCa_max DDCa_mean DDCa_std DDCa_mav ' )
  f_summ.write( 'DDCd_#epochs DDCd_min DDCd_max DDCd_mean DDCd_std DDCd_mav ' )
  if ( doppler_data ) :
    f_summ.write( 'DDDp_#epochs DDDp_min DDDp_max DDDp_mean DDDp_std DDDp_mav ' )
  f_summ.write( 'SDCNo_#epochs SDCNo_min SDCNo_max SDCNo_mean SDCNo_std SDCNo_mav ' )
  f_summ.write( 'CSlips_All CSlips_Elev Epochs_Elev ' )
  f_summ.write( '\n' )

  for combo in range( diffs_summ.num_combos ) :
    cname = ( convert_sat_type( diffs_summ.combo[combo].ref_sat_type )
            + '_'
            + convert_sub_type( diffs_summ.combo[combo].ref_sub_type )
            )

    # Only append the second signal type if it's different from the first
    if ( ( diffs_summ.combo[combo].ref_sat_type
        != diffs_summ.combo[combo].tst_sat_type
         )
       | ( diffs_summ.combo[combo].ref_sub_type
        != diffs_summ.combo[combo].tst_sub_type
         )
       ) :
      cname = ( cname
              + '/'
              + convert_sat_type( diffs_summ.combo[combo].tst_sat_type )
              + '_'
              + convert_sub_type( diffs_summ.combo[combo].tst_sub_type )
              )

    f_summ.write( str( int( combo ) ) + ' ' )
    f_summ.write( str( cname ) + ' ' )
    f_summ.write( str( int( diffs_summ.combo[ combo ].combo_vld ) ) + ' ' )

    # Start index for the statistics
    idx = cNoPiConst.NUM_MEAS_TYPE * combo

    for meas in range( cNoPiConst.NUM_MEAS_TYPE ) :
      # Skip the D.D. Doppler if it's not been computed
      if ( doppler_data == False and meas == cNoPiConst.MEAS_TYPE_DD_DOPP ) :
        continue

      if ( meas_stats[idx + meas].sv[0].vld_epochs > 0 ) :
        vld_min  = meas_stats[idx + meas].sv[0].vld_min
        vld_max  = meas_stats[idx + meas].sv[0].vld_max
        vld_mean = meas_stats[idx + meas].sv[0].vld_mean
        vld_std  = meas_stats[idx + meas].sv[0].vld_std
        vld_mav  = meas_stats[idx + meas].sv[0].vld_mav
      else :
        vld_min  = 0.0
        vld_max  = 0.0
        vld_mean = 0.0
        vld_std  = 0.0
        vld_mav  = 0.0

      f_summ.write( str( int( meas_stats[idx + meas].sv[0].vld_epochs ) )
                  + ' '
                  )
      f_summ.write( str( vld_min )  + ' ' )
      f_summ.write( str( vld_max )  + ' ' )
      f_summ.write( str( vld_mean ) + ' ' )
      f_summ.write( str( vld_std )  + ' ' )
      f_summ.write( str( vld_mav )  + ' ' )

    meas = cNoPiConst.MEAS_TYPE_DD_CARR
    f_summ.write( str(meas_stats[idx + meas].sv[0].all_cyc_slips) + ' ' )
    f_summ.write( str(meas_stats[idx + meas].sv[0].elv_cyc_slips) + ' ' )
    f_summ.write( str(meas_stats[idx + meas].sv[0].elv_epochs) + ' ' )

    f_summ.write( '\n' )

  f_summ.close()
