Пример #1
0
def test_rescale_clip():
    arr = np.array(np.linspace(0.0, 1.5, num=9).reshape(3, 3))
    dtype = np.__dict__['float32']
    rescale_factor = 1.0
    rescaled_arr = rescale(arr, rescale_factor, dtype, clip=True)
    assert rescaled_arr.max() == 1.0
    rescaled_arr = rescale(arr, rescale_factor, dtype, clip=False)
    assert rescaled_arr.max() == 1.5
Пример #2
0
def test_rescale_overflow():
    arr = np.array(np.linspace(0.0, 1.5, num=9).reshape(3, 3))
    dtype = np.__dict__['uint16']
    rescale_factor = 65535
    rescaled_arr = rescale(arr, rescale_factor, dtype, clip=True)
    assert rescaled_arr.max() == 65535
    rescaled_arr = rescale(arr,
                           rescale_factor,
                           np.__dict__['float32'],
                           clip=False)
    assert rescaled_arr.max() == 65535 * 1.5
    with pytest.raises(ValueError):
        # without clipping, this will overflow
        rescaled_arr = rescale(arr, rescale_factor, dtype, clip=False)
Пример #3
0
def test_rescale2():
    arr = np.array(np.linspace(0.0, 1.5, num=9).reshape(3, 3))
    dtype = np.__dict__['uint8']
    rescale_factor = 1.0
    rescaled_arr = rescale(arr, rescale_factor, dtype)
    mask = (rescaled_arr != np.iinfo(dtype).max) & (rescaled_arr != 1.0)

    assert np.all(rescaled_arr) <= np.iinfo(dtype).max
    assert np.all(rescaled_arr) >= 0.0
    assert np.array_equal(rescaled_arr[mask], arr[mask].astype(int))
    assert rescaled_arr.dtype == 'uint8'
Пример #4
0
def _reflectance_worker(open_files, window, ij, g_args):
    """rio mucho worker for reflectance. It reads input
    files and perform reflectance calculations on each window.

    Parameters
    ------------
    open_files: list of rasterio open files
    window: tuples
    g_args: dictionary

    Returns
    ---------
    out: None
        Output is written to dst_path

    """
    data = riomucho.utils.array_stack([
      src.read(window=window).astype(np.float32)
      for src in open_files
    ])

    depth, rows, cols = data.shape

    if g_args['pixel_sunangle']:
        bbox = BoundingBox(
                    *warp.transform_bounds(
                        g_args['src_crs'],
                        {'init': u'epsg:4326'},
                        *open_files[0].window_bounds(window)))

        E = sun_utils.sun_elevation(
                        bbox,
                        (rows, cols),
                        g_args['date_collected'],
                        g_args['time_collected_utc']).reshape(rows, cols, 1)

    else:
        # We're doing whole-scene (instead of per-pixel) sunangle:
        E = np.array([g_args['E'] for i in range(depth)])

    output = toa_utils.rescale(
        reflectance(
            data,
            g_args['M'],
            g_args['A'],
            E,
            g_args['src_nodata']),
        g_args['rescale_factor'],
        g_args['dst_dtype'],
        clip=g_args['clip'])

    return output
