Ejemplo n.º 1
0
def test_landsat_c1_sr_cloud_mask_snow(img_value, snow, expected):
    input_img = ee.Image.constant(int(img_value, 2)).rename(['pixel_qa'])
    input_args = {'input_img': input_img}
    if snow is not None:
        input_args['snow_flag'] = snow
    output_img = common.landsat_c1_sr_cloud_mask(**input_args)
    assert utils.constant_image_value(output_img)['pixel_qa'] == expected
Ejemplo n.º 2
0
def test_aggregate_to_daily_values_single_band(src_values,
                                               time_values,
                                               expected,
                                               tol=0.01):
    """Test daily aggregation function for single-band constant images"""
    image_list = src_images(src_values, time_values)
    time_list = [
        utils.date_0utc(ee.Date(time)).millis().getInfo()
        for time in time_values
    ]

    # Dates can be ISO Date string or milliseconds since epoch
    # Use the date strings to get 0 UTC dates and better match model calls
    start_date = ee.Date(min(time_values)).format('yyyy-MM-dd')
    end_date = ee.Date(max(time_values)).advance(1, 'day').format('yyyy-MM-dd')
    # start_date = min(time_values)
    # end_date = max(time_values)

    src_coll = interpolate.aggregate_to_daily(
        ee.ImageCollection.fromImages(image_list),
        start_date,
        end_date,
        agg_type='mean')
    src_image = ee.Image(src_coll.first())

    output = utils.constant_image_value(src_image)
    assert abs(output['src'] - expected) <= tol
    assert output['time'] == 0.5 * sum(time_list)
Ejemplo n.º 3
0
def test_daily_small_interp_days(interp_days,
                                 tgt_value,
                                 tgt_time,
                                 src_values,
                                 src_times,
                                 expected,
                                 tol=0.01):
    """Test the daily method for small interp_days values

    This is mainly an issue when the number of interp_days is less than the
    timestep of the source images but can also be an issue when there are lots
    of images missing due to clouds.
    """
    tgt_coll = ee.ImageCollection([tgt_image(tgt_value, tgt_time)])
    src_coll = ee.ImageCollection.fromImages(src_images(src_values, src_times))
    output_coll = ee.ImageCollection(
        interpolate.daily(tgt_coll,
                          src_coll,
                          interp_days=interp_days,
                          interp_method='linear',
                          use_joins=False))
    output = utils.constant_image_value(ee.Image(output_coll.first()))
    if expected is None:
        assert output['src'] is None
        assert abs(output['tgt'] - tgt_value) <= tol
    else:
        assert abs(output['src'] - expected) <= tol
        assert abs(output['tgt'] - tgt_value) <= tol
Ejemplo n.º 4
0
def test_constant_image_value_multiband(tol=0.000001):
    """Test that a multiband image returns multiple values"""
    expected = 10.123456789
    input_img = ee.Image.constant([expected, expected + 1])
    output = utils.constant_image_value(input_img)
    assert abs(output['constant_0'] - expected) <= tol
    assert abs(output['constant_1'] - (expected + 1)) <= tol
Ejemplo n.º 5
0
def test_landsat_c1_toa_cloud_mask_cirrus(img_value, cirrus, expected):
    input_img = ee.Image.constant(int(img_value, 2)).rename(['BQA'])
    input_args = {'input_img': input_img}
    if cirrus is not None:
        input_args['cirrus_flag'] = cirrus
    output_img = common.landsat_c1_toa_cloud_mask(**input_args)
    assert utils.constant_image_value(ee.Image(output_img))['BQA'] == expected
Ejemplo n.º 6
0
def test_constant_image_value_multiband_bands(tol=0.000001):
    """Test that the band names are carried through on a multiband image"""
    expected = 10.123456789
    input_img = ee.Image.constant([expected, expected + 1])\
        .rename(['foo', 'bar'])
    output = utils.constant_image_value(input_img)
    assert abs(output['foo'] - expected) <= tol
    assert abs(output['bar'] - (expected + 1)) <= tol
Ejemplo n.º 7
0
def test_daily_use_joins_true(tgt_value,
                              tgt_time,
                              src_values,
                              src_times,
                              expected,
                              tol=0.01):
    """Test that output with use_joins=True is the same as use_joins=False"""
    tgt_coll = ee.ImageCollection([tgt_image(tgt_value, tgt_time)])
    src_coll = ee.ImageCollection.fromImages(src_images(src_values, src_times))
    output_coll = interpolate.daily(tgt_coll,
                                    src_coll,
                                    interp_days=32,
                                    interp_method='linear',
                                    use_joins=True)
    output = utils.constant_image_value(ee.Image(output_coll.first()))
    assert abs(output['src'] - expected) <= tol
    assert abs(output['tgt'] - tgt_value) <= tol
