Skip to content

Commit

Permalink
Changed image_value functions to return dict of values instead of values
Browse files Browse the repository at this point in the history
These utils functions are now in line with the functions in openet.core.utils.

Updated some of the test collection IDs to use the new cloud project assets.
  • Loading branch information
cgmorton committed Feb 17, 2024
1 parent 51da573 commit a229fd3
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 183 deletions.
18 changes: 12 additions & 6 deletions openet/ptjpl/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,12 +909,12 @@ def from_image_id(cls, image_id, **kwargs):

@classmethod
def from_landsat_c2_sr(cls, sr_image, cloudmask_args={}, **kwargs):
"""Returns a PT-JPL Image instance from a Landsat Collection 2 SR image
"""Returns a PT-JPL Image instance from a Landsat C02 level 2 (SR) image
Parameters
----------
sr_image : ee.Image
A raw Landsat Collection 2 SR image.
sr_image : ee.Image, str
A raw Landsat Collection 2 level 2 (SR) image or image ID.
cloudmask_args : dict
keyword arguments to pass through to cloud mask function
kwargs : dict
Expand Down Expand Up @@ -964,7 +964,9 @@ def from_landsat_c2_sr(cls, sr_image, cloudmask_args={}, **kwargs):
if 'cloud_score_flag' not in cloudmask_args.keys():
cloudmask_args['cloud_score_flag'] = True
if 'cloud_score_pct' not in cloudmask_args.keys():
cloudmask_args['cloud_score_pct'] = True
cloudmask_args['cloud_score_pct'] = 100
if 'filter_flag' not in cloudmask_args.keys():
cloudmask_args['filter_flag'] = False
# QA_RADSAT band will need to be added above if applying saturated masking
# if 'saturated_flag' not in cloudmask_args.keys():
# cloudmask_args['saturated_flag'] = False
Expand Down Expand Up @@ -1011,7 +1013,6 @@ def from_landsat_c2_sr(cls, sr_image, cloudmask_args={}, **kwargs):
# Instantiate the class
return cls(input_image, **kwargs)

# CGM - Copied from _crop_type() in the SIMS model.py
@lazy_property
def crop_type(self):
"""Crop type
Expand All @@ -1038,6 +1039,10 @@ def crop_type(self):
------
ValueError for unsupported crop_type_sources
Notes
-----
Copied from _crop_type() in the OpenET SIMS model.py
"""
properties = ee.Dictionary()

Expand Down Expand Up @@ -1072,7 +1077,8 @@ def crop_type(self):
crop_type_img = ee.Image(self.crop_type_source).select(['cropland'])
properties = properties.set('id', crop_type_img.get('system:id'))
elif (type(self.crop_type_source) is str and
'projects/openet/crop_type' in self.crop_type_source.lower()):
(('projects/openet/crop_type' in self.crop_type_source.lower()) or
('projects/openet/assets/crop_type' in self.crop_type_source.lower()))):
# Use the crop_type image closest to the image date
crop_coll = ee.ImageCollection(self.crop_type_source)
cdl_year = (
Expand Down
21 changes: 13 additions & 8 deletions openet/ptjpl/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,18 @@ def from_scene_et_fraction(
if type(et_reference_source) is str:
# Assume a string source is an single image collection ID
# not an list of collection IDs or ee.ImageCollection
daily_et_ref_coll = ee.ImageCollection(et_reference_source) \
.filterDate(start_date, end_date) \
daily_et_ref_coll = (
ee.ImageCollection(et_reference_source)
.filterDate(start_date, end_date)
.select([et_reference_band], ['et_reference'])
)
# elif isinstance(et_reference_source, computedobject.ComputedObject):
# # Interpret computed objects as image collections
# daily_et_reference_coll = et_reference_source \
# .filterDate(self.start_date, self.end_date) \
# daily_et_reference_coll = (
# et_reference_source
# .filterDate(self.start_date, self.end_date)
# .select([et_reference_band])
# )
else:
raise ValueError(f'unsupported et_reference_source: {et_reference_source}')

Expand Down Expand Up @@ -212,7 +216,8 @@ def interpolate_prep(img):
if 'count' in variables:
aggregate_coll = openet.core.interpolate.aggregate_to_daily(
image_coll=scene_coll.select(['mask']),
start_date=start_date, end_date=end_date)
start_date=start_date, end_date=end_date,
)
# The following is needed because the aggregate collection can be
# empty if there are no scenes in the target date range but there
# are scenes in the interpolation date range.
Expand All @@ -232,7 +237,7 @@ def interpolate_prep(img):
interp_method=interp_method,
interp_days=interp_days,
use_joins=use_joins,
compute_product=False
compute_product=False,
)

