hyperdd 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hyperdd/io/__init__.py ADDED
File without changes
hyperdd/io/readers.py ADDED
@@ -0,0 +1,312 @@
1
+ """Data reading functions for hypoDD input files.
2
+
3
+ Reads event catalogs, station lists, cross-correlation differential times,
4
+ and catalog differential times into numpy-based dictionaries.
5
+ """
6
+
7
+ import numpy as np
8
+
9
+
10
+ def read_events(filepath):
11
+ """Read a hypoDD event file.
12
+
13
+ Each line has 10 or 11 fields::
14
+
15
+ DATE TIME LAT LON DEP MAG HERR ZERR RES CUSP [FIX]
16
+
17
+ Parameters
18
+ ----------
19
+ filepath : str or path-like
20
+ Path to the event file (e.g. ``event.sel``).
21
+
22
+ Returns
23
+ -------
24
+ dict
25
+ Keys: ``cusp`` (int32), ``lat`` (float64), ``lon`` (float64),
26
+ ``dep`` (float32), ``mag`` (float32), ``herr`` (float32),
27
+ ``zerr`` (float32), ``res`` (float32), ``date`` (int32),
28
+ ``time`` (int32), ``fix`` (int32) -- all numpy arrays -- and
29
+ ``nev`` (int).
30
+ """
31
+ dates = []
32
+ times = []
33
+ lats = []
34
+ lons = []
35
+ deps = []
36
+ mags = []
37
+ herrs = []
38
+ zerrs = []
39
+ ress = []
40
+ cusps = []
41
+ fixes = []
42
+
43
+ with open(filepath) as f:
44
+ for line in f:
45
+ parts = line.split()
46
+ if not parts:
47
+ continue
48
+ nf = len(parts)
49
+ if nf < 10:
50
+ continue
51
+ dates.append(int(parts[0]))
52
+ times.append(int(parts[1]))
53
+ lats.append(float(parts[2]))
54
+ lons.append(float(parts[3]))
55
+ deps.append(float(parts[4]))
56
+ mags.append(float(parts[5]))
57
+ herrs.append(float(parts[6]))
58
+ zerrs.append(float(parts[7]))
59
+ ress.append(float(parts[8]))
60
+ cusps.append(int(parts[9]))
61
+ if nf >= 11:
62
+ fixes.append(int(parts[10]))
63
+ else:
64
+ fixes.append(0)
65
+
66
+ nev = len(cusps)
67
+
68
+ # Ensure 2-digit years get century prefix (same as Fortran)
69
+ date_arr = np.array(dates, dtype=np.int32)
70
+ date_arr[date_arr < 10000000] += 19000000
71
+
72
+ # Minimum depth enforcement (Fortran getdata.f:197)
73
+ dep_arr = np.array(deps, dtype=np.float32)
74
+ dep_arr[dep_arr < 0.01] = np.float32(0.01)
75
+
76
+ return {
77
+ 'date': date_arr,
78
+ 'time': np.array(times, dtype=np.int32),
79
+ 'lat': np.array(lats, dtype=np.float32), # Fortran: real ev_lat
80
+ 'lon': np.array(lons, dtype=np.float32), # Fortran: real ev_lon
81
+ 'dep': dep_arr,
82
+ 'mag': np.array(mags, dtype=np.float32),
83
+ 'herr': np.array(herrs, dtype=np.float32),
84
+ 'zerr': np.array(zerrs, dtype=np.float32),
85
+ 'res': np.array(ress, dtype=np.float32),
86
+ 'cusp': np.array(cusps, dtype=np.int32),
87
+ 'fix': np.array(fixes, dtype=np.int32),
88
+ 'nev': nev,
89
+ }
90
+
91
+
92
+ def read_stations(filepath, imod=0):
93
+ """Read a hypoDD station file.
94
+
95
+ Each line has 3 or 4 fields::
96
+
97
+ LABEL LAT LON [ELV]
98
+
99
+ Elevation is always divided by 1000 for 4-field lines (Fortran convention).
100
+ For imod != 5, negative elevations are set to 0.
101
+
102
+ Parameters
103
+ ----------
104
+ filepath : str or path-like
105
+ Path to the station file (e.g. ``station.sel``).
106
+ imod : int, optional
107
+ Velocity model type. For imod != 5, negative elevations are zeroed.
108
+
109
+ Returns
110
+ -------
111
+ dict
112
+ Keys: ``lab`` (list of str), ``lat`` (float32 array),
113
+ ``lon`` (float32 array), ``elv`` (float32 array, in km),
114
+ ``nsta`` (int).
115
+ """
116
+ labs = []
117
+ lats = []
118
+ lons = []
119
+ elvs = []
120
+
121
+ with open(filepath) as f:
122
+ for line in f:
123
+ parts = line.split()
124
+ if not parts:
125
+ continue
126
+ nf = len(parts)
127
+ if nf < 3:
128
+ continue
129
+ labs.append(parts[0][:7])
130
+ lats.append(float(parts[1]))
131
+ lons.append(float(parts[2]))
132
+ if nf >= 4:
133
+ elv = float(parts[3])
134
+ # Fortran always divides by 1000 for 4-field lines
135
+ elv /= 1000.0
136
+ # Fortran: for imod!=5, set negative elevations to 0
137
+ # (except sentinel value -999/1000 = -0.999)
138
+ if imod != 5 and elv < 0.0 and abs(elv - (-0.999)) > 0.001:
139
+ elv = 0.0
140
+ else:
141
+ elv = 0.0
142
+ elvs.append(elv)
143
+
144
+ nsta = len(labs)
145
+ return {
146
+ 'lab': labs,
147
+ 'lat': np.array(lats, dtype=np.float32),
148
+ 'lon': np.array(lons, dtype=np.float32),
149
+ 'elv': np.array(elvs, dtype=np.float32),
150
+ 'nsta': nsta,
151
+ }
152
+
153
+
154
+ def read_cc(filepath):
155
+ """Read a cross-correlation differential time file (``dt.cc``).
156
+
157
+ Header lines start with ``#``::
158
+
159
+ # ID1 ID2 OTC
160
+
161
+ Data lines::
162
+
163
+ STA DT QUAL PHA
164
+
165
+ The stored differential time is ``dt_value - otc`` for each pair.
166
+ Phase codes: ``P`` -> idx=1, ``S`` -> idx=2.
167
+
168
+ Parameters
169
+ ----------
170
+ filepath : str or path-like
171
+ Path to the cross-correlation dt file.
172
+
173
+ Returns
174
+ -------
175
+ dict
176
+ Keys: ``sta`` (list of str), ``dt`` (float32 array),
177
+ ``qual`` (float32 array), ``c1`` (int32 array), ``c2`` (int32 array),
178
+ ``idx`` (int32 array), ``ndt`` (int).
179
+ """
180
+ stas = []
181
+ dts = []
182
+ quals = []
183
+ c1s = []
184
+ c2s = []
185
+ idxs = []
186
+
187
+ ic1 = 0
188
+ ic2 = 0
189
+ otc = 0.0
190
+
191
+ with open(filepath) as f:
192
+ for line in f:
193
+ line = line.strip()
194
+ if not line:
195
+ continue
196
+ if line[0] == '#':
197
+ parts = line.split()
198
+ # # ID1 ID2 OTC
199
+ ic1 = int(parts[1])
200
+ ic2 = int(parts[2])
201
+ otc = float(parts[3]) if len(parts) >= 4 else 0.0
202
+ else:
203
+ parts = line.split()
204
+ if len(parts) < 4:
205
+ continue
206
+ sta = parts[0]
207
+ dt_val = float(parts[1]) - otc
208
+ qual = float(parts[2])
209
+ pha = parts[3].upper()
210
+ if pha == 'P':
211
+ idx = 1
212
+ elif pha == 'S':
213
+ idx = 2
214
+ else:
215
+ continue
216
+ stas.append(sta)
217
+ dts.append(dt_val)
218
+ quals.append(qual)
219
+ c1s.append(ic1)
220
+ c2s.append(ic2)
221
+ idxs.append(idx)
222
+
223
+ ndt = len(stas)
224
+ return {
225
+ 'sta': stas,
226
+ 'dt': np.array(dts, dtype=np.float32),
227
+ 'qual': np.array(quals, dtype=np.float32),
228
+ 'c1': np.array(c1s, dtype=np.int32),
229
+ 'c2': np.array(c2s, dtype=np.int32),
230
+ 'idx': np.array(idxs, dtype=np.int32),
231
+ 'ndt': ndt,
232
+ }
233
+
234
+
235
+ def read_ct(filepath):
236
+ """Read a catalog differential time file (``dt.ct``).
237
+
238
+ Header lines start with ``#``::
239
+
240
+ # ID1 ID2
241
+
242
+ Data lines::
243
+
244
+ STA T1 T2 QUAL PHA
245
+
246
+ The differential time is ``T1 - T2``.
247
+ Phase codes: ``P`` -> idx=3, ``S`` -> idx=4.
248
+
249
+ Parameters
250
+ ----------
251
+ filepath : str or path-like
252
+ Path to the catalog dt file.
253
+
254
+ Returns
255
+ -------
256
+ dict
257
+ Keys: ``sta`` (list of str), ``dt`` (float32 array),
258
+ ``qual`` (float32 array), ``c1`` (int32 array), ``c2`` (int32 array),
259
+ ``idx`` (int32 array), ``ndt`` (int).
260
+ """
261
+ stas = []
262
+ dts = []
263
+ quals = []
264
+ c1s = []
265
+ c2s = []
266
+ idxs = []
267
+
268
+ ic1 = 0
269
+ ic2 = 0
270
+
271
+ with open(filepath) as f:
272
+ for line in f:
273
+ line = line.strip()
274
+ if not line:
275
+ continue
276
+ if line[0] == '#':
277
+ parts = line.split()
278
+ # # ID1 ID2
279
+ ic1 = int(parts[1])
280
+ ic2 = int(parts[2])
281
+ else:
282
+ parts = line.split()
283
+ if len(parts) < 5:
284
+ continue
285
+ sta = parts[0]
286
+ t1 = float(parts[1])
287
+ t2 = float(parts[2])
288
+ qual = float(parts[3])
289
+ pha = parts[4].upper()
290
+ if pha == 'P':
291
+ idx = 3
292
+ elif pha == 'S':
293
+ idx = 4
294
+ else:
295
+ continue
296
+ stas.append(sta)
297
+ dts.append(t1 - t2)
298
+ quals.append(qual)
299
+ c1s.append(ic1)
300
+ c2s.append(ic2)
301
+ idxs.append(idx)
302
+
303
+ ndt = len(stas)
304
+ return {
305
+ 'sta': stas,
306
+ 'dt': np.array(dts, dtype=np.float32),
307
+ 'qual': np.array(quals, dtype=np.float32),
308
+ 'c1': np.array(c1s, dtype=np.int32),
309
+ 'c2': np.array(c2s, dtype=np.int32),
310
+ 'idx': np.array(idxs, dtype=np.int32),
311
+ 'ndt': ndt,
312
+ }
hyperdd/io/writers.py ADDED
@@ -0,0 +1,153 @@
1
+ """Output writing functions for hypoDD results.
2
+
3
+ Produces files in the same fixed-width format as the Fortran hypoDD code.
4
+ """
5
+
6
+ import numpy as np
7
+
8
+
9
+ def _decode_datetime(date_int, time_int):
10
+ """Extract year, month, day, hour, minute, second from packed integers.
11
+
12
+ Parameters
13
+ ----------
14
+ date_int : int
15
+ Date as YYYYMMDD.
16
+ time_int : int
17
+ Time as HHMMSSss (hours, minutes, seconds*100).
18
+
19
+ Returns
20
+ -------
21
+ yr, mo, dy, hr, mn, sc : int, int, int, int, int, float
22
+ """
23
+ yr = int(date_int) // 10000
24
+ mo = (int(date_int) % 10000) // 100
25
+ dy = int(date_int) % 100
26
+ hr = int(time_int) // 1000000
27
+ mn = (int(time_int) % 1000000) // 10000
28
+ sc = (float(time_int) % 10000.0) / 100.0
29
+ return yr, mo, dy, hr, mn, sc
30
+
31
+
32
+ def write_loc(filepath, events, cluster_id):
33
+ """Write initial event locations in hypoDD.loc format.
34
+
35
+ Matches the Fortran format::
36
+
37
+ (i9,1x,f10.6,1x,f11.6,1x,f9.3,1x,f10.1,1x,f10.1,
38
+ 1x,f10.1,1x,f8.1,1x,f8.1,1x,f8.1,1x,i4,1x,i2,1x,i2,
39
+ 1x,i2,1x,i2,1x,f5.2,1x,f5.2,1x,i3)
40
+
41
+ Parameters
42
+ ----------
43
+ filepath : str or path-like
44
+ Output file path.
45
+ events : dict
46
+ Dictionary with keys ``cusp``, ``lat``, ``lon``, ``dep``, ``x``,
47
+ ``y``, ``z``, ``herr``, ``zerr``, ``date``, ``time``, ``mag``,
48
+ and ``nev``. Coordinates ``x``, ``y``, ``z`` are in metres;
49
+ ``herr``, ``zerr`` are in km (will be written as metres).
50
+ cluster_id : int
51
+ Cluster identifier.
52
+ """
53
+ nev = events['nev']
54
+ with open(filepath, 'w') as f:
55
+ for i in range(nev):
56
+ yr, mo, dy, hr, mn, sc = _decode_datetime(
57
+ events['date'][i], events['time'][i])
58
+ # herr/zerr stored in km in the events dict; Fortran multiplies
59
+ # by 1000 to write in metres.
60
+ ex = events['herr'][i] * 1000.0
61
+ ey = events['herr'][i] * 1000.0
62
+ ez = events['zerr'][i] * 1000.0
63
+ line = (
64
+ f"{int(events['cusp'][i]):9d} "
65
+ f"{float(events['lat'][i]):10.6f} "
66
+ f"{float(events['lon'][i]):11.6f} "
67
+ f"{float(events['dep'][i]):9.3f} "
68
+ f"{float(events['x'][i]):10.1f} "
69
+ f"{float(events['y'][i]):10.1f} "
70
+ f"{float(events['z'][i]):10.1f} "
71
+ f"{ex:8.1f} "
72
+ f"{ey:8.1f} "
73
+ f"{ez:8.1f} "
74
+ f"{yr:4d} "
75
+ f"{mo:2d} "
76
+ f"{dy:2d} "
77
+ f"{hr:2d} "
78
+ f"{mn:2d} "
79
+ f"{sc:5.2f} "
80
+ f"{float(events['mag'][i]):5.2f} "
81
+ f"{cluster_id:3d}\n"
82
+ )
83
+ f.write(line)
84
+
85
+
86
+ def write_reloc(filepath, events, src_dx, src_dy, src_dz, src_dt,
87
+ src_ex, src_ey, src_ez, src_et,
88
+ ncc_p, ncc_s, nct_p, nct_s,
89
+ rms_cc, rms_ct, cluster_id):
90
+ """Write relocated events in hypoDD.reloc format.
91
+
92
+ Matches the Fortran format::
93
+
94
+ (i9,1x,f10.6,1x,f11.6,1x,f9.3,1x,f10.1,1x,f10.1,
95
+ 1x,f10.1,1x,f8.1,1x,f8.1,1x,f8.1,1x,i4,1x,i2,1x,i2,
96
+ 1x,i2,1x,i2,1x,f6.3,1x,f5.2,
97
+ 1x,i5,1x,i5,1x,i5,1x,i5,1x,f6.3,1x,f6.3,1x,i3)
98
+
99
+ Parameters
100
+ ----------
101
+ filepath : str or path-like
102
+ Output file path.
103
+ events : dict
104
+ Dictionary with keys ``cusp``, ``lat``, ``lon``, ``dep``, ``x``,
105
+ ``y``, ``z``, ``date``, ``time``, ``mag``, and ``nev``.
106
+ ``x``, ``y`` are in metres; ``z`` is the relocated depth offset in
107
+ metres (from the Fortran ``src_z``).
108
+ src_dx, src_dy, src_dz, src_dt : array-like
109
+ Source perturbations (unused for final output but kept for API
110
+ compatibility).
111
+ src_ex, src_ey, src_ez, src_et : array-like
112
+ Formal errors in metres (ex, ey, ez) and ms (et).
113
+ ncc_p, ncc_s : array-like of int
114
+ Number of CC P and S observations per event.
115
+ nct_p, nct_s : array-like of int
116
+ Number of catalog P and S observations per event.
117
+ rms_cc, rms_ct : array-like of float
118
+ RMS residual of CC and catalog data per event.
119
+ cluster_id : int
120
+ Cluster identifier.
121
+ """
122
+ nev = events['nev']
123
+ with open(filepath, 'w') as f:
124
+ for i in range(nev):
125
+ yr, mo, dy, hr, mn, sc = _decode_datetime(
126
+ events['date'][i], events['time'][i])
127
+ line = (
128
+ f"{int(events['cusp'][i]):9d} "
129
+ f"{float(events['lat'][i]):10.6f} "
130
+ f"{float(events['lon'][i]):11.6f} "
131
+ f"{float(events['dep'][i]):9.3f} "
132
+ f"{float(events['x'][i]):10.1f} "
133
+ f"{float(events['y'][i]):10.1f} "
134
+ f"{float(events['z'][i]):10.1f} "
135
+ f"{float(src_ex[i]):8.1f} "
136
+ f"{float(src_ey[i]):8.1f} "
137
+ f"{float(src_ez[i]):8.1f} "
138
+ f"{yr:4d} "
139
+ f"{mo:2d} "
140
+ f"{dy:2d} "
141
+ f"{hr:2d} "
142
+ f"{mn:2d} "
143
+ f"{sc:6.3f} "
144
+ f"{float(events['mag'][i]):5.2f} "
145
+ f"{int(ncc_p[i]):5d} "
146
+ f"{int(ncc_s[i]):5d} "
147
+ f"{int(nct_p[i]):5d} "
148
+ f"{int(nct_s[i]):5d} "
149
+ f"{float(rms_cc[i]):6.3f} "
150
+ f"{float(rms_ct[i]):6.3f} "
151
+ f"{cluster_id:3d}\n"
152
+ )
153
+ f.write(line)
File without changes