Skip to content

Commit

Permalink
return filenames, reset after each control iteraction, closes #175
Browse files Browse the repository at this point in the history
  • Loading branch information
jsta committed Apr 8, 2024
1 parent cf69a06 commit d3a0d1a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
8 changes: 6 additions & 2 deletions rabpro/basin_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def compute(
occ_mask = ee.Image("JRC/GSW1_4/GlobalSurfaceWater").select("occurrence").lt(90)

# For each raster
datas, tasks = [], []
datas, tasks, filenames = [], [], []
for dt in control:
if dt.band in ["None", None]:
if dt.type == "image":
Expand Down Expand Up @@ -471,6 +471,8 @@ def remove_geometry(feat):
filename = dataset_to_filename(dt.prepend, dt.data_id, dt.band)
print(filename)

filenames.append(filename)

task = ee.batch.Export.table.toDrive(
collection=table,
description=filename,
Expand All @@ -491,8 +493,10 @@ def remove_geometry(feat):
)
)
tasks.append(task)
# reset filename so it doesn't carry over to subsequent Dataset(s)
filename = None

return datas, tasks
return datas, tasks, filenames


def _parse_reducers(stats=None, base=None):
Expand Down
33 changes: 26 additions & 7 deletions tests/test_basin_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _clean_res(feature):


def test_unvalidated_dataset():
urls, _ = rabpro.basin_stats.compute(
urls, _, _ = rabpro.basin_stats.compute(
[
Dataset(
"NASA/ORNL/DAYMET_V4",
Expand All @@ -49,7 +49,7 @@ def test_customreducer():
def asdf(feat):
return feat.getNumber("max")

urls, _ = rabpro.basin_stats.compute(
urls, _, _ = rabpro.basin_stats.compute(
[Dataset("JRC/GSW1_4/YearlyHistory", "waterClass", stats=["max"])],
gee_feature_path="users/jstacompute/basins",
reducer_funcs=[asdf],
Expand All @@ -62,7 +62,7 @@ def asdf(feat):

def test_categorical_imgcol():

urls, _ = rabpro.basin_stats.compute(
urls, _, _ = rabpro.basin_stats.compute(
[Dataset("MODIS/061/MCD12Q1", "LC_Type1", stats=["freqhist"])],
gee_feature_path="users/jstacompute/basins",
)
Expand All @@ -73,7 +73,7 @@ def test_categorical_imgcol():

def test_timeindexed_imgcol():

urls, _ = rabpro.basin_stats.compute(
urls, _, _ = rabpro.basin_stats.compute(
[
Dataset(
"JRC/GSW1_4/YearlyHistory",
Expand All @@ -91,7 +91,7 @@ def test_timeindexed_imgcol():

def test_timeindexedspecific_imgcol():

urls, _ = rabpro.basin_stats.compute(
urls, _, _ = rabpro.basin_stats.compute(
[
Dataset(
"JRC/GSW1_4/YearlyHistory",
Expand All @@ -110,7 +110,7 @@ def test_timeindexedspecific_imgcol():

def test_nontimeindexed_imgcol():

data, _ = rabpro.basin_stats.compute(
data, _, _ = rabpro.basin_stats.compute(
[
Dataset(
"JRC/GSW1_4/MonthlyRecurrence",
Expand All @@ -129,7 +129,7 @@ def test_nontimeindexed_imgcol():
def test_img():

stat_list = ["min", "max", "range", "std", "sum", "pct50", "pct3"]
data, _ = rabpro.basin_stats.compute(
data, _, _ = rabpro.basin_stats.compute(
[
Dataset(
"JRC/GSW1_4/GlobalSurfaceWater",
Expand All @@ -146,3 +146,22 @@ def test_img():
assert float(res["mean"]) > 0
# mean, count, id, and id_outlet are automatically returned
assert res.shape[1] == len(stat_list) + 4


def test_filenames():
stat_list = ["mean", "std"]
_, _, filenames = rabpro.basin_stats.compute(
[
Dataset("MERIT/DEM/v1_0_3", "dem", stats=stat_list, gee_type="image"),
Dataset(
"JRC/GSW1_4/GlobalSurfaceWater",
"occurrence",
stats=stat_list,
gee_type="image",
),
],
gee_feature_path="users/jstacompute/basins",
)

# make sure we're getting unique filenames
assert filenames.count(filenames[0]) == 1

0 comments on commit d3a0d1a

Please sign in to comment.