;+
; Project     : SOHO - CDS     
;                   
; Name        : ERRC_CHECK
;               
; Purpose     : Accumulates GIS error counts.
;               
; Explanation : Logs the parameters AKCGCFC, AKCGHKFC and AKCGCNE from tm
;               files.
;               
; Use         : IDL> ec_check, input
;    
; Inputs      : input - can be one of:  DOY number (all files read)
;                                       Specific date - in CDS string format
;                                                  (all files read)
;                                       CDS UTC structure
;
;                                       Specific telemetry file name (or part
;                                       thereof, but MUST begin with tm)
;                                       eg 'tm.95_123'  will pick all files 
;                                       from DOY 123
;                                       
;               
; Opt. Inputs : None
;               
; Outputs     : Results are logged in file $CDS_ENG_DATA_W/errc_check.lis
;                    
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: Requires CDS_ENG_DATA_W to be defined.
;               
; Side effects: None
;               
; Category    : Telemetry
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 27-Feb-96
;               
; Modified    : 
;
; Version     : Version 1, 27-Feb-96
;-            

pro errc_check, file

;
;  does user have privilege to write data?
;
direct = getenv('CDS_ENG_DATA_W')
if direct eq '' then begin
   print,'Environment variable CDS_ENG_DATA_W is not defined.'
   return
endif


;
;  input can be 
;               doy    (specific day of year number, all files)
;               CDS  UTC structure (use all files from that day)
;               CDS  date string (use all files from it)
;               Specific file name (use only it)
;
if (n_params() eq 0)  then begin
   print,'USE: errc_check, doy or CDS date or telemetry file name.'
   return
endif

;
;  DOY specified
;

if datatype(file,1) eq 'Integer' or datatype(file,1) eq 'Long' then begin
   cdoy = strmid(string(file+1000,form='(i4)'),1,3)
   date = anytim2cal(doy2utc(file))
   year = strmid(date,7,2)
   ff = findfile(concat_dir('$CDS_TM_DATA',$
                 'tm.'+year+'_'+cdoy+'*'),count=count)
   if count eq 0 then begin
      print,'No telemetry files found for DOY ',file
      return
   endif
endif

;
;  CDS UTC structure
;
if datatype(file,1) eq 'Structure' then begin
   date = anytim2cal(file)
   doy = utc2doy(file)
   cdoy = strmid(string(file+1000,form='(i4)'),1,3)
   year = strmid(date,7,2)
   ff = findfile(concat_dir('$CDS_TM_DATA',$
                 'tm.'+year+'_'+cdoy+'*'),count=count)
   if count eq 0 then begin
      print,'No telemetry files found for UTC ',file
      return
   endif
endif
 
;
;  CDS date/time string or telemetry file name
;  
if datatype(file,1) eq 'String' then begin
   break_file, file, disk, dir, ff, ext
   if dir eq '' then begin
      if strmid(file,0,2) ne 'tm' then begin
         date = anytim2cal(file)
         doy = utc2doy(str2utc(file))
         cdoy = strmid(string(doy+1000,form='(i4)'),1,3)
         year = strmid(date,7,2)
         ff = findfile(concat_dir('$CDS_TM_DATA',$
                       'tm.'+year+'_'+cdoy+'*'),count=count)
         if count eq 0 then begin
            print,'No telemetry files found for date ',file
            return
         endif
      endif else begin
         ff = findfile(concat_dir('$CDS_TM_DATA',$
                       file+'*'),count=count)
         if count eq 0 then begin
            print,'No telemetry files of that name found.',file
            return
         endif
      endelse
   endif else begin
      ff = findfile(file)
   endelse
endif

;
;  try and make sure no funnies got through
;
break_file,ff,disk,dir,f,ext
n = where(strlen(ext) eq 10,count)
if count gt 0 then begin
   ff = ff(n) 
endif else begin
   print,'No valid files found.'
   return
endelse 

;
;  OK to continue?
;
if ff(0) eq '' then begin
   print,'No valid files.'
   return
endif
nf = n_elements(ff)

   
;
;  pick up the names
;
get_mask_etc, mask

bnames = ['AKCGCFC','AKCGHKFC','AKCGCNE']
pnum = intarr(3)
for i=0,2 do pnum(i) = where(mask(0,*) eq bnames(i))

;
;  open output file
;
openw,lunh,concat_dir('$CDS_ENG_DATA_W','errc_check.lis'),/get_lun

;
;  now LOOP for every tm file to be read
;

last = [0,0,0]
for kk = 0,nf-1 do begin
   bad = 0
   tfile = ff(kk)
   print,' '
   print,'Processing telemetry file: ',tfile
   printf,lunh,'Processing telemetry file: ',tfile

;
;  read temperature data from telemetry file
;
   openr,lun,tfile,/get_lun,error=err
   if err ne 0 then begin
      print,'Error opening file ',tfile
      return
   endif

   packet   = bytarr(306)
   ipack = -1

   on_ioerror, duff_pkt

   while (not eof(lun)) do begin
      readu,lun,packet


;
;  Eng B packet
;
      if (packet(18) eq '0a'x) then begin
         ipack = ipack + 1
         sdat = intarr(3)       
         for i=0,2 do begin
            temp = (strmid(mask(5,pnum(i)),2,4))(0) 
            hex2dec,temp,pmask,/quiet
            offset = fix(mask(3,pnum(i)))
            type = mask(2,pnum(i))
            case type of
              'B': begin
                     if pmask gt 0 then begin
                       data = fix(packet(offset) and pmask)
                     endif else begin
                       data = fix(packet(offset))
                     endelse
                   end
              'W': begin
                     temp = byteswap((fix(packet,offset,1))(0))
                     if pmask gt 0 then begin
                       data = unsign(temp) and long(pmask)
                     endif else begin
                       data = unsign(temp)
                     endelse
                   end
            endcase
            sdat(i) = data
         endfor
         if not same_data(last,sdat) then begin
            if bad eq 0 then begin
               print,'      Time                AKCGCFC  AKCGHKFC  AKCGCNE'
               printf,lunh,'      Time                AKCGCFC  AKCGHKFC  AKCGCNE'
               bad = 1
            endif
            obt = bytarr(6)
            t1 = long(packet,12,1)
            t2 =  fix(packet,16,1)
            obt(0:3) = byte(t1,0,4)
            obt(4:5) = byte(t2,0,2)
            time = anytim2cal(tai2utc(obt2tai(obt)))
  
            last = sdat
            print,time, sdat(0:1),unsign(sdat(2)), form='(a20,2i10,6x,z4.4)'
            printf,lunh,time, sdat(0:1),unsign(sdat(2)), form='(a20,2i10,6x,z4.4)'
         endif
      endif
   endwhile

   duff_pkt:
   on_ioerror, null

;
;  was anything found in this file?
;
   if ipack eq -1 then begin
      print,'Nothing in this file'
   endif
   free_lun, lun
endfor

;
;  close hard copy file
;
free_lun, lunh

end
