#!/usr/bin/env python
import os
import subprocess
import hashlib
import numpy

print "Begin testing driver"

indent='  '

# Switch to first test directory
os.chdir("verification/tutorial_barotropic_gyre/input/")

cmd="python gendata.py"
p = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
out, err = p.communicate()
result=out.split('\n')
for lin in result:
 print indent, lin

# Lets see if hash matches reference
print indent,'md5sum: ',' windx.sin_y',hashlib.md5(open('windx.sin_y', 'rb').read()).hexdigest()
print indent,'md5sum: ',' topog.box',hashlib.md5(open('topog.box', 'rb').read()).hexdigest()

# Lets check if statistics are within expected accuracy
with open('windx.sin_y', 'rb') as fid:
        tau=numpy.fromfile(fid, ">f8")
print indent,'stats(std,max,min): ','windx.sin_y',numpy.std(tau),numpy.max(tau),numpy.min(tau)

with open('topog.box', 'rb') as fid:
        topo=numpy.fromfile(fid, ">f8")
print indent,'stats(std,max,min): ','topog.box',numpy.std(topo),numpy.max(topo),numpy.min(topo)

print "End testing driver"
