forked from sushmita-1110/Final_project_geos694
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.py
More file actions
234 lines (191 loc) · 6.53 KB
/
Copy pathfetch_data.py
File metadata and controls
234 lines (191 loc) · 6.53 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
import time
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
import pandas as pd
from obspy import UTCDateTime
from obspy.clients.fdsn import Client
# User input
CROSSING_FILE = Path("crossings_final50.txt")
OUTPUT_ROOT = Path("output/miniSEED")
OUTPUT_ROOT.mkdir(parents=True, exist_ok=True)
TIME_WINDOW = 120
MAX_WORKERS = 4
# Network configurations
NETWORK_CONFIGS = {
"AK": {
"channels": ["HNZ", "HHZ"],
"locations": ["", "00"],
"stations": [
"CDVT", "DAM1", "DAM2", "FA01", "FA02", "FA05", "FA06",
"FA07", "FA09", "FA10", "FA12", "FIRE", "GLI", "HIN",
"K203", "K204", "K205", "K208", "K209", "K210", "K211",
"K212", "K213", "K214", "K215", "K216", "K217", "K220",
"K221", "K222", "K223", "PWL",
],
},
"AV": {
"channels": ["HHZ", "HNZ"],
"locations": ["", "01", "02", "03", "04", "05", "06"],
"stations": ["DLL", "SDPI"],
},
"DE": {
"channels": ["HHZ", "HNZ"],
"locations": [""],
"stations": ["UAF01", "UAF02"],
},
"GM": {
"channels": ["HHZ", "HNZ"],
"locations": ["", "--", "00", "01", "02", "20"],
"stations": [
"AD02", "AD03", "AD04", "AD06", "AD07",
"AD08", "AD09", "AD11", "AD13", "AD14",
],
},
"IU": {
"channels": ["HHZ", "HNZ"],
"locations": ["00", "10", "20", "40"],
"stations": ["COLA"],
},
"NP": {
"channels": ["HNZ"],
"locations": [f"{i:02d}" for i in range(1, 33)] +
[f"D{i}" for i in range(0, 6)],
"stations": ["8040", "NIKO"],
},
"TA": {
"channels": ["HNZ"],
"locations": [""],
"stations": ["O19K", "P18K", "Q16K", "R17L"],
},
"XI": {
"channels": ["HHZ"],
"locations": [""],
"stations": ["APEX1", "APEX2", "APEX4", "APEX8", "APEX9"],
},
"XO": {
"channels": ["HHZ", "HNZ"],
"locations": [""],
"stations": [
"EP14", "EP15", "EP16", "EP21", "EP22", "EP23",
"ET17", "ET18", "ET19", "ET20",
"KD01", "KD02", "KD04", "KD05", "KD12",
"KS03", "KS11", "KS13",
"KT06", "KT07", "KT08", "KT09", "KT10",
"WP24", "WP25", "WP30",
"WS26", "WS27", "WS28",
],
},
"XV": {
"channels": ["HHZ"],
"locations": [""],
"stations": [
"F1TN", "F2TN", "F3TN", "F4TN",
"F5MN", "F6TP", "F7TV", "F8KN",
"FAPT", "FNN1", "FNN2", "FPAP", "FTGH",
],
},
}
# Functions
def safe_parse_time(value):
try:
return UTCDateTime(str(value).strip())
except Exception:
return None
def download_with_iris(client, network, station, location, channel, timestamp, outdir):
start = timestamp - TIME_WINDOW
end = timestamp + TIME_WINDOW
try:
st = client.get_waveforms(
network=network,
station=station,
location=location,
channel=channel,
starttime=start,
endtime=end,
attach_response=False,
)
except Exception:
return 0
if not st:
return 0
outdir.mkdir(parents=True, exist_ok=True)
loc_tag = location if location else "NONE"
time_tag = timestamp.strftime("%Y-%m-%dT%H-%M-%S")
outfile = outdir / f"{network}.{station}.{loc_tag}.{channel}_{time_tag}.mseed"
try:
st.write(str(outfile), format="MSEED")
return 1
except Exception:
return 0
def process_row(row, network, channels, locations):
client = Client("IRIS")
station = str(row["station"]).strip()
timestamp = row["time_str"]
row_channel = str(row["channel"]).strip()
row_location = str(row["location"]).strip()
equipment = str(row["equipment"]).strip()
time_str = timestamp.strftime("%Y-%m-%dT%H:%M:%S")
print(f"\n{network}.{station} at {time_str} equipment={equipment}")
channel_list = [row_channel] if row_channel else channels
location_list = [row_location] if row_location in locations else locations
saved = 0
for channel in channel_list:
found = False
for location in location_list:
loc_display = location if location else "None"
print(f" -> Trying loc={loc_display}, chan={channel}")
outdir = OUTPUT_ROOT / network / station
count = download_with_iris(
client,
network,
station,
location,
channel,
timestamp,
outdir,
)
if count:
print(f" Saved: {count} file(s) to {outdir}")
saved += count
found = True
break
if not found:
print(f" No data found for channel {channel}")
time.sleep(0.1)
return saved
def main():
df = pd.read_csv(CROSSING_FILE, sep="\t")
df.columns = df.columns.str.strip()
df["time_str"] = df["time_str"].apply(safe_parse_time)
df["network"] = df["network"].astype(str).str.strip()
df["station"] = df["station"].astype(str).str.strip()
df["location"] = df["location"].fillna("").astype(str).str.strip()
df["channel"] = df["channel"].fillna("").astype(str).str.strip()
df["equipment"] = df["equipment"].fillna("").astype(str).str.strip()
df = df.dropna(subset=["time_str", "station", "network"])
print(df.columns.tolist())
print(f"Loaded {len(df)} total timestamps.")
total_saved = 0
for network, config in NETWORK_CONFIGS.items():
print(f"Processing Network: {network}")
stations = config["stations"]
channels = config["channels"]
locations = config["locations"]
net_df = df[(df["network"] == network) & (df["station"].isin(stations))]
print(f"Found {len(net_df)} timestamps for this network.")
if net_df.empty:
continue
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
futures = [
executor.submit(process_row, row, network, channels, locations)
for _, row in net_df.iterrows()
]
for future in as_completed(futures):
try:
total_saved += future.result()
except Exception as e:
print(f"Worker failed: {e}")
print(f"TOTAL MiniSEED files saved: {total_saved}")
print(f"Output directory: {OUTPUT_ROOT}")
if __name__ == "__main__":
main()