Ejemplo n.º 8
0
def test_daily_compute_product_true(tgt_value,
                                    tgt_time,
                                    src_values,
                                    src_times,
                                    expected,
                                    tol=0.01):
    """Test if the compute_product flag returns the product bands"""
    tgt_coll = ee.ImageCollection([tgt_image(tgt_value, tgt_time)])
    src_coll = ee.ImageCollection.fromImages(src_images(src_values, src_times))
    output_coll = interpolate.daily(tgt_coll,
                                    src_coll,
                                    interp_days=32,
                                    interp_method='linear',
                                    use_joins=False,
                                    compute_product=True)
    output = utils.constant_image_value(ee.Image(output_coll.first()))
    # assert abs(output['src'] - expected) <= tol
    # assert abs(output['tgt'] - tgt_value) <= tol
    assert abs(output['src_1'] - expected * tgt_value) <= tol
Ejemplo n.º 9
0
def test_daily_interp_days_use_joins(interp_days,
                                     tgt_value,
                                     tgt_time,
                                     src_values,
                                     src_times,
                                     expected,
                                     tol=0.01):
    """Test the daily method for small interp_days values"""
    tgt_coll = ee.ImageCollection([tgt_image(tgt_value, tgt_time)])
    src_coll = ee.ImageCollection.fromImages(src_images(src_values, src_times))
    output_coll = ee.ImageCollection(
        interpolate.daily(tgt_coll,
                          src_coll,
                          interp_days=interp_days,
                          interp_method='linear',
                          use_joins=True))
    output = utils.constant_image_value(ee.Image(output_coll.first()))
    if expected is None:
        assert output['src'] is None
        assert abs(output['tgt'] - tgt_value) <= tol
    else:
        assert abs(output['src'] - expected) <= tol
        assert abs(output['tgt'] - tgt_value) <= tol
Ejemplo n.º 10
0
def test_aggregate_to_daily_values_multi_band(src_values,
                                              time_values,
                                              expected,
                                              tol=0.01):
    """Test daily aggregation function for multi-band constant images"""
    image_list = []
    time_list = []
    for src, time in zip(src_values, time_values):
        time_0utc = utils.date_0utc(ee.Date(time)).millis()
        image = ee.Image.constant(src).double()\
            .addBands(ee.Image.constant(time_0utc).double())\
            .rename(['etrf', 'etof', 'time'])\
            .set({'system:index': ee.Date(time).format('yyyyMMdd'),
                  'system:time_start': time})
        image_list.append(image)
        time_list.append(time_0utc.getInfo())

    # Dates can be ISO Date string or milliseconds since epoch
    # Use the date strings to get 0 UTC dates and better match model calls
    start_date = ee.Date(min(time_values)).format('yyyy-MM-dd')
    end_date = ee.Date(max(time_values)).advance(1, 'day').format('yyyy-MM-dd')
    # start_date = min(time_values)
    # end_date = max(time_values)

    output_coll = interpolate.aggregate_to_daily(
        ee.ImageCollection.fromImages(image_list),
        start_date,
        end_date,
        agg_type='mean')
    output_image = ee.Image(output_coll.first())\
        .select([0, 1, 2], ['etrf', 'etof', 'time'])

    output = utils.constant_image_value(output_image)
    assert abs(output['etrf'] - expected[0]) <= tol
    assert abs(output['etof'] - expected[1]) <= tol
    assert output['time'] == 0.5 * sum(time_list)
Ejemplo n.º 11
0
def test_constant_image_value_band_name(tol=0.000001):
    """Test that a custom band name is carried through"""
    expected = 10.123456789
    input_img = ee.Image.constant(expected).rename('foo')
    output = utils.constant_image_value(input_img)
    assert abs(output['foo'] - expected) <= tol
Ejemplo n.º 12
0
def test_constant_image_value(tol=0.000001):
    expected = 10.123456789
    input_img = ee.Image.constant(expected)
    output = utils.constant_image_value(input_img)
    assert abs(output['constant'] - expected) <= tol
Ejemplo n.º 13
0
def test_landsat_c1_sr_cloud_mask(img_value, expected):
    input_img = ee.Image.constant(int(img_value, 2)).rename(['pixel_qa'])
    output_img = common.landsat_c1_sr_cloud_mask(input_img)
    assert utils.constant_image_value(output_img)['pixel_qa'] == expected
Ejemplo n.º 14
0
def test_sentinel2_sr_cloud_mask(img_value, expected):
    input_img = ee.Image.constant(int(img_value, 2)).rename(['QA60'])
    output_img = common.sentinel2_sr_cloud_mask(input_img)
    assert utils.constant_image_value(ee.Image(output_img))['QA60'] == expected