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

import os
import sys
import nijmetrics
		
if __name__ == '__main__':

	if '--help' in sys.argv:
		
		print "This script takes a pickled file from 'extract_nij_metrics' and generates various outputs out of it. The kind of output depends on the first argument."
		print ""
		print "Syntax:"
		print os.path.split(sys.argv[0])[1]+" ACTION INPUT_FILE [ARGS]"
		print ""
		print "Options:"
		print " -t FILE: tee (duplicate) the output to the specified logfile."
		print " -d DIRECTORY: dump various debug files to the specified directory."
		print " -c DIRECTORY: cache startup files to the specified directory. Note: you need to clear the cache yourself if the input file changes."
		print ""
		print "Actions:"
		print " images: generates 4 .PNG files per exercise: one for each quality level (good, medium and low) and an overlaid version (consensus). Take care: the consensus image is *not* appropriate for numerical analysis since the colors from PiAnoS are used and these do not map to the 3 RGB channels. Files for each quality level are only slightly better: the pixel intensity is normalized to the [0-255] range."
		print " csv: generates many .CSV files: for each exercise, one consensus and one file per user and quality level (so for 5 exercises with 100 users this is 5x(100x3+1)=1505 files)"
		print " metrics: generates the .CSV files containing the Mx metrics (M1, M2, etc. as per the 'data_extraction_log.doc' document)"
		print "          ARGS are a list of metric names to calculate (leave empty for all)"
		sys.exit(0)
	
	tee = None
	args = sys.argv[1:]
	try:
		idx = args.index('-t')
		if len(args) <= idx:
			print "ERROR: -t option requires an argument"
			sys.exit(1)
		tee = args[idx+1]
		args = args[:idx] + args[idx+2:]
	except ValueError:
		pass
		
	dumpdir = None
	try:
		idx = args.index('-d')
		if len(args) <= idx:
			print "ERROR: -d option requires an argument"
			sys.exit(1)
		dumpdir = args[idx+1]
		args = args[:idx] + args[idx+2:]
	except ValueError:
		pass

	cache = None
	try:
		idx = args.index('-c')
		if len(args) <= idx:
			print "ERROR: -c option requires an argument"
			sys.exit(1)
		cache = args[idx+1]
		args = args[:idx] + args[idx+2:]
	except ValueError:
		pass

	if len(args) < 1:
		print "ERROR: you must specify an action as first argument"
		sys.exit(1)

	if len(args) < 2:
		print "ERROR: you must specify a .pickle file as second argument"
		sys.exit(1)
	
	with nijmetrics.Tee(tee):
		if args[0] == 'images':
			app = nijmetrics.QMapToImage(args[1], dumpdir=dumpdir, cache=cache)
		elif args[0] == 'csv':
			app = nijmetrics.QMapToCSV(args[1], dumpdir=dumpdir, cache=cache)
		elif args[0] == 'metrics':
			app = nijmetrics.MxMetrics(args[1], dumpdir=dumpdir, cache=cache, with_metrics=args[2:] if args[2:] else None)
		elif args[0] == 'minutiae':
			app = nijmetrics.MinutiaeExport(args[1], dumpdir=dumpdir, cache=cache)
		else:
			print "ERROR: %s is not a valid action. Try --help" % args[0]
			sys.exit(1)
			
		sys.exit(0 if app.run() else 1)
	