;+
;     16-Aug-2000  DG
;       Renamed PLOTERR (IDL version) to OVSA_PLOTERR, and copied to
;       OVSA tree, then changed PLOTERR calls to OVSA_PLOTERR, to avoid
;       conflict with SSW's PLOTERR.
;-

pro calphz,file1,file2
;   openu,outlun,/get_lun,'c:\temp\basfit.out',/append

   ; Read the linear feed amphit file data for baseline 12
   in = {hed:strarr(3),data:fltarr(6,43)}
   openr,lun,/get_lun,file1
   readf,lun,in
   free_lun,lun
   idat = in.data

   rlidat = fltarr(6,43,3)

   ; Read the R, L, and I feed amphit file data for baseline 12
   openr,lun,/get_lun,file2
   for i = 0, 2 do begin
      readf,lun,in
      rlidat[*,*,i] = in.data
   endfor
   free_lun,lun

   p = ((reform(rlidat[3,*,2] - idat[3,*]) + 18180.) mod 360.) - 180.
   f = reform(idat[0,*])
   nf = n_elements(f)
   sn = reform(idat[1,*]/idat[2,*])
   dp = (1/(sn*!dtor))>5.  ; Delta phase, but make at least 5 degrees)

   junk = ''
   manual = 1
   window,0,xsiz=450,ysiz=600
   junk = ''
   outstr = ''
   manual = 0
   nit = 0
   ap = fltarr(nf)
again:
   nit = nit + 1
   res = linfit(f,p+ap*360,sdev=dp,chi=chi,sigma=sigma)
;         res2 = linfit(f,p+ap*360,sdev=dp,chi=chi2)

   if (manual) then begin
      ovsa_ploterr,f,p+ap*360.,dp,psym=-2,xran=[0,20],xsty=1,$
              tit=' Chisq = '+string(chi,format='(f7.1)')
      oplot,[0,f,20],res(0)+res(1)*[0,f,20]
;      oplot,[0,f,12],res2(0)+res2(1)*[0,f,12],linesty=2
      read,'Adjustments (all 0=>cont): ',ix,dx
      x = x*0
      x[ix:nf-1] = dx
      adj = where(x ne 0,nadj)
      ap = ap + x
      if (nadj gt 0) then goto, again
   endif else begin
      x = intarr(nf)
      diff = [p+ap*360,0] - [0,p+ap*360]

      ; See if we need to lower any phases
      ilower = where(diff[0:nf-1] gt 180,nlower)
      if (nlower ne 0) then begin
         ; Yes, we have to lower at least one, so lower first
         x[ilower[0]:nf-1] = -1
      endif else begin
         ; We did not have to lower any, so see if we
         ; have to raise any.
         iraise = where(diff[0:nf-1] lt -180,nraise)
         if (nraise ne 0) then x[iraise[0]:nf-1] = 1
      endelse
      adj = where(x ne 0,nadj)
      ap = ap + x
      ovsa_ploterr,f,p+ap*360.,dp,psym=-2,xran=[0,20],xsty=1,$
              tit=' Chisq = '+string(chi,format='(f7.1)')
      if (nadj gt 0 and nit lt 20) then goto, again
      ovsa_ploterr,f,p+ap*360.,dp,psym=-2,xran=[0,20],xsty=1,$
              tit=' Chisq = '+string(chi,format='(f7.1)')
      oplot,[0,f,20],res(0)+res(1)*[0,f,20]
;      oplot,[0,f,12],res2(0)+res2(1)*[0,f,12],linesty=2
;            read,'CR to continue: ',junk
      manual = 1
      ; If excessive chi-square, ask for manual verification
      if (chi gt 50) then goto, again
   endelse
;         print,strmid(file,8,5),tim,res[0]+res[1],chi,'     '+bl(i),format='(A5,f8.0,f8.1,f8.0,A7)'
   outstr = outstr+string(res[0],res[1],chi,format='(2f7.1,f6.0)')
;      printf,outlun,strmid(file,8,5),tim,outstr,format='(A5,f8.0,A)'
      print,outstr,format='(A)'
;   free_lun,outlun
return
end
