Viewing contents of file '../idllib/iuedac/iuelib/pro/bs_flag.pro'
;******************************************************************************
;+
;*NAME:
;
;     	BS_FLAG        (RDAF General Production Library)        JUNE 18, 1985
; 
;*CLASS:
;
;       Data Editing
;
;*CATEGORY:
;
;       IUESIPS         NEWSIPS
;
;*PURPOSE:  
;
;     	Sets IUESIPS epsilon flags or NEWSIPS nu flags and/or interpolates over
;       blemish when called by FLAGBLEM.
; 
;*CALLING SEQUENCE:
;
;       BS_FLAG,WAVE,FLUX,EPS,INX,NFLAG,MODE,ER1,NEWSIPS
;
;*PARAMETERS:
;
;     	WAVE	(REQ) (I) (1) (R)
;            	Wavelength vector in Angstroms.
;
;       FLUX  	(REQ) (I) (1) (R)
;            	The slit-integrated flux vector.
;
;       EPS   	(REQ) (I) (1) (R)
;            	For IUESIPS, the epsilon vector quality flags to mark
;               questionable data at each wavelength.  For NEWSIPS, the nu
;               flags.
;        
;   	INX   	(REQ) (I) (1) (R)
;            	Expanded window around blemished region.
;
;   	NFLAG 	(REQ) (I) (0) (I)
;            	Determine if any user flagging was performed.
;
;   	MODE  	(REQ) (I) (0) (I)
;            	Used to key interpolation step:
;                  0 = use nearest good data points for interpolation.
;                  1 = use cursor (horizontal) to set flux level.
;
;   	ER1  	(REQ) (O) (0) (L)
;            	Checks !err in program to determine error flags.
;
;       NEWSIPS (REQ) (I) (0) (I)
;               Equals 0 for IUESIPS data.  Equals 1 for NEWSIPS data.
;
;*EXAMPLES:
;
;     	See documentation for BSPOT.PRO.    
;
;*SYSTEM VARIABLES USED:
;
;     	!err	-   !err = 48, then exits program immediately
;		-   !err not equal 67 or not equal to 99 (C not hit)
;		         then the program runs
;
;*INTERACTIVE INPUT:
;
;    	Positioning of cursor - see documentation for BSPOT.PRO 
; 
;*SUBROUTINES CALLED:
;
;	PARCHECK
;     	TABINV
;
;*FILES USED: 
;
;     	Those established by BSPOT.PRO
;
;*SIDE EFFECTS:
;
;*NOTES:
;
;     	MODE -  used to key interpolation step:
;          0 = use nearest good data points for interpolation.
;          1 = use cursor (horizontal) to set flux level.
;
;       For IUESIPS, an eps is set to -210 for a user flagged point and -325
;       for linear interpolation.
;
;       For NEWSIPS, a nu flag is set to -32 (bit 6).
;
;*PROCEDURE:
;
;     	User's input (screen positions and/or character) are read, the
;     	subscripts for the area specified are computed, user flagging is
;     	deleted if user typed X, linear interpolation over bad regions 
;     	performed using nearest good points (MODE = 0) or cursor Y positions
;     	(MODE = non-zero), and data quality EPS vector updated.
;
;*I_HELP nn:
;
;*MODIFICATION HISTORY:
;
;     	30-Jan-83   Programmer R.J. Panek
;     	26-Apr-88   HAA add RDAF Prolog
;       jan-10-90 jtb @gsfc modified for unix / sun idl version 1.2
;	jul-22-91 pjl @gsfc cleaned up; added npar equals 0 print and
;			    PARCHECK; tested on SUN and VAX; updated prolog
;     	See BSPOT.PRO modification history for futher details     
;        6 Apr 94  PJL  added newsips parameter
;
;-
;******************************************************************************
 pro bs_flag,w,f,e,inx,nflag,mode,er1,newsips
;
 npar = n_params(0)
 if (npar eq 0) then begin
    print,'BS_FLAG,WAVE,FLUX,EPS,INX,NFLAG,MODE,ER1,NEWSIPS'
    retall
 endif  ; npar eq 0
 parcheck,npar,8,'BS_FLAG'
;
 iw = fltarr(2)
;
;  read users input (screen positions and/or character)
;
 cursor,dw1,df1,1,/data         ; 1st cursor position
 flush = get_kbrd(0)
 c_err = !err
 er1 = !err
 print,string(7b)             ; ring bell
;
 if (c_err eq 48) then begin     ; abort if 0 hit
    print,'Finished. ACTION: Exiting'
    retall
 endif  ; if user hit 0 to exit
;
;  compute subscripts for area specified
;
 if ( (c_err ne 67) and (c_err ne 99) ) then begin    ; c not hit
    cursor,dw2,df2,1,/data       ; get 2nd cursor position
    flush = get_kbrd(0)
    print,string(7b)           ; and ring bell
    tabinv,w,dw1,iwx
    iw(0) = iwx
    tabinv,w,dw2,iwx
    iw(1) = iwx
    iw1 = fix(min(iw)+0.5)
    iw2 = fix(max(iw)+0.5)
;
;  delete user flagging (if user typed x)
;
    if ( (er1 eq 88) or (er1 eq 120) ) then begin  ; x hit
       if (newsips) then begin
          for j=iw1,iw2 do if ( (abs(e(j)) and 32) ne 0 ) then e(j) = e(j) + 32
       endif else begin
          for j=iw1,iw2 do if ( (e(j) eq -325) or (e(j) eq -210) ) then   $
             e(j) = 100
       endelse ; newsips
       nflag = 1
    endif  ; !err eq 88 loop
;
;  delete all flagging in region (including IUESIPS)
;
    if ( (er1 eq 82) or (er1 eq 114) ) then begin    ; r hit
       if (newsips) then begin
          for i=iw1,iw2 do if (e(i) lt 0) then e(i) = 0
       endif else begin
          for i=iw1,iw2 do if (e(i) lt 0) then e(i) = abs(e(i))/10.
       endelse ; newsips
       nflag = 1
    endif  ; !err eq 82 loop
;
;  perform linear interpolation over bad region. use nearest good
;  points (mode=0) or cursor y positions (mode non-zero)
;
    if ( (er1 eq 73) or (er1 eq 105) ) then begin       ; i entered       
       if (mode eq 0) then begin                     ; user flag
          s = (f(iw2+1)-f(iw1-1))/(w(iw2+1)-w(iw1-1))  ; nearest good points
          for i=iw1,iw2 do f(i) = f(iw1-1) + s*(w(i)-w(iw1-1))
       endif else begin
          s = (df2-df1)/(w(iw2)-w(iw1))           ; cursor y positions
          for i=iw1,iw2 do f(i) = df1 + s*(w(i)-w(iw1))
       endelse  ; mode
;
;  flag points
;
       if (newsips) then begin
          for j=iw1,iw2 do if ( (abs(e(j)) and 32) eq 0 ) then e(j) = e(j) - 32
       endif else e(iw1:iw2) = -210
       nflag = 1
    endif  ; !err eq 73 loop
;    
;  user flagged points, update data quality eps vector
;    
    if (er1 eq 13) then begin                      ; <cr> hit
       if (newsips) then begin
          for j=iw1,iw2 do if ( (abs(e(j)) and 32) eq 0 ) then e(j) = e(j) - 32
       endif else begin
          for j = iw1,iw2 do if (e(j) gt 0) then e(j) = -325
       endelse  ; newsips
       nflag = 1       
    endif  ; <cr> entered.
 endif  ; c not entered.
;
 return
 end  ; bs_flag