Skip to content

Commit

Permalink
Misc. formatting cleanup and flake8 changes
Browse files Browse the repository at this point in the history
Letting line lengths go to ~100 where it seems to improves readability.  Also, trying to move away from using trailing slashes.
  • Loading branch information
cgmorton committed Oct 25, 2023
1 parent f3fd19c commit 7fefe43
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 286 deletions.
4 changes: 3 additions & 1 deletion openet/ssebop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@

MODEL_NAME = 'SSEBOP'

# __version__ = metadata.version(__package__ or __name__)
# # __version__ = metadata.version(__package__ or __name__)
# __version__ = metadata.version(__package__.replace('.', '-') or __name__.replace('.', '-'))
# # __version__ = metadata.version('openet-ssebop')
235 changes: 134 additions & 101 deletions openet/ssebop/collection.py

Large diffs are not rendered by default.

245 changes: 116 additions & 129 deletions openet/ssebop/image.py

Large diffs are not rendered by default.

69 changes: 38 additions & 31 deletions openet/ssebop/interpolate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import datetime
import logging
import pprint
# import pprint

from dateutil.relativedelta import *
from dateutil.relativedelta import relativedelta
import ee
import openet.core.interpolate
# TODO: import utils from openet.core
Expand Down Expand Up @@ -172,30 +172,36 @@ def from_scene_et_fraction(
if type(et_reference_source) is str:
# Assume a string source is a single image collection ID
# not a list of collection IDs or ee.ImageCollection
if (et_reference_date_type is None or
et_reference_date_type.lower() == 'daily'):
daily_et_ref_coll = ee.ImageCollection(et_reference_source) \
.filterDate(start_date, end_date) \
if (et_reference_date_type is None) or (et_reference_date_type.lower() == 'daily'):
daily_et_ref_coll = (
ee.ImageCollection(et_reference_source)
.filterDate(start_date, end_date)
.select([et_reference_band], ['et_reference'])
)
elif et_reference_date_type.lower() == 'doy':
# Assume the image collection is a climatology with a "DOY" property
def doy_image(input_img):
"""Return the doy-based reference et with daily time properties from GRIDMET"""
image_date = ee.Algorithms.Date(input_img.get('system:time_start'))
image_doy = ee.Number(image_date.getRelative('day', 'year')).add(1).int()
doy_coll = ee.ImageCollection(et_reference_source)\
.filterMetadata('DOY', 'equals', image_doy)\
doy_coll = (
ee.ImageCollection(et_reference_source)
.filterMetadata('DOY', 'equals', image_doy)
.select([et_reference_band], ['et_reference'])
)
# CGM - Was there a reason to use rangeContains if limiting to one DOY?
# .filter(ee.Filter.rangeContains('DOY', doy, doy))\
return ee.Image(doy_coll.first())\
.set({'system:index': input_img.get('system:index'),
'system:time_start': input_img.get('system:time_start')})
# Note, the collection and band that are used are important as
# long as they are daily and available for the time period
daily_et_ref_coll = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET')\
.filterDate(start_date, end_date).select(['eto'])\
daily_et_ref_coll = (
ee.ImageCollection('IDAHO_EPSCOR/GRIDMET')
.filterDate(start_date, end_date)
.select(['eto'])
.map(doy_image)
)
# elif isinstance(et_reference_source, computedobject.ComputedObject):
# # Interpret computed objects as image collections
# daily_et_reference_coll = ee.ImageCollection(et_reference_source)\
Expand All @@ -208,9 +214,12 @@ def doy_image(input_img):
# CGM - Resampling is not working correctly so not including for now
if et_reference_factor and et_reference_factor != 1:
def et_reference_adjust(input_img):
return input_img.multiply(et_reference_factor) \
.copyProperties(input_img) \
return (
input_img.multiply(et_reference_factor)
.copyProperties(input_img)
.set({'system:time_start': input_img.get('system:time_start')})
)

daily_et_ref_coll = daily_et_ref_coll.map(et_reference_adjust)

