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

update: som error hadling in get_measurements.py

parent b21b134f
No related branches found
No related tags found
1 merge request!16Clhp map into main
...@@ -81,7 +81,7 @@ class IceHTTP(BaseHTTPRequestHandler): ...@@ -81,7 +81,7 @@ class IceHTTP(BaseHTTPRequestHandler):
lake_name = unquote(lake_name_param) # Decode url param lake_name = unquote(lake_name_param) # Decode url param
if lake_name_param: if lake_name_param:
get_measurements(self, cursor, lake_name) get_measurements(self, lake_name)
else: else:
self.send_response(400) self.send_response(400)
self.send_header('Content-type', 'application/json') self.send_header('Content-type', 'application/json')
......
import os
import json import json
from datetime import datetime from datetime import datetime
...@@ -5,7 +6,7 @@ from server.consts import LAKE_RELATIONS_PATH ...@@ -5,7 +6,7 @@ from server.consts import LAKE_RELATIONS_PATH
from server.ModelFromNVE.icemodellingscripts.getIceThicknessLakes import get_raw_dates, ice_prognosis_raw_data from server.ModelFromNVE.icemodellingscripts.getIceThicknessLakes import get_raw_dates, ice_prognosis_raw_data
def get_measurements(self, cursor, lake_name): def get_measurements(self, lake_name):
""" """
Retrieves LiDar data for a given lake, and adds weather data to each subdivision. Retrieves LiDar data for a given lake, and adds weather data to each subdivision.
...@@ -15,14 +16,18 @@ def get_measurements(self, cursor, lake_name): ...@@ -15,14 +16,18 @@ def get_measurements(self, cursor, lake_name):
lake_name (str): The name of the requested file/lake lake_name (str): The name of the requested file/lake
""" """
try: try:
# Read the newest lidar data from JSON file file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_measurements.json')
with open(LAKE_RELATIONS_PATH + lake_name + '_lidar_data.json', 'r') as file:
lidar_data = json.load(file)
sub_div_ids = [] sub_div_ids = []
measurements = []
# Check if the file exists
if os.path.exists(file_path):
# Read the newest lidar data from JSON file
with open(LAKE_RELATIONS_PATH + lake_name + '_measurements.json', 'r') as file:
measurements = json.load(file)
# Iterate over all fetched rows # Iterate over all fetched rows
for measurement in lidar_data: for measurement in measurements:
for sub_division in measurement['Subdivisions']: for sub_division in measurement['Subdivisions']:
measurement_id = measurement['MeasurementID'] measurement_id = measurement['MeasurementID']
...@@ -42,15 +47,16 @@ def get_measurements(self, cursor, lake_name): ...@@ -42,15 +47,16 @@ def get_measurements(self, cursor, lake_name):
'Color': calculateColor(sub_division['MinThickness'], ), 'Color': calculateColor(sub_division['MinThickness'], ),
# NB color calculated based on average thickness, should be minimum # NB color calculated based on average thickness, should be minimum
# Fetch weather data from the NVE model # Fetch weather data from the NVE model
'IceStats': get_raw_dates(ice_prognosis_raw_data(sub_div_id=subdiv_id, x=center_lat, y=center_lng)) 'IceStats': get_raw_dates(
ice_prognosis_raw_data(sub_div_id=subdiv_id, x=center_lat, y=center_lng))
} }
sub_div_ids.append(subdiv_id) sub_div_ids.append(subdiv_id)
lidar_data[measurement_id]['Subdivisions'].append(sub_division) measurements[measurement_id]['Subdivisions'].append(sub_division)
# Populate remaining subdivisions and create "invalid" or "proxy" measurement to store them # Populate remaining subdivisions and create "invalid" or "proxy" measurement to store them
remaining_sub_divs = fill_remaining_subdivisions(lake_name, sub_div_ids) remaining_sub_divs = fill_remaining_subdivisions(lake_name, sub_div_ids)
lidar_data[-1] = { measurements[-1] = {
'MeasurementID': -1, 'MeasurementID': -1,
'TimeMeasured': str(datetime.now()), 'TimeMeasured': str(datetime.now()),
'CenterLat': None, 'CenterLat': None,
...@@ -60,21 +66,21 @@ def get_measurements(self, cursor, lake_name): ...@@ -60,21 +66,21 @@ def get_measurements(self, cursor, lake_name):
} }
# Convert dictionary values to list of measurements # Convert dictionary values to list of measurements
data = list(lidar_data.values()) data = list(measurements.values())
# Write the newest measurements to file # Write the newest measurements to file
with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_lidar_data.json', 'w') as f: with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_measurements.json', 'w') as f:
json.dump(data, f) json.dump(data, f)
if len(data) == 0: if len(data) == 0:
marker_data = json.dumps(['no measurements']) response_data = json.dumps(['no measurements'])
else: else:
# Convert list of dictionaries to JSON # Convert list of dictionaries to JSON
marker_data = json.dumps(data, indent=4) response_data = json.dumps(data, indent=4)
except Exception as e: except Exception as e:
print(f"Error in getting measurements: {e}") print(f"Error in getting measurements: {e}")
marker_data = '[]' response_data = '[]'
# Set headers # Set headers
self.send_response(500) self.send_response(500)
...@@ -86,8 +92,8 @@ def get_measurements(self, cursor, lake_name): ...@@ -86,8 +92,8 @@ def get_measurements(self, cursor, lake_name):
self.send_header("Content-type", "application/json") self.send_header("Content-type", "application/json")
self.end_headers() self.end_headers()
# Write marker data to response object # Write processed data to response object
self.wfile.write(marker_data.encode('utf-8')) self.wfile.write(response_data.encode('utf-8'))
def fill_remaining_subdivisions(lake_name: str, sub_div_ids: list): def fill_remaining_subdivisions(lake_name: str, sub_div_ids: list):
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"TimeMeasured": "2024-04-15 16:23:28.620516", "TimeMeasured": "2024-04-15 16:23:28.620516",
"CenterLat": 60.841532, "CenterLat": 60.841532,
"CenterLon": 10.717878, "CenterLon": 10.717878,
"Sensor": null, "Sensor": 2,
"Subdivisions": [ "Subdivisions": [
{ {
"SubdivID": 36, "SubdivID": 36,
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
"AvgThickness": 1, "AvgThickness": 1,
"CenLatitude": 60.841532, "CenLatitude": 60.841532,
"CenLongitude": 10.717878, "CenLongitude": 10.717878,
"Accuracy": null, "Accuracy": 2,
"Heights": [1] "Heights": [1]
}, },
{ {
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
"AvgThickness": 14, "AvgThickness": 14,
"CenLatitude": 60.828326, "CenLatitude": 60.828326,
"CenLongitude": 10.982563, "CenLongitude": 10.982563,
"Accuracy": null, "Accuracy": 2,
"Heights": [1, 27] "Heights": [1, 27]
}, },
{ {
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
"AvgThickness": 1, "AvgThickness": 1,
"CenLatitude": 60.771059, "CenLatitude": 60.771059,
"CenLongitude": 10.698341, "CenLongitude": 10.698341,
"Accuracy": null, "Accuracy": 2,
"Heights": [1] "Heights": [1]
}, },
{ {
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
"AvgThickness": 9, "AvgThickness": 9,
"CenLatitude": 60.396856, "CenLatitude": 60.396856,
"CenLongitude": 11.220933, "CenLongitude": 11.220933,
"Accuracy": null, "Accuracy": 2,
"Heights": [1, 4, 6, 27, 7] "Heights": [1, 4, 6, 27, 7]
} }
] ]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment