Viewing contents of file '../idllib/iuedac/iuelib/pro/confind.pro'
;************************************************************************
;+
;*NAME:
;
;     CONFIND   (General IDL Library 01) 
;  
;*CLASS:
;
;     User i/o; curve fitting
;  
;*CATEGORY:
;  
;*PURPOSE:
;
;     Procedure to select continuum points in a spectrum. CONFIND is a
;     modified version of the front end of NORM which uses the interactive
;     graphics cursor to pick out points for later continuum fitting.
;  
;*CALLING SEQUENCE:
;
;     CONFIND,WAVE,FLUX,WCONT,FCONT
;  
;*PARAMETERS:
;
;     WAVE   (REQ) (I) (1) (F D)
;            Required input vector containing the wavelength data for the
;            spectrum of interest.
;
;     FLUX   (REQ) (I) (1) (F D)
;            Required input vector containing the flux data.
;
;     WCONT  (REQ) (O) (1) (F D)
;            Required output vector containing the wavelengths of the
;            selected points.
;
;     FCONT  (REQ) (O) (1) (F D)
;            Required output vector containing the fluxes of the selected
;            points.
;
;*EXAMPLES:
;
;     To pick central wavelengths for continuum bandpasses:
;     CONFIND,WAVE,FLUX,WCONT,FCONT
;     then specify widths of bandpasses in vector WIDTHS
;     BINS,WAVE,FLUX,WCONT,WIDTHS,WMEAN,WSIG,WGT
;
;     To select continuum points for normalization:
;     CONFIND,WAVE,FLUX,WCONT,FCONT
;
;*SYSTEM VARIABLES USED:
;
;     !ERR
;     !d.x_ch_size
;     !d.y_ch_size
;     !d.y_size
;     !d.name
;  
;*INTERACTIVE INPUT:
;
;     User is prompted for rescaling plot of wave vs flux
;     The user also selects the continuum points via the graphics cursor.
;
;*FILES USED:
;  
;*SUBROUTINES CALLED:
;
;     PARCHECK
;     YESNO
;
;*SIDE EFFECTS:
;
;     If aborted, some system variables may be reset.
;  
;*RESTRICTIONS:
;
;     Device Dependent - This procedure requires a graphics cursor.
;  
;*NOTES:
;
;     !FANCY should be =0 before beginning this procedure, as the
;     fancy graphics conflict with cursor commands.
;
;	tested with IDL Version 2.1.0  (sunos sparc) 	10 Jul 91
;	tested with IDL Version 2.1.0  (ultrix mispel)  N/A
;	tested with IDL Version 2.1.0  (vms vax)     	10 Jul 91
;
;*PROCEDURE:
;
;     The user views the spectrum, and is given the opportunity to
;     rescale the plot. This is done iteratively until the user indicates
;     satisfaction by typing 0 (no more rescaling).
;
;     Next, while viewing the spectrum, the users marks points corresponding
;     to the continuum with the graphics cursor. Points can be added until
;     the user types 0. The data x and y coordinates of the points are 
;     calculated from the screen locations of the cursor.
;  
;*INF_1:
;  
;*MODIFICATION HISTORY:
;
;     May    1985  CAG GSFC wrote procedure based on NORM
;     Jun  6 1985  RWT GSFC allowed exit from CONFIND
;     Jun 11 1985  RWT GSFC modified user prompt for continuum points
;     Apr 13 1987  RWT GSFC use vector assignment statements, remove
;                           INSERT and EXTRACT commands, and use XYOUTS
;                           for XYOUT.
;     May 22 1987  RWT GSFC improve sorting 
;     Mar  1 1988  RWT GSFC make CONFIND a separate procedure
;     Mar  8 1988  CAG GSFC added VAX RDAF-style prolog, printing calling
;                           sequence if no parameters are specified, and
;                           check for minimum number of parameters.
;     1/08/90 RWT UNIX mods: convert to lower-case,
;     2-20-91 PJL  updated prolog
;     Jun 21 1991  PJL GSFC cleaned up; tested on SUN and VAX; updated prolog
;     Jul  1 1991  GRA CASA substantially modified; so that continuum is
;                           properly displayed if contiuum points are
;                           entered in non-ascending wavelength order,
;                           and the last *entered* point can be properly
;                           rejected if so desired.
;     Jul 10 1991  PJL GSFC corrected xmin, xmax, ymin, and ymax initialization;
;			    tested on SUN and VAX; updated prolog
;     May  4 1992  PJL GSFC corrected placement of tek "abort" option
;
;-
;************************************************************************
 pro confind,wave,flux,wcont,fcont
;
; ck. parameters and initialize variables
;
 if n_params(0) eq 0 then begin
    print,' CONFIND,WAVE,FLUX,WCONT,FCONT'
    retall
 endif  ; n_params(0)
 parcheck,n_params(0),4,'CONFIND'
