;+
; NAME:
;     plot_pcal
; PURPOSE:
;     Plot the results of pcal.pro.
; CATEGORY:
;     scbong OVRO
; CALLING SEQUENCE:
;     plot_pcal, filename[, antset = antset]
; INPUTS:
;     filename   File name saved by pcal.pro. If not given, 'pcal.sav' will be used.
;     antset     Antenna numbers of sets to be shown.
;                This is [3, nset] dimensional array where nset is the number of sets.
;                A set consists of three antennas which should satisfy the phase closure condition
;                In the phase closure condition, for any three antennas a, b, c
;                in increasing antenna number order, ab - ac + bc = 0
;                where ab is the phase of baseline a-b and so on.
;                The default value is [[1, 2, 4], [1, 2, 5], [1, 2, 6]].
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     combine    Set this keyword to combine each baseline data over time.
;                A new window is opened and each plot contains fitting lines or summed lines.
; ROUTINES CALLED:
;     sec2hms
; OUTPUTS:
;
; COMMENTS:
;     plot_pcal will show the same number of windows as that of files used in pcal.
;     A window has 0 marked points which are the points of 0 phase fold.
;     In the last row plots, three dashed lines are fitted lines of
;     each labeled baseline index and the solid line is the sum of them.
;     dashed lines and the data points are flipped if the sign next to the label is '-',
;     so that the sum of them becomes 0 according to the phase closure condition.
;     If time keyword is set, a window is opened and
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 24-Jan-2000 by Su-Chan Bong
;-
pro plot_pcal, filename_, antset = antset_, combine = combine
   if keyword_set(filename_) then filename = filename_ else filename = 'pcal.sav'
   if keyword_set(antset_) then antset = antset_ else antset = [[1, 2, 4], [1, 2, 5], [1, 2, 6]]

   ;Restore the results. ttab, atab, btab, ptab, f, pfold are restored.
   restore, filename

   sptab = size(ptab)
   ;nbl = nant(nant - 1) / 2 => nant^2 - nant - 2nbl = 0
   nant = (1 + fix(sqrt(1 + 8 * sptab[1]))) / 2
   nant1 = nant - 1
   nfq = sptab[2]
   nfq1 = nfq - 1
   if sptab[0] eq 2 then nfl = 1 else nfl = sptab[3]
   nfl1 = nfl - 1
   nset = n_elements(antset[0, *])
   nset1 = nset - 1

   ;Check antset and convert it to antenna indice.
   anti0 = intarr(3)
   anti = intarr(3, nset)
   for iset = 0, nset1 do begin
      for iant = 0, 2 do begin
         antn = antset[iant, iset]
         if antn lt 3 then anti0[iant] = antn - 1 else anti0[iant] = antn - 2
      endfor
      if n_elements(uniq(anti0)) ne 3 then begin
         print, 'Duplicate antennas in a set.'
         return
      endif
      anti[*, iset] = anti0[sort(anti0)]
   endfor
   dummy = where(antset eq 3, n3)
   dummy = where(anti lt 0 or anti ge nant, nout)
   if n3 + nout gt 0 then begin
      print, 'Invalid antenna number.'
      return
   endif

   ;From the antenna indices obtain baseline indice.
   ibl = intarr(3, nset)
   k = 0
   for i = 0, 1 do for j = i + 1, 2 do begin
      ;ibl = sum(l = 0 to antii)(nant - l - 1) - nant + antij
      antii = anti[i, *]
      antii1 = antii + 1
      ibl[k, *] = nant1 * (antii1) - antii * (antii1) / 2 - nant + anti[j, *]
      k = k + 1
   endfor

   ;Plot data, fitting lines and sums.
   suma = fltarr(nset, nfl)
   sumb = suma
   sump = fltarr(nset, nfq, nfl)
   pfit = reform(ptab[ibl, *, *] + 360. * pfold[ibl, *, *], 3, nset, nfq, nfl)
   sign = [1., -1., 1.]
   for k = 0, 2 do begin
      ibl0 = ibl[k, *]
      suma = suma + sign[k] * atab[ibl0, *]
      sumb = sumb + sign[k] * btab[ibl0, *]
      sump = sump + sign[k] * pfit[k, *, *, *]
   endfor
   wxsize = 420
   wysize = 594
   chscl = [float(!d.x_ch_size) / wxsize, float(!d.y_ch_size) / wysize]
   marg = [10., 4., 3., 4.] * [chscl, chscl]
   pxsize = (1. - marg[2] - marg[0]) / nset
   pysize = (1. - marg[3] - marg[1]) / 4.
   xvec = pxsize * [1., 0., 1., 0.]
   yvec = pysize * [0., 1., 0., 1.]
   pos0 = [marg[0], marg[1], marg[0] + pxsize, marg[1] + pysize]
   pmt = 1. - marg[3] * 1.75 / 4.
   pyt = marg[0] / 5.
   pxt = marg[1] / 4.
   flast = f[nfq1]
   fmax = flast * 1.1
   frange = [0., fmax]
   prange = [-fmax, fmax] * 50.
   iblposx = fmax * 0.1
   iblposy = fmax * 160.
   pfmax = fix(prange[1] / 360.)
   symsize = 3. / pfmax < 1.
   thms = sec2hms(ttab / 1.d3)
   iflstr = 'File ' + strtrim(sindgen(nfl), 1) + $
    string(fix(thms / 10000.), format = '(" (", i2.2, ":")') + $
    string(fix((thms mod 10000.) / 100.), format = '(i2.2, ":")') + $
    string(thms mod 100., format = '(i2.2, ")")')
   spc = strarr(10) + ' '
   iantset = [[0, 0, 1], [1, 2, 2]]
   antstr = strtrim(string(fix(antset)), 1)
   setstr = reform(antstr[0, *] + '-' + antstr[1, *] + '-' + antstr[2, *])
   iblstr = strtrim(string(ibl), 1)
   blstr = 'BL ' + iblstr + ' (' + antstr[iantset[*, 0], *] + '-' + $
    antstr[iantset[*, 1], *] + ')'
   signstr = ['+', '-', '+']
   one = fltarr(nfq) + 1.
   if keyword_set(combine) then begin
      pgtitle = filename + ' (all)'
      window, /free, xsize = wxsize, ysize = wysize, title = filename + ' (all)'
      xyouts, 0.5, pmt, pgtitle, charsize = 1.25, alignment = 0.5, /normal
      xyouts, pyt, 0.5, 'Phase', alignment = 0.5, /normal, orientation = 90.
      xyouts, 0.5, pxt, 'Frequency [GHz]', alignment = 0.5, /normal
      ifl0 = indgen(nfl)
      iflstr = strtrim(sindgen(nfl), 1)
      for iset = 0, nset1 do begin
         if iset eq 0 then ytickname = '' else ytickname = spc
         pos = pos0 + iset * xvec + 3 * yvec
         for k = 0, 2 do begin
            ibl0 = ibl[k, iset]
            flin = atab[ibl0, ifl0] ## one + btab[ibl0, ifl0] ## f
            if k eq 0 then settitle = setstr[iset] else settitle = ''
            plot, f, flin[*, 0], position = pos, /noerase, $
             xrange = frange, yrange = prange, xstyle = 1, ystyle = 1, $
             xtickname = spc, ytickname = ytickname, title = settitle
            if nfl gt 1 then for ifl = 1, nfl1 do oplot, f, flin[*, ifl], linestyle = ifl mod 6
            for ifl = 0, nfl1 do xyouts, flast, flin[nfq1, ifl], iflstr[ifl]
            pos = pos - yvec
         endfor
         plot, f, suma[iset, 0] + sumb[iset, 0] * f, position = pos, /noerase, $
          xrange = frange, yrange = prange, xstyle = 1, ystyle = 1, ytickname = ytickname
         if nfl gt 1 then for ifl = 1, nfl1 do oplot, f, suma[iset, ifl] + sumb[iset, ifl] * f, $
          linestyle = ifl mod 6
         for ifl = 0, nfl1 do xyouts, flast, suma[iset, nfl1] + sumb[iset, nfl1] * f, iflstr[ifl]
      endfor
   endif else for ifl = 0, nfl1 do begin
      window, ifl, xsize = wxsize, ysize = wysize, title = iflstr[ifl]
      xyouts, 0.5, pmt, iflstr[ifl], charsize = 1.25, alignment = 0.5, /normal
      xyouts, pyt, 0.5, 'Phase', alignment = 0.5, /normal, orientation = 90.
      xyouts, 0.5, pxt, 'Frequency [GHz]', alignment = 0.5, /normal
      for iset = 0, nset1 do begin
         if iset eq 0 then ytickname = '' else ytickname = spc
         pos = pos0 + iset * xvec + 3 * yvec
         ibl0 = ibl[*, iset]
         flin = atab[ibl0, ifl] ## one + btab[ibl0, ifl] ## f
         for k = 0, 2 do begin
            ptab0 = ptab[ibl0[k], *, ifl]
            if k eq 0 then settitle = setstr[iset] else settitle = ''
            plot, f, flin[*, k], position = pos, /noerase, $
             xrange = frange, yrange = prange, xstyle = 1, ystyle = 1, $
             xtickname = spc, ytickname = ytickname, title = settitle
            for ipf = -pfmax, pfmax do oplot, f, ptab0 + 360. * ipf, psym = 7, symsize = symsize
            xyouts, f, ptab0, '0', alignment = 0.5, charsize = symsize
            xyouts, iblposx, iblposy, blstr[k, iset]
            pos = pos - yvec
         endfor
         plot, f, suma[iset, ifl] + sumb[iset, ifl] * f, position = pos, /noerase, $
          xrange = frange, yrange = prange, xstyle = 1, ystyle = 1, ytickname = ytickname
         oplot, f, sump[iset, *, ifl], psym = 7, symsize = symsize
         for k = 0, 2 do begin
            oplot, f, sign[k] * flin[*, k], linestyle = 2
            oplot, f, sign[k] * pfit[k, iset, *, ifl], $
             psym = k + 4, symsize = symsize
            xyouts, flast, sign[k] * flin[nfq1, k], iblstr[k, iset] + signstr[k]
         endfor
      endfor
   endfor
end