#!/usr/bin/env python
#
# vim: tw=0

import argparse
import math
import re
import sys

strataBounds = [ 0.0, 1.0e-8, 1.0e-7, 1.0e-6, 1.0e-5, 1.0e-4, 1.0e-3, 1.0e-2, 1.0e-1, 1.0, 2.0 ]

parser = argparse.ArgumentParser()

parser.add_argument("FILE",
    help = "Read metric from FILE",
    nargs = "+")

options = parser.parse_args()

for f in options.FILE:
  if f == "-":
    fd = sys.stdin
  else:
    fd = open(f, "r")

  N = 0
  strataCounts = [ 0 ] * (len(strataBounds)-1)
  for line in fd:
    result = re.compile("matrix.*= ([0-9.eE+-]+)$").search(line)
    if result:
      N += 1
      aij = math.fabs(float(result.group(1)))
      foundMatrixElement = False
      for i in range(len(strataBounds)-1):
        if (aij >= strataBounds[i]) and (aij < strataBounds[i+1]):
          strataCounts[i] += 1
          foundMatrixElement = True
          break
      if not foundMatrixElement:
        print("could not place \"%s\" anywhere" % (line.strip()))
        sys.exit(1)

  fd.close()

  print("(%s) read %i matrix elements (%ix%i = %i)" % (f, N,
    int(math.sqrt(N)), int(math.sqrt(N)), int(math.sqrt(N))**2))

  total = 0
  for i in range(len(strataBounds)-1):
    total += strataCounts[i]
    print("[%1.2e, %1.2e) = %i (%1.2f%%) %i" % (strataBounds[i], strataBounds[i+1],
      strataCounts[i], 100*(strataCounts[i]/N), total))
