
proc init_IDL {} {
global vp wpp dx dy x y
    catch {unset dx dy x y}
    if {[catch {set vp}]} {
        set vp [open |idl w+]
        set wpp -1
    }
}


proc window_IDL {title {geometry 600x250}} {
global vp wpp 
#dx dy x y      check for appearance of these elsewhere, then delete
#
# initialize the geometry if it hasn't been done already
# put the first graph window in the vicinity of the GUI conrol panel
# and place the window near the control panel, unless geometry is given.
    set msize(y) 1000
    scan $geometry  %dx%d+%d+%d dx dy x y
    if {![info exists x]} {
        scan [winfo geometry .]  %dx%d+%d+%d dx0 dy0 x y
        incr x $dx0
        set y [expr "$y + $dy0"]
    }


# Open the window
    incr wpp
    set ypos [expr "$msize(y) - $y"]
    puts $vp "window, $wpp, title='$title', xpos=$x, ypos=$ypos,\
        xsize=$dx, ysize=$dy"
    return $wpp
}


#
# Plot 1 dimensional data from a file
#
# This version expects columnar data
#
proc plot_IDL {window axstyle args} {
    global vp
    set options ", xstyle=1, ystyle=1"
    case $axstyle in {
	linear { set plotcmd plot }
	log-linear { set plotcmd plot_oi }
	linear-log { set plotcmd plot_io }
    }
    puts $vp "wset, $window"
    foreach graph $args {
	set infile [lindex $graph 0]
	set format [lindex $graph 1]
	set graphstyle [lindex $graph 2]
	if {![file exists $infile]} {return 1}
	set d1 [get_dims $infile]
	set dims "[set nsets [lindex $d1 1]],[set npts [lindex $d1 0]]"
	case $format in {
	    xydydy {
		set options "$options, psym=6"
	    }
	    hdf {}
	    ncdf {}
	}

# Read the data
#   skip comments
	puts $vp "
        n = $npts
        y = fltarr($dims)
        s = 'a variable length string' & s0 = '%'
        openr, 1, '$infile'
        repeat begin & $
          point_lun,-1,position & $
          readf, 1, s  &  strput,s0,s,0 & $
        endrep until s0 ne '#' and s0 ne '%'
        point_lun, 1,position
        readf,1,y
        close, 1
        "
# Plot the data
	if {$nsets == 1} then {
	    set vars y(0,*)
	} elseif {$nsets > 1} then {
	    set vars y(0,*),y(1,*)
	} else {
	    return -1
	}
	case $graphstyle in {
	    scatter {set options "$options, psym=6"}
	}
	if ([regexp "^ *plot" $plotcmd]) {set options "$options , title='$infile'"}
puts "$plotcmd, $vars $options"
	case $format in {
	    {y xy xydxdx} {puts $vp "$plotcmd, $vars $options"}
	    xydydy {
		puts $vp "
                $plotcmd, $vars $options
                errplot, y(0,*), y(2,*), y(3,*)
            "
	    }
	    xy1y2... {
		puts $vp "
                ncols = sqrt($nsets)
                !p.multi = \[0, ncols, ${nsets}/ncols+($nsets mod ncols gt 0)\]
                $plotcmd, $vars $options
                for i=2,$nsets-1 do $plotcmd, y(0,*),y(i,*) $options
                !p.multi = 0
            "
	    }
	}
	if (![regexp ^oplot $plotcmd]) {set plotcmd oplot}
	set options {}
    }

    # puts $vp help and the while loop are included to make IDL print messages
    # to aid debugging.
    puts $vp help
    # flush the viz buffer, so the plot will appear
    flush $vp
    dump_IDL stderr
}



proc dump_IDL {fp} {
    global vp
    while {[gets $vp msg] > 0} {puts $fp $msg}
}


proc close_IDL {} {}
