###############################################################################
# Copyright (c) 2011 - 2012 Trimble Navigation Ltd
# $Id: NoPiSG_Single_Day.py,v 1.4 2012/08/23 21:47:37 shankar Exp $
###############################################################################
#
# NoPiSG_Single_Day.py
#
# Module to compute single day statistics for NoPi processing results
#
###############################################################################

import sys
import os

from NoPiSG_Process import *
from NoPiSG_Utils import add_utils_dir_to_path
add_utils_dir_to_path()

from NoPiUT_Load_Summary import *
from NoPiUT_Common_Meas import construct_combo_name

IDX_INVALID = -1

###############################################################################
# Show a few lines of help if the command line parameters cannot be decoded
# correctly
###############################################################################

def show_stats_checker_help() :
  print 'Expected command line is:'
  print 'python NoPiSC_Main.py [-a] <path_1> <path_2>'
  print 'where: '
  print 'path_1: path to normative values file'
  print 'path_2: path to data directory'
  print '[-a]: optional feature to enable acquisition stats checker '

###############################################################################
# Parse command line
###############################################################################

if ( len( sys.argv ) != 3 and len( sys.argv ) != 4 ) :
  show_stats_checker_help()
  sys.exit()

if( len( sys.argv ) == 4 ) :
  do_acq_flag    = sys.argv[1]
  norm_vals_file = sys.argv[2]
  data_path      = sys.argv[3]

else :
  do_acq_flag    = "x"
  norm_vals_file = sys.argv[1]
  data_path      = sys.argv[2]

# Ensure the user input data path is terminated with a directory slash
data_path = data_path_valid( data_path )

###############################################################################
# Load summary file
###############################################################################

diffs_summ = cl_load_diffs_summ( data_path + 'diffs_summary.txt' )

# Acquisition Analysis will be done only if the user requests the
# optional process. Can be done only if the measurement rate was
# greater than the acquisition analysis duration

if do_acq_flag == "-a" :
  if ( diffs_summ.do_acq_analysis ) :
    print 'Evaluating Acquisition Analysis Statistics '
  else :
    print 'Cannot Evaluate Acquisition Analysis Statistics '
else :
  diffs_summ.do_acq_analysis = 0

###############################################################################
# Process the results for each combo
# Compute statistics for NoPi results and write them to a file
###############################################################################

norm_vals_parsed = parse_norm_vals( norm_vals_file )

# If specified file containing normative values does not exist, use
# sample mean/std deviation values instead

for combo in range( diffs_summ.num_combos ) :
    
  combo_name = construct_combo_name( diffs_summ, combo )
  combo_fname = data_path + 'diffs_combo_' + str( int( combo ) ) + '.mtb'
  
  if ( diffs_summ.combo[ combo ].combo_vld ) :
    print 'Processing file: ' + combo_fname
    load_combo( diffs_summ, combo_fname )        
    norm_val_idx = get_norm_val_idx( combo_name, norm_vals_parsed ) 
    if( norm_val_idx == IDX_INVALID and norm_vals_parsed ) :
      print 'Normative values unspecified for combo: ' + combo_name
      print 'Using sample mean/std. deviation'
  else :
    print 'Skipping empty file: ' + combo_fname
  
  # Process each of the three measurements types
  #   0 = D.D. Carrier phase
  #   1 = D.D. Pseudorange
  #   2 = S.D. CNo
    
  for mtype in range( 3 ) :
    if ( diffs_summ.combo[ combo ].combo_vld and 
         os.path.exists( combo_fname ) ) :
      process_data( diffs_summ, 
                    combo, 
                    mtype, 
                    data_path, 
                    'DATA', 
                    norm_val_idx  )
    elif ( diffs_summ.combo[ combo ].combo_vld ) :
      print 'Cannot locate diffs_combo_%d.mtb' % combo
      break
  
  # If acquisition analysis is enabled, use the acq_diffs_combo.mtb
  # files generated by NoPi_Data_Checker and process each of the three
  # measurement types
  
  if ( diffs_summ.do_acq_analysis and diffs_summ.combo[ combo ].combo_vld ) :
    acq_combo_fname = [ data_path 
                        + 'acq_diffs_combo_' 
                        + str( int( combo ) ) 
                        + '.mtb' ]    
    if ( os.path.exists( acq_combo_fname ) ) :
      load_combo( acq_combo_fname ) 
    else :
      print 'Cannot locate acq_diffs_combo_%d.mtb' % combo
  
  for mtype in range( 3 ) :
    if ( diffs_summ.do_acq_analysis and diffs_summ.combo[ combo ].combo_vld ) :
      if ( os.path.exists( acq_combo_fname ) ) :    
        process_data( diffs_summ, 
                      combo, 
                      mtype, 
                      data_path, 
                      'ACQ_DATA', 
                      norm_val_idx )
      else :
        break
