Viewing contents of file '../idllib/iuedac/iuelib/pro/bspot.pro'
;******************************************************************************
;+
;*NAME:
;
;     	BSPOT       (RDAF General Production Library)      Jan 30, l983
; 
;*CLASS:
;
;     	Data Editing
;
;*CATEGORY:
;
;       IUESIPS         NEWSIPS
;
;*PURPOSE:  
;
;    	To interactively flag or linearly interpolate over bright spots or
;       cosmic ray hits in an IUESIPS LBLS or NEWSIPS SILO file so that the
;       background is not contaminated and on-order hits will be flagged in
;       low dispersion spectra.
; 
;*CALLING SEQUENCE:    
;
;    	BSPOT,IMAGI,IMAGO,mode
; 
;*PARAMETERS:
;
;    	IMAGI  	(REQ) (I) (1) (S)
;           	For IUESIPS:  Input LBLS (or ELBL) file specified as a string
;               (e.g. 'swp7654s').
;               For NEWSIPS:  Input SILO file specified as a string (e.g.,
;               'swp17549.silo').
;
;    	IMAGO  	(REQ) (O) (1) (S)
;           	Output file name for modified IUESIPS LBLS or NEWSIPS-like SILO
;               file.  If specified as a string, the input file will be copied
;               into a new file which BSPOT can then modify.
;
;               For IUESIPS, user flagged points are given an EPS value of
;               -325, and interpolated points are assigned an EPS of -210.
;
;               For NEWSIPS, user flagged and interpolated points are assigned
;               a nu flag with bit 6 (-32) set (in addition to the NEWSIPS nu
;               flags that were set).
;
;               - for VMS implementation -
;            	If specified as a blank (i.e. ' ') or 0, output file will
;               have same name as IMAGI with version number incremented.  
;
;               - for Unix implementation -
;            	If specified as a blank (i.e. ' ') or 0, output file is 
;               created with same name as input file (replacing previous 
;               version).
;
;     	MODE   	(OPT) (I) (0) (I)
;            	Determines type of interpolation to be used when user selects
;               interpolation.  If not specified, MODE is set to 0.  The
;               following values are allowed:
;              	   0 - the interpolation step is performed using the nearest
;                      good points (i.e. those just above and below the
;                      endpoints specified by the x-coordinate of the verticle
;                      crosshair). 
;                 >0 - the flux values used in the interpolation are taken to
;                      be the y-coordinates of the horizontal crosshair at the
;                      user specified interpolation limits.
;                 >1 - IUEPLOT/NSPLOT is called to display the modified line of
;                      the LBLS/SILO file. Since each blemish is displayed
;                      individually, this step is not necessary.  A 10 second
;                      pause is issued before the IUEPLOT/NSPLOT figure is
;                      erased, and the user is prompted for the next line.
;
;*EXAMPLES:
;
;       (Optional parameter Mode default is 0)
;
;
;       For IUESIPS:
;
;          bspot,'swp7654s','ssss'    ; swp7654s is copied into the file 'ssss'
;                                     ; and all BSPOT corrections are made to
;                                     ; ssss.
; 
;          bspot,'swp7654s',' '       ; swp7654s is copied into a new file with
;               or                    ; the same name (and the version number
;          bspot,'swp7654s',0         ; incremented - on VMS systems) and all
;                                     ; BSPOT changes are made to this file.
; 
;
;	For NEWSIPS:
;
;          bspot,'swp17549.silo','ssss'   ; swp17549.silo is copied into the
;                                         ; file 'ssss.silo' and all BSPOT
;                                         ; corrections are made to ssss.silo.
; 
;          bspot,'swp17549.silo',' '      ; All BSPOT changes are written to
;                  or                     ; new versions of swp17549.silo.  On
;          bspot,'swp17549.silo',0        ; VMS systems, the version number is
;                                         ; incremented.
; 
;*SYSTEM VARIABLES USED:
;
;       !d.name
;       !stime
;
;*INTERACTIVE INPUT:
;
;    	User is prompted for the following:
;      	   - line number to be edited, or 0 to exit,
;      	   - cursor position of each blemish to be flagged,
;      	   - cursor positions of endpoints of region to be flagged, or cursor
;            positions of region to have user-specified flags removed, or
;            cursor positions of region to have ALL flags removed, or cursor
;            positions of region to interpolate over, or C to continue, or 0 to
;            exit procedure.
;      	   - 0 to continue, 1 to redisplay last plot with changes removed, or 2
;            to redisplay last plot with all changes included,
;          - new line number, or 0 to exit.
; 
;*SUBROUTINES CALLED:
;
;       IUEFTYPE
;    	IUEPLOT
;       NSPLOT
;    	READFILE
;       READSI
;       ADDPAR
;       STPAR
;    	TABINV
;    	PARCHECK
;    	COPUF
;    	GETBLEMW
;    	FLAGBLEM 
;    	BS_UPDATE
;
;*FILES USED:
;
;     	IMAGI  	(I) 
;          Input/output IUESIPS LBLS or NEWSIPS SILO file (output file only if
;          specified by user).
;
;     	IMAGET 	(O)
;          Output IUESIPS LBLS or NEWSIPS-like SILO file.
;
;*SIDE EFFECTS:
;
;       For VMS systems, BSPOT creates a new file each time the information 
;       is saved. There will possibly be mutiple copies of similar files
;       just with different version numbers.  On Unix systems, the file is
;       overwritten.
;
;*RESTRICTIONS:
;
;       - only for use with tektronix style plot device (!d.name = 'TEK')
;
;*NOTES:
;
;     	1) For IUESIPS, the file is edited one line at a time, and updated each
;          time a line is modified.  For NEWSIPS, a file is written after each
;          line is modified.  This means that the procedure can be exited
;          without losing any modifications made to previously modified lines.
;          If the routine is exited during the prompt for flagging endpoints of
;          a blemish however, the changes in that particular line will not be
;          saved.
;     	2) If the system crashes while the file is open, the file may not be
;          recoverable.
;     	3) BSPOT flags or unflags regions of pixels by having the user input
;          the cursor postions of the endpoints.  The endpoints specified are
;          INCLUDED in the pixels to be flagged.
;     	4) For IUESIPS, BSPOT flags bad points by setting epsilons > 0 to -325.
;          Interpolated points are assigned epsilons values of -210.  Both flag
;          are removed by replacing epsilons with a value of 100 in the X mode.
;          In the R mode, epsilons less than zero are given the value
;          abs(eps)/10.
;          For NEWSIPS, BSPOT flags bad points and interpolated points by
;          setting bit 6 (-32) of the nu flag.  The nu flag is removed by
;          unsetting bit 6 of the nu flag in the X mode.  In the R mode, nu
;          flags less that zero are given the value 0.
;   	   NOTE that in the R MODE, the ORIGINAL IUESIPS or NEWSIPS FLAGGING is
;          REMOVED AND is NOT EASILY RECOVERABLE.
;       5) The user should identify the bright spots from inspection of the
;          display of the IUESIPS LBLS or NEWSIPS SILO data.  See SRPLOT for
;          display.
;
;*PROCEDURE:
;
;     	For IUESIPS, BSPOT first reads the IUESIPS line-by-line input file into
;       memory using READFILE.  BSPOT checks the IMAGO parameter to decide
;       whether a new LBLS file is to be created or whether the modifications
;       are to be made directly to the input file.  If IMAGO is specified as a
;       string, new .DAT and .LAB files are created from the input IMAGI files.
;       The type of file (i.e. LBLS or ELBL) is determined from the header
;     	and is used to determine the range of allowed line numbers.  The user
;       is prompted for the line number (i.e. order) in the LBLS file to be
;       edited.  The appropriate line is extracted from the file and displayed. 
;
;       For NEWSIPS, BSPOT first reads the NEWSIPS resampled image (SILO) input
;       file into memory using READSI.  The filename for the output file is
;       determined.  The range of allowed line numbers is determined from the
;       fits header.  The user is prompted for the line number of the SILO
;       image to be edited.  The appropriate line is displayed. 
;
;       The user is then asked to position the cursor on each blemish in the
;       line which he wishes to examine (up to 10 blemishes can be specified
;       per order).
;	
;       BSPOT then plots an expanded region (i.e. 50 points) centered 
;       about each of the selected hits.  For each region, the user can either
;      	   1) flag a range of pixels by positioning the cursor at the 
;             left-most and right-most points to be flagged,
;          2) type 0 to exit routine,
;          3) type C to go on to next blemish or line to be examined,
;          4) type X to undue users flagging between (& including) endpoints
;          5) type R to remove ALL flagging between (& including) endpoints, or
;          6) type I to linearly interpolate over the specified region.
;
;    	After the above prompt, the new IUESIPS epsilon flags or NEWSIPS nu
;       flags are displayed and the user is asked to type 0 to continue, 1 to
;       redisplay the expanded region with all changes removed, or 2 to
;       redisplay region keeping all changes.
;
;    	After all the specified blemishes for the current line have been
;    	displayed, BSPOT displays the entire line (IF MODE > 1).  For IUESIPS, 
;       the output file is modified if any changes were made by the user.  For
;       NEWSIPS, a new fits file is written if any changes were made by the
;       user.  The program continues by prompting the user for either the next
;       line number to edit, or 0 to exit.
;
;*I_HELP nn:
;
;*MODIFICATION HISTORY:
;
;     	Programmer: R.J. Panek 30-Jan-1983
;     	8-2-85 GAR allow user to not flag points after choosing a
;            	wavelength
;     	6-13-85 RWT compress code, replace IUEIM with HSWATH, replace
;            	READCRS with TEKDATA, reformat main routine, test user
;            	input after 1st cursor read, add R option, add DECOMPOSE, 
;            	position cursor ON blemish (not outside blemish)
;     	6-19-85 RWT add subroutine INOUT, rewrite DOC file
;     	6-24-85 RWT delete subroutines DELFLAG & ADDFLAG and simplify
;            	coding of user-specified epsilon flags.
;    	10-22-85 RWT DIDL changes (i.e., use #, NELEMENTS, and new INDGEN)
;            	and truncate EPS values at 100 rather than 1
;     	6-25-86 RWT & JKF modify for ELBL files, use SETXY, replace TEKDATA
;            	with new CURSOR command, allow interpolation of bad points,
;            	and flag interpolated points with EPS = -210.
;    	12-29-86 RWT VAX mods: use !MODE for !VAR5, SET_XY for SETXY, use
;                READFILE, and remove HSWATH & GETNLS.
;     	4-13-87 RWT add PARCHECK, axes titles, remove INSERT and EXTRACT 
;               commands
;     	6-22-87 RWT remove redundant reextraction of W, F, and E arrays
;               and fix location of user prompts
;     	8-25-87 RWT use GET_LUN, add optional parameter MODE to replace
;            	!MODE, and allow procedure call listing
;     	1-28-88 RWT display correct order and write corrected line into
;            	correct record of output file
;     	2-23-88 RWT separate (& rename) subroutines
;     	3-09-88 HAA add RDAF Prolog
;       jan-10-90 jtb @gsfc modified for unix / sun idl
;	jul-22-91 pjl @gsfc cleaned up; tested on SUN and VAX; updated prolog
;       aug-23-91 jg  corrected prolog
;       nov-22-91 pjl corrected typo in ne 'tek' message
;         7-22-92 LLT check size of IEPS vector to avoid oplot of nonexistant
;                     epsilon vectors.
;       apr-06-94 PJL added NEWSIPS compatibility including IUEFTYPE and
;                     NSPLOT; removed 'goto'; updated prolog
;       mar-02-95 RWT replace bs_inout with copuf, remove capability to
;                     overwrite vms iuesips files, move writesi from
;                     bs_update to main routine (so newsips file is updated 
;                     once, not once for each line), and call new writesi
;                     with scale option.
;       
;-
;******************************************************************************
 pro bspot,imagi,imago,mode
;
 npar = n_params(0)
 if (npar eq 0) then begin
    print,'BSPOT,IMAGI,IMAGO,mode'
    retall
 endif  ; npar eq 0
 parcheck,npar,[2,3],'BSPOT'
;
 if (strlowcase(!d.name) ne 'tek') then begin
    print,'This procedure can only be used with a tektronix style plot device.'
    print,'To modify IUESIPS LBL files or NEWSIPS SILO files on a monitor with'
    print,'image display capabilites (eg. a workstation) use the IUEDAC'
    print,'procedure LBLFIX.'
    print,'ACTION:  Returning'
    retall
 endif  ; !d.name
;
 if (npar eq 2) then mode = 0
;
;  determine file type
;
 iueftype,imagi,filetype
;
 case (strlowcase(filetype)) of
    's':  begin                    ; IUESIPS LBLS or ELBL file
;
;  read line-by-line file into memory
;
             print,'Reading IUESIPS line-by-line file into memory.'
             print,' '
             readfile,imagi,lab,h,wave,fimage,eimage
             nls = h(2)     ; number of lines (55 or 110)
             print,'Number of lines in input file: ',byte(nls)
             npts = h(300)
             newsips = 0
             copuf,imagi,imago      ; create output file if appropriate
          end  ; strlowcase(filetype) eq 's'
    'silo':  begin                  ; NEWSIPS silo file
;
;  read SI file into memory
;
                print,'Reading NEWSIPS resampled file into memory.'
                print,' '
                readsi,imagi,h,wave,fimage,eimage
                stpar,h,'NAXIS2',nls,err
                if (err) then begin
                   temp = size(fimage)
                   nls = temp(temp(0))
                endif  ; err
                print,'Number of lines in input file: ',strtrim(nls,2)
                addpar,h,'HISTORY',' BSPOT with mode = ' + strtrim(mode,2) +  $
                   '  ' + !stime,'','IUEDAC'
                newsips = 1
                copuf,imagi,imago,/nocopy    ; only determine output name 
             end  ; strlowcase(filetype) eq 'silo'
    else:  begin                    ; file not found
              print,' '
              print,'File ' + imagi + ' not of proper type (i.e., not an'
              print,'IUESIPS LBLS or NEWSIPS SILO file).'
              print,'ACTION:  retall'
              retall
           end  ; else
 endcase  ; newsips
;
; create output disk file if requested
;
; bs_inout,imagi,imago,imaget,newsips
;
;  prompt user for first lbls order (line) or si line to be displayed
;
 print,' '
 read,'Enter number of line to be edited (0 to end): ',line
 if ( (line lt 1) or (line gt nls) ) then begin
    if (line eq 0) then retall
    print,'Exiting BSPOT.  line number = ' + strtrim(line,2) + ' invalid.'
    retall
 endif  ; (line lt 1) or (line gt nls)
;
;  loop through code for each line requested by user.
;
 finish = 0
 while (not(finish)) do begin    ; loop back here to edit another line
;;; nextl:          ; loop back here to edit another line
    line = fix(line)
    nflag = 0
    flux = fimage(*,line-1)
    eps = eimage(*,line-1)
    plot,wave,flux, $
       xrange=[0,0],yrange=[0,0],psym=10,xtitle='Wave',ytitle='Flux'
    ieps = where(eps lt 0,ecount)
    if (ecount gt 0) then oplot,wave(ieps),flux(ieps),psym=7
    getblemw,bswave,nwave            ; get wavelengths of blemishes.
;
;  loop through code for each specified blemish.
;
    if (nwave gt 0) then for nw=0,nwave-1 do begin
       w0 = bswave(nw)
;
;  determine expanded plotting area for each bright spot.
;
       tabinv,wave,w0,iw0                       ; center of blemish region
       iw0 = fix(iw0 + 0.5)
       i1 = (iw0-25)>0                          ; 25 angs. shortward
       i2 = (iw0+25)<(n_elements(wave)-1)       ; 25 angs. longward
       inx = indgen(i2-i1+1)+i1                 ; expanded display window.
;
;  plot above region identifying sips(x) & user flagged(+) points
;
       flagblem,wave,flux,eps,mode,inx,nflag,newsips
    endfor  ; looping through requested wavelengths
;
;  update entries in lbls or si file if new points were flagged.
;
    if (nflag ne 0) then begin     ; update line only if new points flagged! 
       if (mode gt 1) then if (newsips) then nsplot,h,wave,flux,eps else   $
          iueplot,h,wave,flux,eps
       if (mode gt 1) then wait,10
       if (newsips) then   $
          bs_update,imago,flux,eps,line,nls,h,wave,fimage,eimage else   $
          bs_update,imago,flux,eps,line,nls
    endif  ; updating line
;
;  go on to next line or exit routine.
;
    erase  
    print,'Number of last line edited: ' + strtrim(line,2)
    read,'Enter number of next line to be edited (0 to end): ',line
;
;  loop back or not
;
    if ( (line ge 1) and (line le nls) ) then finish = 0 else finish = 1
;;;    if ( (line ge 1) and (line le nls) ) then goto,nextl   ; loop back
 endwhile  ; not(finish)
;
; now write out newsips file
; (iuesips files already created by bs_update)
;
 if (newsips) then begin
    print,' '
    print,'Creating new FITS file'
    writesi,h,wave,fimage,eimage,filename=imago,/scale
 endif
 print,' '
 print,'Exiting BSPOT'
 if (line ne 0) then print,' line number = ' + strtrim(fix(line),2) +   $
    ' is invalid.'
;
 return
 end  ; bspot