Skip to content

Commit

Permalink
add explicit nir and red bands to std_ndvi_vals
Browse files Browse the repository at this point in the history
  • Loading branch information
atmorling committed May 15, 2024
1 parent bfee612 commit d144ee6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
6 changes: 3 additions & 3 deletions ecoscope/analysis/seasons.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def _min_max_scaler(x):
return x_std


def std_ndvi_vals(aoi=None, img_coll=None, band=None, img_scale=1, start=None, end=None):
def std_ndvi_vals(aoi=None, img_coll=None, nir_band=None, red_band=None, img_scale=1, start=None, end=None):

coll = (
ee.ImageCollection(img_coll)
.select(band)
.select([nir_band, red_band])
.filterDate(start, end)
.map(lambda x: x.multiply(ee.Image(img_scale)).set("system:time_start", x.get("system:time_start")))
)
Expand All @@ -35,7 +35,7 @@ def std_ndvi_vals(aoi=None, img_coll=None, band=None, img_scale=1, start=None, e

img_dates = pandas.to_datetime(coll.aggregate_array("system:time_start").getInfo(), unit="ms", utc=True)

coll = coll.map(lambda x: x.normalizedDifference(band))
coll = coll.map(lambda x: x.normalizedDifference([nir_band, red_band]))
ndvi_vals = coll.toBands().reduceRegion("mean", geo, bestEffort=True).values().getInfo()

df = pandas.DataFrame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@
"metadata": {},
"outputs": [],
"source": [
"img_coll = \"MODIS/061/MOD13A1\"\n",
"img_coll = \"MODIS/061/MCD43A4\"\n",
"band = \"NDVI\"\n",
"img_scale = 0.0001\n",
"since_filter = \"2010-01-01\"\n",
"img_scale = 1\n",
"since_filter = \"2020-01-01\"\n",
"until_filter = \"2022-06-18\"\n",
"\n",
"ndvi_vals = seasons.std_ndvi_vals(\n",
" img_coll=img_coll,\n",
" band=band,\n",
" nir_band=\"Nadir_Reflectance_Band2\",\n",
" red_band=\"Nadir_Reflectance_Band1\",\n",
" img_scale=img_scale,\n",
" aoi=aoi,\n",
" start=since_filter,\n",
Expand Down
11 changes: 9 additions & 2 deletions tests/test_seasons.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
)


@pytest.mark.skip(reason="this has been failing since May 2022; will be fixed in a follow-up pull")
@pytest.mark.skipif(not pytest.earthengine, reason="No connection to EarthEngine.")
def test_seasons():
gdf = gpd.read_file("tests/sample_data/vector/AOI_sites.gpkg").to_crs(4326)

aoi = gdf.geometry.iat[0]

# Extract the standardized NDVI ndvi_vals within the AOI
ndvi_vals = ecoscope.analysis.seasons.std_ndvi_vals(aoi, start="2010-01-01", end="2021-01-01")
ndvi_vals = ecoscope.analysis.seasons.std_ndvi_vals(
aoi,
img_coll="MODIS/061/MCD43A4",
nir_band="Nadir_Reflectance_Band2",
red_band="Nadir_Reflectance_Band1",
start="2010-01-01",
end="2021-01-01",
)

# Calculate the seasonal transition point
cuts = ecoscope.analysis.seasons.val_cuts(ndvi_vals, 2)
Expand Down

0 comments on commit d144ee6

Please sign in to comment.