# Initialize variable list to only variables that can be interpolated
Expand All @@ -235,9 +244,7 @@ def et_reference_adjust(input_img):
# For count, compute the composite/mosaic image for the mask band only
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,
image_coll=scene_coll.select(['mask']), start_date=start_date, end_date=end_date,
)

# The following is needed because the aggregate collection can be
Expand All @@ -247,7 +254,7 @@ def et_reference_adjust(input_img):
# bands will be which causes a non-homogeneous image collection.
aggregate_coll = aggregate_coll.merge(
ee.Image.constant(0).rename(['mask'])
.set({'system:time_start': ee.Date(start_date).millis()})
.set({'system:time_start': ee.Date(start_date).millis()})
)

# Interpolate to a daily time step
Expand Down Expand Up @@ -294,13 +301,13 @@ def aggregate_image(agg_start_date, agg_end_date, date_format):
"""
if 'et' in variables or 'et_fraction' in variables:
et_img = daily_coll.filterDate(agg_start_date, agg_end_date) \
.select(['et']).sum()
et_img = daily_coll.filterDate(agg_start_date, agg_end_date).select(['et']).sum()
if 'et_reference' in variables or 'et_fraction' in variables:
# et_reference_img = daily_coll \
et_reference_img = daily_et_ref_coll \
.filterDate(agg_start_date, agg_end_date) \
et_reference_img = (
daily_et_ref_coll
.filterDate(agg_start_date, agg_end_date)
.select(['et_reference']).sum()
)

image_list = []
if 'et' in variables:
Expand All @@ -309,25 +316,26 @@ def aggregate_image(agg_start_date, agg_end_date, date_format):
image_list.append(et_reference_img.float())
if 'et_fraction' in variables:
# Compute average et fraction over the aggregation period
image_list.append(
et_img.divide(et_reference_img).rename(['et_fraction']).float()
)
image_list.append(et_img.divide(et_reference_img).rename(['et_fraction']).float())
if 'ndvi' in variables:
# Compute average ndvi over the aggregation period
ndvi_img = daily_coll \
.filterDate(agg_start_date, agg_end_date) \
ndvi_img = (
daily_coll.filterDate(agg_start_date, agg_end_date)
.mean().select(['ndvi']).float()
)
image_list.append(ndvi_img)
if 'count' in variables:
count_img = aggregate_coll \
.filterDate(agg_start_date, agg_end_date) \
count_img = (
aggregate_coll.filterDate(agg_start_date, agg_end_date)
.select(['mask']).sum().rename('count').uint8()
)
image_list.append(count_img)

return ee.Image(image_list) \
.set({
'system:index': ee.Date(agg_start_date).format(date_format),
'system:time_start': ee.Date(agg_start_date).millis()})
'system:time_start': ee.Date(agg_start_date).millis(),
})
# .set(interp_properties) \

# Combine input, interpolated, and derived values
Expand Down Expand Up @@ -386,6 +394,5 @@ def agg_annual(agg_start_date):
elif t_interval.lower() == 'custom':
# Returning an ImageCollection to be consistent
return ee.ImageCollection(aggregate_image(
agg_start_date=start_date, agg_end_date=end_date,
date_format='YYYYMMdd',
agg_start_date=start_date, agg_end_date=end_date, date_format='YYYYMMdd',
))
18 changes: 6 additions & 12 deletions openet/ssebop/landsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,19 @@ def emissivity(landsat_image):
"""
ndvi_img = ndvi(landsat_image)
Pv = ndvi_img.expression('((ndvi - 0.2) / 0.3) ** 2', {'ndvi': ndvi_img})
pv = ndvi_img.expression('((ndvi - 0.2) / 0.3) ** 2', {'ndvi': ndvi_img})
# ndviRangevalue = ndvi_image.where(
# ndvi_image.gte(0.2).And(ndvi_image.lte(0.5)), ndvi_image
# )
# Pv = ndviRangevalue.expression(
# pv = ndviRangevalue.expression(
# '(((ndviRangevalue - 0.2) / 0.3) ** 2',
# {'ndviRangevalue':ndviRangevalue}
# )

