#!/usr/bin/env python
"""
#+
# NAME:
#	smei_star_split
# PURPOSE:
#	Extract all data from a collection 'pnt' files in subdirectories
#	c1, c2, c3, c1m0, c2m0, c3m0 of SOURCE where SOURCE is specified
#	through option --source
# CALLING SEQUENCE:
#	smei_star_split [--source=SOURCE ]
# OPTIONAL INPUTS:
#	--source=SOURCE		source directory for 'pnt' files;
#						default: $SMEISKY0/pnt
#						all 'pnt' files in subdirectories
#						c1,c2,c3,c1m0,c2m0,c3m0 are processed
# OUTPUTS:
#	The resulting files for individual stars are written to the
#	current working directory.
# RESTRICTIONS:
#	The script opens as many files as there are stars present in
#	the 'pnt' files (plus planets and asteroids). The current total
#	is just under 6000 objects. The script should raise a flag if
#	opening this many files at the same time is not allowed.
#	To be able to open this many files it is probably necessary to
#	add a line to /etc/security/limits.conf, e.g. if running on the
#	'soft' account add:
#		soft        soft    nofile          8192
#		soft        hard    nofile          8192
# PROCEDURE:
#	'pnt' files are produced as byproduct of the star fitting and
#	subtraction process by the IDL procedure smei_star_remove.pro.
#	By default, these files are collected in subdirectories
#	c1, c2, c3, c1m0, c2m0, c3m0 of directory $SMEISKY0/pnt.
#	These files contain all fit information for stars in a single
#	skymap for camera 1, 2 or 3.
#	This script reorders the content of these files by creating
#	a single file for each individual star containing all fits
#	for that stars across all 'pnt' files.
#
#	These files will initially contain six consecutive groups of
#	records: one for each of the six subdirectories, with fits for
#	each camera ordered chronologically.
#
#	After the 'pnt'-rewriting is finished the resulting files
#	for each star can be sorted into strict chronological order
#	(independent of originating subdirectory) by running all
#	files through the IDL procedure smei_star_split.pro.
# MODIFICATION HISTORY:
#	JUL-2012, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#		Spruced up Johns original version.
#-
"""
import os, sys, glob, time, resource

openFiles = dict()

def fileIsOpen(name):
	return openFiles.has_key(name)

def readFile(file):	# remove lines with comments
	linestmp = open(file,"r").readlines()
	lines = filter(lambda line: not line[0] == ';', linestmp)
	return lines
	#return [ line for line in open(file,"r").readlines() if line[0] != ';' ]

def addLine(name, line, verbose):
	if len(line) == 0:
		if verbose:
			print "   dropping empty line for '%s"%name
	else:
		if not fileIsOpen(name):
			if verbose:
				print "   star %5s: %s"%(len(openFiles),name)
			openFiles[name] = open(name, "w")
		openFiles[name].write(line)

def closeAllFiles():
	for file in openFiles:
		openFiles[file].close()

def getName(star):
	return star.replace(' ','_').replace('*','#')+'.txt.dirty'
    
if __name__ == '__main__':

	from optparse import OptionParser

	usage =	"%prog --source=SOURCE\n" + \
			"\tall pnt files in subdirectores 'c1','c2' and 'c3' of SOURCE are\n" + \
			"\tprocessed. Star timeseries files are written to working directory"

	version = '0.01'
	parser = OptionParser(usage=usage,version=version)
	source = os.path.join( os.environ['SMEISKY0'], 'pnt'  )

	parser.add_option('-s', '--source'	,
		action  = 'store'				,
		type    = 'string'				,
		default = source				,
		help    = 'source directory of pnt files (default: %s)'%source
	)
	parser.add_option('-v', '--verbose'	,
		action  = 'store_true'  		,
		default = False                	,
		help    = 'verbose mode (default: False)'
	)

	options, args = parser.parse_args()
 
	limits = resource.getrlimit(resource.RLIMIT_NOFILE)
	if (min(limits) < 8192):
		print "Increase limit on number of open files from %d to at least 8192" % (min(limits))
		print "by adding user '%s' to /etc/security/limits.conf:"%os.environ['USER']
		print "    soft        soft    nofile          8192"
		print "    soft        hard    nofile          8192"
		sys.exit(1)

	if options.verbose:
		print "Processing pnt files from %s"%options.source

	for subdir in ('c1','c1m0','c2','c2m0','c3','c3m0'):
		files = glob.glob(os.path.join(options.source,subdir,'*.txt'))
		files.sort()		# This makes each subdir chronological

		if options.verbose:
			print 'Processing subdir: %s (%d pnt files)'%(subdir,len(files))
			time.sleep(5)

		for file in files:
			for line in readFile(file):
				# First 12 chars of each line is the the name of a star
				addLine(getName(line[0:12]),line,options.verbose)
 
	for file in openFiles:
		openFiles[file].close()

	sys.exit(0) 
