-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS1_burst_search.py
394 lines (324 loc) · 11.9 KB
/
S1_burst_search.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
#!/usr/bin/env python3
# -----------------------: Imports
import argparse
import logging
import os
from datetime import datetime
import asf_search
import contextily as ctx
import geopandas as gpd
import matplotlib.pyplot as plt
import pandas as pd
import xyzservices.providers as xyz
import yaml
# -----------------------: Logging Setup
# Set up basic logging to both console and file with a timestamped log filename.
log_filename = f"S1_burst_search_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
# Remove existing handlers if rerunning in an interactive environment (e.g., Jupyter)
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
# logging.FileHandler(log_filename), # Log to a file
logging.StreamHandler(), # Log to the console
],
)
logger = logging.getLogger(__name__)
# -----------------------: Configuration Loading and Validation Functions
def load_config(config_path: str) -> dict:
"""
Load configuration from a YAML file.
Args:
config_path (str): Path to the YAML configuration file.
Returns:
dict: Configuration dictionary loaded from the file.
Raises:
FileNotFoundError: If the configuration file does not exist.
"""
if not os.path.exists(config_path):
raise FileNotFoundError(f"Config file '{config_path}' not found.")
with open(config_path, "r") as f:
config = yaml.safe_load(f)
return config
def check_required_config(config: dict, required_keys: dict):
"""
Ensure all required sections and keys exist in the configuration.
Args:
config (dict): The configuration dictionary.
required_keys (dict): Dictionary where keys are section names and values are lists of required keys.
Raises:
ValueError: If a required section or key is missing.
"""
for section, keys in required_keys.items():
if section not in config:
raise ValueError(
f"Missing required section: '{section}' in the config file."
)
for key in keys:
if key not in config[section]:
raise ValueError(
f"Missing required key '{key}' in section '{section}'."
)
def check_required_group_fields(group: dict, group_index: int):
"""
Ensure each burst_id_group contains the required keys.
Args:
group (dict): A burst group dictionary.
group_index (int): Index of the group in the list.
Raises:
ValueError: If a required field or subfield is missing.
"""
required = ["iw", "prefix", "range"]
for key in required:
if key not in group:
raise ValueError(
f"Missing required field '{key}' in burst_id_groups at index {group_index}."
)
for subkey in ["start", "end"]:
if subkey not in group["range"]:
raise ValueError(
f"Missing required field 'range.{subkey}' in burst_id_groups at index {group_index}."
)
# -----------------------: Burst ID Generation Function
def generate_burst_ids_from_groups(relative_orbit, burst_groups: list) -> list:
"""
Generates a complete burst ID list from the burst groups.
The full burst ID is constructed as:
{relative_orbit}_{prefix}{num}_IW{iw}
The relative orbit is zero-padded to three digits.
Args:
relative_orbit: Relative orbit number.
burst_groups (list): List of burst group dictionaries.
Returns:
list: List of generated burst IDs.
"""
# Convert relative_orbit to a zero-padded string.
relative_orbit_str = str(relative_orbit).zfill(3)
burst_ids = []
for i, group in enumerate(burst_groups):
check_required_group_fields(group, i)
iw = str(group["iw"])
prefix = str(group["prefix"])
start = int(group["range"]["start"])
end = int(group["range"]["end"])
for num in range(start, end + 1):
burst_ids.append(f"{relative_orbit_str}_{prefix}{num}_IW{iw}")
return burst_ids
# -----------------------: Burst Search Function
def search_bursts(
burst_id_list: list,
start_date: str,
end_date: str,
beam_mode: str,
relative_orbit,
flight_direction: str,
polarization: str,
) -> gpd.GeoDataFrame:
"""
For each full burst ID in burst_id_list, perform an ASF search and concatenate the results.
Args:
burst_id_list (list): List of full burst IDs.
start_date (str): Start date in ISO format.
end_date (str): End date in ISO format.
beam_mode (str): Beam mode.
relative_orbit: Relative orbit number.
flight_direction (str): Flight direction.
polarization (str): Polarization.
Returns:
gpd.GeoDataFrame: GeoDataFrame containing concatenated search results.
"""
all_bursts = gpd.GeoDataFrame()
for full_burst_id in burst_id_list:
logger.info(f"Searching for burst with fullBurstID: {full_burst_id}")
search_result = asf_search.search(
start=datetime.fromisoformat(start_date),
end=datetime.fromisoformat(end_date),
beamMode=beam_mode,
relativeOrbit=relative_orbit,
flightDirection=flight_direction,
polarization=polarization,
fullBurstID=full_burst_id,
)
# Convert the search result to a GeoDataFrame (assuming EPSG:4326)
single_burst_geodata = gpd.GeoDataFrame.from_features(
search_result.geojson(), crs=4326
)
all_bursts = pd.concat([all_bursts, single_burst_geodata], ignore_index=True)
return all_bursts
# -----------------------: Scene Coverage Export Function
def export_scene_coverage(
gdf,
fig_savename="burst_coverage.png",
column=None,
alpha=0.3,
add_basemap=True,
user_epsg=None,
figsize=(8.3, 11.7),
):
"""
Exports a plot of the scene coverage from the provided GeoDataFrame.
Args:
gdf (GeoDataFrame): GeoDataFrame containing scene data.
fig_savename (str): Filename for the saved figure.
column: Column used for plotting.
alpha (float): Transparency level.
add_basemap (bool): Whether to add a basemap.
user_epsg: User specified EPSG code.
figsize (tuple): Figure size.
"""
if gdf.empty:
print("Warning: The GeoDataFrame is empty. Nothing to plot.")
return
fig, ax = plt.subplots(figsize=figsize)
# Determine which CRS to use
original_crs = gdf.crs
if user_epsg:
gdf = gdf.to_crs(epsg=user_epsg)
elif add_basemap and (original_crs is None or original_crs.to_epsg() != 3857):
gdf = gdf.to_crs(epsg=3857)
# Plot the GeoDataFrame
gdf.plot(column=column, ax=ax, alpha=alpha, edgecolor="black")
# Add basemap if requested
if add_basemap:
try:
ctx.add_basemap(
ax, crs=gdf.crs, source=ctx.providers.OpenStreetMap.Mapnik, alpha=0.75
)
except AttributeError:
ctx.add_basemap(
ax, crs=gdf.crs, source=ctx.providers.CartoDB.Positron, alpha=0.75
)
# Format plot
ax.set_axis_off()
plt.savefig(
fig_savename,
dpi=300,
transparent=False,
facecolor="w",
edgecolor="w",
bbox_inches="tight",
)
logger.info(f"Figure saved at: {fig_savename}")
plt.close()
# -----------------------: Search Output Summary Function
def summarize_search_output(input_df):
"""
Summarizes the search output by calculating statistics of acquisition dates.
Args:
input_df: DataFrame containing search output.
"""
# Ensure 'stopTime' is in datetime format (in case it is not)
input_df["stopTime"] = pd.to_datetime(input_df["stopTime"], errors="coerce")
# Extract the unique dates by converting 'stopTime' to the date part only
unique_acquisition_dates = input_df["stopTime"].dt.date.unique()
# Calculate time intervals (in days) between the unique acquisition dates
time_intervals_in_days = pd.Series(
pd.to_datetime(unique_acquisition_dates).diff().dropna()
).dt.days
# Calculate the statistics
date_count = len(unique_acquisition_dates)
max_time_interval = time_intervals_in_days.max()
min_time_interval = time_intervals_in_days.min()
logger.info(f"Number of acquisitions: {date_count}")
logger.info(f"First acquisition: {unique_acquisition_dates[0]}")
logger.info(f"Last acquisition: {unique_acquisition_dates[-1]}")
logger.info(f"Max time interval: {max_time_interval} days")
logger.info(f"Min time interval: {min_time_interval} days")
# -----------------------: Main Function
def main():
# -----------------------: Argument Parsing
parser = argparse.ArgumentParser(
description="Search for Sentinel-1 bursts using parameters from a YAML configuration file."
)
parser.add_argument(
"--config",
type=str,
default="config.yaml",
help="Path to the YAML configuration file",
)
args = parser.parse_args()
# -----------------------: Load Configuration
config = load_config(args.config)
# Check that all required parameters are present.
# Define required sections and keys
required_keys = {
"SEARCH": [
"relative_orbit",
"start_date",
"end_date",
"beam_mode",
"flight_direction",
"polarization",
"burst_id_groups",
],
}
check_required_config(config, required_keys)
# -----------------------: Extract SEARCH Parameters
search_params = config["SEARCH"]
relative_orbit = search_params["relative_orbit"]
start_date = search_params["start_date"]
end_date = search_params["end_date"]
beam_mode = search_params["beam_mode"]
flight_direction = search_params["flight_direction"]
polarization = search_params["polarization"]
burst_groups = search_params["burst_id_groups"]
# Validate burst groups
for index, group in enumerate(burst_groups):
check_required_group_fields(group, index)
# -----------------------: Generate Burst IDs
burst_id_list = generate_burst_ids_from_groups(relative_orbit, burst_groups)
logger.info(f"Generated burst ID list: {burst_id_list}")
# -----------------------: Search Bursts
all_bursts = search_bursts(
burst_id_list,
start_date,
end_date,
beam_mode,
relative_orbit,
flight_direction,
polarization,
)
# -----------------------: Process and Save Search Output
output_burst_df = all_bursts[
[
"centerLat",
"centerLon",
"stopTime",
"fileID",
"flightDirection",
"pathNumber",
"processingLevel",
"startTime",
"sceneName",
"platform",
"orbit",
"polarization",
"sensor",
"groupID",
"pgeVersion",
"beamModeType",
"burst",
]
]
output_burst_df = output_burst_df.sort_values(by="stopTime")
output_savename = f"{flight_direction}_{str(relative_orbit).zfill(3)}_{beam_mode}_{polarization}_{start_date}_{end_date}.xlsx"
output_burst_df.to_excel(output_savename, index=False)
logger.info(f"Burst search complete. File saved at {output_savename}")
# -----------------------: Plot Scene Coverage
acquisition_dates = list(
set(([ele.split("T")[0] for ele in all_bursts["startTime"]]))
)
select_bursts2plot = all_bursts[
all_bursts["startTime"].str.contains(acquisition_dates[0], na=False)
]
export_scene_coverage(
select_bursts2plot,
column="fileID",
fig_savename=output_savename.replace(".xlsx", ".png"),
)
# -----------------------: Summarize Search Output
summarize_search_output(output_burst_df)
if __name__ == "__main__":
main()