#!/usr/bin/env python
# -*- coding: utf-8 -*-

import scipy as sp
import sys
import time
from optparse import OptionParser
import myutil

def main(argv=None):
	if argv is None:
		argv = sys.argv[1:]
		
	parser = OptionParser(usage = "usage: %prog [options] FILE1 ...", version = "%prog 0.1")
	parser.add_option('-l', '--linenumber-size', dest='line_size', choices = range(1,8), default = 6, help = "number of characters that the line number takes")
	parser.add_option('-v', '--vector-length', dest='vector_length', type = int, default = 1849, help = "bitvector length")
	parser.add_option('-f', '--output', dest='output_file', metavar="FILE", help = "text output file name")
	(options, datafiles) = parser.parse_args(args = argv)
	
	if len(datafiles) != 2:
		parser.error("incorrect number of arguments")

	time_start = time.time()
#	remainingColumnsSet = set(range(0, (options.vector_length + 1)))
	setCover = []

	bitmapFilePath = datafiles[1]	
	print "loading ... "
	inputData = sp.genfromtxt(bitmapFilePath, delimiter = [options.line_size] + [1] * options.vector_length, dtype = int)
	inputData = sp.array(inputData)
	print "processing ..."		
	remainingColumnsList = sp.array([1] * options.vector_length, dtype = int)
	sums = sp.sum(inputData[:,1:] * remainingColumnsList, axis = 1)
	maxTableLineNumber = sp.argmax(sums)
# 		print "max table line number", maxTableLineNumber
	stageCounter = 0 
	while sums[maxTableLineNumber] > 0:
		setCover.append(inputData[maxTableLineNumber, 0])
		remainingColumnsList = sp.where(inputData[maxTableLineNumber,1:] > 0, 0, remainingColumnsList)					
		sums = sp.sum(inputData[:, 1:] * remainingColumnsList, axis = 1)
		maxTableLineNumber = sp.argmax(sums)	

		sys.stdout.write("|")
		sys.stdout.flush()
		stageCounter += 1	 
		
	sys.stdout.write("\n")
	setCover.sort()
	print setCover		
			
	textFilePath = datafiles[0]
	f = open(textFilePath, 'r')
	out = open(options.output_file, 'w')
				
	coverLinesIndex = 0
	lineCounter = 0
	coverLinesLength = len(setCover)
	textLine = f.readline()
	while coverLinesIndex < coverLinesLength:
		while lineCounter < setCover[coverLinesIndex]:
			textLine = f.readline()
			lineCounter += 1
		out.write("%s" % textLine)
		coverLinesIndex += 1	
	f.close()	
	out.close()			
		
	time_end = time.time()
	print "Took time %s" % (myutil.hrtime(time_end - time_start))	
	

if __name__ == "__main__":
	sys.exit(main())