;
 yn = ' ' 
 n  = 0 
 ix = fltarr(1)
 iy = fltarr(1)
 !err="61
 vpos = !d.y_size - !d.y_ch_size
 vspace = 1.1 * !d.y_ch_size
 hpos = !d.x_ch_size
 xmin = 0.0
 xmax = 0.0
 ymin = 0.0
 ymax = 0.0
;
; plot rescaling section (useful in emission line spectra)
;
 plot,wave,flux
 read,' Are you satisfied with plot scale (y or n)? ',yn
 yesno,yn
 while (not yn) do begin
    read,' Enter xmin and max: ',xmin,xmax
    read,' Enter ymin and max: ',ymin,ymax
    plot,wave,flux,XRANGE=[xmin,xmax],YRANGE=[ymin,ymax]
    yn=''
    read,' Are you satisfied with plot scale now (y or n)? ',yn
    yesno,yn
 end ; not yn
;
; plot wave vs flux with directions for user to define continuum
; Let user set cross hairs to define continuum
; For console monitors, select pt = 1, replot = 2, finished = 4
; For tek emulation, select pt = "60 (ascii 0), replot = "61 (ascii 1)
; finished = "62 (ascii 2).
;
; While loop for contiuum point selection.
;
 while (!err ne "60) and (!err ne 4) do begin  
   ;
    if (!err eq "61) or (!err eq 2) then begin
      ;
      ; First time through loop
      ;            or
      ; Replot and print instructions 
      ;
       plot,wave,flux,XRANGE=[xmin,xmax],YRANGE=[ymin,ymax]
      ;
       xyouts,hpos,vpos-2.2*vspace,' '
       if (strlowcase(!d.name) eq 'tek') then begin
          print,'  To select point, move cursor to point and press space bar'
          print,'  To abort,              type 2 '
          print,'  To replot last point,  type 1 '
          print,'  If finished,           type 0 '
       endif else begin
          print,' '
          print,'  To select point, move cursor to point and press left button'
          print,'  To replot last point,  press middle mouse button'
          print,'  If finished,           press right mouse button'
          print,' '
       endelse  ; !d.name
      ;
      ; remove last point from ix, iy vectors
      ;
       if n gt 0 then begin 
         ;
          n = n - 1
          if n le 1 then begin
             x = fltarr(1)
             y = fltarr(1)
             if n eq 1 then begin
                x(0) = ix(0)
                y(0) = iy(0)
             endif  ; n eq 1
          endif else begin
             x = ix(0:n-1)
             y = iy(0:n-1)
          endelse  ; n gt 1
          ix = x
          iy = y
       endif  ; n gt 0
      ;
       if n ge 2 then begin
         ;
         ; create sorted vectors for plotting, final output
         ;
          ind = sort(ix)
          px = ix(ind)
          py = iy(ind)
         ;
          oplot,px,py,psym=0  ; plot sorted continuum points
         ;
       endif  ; n ge 2
      ;
    endif  ; !err
   ;
    if (!err ne "61) and (!err ne 2) then begin
      ;
      ; Not "replot" or "finished"
      ;
       if (!err ne "60) and (!err ne 4) then begin 
         ;
         ; add last point to ix, iy vectors
         ; 
          n = n + 1  ; # of points 
          x = fltarr(n)
          x(0) = ix
          x(n-1) = tx
          ix = x
         ;
          y = fltarr(n)
          y(0) = iy
          y(n-1) = ty
          iy = y
         ;
         ; create sorted vectors for plotting, final output
         ;
          ind = sort(ix)
          px = ix(ind)
          py = iy(ind)
         ;
          if n ge 2 then begin
             if tx lt max(ix) then begin 
               ; 
               ; replot to avoid double valued appearance
               ;
                plot,wave,flux,XRANGE=[xmin,xmax],YRANGE=[ymin,ymax]
               ;
                xyouts,hpos,vpos-2.2*vspace,' '
                if (strlowcase(!d.name) eq 'tek') then begin
                   print, $
                '  To select point, move cursor to point and press space bar'
                   print,'  To abort,              type 2 '
                   print,'  To replot last point,  type 1 '
                   print,'  If finished,           type 0 '
                endif else begin
                   print,' '
                   print, $
                '  To select point, move cursor to point and press left button'
                   print,'  To replot last point,  press middle mouse button'
                   print,'  If finished,           press right mouse button'
                   print,' '
                endelse  ; !d.name
               ;
             endif 
            ; 
             oplot,px,py,psym=0  ; plot sorted continuum
            ;
          endif  ; n ge 2
         ;
          !err=0   ; force one more point
         ;
       endif  ; add point
      ;
    endif  ; !err ne "61 or 2
   ;
   ; get next point
   ;
    flush = "not null string"
    while flush ne '' do begin
       flush = get_kbrd(0)
    endwhile  ; flush keyboard output buffer
   ;
    cursor,tx,ty,/down,/data  ;read cursor
   ;
    if !err eq "62 then retall  ; abort (tek only)
   ;
 endwhile  ; !err ne "60 or 4
;
; define output variables and return
;
 wcont  = px
 fcont  = py
;
 return
 end  ; confind