Skip to content

Commit fb4f1ca

Browse files
authored
Merge branch 'main' into docs/refactor
2 parents b23bf1a + 10618a1 commit fb4f1ca

18 files changed

+3063725
-680
lines changed

backend/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"files.trimFinalNewlines": true,
55
"python.analysis.autoImportCompletions": true,
66
"python.analysis.typeCheckingMode": "basic",
7+
"python.analysis.diagnosticSeverityOverrides": {
8+
"reportPrivateImportUsage": "none"
9+
},
710
"[python]": {
811
"editor.formatOnSave": true,
912
"editor.codeActionsOnSave": {

backend/src/cache/msavi_cache.py

Lines changed: 524 additions & 542 deletions
Large diffs are not rendered by default.

backend/src/controller/__init__.py

Whitespace-only changes.

backend/src/controller/sat_index_controller.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

backend/src/gee/caching_script.py

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,99 @@
1-
import json
2-
3-
content = [
4-
{"timestamp": 1493251200, "value": 0.6265267304234295},
5-
{"timestamp": 1494720000, "value": 0.68603163673333},
6-
{"timestamp": 1494979200, "value": 0.755257128311451},
7-
]
8-
var_name = "msavi_daily_cache"
9-
file_name = "../cache/temp_cache.py"
10-
INDENT = " "
11-
12-
def combine_chache_with_update():
13-
# update existing cache
14-
return
15-
16-
def write_year(file, year, results):
17-
file.write(f"{INDENT}# Year {year}\n")
18-
19-
def write_results_to_cache(results: list, var_name: str, file_name: str):
20-
with open(file_name, "w") as file:
21-
file.write(f"{var_name} = [\n")
22-
for day in results:
23-
file.write(f"{INDENT}{json.dumps(day)},\n")
1+
import os
2+
from enum import Enum
3+
4+
from ..constants import IndexType
5+
from ..cache import ndvi_cache
6+
from ..cache import msavi_cache
7+
8+
9+
TIMESTAMP = "timestamp"
10+
CACHE_PATH = "../cache/"
11+
12+
13+
class ChosenCache(Enum):
14+
NDVI = ndvi_cache.ndvi_daily_cache
15+
MSAVI = msavi_cache.msavi_daily_cache
16+
17+
18+
index_type_to_cache = {
19+
IndexType.NDVI: ChosenCache.NDVI,
20+
IndexType.MSAVI: ChosenCache.MSAVI,
21+
}
22+
23+
24+
class CacheName(Enum):
25+
NDVI = "ndvi_daily_cache"
26+
MSAVI = "msavi_daily_cache"
27+
28+
29+
index_type_to_cache_name = {
30+
IndexType.NDVI: CacheName.NDVI,
31+
IndexType.MSAVI: CacheName.MSAVI,
32+
}
33+
34+
35+
class CacheFileName(Enum):
36+
NDVI = "ndvi_cache.py"
37+
MSAVI = "msavi_cache.py"
38+
39+
40+
index_type_to_cache_file = {
41+
IndexType.NDVI: CacheFileName.NDVI,
42+
IndexType.MSAVI: CacheFileName.MSAVI,
43+
}
44+
45+
46+
def load_cache_file(index_type):
47+
try:
48+
cache_list = index_type_to_cache[index_type].value
49+
if isinstance(cache_list, list) and all(
50+
isinstance(item, dict) for item in cache_list
51+
):
52+
return cache_list
53+
except (SyntaxError, ValueError, IndexError):
54+
pass
55+
56+
return []
57+
58+
59+
def save_cache_to_file(cache, index_type):
60+
"""Save the updated cache to the file, maintaining the variable structure."""
61+
dirname = os.path.dirname(__file__)
62+
filepath = os.path.join(
63+
dirname, CACHE_PATH, index_type_to_cache_file[index_type].value
64+
)
65+
with open(filepath, "w") as file:
66+
cache_name = index_type_to_cache_name[index_type].value
67+
file.write(f"{cache_name} = [\n")
68+
for entry in cache:
69+
file.write(f" {entry},\n")
2470
file.write("]\n")
25-
file.close()
2671

27-
return
72+
73+
def update_cache_file(data, index_type: IndexType):
74+
"""Update the cache with new data while maintaining chronological order."""
75+
cache = load_cache_file(index_type)
76+
existing_timestamps = {entry[TIMESTAMP] for entry in cache}
77+
78+
# Add only new entries
79+
new_entries = [
80+
entry for entry in data if entry[TIMESTAMP] not in existing_timestamps
81+
]
82+
cache.extend(new_entries)
83+
84+
# Sort the cache by timestamp
85+
cache.sort(key=lambda x: x[TIMESTAMP])
86+
87+
# Save back to the file
88+
save_cache_to_file(cache, index_type)
2889

2990

30-
# write_results_to_cache(content, var_name, file_name)
91+
def update_cache(input: list[dict], index_type: IndexType):
92+
try:
93+
if isinstance(input, list) and all(isinstance(item, dict) for item in input):
94+
update_cache_file(input, index_type)
95+
print("Cache updated successfully.")
96+
else:
97+
print("Invalid input format. Expected a list of dictionaries.")
98+
except ValueError as e:
99+
print(f"Error parsing input data: {e}")

backend/src/gee/sat_index_info.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
def get_index_image_by_index_type(index_type: IndexType, image: ee.Image):
1010
match index_type:
1111
case IndexType.NDVI:
12-
return image.normalizedDifference(["B8", "B4"]).rename("NDVI")
12+
return image.normalizedDifference(["B8", "B4"]).rename("NDVI") # B8 = NIR, B4 = RED
1313
case IndexType.MSAVI:
1414
return image.expression(
1515
expression="((2 * NIR + 1) - ((2 * NIR + 1)**2 - 8 * (NIR - RED))**0.5) / 2",
1616
opt_map={
17-
"NIR": image.select("B4"),
18-
"RED": image.select("B8"),
17+
"NIR": image.select("B8"),
18+
"RED": image.select("B4"),
1919
},
2020
).rename("MSAVI")
2121
case _:

backend/src/service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def sat_index_service(
169169
sat_index_time_series = get_sat_index_info(
170170
masked_images, LocationPolygon[location.value].value, index_type
171171
)
172+
# call caching script here
172173

173174
# Get cached range
174175
if cache_start_date:

backend/src/validation/utils.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,3 @@ def validate_timestamp_start_date_before_end_date(startDate, endDate):
2626
raise HTTPException(
2727
status_code=400, detail="endDate must be after startDate")
2828
return endDate
29-
30-
31-
def validate_temperature_timestamp_in_range(start, end):
32-
min_timestamp = -946771200 # 01/01/1940
33-
max_timestamp = int(time.time()) # now
34-
print(end)
35-
print(max_timestamp)
36-
if (start < min_timestamp or end > max_timestamp):
37-
raise HTTPException(
38-
status_code=400, detail=f"Timestamp must be between {min_timestamp} and {max_timestamp}")
39-
elif end <= start:
40-
raise HTTPException(
41-
status_code=400, detail="endDate must be after startDate")
42-
return end

0 commit comments

Comments
 (0)