# Compute ET from ETf and ETr (if necessary)
Expand Down Expand Up @@ -506,8 +511,8 @@ def from_scene_et_actual(
# logging.debug('et_reference_resample was not set, default to nearest')
# # raise ValueError('et_reference_resample was not set')

# Assume a string source is an single image collection ID
# not an list of collection IDs or ee.ImageCollection
# Assume a string source is a single image collection ID
# not a list of collection IDs or ee.ImageCollection
daily_et_ref_coll_id = model_args['et_reference_source']
daily_et_ref_coll = (
ee.ImageCollection(daily_et_ref_coll_id)
Expand Down
2 changes: 1 addition & 1 deletion openet/ptjpl/tests/test_a_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_point_coll_value(image_id, image_date, xy, scale, expected, tol):

def test_c_to_k(c=20, k=293.15, tol=0.000001):
output = utils.constant_image_value(utils.c_to_k(ee.Image.constant(c)))
assert abs(output - k) <= tol
assert abs(output['constant'] - k) <= tol


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion openet/ptjpl/tests/test_b_daily_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_day_angle_rad_from_doy_image(rn, hour_of_day, sunrise_hour,
sunrise_hour=ee.Image.constant(sunrise_hour),
daylight_hours=ee.Image.constant(daylight_hours),
))
assert abs(output - expected) <= tol
assert abs(output['constant'] - expected) <= tol


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion openet/ptjpl/tests/test_b_daylight_hours.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_sha_deg_from_doy_lat(doy, latitude, expected, tol=0.000001):
ee.Number(doy), ee.Image.constant(latitude)
))
# output = utils.getinfo(dh.sha_deg_from_doy_lat(ee.Number(doy), ee.Number(latitude)))
assert abs(output - expected) <= tol
assert abs(output['constant'] - expected) <= tol


