#!/usr/bin/env /home/beams/BCDA/bin/python
# This python program will generate ascii file under ~/Yorick/ASCII
# directory for all 1D and 2D mda files found in the user specified mda
# directory.
#
# It should be invoked from the ~/Yorick directory, it will use the
# yorick program to generate the ascii report for user.
#
# Usage:
# 	mdaRpt.py /path/to/data/mda/directory
#
# for 1D mda a single ascii file generated
# for 2D mda an image file is generated for each defined detector
#
# User has to be careful because lots of txt file may be generated by this
# script, user has to make sure there are enough space available in the
#  ~/Yorick/ASCII directory 
#
# The old ASCII file will be overwritten by new MDA file, user has to make
# sure the integrity of ASCII directory for different run
# 
#

import sys
import os
import string

def RptAll(p):
	c_dir = os.getcwd()
	if os.path.exists('ASCII') == 0:
	   os.mkdir('ASCII')
	os.chdir(p)
	d_dir = os.getcwd()
	lst = os.listdir(d_dir)
	os.chdir(c_dir)
	fns = []
	for fn in lst:
		ln = len(fn)
		fd = string.find(fn,'.mda',ln-4,ln)
		if fd > 0 : fns.append(fn)
	fns.sort()
	for fn in fns:
		fd = string.find(fn,'.mda')
		if fd > 0 : 
		   xfd = string.find(fn,'.mda.')
		   if xfd == -1: 
			temp = 'yca scanSee_report '+d_dir+os.sep+fn+',pick1=1,pick2=85,exit=1'
			print temp
			os.system(temp)
		

try:
    if sys.argv[1] != '':
 	RptAll(sys.argv[1])

except IndexError:
	print 'Usage:   mdaRpt.py /full/path/to/data/directory/mda'


except os.error,value:
	print value
