#!/usr/bin/env python
# -*- python -*-
# FILE:
# negcheck
#
# DESCRIPTION:
# Remove header files from Adaytum position output.
#
# NOTES:
#
# HISTORY:
# Copyright (c) 2001 Maricopa County
# GPL Version 2.0 or later

import MCOptions
import os
import glob
import pstats
import string
import sys
import time
from MCGetOpt import *


__version__ = "0.0.1"

_usage = """
Usage: %s [-h] [--help] [-d level] [--debug=level] 
       [-v] [--version] <file to convert> <converted file>

Report bugs to dahill@mail.maricopa.gov.
"""

class ConvertPosition:

  def run(self):
    begin = time.clock()
    # Send std out to a log file
    sys.stdout = open("logfile.txt","w")

    # Parse command line options
    CmdOpt = MCGetOpt()
    CmdOpt.parseOptions(sys.argv)
    MCOptions.DEBUG = CmdOpt.getOption('debug_level')
    help = CmdOpt.getOption('help')
    version = CmdOpt.getOption('version')

    if MCOptions.DEBUG:
      print "Debug level =",MCOptions.DEBUG
            
    # assign input file from 1st free argument
    try:
      self.inputfile = CmdOpt.getArgument(0) 
    except:
      if help : print _usage % (sys.argv[0])
      elif version : print "%s v%s" % (sys.argv[0], __version__)
      else: print "No Input File Specified."
      sys.exit()

    # assign output file from 2nd free argument
    try:
      self.outputfile = CmdOpt.getArgument(1) 
    except:
      print "No Output File Specified."
      sys.exit()
      return (self.inputfile, self.outputfile)
    

    # get the file handlers
    try:
      file_input = open(self.inputfile)
    except:
      print "Cannot open %s for input." % (self.inputfile)
      sys.exit()
    try:
      file_output = open(self.outputfile,"w")
    except:
      print "Cannot open %s for writing." % (self.outputfile)
      sys.exit()      

    InRecords = ChgRecords = 0
# Read First Line
    line = file_input.readline()
# Determine which column to being looking for negatives
    header_rec = string.split(line, "\t")
    col_start = header_rec.index('"Jul"')
    col_end = len(header_rec)
    print("Data Column Start: %s" % col_start)
    print("Data Column End  : %s" % col_end)
# Write Header Out
#maybe    file_output.write(line)

    self.CheckNegative

    print '%(InRecords)d records input, %(ChgRecords)d changed.' % vars()
    file_input.close
    file_output.close
    end = time.clock()
    total = end - begin
    print "Time Elapsed : %3.5f ." % (total)

  def CheckNegative(self):
# Run Through Data
    line = self.file_input.readline()
    while line:
      InRecords = InRecords + 1
      pos_rec = string.split(line, "\t")
# Run Only Through the Numbers
      for i in range(col_start,col_end):
# If you find a Negative
        if(float(pos_rec[i]) < 0):
          ChgRecords = ChgRecords + 1
          file_output.write("\n\nError: %s\n" % ChgRecords)
          for i in range(0,col_end):
            if(i < col_start):
              file_output.write("%s : %s\n" % (string.replace(header_rec[i],'"',''), string.replace(pos_rec[i],'"','')))
            else:
              file_output.write(pos_rec[i])
          break
      line = file_input.readline()

if __name__ == '__main__':
  ConvertPosition().run()

