1# benchmark reads and writes, with and without compression. 2# tests all four supported file formats. 3from numpy.random.mtrand import uniform 4import netCDF4 5from timeit import Timer 6import os, sys 7 8# create an n1dim by n2dim by n3dim random array. 9n1dim = 30 10n2dim = 15 11n3dim = 73 12n4dim = 144 13ntrials = 10 14sys.stdout.write('reading and writing a %s by %s by %s by %s random array ..\n'%(n1dim,n2dim,n3dim,n4dim)) 15sys.stdout.write('(average of %s trials)\n\n' % ntrials) 16array = uniform(size=(n1dim,n2dim,n3dim,n4dim)) 17 18 19def write_netcdf(filename,complevel,lsd): 20 file = netCDF4.Dataset(filename,'w',format='NETCDF4') 21 file.createDimension('n1', n1dim) 22 file.createDimension('n2', n2dim) 23 file.createDimension('n3', n3dim) 24 file.createDimension('n4', n4dim) 25 foo = file.createVariable('data',\ 26 'f8',('n1','n2','n3','n4'),\ 27 zlib=True,shuffle=True,complevel=complevel,\ 28 least_significant_digit=lsd) 29 foo[:] = array 30 file.close() 31 32def read_netcdf(filename): 33 file = netCDF4.Dataset(filename) 34 data = file.variables['data'][:] 35 file.close() 36 37lsd = None 38sys.stdout.write('using least_significant_digit %s\n\n' % lsd) 39for complevel in range(0,10,2): 40 sys.stdout.write('testing compression with complevel %s...\n' % complevel) 41 # writing. 42 t = Timer("write_netcdf('test.nc',%s,%s)" % (complevel,lsd),"from __main__ import write_netcdf") 43 sys.stdout.write('writing took %s seconds\n' %\ 44 repr(sum(t.repeat(ntrials,1))/ntrials)) 45 # test reading. 46 t = Timer("read_netcdf('test.nc')","from __main__ import read_netcdf") 47 sys.stdout.write('reading took %s seconds\n' % 48 repr(sum(t.repeat(ntrials,1))/ntrials)) 49 # print out size of resulting files. 50 sys.stdout.write('size of test.nc = %s\n'%repr(os.stat('test.nc').st_size)) 51 52complevel = 4 53sys.stdout.write('\nusing complevel %s\n\n' % complevel) 54for lsd in range(1,6): 55 sys.stdout.write('testing compression with least_significant_digit %s...\n' % lsd) 56 # writing. 57 t = Timer("write_netcdf('test.nc',%s,%s)" % (complevel,lsd),"from __main__ import write_netcdf") 58 sys.stdout.write('writing took %s seconds\n' %\ 59 repr(sum(t.repeat(ntrials,1))/ntrials)) 60 # test reading. 61 t = Timer("read_netcdf('test.nc')","from __main__ import read_netcdf") 62 sys.stdout.write('reading took %s seconds\n' % 63 repr(sum(t.repeat(ntrials,1))/ntrials)) 64 # print out size of resulting files. 65 sys.stdout.write('size of test.nc = %s\n'%repr(os.stat('test.nc').st_size)) 66