Skip to content

Instantly share code, notes, and snippets.

@odarbelaeze
Last active August 29, 2015 14:04
Show Gist options
  • Save odarbelaeze/be452a4cf2899cd97115 to your computer and use it in GitHub Desktop.
Save odarbelaeze/be452a4cf2899cd97115 to your computer and use it in GitHub Desktop.
Test of binary IO using netCDF in python against text IO.
# coding=utf-8
"""
Little example using netfdf-python to store Monte Carlo time-series data.
"""
from netCDF4 import Dataset
from numpy.random import uniform
from numpy.lib.npyio import savetxt
def create_test(name, zlib=False):
"""
Creates a test netCDF file called `name`
"""
test = Dataset(name, 'w')
test.createDimension('seed', None)
test.createDimension('mcs', None)
test.createDimension('temp', None)
test.createVariable('seed', 'i8', ('seed', ))
test.createVariable('temp', 'f8', ('temp', ))
ene = test.createVariable(
'ene', 'f8',
('seed', 'temp', 'mcs', ),
zlib=zlib)
mag = test.createVariable(
'mag', 'f8',
('seed', 'temp', 'mcs', ),
zlib=zlib)
for seed in range(10):
for temp in range(100):
ene[seed, temp, 0:100000] = uniform(100000)
mag[seed, temp, 0:100000] = uniform(100000)
test.close()
def create_text(name):
"""
Creates a file containing severa milion of data in text format.
"""
ftext = open(name, 'w')
for seed in range(10):
for temp in range(100):
ftext.write(str(seed))
ftext.write(str(temp))
mag = uniform(size=(1, 100000))
ene = uniform(size=(1, 100000))
savetxt(ftext, mag)
savetxt(ftext, ene)
ftext.close()
def main():
"""
Creates a test.nc file and a test_zlib.nc file just to try out netcdf.
"""
from time import time
start = time()
create_test('test.nc')
create_test('test_zlib.nc', zlib=True)
end = time()
print "Elapsed time 2 binary io: ", end - start
start = time()
create_text('test.txt')
end = time()
print "Elapsed time 1 text io: ", end - start
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment