generated from oceanhackweek/ohwyy_proj_template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CheckJson.py
92 lines (66 loc) · 2.81 KB
/
CheckJson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# %% ---------------------------------------------
# Load packages
import json
import pickle as pkl
import numpy as np
from datetime import datetime
# %% ---------------------------------------------
# Instructions for Ranisa
# This script extracts the maximum temperature anomaly from the JSON file and saves the output as a pickle file
# (1) setup a trigger that runs this script when a new json file is added to the folder
# (2) change the load directory to where you have stored the json files
# (3) load the MAI090_PercentilesHeatMap.json file, run the loop
# (4) change the save directory to where you want to save the pickle file and run code to save
# Notes
# the runpy package could be used to run this script from the web app script?
# see example below:
# import runpy
# # Assuming 'my_script' is a module in your Python path
# runpy.run_path('path/to/my_script.py')
# %% ---------------------------------------------
# load latest json plot
# Temp anomaly (called percentiles) plot
load_directory = r'C:\Users\mphem\OneDrive - UNSW\Work\OHW\ohw24_OceanExtremes_VisReportApp_au\New_data'
# load_directory = r'C:\Users\mphem\OneDrive - UNSW\Work\OHW\ohw24_OceanExtremes_VisReportApp_au\Figures'
# Load the JSON file
with open(load_directory + '\\' + 'MAI090_PercentilesHeatMap.json', 'r') as file:
heatmaps = json.load(file)
# %% ---------------------------------------------
# Extract the data from the JSON object
data = heatmaps['data']
# Prepare to track the maximum temperature anomalies by year
max_temps_by_year = {}
for n in range(len(data)):
x = data[n]['x']
y = data[n]['y']
z = data[n]['z']
# Convert None to np.nan
z = np.array(z, dtype=float).flatten()
z = np.where(z == None, np.nan, z)
name = data[n]['name']
year = name.split(' ')[0]
depth = name.split(' ')[-1]
# Find the maximum temperature anomaly in the data for this year
if np.isfinite(z).sum() > 10:
max_temp = np.nanmax(z)
else:
max_temp = np.nan
max_temps_by_year[year] = max_temp
all_time_depth_max = np.nanmax(list(max_temps_by_year.values()))
# Output the results
# for year, max_temp in max_temps_by_year.items():
# print(f"Year {year}: Max Temperature Anomaly {max_temp}")
# %% ---------------------------------------------
# save variables using pickle
save_directory = r'C:\Users\mphem\OneDrive - UNSW\Work\OHW\ohw24_OceanExtremes_VisReportApp_au\New_data'
# Get the current date and time
now = datetime.now()
# Format the date and time as 'YYYYMMDD_HHMM'
formatted_time = now.strftime('%Y%m%d_%H%M%S')
data_to_save = {
'max_temps_by_year': max_temps_by_year,
'all_time_depth_max': all_time_depth_max
}
# Save data to a pickle file
with open(save_directory + '\\' + 'maxTemp_' + formatted_time + '.pkl', 'wb') as file:
pkl.dump(data_to_save, file)