コード例 #1
0
def test_glintcorr_8():
    """
    Ensure that GlintCorr will raise an Exception if
    fmask information can't be found in odc-metadata.yaml

    This will test GlintCorr.get_fmask_file()
    """
    # load the odc_meta_file
    meta_dict = load_yaml_file(odc_meta_file)

    # change "oa_fmask" key
    meta_dict["measurements"]["no_mask"] = meta_dict["measurements"].pop(
        "oa_fmask")

    # save to data_path where the bands exist. This file will be deleted
    mod_meta_file = data_path / "ga_ls8c_aard_3-2-0_091086_MODIFIED.odc-metadata.yaml"
    with open(mod_meta_file, "w") as ofid:
        yaml.dump(meta_dict, ofid)

    with pytest.raises(Exception) as excinfo:
        deglint.GlintCorr(mod_meta_file, product)
    assert "Unable to extract fmask" in str(excinfo)

    # remove yaml
    mod_meta_file.unlink()
コード例 #2
0
def test_load_bands_10():
    """
    test the outputs of load_bands()
    """
    g = deglint.GlintCorr(odc_meta_file, product)

    b2_file = g.band_list[g.band_ids.index("2")]
    b3_file = g.band_list[g.band_ids.index("3")]
    b4_file = g.band_list[g.band_ids.index("4")]

    spectral_cube, meta = rio_funcs.load_bands(
        [b2_file, b3_file, b4_file], g.scale_factor, False
    )

    assert isinstance(spectral_cube, np.ndarray)
    assert isinstance(meta, dict)
    assert isinstance(meta["crs"], CRS)
    assert isinstance(meta["transform"], Affine)
    assert spectral_cube.ndim == 3
    assert spectral_cube.shape[0] == meta["count"]
    assert spectral_cube.shape[1] == meta["height"]
    assert spectral_cube.shape[2] == meta["width"]
    assert spectral_cube.dtype == np.dtype(meta["dtype"])
    for z in range(spectral_cube.shape[0]):
        assert not np.all(spectral_cube[z, :, :] == meta["nodata"])
