###############################################################################
# Copyright (c) 2011 - 2012 Trimble Navigation Ltd
# $Id: NoPiSG_Multi_Day.py,v 1.5 2012/08/30 00:23:09 shankar Exp $
###############################################################################
#
# NoPiSG_Multi_Day.py
#
# This top level module loops over a range of dates and invokes
# NoPiSG_Single_Day.py to compute per day statistics. Either a user
# provided set of baselines or a default set of baselines are
# processed.
###############################################################################

import sys
import os
import glob
from dateutil.rrule import rrule, DAILY

from NoPiSG_Utils import add_utils_dir_to_path
add_utils_dir_to_path()

from NoPiUT_Common_Meas import *

###############################################################################
# Process data for over the range start_date - end_date for relevant
# baselines. The baselines are parsed from either a user specified
# file or from NoPiSG_Baselines.cfg
###############################################################################

# Path to data directory is a required argument
if ( len( sys.argv ) < 2 ) :
  show_multi_day_help()

# Ensure data directory name includes appropriate forward/back slash and
# that the directory exists
data_path = data_path_valid( sys.argv[1] )

# Parse multi day stats generator command line options
user_opts    = parse_multi_day_options()

# Parse default/user specified baseline file
[baseline_name, baseline_dir, norm_vals_file]=parse_baselines_file( user_opts )

# Determine processing start/end dates
[start_date, end_date]   = get_date_range( user_opts ) 

# Generate statistics for NoPi results for each baseline over the
# period: start_date to end_date
for baseline,dir_name,norm_file in zip( baseline_name,baseline_dir,norm_vals_file ) :
  # Loop from start_date to end_date
  for dt in rrule(DAILY, dtstart=start_date, until=end_date):

    [cur_date, cur_day_path] = get_cur_day_path( dt, data_path, baseline,
                                                 dir_name, disp_date = True )
    if ( os.path.isdir( cur_day_path ) ) :
      # If stats_combo files do not exist for a particular date, invoke
      # NoPiSG_Single_Day.py to generate stats_combo files. To do so,
      # diffs_summary.txt file should exist in that directory.
      #
      # Regenerates statistics if the user requests reprocessing the data
      if ( ( len ( glob.glob( cur_day_path + 'stats_combo*.mtb' ) ) == 0 or 
             user_opts.reprocess_data ) and
           os.path.isfile( cur_day_path + 'diffs_summary.txt' ) ) :
        if ( len( glob.glob( cur_day_path + 'diffs_combo*.bz2' ) ) > 0 and 
             os.path.isfile( cur_day_path + 'diffs_summary.txt' ) ) :
          print 'Uncompressing diffs_combo files'
          os.system( 'bunzip2 -kf ' + cur_day_path + 'diffs_combo*.bz2' )
        print 'Calling Single Day NoPi Stats Generator'
        os.system( ( 'python ../NoPi_Stats_Generator/NoPiSG_Single_Day.py ' 
                     + norm_file 
                     + ' ' 
                     + cur_day_path ) )
        combo_list = glob.glob( cur_day_path + 'diffs_combo*.mtb' )
        if ( len( combo_list ) > 0 ) :
          for idx in range( 0, len( combo_list ) ) :
            os.remove( combo_list[ idx ] )
