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

update: buggy, first attempt to implement ice stats

parent 7649cb49
No related branches found
No related tags found
1 merge request!12Clhp map
File added
......@@ -12,5 +12,3 @@ SSL_CERT_PATH = CERT_DIR + "testCert.crt"
# File paths
MAP_HANDLER_PATH = "server/map_handler/"
LAKE_RELATIONS_PATH = MAP_HANDLER_PATH + "lake_relations/"
PLOT_PATH = "server/ModelFromNVE/output/plots/"
No preview for this file type
No preview for this file type
import json
import random
from datetime import datetime
from server.consts import PLOT_PATH
from server.ModelFromNVE.icemodellingscripts.getIceThicknessLakes import get_raw_dates, ice_prognosis_raw_data
# get_markers requests all marker data or valid markers, converts the data to json, and writes
# the data to the response object
def get_measurements(self, cursor, lake_name):
try:
print("Lake name in get_measurements: ", lake_name)
sql_query = '''
SELECT m.MeasurementID, m.SensorID, m.TimeMeasured, m.CenterLat, m.CenterLon,
s.SensorType, s.Active,
b.Name,
d.SubDivisionID, d.GroupID, d.MinimumThickness,
d.AverageThickness, d.CenterLatitude, d.CenterLongitude,
d.Accuracy
FROM Measurement m
INNER JOIN Sensor s ON m.SensorID = s.SensorID
INNER JOIN BodyOfWater b ON m.WaterBodyName = b.Name
LEFT JOIN SubDivision d ON m.MeasurementID = d.MeasurementID
WHERE b.Name = ?
'''
cursor.execute(sql_query, (lake_name,))
rows = cursor.fetchall()
# List of all fetched measurement objects
measurement_data = {}
'''
f = open('server/?')
data = json.load(f)
'''
# Iterate over all fetched rows
for row in rows:
measurement_id = row[0]
sub_div_id = row[8]
center_lat = row[12]
center_lng = row[13]
'''
curr_stat = []
# Find matching ice stat
for stat in data['stats']
if ice_stats.sub_div_id == sub_div_id
curr_stat = stat
break
'''
# Create subdivision new object
sub_division = {
'SubdivID': sub_div_id,
'GroupID': row[9],
'MinThickness': row[10],
'AvgThickness': row[11],
'CenLatitude': center_lat,
'CenLongitude': center_lng,
'Accuracy': row[14],
'Color': calculateColor(row[11]), # NB color calculated based on average thickness, should be minimum
'IceStats': () # NB temp empty, add ice stats later
}
# Check if measurement ID already exists in measurement_data
if measurement_id in measurement_data:
# Create new subdivision within measurement if it does not already exist
if sub_division not in measurement_data[measurement_id]['Subdivisions']:
measurement_data[measurement_id]['Subdivisions'].append(sub_division)
else:
# Create a new entry for measurement_id if it does not already exist in the list
measurement_data[measurement_id] = {
'MeasurementID': measurement_id,
'TimeMeasured': row[2],
'CenterLat': row[3],
'CenterLon': row[4],
'Sensor': { # Each measurement only has one related sensor
'SensorID': row[1],
'SensorType': row[5],
'Active': bool(row[6])
},
'Subdivisions': [sub_division], # Array of sub_division objects
}
# NB remember to clos file after implementation
# f.close()
# Convert dictionary values to list of measurements
data = list(measurement_data.values())
if len(data) == 0:
marker_data = json.dumps(['no measurements'])
else:
# Convert list of dictionaries to JSON
marker_data = json.dumps(data, indent=4)
except Exception as e:
print(f"Error in querying database: {e}")
marker_data = '[]'
# Set headers
self.send_response(500)
self.send_header("Content-type", "application/json")
self.end_headers()
# Set headers
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
# Write marker data to response object
self.wfile.write(marker_data.encode('utf-8'))
def get_measurements(self, cursor, lake_name):
try:
print("Lake name in get_measurements: ", lake_name)
sql_query = '''
SELECT m.MeasurementID, m.SensorID, m.TimeMeasured, m.CenterLat, m.CenterLon,
s.SensorType, s.Active,
......@@ -161,6 +46,10 @@ def get_measurements(self, cursor, lake_name):
curr_stat = stat
break
'''
# NB test
print("sub_div_id before call: ", sub_div_id, " center lat and lng: ", center_lng, " ", center_lng)
ice_stats = get_raw_dates(ice_prognosis_raw_data(sub_div_id, center_lat, center_lng))
print("ice stats:", ice_stats[0])
# Create subdivision new object
sub_division = {
......@@ -172,7 +61,7 @@ def get_measurements(self, cursor, lake_name):
'CenLongitude': center_lng,
'Accuracy': row[14],
'Color': calculateColor(row[11]), # NB color calculated based on average thickness, should be minimum
'IceStats': () # NB temp empty, add ice stats later
'IceStats': get_raw_dates(ice_prognosis_raw_data(sub_div_id, center_lat, center_lng))
}
# Check if measurement ID already exists in measurement_data
......@@ -196,8 +85,6 @@ def get_measurements(self, cursor, lake_name):
'Subdivisions': [sub_division], # Array of sub_division objects
}
# Add ice stats
# NB remember to clos file after implementation
# f.close()
......@@ -211,7 +98,7 @@ def get_measurements(self, cursor, lake_name):
marker_data = json.dumps(data, indent=4)
except Exception as e:
print(f"Error in querying database: {e}")
print(f"Error in getting measurements: {e}")
marker_data = '[]'
# Set headers
......@@ -239,5 +126,3 @@ def calculateColor(thickness: float): # NB neither final colors nor ranges
return 0xFF00d6ff # Blue
else:
return 0xFF939393 # Gray
#def addIceStats(lake: str, measurements: list):
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