Esempio n. 1
0
    def _validate_band_specs(self, band_specs, name):
        '''Validate "band_specs"'''

        if all(isinstance(bs, BandSpec) for bs in band_specs):
            return band_specs
        if not band_specs or not isinstance(band_specs, (tuple, list)):
            raise ElmConfigError(
                'data_sources:{} gave band_specs which are not a '
                'list {}'.format(name, band_specs))
        if not all(isinstance(bs, (dict, string_types)) for bs in band_specs):
            raise ElmConfigError(
                'Expected "band_specs" to be a list of dicts or list of strings'
            )

        new_band_specs = []
        field_names = [x.name for x in attr.fields(BandSpec)]
        for band_spec in band_specs:
            if isinstance(band_spec, string_types):
                new_band_specs.append(
                    BandSpec(
                        **{
                            'search_key': 'sub_dataset_name',
                            'search_value': band_spec,
                            'name': band_spec
                        }))
            elif not all(k in field_names for k in band_spec):
                raise ElmConfigError(
                    "band_spec {} did not have keys: {}".format(
                        band_spec, field_names))
            else:
                new_band_specs.append(BandSpec(**band_spec))
        return new_band_specs
Esempio n. 2
0
def test_read_using_list_of_variables():
    for nc_file in NETCDF_FILES:
        meta = load_netcdf_meta(nc_file)
        ds = load_netcdf_array(nc_file, meta, variables_list)
        _validate_array_test_result(ds)
        variables_list2 = [BandSpec('', '', v) for v in variables_list]
        ds = load_netcdf_array(nc_file, meta, variables_list2)
        _validate_array_test_result(ds)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
def test_read_meta():
    TIF_DIR = os.path.dirname(TIF_FILES[0])
    for tif in TIF_FILES:
        raster, meta = load_tif_meta(tif)
        assert hasattr(raster, 'read')
        assert hasattr(raster, 'width')
        band_specs_with_band_8 = band_specs + [BandSpec('name', '_B8.TIF', 'band_8')]
        meta = load_dir_of_tifs_meta(TIF_DIR, band_specs_with_band_8)
        band_meta = meta['band_meta']
        heights_names = [(m['height'], m['name']) for m in band_meta]
        # band 8 is panchromatic with 15 m resolution
        # other bands have 30 m resolution.  They
        # have the same bounds, so band 8 has 4 times as many pixels
        heights_names.sort(key=lambda x:x[0])
        assert heights_names[-1][-1].endswith('_B8.TIF')
Esempio n. 6
0
def get_band_specs(filename):
    if os.path.basename(filename).startswith('3B-MO'):
        sub_dataset_names = ('/precipitation',)
    else:
        sub_dataset_names = ('HQobservationTime',
                             'HQprecipSource',
                             'HQprecipitation',
                             'IRkalmanFilterWeight',
                             'IRprecipitation',
                             'precipitationCal',
                             'precipitationUncal',
                             'probabilityLiquidPrecipitation',)
    band_specs = []
    for sub in sub_dataset_names:
        band_specs.append(BandSpec(search_key='sub_dataset_name',
                               search_value=sub + '$', # line ender regex
                               name=sub,
                               meta_to_geotransform="earthio.util:grid_header_to_geo_transform",
                               stored_coords_order=("x", "y")))
    return sub_dataset_names, band_specs
Esempio n. 7
0
import attr
import numpy as np
import pytest

from earthio.tif import (load_dir_of_tifs_meta, load_dir_of_tifs_array,
                         load_tif_meta, ls_tif_files)
from earthio.tests.util import (ELM_HAS_EXAMPLES, ELM_EXAMPLE_DATA_PATH,
                                TIF_FILES, assertions_on_metadata,
                                assertions_on_band_metadata)

from earthio.util import BandSpec

TIF_DIR = os.path.dirname(TIF_FILES[0])
band_specs = [
    BandSpec('name', '_B1.TIF', 'band_1'),
    BandSpec('name', '_B2.TIF', 'band_2'),
    BandSpec('name', '_B3.TIF', 'band_3'),
    BandSpec('name', '_B4.TIF', 'band_4'),
    BandSpec('name', '_B5.TIF', 'band_5'),
    BandSpec('name', '_B6.TIF', 'band_6'),
    BandSpec('name', '_B7.TIF', 'band_7'),
    BandSpec('name', '_B9.TIF', 'band_9'),
    BandSpec('name', '_B10.TIF', 'band_10'),
    BandSpec('name', '_B11.TIF', 'band_11'),
]


@pytest.mark.skipif(not ELM_HAS_EXAMPLES,
                    reason='elm-data repo has not been cloned')
def test_read_meta():
Esempio n. 8
0
import pytest

from earthio.hdf4 import (load_hdf4_meta,
                              load_hdf4_array)

from earthio.util import BandSpec

from earthio.tests.util import (ELM_HAS_EXAMPLES,
                                    ELM_EXAMPLE_DATA_PATH,
                                    HDF4_FILES,
                                    assertions_on_metadata,
                                    assertions_on_band_metadata)

kwargs = {}
band_specs = [
    BandSpec('long_name', 'Band 1 ', 'band_1', **kwargs),
    BandSpec('long_name', 'Band 2 ', 'band_2', **kwargs),
    BandSpec('long_name', 'Band 3 ', 'band_3', **kwargs),
    BandSpec('long_name', 'Band 4 ', 'band_4', **kwargs),
    BandSpec('long_name', 'Band 5 ', 'band_5', **kwargs),
    BandSpec('long_name', 'Band 7 ', 'band_7', **kwargs),
    BandSpec('long_name', 'Band 8 ', 'band_8', **kwargs),
    BandSpec('long_name', 'Band 10 ', 'band_10', **kwargs),
    BandSpec('long_name', 'Band 11 ', 'band_11', **kwargs),
]

@pytest.mark.parametrize('hdf', HDF4_FILES or [])
@pytest.mark.skipif(not ELM_HAS_EXAMPLES,
               reason='elm-data repo has not been cloned')
def test_read_meta(hdf):
    meta = load_hdf4_meta(hdf)