pro insert_fsurvey,header,kant,tpdat

   ; Determine year and day number
   year = strmid(header.tls.yrday,0,4)
   day  = strmid(header.tls.yrday,5,3)
   ; True if a leap year
   lpyr = 0
   if ((year mod 4) eq 0) then lpyr = 1

   npts = n_elements(tpdat)
   if (npts ne 2126) then begin
      print, 'INSERT_FSURVEY: Invalid number of points in data.',NPTS
      return
   endif
   ; Declare the basic structure

   filename = !defaults.dbdir+year+'_ant'+string(kant,format='(i1)')+'.svy'

   ; Set up error handler to open the file as writable if it does not
   ; already exist
   CATCH,err_stat
   if (err_stat ne 0) then begin
      ; If ENTRY already exists, we are in a loop where opening the file
      ; somehow is not working, so skip it
      if (n_elements(entry) ne 0) then begin
         print,'INSERT_FSURVEY: Problem opening file ',filename
         return
      endif

      ; The file does not already exist for the current antenna and year,
      ; so create a new one filled with zeroes.
      openw,lun,/get_lun,filename,/compress,/xdr

      ; Write a whole lot of zeroes, one for each day of the year.  Since
      ; this is a compressed file, the file size will be tiny.
      entry = fltarr(npts)
      for i = 0, 364 + lpyr do begin
         writeu,lun,entry
      endfor
      free_lun,lun
   endif

   ; Try to open the file as readable.  If this causes an error, a new
   ; file will be created.
   openr,lun,/get_lun,filename,/compress,/xdr
   CATCH,/CANCEL

   ; In order for this to work with /XDR files, it appears that we must
   ; read the entire file, write the entry into the data array, and write
   ; out the entire file again.
   datall = fltarr(npts,365+lpyr)
   readu,lun,datall
   free_lun,lun

   ; Insert the data entry
   datall[*,day] = tpdat

   ; Write the result to the file (will overwrite any existing entry)
   openw,lun,/get_lun,filename,/compress,/xdr
   writeu,lun,datall

   free_lun,lun

return
end