#! /usr/bin/python3

import sys
import numpy

numpy.set_printoptions(suppress=True, linewidth=100000)

maxdiff = 3.

reference = numpy.loadtxt(sys.argv[1])
array = numpy.loadtxt(sys.argv[2])

if reference.shape != array.shape:
   print("Size {}x{} does not match reference shape {}x{}"
         .format(array.shape[0], array.shape[1],
                 reference.shape[0], reference.shape[1]))
   sys.exit(1)

# We only check the coordinate columns, as the fluxes seem to be a bit
# unstable
diff = numpy.abs(reference[:,:8] - array[:,:8])

differing_rows = numpy.unique(numpy.where(diff >= maxdiff)[0])
print("Max diff to reference is {}".format(diff.max()))

if len(differing_rows) == 0:
   sys.exit(0)
else:
   print("Result differs in {} rows from reference:".format(len(differing_rows)))
   print(differing_rows)
   for row in differing_rows[:5]:
      print("\n- " + str(reference[row]))
      print("+ " + str(array[row]))
   if len(differing_rows) > 5:
      print("\n[…]")

   sys.exit(1)