コード例 #3
0
def test_empty_band(tmp_path):
    """
    Ensure that hedley_2005() raises an Exception if
    the VIS and NIR band only contain nodata pixels
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    hedley_dir = tmp_path / "HEDLEY"
    hedley_dir.mkdir()

    with pytest.raises(Exception) as excinfo:
        g.hedley_2005(
            vis_bands=["7"],  # dummy band only contains nodata (-999)
            corr_band="6",
            roi_shpfile=shp_file,
            odir=hedley_dir,
            water_val=5,
            plot=False,
        )
    assert "only contains a single value" in str(excinfo)

    with pytest.raises(Exception) as excinfo:
        g.hedley_2005(
            vis_bands=["3"],
            corr_band="7",  # dummy band only contains nodata (-999)
            roi_shpfile=shp_file,
            odir=hedley_dir,
            water_val=5,
            plot=False,
        )
    assert "only contains a single value" in str(excinfo)
コード例 #4
0
def test_load_bands_11():
    """
    test that scaling actually occurs in load_bands()
    """
    g = deglint.GlintCorr(odc_meta_file, product)

    b2_file = g.band_list[g.band_ids.index("2")]

    scaled_im, meta = rio_funcs.load_bands([b2_file], g.scale_factor, True)
    unscaled_im, meta = rio_funcs.load_bands([b2_file], g.scale_factor, False)

    scaled_im = np.array(scaled_im[0, :, :], copy=True, order="K")
    unscaled_im = np.array(unscaled_im[0, :, :], copy=True, order="K", dtype=np.float32)

    ix = (unscaled_im > 0) & (unscaled_im <= g.scale_factor)
    ratio = unscaled_im[ix] / scaled_im[ix]  # e.g. 834/0.0834 = 10,000
    ratio_mean = ratio.mean()
    range_about_mean = 100.0 * (ratio.max() - ratio.min()) / ratio_mean

    # Due to rounding errors, associated with converting to np.float32
    # during the scaling, we can't expect the ratio of all pixels to
    # equal g.scale_factor. Hence test that the mean == g.scale_factor
    # and that percentage range about the mean <= 0.0001.
    # A value of 0.0001 for a scale_factor of 10,000 implies that:
    # ratio.min()  =  9999.99
    # ratio.mean() = 10000.00
    # ratio.max()  = 10000.01
    assert np.all(ratio_mean == g.scale_factor)
    assert range_about_mean <= 0.0001
コード例 #5
0
def test_mnir_image():
    """
    Check that the generated deglinted band is nearly identical
    to the expected deglinted band
    """
    # Initiate the sunglint correction class
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    # ---------------------- #
    #     NIR subtraction    #
    # ---------------------- #
    mnir_xarrlist = g.glint_subtraction(
        vis_bands=["3"],
        corr_band="6",
        water_val=5,
    )

    sungc_band = mnir_xarrlist[0].lmbadj_green.values  # 3D array

    # path to expected sunglint corrected output from NIR subtraction
    exp_sungc_band = (
        data_path / "MINUS_NIR" /
        "ga_ls8c_lmbadj_3-2-0_091086_2014-11-06_final_band03-deglint-600m.tif")

    # ensure that all valid sungint corrected pixels match expected
    with rasterio.open(exp_sungc_band, "r") as exp_sungc_ds:
        urd_band = urd(sungc_band[0, :, :], exp_sungc_ds.read(1),
                       exp_sungc_ds.nodata)
        assert urd_band.max() < 0.001
コード例 #6
0
def test_geom_same_dims(tmp_path):
    """
    Ensure that the Cox and Munk module will raise an
    Exception if a view-solar geometry band does not
    have the same dimensions as the other bands.
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    cxmk_dir = tmp_path / "COX_MUNK"
    cxmk_dir.mkdir()

    # create a small view-zenith tiff
    resmpl_tifs, rio_meta = create_halved_band(g.find_file("satellite_view"), cxmk_dir)

    # cox_munk() should raise an Exception as vzen band does not
    # have the same shape as the solar-zenith and relative-azimuth
    # bands
    with pytest.raises(Exception) as excinfo:
        g.cox_munk(
            vis_bands=["3"],
            vzen_file=resmpl_tifs[0],
            wind_speed=5,
            water_val=5,
        )
    assert "Dimension mismatch" in str(excinfo)
