pro write_ampcal,amp,damp,tls1,tls2

   amp  = amp*100.
   damp = damp*100.

   ; Get an appropriate TIME/LABEL structure containing the system
   ; date/time.
   tl_struct = tlsnow(!SEGM.AMPCAL)

   ; (For now, just ask for date.  This will be in the AMF file soon)
   tls1 = get_tl_struct()
   read,'Enter year, day of year, and start HHMM of I file [YYYY DDD HHMM]: ',year,doy,hhmm
   tls1.year = year
   tls1.day  = doy
   tls1.yrday = string(year,doy,format='(I4,".",I3.3)')
   tls1.msec = hms2sec(hhmm*100L)*1000L
   tls1.timstr = string(fix(hhmm/100),hhmm mod 100,format='(" ",I2.2,":",I2.2,":00.000")')
   read,'Enter year, day of year, and start HHMM of R/L file [YYYY DDD HHMM]: ',year,doy,hhmm
   tls2 = tls1
   tls2.year = year
   tls2.day  = doy
   tls2.yrday = string(year,doy,format='(I4,".",I3.3)')
   tls2.msec = hms2sec(hhmm*100L)*1000L
   tls2.timstr = string(fix(hhmm/100),hhmm mod 100,format='(" ",I2.2,":",I2.2,":00.000")')

   ; Convert tl_struct to binary data
   tldata = tl_encode(tl_struct)
   tlsi   = tl_encode(tls1)   ; Mostly empty TLS structure corresponding to the I data
   tlsrl  = tl_encode(tls2)   ; Mostly empty TLS structure corresponding to the R/L data

   ; Declare storage for entire amplitude record (2 records)
   data = intarr(1024)
   data2 = intarr(1024)

   ; Fill in record
   data[0:11]  = tldata                 ; Time/Label field
   data[12:23] = tlsi                   ; Time/Label field of 27-m data epoch
   data[24:35] = tlsrl                  ; Time/Label field of  2-m data epoch

   ; For the second record, update the NRS field
   tl_struct.nrs = 2
   tldata = tl_encode(tl_struct)
   data2[0:11]  = tldata                ; Time/Label field

   ; Loop over frequency.  Replace all bad (NaN) data with 'FFFF'x
   for i = 0, 48 do begin
      bad = where(finite(amp[*,i]) eq 0,nbad)
      ampdat = uint(reform(amp[*,i]))
      if (nbad ne 0) then ampdat[bad] = 'FFFF'x
      bad = where(finite(damp[*,i]) eq 0,nbad)
      rmsdat = uint(reform(damp[*,i]))
      if (nbad ne 0) then rmsdat[bad] = 'FFFF'x
      ; AMPDAT now contains the scaled calibration factors for up to 10 feeds
      ;          1I, 1L, 2R, 2I, 4I, 5I, 6I, etc.
      ; RMSDAT now contains the scaled rms values corresponding to these feeds.
      ; To get back the actual calibration factors in units/SFU, read the integer data,
      ; multiply by 0.01, and apply the factors to the baseline data by multiplying by
      ; the amp factors for BOTH antennas.
      wrdoff = 36 + i*20
      data[wrdoff] = ampdat
      data[wrdoff+10] = rmsdat
   endfor
   for i = 49, 85 do begin
      bad = where(finite(amp[*,i]) eq 0,nbad)
      ampdat = uint(reform(amp[*,i]))
      if (nbad ne 0) then ampdat[bad] = 'FFFF'x
      bad = where(finite(damp[*,i]) eq 0,nbad)
      rmsdat = uint(reform(damp[*,i]))
      if (nbad ne 0) then rmsdat[bad] = 'FFFF'x
      wrdoff = 12 + (i-49)*20
      data2[wrdoff] = ampdat
      data2[wrdoff+10] = rmsdat
   endfor

   cd,!defaults.ephemdir,current=cwd

   ; Open the output file to write the record.  This will overwrite any
   ; existing file, or create a new one if none exists.
   openw,lun,/get_lun,'ampcal.rec',bufsiz=2048
   ; Create an associated variable for the file
   b = assoc(lun,intarr(1024))
   ; Write the data
   b(0) = data
   b(1) = data2
   ; Close the file and free the unit number
   free_lun,lun

   cd,cwd

return
end