pro plot_vis2vis, t, uv_ij, vis_obs, vis_mdl, tplt=tplt

vis0_ij	= vis_obs
vis_ij	= vis_mdl

big_tit ='I M A G R / V i s 2 v i s'

nt	=n_elements(t)
nb	=n_elements(vis_ij[0,*])
ntf	=n_elements(vis_ij[*,0])
; therefore
nf	=ntf/nt

;-choose whether to plot visibility vs. time or visibility vs. uv
if NOT keyword_set(tplt) then begin
   tplt=1
   if (nt lt nf) then tplt = 0
endif

; To handle multi-frequencies, better go back to the original set
uv   = complexarr(nt,nb,nf)
vis  = complexarr(nt,nb,nf)
vis_ = complexarr(nt,nb,nf)

for k=0,nf-1 do begin
   i0=k*nt & i1=i0+(nt-1)
   uv[*,*,k] = uv_ij[i0:i1,*]
   vis[*,*,k] = vis0_ij[i0:i1,*]
   vis_[*,*,k] = vis_ij[i0:i1,*]
endfor

;------Now will sort out the quantities for plot
;------depending on whether we want time-plot or uv-plot.
;------x axis will be 1-dim arr with size, nx
;------y axis will take a form of arr(nx,nscatt,nrow)
;------where nscatt means scatter points in y-axis,
;------and nrow=3 at presnt setup.

case tplt of
1 : begin
;-------------------------------------------------
; time-plot: this is simple because
; 	x=t(nt), y,z=vis(nt,nf,nb_selected)
;-------------------------------------------------

bsel=[0,1,2] ; plots 1-2, 1-4 & 1-5 bsls only

nrow	=3
nx  	=nt
nscatt	=nf
	x = fltarr(nx,nrow)
	y = complexarr(nx,nscatt,nrow)
	z = complexarr(nx,nscatt,nrow)
    for m=0,nrow-1 do begin
	x[*,m]=t
      for k=0, nscatt-1 do begin
;	y[*,k,m]=reform(vis[*,bsel[m],k],nx)
;	z[*,k,m]=reform(vis_[*,bsel[m],k],nx)
for i=0,nt-1 do y[i,k,m]=vis[i,bsel[m],k]
for i=0,nt-1 do	z[i,k,m]=vis_[i,bsel[m],k]

      endfor
    endfor
	xtit='time [UT]'
	ytit='Visibility [s.f.u]'
	tit=['V_Real (1-2 baseline)','V_Imaginary (1-2 baseline)', $
	     'V_Real (1-4 baseline)','V_Imaginary (1-4 baseline)', $
	     'V_Real (1-5 baseline)','V_Imaginary (1-5 baseline)']
	titls=[xtit,ytit,tit]
end
0 : begin
;-------------------------------------------------
; uv-plot: this is a bit complicated
;	since
; x=uv(slectd bsl * all freq.), independent of time, although
; it does depend on time, we ignore, so
	ntcen=nt/2
; y,z=vis(nf*nbsel,nscatter, nrow), nrow consider 3 combi. of ant's.
	nbsel=3 ; as chosen below
nrow =3
nbsel=2
nscatt=nt
nx = nf * nbsel
	x = fltarr(nx,nrow)
	y = complexarr(nx,nscatt,nrow)
	z = complexarr(nx,nscatt,nrow)
    for m=0,nrow-1 do begin
if m eq 0 then bsel=[1,0] ; 1-4, 1-2 / order of uv distance
if m eq 1 then bsel=[2,3] ; 1-5, 1-6 
if m eq 2 then bsel=[0,4] ; 2-1, 2-4
     x[*,m]=reform(abs(uv[ntcen,bsel,*]),nx)
      for k=0, nscatt-1 do begin
	y[*,k,m]=reform(vis[k,bsel,*],nx)
	z[*,k,m]=reform(vis_[k,bsel,*],nx)
      endfor
    endfor

; in this case you need to sort out uv distance
    for m=0,nrow-1 do begin
	dum=reform(x[*,m]) & sor=sort(dum)
    x[*,m]=dum[sor]
      for k=0, nscatt-1 do begin
	  dum=reform(y[*,k,m]) & y[*,k,m]=dum(sor)
      dum=reform(z[*,k,m]) & z[*,k,m]=dum(sor)
      endfor
    endfor

	xtit='uv [1/rad]'
	ytit='Visibility [s.f.u]'
	tit=['V_Real (1-2-4 baselines)','V_Imaginary (1-2-4)', $
    	     'V_Real (1-5-6 baselines)','V_Imaginary (1-5-6)', $
    	     'V_Real (2-1,5,4 baselines)','V_Imaginary (2-1,5,4)']
	titls=[xtit,ytit,tit]
end
endcase

;------------------------window size/pos stuff
xwt 	=695 & ywt =460
xw  	=300 & yw  =100
xpos	=[50,  50, 50]
ypos	=[30+280, 30+140, 30]
tpos	=[200,435]
gost	=intarr(51,51) & gost[0,0]=255 & gost=bytscl(gost)

;------------------------ plot

window,3,xs=xwt, ys=ywt
device,decompo=0
!p.multi=[0,2,3]
!p.charsize=1.5
!p.font=-1

;-----need to set some helping parameters
nscatt=n_elements(y[0,*,0])
nscen =nscatt/2
notik=[' ',' ',' ',' ',' ']

for m=0,2 do begin
;yr=[-1,1]*max([abs(y[*,*,m]),abs(z[*,*,m])])

 xp0=xpos[m] & xp1=xp0+xw & yp0=ypos[m] & yp1=yp0+yw
 ymin=min(float(y[*,nscen,m]))
 ymax=max(float(y[*,nscen,m])) 
 yde=(ymax-ymin)*0.05 & ymin=ymin-yde & ymax=ymax+yde
  plot,x[*,m],float(y[*,nscen,m]), $
    	pos=[xp0,yp0,xp1,yp1],/dev, psym=10, $
	xtit=titls[0],ytit=titls[1],tit=titls[2+m*2],$
	xst=1,yst=1,yr=[ymin,ymax],yticks=4
  oplot,x[*,m],float(z[*,nscen,m]), thick=2
;  for k=0,nscatt-1 do begin
;  oplot,x[*,m],float(y[*,k,m]),psym=1,symsize=0.5
;  oplot,x[*,m],float(z[*,k,m]),psym=4,symsize=0.5
;  endfor

 xp0=xp1+2+35 & xp1=xp0+xw
 ymin=min(imaginary(y[*,nscen,m]))
 ymax=max(imaginary(y[*,nscen,m]))
 yde=(ymax-ymin)*0.05 & ymin=ymin-yde & ymax=ymax+yde
  plot,x[*,m],imaginary(y[*,nscen,m]), $
    	pos=[xp0,yp0,xp1,yp1],/dev, $
	xtit=titls[0],tit=titls[2+m*2+1], psym=10, $
	yticks=4,xst=1,yst=1,yr=[ymin,ymax]
  oplot,x[*,m],imaginary(z[*,nscen,m]), thick=2
;  for k=0,nscatt-1 do begin
;  oplot,x[*,m],imaginary(y[*,k,m]),psym=1,symsize=0.5
;  oplot,x[*,m],imaginary(z[*,k,m]),psym=4,symsize=0.5
;  endfor
endfor

xyouts,tpos[0],tpos[1],big_tit,charsiz=2,charthic=2,/device

return
end
