Skip to content
Snippets Groups Projects
Commit 36ed3f5b authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

update: rename get_measurement, and add back self

parent 63290bae
No related branches found
No related tags found
1 merge request!16Clhp map into main
This diff is collapsed.
No preview for this file type
......@@ -10,7 +10,7 @@ from map_handler.add_new_lake import cut_map
from server.scheduler import update_scheduler
from server.consts import LAKE_RELATIONS_PATH
from map_handler.get_lake_relation import get_map_data
from map_handler.get_measurements import get_measurements
from map_handler.update_measurements import update_measurements
from map_handler.input_new_data import input_new_Lidar_data
app = Flask(__name__)
......@@ -74,7 +74,7 @@ class IceHTTP(BaseHTTPRequestHandler):
self.send_response(400)
self.send_header('Content-type', 'application/json')
self.end_headers()
elif self.path.startswith('/get_measurements'):
elif self.path.startswith('/update_measurements'):
parsed_path = urlparse(self.path)
query_params = parse_qs(parsed_path.query)
......@@ -82,7 +82,7 @@ class IceHTTP(BaseHTTPRequestHandler):
lake_name = unquote(lake_name_param) # Decode url param
if lake_name_param:
get_measurements(self, lake_name)
update_measurements(self, lake_name)
else:
self.send_response(400)
self.send_header('Content-type', 'application/json')
......
File deleted
File deleted
File added
......@@ -7,12 +7,13 @@ from server.consts import LAKE_RELATIONS_PATH
from server.ModelFromNVE.icemodellingscripts.getIceThicknessLakes import get_raw_dates, ice_prognosis_raw_data
def get_measurements(lake_name):
def update_measurements(self, lake_name: str):
"""
Retrieves LiDar data for a given lake, and adds weather data to each subdivision.
Parameters:
lake_name (str): The name of the requested file/lake
self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler
lake_name (str): The name of the requested lake
"""
try:
# Define file path to lidar data file
......@@ -89,19 +90,39 @@ def get_measurements(lake_name):
# Write the newest measurements to file
with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_measurements.json', 'w') as f:
json.dump(measurements, f)
json.dump(measurements, f, indent=4)
if self is not None:
# Convert list of dictionaries to JSON
response_data = json.dumps(measurements, indent=4)
# Set headers
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
# Write processed data to response object
self.wfile.write(response_data.encode('utf-8'))
except Exception as e:
print(f"Error in updating measurements: {e}")
if self is not None:
# Set headers
self.send_response(500)
self.send_header("Content-type", "application/json")
self.end_headers()
print("Reached exception. Right before self")
self.wfile.write(f"Error in updating measurements: {e}".encode('utf-8'))
def fill_remaining_subdivisions(lake_name: str, sub_div_ids: list):
def fill_remaining_subdivisions(lake_name: str, processed_ids: list):
"""
Returns a list of subdivision dictionaries for subdivisions without measurements.
Parameters:
lake_name (str): The name of the requested file/lake
sub_div_ids (list): A list of ids (int) of all subdivisions that have already been processed
processed_ids (list): List of ids (int) of all subdivisions that have already been processed
Returns:
sub_divisions (list): A list of subdivision dictionaries
......@@ -116,12 +137,14 @@ def fill_remaining_subdivisions(lake_name: str, sub_div_ids: list):
# Loop through each feature and extract all subdivisions
for sub_div in relation['features']:
sub_div_id = int(sub_div['properties']['sub_div_id'])
# Only get subdivisions that are not in the list
if sub_div_id not in sub_div_ids:
# Only get subdivisions that are not in the list already
if sub_div_id not in processed_ids:
center_lat = sub_div['properties']['sub_div_center'][0]
center_lng = sub_div['properties']['sub_div_center'][1]
# Fetch weather data for each subdivision from the NVE model
# NB hard coded date to december 2024 for testing, remove to_date param before use
# temp_date = datetime(2024, 12, 12)
ice_stats = get_raw_dates(ice_prognosis_raw_data(sub_div_id=sub_div_id, x=center_lat, y=center_lng))
total_ice_thickness = ice_stats[0]['Total ice (m)']
......
......@@ -2,14 +2,14 @@ import json
import time
import schedule
from map_handler.get_measurements import get_measurements
from map_handler.update_measurements import update_measurements
from server.consts import LAKE_RELATIONS_PATH
def update_all_measurements(lake_names: list):
"""Loops through all lake names and calls get_measurements() on each lake"""
for lake in lake_names:
get_measurements(lake)
update_measurements(None, lake)
def update_scheduler():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment