#!/usr/local/bin/python2.2

"""
build a directory containing links to a subset of the available RHIs,
so that Zebra doesn't get overloaded by seeing each RHI
"""
import os,os.path,stat,xreadlines,glob,string,time

from Perp.util.fileutil import safeMkdir,safeunlink,relink
from Perp.util.DoradeUtil import sweepTypeFromName,sweepFixedAngle
import cPickle

def is_a_directory(file):
    return stat.S_ISDIR(os.stat(file)[stat.ST_MODE])

def setup(
	baseDir, 	# base directory - e.g., /scr/data/spol
	subsetPlatforms,# platforms to be subsetted, e.g.,
			# ('particles', 'spol')
	):
    """ setup directories for zebra sweepfile subset creation """

    #
    # Get a listing of the base directory
    #
    baseContents = os.listdir(baseDir)

    
    #
    # Make the directories under baseDir to hold the data subsets for 
    # each of the subsetPlatforms.  E.g., if "spol" is one of the
    # subsetPlatforms, we will create a <baseDir>/spol_subset directory here.
    #
    for plat in subsetPlatforms:
        subsetDir = plat + "_subset"
	if subsetDir in baseContents:
	    # if the subset directory already exists, remove it from the
	    # baseDir listing, so we don't create an extra link to it below
	    baseContents.remove(subsetDir);
	else:
	    safeMkdir(os.path.join(baseDir, subsetDir))



def build_selected_links(
	baseDir, 	# base directory - e.g /scr/data/spol
	subsetPlatforms,# platforms to be subsetted, e.g.,
			# ('particles', 'spol')
	rhi_angle_list  # e.g  (1,5,10,20)
	):

    """ build/update sweepfiles links for 'subsetPlatforms', 
        only creating links to specified angles.
    """

    global Fdict
    for s in subsetPlatforms:
	platDir = os.path.join(baseDir, s)
	destDir = platDir + "_subset"
	flist = glob.glob(os.path.join(platDir,'swp.*'))
	for f in flist:
	    base = os.path.basename(f)
# save ourselves the trouble of attempting to make links if we've already
# processed this file for this directory
	    if Fdict.has_key((base,s)):
#	        print 'cache hit on (%s,%s)' % (base,s)
		continue

	    Fdict[(base,s)]= 1  
	    type = sweepTypeFromName(f)
	    dest = os.path.join(destDir,base)
            if not os.path.exists(dest):
		if type != 'RHI':
		    print 'linking (%s,%s)' % (base,s)
		    relink(f, dest,deleteSource=0)
		    os.system("dsrescan -file %s %s" % (base, s))
		else:
		    angle = sweepFixedAngle(f)
		    if angle in rhi_angle_list:
			print 'linking (%s,%s)' % (base,s)
			relink(f, dest,deleteSource=0)
			os.system("dsrescan -file %s %s" % (base, s))
	

def mainFunc():
    global Fdict
    Fdict = {}
    include_list = []
    rhi_include_file = '/usr/local/zebra/project/ihop/rhi_include'  
    delayTime = 5 # seconds to sleep
    pickleFile = "/tmp/build_selected.pck"
    f = open(rhi_include_file, 'r')

    for line in xreadlines.xreadlines(f):
	numList = string.split(line)
	# treat two numbers on a line as start,ending[inclusive] angle
	if len(numList) == 2:
	    start,end = float(numList[0]),float(numList[1])
	    for r in range(start,end+1):
		include_list.append(r)
	else:
	    include_list.append(float(line))
    f.close()
    # restore previous contents of dictionary, if one exists.
    if os.path.exists(pickleFile):
        try:
            pf = open(pickleFile, 'r')
            Fdict = cPickle.load(pf)
            pf.close()
        except EOFError:
            pass
        except OSError:
            pass

    baseDir = '/scr/data/spol'
    subsetPlatforms = ('particles', 'spol')

    setup(baseDir,  subsetPlatforms)

    cnt = 0
    saveFrequency = 10
    while 1:
#	print 'building links'
	build_selected_links(baseDir, subsetPlatforms, include_list )
#	print '\twaiting'
	# periodically save the dictionary of what files we've processed.
        if (cnt % saveFrequency) == 0:
	    pf = open(pickleFile, 'w')
	    cPickle.dump(Fdict, pf, 1)
	    pf.close()
	cnt = cnt+1
	time.sleep(delayTime)

if __name__ == '__main__':
	mainFunc()