コード例 #7
0
def test_hedley_bands(tmp_path):
    """
    Ensure that hedley_2005() raises an Exception if
    the specifued vis_band_id and corr_band do not exist
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    hedley_dir = tmp_path / "HEDLEY"
    hedley_dir.mkdir()

    # check VIS band
    with pytest.raises(Exception) as excinfo:
        g.hedley_2005(
            vis_bands=["20"],  # this band doesn't exist
            corr_band="6",
            roi_shpfile=shp_file,
            odir=hedley_dir,
            water_val=5,
            plot=False,
        )
    assert "is missing from bands" in str(excinfo)

    # check NIR band
    with pytest.raises(Exception) as excinfo:
        g.hedley_2005(
            vis_bands=["3"],
            corr_band="20",  # this band doesn't exist
            roi_shpfile=shp_file,
            odir=hedley_dir,
            water_val=5,
            plot=False,
        )
    assert "is missing from bands" in str(excinfo)
コード例 #8
0
def test_resample_1():
    """
    Ensures that rio_funcs.resample_bands() raises and Exception when:
        * resample_spatial_res <= 0
        * if save=True and odir=None
        * if save=True and odir does not exist.

    """
    g = deglint.GlintCorr(odc_meta_file, product)

    b3_file = g.band_list[g.band_ids.index("3")]
    with rasterio.open(b3_file, "r") as ds:
        spatial_res = ds.transform.a

    # ------------------------- #
    #   Raise Exception when    #
    # resample_spatial_res <= 0 #
    # ------------------------- #
    with pytest.raises(Exception) as excinfo:
        rio_funcs.resample_bands(
            bandlist=[b3_file],
            resample_spatial_res=0,
            resample_option=Resampling.nearest,
            load=True,
            save=False,
            odir=None,
        )
    assert "resample_spatial_res must be > 0" in str(excinfo)

    # ------------------------- #
    #   Raise Exception when    #
    #  save=True and odir=None  #
    # ------------------------- #
    with pytest.raises(Exception) as excinfo:
        rio_funcs.resample_bands(
            bandlist=[b3_file],
            resample_spatial_res=3 * spatial_res,
            resample_option=Resampling.nearest,
            load=False,
            save=True,
            odir=None,
        )
    assert "save requested for resampled geotiff, but odir not specified" in str(excinfo)

    # ------------------------------- #
    #       Raise Exception when      #
    #  save=True & not odir.exists()  #
    # ------------------------------- #
    non_path = Path("/freo/give/em/the/old/heave/ho")
    with pytest.raises(Exception) as excinfo:
        rio_funcs.resample_bands(
            bandlist=[b3_file],
            resample_spatial_res=3 * spatial_res,
            resample_option=Resampling.nearest,
            load=False,
            save=True,
            odir=non_path,
        )
    assert f"{non_path} does not exist" in str(excinfo)
コード例 #9
0
def test_glintcorr_1(tmp_path):
    """
    Ensure that GlintCorr will raise an Exception if
    ".odc-metadata.yaml" not in yaml basename
    """
    random_yaml = tmp_path / "random.yaml"
    with pytest.raises(Exception) as excinfo:
        deglint.GlintCorr(random_yaml, product)
    assert "is not a .odc-metadata.yaml" in str(excinfo)
コード例 #10
0
def test_glintcorr_6():
    """
    Ensure that GlintCorr will raise an Exception if
    geotiffs associated with products that were not
    packaged/indexed are requested. Here, lmbadj
    products were packaged, however, lmbskyg bands
    are requested.

    This will test GlintCorr.get_band_list()
    """
    with pytest.raises(Exception) as excinfo:
        deglint.GlintCorr(odc_meta_file, "lmbskyg")
    assert "Could not find any geotifs in" in str(excinfo)
コード例 #11
0
def test_nodata_band():
    """
    Ensure that the Cox and Munk module raises an
    Exception if the input band only contains nodata
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    with pytest.raises(Exception) as excinfo:
        g.cox_munk(
            vis_bands=["7"],  # dummy band only contains nodata (-999)
            wind_speed=5,
            water_val=5,
        )
    assert "only contains a single value" in str(excinfo)
コード例 #12
0
def test_cxmk_bands():
    """
    Ensure that the Cox and Munk module raises an
    Exception if the specified vis_band_id does not exist
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    with pytest.raises(Exception) as excinfo:
        g.cox_munk(
            vis_bands=["20"],  # this band id doesn't exist
            wind_speed=5,
            water_val=5,
        )
    assert "is missing from bands" in str(excinfo)
コード例 #13
0
def test_windspeed_lt0():
    """
    Ensure that the Cox and Munk module will raise an
    Exception if the wind speed < 0 m/s
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    with pytest.raises(Exception) as excinfo:
        g.cox_munk(
            vis_bands=["3"],
            wind_speed=-1,
            water_val=5,
        )
    assert "wind_speed must be greater than 0 m/s" in str(excinfo)
