pro xrt_id_miss_pack, year,month,day,miss_pack,tot_pack, zidx,file

  ;+
; PROJECT: 
;       Solar-B / Hinode / XRT
; 
; NAME: 
;       XRT_ID_MISS_PACK
;
; CATEGORY:
;
;       XRT Data Verification
; 
; PURPOSE:
;
;       Identify missing packets in files for a given date. 
;
; CALLING SEQUENCE:
; 
;       XRT_ID_MISS_PACK, YEAR, MONTH, DAY, MISS_PACKET,$
;                         TOTAL_PACKET, MISS_PACKET_INDEX, FILENAME 
;
; INPUTS:
;
;       YEAR           - Year (e.g., 2008)
;
;       MONTH          - Month (e.g.,   5)
;
;       DAY            - Day  (e.g.,   12)
;
;
; KEYWORDS:
;       
;
; OUTPUTS:
;       
;       MISS_PACKET    - Total number of missing packets in unit of 64
;                        pixels (one minimal packet). If this
;                        number isn't a whole number, then the
;                        users are encouraged to examine the
;                        MISS_PACKET_INDEX to see if any files are
;                        corrupted. 
; 
;       TOTAL_PACKET   - Total of the packet size expected (64 pixels
;                        per unit)
; 
;       MISS_PACEKT_INDEX - Similar to MISS_INDEX, contains how many
;                           packets (again in unit of 64pixels) are
;                           missing. 
; 
;       FILENAME       - Names of FITS files examined. 
;       
; EXAMPLES:
; 
;       IDL> xrt_id_miss_pack, 2008, 5, 12, missp, zidx, filename
;
; NOTES:
;
;       !!CAVEAT!!  
;       Each user must modify "rootdir" variable to the
;       right location for Hinode/XRT data archive directory. 
;
;       Modified from XRT_ECID.pro, taking only the parts dealing with
;       missing packets.  Missing images are now handled by XRT_ID_ECIDS.pro.
;
; MODIFICATION HISTORY:
; 
progver='v2011.Feb.10'     ;--- K. Reeves, modified from XRT_ECID.pro
;
;-
;==================================================================

  ;; set directory for files

  rootdir = '/archive/hinode/xrt/level0/' ; SAO

  ;;---- Search files obtained on the specific date ----- 
  ;;
  ;; [it assumes the same directory structure for data storage as in ISAS.] 
  ;;
  
  mm = month
  dd = day

  if (month lt 10) then mm= '0' + strtrim(month,1)
  if (day lt 10)   then dd= '0' + strtrim(day,1)

; search for the data files taken in the specified date
  datadir = strtrim(rootdir,1) + strtrim(year,1) + '/' + strtrim(mm,1) + '/' + strtrim(dd,1) + '/*/XRT*fits*'
  file    = file_search(datadir,count=cnt) 

  ; if no file found (cnt == 0), abort the process.
  if (cnt eq 0) then begin
     print, 'XRT_ID_MISS_PACK: no valid file found for the given date...aborting.'
     return
  endif

  ;; initialize index arrays. 
  n_fi   = n_elements(file)
  zidx   = fltarr(n_fi)
  tzidx  = fltarr(n_fi)

  ;; read data files in, record its EC_ID and if its data array contains
  ;; some missing data packets (pixel_value == 0). 
  ;;
  ;; for the packet size, see Chapter 16, page 38 in Solar-B blue
  ;; book. For simplicity, I set the minimum packet unit to be 64
  ;; pixels. 

  for i=0, n_fi-1 do begin
     NULL      = mrdfits(file[i],0,hdr,/silent)
     hz        = where(NULL eq 0,zp)

     ;; 1 packet = 64Kpixel = 64 * 1024 pixels = 65536 pixels max. 
     zp        = zp/64.         

     zidx[i]   = zp
     tzidx[i]  = n_elements(NULL)/64. ; again, 1 packet
  endfor

  miss_pack = total(zidx)
  tot_pack  = total(tzidx)

end
