Skip to content

Commit

Permalink
Updated point image and point collection value tests
Browse files Browse the repository at this point in the history
Calls to the USGS/NED asset were returning None for some versions of the EE api.  The asset may have been changed or is being deprecated.  Switched the tests to check the NASA DEM and USGS 3DEP assets instead.

Also, renamed tests so that utils and landsat tests run first.  There is probably a correct way of doing this in pytest.
  • Loading branch information
cgmorton committed Mar 23, 2024
1 parent 195edd6 commit 21611ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,40 @@ def test_constant_image_value_multiband_bands(expected=10.123456789, tol=0.00000
@pytest.mark.parametrize(
'image_id, xy, scale, expected, tol',
[
['USGS/NED', [-106.03249, 37.17777], 10, 2364.351, 0.001],
['USGS/NED', [-106.03249, 37.17777], 1, 2364.351, 0.001],
['USGS/3DEP/10m', [-106.03249, 37.17777], 30, 2364.169, 0.001],
['USGS/3DEP/10m', [-106.03249, 37.17777], 10, 2364.138, 0.001],
['USGS/3DEP/10m', [-106.03249, 37.17777], 1, 2364.138, 0.001],
['NASA/NASADEM_HGT/001', [-106.03249, 37.17777], 30, 2361, 0.001],
]
)
def test_point_image_value(image_id, xy, scale, expected, tol):
output = utils.point_image_value(ee.Image(image_id).rename('output'), xy)
output = utils.point_image_value(
ee.Image(image_id).select(['elevation'], ['output']), xy, scale
)
assert abs(output['output'] - expected) <= tol


@pytest.mark.parametrize(
'image_id, image_date, xy, scale, expected, tol',
[
# CGM - This test stopped working for a scale of 1 and returns a different
# value for a scale of 10 than the point_image_value() function above.
# This function uses getRegion() instead of a reduceRegion() call,
# so there might have been some sort of change in getRegion().
['USGS/NED', '2012-04-04', [-106.03249, 37.17777], 10, 2364.286, 0.001],
# CGM - The default scale of 1 now returns None/Null for some reason
# ['USGS/NED', '2012-04-04', [-106.03249, 37.17777], 1, 2364.351, 0.001],
['USGS/3DEP/10m', '2012-04-04', [-106.03249, 37.17777], 30, 2364.169, 0.001],
['USGS/3DEP/10m', '2012-04-04', [-106.03249, 37.17777], 10, 2364.097, 0.001],
['USGS/3DEP/10m', '2012-04-04', [-106.03249, 37.17777], 1, 2364.138, 0.001],
['NASA/NASADEM_HGT/001', '2012-04-04', [-106.03249, 37.17777], 30, 2361, 0.001],
]
)
def test_point_coll_value(image_id, image_date, xy, scale, expected, tol):
input_img = ee.Image(image_id).rename(['output'])
# The image must have a system:time_start for this function to work correctly
input_img = (
ee.Image(image_id).select(['elevation'], ['output'])
.set({'system:time_start': ee.Date(image_date).millis()})
)
output = utils.point_coll_value(ee.ImageCollection([input_img]), xy, scale)
assert abs(output['output'][image_date] - expected) <= tol


def test_point_coll_value_no_system_time_start_exception():
# The function should raise an exception for images with no system:time_start
input_img = ee.Image('USGS/3DEP/10m').select(['elevation'], ['output'])
with pytest.raises(Exception):
utils.point_coll_value(ee.ImageCollection([input_img]), [-106, 37], 30)
File renamed without changes.
11 changes: 5 additions & 6 deletions openet/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,8 @@ def list_2_str_ranges(i):
return ','.join(output)


# Should these be test fixtures instead?
# I'm not sure how to make them fixtures and allow input parameters
def constant_image_value(image, crs='EPSG:32613', scale=1):
"""Extract the output value from a calculation done with constant images"""
"""Extract the output value from a "constant" image"""
rr_params = {
'reducer': ee.Reducer.first(),
'geometry': ee.Geometry.Rectangle([0, 0, 10, 10], crs, False),
Expand All @@ -532,7 +530,7 @@ def constant_image_value(image, crs='EPSG:32613', scale=1):


def point_image_value(image, xy, scale=1):
"""Extract the output value from a calculation at a point"""
"""Extract the output value from an image at a point"""
rr_params = {
'reducer': ee.Reducer.first(),
'geometry': ee.Geometry.Point(xy),
Expand All @@ -542,17 +540,18 @@ def point_image_value(image, xy, scale=1):


def point_coll_value(coll, xy, scale=1):
"""Extract the output value from a calculation at a point"""
"""Extract the output value from a collection at a point"""
output = get_info(coll.getRegion(ee.Geometry.Point(xy), scale=scale))

# Structure output to easily be converted to a Pandas dataframe
# First key is band name, second key is the date string
col_dict = {}
info_dict = {}
for i, k in enumerate(output[0][4:]):
col_dict[k] = i + 4
info_dict[k] = {}

for row in output[1:]:
# TODO: Add support for images that don't have a system:time_start
date = datetime.utcfromtimestamp(row[3] / 1000.0).strftime('%Y-%m-%d')
for k, v in col_dict.items():
info_dict[k][date] = row[col_dict[k]]
Expand Down

0 comments on commit 21611ff

Please sign in to comment.