def test_reconciliation(self): "Tests that reconciling multiple rasters works correctly for multiple views." lims = {} ns = {} hfs = {} views = [] for order in ['xy','yx']: for first in ['+','-']: for second in ['+','-']: view = order[0]+first+order[1]+second views.append(view) lim_lo = np.random.uniform(-1,-.5,size=2) lim_hi = np.random.uniform(.5,1,size=2) lim = [lim_lo[0], lim_hi[0], lim_lo[1], lim_hi[1]] n = np.random.randint(200,500,size=2) lims[view]=np.array(lim)*180./np.pi ns[view]=n x = np.linspace(lim[0],lim[1],n[0]) y = np.linspace(lim[2],lim[3],n[1]) print view fname, hf = write_datafile(x,y,view=view) hfs[view] = hf lon, lat, layers = reconcile_multiple_rasters([hfs[v].root for v in views], thin=2) # Check that the layers all have the right shape assert(np.all([(l.shape == (len(lon),len(lat))) for l in layers])) # Check that the limits are inside the joint intersection assert(np.all([(lon.min() >= l[0]) * (lon.max() <= l[1]) * (lat.min() >= l[2]) * (lat.max() <= l[3]) for l in lims.itervalues()])) # Check that the coarseness is maximal assert([(lims[v][1]-lims[v][0])/float(ns[v][0]-1) <= lon[1]-lon[0] for v in views]) assert([(lims[v][2]-lims[v][3])/float(ns[v][1]-1) <= lat[1]-lat[0] for v in views]) lonmin = np.min([l[0] for l in lims.itervalues()]) lonmax = np.max([l[1] for l in lims.itervalues()]) latmin = np.min([l[2] for l in lims.itervalues()]) latmax = np.max([l[3] for l in lims.itervalues()]) import pylab as pl for i in xrange(len(views)): pl.figure(figsize=(10,6)) pl.subplot(1,2,2) pl.imshow(grid_convert(layers[i],'x+y+','y+x+'),extent=[lon.min(),lon.max(),lat.min(),lat.max()]) pl.title(views[i] + ': ' + str(hfs[views[i]].root.data.shape)) pl.axis([lonmin, lonmax, latmin, latmax]) pl.subplot(1,2,1) pl.imshow(grid_convert(hfs[views[i]].root.data[:], views[i], 'y+x+'), extent = lims[views[i]]) pl.plot([lon.min(), lon.max(), lon.max(), lon.min()],[lat.min(), lat.min(), lat.max(), lat.max()],'r-') pl.axis([lonmin, lonmax, latmin, latmax])
def make_covering_raster(thin=1, env_variables=(), **kwds): a='MODIS-hdf5/raw-data.land-water.geographic.world.version-4' names = [a]+env_variables names.sort() cache_fname = 'rasters_%i_%s_.hdf5'%(thin, hashlib.sha1('~*~lala~*~oolala'.join(names)).hexdigest()) cache_found = False if 'anopheles-caches' in os.listdir('.'): if cache_fname in os.listdir('anopheles-caches'): hf = tables.openFile(os.path.join('anopheles-caches',cache_fname)) lon = hf.root.lon[:] lat = hf.root.lat[:] layers = [] for n in [a] + env_variables: layers.append(getattr(hf.root,os.path.split(n)[1])[:]) hf.close() cache_found = True if not cache_found: lon,lat,layers = reconcile_multiple_rasters([get_datafile(n) for n in [a] + env_variables], thin=thin) hf = tables.openFile(os.path.join('anopheles-caches',cache_fname),'w') hf.createArray('/','lon',lon) hf.createArray('/','lat',lat) names = [a] + env_variables for i in xrange(len(layers)): hf_arr=hf.createCArray('/',os.path.split(names[i])[1],shape=layers[i].shape,chunkshape=layers[i].shape,atom=tables.FloatAtom(),filters=tables.Filters(complevel=1)) hf_arr[:] = layers[i] hf.close() mask = np.round(layers[0]).astype(bool) img_extent = [lon.min(), lat.min(), lon.max(), lat.max()] lat_grid, lon_grid = np.meshgrid(lat*np.pi/180.,lon*np.pi/180.) x=np.dstack([lon_grid,lat_grid]+[l for l in layers[1:]]) if x.shape[:-1] != mask.shape: raise RuntimeError, 'Shape mismatch in mask and other layers.' return mask, x, img_extent