#!/usr/bin/python
"""
#+
# NAME:
#	vox_update
# PURPOSE:
#	Updates vox files for IPS forecast
# CATEGORY:
#	gen/python
# CALLING SEQUENCE:
# INPUTS:
# OPTIONAL INPUT PARAMETERS:
# OUTPUTS:
# OPTIONAL OUTPUT PARAMETERS:
# CALLS:
#	tiny.args
# SEE ALSO:
# SIDE EFFECTS:
# RESTRICTIONS:
# EXAMPLE:
# PROCEDURE:
# MODIFICATION HISTORY:
#	???-2003, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#-
"""
import os, sys, tempfile
from tiny import start
tempfile.tempdir = os.environ['TUB']

if __name__ == '__main__':
	nv_prefix = start('-nv_prefix=', sys.argv)
	if nv_prefix == '':
		nv_prefix = 'nv3d'

	bb_prefix = start('-bb_prefix=', sys.argv)
	if bb_prefix == '':
		bb_prefix = 'wson'

	dat = os.environ['DAT']

	nvb_dir = start('-source=', sys.argv)
	if nvb_dir == '':
		nvb_dir = os.path.join( dat, 'nagoya', 'fast', 'final' )

	vox_dir = start('-destination=', sys.argv)
	if vox_dir == '':
		vox_dir = os.path.join( dat, 'nagoya', 'fast', 'vox' )

	nv_len = len(nv_prefix)

	# Make list of vox files already present in destination

	vox_files = os.listdir(vox_dir)	# Vox file names (no directory)
	vox_times = []					# Times encoded in file name
	vox_stamp = []					# File stamp (modification time)

	for file in vox_files:			# Loop over all vox files
		tt = file.split('_')
		tt = tt[0]+'.'+tt[1]		# Integer and fraction of rotation
		tt = tt[nv_len:]			# Drop file name prefix
		vox_times.append( tt )
		vox_stamp.append( (os.stat( os.path.join(vox_dir,file)))[8] )

	# Make list of tomography density/velocity files in source directory
	# and pair them with the matching magnetic field files (if present).

	files = os.listdir(nvb_dir)
	bb_files = []
	nv_files = []

	for file in files:
		if file.find(nv_prefix) == 0:

			tt = file[nv_len:]		# Time encoded in name of nv-file

			# If vox file does not exist yet then process nv_file

			add_file = vox_times.count( tt ) == 0

			if not add_file:		# Vox file already exists

				# A vox file for time tt already exists.
				# The nv-file 'file' is processed only if it has a later
				# time stamp then the vox file.

				stamp = (os.stat(os.path.join(nvb_dir,file)))[8]
				add_file = stamp >= vox_stamp[ vox_times.index( tt ) ]

			if add_file:
				nv_files.append( file )	# Add to list of nv-files to be processed

				tt = bb_prefix+tt		# Find matching magnetic field file
				if files.count(tt) == 0:
					bb_files.append('')
				else:
					bb_files.append( files[files.index(tt)] )

	main_pro = tempfile.mktemp('.pro')
	tmp_list = tempfile.mktemp('.txt')

	# Write the main IDL program that produces vox_files.
	# This program calls vox_update.pro with the name of the input file tmp_list as argument

	iu = open( main_pro, 'w' )
	iu.write('vox_update, "'+tmp_list+'"\n')					# Executes vox_update
	iu.write('tmp = do_file(/delete, "'+tmp_list+'", /silent)\n' )
	iu.write('tmp = do_file(/delete, "'+main_pro+'", /silent)\n' )	# main.pro deletes itself
	iu.write('exit\n')
	iu.close()

	# Write the input file for smei_buf_l1a
	
	iu = open( tmp_list, 'w' )
	iu.write( nvb_dir+'\n' )		# Source directory
	iu.write( vox_dir+'\n' )		# Destination directory
	for i in range(len(nv_files)): iu.write( nv_files[i]+' '+bb_files[i]+'\n' )
	iu.close()

	# Create the vox files (the IDL program gets its input
	# from the file tmp_list.

	os.spawnlp( os.P_WAIT, 'idl', 'idl', '-quiet', main_pro )

	sys.exit()
