#
# 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.
#
#
# This is an action file from the query tool.  It is called with two
# arguemenets
#
# 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 { 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
	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 "loadct,1"
	flush $fp
	Working
	MkNotice "Begining transfer file 1 out of [llength $selection]"
	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]"
			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"
				puts $fp "erase"
				puts $fp "g=reverse(g,2)"
				puts $fp "tvscl,g"
				puts $fp "xyouts,100,450,'$outfile',/device,charsize=1.5"
				flush $fp
				after 30000
				rm $outfile

			} else {
					#If pcat is empty
				puts "Problem pcat: $err"
				rm $outfile
			}
			# End of Idl script
		}
	}
	close $fp
	Waiting
	catch {destroy .ft_note}

}
