Skip to content

Commit

Permalink
Update collection.py
Browse files Browse the repository at this point in the history
It seems like .count() doesn't work anymore, but calling .reduce(ee.Reducer.count())
  • Loading branch information
cgmorton authored Aug 5, 2024
1 parent a0368cb commit 8444e5f
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions openet/sims/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ def __init__(
raise ValueError('end_date must be after start_date')

# Check cloud_cover_max
if (not type(self.cloud_cover_max) is int and
not type(self.cloud_cover_max) is float and
if ((type(self.cloud_cover_max) is not int) and
(type(self.cloud_cover_max) is not float) and
not utils.is_number(self.cloud_cover_max)):
raise TypeError('cloud_cover_max must be a number')
if (type(self.cloud_cover_max) is str and
if ((type(self.cloud_cover_max) is str) and
utils.is_number(self.cloud_cover_max)):
self.cloud_cover_max = float(self.cloud_cover_max)
if self.cloud_cover_max < 0 or self.cloud_cover_max > 100:
if (self.cloud_cover_max < 0) or (self.cloud_cover_max > 100):
raise ValueError('cloud_cover_max must be in the range 0 to 100')

# Check geometry?
Expand Down Expand Up @@ -263,9 +263,9 @@ def _build(self, variables=None, start_date=None, end_date=None):

# TODO: Move this to a separate function (maybe in utils.py?)
# since it is identical for all the supported collections
if (self.filter_args is None or
not isinstance(self.filter_args, dict) or
coll_id not in self.filter_args.keys()):
if ((self.filter_args is None) or
(not isinstance(self.filter_args, dict)) or
(coll_id not in self.filter_args.keys())):
pass
elif isinstance(self.filter_args[coll_id], ee.ComputedObject):
input_coll = input_coll.filter(self.filter_args[coll_id])
Expand Down Expand Up @@ -355,7 +355,7 @@ def interpolate(
interp_method='linear',
interp_days=32,
use_joins=True,
**kwargs
**kwargs,
):
"""
Expand Down Expand Up @@ -403,7 +403,7 @@ def interpolate(
elif interp_method.lower() not in ['linear']:
raise ValueError(f'unsupported interp_method: {interp_method}')

if type(interp_days) is str and utils.is_number(interp_days):
if (type(interp_days) is str) and utils.is_number(interp_days):
interp_days = int(interp_days)
elif not type(interp_days) is int:
raise TypeError('interp_days must be an integer')
Expand Down Expand Up @@ -446,18 +446,18 @@ def interpolate(
# Update model_args if et_reference parameters were passed to interpolate
# Intentionally using model_args (instead of self.et_reference_source, etc.) in
# this function since model_args is passed to Image class in _build()
# if 'et' in variables or 'et_reference' in variables:
if ('et_reference_source' in kwargs.keys() and
kwargs['et_reference_source'] is not None):
# if ('et' in variables) or ('et_reference' in variables):
if (('et_reference_source' in kwargs.keys()) and
(kwargs['et_reference_source'] is not None)):
self.model_args['et_reference_source'] = kwargs['et_reference_source']
if ('et_reference_band' in kwargs.keys() and
kwargs['et_reference_band'] is not None):
if (('et_reference_band' in kwargs.keys()) and
(kwargs['et_reference_band'] is not None)):
self.model_args['et_reference_band'] = kwargs['et_reference_band']
if ('et_reference_factor' in kwargs.keys() and
kwargs['et_reference_factor'] is not None):
if (('et_reference_factor' in kwargs.keys()) and
(kwargs['et_reference_factor'] is not None)):
self.model_args['et_reference_factor'] = kwargs['et_reference_factor']
if ('et_reference_resample' in kwargs.keys() and
kwargs['et_reference_resample'] is not None):
if (('et_reference_resample' in kwargs.keys()) and
(kwargs['et_reference_resample'] is not None)):
self.model_args['et_reference_resample'] = kwargs['et_reference_resample'].lower()

# Check that all et_reference parameters were set
Expand Down Expand Up @@ -504,12 +504,12 @@ def et_reference_adjust(input_img):
interp_vars = list(set(self._interp_vars) & set(variables))

# To return ET, the ET fraction must be interpolated
if 'et' in variables and 'et_fraction' not in interp_vars:
if ('et' in variables) and ('et_fraction' not in interp_vars):
interp_vars.append('et_fraction')

# With the current interpolate.daily() function,
# something has to be interpolated in order to return et_reference
if 'et_reference' in variables and 'et_fraction' not in interp_vars:
if ('et_reference' in variables) and ('et_fraction' not in interp_vars):
interp_vars.append('et_fraction')

# The time band is always needed for interpolation
Expand Down Expand Up @@ -617,7 +617,7 @@ def aggregate_image(agg_start_date, agg_end_date, date_format):
"""
# et_img = None
# et_reference_img = None
if ('et' in variables) or ('et_fraction') in variables:
if ('et' in variables) or ('et_fraction' in variables):
et_img = (
daily_coll.filterDate(agg_start_date, agg_end_date)
.select(['et']).sum()
Expand Down Expand Up @@ -649,14 +649,21 @@ def aggregate_image(agg_start_date, agg_end_date, date_format):
if 'ndvi' in variables:
# Compute average ndvi over the aggregation period
ndvi_img = (
daily_coll.filterDate(agg_start_date, agg_end_date)
.mean().select(['ndvi']).float()
daily_coll
.filterDate(agg_start_date, agg_end_date)
.select(['ndvi'])
.mean()
.float()
)
image_list.append(ndvi_img)
if 'count' in variables:
count_img = (
aggregate_coll.filterDate(agg_start_date, agg_end_date)
.select(['mask']).count().rename('count').uint8()
aggregate_coll
.filterDate(agg_start_date, agg_end_date)
.select(['mask'])
.reduce(ee.Reducer.count())
.rename('count')
.uint8()
)
image_list.append(count_img)

Expand Down

0 comments on commit 8444e5f

Please sign in to comment.