# Assuming typical Soil Emissivity of 0.97 and Veg Emissivity of 0.99
# and shape Factor mean value of 0.553
dE = Pv.expression(
'(1 - 0.97) * (1 - Pv) * (0.55 * 0.99)', {'Pv': Pv}
)
RangeEmiss = dE.expression(
'(0.99 * Pv) + (0.97 * (1 - Pv)) + dE', {'Pv': Pv, 'dE': dE}
)
de = pv.expression('(1 - 0.97) * (1 - Pv) * (0.55 * 0.99)', {'Pv': pv})
RangeEmiss = de.expression('(0.99 * Pv) + (0.97 * (1 - Pv)) + dE', {'Pv': pv, 'dE': de})

return (
ndvi_img
Expand Down Expand Up @@ -128,8 +124,7 @@ def ndvi(landsat_image):
ee.Image
"""
return ee.Image(landsat_image).normalizedDifference(['nir', 'red'])\
.rename(['ndvi'])
return ee.Image(landsat_image).normalizedDifference(['nir', 'red']).rename(['ndvi'])


def ndwi(landsat_image):
Expand All @@ -145,8 +140,7 @@ def ndwi(landsat_image):
ee.Image
"""
return ee.Image(landsat_image).normalizedDifference(['green', 'swir1'])\
.rename(['ndwi'])
return ee.Image(landsat_image).normalizedDifference(['green', 'swir1']).rename(['ndwi'])


def landsat_c2_qa_water_mask(landsat_image):
Expand Down
6 changes: 4 additions & 2 deletions openet/ssebop/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ def elr_adjust(temperature, elevation, radius=80):
# Then generate the smoothed elevation image
elev_tmax_smoothed = (
elev_tmax_fine
.reduceNeighborhood(reducer=ee.Reducer.median(),
kernel=ee.Kernel.square(radius=radius, units='pixels'))
.reduceNeighborhood(
reducer=ee.Reducer.median(),
kernel=ee.Kernel.square(radius=radius, units='pixels')
)
.reproject(crs=tmax_projection)
)

Expand Down
20 changes: 10 additions & 10 deletions openet/ssebop/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ def getinfo(ee_obj, n=4):
# 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"""
return getinfo(ee.Image(image).reduceRegion(
reducer=ee.Reducer.first(), scale=scale,
geometry=ee.Geometry.Rectangle([0, 0, 10, 10], crs, False)))
rr_params = {
'reducer': ee.Reducer.first(),
'geometry': ee.Geometry.Rectangle([0, 0, 10, 10], crs, False),
'scale': scale,
}
return getinfo(ee.Image(image).reduceRegion(**rr_params))


def point_image_value(image, xy, scale=1):
"""Extract the output value from a calculation at a point"""
return getinfo(ee.Image(image).reduceRegion(
reducer=ee.Reducer.first(), geometry=ee.Geometry.Point(xy),
scale=scale))
rr_params = {'reducer': ee.Reducer.first(), 'geometry': ee.Geometry.Point(xy), 'scale': scale}
return getinfo(ee.Image(image).reduceRegion(**rr_params))


def point_coll_value(coll, xy, scale=1):
Expand All @@ -62,8 +64,7 @@ def point_coll_value(coll, xy, scale=1):
col_dict[k] = i + 4
info_dict[k] = {}
for row in output[1:]:
date = datetime.datetime.utcfromtimestamp(row[3] / 1000.0).strftime(
'%Y-%m-%d')
date = datetime.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]]
return info_dict
Expand Down Expand Up @@ -97,8 +98,7 @@ def date_to_time_0utc(date):
ee.Number
"""
return ee.Date.fromYMD(date.get('year'), date.get('month'),
date.get('day')).millis()
return ee.Date.fromYMD(date.get('year'), date.get('month'), date.get('day')).millis()
# Extra operations are needed since update() does not set milliseconds to 0.
# return date.update(hour=0, minute=0, second=0).millis()\
# .divide(1000).floor().multiply(1000)
Expand Down

0 comments on commit 7fefe43

Please sign in to comment.