コード例 #14
0
def test_quicklook_gen(tmp_path):
    """
    Test that the quicklook rgb is as expected
    """
    dwnscale_factor = 3
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    # generate a quicklook
    rgb_im, rgb_meta = quicklook_rgb(
        rgb_bandlist=g.rgb_bandlist,
        scale_factor=g.scale_factor,
        dwnscale_factor=dwnscale_factor,
    )

    rgb_shape = rgb_im.shape

    assert isinstance(rgb_im, np.ndarray) is True
    assert rgb_im.ndim == 3
    assert rgb_shape[2] == 3
    assert rgb_im.dtype == np.uint8
    assert rgb_meta["band_1"] == "ga_ls8c_lmbadj_3-2-0_091086_2014-11-06_final_band04"
    assert rgb_meta["band_2"] == "ga_ls8c_lmbadj_3-2-0_091086_2014-11-06_final_band03"
    assert rgb_meta["band_3"] == "ga_ls8c_lmbadj_3-2-0_091086_2014-11-06_final_band02"

    ix_b3 = g.band_ids.index("3")
    # check that the downsampling procedure worked!
    with rasterio.open(g.band_list[ix_b3], "r") as b3_ds:
        b3_meta = b3_ds.meta.copy()
        assert rgb_meta["height"] == b3_meta["height"] // dwnscale_factor
        assert rgb_meta["width"] == b3_meta["width"] // dwnscale_factor
        assert rgb_meta["transform"].b == b3_meta["transform"].b
        assert rgb_meta["transform"].c == b3_meta["transform"].c
        assert rgb_meta["transform"].d == b3_meta["transform"].d
        assert rgb_meta["transform"].f == b3_meta["transform"].f
        assert rgb_meta["transform"].a == b3_meta["transform"].a * dwnscale_factor
        assert rgb_meta["transform"].e == b3_meta["transform"].e * dwnscale_factor

    # check Exception is raised if dwnscale_factor < 1
    with pytest.raises(ValueError) as excinfo:
        quicklook_rgb(
            rgb_bandlist=g.rgb_bandlist,
            scale_factor=g.scale_factor,
            dwnscale_factor=0.999,
        )
    assert "dwnscale_factor must be a float >= 1" in str(excinfo)
コード例 #15
0
def test_resample_4(tmp_path):
    """
    Ensures that the outputs from resample_bands() are as expected.
    In this test, load=True and save=True, meaning that the output
    values should be:
    resmpl_ofiles: list, len=2
    spectral_cube: np.ndarray
        * ndim = 3 as a TWO bands is used.
        * same dtype as input band
        * dimensions 1/3 the size of input band
    metad: dict
    """
    g = deglint.GlintCorr(odc_meta_file, product)

    downscale_factor = 3
    b2_file = g.band_list[g.band_ids.index("2")]
    b3_file = g.band_list[g.band_ids.index("3")]

    with rasterio.open(b3_file, "r") as ds:
        # in the test dataset, b2 and b3 have the same ds.meta
        ini_meta = ds.meta.copy()
        spatial_res = ini_meta["transform"].a

    resmpl_dir = tmp_path / "resample_test"
    resmpl_dir.mkdir()

    resmpl_ofiles, resmpl_cube, resmpl_metad = rio_funcs.resample_bands(
        bandlist=[b2_file, b3_file],
        resample_spatial_res=downscale_factor * spatial_res,
        resample_option=Resampling.nearest,
        load=True,
        save=True,
        odir=resmpl_dir,
    )

    # check resmpl_ofiles
    assert isinstance(resmpl_ofiles, list)
    assert len(resmpl_ofiles) == 2
    for resmpl_tiff in resmpl_ofiles:
        assert isinstance(resmpl_tiff, Path)
        assert resmpl_tiff.suffix == ".tif"
        assert resmpl_tiff.exists()

    check_resampled_array(resmpl_cube, resmpl_metad, ini_meta, downscale_factor)
    check_resampled_metad(resmpl_cube, resmpl_metad, ini_meta)
