Skip to content

Commit

Permalink
Flake 8 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmorton committed Aug 24, 2023
1 parent 1d53045 commit c6c5a93
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 66 deletions.
4 changes: 2 additions & 2 deletions openet/core/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys
# import sys

import ee

Expand All @@ -22,7 +22,7 @@ def collection(
t_interval,
geometry,
**kwargs
):
):
"""Generic OpenET Collection
Parameters
Expand Down
4 changes: 0 additions & 4 deletions openet/core/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import re
import warnings

import ee


Expand Down Expand Up @@ -547,4 +544,3 @@ def ged_emis(ged_col):

return L8_LST.rename('surface_temperature')
# .set('system:time_start', image.get('system:time_start'))

6 changes: 2 additions & 4 deletions openet/core/ensemble.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import pprint

import ee

from . import utils
# import openet.core.utils as utils
# from . import utils
# # import openet.core.utils as utils

model_index = {
'disalexi': 1,
Expand Down
87 changes: 59 additions & 28 deletions openet/core/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
import logging

import ee
from dateutil.relativedelta import *
from dateutil.relativedelta import relativedelta

from . import utils
# import openet.core.utils as utils


def daily(target_coll, source_coll, interp_days=32, interp_method='linear',
use_joins=False, compute_product=False):
def daily(
target_coll,
source_coll,
interp_days=32,
interp_method='linear',
use_joins=False,
compute_product=False
):
"""Interpolate non-daily source images to a daily target image collection
Parameters
Expand Down Expand Up @@ -252,8 +258,12 @@ def aggregate_daily(image_coll, start_date=None, end_date=None,
return aggregate_to_daily(image_coll, start_date, end_date, agg_type)


def aggregate_to_daily(image_coll, start_date=None, end_date=None,
agg_type='mean'):
def aggregate_to_daily(
image_coll,
start_date=None,
end_date=None,
agg_type='mean'
):
"""Aggregate images by day without using joins
The primary purpose of this function is to join separate Landsat images
Expand Down Expand Up @@ -288,10 +298,12 @@ def aggregate_to_daily(image_coll, start_date=None, end_date=None,
test_coll = image_coll.filterDate(ee.Date(start_date), ee.Date(end_date))
elif start_date:
test_coll = image_coll.filter(ee.Filter.greaterThanOrEquals(
'system:time_start', ee.Date(start_date).millis()))
'system:time_start', ee.Date(start_date).millis()
))
elif end_date:
test_coll = image_coll.filter(ee.Filter.lessThan(
'system:time_start', ee.Date(end_date).millis()))
'system:time_start', ee.Date(end_date).millis()
))
else:
test_coll = image_coll

Expand Down Expand Up @@ -321,10 +333,15 @@ def aggregate_func(date_str):
return ee.ImageCollection(date_list.map(aggregate_func))


def from_scene_et_fraction(scene_coll, start_date, end_date, variables,
interp_args, model_args, t_interval,
use_joins=False,
):
def from_scene_et_fraction(
scene_coll,
start_date, end_date,
variables,
interp_args,
model_args,
t_interval,
use_joins=False,
):
"""Interpolate from a precomputed collection of Landsat ET fraction scenes
Parameters
Expand Down Expand Up @@ -521,8 +538,10 @@ def et_reference_adjust(input_img):
# For count, compute the composite/mosaic image for the mask band only
if 'count' in variables:
aggregate_coll = 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
# empty if there are no scenes in the target date range but there
Expand All @@ -531,13 +550,15 @@ 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
daily_coll = daily(
target_coll=daily_et_ref_coll,
source_coll=scene_coll.select(interp_vars),
interp_method=interp_method, interp_days=interp_days,
interp_method=interp_method,
interp_days=interp_days,
use_joins=use_joins,
compute_product=False,
)
Expand All @@ -554,8 +575,7 @@ def et_reference_adjust(input_img):
# if 'et' in variables or 'et_fraction' in variables:
def compute_et(img):
"""This function assumes ETr and ETf are present"""
et_img = img.select(['et_fraction']) \
.multiply(img.select(['et_reference']))
et_img = img.select(['et_fraction']).multiply(img.select(['et_reference']))
return img.addBands(et_img.double().rename('et'))
daily_coll = daily_coll.map(compute_et)

Expand Down Expand Up @@ -598,8 +618,8 @@ def aggregate_image(agg_start_date, agg_end_date, date_format):
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())
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 \
Expand Down Expand Up @@ -629,7 +649,8 @@ def agg_daily(daily_img):
return aggregate_image(
agg_start_date=agg_start_date,
agg_end_date=ee.Date(agg_start_date).advance(1, 'day'),
date_format='YYYYMMdd')
date_format='YYYYMMdd',
)

