;+
; Project     : SOHO - CDS     
;                   
; Name        : CHK_TM_TIME
;               
; Purpose     : Check packet times are incrementing correctly.
;               
; Explanation : Collects the packet times for science and HK packets separately
;               from the list of tm files input and checks them for
;               orderliness.
;               
; Use         : IDL> chk_tm_time, files
;    
; Inputs      : files - array of telemetry file names (eg as gathered by
;                       findfile. Files can be in current directory or in 
;                       standard directory $CDS_TM_DATA.
;               
; Opt. Inputs : None
;               
; Outputs     : None
;               
; Opt. Outputs: None
;               
; Keywords    : VERBOSE - if present, information on the packets surrounding
;                         any misplaced packets is displayed.
;
;               HARDCOPY - if present the verbose output is sent to a file
;                          show_tm_pkt_xxx in the user's home directory.
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Telemetry
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 2-Oct-95
;               
; Modified    : Add hardcopy option.  CDP, 3-Oct-95
;               Check for zero and 'ff' time bytes and warn.  CDP, 3-Oct-95
;
; Version     : Version 3, 3-Oct-95
;-            

pro chk_tm_time, files, verbose=verbose, hardcopy=hardcopy

;
;  check at least input files specified
;
if n_params() eq 0 then begin
   print,'Use: chk_tm_time, files'
   return
endif

;
;  set up IDs for HK packets
;
type = strarr(225)
type(*) = ''
type(16) = 'HK'
type(32) = 'HK'
type(64) = 'HK'
type(80) = 'HK'
type(96) = 'HK'
type(128) = 'HK'                    ;
type(144) = 'HK'
type(224) = 'HK'

;
;  Define general packet header structure
;
PacketHeader = {PacketHeaderDef,                $
                Msgid:               FIX(0),    $
                MsgLen:              FIX(0),    $
                MsgReqid:            FIX(0),    $
                ap_id:               BYTARR(2), $
                counter:             FIX(0),    $
                length:              FIX(0),    $
                Time:                BYTARR(6), $
                ID:                  BYTE(0),   $
                StreamPacketCounter: BYTE(0)}

;
;  and dummy data array
;
b286 = bytarr(286)
                        
;
;  loop over the input files
;
for nf=0,n_elements(files)-1 do begin

;
;  allow user to have own version of telemetry files
;
   tfile = ''
   if  file_exist(files(nf)) then begin
      tfile = files(nf)
   endif else begin
      if file_exist(concat_dir('$CDS_TM_DATA',files(nf))) then begin
         tfile = concat_dir('$CDS_TM_DATA',files(nf))
      endif
   endelse

;
;  telemetry file doesn't exist in user or std directory
;
   if tfile eq '' then begin
      print,'chk_tm_time:  Input file ',files(nf),' does not exist.'
      return
   endif

;
;  try to open telemetry file.  If OK then get size in packets.
;
   openr,tmlun,tfile,/get_lun,error=err
   if err ne 0 then begin
      print,'Cannot open telemetry file ',tfile
      return
   endif else begin
      f = fstat(tmlun)
      maxpax = f.size/306L
   endelse

;
;  save time for output if needed
;
   ts_save  = dblarr(maxpax)
   thk_save = dblarr(maxpax)

;
;  ... and loop around the packets
;
   notime = 0
   badtime = 0
   for i=0,maxpax-1 do begin
      readu,tmlun, packetheader, b286
      if packetheader.id gt 224 or packetheader.id lt 0 then packetheader.id = 0
      if total(packetheader.time) gt 0.0 and $
              total(packetheader.time) lt 1530 then begin
         tt = obt2tai(packetheader.time)
         if type(packetheader.id) eq 'HK' then thk_save(i)=tt else ts_save(i)=tt
      endif else begin
         if total(packetheader.time) eq 0.0 then begin
            notime = 1
         endif else begin
            badtime = 1
         endelse
      endelse
   endfor
  
;
;  tidy the time arrays
;
   n = where(thk_save gt 0.0)
   if n(0) ge 0 then begin
      thk_save = thk_save(n)
      hk_pkts = n
   endif else begin
      thk_save = -1
      hk_pkts = -1
   endelse

   n = where(ts_save gt 0.0)
   if n(0) ge 0 then begin
      ts_save = ts_save(n)
      s_pkts = n
   endif else begin
      s_save = -1
      s_pkts = -1
   endelse

;
;  release unit
;
   free_lun,tmlun

   print,' '
   print,'TM file: ',files(nf)
   print,'--------'

;
;  warn if zero time bytes seen
;
   if notime then begin
      bell
      print,'** Warning: some packets had all zeros in time bytes.'
   endif
;
;  warn if 'ff' time bytes seen
;
   if badtime then begin
      bell
      print,"** Warning: some packets had 'ff' in all time bytes."
   endif

   bad = 0

;
;  check HK packets were all in sequence
;
   if n_elements(hk_pkts) gt 1 then begin
      x = thk_save-shift(thk_save,1)
      n = where(x(1:*) le 0)
      if n(0) ge 0 then begin
         n = n + 1
         for i=0,n_elements(n)-1 do begin
            print,'HK packet # ',hk_pkts(n(i)),' is out of time order. '+$
                  anytim2cal(tai2utc(thk_save(n(i))),form=9)
            if keyword_set(verbose) or keyword_set(hardcopy) then begin
               if keyword_set(verbose) then begin
                  print,'----------------------------------------------'
                  show_tm_pkt,files(nf),[hk_pkts(n(i))-2,hk_pkts(n(i))+2]
                  print,'----------------------------------------------'
               endif
               if keyword_set(hardcopy) then begin
                  show_tm_pkt,files(nf),[hk_pkts(n(i))-2,hk_pkts(n(i))+2],$
                              /keep,/quiet
               endif
            endif      
         endfor
         bad = 1
      endif
   endif   
 
;
;  check all science packets were in sequence
;  
   if n_elements(s_pkts) gt 1 then begin
      x = ts_save-shift(ts_save,1)
      n = where(x(1:*) le 0)
      if n(0) ge 0 then begin
         n = n + 1
         for i=0,n_elements(n)-1 do begin
            print,'Science packet # ',s_pkts(n(i)),' is out of time order. '+$
                  anytim2cal(tai2utc(ts_save(n(i))),form=9)
            if keyword_set(verbose) or keyword_set(hardcopy) then begin
               if keyword_set(verbose) then begin
                  print,'----------------------------------------------'
                  show_tm_pkt,files(nf),[s_pkts(n(i))-2,s_pkts(n(i))+2]
                  print,'----------------------------------------------'
               endif
               if keyword_set(hardcopy) then begin
                  show_tm_pkt,files(nf),[s_pkts(n(i))-2,s_pkts(n(i))+2],$
                              /keep,/quiet
               endif
            endif      
         endfor
         bad = 1
      endif
   endif   
   if not bad then print,'......OK'
   print,' '
endfor
end