コード例 #16
0
def test_glint_images():
    """
    Tes that the generated glint reflectance is nearly
    identical to expected glint reflectance
    """
    exp_glint_band = (
        data_path
        / "COX_MUNK"
        / "ga_ls8c_lmbadj_3-2-0_091086_2014-11-06_final_cm_glint.tif"
    )

    # Initiate the sunglint correction class
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    vzen_file = g.find_file("satellite_view")
    szen_file = g.find_file("solar_zenith")
    razi_file = g.find_file("relative_azimuth")

    # load the required geometry images
    vzen_im, vzen_meta = load_singleband(vzen_file)
    szen_im, szen_meta = load_singleband(szen_file)
    razi_im, razi_meta = load_singleband(razi_file)
    cm_meta = vzen_meta.copy()

    # cox and munk:
    p_glint, p_fresnel = coxmunk_backend(
        view_zenith=vzen_im,
        solar_zenith=szen_im,
        relative_azimuth=razi_im,
        wind_speed=5,
        return_fresnel=False,
    )

    # convert p_glint & p_fresnel from np.float32 to np.int16
    p_nodata = cm_meta["nodata"]  # this is np.nan
    p_glint[p_glint != p_nodata] *= g.scale_factor
    p_glint[p_glint == p_nodata] = -999.0
    p_glint = np.array(p_glint, order="C", dtype=np.int16)

    # ensure that all valid sungint corrected pixels match expected
    with rasterio.open(exp_glint_band, "r") as eglint_ds:
        urd_glint = urd(p_glint, eglint_ds.read(1), eglint_ds.nodata)
        assert urd_glint.max() < 0.001
コード例 #17
0
def test_glintcorr_7(tmp_path):
    """
    Ensure that GlintCorr will raise an Exception if
    the bands specified in .odc-metadata.yaml do not
    exists.

    This will test GlintCorr.get_band_list()
    """
    # load the odc_meta_file
    meta_dict = load_yaml_file(odc_meta_file)

    # save to tmp_path where the bands won't exist
    mod_meta_file = tmp_path / "ga_ls8c_aard_3-2-0_091086_MODIFIED.odc-metadata.yaml"
    with open(mod_meta_file, "w") as ofid:
        yaml.dump(meta_dict, ofid)

    band2 = tmp_path / meta_dict["measurements"]["lmbadj_blue"]["path"]
    with pytest.raises(Exception) as excinfo:
        deglint.GlintCorr(mod_meta_file, product)
    assert f"Error: {band2} does not exist." in str(excinfo)
コード例 #18
0
def test_resample_5(tmp_path):
    """
    Ensures that the outputs from resample_bands() are as expected.
    In this test, load=False and save=True, meaning that the output
    values should be:
    resmpl_ofiles: list, len=2
    spectral_cube: NoneType
    metad: NoneType
    """
    g = deglint.GlintCorr(odc_meta_file, product)

    downscale_factor = 3
    b2_file = g.band_list[g.band_ids.index("2")]
    b3_file = g.band_list[g.band_ids.index("3")]

    with rasterio.open(b3_file, "r") as ds:
        # in the test dataset, b2 and b3 have the same ds.meta
        spatial_res = ds.meta["transform"].a

    resmpl_dir = tmp_path / "resample_test"
    resmpl_dir.mkdir()

    resmpl_ofiles, resmpl_cube, resmpl_metad = rio_funcs.resample_bands(
        bandlist=[b2_file, b3_file],
        resample_spatial_res=downscale_factor * spatial_res,
        resample_option=Resampling.nearest,
        load=False,
        save=True,
        odir=resmpl_dir,
    )

    # check resmpl_ofiles
    assert isinstance(resmpl_ofiles, list)
    assert len(resmpl_ofiles) == 2
    for resmpl_tiff in resmpl_ofiles:
        assert isinstance(resmpl_tiff, Path)
        assert resmpl_tiff.suffix == ".tif"
        assert resmpl_tiff.exists()

    assert isinstance(resmpl_cube, type(None))
    assert isinstance(resmpl_metad, type(None))