return ee.ImageCollection(daily_coll.map(agg_daily))

Expand All @@ -647,7 +668,8 @@ def agg_monthly(agg_start_date):
return aggregate_image(
agg_start_date=agg_start_date,
agg_end_date=ee.Date(agg_start_date).advance(1, 'month'),
date_format='YYYYMM')
date_format='YYYYMM',
)

return ee.ImageCollection(month_list.map(agg_monthly))

Expand All @@ -664,21 +686,30 @@ def agg_annual(agg_start_date):
return aggregate_image(
agg_start_date=agg_start_date,
agg_end_date=ee.Date(agg_start_date).advance(1, 'year'),
date_format='YYYY')
date_format='YYYY',
)

return ee.ImageCollection(year_list.map(agg_annual))

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',
))


def from_scene_et_actual(scene_coll, start_date, end_date, variables,
interp_args, model_args, t_interval,
use_joins=False,
):
def from_scene_et_actual(
scene_coll,
start_date,
end_date,
variables,
interp_args,
model_args,
t_interval,
use_joins=False,
):
"""Interpolate from a precomputed collection of Landsat actual ET scenes
Parameters
Expand Down
4 changes: 2 additions & 2 deletions openet/core/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pprint
# import pprint

import ee
import pytest
Expand Down Expand Up @@ -293,4 +293,4 @@ def test_landsat_c2_sr_lst_correct_values(image_id, xy, expected, tol=0.25):
# original = utils.point_image_value(lst_img, xy, scale=30)['ST_B10']
output_img = common.landsat_c2_sr_lst_correct(input_img, ndvi_img)
corrected = utils.point_image_value(output_img, xy, scale=30)
assert abs(corrected['surface_temperature'] - expected) <= tol
assert abs(corrected['surface_temperature'] - expected) <= tol
6 changes: 2 additions & 4 deletions openet/core/tests/test_ensemble.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# import datetime
import logging
import pprint
# import logging
# import pprint

import ee
import pytest
Expand All @@ -11,8 +11,6 @@
# logging.basicConfig(level=logging.DEBUG, format='%(message)s')




# TODO: Write a test to check that the output bandnames
# def test_mad_bandname():
# assert False
Expand Down
33 changes: 14 additions & 19 deletions openet/core/tests/test_interpolate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import logging
import pprint
# import pprint

import ee
import pytest
Expand All @@ -23,6 +23,7 @@ def tgt_image(tgt_value, tgt_time):
'system:index': datetime.datetime.utcfromtimestamp(
tgt_time / 1000.0).strftime('%Y%m%d')})


def src_images(src_values, src_times):
"""Build constant source images from values and times"""
src_images = []
Expand Down Expand Up @@ -82,22 +83,16 @@ def scene_coll(variables, et_fraction=[0.4, 0.4, 0.4], et=[5, 5, 5],

# Mask and time bands currently get added on to the scene collection
# and images are unscaled just before interpolating in the export tool
scene_coll = ee.ImageCollection([
ee.Image([img.add(et_fraction[0]), img.add(et[0]), img.add(ndvi[0]),
img.add(date1), mask])\
scene_coll = ee.ImageCollection.fromImages([
ee.Image([img.add(et_fraction[0]), img.add(et[0]), img.add(ndvi[0]), img.add(date1), mask])
.rename(['et_fraction', 'et', 'ndvi', 'time', 'mask'])
.set({'system:index': 'LE07_044033_20170708',
'system:time_start': time1}),
ee.Image([img.add(et_fraction[1]), img.add(et[1]), img.add(ndvi[1]),
img.add(date2), mask])\
.set({'system:index': 'LE07_044033_20170708', 'system:time_start': time1}),
ee.Image([img.add(et_fraction[1]), img.add(et[1]), img.add(ndvi[1]), img.add(date2), mask])
.rename(['et_fraction', 'et', 'ndvi', 'time', 'mask'])
.set({'system:index': 'LC08_044033_20170716',
'system:time_start': time2}),
ee.Image([img.add(et_fraction[2]), img.add(et[2]), img.add(ndvi[2]),
img.add(date3), mask])\
.set({'system:index': 'LC08_044033_20170716', 'system:time_start': time2}),
ee.Image([img.add(et_fraction[2]), img.add(et[2]), img.add(ndvi[2]), img.add(date3), mask])
.rename(['et_fraction', 'et', 'ndvi', 'time', 'mask'])
.set({'system:index': 'LE07_044033_20170724',
'system:time_start': time3}),
.set({'system:index': 'LE07_044033_20170724', 'system:time_start': time3}),
])
return scene_coll.select(variables)

Expand Down Expand Up @@ -728,7 +723,7 @@ def test_from_scene_et_actual_daily_et_fraction_max(tol=0.0001):
def test_from_scene_et_fraction_t_interval_bad_value():
# Function should raise a ValueError if t_interval is not supported
with pytest.raises(ValueError):
output_coll = interpolate.from_scene_et_fraction(
interpolate.from_scene_et_fraction(
scene_coll(['et', 'time', 'mask']),
start_date='2017-07-01', end_date='2017-08-01', variables=['et'],
interp_args={'interp_method': 'linear', 'interp_days': 32},
Expand All @@ -742,7 +737,7 @@ def test_from_scene_et_fraction_t_interval_bad_value():
def test_from_scene_et_fraction_t_interval_no_value():
# Function should raise an Exception if t_interval is not set
with pytest.raises(TypeError):
output_coll = interpolate.from_scene_et_fraction(
interpolate.from_scene_et_fraction(
scene_coll(['et', 'time', 'mask']),
start_date='2017-07-01', end_date='2017-08-01',
variables=['et', 'et_reference', 'et_fraction', 'count'],
Expand All @@ -756,7 +751,7 @@ def test_from_scene_et_fraction_t_interval_no_value():
def test_from_scene_et_actual_t_interval_bad_value():
# Function should raise a ValueError if t_interval is not supported
with pytest.raises(ValueError):
output_coll = interpolate.from_scene_et_actual(
interpolate.from_scene_et_actual(
scene_coll(['et', 'time', 'mask']),
start_date='2017-07-01', end_date='2017-08-01', variables=['et'],
interp_args={'interp_method': 'linear', 'interp_days': 32,
Expand All @@ -772,7 +767,7 @@ def test_from_scene_et_actual_t_interval_bad_value():
def test_from_scene_et_actual_t_interval_no_value():
# Function should raise an Exception if t_interval is not set
with pytest.raises(TypeError):
output_coll = interpolate.from_scene_et_actual(
interpolate.from_scene_et_actual(
scene_coll(['et', 'time', 'mask']),
start_date='2017-07-01', end_date='2017-08-01', variables=['et'],
interp_args={'interp_method': 'linear', 'interp_days': 32,
Expand All @@ -787,7 +782,7 @@ def test_from_scene_et_actual_t_interval_no_value():

"""
These tests were attempts at making "full" interpolation calls.
They could be removed but are being left in case we want to explore this again
They could be removed but are being left in case we want to explore this again
at some point in the future.
"""
# def test_daily_values_collection_a():
Expand Down
Loading

0 comments on commit c6c5a93

Please sign in to comment.