-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
315 lines (250 loc) · 11 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
""" This file shows an example of how to predict solar balloon trajectories and produces several plots
as well as an html trajectory map that uses Google maps and can be opened in an internet browser.
run saveNETCDF.py before running this file to download a forecast from NOAA.
Maybe convert to this new library later https://unidata.github.io/python-training/workshop/Bonus/downloading-gfs-with-siphon/
"""
import math
import solve_states
import GFS
import ERA5
from termcolor import colored
import matplotlib.pyplot as plt
import fluids
import gmplot
import time as tm
import pandas as pd
import os
import numpy as np
import re
import copy
import seaborn as sns
import xarray as xr
from netCDF4 import Dataset
import radiation
import config_earth
import windmap
if not os.path.exists('trajectories'):
os.makedirs('trajectories')
scriptstartTime = tm.time()
GMT = 7 #0 # UTC MST
dt = config_earth.simulation['dt']
coord = config_earth.simulation['start_coord']
t = config_earth.simulation['start_time']
start = t
nc_start = config_earth.netcdf_gfs["nc_start"]
min_alt = config_earth.simulation['min_alt']
alt_sp = config_earth.simulation['alt_sp']
v_sp = config_earth.simulation['v_sp']
sim_time = config_earth.simulation['sim_time'] * int(3600*(1/dt))
lat = [coord["lat"]]
lon = [coord["lon"]]
GFSrate = config_earth.forecast['GFSrate']
hourstamp = config_earth.netcdf_gfs['hourstamp']
balloon_trajectory = config_earth.simulation['balloon_trajectory']
forecast_type = config_earth.forecast['forecast_type']
atm = fluids.atmosphere.ATMOSPHERE_1976(min_alt)
'''
#Some netcdf testing stuff
rootgrp = Dataset(config_earth.netcdf_gfs['nc_file'], "r", format="NETCDF4")
#rootgrp = Dataset("forecasts/" + config_earth.netcdf_era5['filename'], "r", format="NETCDF4")
print(rootgrp.data_model)
print(rootgrp.groups)
print(rootgrp.dimensions)
print(rootgrp.variables)
for name in rootgrp.ncattrs():
print("Global attr {} = {}".format(name, getattr(rootgrp, name)))
sdfs
data = xr.open_dataset(config_earth.netcdf_gfs['nc_file'])
data = xr.open_dataset("forecasts/" + config_earth.netcdf_era5['filename'])
data2 = data.to_array()
print (data)
print(data.attrs)
'''
#Get trajectory name from config file for Google Maps:
if balloon_trajectory != None:
trajectory_name = copy.copy(balloon_trajectory)
replacements=[("balloon_data/", ""), (".csv", "")]
for pat,repl in replacements:
trajectory_name = re.sub(pat, repl, trajectory_name)
print (trajectory_name)
# Variables for Simulation and Plotting
T_s = [atm.T]
T_i = [atm.T]
T_atm = [atm.T]
el = [min_alt]
v= [0.]
coords = [coord]
x_winds_old = [0]
y_winds_old = [0]
x_winds_new = [0]
y_winds_new = [0]
ttt = [t - pd.Timedelta(hours=GMT)] #Just for visualizing plot better]
data_loss = False
burst = False
gmap1 = gmplot.GoogleMapPlotter(coord["lat"],coord["lon"], 9) #9 is how zoomed in the map starts, the lower the number the more zoomed out
e = solve_states.SolveStates()
if forecast_type == "GFS":
gfs = GFS.GFS(coord)
else:
gfs = ERA5.ERA5(coord)
lat_aprs_gps = [coord["lat"]]
lon_aprs_gps = [coord["lon"]]
ttt_aprs = [t - pd.Timedelta(hours=GMT)]
coords_aprs = [coord]
for i in range(0,sim_time):
T_s_new,T_i_new,T_atm_new,el_new,v_new, q_rad, q_surf, q_int = e.solveVerticalTrajectory(t,T_s[i],T_i[i],el[i],v[i],coord,alt_sp,v_sp)
T_s.append(T_s_new)
T_i.append(T_i_new)
el.append(el_new)
v.append(v_new)
T_atm.append(T_atm_new)
t = t + pd.Timedelta(hours=(1/3600*dt))
ttt.append(t - pd.Timedelta(hours=GMT)) #Just for visualizing plot better
if i % GFSrate == 0:
lat_new,lon_new,x_wind_vel,y_wind_vel, x_wind_vel_old, y_wind_vel_old, bearing,nearest_lat, nearest_lon, nearest_alt = gfs.getNewCoord(coords[i],dt*GFSrate) #(coord["lat"],coord["lon"],0,0,0,0,0,0)
coord_new = {
"lat": lat_new, # (deg) Latitude
"lon": lon_new, # (deg) Longitude
"alt": el_new, # (m) Elevation
"timestamp": t, # Timestamp
}
coords.append(coord_new)
lat.append(lat_new)
lon.append(lon_new)
x_winds_old.append(x_wind_vel_old)
y_winds_old.append(y_wind_vel_old)
x_winds_new.append(x_wind_vel)
y_winds_new.append(y_wind_vel)
rad = radiation.Radiation()
zen = rad.get_zenith(t, coord_new)
if i % 360*(1/dt) == 0:
print(str(t - pd.Timedelta(hours=GMT)) #Just for visualizing better
+ " el " + str("{:.4f}".format(el_new))
+ " v " + str("{:.4f}".format(v_new))
#+ " accel " + str("{:.4f}".format(dzdotdt))
+ " T_s " + str("{:.4f}".format(T_s_new))
+ " T_i " + str("{:.4f}".format(T_i_new))
+ " zen " + str(math.degrees(zen))
)
print(colored(("U wind speed: " + str(x_wind_vel) + " V wind speed: " + str(y_wind_vel) + " Bearing: " + str(bearing)),"yellow"))
print(colored(("Lat: " + str(lat_new) + " Lon: " + str(lon_new)),"green"))
print(colored(("Nearest Lat: " + str(nearest_lat) + " Nearest Lon: " + str(nearest_lon) +
" Nearest Alt: " + str(nearest_alt)),"cyan"))
df = None
#Plots
'''
Add code to check if the trajectory name exists before running simulations
Can I just switch order???
'''
if balloon_trajectory != None:
df = pd.read_csv(balloon_trajectory)
df["time"] = pd.to_datetime(df['time'])
df["time"] = df['time'] - pd.to_timedelta(7, unit='h') #Convert to MST
df["dt"] = df["time"].diff().apply(lambda x: x/np.timedelta64(1, 's')).fillna(0).astype('int64')
gmap1.plot(df['lat'], df['lng'],'white', edge_width = 2.5) # Actual Trajectory
gmap1.text(coord["lat"]-.1, coord["lon"]-.2, trajectory_name + " True Trajectory", color='white')
#Reforecasting
if balloon_trajectory != None:
alt_aprs = df["altitude"].to_numpy()
time_aprs = df["time"].to_numpy()
dt_aprs = df["dt"].to_numpy()
t = config_earth.simulation['start_time']
#t = config_earth.simulation['start_time']
for i in range(0,len(alt_aprs)-1):
lat_new,lon_new,x_wind_vel,y_wind_vel, x_wind_vel_old, y_wind_vel_old, bearing,nearest_lat, nearest_lon, nearest_alt = gfs.getNewCoord(coords_aprs[i],dt_aprs[i])
t = t + pd.Timedelta(seconds=dt_aprs[i+1])
ttt_aprs.append(t - pd.Timedelta(hours=GMT))
coord_new = {
"lat": lat_new, # (deg) Latitude
"lon": lon_new, # (deg) Longitude
"alt": alt_aprs[i], # (m) Elevation
"timestamp": t, # Timestamp
}
print(ttt_aprs[i], dt_aprs[i])
coords_aprs.append(coord_new)
lat_aprs_gps.append(lat_new)
lon_aprs_gps.append(lon_new)
print(colored(("El: " + str(alt_aprs[i]) + " Lat: " + str(lat_new) + " Lon: " + str(lon_new) + " Bearing: " + str(bearing)),"green"))
sns.set_palette("muted")
fig, ax = plt.subplots()
ax.plot(ttt,el, label = "reforecasted simulation")
plt.xlabel('Datetime (MST)')
plt.ylabel('Elevation (m)')
if balloon_trajectory != None:
ax.plot(df["time"],df["altitude"],label = "trajectory")
if forecast_type == "GFS":
gmap1.plot(lat_aprs_gps, lon_aprs_gps,'cyan', edge_width = 2.5) #Trajectory using Altitude balloon data with forecast data
gmap1.text(coord["lat"]-.3, coord["lon"]-.2, trajectory_name + " Alt + " + forecast_type + " Wind Data" , color='cyan')
elif forecast_type == "ERA5":
gmap1.plot(lat_aprs_gps, lon_aprs_gps,'orange', edge_width = 2.5) #Trajectory using Altitude balloon data with forecast data
gmap1.text(coord["lat"]-.3, coord["lon"]-.2, trajectory_name + " Alt + " + forecast_type + " Wind Data" , color='orange')
fig2, ax2 = plt.subplots()
ax2.plot(ttt,T_s,label="Surface Temperature")
ax2.plot(ttt,T_i,label="Internal Temperature")
ax2.plot(ttt,T_atm,label="Atmospheric Temperature")
#ax2.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M',tz=pytz.timezone(coord['timezone'])))
plt.xlabel('Datetime (MST)')
plt.ylabel('Temperature (K)')
plt.legend(loc='upper right')
plt.title('Solar Balloon Temperature - Earth')
def windVectorToBearing(u, v):
bearing = np.arctan2(v,u)
speed = np.power((np.power(u,2)+np.power(v,2)),.5)
return [bearing, speed]
'''
plt.figure()
plt.plot(ttt, x_winds_new, label = "X winds New", color = "blue")
plt.plot(ttt, x_winds_old, label = "X winds Old", color = "cyan")
plt.plot(ttt, y_winds_new, label = "Y winds New", color = "red")
plt.plot(ttt, y_winds_old, label = "Y winds Old", color = "orange")
'''
plt.legend(loc='upper right')
plt.title('Wind Interpolation Comparison')
#Winds Figure
plt.figure()
if any(x_winds_old):
plt.plot(ttt, np.degrees(windVectorToBearing(x_winds_old, y_winds_old)[0]), label = "Bearing old", color = "blue")
plt.plot(ttt, np.degrees(windVectorToBearing(x_winds_new, y_winds_new)[0]), label = "Bearing New", color = "red")
plt.legend(loc='upper right')
plt.title('Wind Interpolation Comparison')
# Outline Downloaded NOAA forecast subset:
if forecast_type == "GFS":
region= zip(*[
(gfs.LAT_LOW, gfs.LON_LOW),
(gfs.LAT_HIGH, gfs.LON_LOW),
(gfs.LAT_HIGH, gfs.LON_HIGH),
(gfs.LAT_LOW, gfs.LON_HIGH)
])
gmap1.plot(lat, lon,'blue', edge_width = 2.5) # Simulated Trajectory
gmap1.text(coord["lat"]-.2, coord["lon"]-.2, 'Simulated Trajectory with GFS Forecast', color='blue')
gmap1.polygon(*region, color='cornflowerblue', edge_width=5, alpha= .2) #plot region
elif forecast_type == "ERA5":
region= zip(*[
(gfs.LAT_LOW, gfs.LON_LOW),
(gfs.LAT_HIGH, gfs.LON_LOW),
(gfs.LAT_HIGH, gfs.LON_HIGH),
(gfs.LAT_LOW, gfs.LON_HIGH)
])
gmap1.plot(lat, lon,'red', edge_width = 2.5) # Simulated Trajectory
gmap1.text(coord["lat"]-.2, coord["lon"]-.2, 'Simulated Trajectory with ERA5 Reanalysis', color='red')
gmap1.polygon(*region, color='orange', edge_width=1, alpha= .15) #plot region
year = str(tm.localtime()[0])
month = str(tm.localtime()[1]).zfill(2)
day = str(tm.localtime()[2]).zfill(2)
if balloon_trajectory != None:
if forecast_type == "GFS":
gmap1.draw("trajectories/" + trajectory_name +"_GFS_" + str(t.year) + "_" + str(t.month) + "_" + str(start.day) + ".html" )
elif forecast_type == "ERA5":
gmap1.draw("trajectories/" + trajectory_name +"_ERA5_" + str(t.year) + "_" + str(t.month) + "_" + str(start.day) + ".html" )
else:
if forecast_type == "GFS":
gmap1.draw("trajectories/PREDICTION_GFS_" + str(t.year) + "_" + str(t.month) + "_" + str(start.day) + ".html" )
elif forecast_type == "ERA5":
gmap1.draw("trajectories/PREDICTION_ERA5_" + str(t.year) + "_" + str(t.month) + "_" + str(start.day) + ".html" )
executionTime = (tm.time() - scriptstartTime)
print('\nSimulation executed in ' + str(executionTime) + ' seconds.')
windmap = windmap.Windmap()
#windmap.plotWindVelocity(windmap.hour_index,windmap.LAT,windmap.LON)
windmap.plotWind2(windmap.hour_index,windmap.LAT,windmap.LON)
#windmap.plotWindOLD(windmap.hour_index,windmap.LAT,windmap.LON)
plt.show()