コード例 #19
0
def test_glintcorr_4(tmp_path):
    """
    Ensure that GlintCorr will raise an Exception if
    the sensor in the .odc-metadata.yaml isn't supported.

    This will test GlintCorr.check_sensor()
    """
    # load the odc_meta_file
    meta_dict = load_yaml_file(odc_meta_file)

    # change sensor to something random
    meta_dict["properties"]["eo:platform"] = "DESIS-999"

    # save changes
    mod_meta_file = tmp_path / "ga_ls8c_aard_3-2-0_091086_MODIFIED.odc-metadata.yaml"
    with open(mod_meta_file, "w") as ofid:
        yaml.dump(meta_dict, ofid)

    with pytest.raises(Exception) as excinfo:
        deglint.GlintCorr(mod_meta_file, product)
    assert "Supported sensors are" in str(excinfo)
コード例 #20
0
def test_resample_3():
    """
    Ensures that the outputs from resample_bands() are as expected.
    In this test, load=True and save=False, meaning that the output
    values should be:
    resmpl_ofiles: NoneType (as save=False)
    spectral_cube: np.ndarray
        * ndim = 3 as a TWO bands is used.
        * same dtype as input band
        * dimensions 1/3 the size of input band
    metad: dict
    """
    g = deglint.GlintCorr(odc_meta_file, product)

    downscale_factor = 3
    b2_file = g.band_list[g.band_ids.index("2")]
    b3_file = g.band_list[g.band_ids.index("3")]

    with rasterio.open(b3_file, "r") as ds:
        # in the test dataset, b2 and b3 have the same ds.meta
        ini_meta = ds.meta.copy()
        spatial_res = ini_meta["transform"].a

    resmpl_ofiles, resmpl_cube, resmpl_metad = rio_funcs.resample_bands(
        bandlist=[b2_file, b3_file],
        resample_spatial_res=downscale_factor * spatial_res,
        resample_option=Resampling.nearest,
        load=True,
        save=False,
        odir=None,
    )

    # check resmpl_ofiles
    assert resmpl_ofiles is None

    # check resampled output
    check_resampled_array(resmpl_cube, resmpl_metad, ini_meta, downscale_factor)

    # check the resampled metadata
    check_resampled_metad(resmpl_cube, resmpl_metad, ini_meta)
コード例 #21
0
def test_empty_inputs():
    """
    Ensure that glint_subtraction() raises and Exception if
    the VIS and NIR band only contain nodata pixels
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    with pytest.raises(Exception) as excinfo:
        g.glint_subtraction(
            vis_bands=["3"],
            corr_band="7",  # this dummy band only contains nodata
            water_val=5,
        )
    assert "only contains a single value" in str(excinfo)

    with pytest.raises(Exception) as excinfo:
        g.glint_subtraction(
            vis_bands=["7"],  # this dummy band only contains nodata
            corr_band="6",
            water_val=5,
        )
    assert "only contains a single value" in str(excinfo)
コード例 #22
0
def test_mnir_bands():
    """
    Ensure that glint_subtraction() raises and Exception if
    the specified vis_band_id/corr_band do not exist
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    with pytest.raises(Exception) as excinfo:
        g.glint_subtraction(
            vis_bands=["20"],  # this band id doesn't exist
            corr_band="6",
            water_val=5,
        )
    assert "is missing from bands" in str(excinfo)

    with pytest.raises(Exception) as excinfo:
        g.glint_subtraction(
            vis_bands=["3"],
            corr_band="20",  # this band id doesn't exist
            water_val=5,
        )
    assert "is missing from bands" in str(excinfo)
