-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBLADE_main.py
More file actions
427 lines (363 loc) · 16.7 KB
/
Copy pathBLADE_main.py
File metadata and controls
427 lines (363 loc) · 16.7 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
BLADE Main Script (version 1.1)
This script implements the BLADE (Bolide Light-curve Analysis and Discrimination Explorer)
framework for automated processing and classification of digitized CNEOS fireball light curves.
Related publication:
"BLADE: An Automated Framework for Classifying Light Curves from the Center for
Near-Earth Object Studies (CNEOS) Fireball Database", The Astronomical Journal
doi: 10.3847/1538-3881/adeb55
License: MIT License
"""
import os
import re
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
from scipy.signal import savgol_filter, find_peaks
# User-adjustable parameters
METADATA_FILE = "LC_processing.csv"
LIGHT_CURVE_FOLDER = "Processed_LC"
OUTPUT_FOLDER = "Output_LC_BLADE"
PICKS_FOLDER = os.path.join(OUTPUT_FOLDER, "automated_picks")
PROGRESS_FILE = os.path.join(OUTPUT_FOLDER, "processing_progress.txt")
SUMMARY_FILE = os.path.join(OUTPUT_FOLDER, "summary.csv")
TIME_TOLERANCE = 5 # seconds for matching filenames to metadata
FRAG_THRESHOLD = 15 # sample-index threshold for continuous vs discrete fragmentation
PROMINENCE_THRESHOLD = 0.10 # in normalized-intensity units
HEIGHT_THRESHOLD = 0.10 # in normalized-intensity units
MIN_DISTANCE = 5 # minimum samples between peaks
# Enable or disable all plotting (True/False)
ENABLE_PLOTTING = True
# Ensure output directories exist
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
os.makedirs(PICKS_FOLDER, exist_ok=True)
# Load and clean metadata
try:
metadata_df = pd.read_csv(METADATA_FILE)
except Exception as e:
print(f"Error loading metadata file '{METADATA_FILE}': {e}")
metadata_df = None
REQUIRED_METADATA_COLS = [
"UTC Year", "UTC Month", "UTC Day",
"UTC Hour", "UTC Minute", "UTC Second",
"Velocity [km/s]", "Entry Angle [deg]", "Bolide Altitude [km]",
"Bolide Latitude [deg]", "Bolide Longitude [deg]", "Azimuth [deg]",
"Total impact energy [kt]"
]
if metadata_df is not None:
metadata_cleaned = metadata_df.dropna(subset=REQUIRED_METADATA_COLS, how="any").copy()
metadata_cleaned["Event_Time"] = pd.to_datetime(
metadata_cleaned["UTC Year"].astype(int).astype(str) + "-" +
metadata_cleaned["UTC Month"].astype(int).astype(str).str.zfill(2) + "-" +
metadata_cleaned["UTC Day"].astype(int).astype(str).str.zfill(2) + " " +
metadata_cleaned["UTC Hour"].astype(int).astype(str).str.zfill(2) + ":" +
metadata_cleaned["UTC Minute"].astype(int).astype(str).str.zfill(2) + ":" +
metadata_cleaned["UTC Second"].astype(int).astype(str).str.zfill(2)
)
else:
metadata_cleaned = None
def preprocess_light_curve(intensities: np.ndarray) -> tuple:
"""
Smooth the raw intensity series with an adaptive Savitzky-Golay filter,
then normalize to [0, 1].
"""
noise_level = np.std(np.diff(intensities, prepend=intensities[0]))
if noise_level > 1.0:
window_length = 15
polyorder = 2
elif noise_level > 0.5:
window_length = 21
polyorder = 3
else:
window_length = 31
polyorder = 3
window_length = min(window_length, len(intensities) - 1)
if window_length % 2 == 0:
window_length -= 1
if window_length < 3:
window_length = 3
try:
smoothed = savgol_filter(intensities, window_length=window_length, polyorder=polyorder)
except Exception:
smoothed = intensities.copy()
min_val = np.min(smoothed)
max_val = np.max(smoothed)
if max_val != min_val:
normalized = (smoothed - min_val) / (max_val - min_val)
else:
normalized = np.zeros_like(smoothed)
return smoothed, normalized
def calculate_altitude_vector(times: np.ndarray, peak_time: float,
peak_altitude_m: float, velocity_m_s: float,
entry_angle_deg: float) -> np.ndarray:
"""
Compute altitude (in meters) at each timestamp, assuming a linear vertical
velocity component:
alt(t) = peak_altitude_m - velocity_m_s * (t - peak_time) * sin(entry_angle)
"""
times_shifted = times - peak_time
theta = np.radians(entry_angle_deg)
altitudes_m = peak_altitude_m - velocity_m_s * times_shifted * np.sin(theta)
return altitudes_m
def detect_peaks(normalized_intensities: np.ndarray) -> tuple:
"""
Identify peaks using SciPy's find_peaks with configured thresholds.
"""
peaks, properties = find_peaks(
normalized_intensities,
prominence=PROMINENCE_THRESHOLD,
height=HEIGHT_THRESHOLD,
distance=MIN_DISTANCE
)
return peaks, properties
def classify_event(times: np.ndarray, normalized_intensities: np.ndarray,
peaks: np.ndarray, gradient: np.ndarray) -> str:
"""
Classification rules:
- No peaks: "No Significant Peaks"
- Multiple peaks:
if any adjacent-peak index difference < FRAG_THRESHOLD: "Continuous Fragmentation"
else: "Discrete Fragmentation"
- Single peak:
rise_time = t_peak - t_start
fall_time = t_end - t_peak
max_grad = max abs gradient
if (rise_time < 0.5 * fall_time) and (max_grad > 0.5): "Airburst"
else: "Single Peak"
"""
n_peaks = len(peaks)
if n_peaks == 0:
return "No Significant Peaks"
elif n_peaks > 1:
intervals = np.diff(peaks)
if np.min(intervals) < FRAG_THRESHOLD:
return "Continuous Fragmentation"
else:
return "Discrete Fragmentation"
else:
idx = peaks[0]
t_peak = times[idx]
rise_time = t_peak - times[0]
fall_time = times[-1] - t_peak
max_grad = np.max(np.abs(gradient))
if (rise_time < 0.5 * fall_time) and (max_grad > 0.5):
return "Airburst"
else:
return "Single Peak"
def process_light_curve(file_path: str, metadata: pd.DataFrame) -> tuple:
"""
Process a single digitized light-curve CSV, generating all required outputs.
"""
try:
file_name = os.path.basename(file_path)
match = re.match(r"^(\d{4})(\d{2})(\d{2})_(\d{6})\.csv$", file_name)
if not match:
raise ValueError(f"Invalid filename format: {file_name}")
year, month, day, hhmmss = match.groups()
event_time = datetime(
year=int(year), month=int(month), day=int(day),
hour=int(hhmmss[:2]), minute=int(hhmmss[2:4]), second=int(hhmmss[4:])
)
folder_name = f"{year}{month}{day}_{hhmmss}"
event_folder = os.path.join(OUTPUT_FOLDER, folder_name)
os.makedirs(event_folder, exist_ok=True)
# Match metadata row within TIME_TOLERANCE seconds
matched_row = None
if metadata is not None and not metadata.empty:
start_window = event_time - timedelta(seconds=TIME_TOLERANCE)
end_window = event_time + timedelta(seconds=TIME_TOLERANCE)
subset = metadata[
(metadata["Event_Time"] >= start_window) &
(metadata["Event_Time"] <= end_window)
]
if not subset.empty:
matched_row = subset.iloc[0]
# Read raw CSV
lc_df = pd.read_csv(file_path)
if "Time [s]" not in lc_df.columns or "Intensity [W/sr]" not in lc_df.columns:
raise KeyError("CSV missing 'Time [s]' or 'Intensity [W/sr]' columns.")
times = lc_df["Time [s]"].to_numpy(dtype=float)
intensities = lc_df["Intensity [W/sr]"].to_numpy(dtype=float)
# Plot raw Intensity vs Time
if ENABLE_PLOTTING:
plt.figure(figsize=(8, 6))
plt.plot(times, intensities, color="blue")
plt.xlabel("Time [s]", fontsize=14)
plt.ylabel("Intensity [W/sr]", fontsize=14)
plt.title(event_time.strftime("%Y-%m-%d %H:%M:%S UTC"), fontsize=16)
plt.grid(linestyle=":", linewidth=0.5)
raw_plot_path = os.path.join(event_folder, f"{folder_name}_raw_intensity.png")
plt.tight_layout()
plt.savefig(raw_plot_path, dpi=300)
plt.close()
# Smooth and normalize
smoothed, normalized = preprocess_light_curve(intensities)
# Plot normalized Intensity vs Time
if ENABLE_PLOTTING:
plt.figure(figsize=(8, 6))
plt.plot(times, normalized, color="blue")
plt.xlabel("Time [s]", fontsize=14)
plt.ylabel("Normalized Intensity", fontsize=14)
plt.title(event_time.strftime("%Y-%m-%d %H:%M:%S UTC"), fontsize=16)
plt.grid(linestyle=":", linewidth=0.5)
norm_plot_path = os.path.join(event_folder, f"{folder_name}_normalized_intensity.png")
plt.tight_layout()
plt.savefig(norm_plot_path, dpi=300)
plt.close()
# Compute gradient safely
with np.errstate(divide="ignore", invalid="ignore"):
gradient = np.gradient(normalized, times)
gradient = np.nan_to_num(gradient, nan=0.0, posinf=0.0, neginf=0.0)
# Detect peaks
peaks, properties = detect_peaks(normalized)
peak_times = times[peaks]
prominences = properties.get("prominences", np.array([0.0] * len(peaks)))
# Create 2x1 figure: top=normalized with picks; bottom=bar plot of prominence
if ENABLE_PLOTTING:
fig, axes = plt.subplots(2, 1, figsize=(10, 8), sharex=True)
# Top subplot: normalized curve + red dots at picks
axes[0].plot(times, normalized, color="blue", label="Normalized Intensity")
if peaks.size > 0:
axes[0].scatter(peak_times, normalized[peaks], color="red", s=50, label="Picks")
axes[0].set_ylabel("Normalized Intensity", fontsize=14)
axes[0].set_title(event_time.strftime("%Y-%m-%d %H:%M:%S UTC"), fontsize=16)
axes[0].grid(linestyle=":", linewidth=0.5)
axes[0].legend(loc="upper right")
# Bottom subplot: bar plot of prominence vs time (no title)
if peaks.size > 0:
full_width = times[-1] - times[0]
bar_width = full_width * 0.01 if full_width > 0 else 0.1
axes[1].bar(peak_times, prominences, width=bar_width, color="green", edgecolor="black")
axes[1].set_xlabel("Time [s]", fontsize=14)
axes[1].set_ylabel("Prominence", fontsize=14)
axes[1].grid(linestyle=":", linewidth=0.5)
multi_plot_path = os.path.join(event_folder, f"{folder_name}_lc_prominence.png")
plt.tight_layout()
plt.savefig(multi_plot_path, dpi=300)
plt.close()
# Overlay plot: gradient + normalized + picks (no prominence)
if ENABLE_PLOTTING:
plt.figure(figsize=(10, 6))
plt.plot(times, gradient, label="Gradient", color="green", linestyle="--")
plt.plot(times, normalized, label="Normalized Intensity", color="blue")
if peaks.size > 0:
plt.scatter(peak_times, normalized[peaks], color="red", s=50, label="Detected peaks")
plt.xlabel("Time [s]", fontsize=14)
plt.ylabel("Normalized / Gradient", fontsize=14)
plt.title(event_time.strftime("%Y-%m-%d %H:%M:%S UTC"), fontsize=16)
plt.grid(linestyle=":", linewidth=0.5)
plt.legend(loc="upper right", fontsize=12)
overlay_plot_path = os.path.join(event_folder, f"{folder_name}_auto_picks.png")
plt.tight_layout()
plt.savefig(overlay_plot_path, dpi=300)
plt.close()
# Classify the event
classification = classify_event(times, normalized, peaks, gradient)
# Compute altitudes if metadata is present
full_alt_km = None
peak_alt_km = [None] * len(peaks)
if matched_row is not None:
altitude_km = matched_row["Bolide Altitude [km]"]
velocity_km_s = matched_row["Velocity [km/s]"]
entry_angle_deg = matched_row["Entry Angle [deg]"]
if pd.notna(altitude_km) and pd.notna(velocity_km_s) and pd.notna(entry_angle_deg):
peak_alt_m = float(altitude_km) * 1000.0
velocity_m_s = float(velocity_km_s) * 1000.0
entry_angle = float(entry_angle_deg)
global_peak_idx = int(np.argmax(smoothed))
peak_time = times[global_peak_idx]
altitudes_m = calculate_altitude_vector(
times, peak_time, peak_alt_m, velocity_m_s, entry_angle
)
full_alt_km = altitudes_m / 1000.0
for i, p in enumerate(peaks):
peak_alt_km[i] = float(altitudes_m[p] / 1000.0)
# Plot Altitude vs Intensity (Y-X)
if ENABLE_PLOTTING:
plt.figure(figsize=(6, 10))
plt.plot(intensities, full_alt_km, color="blue")
plt.xlabel("Intensity [W/sr]", fontsize=14)
plt.ylabel("Altitude [km]", fontsize=14)
plt.title(event_time.strftime("%Y-%m-%d %H:%M:%S UTC"), fontsize=16)
plt.grid(linestyle=":", linewidth=0.5)
alt_yx_path = os.path.join(event_folder, f"{folder_name}_alt_vs_intensity_yx.png")
plt.tight_layout()
plt.savefig(alt_yx_path, dpi=300)
plt.close()
# Plot Intensity vs Altitude (X-Y inverted)
plt.figure(figsize=(8, 6))
plt.plot(full_alt_km, intensities, color="blue")
plt.gca().invert_xaxis()
plt.xlabel("Altitude [km]", fontsize=14)
plt.ylabel("Intensity [W/sr]", fontsize=14)
plt.title(event_time.strftime("%Y-%m-%d %H:%M:%S UTC"), fontsize=16)
plt.grid(linestyle=":", linewidth=0.5)
alt_xy_path = os.path.join(event_folder, f"{folder_name}_intensity_vs_alt_xy.png")
plt.tight_layout()
plt.savefig(alt_xy_path, dpi=300)
plt.close()
# Save picks CSV in both event folder and automated picks folder
picks_header = "Peak Time [s],Original Intensity,Normalized Intensity,Peak Altitude [km],Prominence\n"
picks_lines = []
for i, p in enumerate(peaks):
tpk = times[p]
orig = intensities[p]
norm = normalized[p]
alt = peak_alt_km[i] if peak_alt_km[i] is not None else ""
prom = prominences[i]
picks_lines.append(f"{tpk:.6f},{orig:.6f},{norm:.6f},{alt},{prom:.6f}\n")
# Write to event folder
event_picks_csv = os.path.join(event_folder, f"{folder_name}_picks.csv")
with open(event_picks_csv, "w") as fout:
fout.write(picks_header)
fout.writelines(picks_lines)
# Write to automated picks folder
auto_picks_csv = os.path.join(PICKS_FOLDER, f"{folder_name}_picks.csv")
with open(auto_picks_csv, "w") as fout:
fout.write(picks_header)
fout.writelines(picks_lines)
return True, file_name, classification
except Exception as e:
print(f"Error processing '{file_path}': {e}")
return False, os.path.basename(file_path), ""
# Load the list of already processed files
completed_files = set()
if os.path.exists(PROGRESS_FILE):
with open(PROGRESS_FILE, "r") as pf:
completed_files = set(line.strip() for line in pf if line.strip())
# Initialize summary CSV if not already present
if not os.path.exists(SUMMARY_FILE):
with open(SUMMARY_FILE, "w") as sf:
sf.write("File Name,Classification\n")
# Iterate over all light-curve CSV files
light_curve_files = [
os.path.join(LIGHT_CURVE_FOLDER, fn)
for fn in os.listdir(LIGHT_CURVE_FOLDER)
if fn.lower().endswith(".csv")
]
processed_count = 0
skipped_count = 0
for lc_path in sorted(light_curve_files):
lc_name = os.path.basename(lc_path)
if lc_name in completed_files:
print(f"[SKIP] Already processed: {lc_name}")
skipped_count += 1
continue
success, fname, classification = process_light_curve(lc_path, metadata_cleaned)
if success:
with open(PROGRESS_FILE, "a") as pf:
pf.write(fname + "\n")
completed_files.add(fname)
with open(SUMMARY_FILE, "a") as sf:
sf.write(f"{fname},{classification}\n")
print(f"[DONE] {fname} -> {classification}")
processed_count += 1
else:
print(f"[ERROR] Failed processing {fname}")
skipped_count += 1
print("\nProcessing complete.")
print(f" Files processed: {processed_count}")
print(f" Files skipped: {skipped_count}")