import iris
stash_constraint = iris.AttributeConstraint(STASH='m01s03i004')
pressure_constraint = iris.Constraint(pressure=500.)
air_t = iris.load_cube(files, stash_constraint)
t500 = air_t.extract(pressure_constraint)import iris.fileformats.pp as ppfor field in pp.load(filename):print(field.stash, field.lbcode)
from __future__ import print_function
from glob import glob
import hashlib
import iris
import os.path
from six.moves import cPickle
import six
def cached_load(filenames):
"""
Load iris cubes given the filenames
"""
if isinstance(filenames, six.string_types):
filenames = [filenames]
# Turn the list of filename patterns into a sorted list of unique
# filenames.
filenames = sorted(set(fname for filename_pattern in filenames
for fname in glob(filename_pattern)))
# Hash the files.
hasher = hashlib.sha256()
for fname in filenames:
with open(fname) as fh:
buf = fh.read(65536)
while len(buf) > 0:
hasher.update(buf)
buf = fh.read(65536)
digest = hasher.hexdigest()
cachefile = '.iris_cache_{}_{}'.format(iris.__version__, digest)
if not os.path.exists(cachefile):
print('Loading the cubes. This can take some time!')
result = iris.load(filenames)
with open(cachefile, 'wb') as fh:
cPickle.dump(result, fh, cPickle.HIGHEST_PROTOCOL)
else:
with open(cachefile, 'rb') as fh:
result = cPickle.load(fh)
return result
# Load some actual data to try it out:
print(cached_load(['/path/to/data/engl_*.pp']))
Just to point out, I've not actually triggered data loading at this point, so the pickle files are still referencing the data payload of the original (pp) files.$ time python memoize_load.py
Loading the cubes. This can take some time!
0: unknown / (unknown) (latitude: 481; longitude: 640)
1: surface_downwelling_longwave_flux_assuming_clear_sky / (W m-2) (latitude: 481; longitude: 640)
...
144: y_wind / (m s-1) (model_level_number: 70; latitude: 480; longitude: 1)
real 0m32.09s
$ time python memoize_load.py
0: unknown / (unknown) (latitude: 481; longitude: 640)
1: surface_downwelling_longwave_flux_assuming_clear_sky / (W m-2) (latitude: 481; longitude: 640)
...
144: y_wind / (m s-1) (model_level_number: 70; latitude: 480; longitude: 1)
real 0m2.29s
--
You received this message because you are subscribed to the Google Groups "Iris" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scitools-iri...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.