;+
; NAME:
;     SHOW_OSC
; PURPOSE:
;     Gives a graphical display of phase locks for a given antenna/oscillator,
;     finds the locks, and returns the tuning current list for all harmonics
;     of the oscillator.
; CATEGORY:
;     OVRO APC DIAGNOSTIC
; CALLING SEQUENCE:
;     tcmid = show_osc(iosc,iant,tc_old,nn_old,fctemp,qual)
; INPUTS:
;     iosc     The oscillator number for which the output should be
;                generated.  IOSC = 0,1,2 for Lo, Mid, Hi oscillator.
;     iant     The antenna number of the antenna for which the output should
;                be generated.  This is the ordinal (index) number 0-4.
;     nn_old   The nomtune harmonic table as contained in the NOMTUNE.FTH file.
;                The array size is NN_OLD(nant,nosc*3).
;     tc_old   The tuning current table as contained in the NOMTUNE.FTH file.
;                The array size is TC_OLD(nant,nosc*3).
;     fctemp   The entire FCAL data array of size FCTEMP(nosc,nant,4096)
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     nint
; OUTPUTS:
;     tcmid    The tuning current list corresponding to the middle of the lock
;                for each harmonic of the oscillator.  Missing locks are flagged
;                with NaN.
;     qual     The quality of the phase locks, expressed as array of found
;                locks.
; COMMENTS:
; SIDE EFFECTS:
;     A single plot box is produced.
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 14-Mar-1999 by Dale E. Gary
;     06-Apr-1999  DG
;       Slight change to QUAL, to return array of indexes of found locks
;       rather than just the number of found locks, for better diagnostics.
;     14-Jan-2001  DG
;       Significant modification to use new style of fit
;     12-Jan-2004  DG
;       Fix problem with #8 V/F, which is showing a 1 instead of OOL in the data.
;       It may be a stuck lower-order bit, but I simply treat 1 as NaN in FCDATA
;       below, and all is copacetic (sp?)
;     15-Feb-2004  DG
;       #8 V/F is a "stuck" bit, but it is now being squelched in FASAR.  It will
;       soon be fixed in hardware.  In any case, the above changes are now
;       commented out.
;-
function show_osc,iosc,iant,tc_old,fctemp,qual

   hmaxtab = [12,40,90]
   hmintab = [ 5,13,41]
   ; Max and min harmonics for this oscillator, and the total number of harmonics
   hmax = hmaxtab[iosc]
   hmin = hmintab[iosc]
   nharm = hmax-hmin+1
   h = dindgen(nharm)
   sclfac = 10^[-1D,-3D,-5D,-6D,-8D]

   ; The nominal slope, rate of change of tuning current with harmonic number
   didh = tc_old(iant,1,iosc)*sclfac[1]
   ; The width of the data to plot for each harmonic
   ididh = nint(didh)
   ; Make sure ididh is even
   if (ididh mod 2) eq 1 then ididh = ididh+1

   ; Get the full list of frequencies and tuning currents corresponding
   ; to the table entry
   ftab2tc,iant,iosc,tc_old,f,tc

   ; Some plot parameters
   yran = [hmin,hmax+1]
   ; Plot the (empty) plot box
   plot,[0,ididh]-ididh/2,yran,ysty=1,xsty=1,/nodata,$
      xmar=[4,2],ymar=[2,2],charsize=1.5
   ; Storage for new determination of tuning current
   tcmid = fltarr(nharm)

   ; Overplot the FCAL data for each harmonic
   for i = 0, nharm-1 do begin
      tcnom = tc[i]             ; Nominal tuning current at harmonic I
      tcmin = tcnom-ididh/2     ; Bottom end of tuning current range to display for this H
      tcmax = tcnom+ididh/2     ; Top end of tuning current range to display for this H

      ; Extract the FCAL data for this tuning current range
      fcdata = reform(fctemp(iosc,iant,tcmin>0:tcmax<4095))

      ; Ant 8 has problems where V/F of 1 is showing up in the data even when OOL
      ; So the next two statements simply set V/F of 1 as NaN
      ;zero = where(fcdata le 1,nzero)
      ;if (nzero gt 0) then fcdata[zero] = !values.f_nan

      ; Plot the extracted FCDATA for this range of tuning currents, moving up one
      ; unit in the plot for each harmonic.
      oplot,indgen(ididh+1)-ididh/2,fcdata/15.+i+hmin
      ; Overplot a vertical zero line
      oplot,[0,0],[yran],color=255

      ; Find the middle of the lock nearest the nominal lock point
      tcmid(i) = findlock(fcdata)

      ; Flag any locks that are too far from the center, defined as +/- 12% of didh
      if (abs(tcmid(i)) gt ididh/8) then tcmid(i) = !values.f_nan
   endfor

   rpt = 0
   ; Do a little cleanup to eliminate values that are too far from the average
again:
   if (rpt le 2) then begin
      finidx = where(finite(tcmid),nfin)
      if (nfin ge 2) then begin
         mom = moment(tcmid,/nan)
         bad = where(abs(tcmid - mom(0)) gt sqrt(mom(1))*3.0,nbad)
         if (nbad ne 0) then begin
            ; Some points were found to be bad, but check to see if they are
            ; already NaN
            good = where(finite(tcmid(bad)),ngood)
            if (ngood gt 0) then begin
               ; At least one was not already NaN, so flag it NaN and redo the
               ; average to see if we got all of the bad points
               tcmid(bad) = !values.f_nan
               rpt = rpt + 1
               goto,again
            endif
         endif
      endif
   endif

   ; Add nominal tuning current
   tcmid = tcmid + tc
   good = where(finite(tcmid),ngood)
   xyouts,0.0,hmax+1+nharm/20.,align=0.5,string(ngood,nharm,$
            format='(I2,"/",I2," good locks")')
   qual = good

return,tcmid
end
