#
# This action is VERY data specific! It will work on the class esrg_schema
# in the terry database on heel, with the field field files.
#
#Animation copy
#
# This is an action file from the query tool.  It is called with two
# arguemenets
# this is flakey, I am taking out text labels
#
# Field_num is the number of the field that the user selected the action to
#perform on
#
#Selection is the selection they choose from the monitor return.
#
proc action_idl_animate1 { field_num selection } {
global PGB env

# The following is not fool proof yet 
# pcat has to be in your path, and it HAS to be the postgres one not the
# Ultrix pcat
				    #First lets store files in $TMPDIR or pwd
	#default field for my data
	if {($field_num==-1)} { set field_num 18}
	set tmpdir $PGB(Tmp)

					#Now loop through each line selected
	set fp [ open {|idl} w+ ]
#	puts $fp "window,0,xs=700,ys=500"
	puts $fp "count=0"
	puts $fp "output=bytarr(695,415,[expr [llength $selection]/2+1])"

	flush $fp
	Working
	MkNotice "Begining transfer file 1 out of [llength $selection]"
	after 300
	set cnt 1

	foreach i $selection {
		set g [ split $i | ]
					#lose the spacer monitor lines
		if { ( [llength $g] > 1 ) } {
			set file [lindex $g $field_num]
			set outfile $tmpdir/[string trim $file]
			mkdir -p [dirname $outfile]


			#
			puts stdout " pcat -D $PGB(Db) $file > $outfile "
UpdateNotice "Transfering file $file :  This is number $cnt out of [llength $selection]"
after 300

			catch "pcat -D $PGB(Db) $file > $outfile " err
			incr cnt

			if " ( [file size $outfile ] > 0 ) " {
				puts $fp g=bytarr(695,415)
				puts $fp openr,1,"$outfile"

				puts $fp "readu,1,g"
				puts $fp "close,1"
				flush $fp
				puts $fp "g=reverse(g,2)"
				flush $fp
				puts $fp "output(*,*,count)=bytscl(g,0,200)"
				puts $fp "count=count+1"
				flush $fp
				rm $outfile

			} else {
				UpdateNotice "Empty File Alert $err"
					#If pcat is empty
				puts stdout "Problem pcat: $err"
				rm $outfile
			}
			# End of Idl script
		}
	}

#	puts $fp "wdelete,0"
	puts $fp "loadct,1"
	puts $fp "	xinteranimate,set=\[700,500,count\],/showload"
	puts $fp "for i=0,count do xinteranimate,frame=i,image=output(*,*,i)"
	puts $fp "	xinteranimate,0"
	puts $fp "wait, 3600"
	puts $fp "exit"
	flush $fp


	#catch { destroy .ft_note}
	Waiting
}
