Skip to content

Instantly share code, notes, and snippets.

@arbennett
Last active May 2, 2019 14:52
Show Gist options
  • Save arbennett/93b08c68b111680878e3 to your computer and use it in GitHub Desktop.
Save arbennett/93b08c68b111680878e3 to your computer and use it in GitHub Desktop.
Comparison of ncdump vs numpy for comparing datasets
#!/usr/bin/python2
import os
import sys
import time
import numpy as np
import subprocess
import matplotlib.pyplot as plt
from netCDF4 import Dataset
# Method one, just use numpy
var_name = "thk"
start = time.time()
test = Dataset("dome.0496.p256.out.hopper.nc","r", format='NETCDF4')
bench = Dataset("dome.0496.p256.out.titan.nc","r", format='NETCDF4')
test_thk = np.array(test.variables[var_name][:], dtype="float64")
bench_thk = np.array(bench.variables[var_name][:], dtype="float64")
diff = np.ndarray.flatten(test_thk - bench_thk)
if diff.any():
test_thk
print(" Numpy DataType : " + str(diff.dtype))
print(" Size of data : " + str(diff.size))
print("Total difference : " + str(np.sum(diff)))
print(" Max difference : " + str(np.amax(np.absolute(diff))))
else:
print("no change")
test.close()
bench.close()
print(" Method 1 took : " + str(time.time() - start) + " seconds")
print("")
# Method two, use ncdiff
start = time.time()
test = "dome.0496.p256.out.hopper.nc"
bench = "dome.0496.p256.out.titan.nc"
comline = ['ncdiff', '-v', 'thk,velnorm', test, bench, 'temp.nc', '-O']
try:
subprocess.check_call(comline)
except Exception as e:
print(str(e)+ ", File: "+ str(os.path.split(sys.exc_info()[2].tb_frame.f_code.co_filename)[1]))
exit(1)
diff = Dataset("temp.nc",'r', format='NETCDF4')
thk = np.ndarray.flatten(diff.variables[var_name][:])
if thk.any():
print(" Numpy DataType : " + str(diff.variables[var_name].dtype))
print(" Size of data : " + str(thk.size))
print("Total difference : " + str(np.sum(thk)))
print(" Max difference : " + str(np.amax(np.absolute(thk))))
else:
print("no change")
diff.close()
os.remove("temp.nc")
print(" Method 2 took : " + str(time.time() - start) + " seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment