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(hdf): meta = load_hdf4_meta(hdf) dset = load_hdf4_array(hdf, meta, layer_specs) for layer in dset.data_vars: sample = getattr(dset, layer) mean_y = np.mean(sample.y) mean_x = np.mean(sample.x) layer_names = np.array([b.name for b in layer_specs]) assert sample.y.size == 1200 assert sample.x.size == 1200 assert len(dset.data_vars) == len(layer_specs) assert np.all(dset.layer_order == [x.name for x in layer_specs]) assertions_on_layer_metadata(sample.attrs) dset2 = load_hdf4_array(hdf, meta, layer_specs=None) assert len(dset2.data_vars) > len(dset.data_vars)
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_hdf4_meta(HDF4_FILES[0]) dset = load_hdf4_array(HDF4_FILES[0], 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_hdf4_meta(HDF4_FILES[0]) es = load_hdf4_array(HDF4_FILES[0], meta, band_specs_kwargs) for b in es.band_order: assert getattr(es, b).values.shape == (300, 200)
def test_read_array(hdf): meta = load_hdf4_meta(hdf) es = load_hdf4_array(hdf, meta, band_specs) for band in es.data_vars: sample = getattr(es, band) 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 sample.y.size == 1200 assert sample.x.size == 1200 assert len(es.data_vars) == len(band_specs) assert np.all(es.band_order == [x.name for x in band_specs]) assertions_on_band_metadata(sample.attrs) es2 = load_hdf4_array(hdf, meta, band_specs=None) assert len(es2.data_vars) > len(es.data_vars)