示例#1
0
def fake_index():
    return mk_fake_index(
        products=dict(
            with_gs=mk_sample_product("with_gs", with_grid_spec=True),
            without_gs=mk_sample_product("without_gs", with_grid_spec=False),
        ),
        datasets=[],
    )
示例#2
0
def test_product_dimensions():
    product = mk_sample_product('test_product')
    assert product.grid_spec is None
    assert product.dimensions == ('time', 'y', 'x')

    product = mk_sample_product('test_product', with_grid_spec=True)
    assert product.grid_spec is not None
    assert product.dimensions == ('time', 'y', 'x')
示例#3
0
def test_product_basics():
    product = mk_sample_product('test_product')
    assert product.name == 'test_product'
    assert 'test_product' in str(product)
    assert 'test_product' in repr(product)
    assert product == product
    assert product == mk_sample_product('test_product')
    assert not (product == mk_sample_product('other'))
    assert not (product == [()])
    assert hash(product) == hash(mk_sample_product('test_product'))
    assert 'time' in dir(product.metadata)
def test_testutils_mk_sample():
    pp = mk_sample_product('tt', measurements=[('aa', 'int16', -999),
                                               ('bb', 'float32', np.nan)])
    assert set(pp.measurements) == {'aa', 'bb'}

    pp = mk_sample_product('tt', measurements=['aa', 'bb'])
    assert set(pp.measurements) == {'aa', 'bb'}

    pp = mk_sample_product('tt', measurements=[dict(name=n) for n in ['aa', 'bb']])
    assert set(pp.measurements) == {'aa', 'bb'}

    with pytest.raises(ValueError):
        mk_sample_product('tt', measurements=[None])
示例#5
0
def test_product_dimensions():
    product = mk_sample_product('test_product')
    assert product.grid_spec is None
    assert product.dimensions == ('time', 'y', 'x')

    product = mk_sample_product('test_product', with_grid_spec=True)
    assert product.grid_spec is not None
    assert product.dimensions == ('time', 'y', 'x')

    partial_storage = product.definition['storage']
    partial_storage.pop('tile_size')
    product = mk_sample_product('tt', storage=partial_storage)
    assert product.grid_spec is None
示例#6
0
def test_product_basics():
    product = mk_sample_product('test_product')
    assert product.name == 'test_product'
    assert 'test_product' in str(product)
    assert 'test_product' in repr(product)
    assert product == product
    assert product == mk_sample_product('test_product')
    assert not (product == mk_sample_product('other'))
    assert not (product == [()])
    assert hash(product) == hash(mk_sample_product('test_product'))
    assert 'time' in dir(product.metadata)

    assert product.measurements == product.lookup_measurements()
    assert product.lookup_measurements(
        ['red']) == product.lookup_measurements('red')
示例#7
0
def test_without_lineage_sources():
    def mk_sample(v):
        return dict(lineage={
            'source_datasets': v,
            'a': 'a',
            'b': 'b'
        },
                    aa='aa',
                    bb=dict(bb='bb'))

    spec = mk_sample_product('tt')

    x = {'a': 1}
    assert without_lineage_sources(x, spec) == x
    assert without_lineage_sources(x, spec, inplace=True) == x

    x = {'a': 1, 'lineage': {}}
    assert without_lineage_sources(x, spec) == x
    assert without_lineage_sources(x, spec, inplace=True) == x

    x = mk_sample(1)
    assert without_lineage_sources(x, spec) != x
    assert x['lineage']['source_datasets'] == 1

    x = mk_sample(2)
    assert without_lineage_sources(x, spec, inplace=True) == x
    assert x['lineage']['source_datasets'] == {}

    assert mk_sample(10) != mk_sample({})
    assert without_lineage_sources(mk_sample(10), spec) == mk_sample({})
    assert without_lineage_sources(mk_sample(10), spec,
                                   inplace=True) == mk_sample({})

    # check behaviour when `sources` is not defined for the type
    no_sources_type = MetadataType(
        {
            'name':
            'eo',
            'description':
            'Sample',
            'dataset':
            dict(
                id=['id'],
                label=['ga_label'],
                creation_time=['creation_dt'],
                measurements=['image', 'bands'],
                format=['format', 'name'],
            )
        },
        dataset_search_fields={})

    assert without_lineage_sources(mk_sample(10),
                                   no_sources_type) == mk_sample(10)
    assert without_lineage_sources(mk_sample(10),
                                   no_sources_type,
                                   inplace=True) == mk_sample(10)
