;+
; Project     : SOHO - CDS     
;                   
; Name        : EMON_TEMP_LOG
;               
; Purpose     : Accumulates CDS temperature logs.
;               
; Explanation : Reads all the temperature telemetry values at two times
;               within a telemetry file (15 and 45 minutes into the hour).
;               These values are added to the temperature log, which is a save
;               file 'temperature_log' kept in $CDS_ENG_DATA.
;               
; Use         : IDL> emon_temp_log, 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 - eg 'tm.95_123'  will pick
;                                       all files from DOY 123
;                                       
;               
; Opt. Inputs : None
;               
; Outputs     : Save file is updated.
;               
; Opt. Outputs: None
;               
; Keywords    : NOCHECK - if present no user check is made of the source of the 
;                         telemetry files.
;
; 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, 26-Oct-95
;               
; Modified    : Check that both points are valid before including in log.
;                                                     CDP, 14-Dec-95
;               Added time range info if no parameters supplied. CDP, 15-Dec-95
;               Added check on CDS_TM_DATA definition after cock-up at RAL
;                                                     CDP, 18-Dec-95
;               Add NOCHECK keyword and skip data if it is within
;               existing time limits.                 CDP, 19-Dec-95
;
;               Allow duplicate data to be added, and any duplicates are
;               yhen removed.   CDP, 26-Jul-96
;
; Version     : Version 6, 26-Jul-96
;-            
pro emon_temp_log, file, nocheck=nocheck


;
;  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,'To create new log entries try...'
   print,'IDL> emon_temp_log, doy or CDS date or telemetry file name.'
   if file_exist(concat_dir('$CDS_ENG_DATA','temperature_log')) then begin
         cds_temp_log = 0
         restore,concat_dir('$CDS_ENG_DATA','temperature_log')
         nsamp = n_elements(cds_temp_log(0).time)
         mintime = min(cds_temp_log(0).time)
         maxtime = max(cds_temp_log(0).time)
         mind = utc2doy(tai2utc(mintime))
         maxd = utc2doy(tai2utc(maxtime))
         print,' '
         print,'Current time limits in log are: ',$
                anytim2cal(mintime),' to '
         print,'                                ',anytim2cal(maxtime)
         print,'DOY: '+fmt_vect([mind,maxd])
         print,' '
   endif   
   return
endif


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

;
;  check where telemetry files are coming from 
;
if not keyword_set(nocheck) then begin
   print,'Telemetry will be read from files in '+getenv('CDS_TM_DATA')
   bell
   yes_no,'Is that OK? :',ok,'Y'
   if not ok then 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
   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

;
;  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
na = where(strmid(mask(0,*),0,2) eq 'AT',count)
if count gt 0 then begin
   aname = reform(mask(0,na))
endif else begin
   print,'No temperature mnemonics found in EngA list'
   return
endelse

nb = where(strmid(mask(0,*),0,2) eq 'BT',count)
if count gt 0 then begin
   bname = reform(mask(0,nb))
endif else begin
   print,'No temperature mnemonics found in EngB list'
   return
endelse


;
;  there should be 48 names - flag major error if not
;
nna = n_elements(aname)
nnb = n_elements(bname)

if ( nna+nnb ) ne 48 then begin
   bell
   print,'Should be 48 mnemonic names. (',nna+nnb,' found).'
   return
endif

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

for kk = 0,nf-1 do begin

   tfile = ff(kk)
   print,'Processing telemetry file: ',tfile

;
;  how many new values are to be stored?  (2 per telemetry file)
;
   new = n_elements(file)*2
   d1 = {name:'',time:dblarr(2),value:intarr(2)}
   temp_new = replicate(d1,48)


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

