;+
; NAME:
;     TL_ENCODE
; PURPOSE:
;     Encodes a time/label field of a record (the first 12 words of
;     each record), from a filled-in TLFIELD structure
; CATEGORY:
;     OVRO APC DATA-GENERATION
; CALLING SEQUENCE:
;     tldata = tl_encode(tl_struct)
; INPUTS:
;     tl_struct  an instance of a TLFIELD structure, filled in with the
;                  values to place into a time/label field in tldata
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
;     tldata     an intarr(12) to be written as the first 12 words of a record,
;                  e.g. data(0:11), representing the time/label field
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 29-Jan-1999 by Dale Gary
;-
function tl_encode,tl_struct

   ; Definition of the TLFIELD structure (from tl_decode())

   ; tl_struct = {tlfield, nrc:0,  $; Current sequential record number (modifiable)
   ;                       nro:0,  $; Original sequential record number (fixed)
   ;                  scancode:0,  $; Scan type code (see scan_decode function)
   ;               segmentcode:0,  $; Segment type code (see seg_decode function)
   ;                       nrs:0,  $; Record number within segment
   ;                      year:0,  $; Year (e.g. 1999)
   ;                       day:0,  $; Day number
   ;                      msec:0L, $; UTC milliseconds in day
   ;                      date:' ',$; String representing yyyy/mm/dd
   ;                     yrday:' ',$; String representing yyyy.ddd
   ;                    timstr:' '} ; String representing hh:mm:ss

   ; Declare storage for output result
   tldata = intarr(12)

   ; Fill in output with values from TL_STRUCT
   tldata(0) = uint(tl_struct.nrc)
   tldata(1) = uint(tl_struct.nro)
   ; Convert two bytes to an integer
   tldata(2) = ishft(tl_struct.segmentcode,8)+tl_struct.scancode
   tldata(3) = uint(tl_struct.nrs)
   tldata(8) = uint(tl_struct.year)
   tldata(9) = uint(tl_struct.day)
   ; Convert LONG to 2-word representation
   tldata(10) = l2w(tl_struct.msec)

return,tldata
end
