;+
; Converts the 43-freq RLI calibration file into 5 files
; with frequencies corresponding to the 10-freq I scans,
; for use as a calibration for the I scan data.
;
; Written 22-Jul-2000 by Dale E. Gary
;-
pro conv_rli

   ; Open the input .AMF file containing the 43-freq RLI data
   filename = dialog_pickfile(path=!defaults.workdir,filter='*.amf',title='Choose an RLI .AMF file')
   if (filename eq '') then return

   ; Create the file stem for naming the output files
   break_file,filename,disk,dir,file,ext
   file = disk+dir+file+'_'

   ; Read in the 43-freq RLI data, for 7 baselines
   nf = 43
   nbl = 7
   in = {hed:strarr(3),data:fltarr(6,nf)}
   heds = strarr(3,nbl)
   calin = fltarr(6,nf,nbl)
   openr,lun,/get_lun,filename
   for i = 0, nbl-1 do begin
      readf,lun,in
      heds[*,i] = in.hed
      calin[*,*,i] = in.data
   endfor
   free_lun,lun

   ; Create a list of standard indexes for the 5 I scans.
   iflist =[[0,5,10,15,20,25,30,35,40,42], $
            [1,6,10,11,16,21,26,31,36,41], $
            [2,7,10,12,17,22,27,32,37,42], $
            [3,8,10,13,18,23,28,33,38,41], $
            [4,9,10,14,19,24,29,34,39,42]]

   ; Create output structure for 10-freq data
   out = {hed:strarr(3),data:fltarr(6,10)}

   ; Create the files to match the 5 I scans.
   for j = 0, 4 do begin
      openw,lun,/get_lun,file+string(j,format='(I1)')+'.cal'
      ; For each scan, write out the 7 baselines of data
      for i = 0, nbl-1 do begin
         out.hed = heds[*,i]
         out.data = calin(*,iflist[*,j],i)
         printf,lun,out.hed,format='(a)'
         printf,lun,out.data,format='(f4.1,1x,2f8.3,1x,3f7.1)'
      endfor
      free_lun,lun
   endfor

   ;All done
return
end