;
;  only want 2 samples per file so assume a complete hour file and take samples
;  at approximately 15 mins and 45 min past the hour (ie every 30 mins during
;  the day.

   fs = fstat(lun)
   npack = fs.size/306L
   nsamp = lonarr(2)
   nsamp(0) = npack/4
   nsamp(1) = nsamp(0)*3
   
   
   packet   = bytarr(306)
   adata    = intarr(nna,2)
   bdata    = intarr(nnb,2)
   pkt_time = dblarr(2,2)

;
;  position file to first and second location and read till find EngA and B
;
   for kp = 0,1 do begin
      point_lun, lun, nsamp(kp)*306L
      found_a = 0
      found_b = 0
      
      while (not eof(lun)) and (found_a + found_b lt 2) do begin
         readu,lun,packet
         case 1 of
           (packet(18) eq '0a'x) : begin
                       pkt_time(kp,0) = obt2tai(packet(12:17))
                       for i=0,nna-1 do begin
                          temp = (strmid(mask(5,na(i)),2,4))(0) 
                          hex2dec,temp,pmask,/quiet
                          offset = fix(mask(3,na(i)))
                          type = mask(2,na(i))
                          case type of
                            'B': begin
                                   if pmask gt 0 then begin
                                     adata(i,kp) = fix(packet(offset) and pmask)
                                   endif else begin
                                     adata(i,kp) = fix(packet(offset))
                                   endelse
                                 end
                            'W': begin
                                   temp = byteswap((fix(packet,offset,1))(0))
                                   if pmask gt 0 then begin
                                     adata(i,kp) = unsign(temp) and long(pmask)
                                   endif else begin
                                     adata(i,kp) = unsign(temp)
                                   endelse
                                 end
                          endcase
                       endfor
                       found_a = 1
                                   end
      
           (packet(18) eq '0b'x) : begin
                       pkt_time(kp,1) = obt2tai(packet(12:17))
                       for i=0,nnb-1 do begin
                          temp = (strmid(mask(5,nb(i)),2,4))(0) 
                          hex2dec,temp,pmask,/quiet
                          offset = fix(mask(3,nb(i)))
                          type = mask(2,nb(i))
                          case type of
                            'B': begin
                                   if pmask gt 0 then begin
                                     bdata(i,kp) = fix(packet(offset) and pmask)
                                   endif else begin
                                     bdata(i,kp) = fix(packet(offset))
                                   endelse
                                 end
                            'W': begin
                                   temp = byteswap((fix(packet,offset,1))(0))
                                   if pmask gt 0 then begin
                                     bdata(i,kp) = unsign(temp) and long(pmask)
                                   endif else begin
                                     bdata(i,kp) = unsign(temp)
                                   endelse
                                 end
                          endcase
                       endfor
                       found_b = 1
                                   end
                             else:
         endcase
      endwhile
   endfor


;
;  load data into new structure
;
   temp_new(0:nna-1).name =  aname
   for i=0,nna-1 do temp_new(i).time(0:1) = pkt_time(*,0)
   for i=0,nna-1 do temp_new(i).value(0:1) = adata(i,*)

   temp_new(nna:nna+nnb-1).name =  bname
   for i=0,nnb-1 do temp_new(nna+i).time(0:1) = pkt_time(*,1)
   for i=0,nnb-1 do temp_new(nna+i).value(0:1) = bdata(i,*)

;
;  set acceptable limits on time values                   
;
   earliest_time =  utc2tai(str2utc('1995-11-23'))
   latest_time   =  utc2tai(str2utc('2005-11-23'))

   ngood = where(temp_new(0).time gt earliest_time and $
                 temp_new(0).time lt latest_time,tc)


;
;  just skip if both times are not good
;
   if tc eq 2 then begin

;
;  does log file already exist?
;
      if file_exist(concat_dir('$CDS_ENG_DATA_W','temperature_log')) then begin
         cds_temp_log = 0
         restore,concat_dir('$CDS_ENG_DATA_W','temperature_log')
         nsamp = n_elements(cds_temp_log(0).time)

;
;  prepare new blank structure
;
         d1 = {name:'',time:dblarr(nsamp+2),value:intarr(nsamp+2)}
         temp_str = replicate(d1,48)
;
;  add new to old
;
         temp_str.name = cds_temp_log.name
         for i=0,47 do begin
            temp_str(i).time = [cds_temp_log(i).time,temp_new(i).time]
            temp_str(i).value = [cds_temp_log(i).value,temp_new(i).value]
         endfor
;
;  sort and remove any duplicates
;
        n = rem_dup(temp_str(0).time)
        nn = n_elements(n)
        d1 = {name:'',time:dblarr(nn),value:intarr(nn)}
        cds_temp_log = replicate(d1,48)
        for i=0,47 do begin
           cds_temp_log(i).name  = temp_str(i).name
           cds_temp_log(i).time  = temp_str(i).time(n)
           cds_temp_log(i).value = temp_str(i).value(n)
        endfor
        save,/xdr,cds_temp_log,$
                  file=concat_dir('$CDS_ENG_DATA_W','temperature_log')
      endif else begin
         print,'Starting new log'
         cds_temp_log = temp_new
         save,/xdr,cds_temp_log,$
                        file=concat_dir('$CDS_ENG_DATA_W','temperature_log')
      endelse              
   endif
   free_lun, lun
endfor

end
