;+
; NAME:
;     ADJ2MCAL
; PURPOSE:
;     Adjusts the amplitude calibration factors on the 2m antennas by
;     comparing with the total power calibration.  The adjustment is
;     to make the calibration factors smoother, without changing the
;     absolute calibration.
; CATEGORY:
;     OVRO APC DATA CALIBRATION
; CALLING SEQUENCE:
;     adj2mcal,ampcal,tpcal,tpupd
; 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:
; ROUTINES CALLED:
; OUTPUTS:
;     ampcal    On output, contains the same values as on input,
;                 except the 2m amplitude calibration factors are
;                 adjusted.
; COMMENTS:
;     A plot of the calibrator comparison can be obtained by a
;     call to COMPARE_CAL2, with identical arguments.
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written  31-Dec-2000 by Dale E. Gary
;     03-Mar-2001  DG
;       Fixed bug that occurred when no total power update was
;       available.
;     29-Mar-2001  DG
;       Accidentally applied correction to Ant 2 I feed.  Changed
;       feed loop to start at 4 instead of 3, so that this doesn't
;       happen.
;     19-Jun-2007  DG
;       Fixed to apply correction to both RCP and LCP (was only doing
;       RCP/I).  This "should" work for older data.
;-
pro adj2mcal,ampcal,tpcal,tpupd

    ampfac = ampcal.factors
    ; Flag zero TPUPD values with NaN
    tpupdfac = tpupd.tpfac
    bad = where(tpupdfac eq 0,nbad)
    if (nbad ne 0) then tpupdfac[bad] = !values.f_nan

    ; Apply update factors to total power channels only
    tpfac  = tpcal.tpfac*tpupdfac
    nfeed = n_elements(ampfac[*,0])
    ; Loop over feeds.
    npol = n_elements(ampfac[0,0,*])
    for j = 0, npol-1 do begin
       for i = 4, nfeed-1 do begin
          ; Make the adjustment in the log--first take log of AMPFAC and TPFAC
          ampf = alog10(reform(ampfac[i,*,j]))
          tpf  = alog10(reform(sqrt(tpfac[i,*,j])))
          ; Determine the differences as a function of frequency
          diff = ampf - tpf
          good = where(finite(diff),ngood)
          if (ngood ne 0) then begin
             ; There are at least some good points, so determine the average
             ; (in the log) differences and adjust the total power factor for
             ; these differences.  This ensures that the absolute calibration
             ; is not affected by the adjustment.
             avgdif = total(diff,/nan)/total(finite(diff))

             ; Note that AMPFAC is defined such that the appropriate factor
             ; for a baseline is A_i*A_j, so total power and amplitude factors
             ; differ by square-root.
             tpadj = reform(sqrt(tpfac[i,*,j])*10^avgdif)

             ; Correct the amplitude calibration factors for this small
             ; antenna feed, by simply replacing the amplitude factors
             ; with the adjusted total power factors.
             good = where(finite(ampcal.factors[i,*,j]),ngood)
             if (ngood ne 0) then ampcal.factors[i,good,j] = tpadj[good]
          endif
       endfor
    endfor
return
end