-
Notifications
You must be signed in to change notification settings - Fork 0
Add MSAVI index to backend #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8e9ef5e
feat: check that time range is valid for S2 harmonized collection
tomADC443 78d8aee
feat: add preprocessing step to remove unnecessary bands from images
tomADC443 3e35820
refactor: Aggregation and Filler DataFrame operations into two separa…
tomADC443 d06b41f
refactor: temporal utility functions (cleaner, more readable)
tomADC443 128b9c4
fix: different timezones (now always utc)
tomADC443 326e70a
feat: introduce static cache file
tomADC443 43f992c
refactor: delete emply file
wherop a2bc44f
refactor: delete emply file
wherop df6cc26
feat: refactor 'ndvi_router' to 'sat_index_router'; add MSAVI route t…
wherop 084a689
Merge branch 'main' into feature/msavi-endpoint
wherop 109c80e
refactor: rename image preprocessing file
wherop 331fdea
refactor: rename gee/ndvi.py to gee/sat_index_info.py
wherop 437f864
feat: add function to change index formula
wherop 60295bb
feat: pass index_type from router to GEE
wherop 05a751b
Merge branch 'main' into feature/msavi-endpoint
wherop b8828d4
refactor: add '__init__.py' to all python package directories
wherop b517f73
fix: update fastapi dependency reference to 'fastapi[standard]'
wherop d9c1ec3
fix: correct constants import error
wherop 128650b
style: update route labels to be more descriptive
wherop d98b02e
feat: add index type to response meta; correct unit for MSAVI meta
wherop 86e72fe
feat&docs: add comments and print statements to index service
wherop a5edee1
refactor: move NDVI cache to cache directory
wherop b9a52b1
feat: create MSAVI cache
wherop dcf318d
feat: add a rudamentary caching script (WIP)
wherop 69767bc
feat: integrate MSAVI cache
wherop 21b6288
fix(index service): handle requests where start or end date equals ca…
wherop 82329e5
Merge branch 'main' into feature/msavi-endpoint
wherop f9bc700
Merge branch 'main' into feature/msavi-endpoint
wherop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,5 @@ earthengine-api | |
| python-dotenv | ||
| pandas | ||
| plotly | ||
| fastapi | ||
| fastapi[standard] | ||
| uvicorn | ||
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import json | ||
|
|
||
| content = [ | ||
| {"timestamp": 1493251200, "value": 0.6265267304234295}, | ||
| {"timestamp": 1494720000, "value": 0.68603163673333}, | ||
| {"timestamp": 1494979200, "value": 0.755257128311451}, | ||
| ] | ||
| var_name = "msavi_daily_cache" | ||
| file_name = "../cache/temp_cache.py" | ||
| INDENT = " " | ||
|
|
||
| def combine_chache_with_update(): | ||
| # update existing cache | ||
| return | ||
|
|
||
| def write_year(file, year, results): | ||
| file.write(f"{INDENT}# Year {year}\n") | ||
|
|
||
| def write_results_to_cache(results: list, var_name: str, file_name: str): | ||
| with open(file_name, "w") as file: | ||
| file.write(f"{var_name} = [\n") | ||
| for day in results: | ||
| file.write(f"{INDENT}{json.dumps(day)},\n") | ||
| file.write("]\n") | ||
| file.close() | ||
|
|
||
| return | ||
|
|
||
|
|
||
| # write_results_to_cache(content, var_name, file_name) |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| from datetime import datetime, timezone | ||
|
|
||
| from fastapi import APIRouter, Query | ||
| from fastapi.responses import JSONResponse | ||
| from src.constants import ( | ||
| AggregationMethod, | ||
| LocationName, | ||
| TemporalResolution, | ||
| Unit, | ||
| IndexType, | ||
| ) | ||
| from src.service import sat_index_service | ||
| from src.utils.temporal import get_optimistic_rounding | ||
| from src.validation.models import NDVIResponse, MSAVIResponse | ||
| from src.validation.utils import ( | ||
| validate_timestamp_in_range_of_S2_imagery, | ||
| validate_timestamp_start_date_before_end_date, | ||
| ) | ||
|
|
||
| sat_index_router = APIRouter() | ||
|
|
||
|
|
||
| @sat_index_router.get("/ndvi", response_model=NDVIResponse) | ||
| async def get_ndvi_data( | ||
| startDate: int = Query( | ||
| ..., | ||
| description="First date of requested date range in UNIX timestamp as seconds", | ||
| ), | ||
| endDate: int = Query( | ||
| ..., | ||
| description="Last date of requested date range in UNIX timestamp as seconds", | ||
| ), | ||
| location: LocationName = Query(..., description="Name of the requested location"), | ||
| temporalResolution: TemporalResolution = Query( | ||
| ..., | ||
| description="Time interval that a single data point should represent e.g. one month", | ||
| ), | ||
| aggregation: AggregationMethod = Query( | ||
| ..., | ||
| description="Method of aggregating available data into a single datapoint to represent the selected time interval e.g. mean average", | ||
| ), | ||
| ): | ||
| validate_timestamp_start_date_before_end_date(startDate, endDate) | ||
| validate_timestamp_in_range_of_S2_imagery(startDate, endDate) | ||
| start_date_dt = datetime.fromtimestamp(startDate, tz=timezone.utc) | ||
| end_date_dt = datetime.fromtimestamp(endDate, tz=timezone.utc) | ||
|
|
||
| rounded_start_date, rounded_end_date = get_optimistic_rounding( | ||
| start_date_dt, end_date_dt, temporalResolution | ||
| ) | ||
|
|
||
| data = sat_index_service( | ||
| location=location, | ||
| temporal_resolution=temporalResolution, | ||
| aggregation_method=aggregation, | ||
| start_date=rounded_start_date, | ||
| end_date=rounded_end_date, | ||
| index_type=IndexType.NDVI, | ||
| ) | ||
|
|
||
| response = { | ||
| "meta": { | ||
| "location": LocationName[location].value, | ||
| "startDate": int(rounded_start_date.timestamp()), | ||
| "endDate": int(rounded_end_date.timestamp()), | ||
| "temporalResolution": TemporalResolution[temporalResolution].value, | ||
| "aggregation": AggregationMethod[aggregation].value, | ||
| "unit": Unit.NORMALIZED_DIFFERENCE.value, | ||
| }, | ||
| "data": data, | ||
| } | ||
|
|
||
| return JSONResponse(content=response) | ||
|
|
||
|
|
||
| @sat_index_router.get("/msavi", response_model=MSAVIResponse) | ||
| async def get_msavi_data( | ||
| startDate: int = Query( | ||
| ..., | ||
| description="First date of requested date range in UNIX timestamp as seconds", | ||
| ), | ||
| endDate: int = Query( | ||
| ..., | ||
| description="Last date of requested date range in UNIX timestamp as seconds", | ||
| ), | ||
| location: LocationName = Query(..., description="Name of the requested location"), | ||
| temporalResolution: TemporalResolution = Query( | ||
| ..., | ||
| description="Time interval that a single data point should represent e.g. one month", | ||
| ), | ||
| aggregation: AggregationMethod = Query( | ||
| ..., | ||
| description="Method of aggregating available data into a single datapoint to represent the selected time interval e.g. mean average", | ||
| ), | ||
| ): | ||
| validate_timestamp_start_date_before_end_date(startDate, endDate) | ||
| validate_timestamp_in_range_of_S2_imagery(startDate, endDate) | ||
| start_date_dt = datetime.fromtimestamp(startDate, tz=timezone.utc) | ||
| end_date_dt = datetime.fromtimestamp(endDate, tz=timezone.utc) | ||
|
|
||
| rounded_start_date, rounded_end_date = get_optimistic_rounding( | ||
| start_date_dt, end_date_dt, temporalResolution | ||
| ) | ||
|
|
||
| data = sat_index_service( | ||
| location=location, | ||
| temporal_resolution=temporalResolution, | ||
| aggregation_method=aggregation, | ||
| start_date=rounded_start_date, | ||
| end_date=rounded_end_date, | ||
| index_type=IndexType.MSAVI, | ||
| ) | ||
|
|
||
| response = { | ||
| "meta": { | ||
| "location": LocationName[location].value, | ||
| "startDate": int(rounded_start_date.timestamp()), | ||
| "endDate": int(rounded_end_date.timestamp()), | ||
| "temporalResolution": TemporalResolution[temporalResolution].value, | ||
| "aggregation": AggregationMethod[aggregation].value, | ||
| "unit": Unit.DIMENSIONLESS.value, | ||
| }, | ||
| "data": data, | ||
| } | ||
|
|
||
| return JSONResponse(content=response) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the long run we might wanna put the schema for input validation into a seperate file which would probably lead to less code and more reusability :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I encountered many things like this that I want to change. I decided not to include them in the scope of this branch.