;+
; NAME:
;     COMPARE_CAL2
; PURPOSE:
;     Compares the Total Power (plotted with histogram) and baseline
;     amplitude-derived (plotted with symbols) calibration factors,
;     using the data contained in the AMPCAL, TPCAL, and TPUPD structures.
;     Differs from COMPARE_CAL by comparing the feed-based factors
;     instead of baseline-based factors
; CATEGORY:
;     OVRO APC DATA CALIBRATION
; CALLING SEQUENCE:
;     compare_cal2,ampcal,tpcal,tpupd[,/printer]
; INPUTS:
;     ampcal    An instance of an AMPCAL structure, as returned by
;                 get_ampcal_struct.
;     tpcal     An instance of a TPCAL structure, as returned by
;                 get_tpcal_struct.
;     tpupd     An instance of a TPUPD structure, as returned by
;                 get_tpupd_struct.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     printer   A switch that, if set, causes the output to be sent to
;                 the default printer.  Otherwise, three plot windows
;                 are used.
; ROUTINES CALLED:
; OUTPUTS:
; COMMENTS:
; SIDE EFFECTS:
;     Three plots are created.
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written  31-Dec-2000 by Dale E. Gary
;-
pro compare_cal2,ampcal,tpcal,tpupd,printer=printer

    ; Given this set of structures, make a plot of comparisons of total power
    ; and amplitude calibration factors.  Naively, one would expect the
    ; amplitude calibration factors to be the same as the total power factors.
    ampfac = ampcal.factors
    tpupdfac = tpupd.tpfac
    tpupdfac[where(tpupdfac eq 0)] = !values.f_nan
    ; Apply update factors to total power channels only
    tpfac  = tpcal.tpfac*tpupdfac
    nfeed = n_elements(ampfac[*,0])

    if (keyword_set(printer)) then set_plot,'printer'
    srcstr = ['I1','L1','R2','I2','I4','I5','I6','I7','I8','I9']
    if (keyword_set(printer)) then begin
       device,/inch,xsiz=7.5,ysiz=7.5,xoff=0.5,yoff=1.75
    endif else begin
       window,0,ysiz=600
    endelse
    !p.multi=[12,4,3,0,0]
    plots,/norm,0.6,0.3,psym=4
    plots,/norm,[0.6,0.65],[0.25,0.25]
    xyouts,/norm,0.66,0.3,'= Amplitude cal factors'
    xyouts,/norm,0.66,0.25,'= Total Power cal factors'
    for i = 0, nfeed-1 do begin
       ampf = alog10(reform(ampfac[i,*]))
       tpf  = alog10(reform(sqrt(tpfac[i,*])))
       diff = ampf - tpf
       good = where(finite(diff),ngood)
       if (ngood ne 0) then begin
          avgdif = total(diff,/nan)/total(finite(diff))
          plot_oo,indgen(86)*0.2+1.0,ampfac[i,*],psym=4,xran=[1,20],xsty=1
          tpadj = reform(sqrt(tpfac[i,*])*10^avgdif)
          oplot,indgen(86)*0.2+1.0,tpadj,psym=10,thick=3
          xyouts,1.5,ampfac[i,12]/5.,'Feed '+srcstr[i]
          if (i gt 3) then begin
             ; Actually correct the amplitude calibration factors for the
             ; small antennas
             good = where(finite(ampcal.factors[i,*]),ngood)
             if (ngood ne 0) then ampcal.factors[i,good] = tpadj[good]
          endif else begin
             ; And adjust outlier (abrupt changes by greater than factor of 2)
             ; amplitude calibration factors for the 27-m feeds
             blah = 0
          endelse
       endif
    endfor
    if (keyword_set(printer)) then begin
       device,/close
       set_plot,'win
    endif
return
end