pro write_phzcal,phz,tls1,tls2

   phz = phz*100.

   tl_struct = tlsnow(!SEGM.PHZCAL)

   ; 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)

   ; 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
   ; Loop over frequency.  Replace all bad (NaN) data with 'FFFF'x
   for i = 0, 85 do begin
      bad = where(finite(phz[*,i]) eq 0,nbad)
      phzdat = uint(reform(phz[*,i]))
      if (nbad ne 0) then phzdat[bad] = 'FFFF'x
      ; PHZDAT now contains the 7 scaled phases for feeds 1I, 1L, 2R, 2I, 4I, 5I, and 6I.
      ; To get back the actual phases in degrees, read the integer data, multiply by 0.01, and
      ; adjust baseline phase by the difference of phases on the two antennas.

      wrdoff = 36 + i*10
      data[wrdoff] = phzdat
   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,'phzcal.rec',bufsiz=2048
   ; Create an associated variable for the file
   b = assoc(lun,intarr(1024))
   ; Write the data
   b(0) = data
   ; Close the file and free the unit number
   free_lun,lun

   cd,cwd

return
end