@pytest.mark.parametrize(
Expand Down
66 changes: 33 additions & 33 deletions openet/ptjpl/tests/test_b_landsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def sr_image(blue=0.2, green=0.2, red=0.2, nir=0.7, swir1=0.2, swir2=0.2, bt=300

# Test the static methods of the class first
# Do these need to be inside the TestClass?
def test_Image_ndvi_band_name():
output = landsat.ndvi(sr_image()).getInfo()['bands'][0]['id']
assert output == 'ndvi'


@pytest.mark.parametrize(
'red, nir, expected',
[
Expand All @@ -49,22 +54,22 @@ def sr_image(blue=0.2, green=0.2, red=0.2, nir=0.7, swir1=0.2, swir2=0.2, bt=300
)
def test_Image_ndvi_calculation(red, nir, expected, tol=0.000001):
output = utils.constant_image_value(landsat.ndvi(sr_image(red=red, nir=nir)))
assert abs(output - expected) <= tol
assert abs(output['ndvi'] - expected) <= tol


def test_Image_ndvi_band_name():
output = landsat.ndvi(sr_image()).getInfo()['bands'][0]['id']
assert output == 'ndvi'
def test_Image_ndwi_band_name():
output = landsat.ndwi(sr_image()).getInfo()['bands'][0]['id']
assert output == 'ndwi'


def test_Image_ndwi_calculation(green=0.2, nir=0.2, expected=0, tol=0.000001):
output = utils.constant_image_value(landsat.ndwi(sr_image(green=green, nir=nir)))
assert abs(output - expected) <= tol
assert abs(output['ndwi'] - expected) <= tol


def test_Image_ndwi_band_name():
output = landsat.ndwi(sr_image()).getInfo()['bands'][0]['id']
assert output == 'ndwi'
def test_Image_mndwi_band_name():
output = landsat.mndwi(sr_image()).getInfo()['bands'][0]['id']
assert output == 'mndwi'


@pytest.mark.parametrize(
Expand All @@ -76,12 +81,12 @@ def test_Image_ndwi_band_name():
)
def test_Image_mndwi_calculation(green, swir2, expected, tol=0.000001):
output = utils.constant_image_value(landsat.mndwi(sr_image(green=green, swir2=swir2)))
assert abs(output - expected) <= tol
assert abs(output['mndwi'] - expected) <= tol


def test_Image_mndwi_band_name():
output = landsat.mndwi(sr_image()).getInfo()['bands'][0]['id']
assert output == 'mndwi'
def test_Image_wri_band_name():
output = landsat.wri(sr_image()).getInfo()['bands'][0]['id']
assert output == 'wri'


@pytest.mark.parametrize(
Expand All @@ -95,12 +100,12 @@ def test_Image_wri_calculation(green, red, nir, swir2, expected, tol=0.000001):
output = utils.constant_image_value(landsat.wri(
sr_image(green=green, red=red, nir=nir, swir2=swir2)
))
assert abs(output - expected) <= tol
assert abs(output['wri'] - expected) <= tol


def test_Image_wri_band_name():
output = landsat.wri(sr_image()).getInfo()['bands'][0]['id']
assert output == 'wri'
def test_Image_emissivity_ptjpl_band_name():
output = landsat.emissivity_ptjpl(sr_image()).getInfo()['bands'][0]['id']
assert output == 'emissivity'


@pytest.mark.parametrize(
Expand All @@ -121,11 +126,11 @@ def test_Image_wri_band_name():
)
def test_Image_emissivity_ptjpl_calculation(red, nir, expected, tol=0.000001):
output = utils.constant_image_value(landsat.emissivity_ptjpl(sr_image(red=red, nir=nir)))
assert abs(output - expected) <= tol
assert abs(output['emissivity'] - expected) <= tol


def test_Image_emissivity_disalexi_band_name():
output = landsat.emissivity_ptjpl(sr_image()).getInfo()['bands'][0]['id']
def test_Image_emissivity_metric_band_name():
output = landsat.emissivity_metric(sr_image()).getInfo()['bands'][0]['id']
assert output == 'emissivity'


Expand All @@ -146,12 +151,12 @@ def test_Image_emissivity_disalexi_band_name():
)
def test_Image_emissivity_metric_calculation(red, nir, expected, tol=0.000001):
output = utils.constant_image_value(landsat.emissivity_metric(sr_image(red=red, nir=nir)))
assert abs(output - expected) <= tol
assert abs(output['emissivity'] - expected) <= tol


def test_Image_emissivity_metric_band_name():
output = landsat.emissivity_metric(sr_image()).getInfo()['bands'][0]['id']
assert output == 'emissivity'
def test_Image_lst_band_name():
output = landsat.lst(sr_image()).getInfo()['bands'][0]['id']
assert output == 'lst'


@pytest.mark.parametrize(
Expand All @@ -163,12 +168,12 @@ def test_Image_emissivity_metric_band_name():
)
def test_Image_lst_calculation(red, nir, bt, expected, tol=0.000001):
output = utils.constant_image_value(landsat.lst(sr_image(red=red, nir=nir, bt=bt)))
assert abs(output - expected) <= tol
assert abs(output['lst'] - expected) <= tol


def test_Image_lst_band_name():
output = landsat.lst(sr_image()).getInfo()['bands'][0]['id']
assert output == 'lst'
def test_Image_albedo_band_name():
output = landsat.albedo_metric(sr_image()).getInfo()['bands'][0]['id']
assert output == 'albedo'


@pytest.mark.parametrize(
Expand All @@ -181,9 +186,4 @@ def test_Image_albedo_calculation(blue, green, red, nir, swir1, swir2, expected,
output = utils.constant_image_value(landsat.albedo_metric(
sr_image(blue=blue, green=green, red=red, nir=nir, swir1=swir1, swir2=swir2)
))
assert abs(output - expected) <= tol


def test_Image_albedo_band_name():
output = landsat.albedo_metric(sr_image()).getInfo()['bands'][0]['id']
assert output == 'albedo'
assert abs(output['albedo'] - expected) <= tol
14 changes: 7 additions & 7 deletions openet/ptjpl/tests/test_b_meteorology.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def test_pascal_to_kilopascal(P_Pa, expected, tol=0.000001):
def test_meteorology_svp(Ta_C, Ea_kPa, expected, tol=0.000001):
# CGM - This test uses images due to .updateMask() call in meteorology
SVP, VPD, RH = meteorology.meteorology(ee.Image.constant(Ta_C), ee.Image.constant(Ea_kPa))
output = utils.constant_image_value(SVP)
assert abs(output - expected) <= tol
output = utils.constant_image_value(SVP.rename(['svp']))
assert abs(output['svp'] - expected) <= tol


@pytest.mark.parametrize(
Expand All @@ -82,11 +82,11 @@ def test_meteorology_svp(Ta_C, Ea_kPa, expected, tol=0.000001):
def test_meteorology_vpd(Ta_C, Ea_kPa, expected, tol=0.000001):
# CGM - This test uses images due to .updateMask() call in meteorology
SVP, VPD, RH = meteorology.meteorology(ee.Image.constant(Ta_C), ee.Image.constant(Ea_kPa))
output = utils.constant_image_value(VPD)
if expected is None and output is None:
output = utils.constant_image_value(VPD.rename(['vpd']))
if expected is None and output['vpd'] is None:
assert True
else:
assert abs(output - expected) <= tol
assert abs(output['vpd'] - expected) <= tol


@pytest.mark.parametrize(
Expand All @@ -99,8 +99,8 @@ def test_meteorology_vpd(Ta_C, Ea_kPa, expected, tol=0.000001):
def test_meteorology_rh(Ta_C, Ea_kPa, expected, tol=0.000001):
# CGM - This test uses images due to .updateMask() call in meteorology
SVP, VPD, RH = meteorology.meteorology(ee.Image.constant(Ta_C), ee.Image.constant(Ea_kPa))
output = utils.constant_image_value(RH)
assert abs(output - expected) <= tol
output = utils.constant_image_value(RH.rename(['rh']))
assert abs(output['rh'] - expected) <= tol


@pytest.mark.parametrize(
Expand Down
8 changes: 4 additions & 4 deletions openet/ptjpl/tests/test_b_vegetation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def test_savi_from_ndvi(ndvi=0.5, expected=0.5 * 0.45 + 0.132, tol=0.000001):
output = utils.constant_image_value(vegetation.savi_from_ndvi(ee.Image.constant(ndvi)))
assert abs(output - expected) <= tol
assert abs(output['constant'] - expected) <= tol


@pytest.mark.parametrize(
Expand All @@ -20,14 +20,14 @@ def test_savi_from_ndvi(ndvi=0.5, expected=0.5 * 0.45 + 0.132, tol=0.000001):
)
def test_fAPAR_from_savi(savi, expected, tol=0.000001):
output = utils.constant_image_value(vegetation.fAPAR_from_savi(ee.Image.constant(savi)))
assert abs(output - expected) <= tol
assert abs(output['constant'] - expected) <= tol


@pytest.mark.parametrize(
'ndvi, expected', [[0.5, (0.45 * 0.5 + 0.132) * 1.3632 - 0.048]])
def test_fAPAR_from_ndvi(ndvi, expected, tol=0.000001):
output = utils.constant_image_value(vegetation.fAPAR_from_ndvi(ee.Image.constant(ndvi)))
assert abs(output - expected) <= tol
assert abs(output['constant'] - expected) <= tol


@pytest.mark.parametrize(
Expand All @@ -40,4 +40,4 @@ def test_fAPAR_from_ndvi(ndvi, expected, tol=0.000001):
)
def test_fIPAR_from_ndvi(ndvi, expected, tol=0.000001):
output = utils.constant_image_value(vegetation.fIPAR_from_ndvi(ee.Image.constant(ndvi)))
assert abs(output - expected) <= tol
assert abs(output['constant'] - expected) <= tol
10 changes: 5 additions & 5 deletions openet/ptjpl/tests/test_c_ptjpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def test_Model_Rns_calculation(Rn, LAI, water_mask, expected, tol=0.001):
water_mask=ee.Image.constant(water_mask)
))
if expected is None:
assert output is None
assert output['constant'] is None
else:
assert abs(output - expected) <= tol
assert abs(output['constant'] - expected) <= tol


# @pytest.mark.parametrize(
Expand All @@ -53,7 +53,7 @@ def test_Model_Rns_calculation(Rn, LAI, water_mask, expected, tol=0.001):
# Rn=ee.Image.constant(Rn), fIPAR=ee.Image.constant(fIPAR),
# Rns=ee.Image.constant(Rns), W=ee.Image.constant(W),
# water_mask=ee.Image.constant(water_mask)))
# assert abs(output - expected) <= tol
# assert abs(output['constant'] - expected) <= tol


# @pytest.mark.parametrize(
Expand Down Expand Up @@ -83,5 +83,5 @@ def test_Model_Rns_calculation(Rn, LAI, water_mask, expected, tol=0.001):
# )
# output = utils.constant_image_value(output_image)
# logging.debug(f'\n Target values: {expected}')
# logging.debug(f' Output values: {output}')
# assert abs(output - expected) <= tol
# logging.debug(f' Output values: {output['constant']}')
# assert abs(output['constant'] - expected) <= tol
Loading

0 comments on commit a229fd3

Please sign in to comment.