pro compare_cal,refcal,printer=printer

    ; Given a refcal structure, 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 geometric mean
    ; of the total power factors
    fac = (*refcal.pfactors)
    nant = refcal.nant
    ; Apply update factors to total power channels only
    fac[*,0:nant-1,*] = fac[*,0:nant-1,*]*(*refcal.pfacupd)[*,0:nant-1,*]
    xlat = [ 0,16,15,14,13,$
             0, 0,11,10, 9,$
             0, 0, 0, 6, 5,$
             0, 0, 0, 0, 1,$
             0, 0, 0, 0, 0]
    if (keyword_set(printer)) then set_plot,'printer'
    polstr = ['RCP','LCP','Stokes I']
    antstr = ['1','2','4','5','6','7']
    for i = 0, refcal.npol-1 do begin
       if (keyword_set(printer)) then begin
          device,/inch,xsiz=7.5,ysiz=7.5,xoff=0.5,yoff=1.75
       endif else begin
          window,i,ysiz=600
       endelse
       k = nant-1
       xyouts,/norm,0.1,0.3,polstr[i],charsize=2
       plots,/norm,0.1,0.2,psym=4
       plots,/norm,[0.1,0.15],[0.15,0.15]
       xyouts,/norm,0.16,0.2,'= Baseline Amplitude cal factors'
       xyouts,/norm,0.16,0.15,'= Total-Power-derived cal factors'
       for j1 = 0, nant-2 do begin
          for j2 = j1+1, nant-1 do begin
             k = k + 1
             !p.multi = [xlat[j1*nant + j2],refcal.nant-1,refcal.nant-1]
             ampfac = alog10(reform(fac[i,k,*]))
             tpfac = alog10(reform(sqrt(fac[i,j1,*]*fac[i,j2,*])))
             diff = ampfac-tpfac
             good = where(finite(diff),ngood)
             if (ngood ne 0) then avgdif = total(diff,/nan)/total(finite(diff)) else avgdif = 0
             plot_oo,*refcal.pfrq,fac[i,k,*],psym=4,xran=[1,20],xsty=1
             oplot,*refcal.pfrq,sqrt(fac[i,j1,*]*fac[i,j2,*])*10^avgdif,psym=10,thick=3
             xyouts,1.5,fac[i,k,5]/10.,'BL '+antstr[j1]+'-'+antstr[j2]
          endfor
       endfor
       if (keyword_set(printer)) then device,/close
    endfor
    if (keyword_set(printer)) then set_plot,'win
return
en