from __future__ import division

def read_cbf_image(cbf_image):
  from cbflib_adaptbx import uncompress
  import binascii

  start_tag = binascii.unhexlify('0c1a04d5')

  data = open(cbf_image, 'rb').read()
  data_offset = data.find(start_tag) + 4
  cbf_header = data[:data_offset - 4]

  fast = 0
  slow = 0
  length = 0

  for record in cbf_header.split('\n'):
    if 'X-Binary-Size-Fastest-Dimension' in record:
      fast = int(record.split()[-1])
    elif 'X-Binary-Size-Second-Dimension' in record:
      slow = int(record.split()[-1])
    elif 'X-Binary-Number-of-Elements' in record:
      length = int(record.split()[-1])
    elif 'X-Binary-Size:' in record:
      size = int(record.split()[-1])

  assert(length == fast * slow)

  pixel_values = uncompress(packed = data[data_offset:data_offset + size],
                            fast = fast, slow = slow)

  return pixel_values

def get_overload(cbf_file):
  for record in open(cbf_file):
    if 'Count_cutoff' in record:
      return int(record.split()[-2])

def build_hist():
  import sys
  from scitbx.array_family import flex

  hist = { }

  thresh = 100
  scale = thresh / get_overload(sys.argv[1])

  for image in sys.argv[1:]:
    sys.stdout.write('.')
    sys.stdout.flush()
    data = read_cbf_image(image)
    scaled = (scale * data.as_double()).iround()
    for d in scaled:
      if not d in hist:
        hist[d] = 0
      hist[d] += 1

  print '\n'
  for d in sorted(hist)[1:]:
    if d < 0.1 * thresh:
      print d, hist[d]
    else:
      print d, hist[d], '***'

if __name__ == '__main__':
  build_hist()

@@ -1091,22 +1091,8 @@
 ex%0A%0A
-  hist = %7B %7D%0A%0A
   th
@@ -1148,16 +1148,32 @@
 gv%5B1%5D)%0A%0A
+%0A  hist = None%0A%0A
   for im
@@ -1288,17 +1288,16 @@
 caled = 
-(
 scale * 
@@ -1316,233 +1316,318 @@
 le()
-).iround()%0A    for d in scaled:%0A      if not d in hist:%0A        hist%5Bd%5D = 0%0A      hist%5Bd%5D += 1%0A%0A  print '%5Cn'%0A  for d in sorted(hist)%5B1:%5D:%0A    if d %3C 0.1 * 
+%0A    if hist == None:%0A      hist = flex.histogram(scaled, data_min=0.0, data_max=(5.0*thresh),%0A                            n_slots=500)%0A    else:%0A      tmp_hist = flex.histogram(scaled, data_min=0.0, data_max=(5.0*
 thresh
-:
+),
 %0A      
-print d, hist%5Bd%5D%0A    else:%0A      print d, hist%5Bd%5D, '***'
+                          n_slots=500)%0A      hist.update(tmp_hist)%0A%0A  hist.show()
 %0A%0Aif

Should work, is not