;+
; NAME:
;     ADJ_PCLOSE
; PURPOSE:
;     Force phase closure in I calibration adjustment data.
; CATEGORY:
;     OVRO APC CALIBRATION
; CALLING SEQUENCE:
;     adjout = adj_pclose(adjin)
; INPUTS:
;     adjin    A float array of size (3*nb,ns) where NB=7 is the number of
;                baselines for which the calibration data were taken, and
;                NS=5 is the number of scans over which the adjustment is
;                to be made.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     pclose
; OUTPUTS:
;     adjout  An exactly parallel array to ADJIN, but with phases corrected
;                for phase closure.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
;     This routine will have to be fixed to work with more than 5
;     antennas (7 baselines)
; MODIFICATION HISTORY:
;     Written 21-Jul-1999 by Dale E. Gary
;-

function adj_pclose,adjin

   adjout = adjin
   ns = n_elements(adjin[0,*])
   indx = indgen(7)*3

   for i = 0, ns-1 do begin

      ; Input phase offset for this scan, one for each baseline
      poffin = reform(adjin[indx,i])
      ; Input phase slope for this scan, one for each baseline
      pslpin = reform(adjin[indx+1,i])

      ; Weights (7-element array), just 1/chi-squared
      w = reform(adjin[indx+2,i])
      bad = where(w eq -99.,nbad)
      if (nbad ne 0) then begin
         poffin(bad) = !values.f_nan
         pslpin(bad) = !values.f_nan
      endif

      ; Correct the phase offset for all of the baselines
      adjout[indx,i] = pclose(poffin,w,cor=Cor,0.)
      adjout[indx+1,i] = pclose(pslpin,w,cor=Cor,0.)
      adjout[indx+2,i] = Cor
   endfor

return,adjout
end