pro wrap_xrt_ecid_monthly, year, month, miss_f, miss_p, miss_pn, tot_p, $
                           fi_idx, p_idx, file, tstamp, $
                           ql=ql 
;+
; PROJECT: 
;       Solar-B / Hinode / XRT
; 
; NAME: 
;       WRAP_XRT_ECID_MONTHL
;
; CATEGORY:
;
;       Wrapper for XRT Data Verification Code XRT_ECID.PRO
; 
; PURPOSE:
;
;       Evaluate how many data files and data packets are missing in a
;       given month. 
;
; HISTORY: 
; 
;       K. Ishibashi   19-Jun-2008  Original
;       K. Ishibashi   23-Jul-2008  Added total number of packets
;                                   expected as output. (0.1.0)
;       K. Ishibashi   24-Jul-2008  Change in message styles. (0.1.1)
;                                   Also add a minor bug fix on year setting.
;
;
;-

; Version control
; Ver = x.y.z 
;             where x == main version control
;                   y == significant update
;                   z == minor bug fixes
  ver = '0.1.1'


; Create a master obev array

  obevroot = '/archive/hinode/xrt/timelines/' ; SAO

  mm    = month
  mm0   = month - 1 
  if (month lt 10) then mm  = '0' + strtrim(month,1)
  if (month le 10) then mm1 = '0' + strtrim(month-1,1) else mm1 = month-1

  year1 = year
  if (month eq 1) then begin
     mm1   = 12                   ;
     year1 = year1 - 1            ;
  endif


  obevmnth0 = strtrim(obevroot,1) + '/' + strtrim(year,1) + '/' + $
              strtrim(mm,1) + '/*/' + strtrim(year,1) + strtrim(mm,1) + $ ;
              '*_exported/obev*.evt'

  obevmnth1 = strtrim(obevroot,1) + '/' + strtrim(year1,1) + '/' + $
              strtrim(mm1,1) + '/*/' + strtrim(year1,1) + strtrim(mm1,1) + $ ;
              '*_exported/obev*.evt'

  obevmnth1tmp = file_search(obevmnth1, count=ocnt) ;
  if (ocnt ne 0) then begin
     obevmnth1    = obevmnth1tmp[ocnt-1] ;
  endif else begin
     obevmnth1    = ''
     print, ' '
     print, 'no obev file in the prior month...'
     print, ' '
  endelse

  obevmnth1tmp = 0                ;
                                    
  uniqst       = strtrim(long(randomu(seed) * 1e6),1)
  spawn,'cat '+ strtrim(obevmnth0,1) + ' ' + strtrim(obevmnth1,1) + ' > /tmp/obev_mast' + strtrim(uniqst,1) + '.tmp'
  spawn,'grep -v HLA /tmp/obev_mast' + strtrim(uniqst,1) + '.tmp | sort -k3 | uniq > /tmp/obev_mast' + strtrim(uniqst,1) + '.dat'
  spawn,'\rm -rf /tmp/obev_mast' + strtrim(uniqst,1) + '.tmp'

  obevfile  = '/tmp/obev_mast' + strtrim(uniqst,1) + '.dat'


; initialize arrays

  miss_f = 0ul                    ; 
  miss_p = 0ul                    ;
  miss_pn= 0ul                    ;
  tot_p  = 0ul                    ;
  fi_idx = [0]                  ;
  p_idx  = [0]                  ;
  file   = ['']                 ;
  tstamp = ['']                 ;

; ignore what month it is. 
  for d = 1, 31 do begin 

     print, '--------------------------------' ;
     print, ' Processing the date: ' + strtrim(year,1) + '/' + strtrim(month,1) + $
            '/' + strtrim(d,1) + '  '
     print, '--------------------------------' ;

     
; initialize the key return values to zero. 

     miss_fi = 0
     miss_pi = 0
     tot_pi  = 0
     fi_idxi = 0
     tstampi = ''
     p_idxi  = 0
     filei   = ''

     xrt_ecid, year, month, d, miss_fi, fi_idxi, tstampi, miss_pi, tot_pi, p_idxi, filei, $
               obevfile=obevfile, /stop_default

     if ((filei[0] ne '') and (n_elements(filei) ne 1)) then begin
  
        hh = where(p_idxi ne 0, cnt) ;
        miss_pn = miss_pn + cnt      ;
        miss_f  = miss_f  + miss_fi  ;
        miss_p  = miss_p  + miss_pi  ;
        tot_p   = tot_p   + tot_pi   ;
        
        fi_idx  = [fi_idx, fi_idxi] ;
        p_idx   = [p_idx,  p_idxi ] ;
        file    = [file,   filei  ] ;
        tstamp  = [tstamp, tstampi] ;
        
        print, miss_f, miss_fi
        if (miss_f lt 0) then stop
        print, ' Number of Missing Files  : ' + strtrim(miss_fi,1)                                      ;
        print, ' Number of Missing Packets: ' + strtrim(miss_pi,1) + ' in unit of 64 pixels per packet' ;
        print, '                   (out of the expected total of ' + strtrim(tot_pi,1) + ' packets)'    ; 
        print, ' Number of Files with Missing Packets : ' + strtrim(cnt,1)                              ;

     endif
 
  endfor

  remove,[0], fi_idx, p_idx, file, tstamp

  print,' '
  print,' * Process Complete * ' 
  print,' ' 
  tbeep, 5

  print, '  '
  print, '--------------------------------------------------------------------'
  print, '  '
  print, ' Missing XRT Data Report for ' + strtrim(year,1) + '/' + strtrim(month,1) + ':'
  print, '  '
  print, '  Number of Missing Files  : ' + strtrim(miss_f,1) 
  print, '  Number of Missing Packets: ' + strtrim(miss_p,1) + ' in unit of 64 pixels per packet' ;
  print, '                    (out of the expected total of ' + strtrim(tot_p,1) + ' packets)'    ; 
  print, '  Number of Files with Missing Packets : ' + strtrim(miss_pn,1)                         ;
  print, '  '
  print, '--------------------------------------------------------------------'

  
  spawn,'\rm -rf /tmp/obev_mast' + strtrim(uniqst,1) + '.dat'

return
end
