def test_loader_latitudes(tmpdir): path = str(tmpdir / "eida50_20190417.nc") with netCDF4.Dataset(path, "w") as dataset: _eida50(dataset, TIMES, LONS, LATS) loader = eida50.Loader(eida50.Locator(path, eida50.Database())) result = loader.latitudes with netCDF4.Dataset(path) as dataset: expect = dataset.variables["latitude"][:] np.testing.assert_array_almost_equal(expect, result)
def test_loader_image(tmpdir): path = str(tmpdir / "file_20190417.nc") with netCDF4.Dataset(path, "w") as dataset: _eida50(dataset, TIMES, LONS, LATS) time = dt.datetime(2019, 4, 17, 12) loader = eida50.Loader(eida50.Locator(path, eida50.Database())) image = loader._image(time) result = set(image.keys()) expect = set(["x", "y", "dw", "dh", "image"]) assert expect == result
def test_locator_find_given_a_single_file(tmpdir): valid_date = dt.datetime(2019, 1, 1) path = str(tmpdir / "test-eida50-20190101.nc") pattern = str(tmpdir / "test-eida50*.nc") times = [valid_date] with netCDF4.Dataset(path, "w") as dataset: set_times(dataset, times) locator = eida50.Locator(pattern, eida50.Database()) paths = locator.glob() found_path, index = locator.find(paths, valid_date) assert found_path == path assert index == 0
def test_find_given_multiple_files(tmpdir): pattern = str(tmpdir / "test-eida50*.nc") dates = [ dt.datetime(2019, 1, 1), dt.datetime(2019, 1, 2), dt.datetime(2019, 1, 3), ] for date in dates: path = str(tmpdir / "test-eida50-{:%Y%m%d}.nc".format(date)) with netCDF4.Dataset(path, "w") as dataset: set_times(dataset, [date]) valid_date = dt.datetime(2019, 1, 2, 0, 14) locator = eida50.Locator(pattern, eida50.Database()) paths = locator.glob() found_path, index = locator.find(paths, valid_date) expect = str(tmpdir / "test-eida50-20190102.nc") assert found_path == expect assert index == 0
def test_locator_find_given_no_files_raises_notfound(tmpdir): any_date = dt.datetime.now() pattern = str(tmpdir / "nofile.nc") locator = eida50.Locator(pattern, eida50.Database()) with pytest.raises(FileNotFound): locator.find([], any_date)