From 9d2dbe47f7acb7935dfe22b071a8d84502674e85 Mon Sep 17 00:00:00 2001 From: Joakim <joakiale@stud.ntnu.no> Date: Wed, 10 Apr 2024 14:51:47 +0200 Subject: [PATCH] add: added all functionality (i hope) for nve model to fit front end --- .../getIceThicknessLakes.py | 185 +++++++++++++++--- 1 file changed, 163 insertions(+), 22 deletions(-) diff --git a/server/ModelFromNVE/icemodellingscripts/getIceThicknessLakes.py b/server/ModelFromNVE/icemodellingscripts/getIceThicknessLakes.py index cdcd83b8..42f05907 100644 --- a/server/ModelFromNVE/icemodellingscripts/getIceThicknessLakes.py +++ b/server/ModelFromNVE/icemodellingscripts/getIceThicknessLakes.py @@ -1,4 +1,3 @@ - import copy import datetime as dt import utm @@ -9,11 +8,41 @@ from server.ModelFromNVE.icemodelling import parameterization as dp, icethicknes ice as ice from server.ModelFromNVE.utilities import makeplots as pts, getgts as gts, getwsklima as gws -def ice_prognosis_raw_data(to_date = None, sub_div_id = 0, met_stnr=0, x=10.709985478463118, y=60.810991171403316, altitude=0, awt=[], mf=0, icerun_dates=[]): + +def ice_prognosis_raw_data(to_date=None, sub_div_id=0, x=10.709985478463118, y=60.810991171403316, + altitude=0, awt=[], mf=0, icerun_dates=[]): """ - lon x - lat y - :return: + + Args: + to_date: the end date for prognosis season, defaults to seven days into the future + sub_div_id: + x: lat + y: lon + altitude: in meters + awt: list of avrage water temperatures + mf: melt factor used by NVE, not much info on this variable + icerun_dates: date for ice of the lake + + Returns: + a list of data at this format: + [ + [ + "Date": date.strftime("%Y-%m-%d"), + "Slush ice (m)": round(slush, 3), + "Black ice (m)": round(black, 3), + "Total ice (m)": round(total, 3), + "Snow depth (m)": round(snow2, 3), + "Total snow (m)": round(sno_tot2, 3), + "Cloud cover": round(cc2, 3), + "Temperature (c)": round(temp2, 3) + ] + ... + [ + ... + ] + ] + + """ current_date = dt.datetime.now() @@ -37,6 +66,9 @@ def ice_prognosis_raw_data(to_date = None, sub_div_id = 0, met_stnr=0, x=10.7099 cords = utm.from_latlon(y, x, 33) x, y = int(cords[0]), int(cords[1]) + # check if utm is valid + x, y = validate_cords(x, y) + gridTemp = gts.getgts(x, y, 'tm', from_date, to_date) gridSno = gts.getgts(x, y, 'sdfsw', from_date, to_date) gridSnoTot = gts.getgts(x, y, 'sd', from_date, to_date) @@ -65,9 +97,9 @@ def ice_prognosis_raw_data(to_date = None, sub_div_id = 0, met_stnr=0, x=10.7099 # cumulated ammount of each ice type at a given date slush_ice = [] black_ice = [] - total = [] + total = [] total_ice = [] - dates = [] + dates = [] for i in calculated_ice: ice_type = -10 @@ -97,20 +129,51 @@ def ice_prognosis_raw_data(to_date = None, sub_div_id = 0, met_stnr=0, x=10.7099 sno_tot, cc, temp): daily_data = { "Date": date.strftime("%Y-%m-%d"), - "Slush ice (m)": slush, - "Black ice (m)": black, - "Total ice (m)": total, - "Snow depth (m)": snow2, - "Total snow (m)": sno_tot2, - "Cloud cover": cc2, - "Temperature (c)": temp2 + "Slush ice (m)": round(slush, 3), + "Black ice (m)": round(black, 3), + "Total ice (m)": round(total, 3), + "Snow depth (m)": round(snow2, 3), + "Total snow (m)": round(sno_tot2, 3), + "Cloud cover": round(cc2, 3), + "Temperature (c)": round(temp2, 3) } data.append(daily_data) return data # return [sub_div_id, x, y, data] + +def validate_cords(easting, northing): + """ + Args: + easting: + northing: + + Returns: + if the easting and northing is not acceptable at utm 33 returns middle of mjosa as new easting and northing values + + """ + default_x, default_y = 266707, 6749365 + + if not (100000 <= easting <= 900000) or not (0 <= northing <= 10000000): + print("cords given are made default to middle of lake mjosa") + easting, northing = default_x, default_y + else: + print("cords are kept") + + return easting, northing + + def get_raw_dates(data, from_date=None, to_date=None): + """ + Args: + data: data from ice_prognosis_raw_data + from_date: if not given default to 4 days into future + to_date: if not given default to 3 days into the past + + Returns: + returns data for the specified time slot + """ if from_date is None: from_date = (dt.datetime.now() - dt.timedelta(days=3)).strftime("%Y-%m-%d") if to_date is None: @@ -119,10 +182,22 @@ def get_raw_dates(data, from_date=None, to_date=None): filtred_data = [entry for entry in data if from_date <= entry["Date"] <= to_date] return filtred_data + # change to take a list of data with an sub div id first followed by data [sub_div_id, data] -def jsonify_data(data, location=se.plot_folder): +def jsonify_data(data, name="temp", location=se.plot_folder): + """ + + Args: + data: the data to be put into file + name: name of file -> data_{name}.json + location: location, default to se.plot_folder + + Returns: + creates a .json file + + """ os.makedirs(location, exist_ok=True) - file_path = os.path.join(location, "data_test_test1.json") + file_path = os.path.join(location, f"data_{name}.json") try: with open(file_path, 'w') as json_file: @@ -131,12 +206,24 @@ def jsonify_data(data, location=se.plot_folder): except Exception as e: print(f"Failed to save data to JSON file. Error: {e}") -def jsonify_data_sub_div_ids(sub_div_and_data, location=se.plot_folder): + +def jsonify_data_sub_div_ids(lake_name, sub_div_and_data, location=se.plot_folder): + """ + + Args: + lake_name: put as file name {lake_name}_sub_div.json + sub_div_and_data: all data at this format [(id, data) ... (...)] + location: plot folder defaults to se.plot_folder + + Returns: + creates a .json file + + """ aggregated_data = {entry[0]: entry[1] for entry in sub_div_and_data} os.makedirs(location, exist_ok=True) - filename = "aggregated_data_test_test.json" + filename = f"{lake_name}_sub_div.json" file_path = os.path.join(location, filename) try: @@ -146,9 +233,32 @@ def jsonify_data_sub_div_ids(sub_div_and_data, location=se.plot_folder): except Exception as e: print(f"Failed to save data to JSON file. Error: {e}") -def expose_data() + +# def expose_data() +def get_subdiv_ids_n_cords(file_path): + """ + + Args: + file_path: path of the file with ids lat lon values + + Returns: + a list of ids lat lon + + """ + # reads file and gets all ids and cords at this format [(id, x, y), (id, x, y) ... ] + id_list = [] + + with open(file_path, 'r') as file: + for line in file: + data = line.strip().split(',') + if len(data) == 3: + id_list.append((int(data[0]), float(data[1]), float(data[2]))) + + return id_list + if __name__ == "__main__": + ''' data = ice_prognosis_raw_data() from_date = "2024-01-10" @@ -156,10 +266,41 @@ if __name__ == "__main__": filtered_dates = get_raw_dates(data, from_date, to_date) jsonify_data(filtered_dates) filtered_dates2 = get_raw_dates(data) - all_will_be_one = [[1, filtered_dates2], [2, filtered_dates]] + + all_will_be_one = [(1, filtered_dates2), [2, filtered_dates]] jsonify_data_sub_div_ids(all_will_be_one) + ''' - #ice_prognosis() + sub_divs = get_subdiv_ids_n_cords('../../lake_relations/skumsjøen_centers.txt') # lokasjon for txt fil - pass \ No newline at end of file + from_date = "2024-01-10" + to_date = "2024-01-20" + #filtered_data_for_dates = [(i[0], get_raw_dates(ice_prognosis_raw_data(sub_div_id=i[0], x=i[1], y=i[2])), from_date, to_date) for i in sub_divs ] + filtered_data_for_dates = [(i[0], get_raw_dates(ice_prognosis_raw_data(sub_div_id=i[0], x=i[1], y=i[2]))) for i in sub_divs ] + + jsonify_data_sub_div_ids("skumsjoen", filtered_data_for_dates, location = se.plot_folder) + ''' + filtered_data_for_dates = [] + + # Iterate over each subdivision in the list + for subdivision in sub_divs: + # Unpack the ID, x-coordinate, and y-coordinate for the subdivision + sub_div_id = subdivision[0] + x_coordinate = subdivision[1] + y_coordinate = subdivision[2] + + # Fetch the raw data for the subdivision + raw_data = ice_prognosis_raw_data(sub_div_id=sub_div_id, x=x_coordinate, y=y_coordinate) # <- problem + + # Filter the raw data based on the specified date range + filtered_data = get_raw_dates(raw_data, from_date, to_date) + + # Add the filtered data to the list + filtered_data_for_dates.append(filtered_data) + ''' + print("hello world") + + # ice_prognosis() + + pass -- GitLab