def load_array(filename, meta=None, band_specs=None, reader=None): '''Create ElmStore from HDF4 / 5 or NetCDF files or TIF directories Parameters: :filename: filename (HDF4 / 5 or NetCDF) or directory name (TIF) :meta: meta data from "filename" already loaded :band_specs: list of strings or earthio.BandSpec objects :reader: named reader from earthio - one of: ('tif', 'hdf4', 'hdf5', 'netcdf') Returns: :es: ElmStore (xarray.Dataset) with bands specified by band_specs as DataArrays in "data_vars" attribute ''' ftype = reader or _find_file_type(filename) if meta is None: if ftype == 'tif': meta = _load_meta(filename, ftype, band_specs=band_specs) else: meta = _load_meta(filename, ftype) if ftype == 'netcdf': return load_netcdf_array(filename, meta, band_specs=band_specs) elif ftype == 'hdf5': return load_hdf5_array(filename, meta, band_specs=band_specs) elif ftype == 'hdf4': return load_hdf4_array(filename, meta, band_specs=band_specs) elif ftype == 'tif': return load_dir_of_tifs_array(filename, meta, band_specs=band_specs) elif ftype == 'hdf': try: es = load_hdf4_array(filename, meta, band_specs=band_specs) except Exception as e: logger.info( 'NOTE: guessed HDF4 type. Failed: {}. \nTrying HDF5'.format( repr(e))) es = load_hdf5_array(filename, meta, band_specs=band_specs) return es
def test_read_array(): meta = load_dir_of_tifs_meta(TIF_DIR, layer_specs) dset = load_dir_of_tifs_array(TIF_DIR, meta, layer_specs) for var in dset.data_vars: sample = getattr(dset, var) mean_y = np.mean(sample.y) mean_x = np.mean(sample.x) layer_names = np.array([b.name for b in layer_specs]) assertions_on_layer_metadata(sample.attrs)
def test_reader_kwargs(): layer_specs_kwargs = [] for b in layer_specs: b = b.get_params() b['buf_xsize'], b['buf_ysize'] = 200, 300 layer_specs_kwargs.append(LayerSpec(**b)) meta = load_dir_of_tifs_meta(TIF_DIR, layer_specs_kwargs) dset = load_dir_of_tifs_array(TIF_DIR, meta, layer_specs_kwargs) for b in dset.layer_order: assert getattr(dset, b).values.shape == (300, 200)
def test_reader_kwargs(): band_specs_kwargs = [] for b in band_specs: b = attr.asdict(b) b['buf_xsize'], b['buf_ysize'] = 200, 300 band_specs_kwargs.append(BandSpec(**b)) meta = load_dir_of_tifs_meta(TIF_DIR, band_specs_kwargs) es = load_dir_of_tifs_array(TIF_DIR, meta, band_specs_kwargs) for b in es.band_order: assert getattr(es, b).values.shape == (300, 200)
def test_read_array(): meta = load_dir_of_tifs_meta(TIF_DIR, band_specs) es = load_dir_of_tifs_array(TIF_DIR, meta, band_specs) for var in es.data_vars: sample = getattr(es, var) mean_y = np.mean(sample.y) mean_x = np.mean(sample.x) band_names = np.array([b.name for b in band_specs]) assert sorted((mean_x, sample.canvas.bounds.left, sample.canvas.bounds.right))[1] == mean_x assert sorted((mean_y, sample.canvas.bounds.top, sample.canvas.bounds.bottom))[1] == mean_y assert np.all(band_names == es.band_order) assertions_on_band_metadata(sample.attrs)