###############################################################################
# Copyright (c) 2012 Trimble Navigation Ltd
# $Id: NoPiSD_Load_Summary.py,v 1.1 2012/02/04 01:08:47 shankar Exp $
###############################################################################
#
# NoPiSD_Load_Summary.py
#
# Class used to contain NoPi Stats Concatenation output combo information This
# is all read and decoded from the summary file output by NoPi Stats_Concat. It
# is therefore dependent on the summary file generation code in NoPi
#
###############################################################################

import sys
import os
import string

###############################################################################
# Sub-class that contains the information that describes each signal combo
###############################################################################

class cl_concat_summ_data :

  #############################################################################
  # Convert a single combo summary data line
  #############################################################################
  
  def __init__( self, fptr, dtype = '' ) :

    # Read the combo line from the file
    tmp_line = fptr.readline()
    split_tmp_line     = string.split( tmp_line, ',' )

    if ( dtype == 'BASELINE' ) :  
      self.baseline_name = split_tmp_line[0]
      self.baseline_dir  = string.strip( split_tmp_line[1] )
    elif ( dtype == 'COMBO' ) :
      self.combo_name    = split_tmp_line[0]
      self.resolve_sdiff = int( split_tmp_line[1] )
  
###############################################################################
# Top-level class that contains all the summary information from NoPi Stats
# Concat 
# Contains a sub-class for each signal combo
###############################################################################

class cl_load_concat_summ :

  #############################################################################
  # Open the summary file and parse the contents
  #############################################################################
  
  def __init__( self, fname ) :
    
    # Parse the summary file only if it exists
    if ( os.access( fname, os.R_OK ) ) :  

      self.combo     = []
      self.baseline  = []
    
      self.fname = fname
      fptr = open( fname, 'rt' )
    
      tmp_line            = fptr.readline()
      self.start_date     = tmp_line[ 0 : len( tmp_line ) - 1 ]
      tmp_line            = fptr.readline()
      self.end_date       = tmp_line[ 0 : len( tmp_line ) - 1 ]

      # Parse each baseline listed in the summary file
      self.num_baselines  = int( fptr.readline() )
      for x in range( self.num_baselines ) :
        self.baseline.append( cl_concat_summ_data( fptr, dtype = 'BASELINE' ) )
      
      # Parse each combo listed in the summary file
      self.num_combos  = int( fptr.readline() )
      for x in range( self.num_combos ) :
        self.combo.append( cl_concat_summ_data( fptr, dtype = 'COMBO' ) )  

      fptr.close()
  
    # If summary file cannot be found, flag an error and terminate
    # execution
    else :
      print 'Missing concat summary file: %s' % fname
      print 'Terminating execution'
      sys.exit()

