-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSN_data_update.py
726 lines (579 loc) · 23 KB
/
OSN_data_update.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import json
from collections import defaultdict
from datetime import datetime, timedelta
from math import asin, cos, radians, sin, sqrt
import numpy as np
import pandas as pd
import paramiko
import rasterio
import requests
import sshtunnel
from pyopensky.trino import Trino
from pyproj import Transformer
from sqlalchemy import create_engine, text, MetaData, insert
from sshtunnel import SSHTunnelForwarder
update_start_time = datetime.now()
LAT_MIN, LAT_MAX = 50.88385859501204322, 50.98427935836787128
LON_MIN, LON_MAX = 6.85029965503896943, 7.005 # 7.03641128126701965
ALT_MIN, ALT_MAX = (
0,
750,
) # update from 700 m to 750 m, in line with CTR limit at 2500 ft plus margin (and accounting for Geoid Height)
TIME_BETWEEN_TRAJS = 30
# SERA.5005(f)(1) criteria
CONGESTED_ALERT_DISTANCE_M = 600 # alert distance wrt obstacles (should be 600)
CONGESTED_ALERT_DELTA_HEIGHT_M = 300 # delta height (should be 300)
# SERA.5005(f)(2) criteria
NONCONGESTED_ALERT_DISTANCE_M = 150
NONCONGESTED_ALERT_DELTA_HEIGHT_M = 150
CPA_MARGIN_M = 20 # allowance for lateral distance to obstacle
DIP_MARGIN_M = 20 # allowance for dip below minimum height (45m corresponds to GVA = 2)
N_MIN = 5
GEOID_HEIGHT_M = 47 # geoid height for Cologne
sshtunnel.SSH_TIMEOUT = 15.0
sshtunnel.TUNNEL_TIMEOUT = 15.0
MYSQL_secrets_json = "./mysql_secrets.json"
PYA_secrets_json = "./PYA_secrets.json"
with open(MYSQL_secrets_json) as MYSQL_secrets:
MYSQL_creds = json.load(MYSQL_secrets)
with open(PYA_secrets_json) as PYA_secrets:
PYA_creds = json.load(PYA_secrets)
username = PYA_creds["PYA_username"]
token = PYA_creds["PYA_token"]
host = PYA_creds["PYA_host"]
domain_name = PYA_creds["PYA_domain"]
ed25519_key = paramiko.Ed25519Key(filename="./.ssh/id_ed25519")
def manifest_update(engine, table_name, processed_date, record_count, start_time, end_time, status, error_message=None):
try:
if isinstance(start_time, int):
start_time = datetime.fromtimestamp(start_time)
if isinstance(end_time, int):
end_time = datetime.fromtimestamp(end_time)
duration_sec = int((end_time - start_time).total_seconds())
metadata = MetaData()
metadata.reflect(bind=engine)
manifest_table = metadata.tables['manifest']
insert_stmt = insert(manifest_table).values(
table_name=table_name,
processed_date=processed_date,
record_count=record_count,
start_time=start_time,
end_time=end_time,
duration_sec=duration_sec,
status=status,
error_message=error_message
)
with engine.begin() as connection:
connection.execute(insert_stmt)
print(f"Manifest entry added for table: {table_name}")
except Exception as e:
print(f"Failed to log update to manifest table: {e}")
# Obtain and format the date to retrieve data for (2 days ago)
two_days_ago = datetime.now() - timedelta(days=2)
date_string = two_days_ago.strftime("%Y-%m-%d")
start = two_days_ago.replace(hour=0, minute=0, second=0, microsecond=0)
end = two_days_ago.replace(hour=23, minute=59, second=59, microsecond=999999)
start_time = int(start.timestamp())
start_hour = start_time - (start_time % 3600)
end_time = int(end.timestamp())
end_hour = end_time - (end_time % 3600)
# First query for State Vectors
svdata4_query = (
f"SELECT * FROM state_vectors_data4"
f" WHERE icao24 LIKE '%'"
f" AND time >= {start_time} AND time <= {end_time}"
f" AND hour >= {start_hour} AND hour <= {end_hour}"
f" AND lat >= {LAT_MIN} AND lat <= {LAT_MAX}"
f" AND lon>= {LON_MIN} AND lon <= {LON_MAX}"
f" AND geoaltitude >= {ALT_MIN} AND geoaltitude <= {ALT_MAX}"
f" ORDER BY time"
)
print("Connecting to OSN database...")
trino = Trino()
svdata4_df = trino.query(
svdata4_query,
cached=False,
compress=True,
)
# Save svdata4 pickle
svdata4_df.to_pickle(f"./OSN_pickles/svdata4df_new_{date_string}.pkl")
# Second Query for Ops Status
icao_list = svdata4_df.icao24.unique()
icao24_str = ", ".join(f"'{item}'" for item in icao_list)
ops_sts_query = (
f"SELECT icao24, mintime, maxtime, nacv, systemdesignassurance, version, positionnac, geometricverticalaccuracy, sourceintegritylevel, barometricaltitudeintegritycode FROM operational_status_data4"
f" WHERE icao24 IN ({icao24_str})"
f" AND mintime >= {start_time} AND maxtime <= {end_time}"
f" AND hour >= {start_hour} AND hour <= {end_hour}"
f" ORDER by mintime"
)
print("Connecting to OSN database...")
trino = Trino()
ops_sts_df = trino.query(
ops_sts_query,
cached=False,
)
ops_sts_df["time"] = ops_sts_df["mintime"].astype("int64")
# Save ops_sts pickle
ops_sts_df.to_pickle(f"./OSN_pickles/opsstsdf_new_{date_string}.pkl")
# Initialize an empty DataFrame to hold the results
merged_df = pd.DataFrame()
# Loop over each unique 'icao24' in both dataframes
unique_icao24s = pd.concat([svdata4_df["icao24"], ops_sts_df["icao24"]]).unique()
for icao24 in unique_icao24s:
# Filter each dataframe by 'icao24'
sub_df1 = svdata4_df[svdata4_df["icao24"] == icao24]
sub_df2 = ops_sts_df[ops_sts_df["icao24"] == icao24]
# Ensure both sub-dataframes are sorted by 'time'
sub_df1 = sub_df1.sort_values("time")
sub_df2 = sub_df2.sort_values("time")
sub_df1["icao24"] = sub_df1["icao24"].astype("object")
sub_df2["icao24"] = sub_df2["icao24"].astype("object")
sub_df1["time"] = sub_df1["time"].astype("int64")
sub_df2["time"] = sub_df2["time"].astype("int64")
# Perform merge_asof on the filtered and sorted dataframes
merged_sub_df = pd.merge_asof(
sub_df1, sub_df2, on="time", by="icao24", direction="backward"
)
# Append the result to the main dataframe
merged_df = pd.concat([merged_df, merged_sub_df], ignore_index=True)
# Third Query for Position data (to get the NIC)
posdata4_query = (
f"SELECT mintime, icao24, nic FROM position_data4"
f" WHERE icao24 IN ({icao24_str})"
f" AND lat >= {LAT_MIN} AND lat <= {LAT_MAX}"
f" AND lon>= {LON_MIN} AND lon <= {LON_MAX}"
f" AND mintime >= {start_time} AND maxtime <= {end_time}"
f" AND hour >= {start_hour} AND hour <= {end_hour}"
f" ORDER by mintime"
)
print("Connecting to OSN database...")
trino = Trino()
posdata4_df = trino.query(
posdata4_query,
cached=False,
)
posdata4_df["time"] = posdata4_df["mintime"].astype("int64")
# Save ops_sts pickle
posdata4_df.to_pickle(f"./OSN_pickles/posdata4df_new_{date_string}.pkl")
# Initialize an empty DataFrame to hold the results
final_df = pd.DataFrame()
for icao24 in unique_icao24s:
# Filter each dataframe by 'icao24'
sub_df1 = merged_df[merged_df["icao24"] == icao24]
sub_df2 = posdata4_df[posdata4_df["icao24"] == icao24]
# Ensure both sub-dataframes are sorted by 'time'
sub_df1 = sub_df1.sort_values("time")
sub_df2 = sub_df2.sort_values("time")
sub_df1["icao24"] = sub_df1["icao24"].astype("object")
sub_df2["icao24"] = sub_df2["icao24"].astype("object")
# Perform merge_asof on the filtered and sorted dataframes
merged_sub_df = pd.merge_asof(
sub_df1, sub_df2, on="time", by="icao24", direction="backward"
)
# Append the result to the main dataframe
final_df = pd.concat([final_df, merged_sub_df], ignore_index=True)
final_df = final_df.drop(columns=["hour", "mintime_x", "maxtime", "mintime_y"])
## Add DEM ground elevation information
crs_transformer = Transformer.from_crs(
4326, 25832, always_xy=True
) # Transformer from WGS-84 to ETRS89-LAEA (3035 for EU-DEM v1.1, 25832 for LAS DEM)
def transform_coords(lon, lat):
return crs_transformer.transform(lon, lat)
final_df["gnd_elev"] = np.nan
dem_src = rasterio.open("./resources/Cologne_DEM_merged_from_LAS_25x25.tif")
final_df["etrs89_x"], final_df["etrs89_y"] = zip(
*final_df.apply(lambda row: transform_coords(row["lon"], row["lat"]), axis=1)
)
def get_elevation(x, y, dem, default_value=None):
try:
row, col = dem.index(x, y)
return dem.read(1)[row, col]
except IndexError:
return default_value
final_df["gnd_elev"] = final_df.apply(
lambda row: get_elevation(
row["etrs89_x"], row["etrs89_y"], dem_src, row.get("gnd_elev", None)
),
axis=1,
)
missing_elev_count = final_df["gnd_elev"].isnull().sum()
print(f"Number of rows with None or NaN in 'gnd_elev': {missing_elev_count}")
final_df = final_df.dropna(subset=["gnd_elev"])
## Add population density
final_df["pop_density"] = np.nan
pop_src = rasterio.open(
"./resources/GHS_POP_E2020_GLOBE_R2023A_4326_3ss_V1_0_Cologne.tif"
)
def get_population(lon, lat, pop_src):
row, col = pop_src.index(lon, lat)
return pop_src.read(1)[row, col]
final_df["pop_density"] = final_df.apply(
lambda row: get_population(row["lon"], row["lat"], pop_src), axis=1
)
## Process df with distance information
def haversine(pt1, pt2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
Returned units are in metres. Differs slightly from PostGIS geography
distance, which uses a spheroid, rather than a sphere.
"""
lat1, lon1 = pt1[0], pt1[1]
lat2, lon2 = pt2[0], pt2[1]
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2.0) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2.0) ** 2
c = 2 * asin(sqrt(a))
r = 6371000 # Radius of earth in m
return c * r
final_df["prev_time"] = final_df.time.shift()
final_df["closest_obst_name"] = ""
final_df["inf_flt"] = False
final_df["inf_pt"] = False
final_df["gnd_inf_flt"] = False
final_df["gnd_inf_pt"] = False
final_df["min_hgt"] = np.nan
final_df["congested"] = final_df["pop_density"] > 2
map_time_traj = defaultdict(dict)
for icao, sfinal_df in final_df.groupby("icao24"):
map_time_traj[icao][sfinal_df.iloc[0]["time"]] = icao + "_1"
n_traj = 1
for i in range(1, sfinal_df.shape[0]):
time = sfinal_df.iloc[i]["time"]
diff = abs(int(time) - int(sfinal_df.iloc[i]["prev_time"]))
if diff > TIME_BETWEEN_TRAJS:
n_traj += 1
map_time_traj[icao][time] = icao + "_" + str(int(n_traj))
final_df["ref"] = (
final_df.apply(lambda x: map_time_traj[x.icao24][x.time], axis=1)
+ "_"
+ final_df.time.apply(lambda x: pd.to_datetime(x, unit="s").strftime("%d%m%y"))
)
# Add a distance column and compute cumulative along-track distance for each flight
final_df["dist"] = 0.0
for flight in final_df.ref.unique():
current = final_df[
final_df["ref"].isin([flight])
] # gets the trajectory of the current flight
previous_pt = None
previous_dist = 0
for row in current.itertuples():
current_pt = (float(row.lat), float(row.lon))
if previous_pt is not None: # Skip the distance calculation for the first point
delta_dist = haversine(previous_pt, current_pt)
final_df.loc[row[0], "dist"] = previous_dist + delta_dist
previous_pt = current_pt
previous_dist = final_df.loc[row[0], "dist"]
## Load obstacle information and check min height
path_to_obstacles_json = "./resources/LAC_obstacles_v1.csv"
obs_df = pd.read_csv(path_to_obstacles_json)
obs_df.rename(columns={"h": "height_m"}, inplace=True)
obs_df = obs_df.sort_values(
by=["height_m"]
) # sort obstacles by incresing height, to avoid that the min_hgt profil is wrong if a shorter obstacle comes after a taller one, in case the aircraft is within two obstacles clearance areas
# new. Test if this should not be done before adding the ref and distance info
final_df = final_df.reset_index(drop=True)
def update_closest_obstacle_xy(final_df, obstacles_df):
# Extract the coordinates and obstacle heights
final_coords = final_df[["etrs89_x", "etrs89_y"]].to_numpy()
obstacle_coords = obstacles_df[["x", "y"]].to_numpy()
obstacle_heights = obstacles_df["height_m"].to_numpy()
obstacle_names = obstacles_df["LAC_Name"].to_numpy()
obstacle_ground_elevs = obstacles_df["gnd_elev"].to_numpy()
# Iterate over each point in final_df and calculate distances
for i, (x_f, y_f) in enumerate(final_coords):
radius = (
CONGESTED_ALERT_DISTANCE_M
if final_df.at[i, "congested"]
else NONCONGESTED_ALERT_DISTANCE_M
)
# Convert radius to a squared radius for comparison with squared distances (faster than computing square roots)
squared_radius = radius**2
# Calculate squared Euclidean distances to all obstacles
distances_sq = (obstacle_coords[:, 0] - x_f) ** 2 + (
obstacle_coords[:, 1] - y_f
) ** 2
# Filter obstacles within the radius
within_radius = distances_sq <= squared_radius
# If there are obstacles within the radius, find the tallest one
if np.any(within_radius):
# Get the indices of obstacles within the radius
obstacles_in_radius_idx = np.where(within_radius)[0]
# Find the index of the tallest obstacle within the radius
tallest_idx = obstacles_in_radius_idx[
np.argmax(obstacle_heights[obstacles_in_radius_idx])
]
# Update final_df with the tallest obstacle details
tallest_obstacle_name = obstacle_names[tallest_idx]
tallest_obstacle_height = obstacle_heights[tallest_idx]
tallest_obstacle_elev = obstacle_ground_elevs[tallest_idx]
final_df.at[i, "closest_obst_name"] = tallest_obstacle_name
# Important: Calculate the minimum height, considerint the tallest obstacle in the alert radius
# min_hgt is referenced to the Geoid !
final_df.at[i, "min_hgt"] = (
GEOID_HEIGHT_M
+ np.float32(tallest_obstacle_elev)
+ np.float32(tallest_obstacle_height)
+ (
CONGESTED_ALERT_DELTA_HEIGHT_M
if final_df.at[i, "congested"]
else NONCONGESTED_ALERT_DELTA_HEIGHT_M
)
)
else:
final_df.at[i, "closest_obst_name"] = "ground"
final_df.at[i, "min_hgt"] = (
GEOID_HEIGHT_M
+ final_df.at[i, "gnd_elev"]
+ (
CONGESTED_ALERT_DELTA_HEIGHT_M
if final_df.at[i, "congested"]
else NONCONGESTED_ALERT_DELTA_HEIGHT_M
)
)
return final_df
final_df = update_closest_obstacle_xy(final_df, obs_df)
final_df["dip"] = final_df["min_hgt"] - final_df["geoaltitude"]
## Add infraction information
# Rule for 'inf_pt'
final_df["inf_pt"] = final_df.apply(
lambda row: True
if row["dip"] > 0 and row["closest_obst_name"] != "ground"
else False,
axis=1,
)
# Rule for 'gnd_inf_pt'
final_df["gnd_inf_pt"] = final_df.apply(
lambda row: True
if (row["dip"] > 0 and row["closest_obst_name"] == "ground")
else False,
axis=1,
)
# Group by 'ref' and update 'inf_flt' based on 'inf_pt'
final_df["inf_flt"] = final_df.groupby("ref")["inf_pt"].transform("any")
# Group by 'ref' and update 'gnd_inf_flt' based on 'gnd_inf_pt'
final_df["gnd_inf_flt"] = final_df.groupby("ref")["gnd_inf_pt"].transform("any")
final_df = final_df.drop(columns=["serials", "nacv"])
final_df[final_df["gnd_inf_pt"]].callsign.unique()
missing_callsign = final_df["callsign"].isnull().sum()
print(f"Number of rows with None or NaN in 'callsign': {missing_callsign}")
final_df = final_df.dropna(subset=["callsign"])
## Create infraction tables
inf_result = pd.DataFrame
gnd_inf_result = pd.DataFrame()
inf_pt_df = final_df[final_df.inf_pt].copy()
inf_pt_df["dist_to_obs"] = np.nan
if not inf_pt_df.empty:
inf_pt_df["dist_to_obs"] = inf_pt_df.apply(
lambda x: haversine(
(
obs_df.loc[obs_df["LAC_Name"] == x["closest_obst_name"], "lat"].iloc[0],
obs_df.loc[obs_df["LAC_Name"] == x["closest_obst_name"], "lon"].iloc[0],
),
(x["lat"], x["lon"]),
)
if not (x["closest_obst_name"] == "ground")
else np.nan,
axis=1,
)
inf_pt_df["time"] = pd.to_datetime(inf_pt_df["time"], unit="s")
inf_pt_df = inf_pt_df.sort_values(by=["ref", "closest_obst_name", "time"])
inf_pt_df["time_diff"] = inf_pt_df.groupby(["ref", "closest_obst_name"])["time"].diff()
inf_pt_df["group"] = (inf_pt_df["time_diff"] >= pd.Timedelta(seconds=30)).cumsum()
inf_grouped = inf_pt_df.groupby(["ref", "closest_obst_name", "group"])
inf_min_dist = inf_grouped.apply(
lambda x: x.loc[x["dist_to_obs"].idxmin()]
).reset_index(drop=True)
inf_max_dip = inf_grouped["dip"].max().reset_index()
group_size = inf_grouped.size().reset_index(name="n")
inf_result = inf_min_dist[
[
"icao24",
"callsign",
"group",
"ref",
"closest_obst_name",
"time",
"lat",
"lon",
"dist_to_obs",
"congested",
"pop_density",
"systemdesignassurance",
"version",
"positionnac",
"sourceintegritylevel",
"nic",
]
].copy()
inf_result = inf_result.merge(inf_max_dip, on=["ref", "closest_obst_name", "group"])
inf_result = inf_result.merge(group_size, on=["ref", "closest_obst_name", "group"])
inf_result.rename(columns={"dist_to_obs": "cpa", "dip": "dip_max"}, inplace=True)
inf_result["entry_count"] = inf_result.groupby("ref").cumcount()
inf_result["inf_ref"] = (
inf_result["ref"].astype(str) + "_" + inf_result["entry_count"].astype(str)
)
if not inf_result.empty:
inf_result["url"] = inf_result.apply(
lambda row: "https://globe.adsbexchange.com/?icao=%s&lat=50.928&lon=6.947&zoom=13.2&showTrace=%s×tamp=%s"
% (
row["icao24"],
row["time"].strftime("%Y-%m-%d"),
str(int(row["time"].timestamp())),
),
axis=1,
)
else:
inf_result["url"] = []
inf_result = inf_result.reset_index(drop=True)
inf_result = inf_result.drop(columns=["entry_count", "group"])
# Ground infractions
gnd_inf_pt_df = final_df[
final_df.gnd_inf_pt & (final_df.dip >= 0) & (final_df.closest_obst_name == "ground")
].copy()
gnd_inf_pt_df["time"] = pd.to_datetime(gnd_inf_pt_df["time"], unit="s")
gnd_inf_pt_df = gnd_inf_pt_df.sort_values(by=["ref", "time"])
gnd_inf_pt_df["time_diff"] = gnd_inf_pt_df.groupby(["ref"])["time"].diff()
gnd_inf_pt_df["group"] = (
gnd_inf_pt_df["time_diff"] >= pd.Timedelta(seconds=30)
).cumsum()
gnd_inf_grouped = gnd_inf_pt_df.groupby(["ref", "group"])
gnd_inf_max_dip = gnd_inf_grouped.apply(lambda x: x.loc[x["dip"].idxmax()]).reset_index(
drop=True
)
group_size = gnd_inf_grouped.size().reset_index(name="n")
gnd_inf_result = gnd_inf_max_dip[
[
"icao24",
"callsign",
"group",
"ref",
"closest_obst_name",
"time",
"lat",
"lon",
"dip",
"congested",
"pop_density",
"systemdesignassurance",
"version",
"positionnac",
"sourceintegritylevel",
"nic",
]
].copy()
gnd_inf_result = gnd_inf_result.merge(group_size, on=["ref", "group"])
gnd_inf_result.rename(columns={"dip": "dip_max"}, inplace=True)
gnd_inf_result["entry_count"] = gnd_inf_result.groupby("ref").cumcount()
gnd_inf_result["inf_ref"] = (
gnd_inf_result["ref"].astype(str)
+ "_"
+ "gnd_"
+ gnd_inf_result["entry_count"].astype(str)
)
if not gnd_inf_result.empty:
gnd_inf_result["url"] = gnd_inf_result.apply(
lambda row: "https://globe.adsbexchange.com/?icao=%s&lat=50.928&lon=6.947&zoom=13.2&showTrace=%s×tamp=%s"
% (
row["icao24"],
row["time"].strftime("%Y-%m-%d"),
str(int(row["time"].timestamp())),
),
axis=1,
)
else:
gnd_inf_result["url"] = []
gnd_inf_result = gnd_inf_result.reset_index(drop=True)
gnd_inf_result = gnd_inf_result.drop(columns=["entry_count", "group"])
###
gdf_record_count = len(final_df)
inf_record_count = len(inf_result)
gndinf_record_count = len(gnd_inf_result)
processed_date = two_days_ago.date()
### Upload data to the MySQL server
with SSHTunnelForwarder(
(MYSQL_creds["SSH_ADDRESS"], 22),
ssh_username=MYSQL_creds["SSH_USERNAME"],
ssh_pkey=ed25519_key, # Use the loaded RSA key
remote_bind_address=(
MYSQL_creds["REMOTE_BIND_ADDRESS"],
MYSQL_creds["REMOTE_BIND_PORT"],
),
allow_agent=False,
) as tunnel:
engstr = (
"mysql+pymysql://"
+ MYSQL_creds["SSH_USERNAME"]
+ ":"
+ MYSQL_creds["PYANYWHERE_PASSWORD"]
+ "@127.0.0.1:"
+ str(tunnel.local_bind_port)
+ "/dme3$"
+ MYSQL_creds["PROD_DATABASE_NAME"]
)
engine = create_engine(engstr)
query = text(
"SELECT max(processed_date) as max_date FROM manifest"
)
result = pd.read_sql_query(query, con=engine)
max_date = result["max_date"].iloc[0]
if max_date < two_days_ago.date():
# Set up the SSH tunnel with the RSA key
with SSHTunnelForwarder(
(MYSQL_creds["SSH_ADDRESS"], 22),
ssh_username=MYSQL_creds["SSH_USERNAME"],
ssh_pkey=ed25519_key,
remote_bind_address=(
MYSQL_creds["REMOTE_BIND_ADDRESS"],
MYSQL_creds["REMOTE_BIND_PORT"],
),
allow_agent=False,
) as tunnel:
print("connected")
engstr = (
"mysql+pymysql://"
+ MYSQL_creds["SSH_USERNAME"]
+ ":"
+ MYSQL_creds["PYANYWHERE_PASSWORD"]
+ "@127.0.0.1:"
+ str(tunnel.local_bind_port)
+ "/dme3$"
+ MYSQL_creds["PROD_DATABASE_NAME"]
)
engine = create_engine(engstr)
print("step 1")
final_df.to_sql(
con=engine, name=MYSQL_creds["MAIN_PROD_TABLE_NAME"], if_exists="append"
)
manifest_update(engine, MYSQL_creds["MAIN_PROD_TABLE_NAME"], processed_date, gdf_record_count, update_start_time, datetime.now(), 'SUCCESS', None)
print("step 2")
inf_result.to_sql(
con=engine, name=MYSQL_creds["INF_PROD_TABLE_NAME"], if_exists="append"
)
manifest_update(engine, MYSQL_creds["INF_PROD_TABLE_NAME"], processed_date, inf_record_count, update_start_time, datetime.now(), 'SUCCESS', None)
print("step 3")
gnd_inf_result.to_sql(
con=engine, name=MYSQL_creds["GNDINF_PROD_TABLE_NAME"], if_exists="append"
)
manifest_update(engine, MYSQL_creds["GNDINF_PROD_TABLE_NAME"], processed_date, gndinf_record_count, update_start_time, datetime.now(), 'SUCCESS', None)
print("Insertion in database done")
else:
print('Date already in database, exiting...')
exit()
### Reload Web app
response = requests.post(
"https://{host}/api/v0/user/{username}/webapps/{domain_name}/reload/".format(
host=host, username=username, domain_name=domain_name
),
headers={"Authorization": "Token {token}".format(token=token)},
)
if response.status_code == 200:
print("Web app reloaded successfully.")
print(response.content)
else:
print(f"Error: Received status code {response.status_code}")
print("Response content:", response.content)