示例#8
0
def test_product_scale_factor():
    product = mk_sample_product('test', measurements=[dict(name='red',
                                                           scale_factor=33,
                                                           add_offset=-5)])
    assert product.validate(product.definition) is None
    assert product.measurements['red'].scale_factor == 33
    assert product.measurements['red'].add_offset == -5
    attrs = product.measurements['red'].dataarray_attrs()
    assert attrs['scale_factor'] == 33
    assert attrs['add_offset'] == -5
示例#9
0
def test_product_nodata_nan():
    # When storing .nan to JSON in DB it becomes a string with value "NaN"
    # Make sure it is converted back to real NaN
    product = mk_sample_product('test',
                                measurements=[
                                    dict(name='_nan',
                                         dtype='float32',
                                         nodata='NaN'),
                                    dict(name='_inf',
                                         dtype='float32',
                                         nodata='Infinity'),
                                    dict(name='_neg_inf',
                                         dtype='float32',
                                         nodata='-Infinity'),
                                ])
    for m in product.measurements.values():
        assert isinstance(m.nodata, float)

    assert numpy.isnan(product.measurements['_nan'].nodata)
    assert product.measurements['_inf'].nodata == numpy.inf
    assert product.measurements['_neg_inf'].nodata == -numpy.inf
示例#10
0
def eo3_product(eo3_metadata):
    return mk_sample_product("eo3_product", metadata_type=eo3_metadata)
示例#11
0
def test_product_load_hints():
    product = mk_sample_product('test_product',
                                load=dict(crs='epsg:3857',
                                          resolution={'x': 10, 'y': -10}))

    assert 'load' in product.definition
    assert DatasetType.validate(product.definition) is None

    hints = product._extract_load_hints()
    assert hints['crs'] == geometry.CRS('epsg:3857')
    assert hints['resolution'] == (-10, 10)
    assert 'align' not in hints

    product = mk_sample_product('test_product',
                                load=dict(crs='epsg:3857',
                                          align={'x': 5, 'y': 6},
                                          resolution={'x': 10, 'y': -10}))

    hints = product.load_hints()
    assert hints['output_crs'] == geometry.CRS('epsg:3857')
    assert hints['resolution'] == (-10, 10)
    assert hints['align'] == (6, 5)
    assert product.default_crs == geometry.CRS('epsg:3857')
    assert product.default_resolution == (-10, 10)
    assert product.default_align == (6, 5)

    product = mk_sample_product('test_product',
                                load=dict(crs='epsg:4326',
                                          align={'longitude': 0.5, 'latitude': 0.6},
                                          resolution={'longitude': 1.2, 'latitude': -1.1}))

    hints = product.load_hints()
    assert hints['output_crs'] == geometry.CRS('epsg:4326')
    assert hints['resolution'] == (-1.1, 1.2)
    assert hints['align'] == (0.6, 0.5)

    # check it's cached
    assert product.load_hints() is product.load_hints()

    # check schema: crs and resolution are compulsory
    for k in ('resolution', 'crs'):
        doc = deepcopy(product.definition)
        assert DatasetType.validate(doc) is None

        doc['load'].pop(k)
        assert k not in doc['load']

        with pytest.raises(InvalidDocException):
            DatasetType.validate(doc)

    # check GridSpec leakage doesn't happen for fully defined gridspec
    product = mk_sample_product('test', with_grid_spec=True)
    assert product.grid_spec is not None
    assert product.load_hints() == {}

    # check for fallback into partially defined `storage:`
    product = mk_sample_product('test', storage=dict(
        crs='EPSG:3857',
        resolution={'x': 10, 'y': -10}))
    assert product.grid_spec is None
    assert product.default_resolution == (-10, 10)
    assert product.default_crs == geometry.CRS('EPSG:3857')

    # check for fallback into partially defined `storage:`
    # no resolution -- no hints
    product = mk_sample_product('test', storage=dict(
        crs='EPSG:3857'))
    assert product.grid_spec is None
    assert product.load_hints() == {}

    # check misspelled load hints
    product = mk_sample_product('test_product',
                                load=dict(crs='epsg:4326',
                                          resolution={'longtude': 1.2, 'latitude': -1.1}))
    assert product.load_hints() == {}