コード例 #23
0
def test_load_singleband_8():
    """
    test load_singleband() function
    """
    g = deglint.GlintCorr(odc_meta_file, product)
    b3_file = g.band_list[g.band_ids.index("3")]

    im, meta = rio_funcs.load_singleband(b3_file)

    # check for consistency between im and it's metadata dict
    assert isinstance(im, np.ndarray)
    assert isinstance(meta, dict)
    assert isinstance(meta["crs"], CRS)
    assert isinstance(meta["transform"], Affine)
    assert im.ndim == 2
    assert im.shape[0] == meta["height"]
    assert im.shape[1] == meta["width"]
    assert im.dtype == np.dtype(meta["dtype"])
    assert meta["count"] == 1
    assert meta["driver"] == "GTiff"
    assert meta["nodata"] == -999.0
    assert not np.all(im == meta["nodata"])
コード例 #24
0
def test_nodata_vzen(tmp_path):
    """
    Ensure that the Cox and Munk module raises an
    Exception if any of the solar-view geometry
    band only contains nodata. We will test this
    with the sensor view-zenith.
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    cxmk_dir = tmp_path / "COX_MUNK"
    cxmk_dir.mkdir()

    # create empty view-zenith tiff
    dum_vzen = cxmk_dir / "ga_ls8c_oa_3-2-0_091086_2014-11-06_final_DUMMY_view-zenith.tif"

    with rasterio.open(g.find_file("satellite_view"), "r") as vzen_ds:
        kwargs = vzen_ds.meta.copy()

        # write dummy geotiff
        nrows = vzen_ds.height
        ncols = vzen_ds.width
        dtype = np.dtype(kwargs["dtype"])
        nodata = kwargs["nodata"]  # np.nan

        arr = np.zeros([nrows, ncols], order="C", dtype=dtype)
        arr[:] = nodata

        with rasterio.open(dum_vzen, "w", **kwargs) as dst:
            dst.write(arr, 1)

    # specify dummy band
    with pytest.raises(Exception) as excinfo:
        g.cox_munk(
            vis_bands=["3"],
            vzen_file=dum_vzen,  # dummy band only contains nodata (np.nan)
            wind_speed=5,
            water_val=5,
        )
    assert "only contains a single value" in str(excinfo)
コード例 #25
0
def test_hedley_image(tmp_path):
    """
    Test if the generated deglinted band is nearly identical
    to expected deglinted band
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)
    hedley_dir = tmp_path / "HEDLEY"
    hedley_dir.mkdir()

    # ------------------ #
    # Hedley et al. 2005 #
    # ------------------ #
    # deglint the vis bands using band 6
    hedley_xarrlist = g.hedley_2005(
        vis_bands=["3"],
        corr_band="6",
        roi_shpfile=shp_file,
        odir=hedley_dir,
        water_val=5,
        plot=False,
    )

    sungc_band = hedley_xarrlist[0].lmbadj_green.values  # 3D array

    # path to expected sunglint corrected output from Hedley et al.
    exp_sungc_band = (
        data_path / "HEDLEY" /
        "ga_ls8c_lmbadj_3-2-0_091086_2014-11-06_final_band03-deglint-600m.tif")

    # ensure that all valid sungint corrected pixels match expected
    with rasterio.open(exp_sungc_band, "r") as exp_sungc_ds:
        urd_band = urd(sungc_band[0, :, :], exp_sungc_ds.read(1),
                       exp_sungc_ds.nodata)
        assert urd_band.max() < 0.001

    # plot=False. Ensure that no png was created in hedley_dir
    png_list = list(hedley_dir.glob("Correlation_*_vs_*.png"))
    assert len(png_list) == 0
コード例 #26
0
def test_hedley_plot(tmp_path):
    """
    Check if a correlation plot (png) is generated. The
    contents of the png are not checked
    """
    hedley_dir = tmp_path / "HEDLEY"
    hedley_dir.mkdir()

    g = deglint.GlintCorr(odc_meta_file, sub_product)
    g.hedley_2005(
        vis_bands=["3"],
        corr_band="6",
        roi_shpfile=shp_file,
        odir=hedley_dir,
        water_val=5,
        plot=True,  # a plot should be generated in hedley_dir
    )

    # The above function should have generated a png file in
    # hedley_dir with the following naming convention Correlation_*_vs_*.png
    png_list = list(hedley_dir.glob("Correlation_*_vs_*.png"))

    assert len(png_list) == 1
