;+
; NAME:
;     FLAGGER
; PURPOSE:
;     Routine to allow interactive flagging of data based on amplitude or
;     phase, using the mouse.  Data selected for flagging are replaced with
;     NaN.
; CATEGORY:
;     OVRO APC DATA CALIBRATION
; CALLING SEQUENCE:
;     out = flagger(frq,avg)
; INPUTS:
;     frq      Array of frequencies contained in the data [GHz]
;     avg      Data array of size (NCHAN,NFRQ,NMEAS), where NCHAN is the
;                number of channels, NFRQ is the number of frequencies,
;                and NMEAS is the number of measurements (times).
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
;     out      The output data array parallel to AVG, with flagged values set
;                to NaN.
; COMMENTS:
;     A plot is shown for each baseline/frequency.  Click on a point to
;     flag it (the point is removed and the plot is redrawn).  Click at
;     any level on the left side of the plot (outside the plot box) to
;     flag all points above that level (amplitude).  Click outside the
;     plot box on the right side of the plot to advance to the next frequency
;     for the current baseline, or to the next baseline if at the last
;     frequency.  The current baseline/frequency combination is shown in
;     the title.
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 25-Mar-2000 by Dale E. Gary
;-

function flagger,frq,avg

   ; Determine which baselines are present, and the corresponding channels
   nchan = n_elements(avg[*,0,0])
   if (nchan eq 25) then begin
      bl_list = [12,14,15,16,24,25,26]
      c_list =  [ 5, 7, 9,11,13,15,17]
   endif else if (nchan eq 36) then begin
      bl_list = [12,14,15,16,17,24,25,26,27]
      c_list =  [ 6, 8,10,12,14,16,18,20,22]
   endif else begin
      print,'This routine only works for 5 or 6 antennas at present.'
      return,0
   endelse

   nf = n_elements(frq)
   ; Work with a copy of the input data
   out = avg

   ; Loop over baselines
   for i = 0, n_elements(bl_list)-1 do begin
      ; Loop over frequencies
      for j = 0, nf-1 do begin
         ; Generate amplitude array for the current baseline/frequency
         amp = reform(sqrt(avg[c_list[i],j,*]^2 + avg[c_list[i]+1,j,*]^2))
         ;pha = (reform(atan(avg[c_list[i],j,*],avg[c_list[i]+1,j,*]))/!dtor + 360) mod 360
         x = 0

         ; Make sure there are at least two good (non-NaN) points
         good = where(finite(amp),ngood)

         ; Do the following loop until the x-coordinate is outside (to right
         ; of) the plot box.  When user clicks to right of plot box, loop is
         ; exited and the next baseline/frequency is displayed.
         while(x lt n_elements(amp) and ngood gt 1) do begin
            ; plot the amplitudes for this baseine/frequency
            plot,amp,psym=1,tit='Baseline '+string(bl_list[i],frq[j],format='(I2," Freq ",F4.1," GHz")')

            ; Get the cursor position selected by the user
            cursor,x,y,/up
            if (x gt 0 and x lt n_elements(amp)) then begin
               ; x-coordinate was within the data, so we are in single point
               ; flagging mode.  Find the nearest index and flag the point.
               idx = nint(x)
               amp[idx] = !values.f_nan
               out(c_list[i],j,idx) = !values.f_nan
               out(c_list[i]+1,j,idx) = !values.f_nan
               good = where(finite(amp),ngood)
            endif else if (x lt 0) then begin
               ; x-coordinate was to left of plot box, so we are in amplitude
               ; flagging mode.  Use y-coordinate to determine upper limit to
               ; amplitude, and flag all points with greater amplitude.
               ymax = y
               bad = where(amp gt y,nbad)
               if (nbad gt 0) then begin
                  out(c_list[i],j,bad) = !values.f_nan
                  out(c_list[i]+1,j,bad) = !values.f_nan
                  amp[bad] = !values.f_nan
               endif
               good = where(finite(amp),ngood)
            endif
         endwhile
      endfor
   endfor

return,out
end
