
#
# 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
				    #First lets store files in $TMPDIR or pwd
	if [info exists env(TMPDIR)] {
		set tmpdir $env(TMPDIR)
	} else {
		set tmpdir .
	}

					#Now loop through each line selected
	set fp [ open {|idl} w+ ]
	puts $fp "window,0,xs=700,ys=500"
	puts $fp "loadct,1"
	Working
	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 "
			catch "pcat -D $PGB(Db) $file > $outfile " err

			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
}
