pro mk_aips_input_from, infile, outfile=outfile
; purpose: create input file that can be read into AIPS
;			from outputs of new apc packages.
; Issues:
; A. what variables needed to be saved?
; 	 tavg, avg, uvt, date, scrname, ra, dec, frq for (R, L, I)
; Caution: The baseline listed in this should match those in the analize pgm.
; B. How to process the result here?
;     See "Data processing" section.
; C. Recognize limitations
;     See the "Fixed parameter" section.
; Comment:
;    NB on BLCODETBL:
;       This table is for indexing baselines. 
;	It represents the different baselines 
;	as 256*ant1+ant2, with the antenna numbers going from 1-5, 
;	e.g. baseline 24 is 2*256+3 	Note here that antenna 4 
;	(in OVSA's naming convenion) is numbered as 3.  
;	BLCODETBL should match BLSTR in order, and also
;	it should be in order of baselines as specified in uv[*,j]
;	Otherwise you have to create another array which tells 
;	how the baselines are ordered.
; 01/05/2001 JL
;----------------------------------------------------

;--------------------------------------
; Restore Data
;--------------------------------------

restore,infile

t	= cal.t
frq 	= cal.f * 1.0E9
uv	= reform(cal.uv[*,*,0])/cal.f[0] ; only need for 1 GHz
rr	= cal.rr
ll	= cal.ll

;--------------------------------------
; Parameters specifying the data
;--------------------------------------

date 	= cal.info.date
target	= cal.info.src+'  '
RADEG	= cal.info.gtab[4]
DECDEG	= cal.info.gtab[5] 
NTmax	= long(n_elements(T))
Nfrq	= long(n_elements(frq))
NBsl	= long(n_elements(uv[0,*,0]))
if NOT keyword_set(outfile) then outfile =  $
		 strupcase(cal.info.src)+'.DAT'

;-----------------------------------------------------------
; Parameters fixed (as far as the array configuration fixed)
;-----------------------------------------------------------
Npol=2 ; R and L polarization

Nant=5 ; needs modification?
    X 	= [0.,     0.,     14.54650,  87.493864, 234.87516]
    Y 	= [-60.96,-487.68, 184.09,   -13.87,     -13.87   ]
    Z 	= [0.,     0.,     1.8437314,-94.427524,-288.68697]

Nbsl	= 7 ; Need to expand in future

    BLSTR = ['12','14','15','16','24','25','26']
BLCODETBL = [ 258, 259, 260, 261, 515, 516, 517]

bandw  	= 5.3e7 ; no meaning
Xcount  = NTmax * Nbsl

;--------------------------------------
; Rearrange data
;--------------------------------------

UTHR	=fltarr(NTmax,Nbsl)
BL_id 	=fltarr(NTmax,Nbsl)
U   	=fltarr(NTmax,Nbsl) ; this is for 1.0 GHz?
V   	=fltarr(NTmax,Nbsl)
W   	=fltarr(NTmax,Nbsl)
VRER	=fltarr(NTmax,Nbsl,Nfrq)
VIMR=VRER & VREL=VRER & VIML=VRER

for i = 0, NTmax-1 do begin
    print,i,'  of  ', NTmax-1
  for j = 0, Nbsl-1 do begin
    Uthr(i,j)= T(i)/24. ; tavg already in units of hour
    BL_id(i,j) = BLCODETBL(j)
    U(i,j) = float(uv[i,j])
    V(i,j) = imaginary(uv[i,j])
    W(i,j) = 0.0
	for k=0, Nfrq-1 do begin
	 VreR(i,j,k)= float(rr[i,j,k]) 
	 VimR(i,j,k)= imaginary(rr[i,j,k]) 
	 VreL(i,j,k)= float(ll[i,j,k]) 
	 VimL(i,j,k)= imaginary(ll[i,j,k]) 
	endfor
endfor
endfor

print,'Ready for write up?'
stop

skip:

;--------------------------------------
; Final check-up of data formats
;--------------------------------------
day  	= string(date)
target	= string(target)
RADEG 	= double(RADEG)
DECDEG	= double(DECDEG)
NIFS  	= long(Nfrq)
Npol  	= long(Npol)
Bandw 	= float(Bandw)
frq     = float(frq)
Nant  	= long(Nant)
X     	= double(X)
Y     	= double(Y)
Z     	= double(Z)
Xcount  = float(Xcount)
uthr  	= float(uthr)
BL_id  	= float(BL_id)
u     	= float(u)
v     	= float(v)
w     	= float(w)
VreR 	= float(VreR)
VimR 	= float(VimR)
VreL 	= float(VreL)
VimL 	= float(VimL)

no=where(finite(VreR) eq 0) & VreR(no)=0.
no=where(finite(VimR) eq 0) & VimR(no)=0.
no=where(finite(VreL) eq 0) & VreL(no)=0.
no=where(finite(VimL) eq 0) & VimL(no)=0. ; already done


;--------------------------------------
; Write-up
;--------------------------------------
print,'writing a file: ', outfile

openw,2,outfile,/f77
writeu,2,Date,target,RAdeg,DECdeg
; Here you cannot say writeu,2,date & writeu,2,target...
writeu,2,NIFs,Npol,Bandw
writeu,2,Frq
writeu,2,Nant,0.0,0.0,0.0
for i=0,Nant-1 do writeu,2,X(i),Y(i),Z(i)
writeu,2,Xcount
for i = 0, NTmax-1 do begin
for j = 0, Nbsl-1 do begin
    writeu,2,Uthr(i,j),BL_id(i,j),U(i,j),V(i,j),W(i,j)
	for k=0, Nfrq-1 do begin
    	writeu,2,VreR(i,j,k),VimR(i,j,k),VreL(i,j,k),VimL(i,j,k)
	endfor
endfor
endfor
close,2

print,'SUCCESSFUL '

end