コード例 #27
0
def test_glintcorr_5(tmp_path):
    """
    Ensure that GlintCorr will raise an Exception if
    GlintCorr is unable to extract the overpass
    datetime information from .odc-metadata.yaml

    This will test GlintCorr.get_overpass_datetime()
    """
    # load the odc_meta_file
    meta_dict = load_yaml_file(odc_meta_file)

    # change "datetime" key
    meta_dict["properties"]["overpass_time"] = meta_dict["properties"].pop(
        "datetime")

    # save changes
    mod_meta_file = tmp_path / "ga_ls8c_aard_3-2-0_091086_MODIFIED.odc-metadata.yaml"
    with open(mod_meta_file, "w") as ofid:
        yaml.dump(meta_dict, ofid)

    with pytest.raises(Exception) as excinfo:
        deglint.GlintCorr(mod_meta_file, product)
    assert "Unable to extract overpass datetime" in str(excinfo)
コード例 #28
0
def test_glintcorr_2(tmp_path):
    """
    Ensure that GlintCorr will raise an Exception if
    it couldn't find the "measurement" key in
    .odc-metadata.yaml. The dictionary contained in
    this key has the required band names.

    This will test GlintCorr.get_meas_dict()
    """
    # load the odc_meta_file
    meta_dict = load_yaml_file(odc_meta_file)

    # change "measurement" key
    meta_dict["image_data"] = meta_dict.pop("measurements")

    # save changes
    mod_meta_file = tmp_path / "ga_ls8c_aard_3-2-0_091086_MODIFIED.odc-metadata.yaml"
    with open(mod_meta_file, "w") as ofid:
        yaml.dump(meta_dict, ofid)

    with pytest.raises(Exception) as excinfo:
        deglint.GlintCorr(mod_meta_file, product)
    assert "neither image nor measurements keys were found in" in str(excinfo)
コード例 #29
0
def test_fake_shp(tmp_path):
    """
    Ensure that hedley_2005() raises an Exception if
    the input shapefile isn't really a shapefile
    """
    g = deglint.GlintCorr(odc_meta_file, sub_product)

    hedley_dir = tmp_path / "HEDLEY"
    hedley_dir.mkdir()

    fake_shp = hedley_dir / "fake.shp"
    with open(fake_shp, "w", encoding="utf-8") as fid:
        fid.write("bogus_geometry")

    with pytest.raises(Exception) as excinfo:
        g.hedley_2005(
            vis_bands=["3"],
            corr_band="6",
            roi_shpfile=fake_shp,
            odir=hedley_dir,
            water_val=5,
            plot=False,
        )
    assert "not recognized as a supported file format" in str(excinfo)
コード例 #30
0
def test_glintcorr_3(tmp_path):
    """
    Ensure that GlintCorr will raise an Exception if
    it can't find the sensor information from the
    .odc-metadata.yaml.

    This will test GlintCorr.get_sensor()
    """
    # load the odc_meta_file
    meta_dict = load_yaml_file(odc_meta_file)

    # change "sensor" key
    meta_dict["properties"]["sensor_info"] = meta_dict["properties"].pop(
        "eo:platform")

    # save changes

    mod_meta_file = tmp_path / "ga_ls8c_aard_3-2-0_091086_MODIFIED.odc-metadata.yaml"
    with open(mod_meta_file, "w") as ofid:
        yaml.dump(meta_dict, ofid)

    with pytest.raises(Exception) as excinfo:
        deglint.GlintCorr(mod_meta_file, product)
    assert "Unable to extract sensor name" in str(excinfo)