;+
; NAME:
;     READAMF
; PURPOSE:
;     Routine to read an AMF (Amphit) file and return the contents as an array
; CATEGORY:
;     OVRO APC SUPPORT
; CALLING SEQUENCE:
;     amf = readamf(filename[,info=info][,ants=ants])
; INPUTS:
;     filename	The name of the AMF file to read
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     getparms, tlsnow, get_tl_struct, tl_encode, dayofyr
; OUTPUTS:
;     amf       A multi-element array containing the contents of the AMF file.  An
;                 AMF file has sections corresponding to baselines, and optionally
;                 both RCP and LCP sections.  Typically an AMF file will contain
;                 baselines associated with a given antenna, e.g. 12, 14, and 24,
;                 but may also contain other baselines.  The identity of the data
;                 in each section is determined by the (optional) INFO structure.
;     info		A rather essential, but optional, structure that describes the
;                 identity, antenna order, and channel phase for each baseline.
;     ants      An optional array containing the antenna number of the antennas
;                 whose baselines are contained in the file.  This information is
;                 also deducible from the INFO structure, but ANTS may be more
;                 convenient for some purposes.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;    11-Jan-2003  DG
;      Slight change to read non-zero phi_ab from 12rli files.
;    17-Jun-2007  DG
;      Added this header.
;-
function readamf,filename,ants=ants,info=info

   if (n_elements(filename) eq 0) then $
       filename = dialog_pickfile(filter='*.amf',path=!defaults.workdir,/read)
   if (filename eq '') then return,-1

   rli = 0   ; Flag to indicate that this is a 12RLI file
   openr,lun,/get_lun,filename
   instr = ''
   readf,lun,instr
   if (strlen(instr) gt 40) then begin
      rli = 1  ; This IS a 12RLI file
      reads,instr,nf,nbl,ns,phi_ab,phi_bc,phi_ca
   endif else reads,instr,nf,nbl,ns

   calin = fltarr(6,nf,nbl,ns)
   info = replicate({bl:'',be:'',order:0,chanphz:0.0},nbl)
   bl = ' '
   be = ' '
   order = 0
   chanphz = 0.0

   in = {hed:strarr(4),data:fltarr(6,nf)}

   ants = replicate(0,8)
   antlist = [1,2,4,5,6,7,8,9]
   for i = 0, ns-1 do begin
      for j = 0, nbl-1 do begin
         readf,lun,in

         reads,in.hed[0],bl,format='(a2)'
         ; Read new header line containing info about this baseline
         reads,in.hed[1],be,order,chanphz,format='(10x,a2,11x,i2,16x,f6.2)'

		 if (rli eq 1) then begin
		    ; This is a 12RLI file, so change the meaning of the CHANPHZ
		    ; entries to make all values accessible to APCAL_ADD_27MANT
		    case j of
		       0: chanphz = phi_ab
		       1: chanphz = phi_bc
		       2: chanphz = phi_ca
		       else:
		    endcase
		 endif
         info[j].bl = bl
         info[j].be = be
         info[j].order = order
         info[j].chanphz = chanphz

         calin[*,*,j,i] = in.data
         ant1 = fix(strmid(in.hed[0],0,1))
         ant2 = fix(strmid(in.hed[0],1,1))
         k = where(antlist eq ant1,n)
         if (n ne 0) then ants[k] = 1
         k = where(antlist eq ant2,n)
         if (n ne 0) then ants[k] = 1
      endfor
   endfor

   free_lun,lun

   head = in.hed
return,calin
end