Пример #5
0
def test_calculate_radiance2(test_data):
    tif, tif_meta, tif_output, tif_shape, tif_output_meta, mtl = test_data
    M = toa_utils._load_mtl_key(
        mtl,
        ['L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'RADIANCE_MULT_BAND_'],
        5)
    A = toa_utils._load_mtl_key(
        mtl,
        ['L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'RADIANCE_ADD_BAND_'], 5)

    toa = toa_utils.rescale(radiance.radiance(tif, M, A), 55000, np.uint16)
    assert toa.dtype == np.uint16
    assert np.all(toa) < 1.5
    assert np.all(toa) >= 0.0
Пример #6
0
def _radiance_worker(data, window, ij, g_args):
    """
    rio mucho worker for radiance
    TODO: integrate rescaling functionality for
    different output datatypes
    """
    output = toa_utils.rescale(
        radiance(
            data[0],
            g_args['M'],
            g_args['A'],
            g_args['src_nodata']),
        g_args['rescale_factor'],
        g_args['dst_dtype'],
        clip=g_args['clip'])

    return output
Пример #7
0
def test_calculate_radiance(test_data):
    tif, tif_meta, tif_output, tif_shape, tif_output_meta, mtl = test_data

    M = toa_utils._load_mtl_key(
        mtl,
        ['L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'RADIANCE_MULT_BAND_'],
        5)
    A = toa_utils._load_mtl_key(
        mtl,
        ['L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'RADIANCE_ADD_BAND_'], 5)

    assert isinstance(M, float)
    toa = radiance.radiance(tif, M, A)
    toa_rescaled = toa_utils.rescale(toa, 255, np.uint8)
    scale = float(np.iinfo(np.uint16).max) / float(np.iinfo(np.uint8).max)
    tif_out_rescaled = np.clip((tif_output / scale), 0,
                               np.iinfo(np.uint8).max).astype(np.uint8)
    assert toa_rescaled.dtype == np.uint8
    assert np.min(tif_out_rescaled) == np.min(toa_rescaled)
    assert int(np.max(tif_out_rescaled)) == int(np.max(toa_rescaled))
Пример #8
0
def test_calculate_reflectance(test_data):
    tif_b, tif_output_single, mtl = test_data[0], test_data[5], test_data[-1]

    M = toa_utils._load_mtl_key(mtl, [
        'L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'REFLECTANCE_MULT_BAND_'
    ], 5)
    A = toa_utils._load_mtl_key(
        mtl,
        ['L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'REFLECTANCE_ADD_BAND_'],
        5)
    E = toa_utils._load_mtl_key(
        mtl, ['L1_METADATA_FILE', 'IMAGE_ATTRIBUTES', 'SUN_ELEVATION'])

    assert (np.sin(np.radians(E)) <= 1) & (-1 <= np.sin(np.radians(E)))
    assert isinstance(M, float)
    toa = reflectance.reflectance(tif_b, M, A, E)
    toa_rescaled = toa_utils.rescale(toa, 55000.0, np.uint16, clip=False)
    assert toa_rescaled.dtype == np.uint16
    # Note, the test data was created under a rescaling code, hence the fuzziness
    diff = toa_rescaled[310:315, 310:315] - tif_output_single[310:315, 310:315]
    assert diff.max() <= 1
Пример #9
0
def test_calculate_reflectance_uint8(test_data):
    tif_b, tif_output_single, mtl = test_data[0], test_data[5], test_data[-1]

    M = toa_utils._load_mtl_key(mtl, [
        'L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'REFLECTANCE_MULT_BAND_'
    ], 5)
    A = toa_utils._load_mtl_key(
        mtl,
        ['L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'REFLECTANCE_ADD_BAND_'],
        5)
    E = toa_utils._load_mtl_key(
        mtl, ['L1_METADATA_FILE', 'IMAGE_ATTRIBUTES', 'SUN_ELEVATION'])

    assert (np.sin(np.radians(E)) <= 1) & (-1 <= np.sin(np.radians(E)))
    assert isinstance(M, float)
    toa = reflectance.reflectance(tif_b, M, A, E)
    toa_rescaled = toa_utils.rescale(toa, 215, np.uint8)
    scale = float(np.iinfo(np.uint16).max) / float(np.iinfo(np.uint8).max)
    tif_out_rescaled = np.clip((tif_output_single / scale), 0,
                               np.iinfo(np.uint8).max).astype(np.uint8)
    assert toa_rescaled.dtype == np.uint8
    assert np.min(tif_out_rescaled) == np.min(toa_rescaled)
Пример #10
0
def test_calculate_reflectance2(test_data):
    tif_b, tif_shape, mtl = test_data[0], test_data[4], test_data[-1]

    M = toa_utils._load_mtl_key(mtl, [
        'L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'REFLECTANCE_MULT_BAND_'
    ], 5)
    A = toa_utils._load_mtl_key(
        mtl,
        ['L1_METADATA_FILE', 'RADIOMETRIC_RESCALING', 'REFLECTANCE_ADD_BAND_'],
        5)
    date_collected = toa_utils._load_mtl_key(
        mtl, ['L1_METADATA_FILE', 'PRODUCT_METADATA', 'DATE_ACQUIRED'])
    time_collected_utc = toa_utils._load_mtl_key(
        mtl, ['L1_METADATA_FILE', 'PRODUCT_METADATA', 'SCENE_CENTER_TIME'])
    bounds = BoundingBox(*toa_utils._get_bounds_from_metadata(
        mtl['L1_METADATA_FILE']['PRODUCT_METADATA']))
    E = sun_utils.sun_elevation(bounds, tif_shape, date_collected,
                                time_collected_utc)
    toa = reflectance.reflectance(tif_b, M, A, E)
    toa_rescaled = toa_utils.rescale(toa, 55000.0, np.uint16)
    assert toa_rescaled.dtype == np.uint16
    assert np.all(toa_rescaled) < 1.5
    assert np.all(toa_rescaled) >= 0.0
Пример #11
0
def test_rescale_dtype():
    arr = np.array(np.linspace(0.0, 1.5, num=9).reshape(3, 3))
    dtype = np.__dict__['float32']
    rescale_factor = 1.0
    rescaled_arr = rescale(arr, rescale_factor, dtype)
    assert rescaled_arr.dtype == dtype