hyperdd 0.1__0-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.
@@ -0,0 +1,103 @@
1
+ import numpy as np
2
+
3
+
4
+ def weighting(ndt, mbad_cusps, idata, maxres_cross, maxres_net, maxdcc, maxdct,
5
+ minwght, wt_ccp, wt_ccs, wt_ctp, wt_cts,
6
+ dt_c1, dt_c2, dt_idx, dt_qual, dt_res, dt_offs, dt_wt):
7
+ """Apply a priori weights and re-weighting (vectorized).
8
+ Modifies dt_wt in place. Returns ineg: 1 if negative weights exist."""
9
+
10
+ idx = dt_idx[:ndt]
11
+ qual = dt_qual[:ndt]
12
+
13
+ # A priori weights (vectorized)
14
+ wt_map = np.array([0.0, wt_ccp, wt_ccs, wt_ctp, wt_cts], dtype=np.float64)
15
+ idx_clipped = np.clip(idx, 0, 4)
16
+ dt_wt[:ndt] = wt_map[idx_clipped] * qual
17
+
18
+ # Zero out bad cusps
19
+ if mbad_cusps:
20
+ c1 = dt_c1[:ndt]
21
+ c2 = dt_c2[:ndt]
22
+ bad = np.array(list(mbad_cusps), dtype=c1.dtype)
23
+ bad_mask = np.isin(c1, bad) | np.isin(c2, bad)
24
+ dt_wt[:ndt][bad_mask] = 0.0
25
+
26
+ ineg = 0
27
+ if np.any(dt_wt[:ndt] == 0.0) and mbad_cusps:
28
+ ineg = 1
29
+
30
+ # Re-weighting
31
+ need_reweight = (
32
+ ((idata == 1 or idata == 3) and (maxres_cross != -999 or maxdcc != -999)) or
33
+ ((idata == 2 or idata == 3) and (maxres_net != -999 or maxdct != -999))
34
+ )
35
+
36
+ if not need_reweight:
37
+ return ineg
38
+
39
+ # Compute median and MAD
40
+ maxres_cc = maxres_cross
41
+ maxres_ct = maxres_net
42
+
43
+ cc_mask = idx <= 2
44
+ ct_mask = idx >= 3
45
+ res = dt_res[:ndt]
46
+
47
+ if idata == 3:
48
+ if maxres_cross >= 1 and np.any(cc_mask):
49
+ cc_res = res[cc_mask]
50
+ med_cc = np.median(cc_res)
51
+ mad_cc = np.median(np.abs(cc_res - med_cc)) / 0.67449
52
+ maxres_cc = mad_cc * maxres_cross
53
+
54
+ if maxres_net >= 1 and np.any(ct_mask):
55
+ ct_res = res[ct_mask]
56
+ med_ct = np.median(ct_res)
57
+ mad_ct = np.median(np.abs(ct_res - med_ct)) / 0.67449
58
+ maxres_ct = mad_ct * maxres_net
59
+
60
+ elif (idata == 1 and maxres_cross >= 1) or (idata == 2 and maxres_net >= 1):
61
+ all_res = res.copy()
62
+ med = np.median(all_res)
63
+ mad = np.median(np.abs(all_res - med)) / 0.67449
64
+ if idata == 1:
65
+ maxres_cc = mad * maxres_cross
66
+ else:
67
+ maxres_ct = mad * maxres_net
68
+
69
+ # Apply distance and residual reweighting (vectorized)
70
+ wt = dt_wt[:ndt]
71
+ offs = dt_offs[:ndt]
72
+ abs_res = np.abs(res)
73
+
74
+ # CC data
75
+ if maxdcc != -999 and np.any(cc_mask):
76
+ ratio = offs[cc_mask] / (maxdcc * 1000)
77
+ np.clip(ratio, 0, 1, out=ratio)
78
+ wt[cc_mask] *= (1 - ratio**5)**5
79
+
80
+ if maxres_cross > 0 and np.any(cc_mask):
81
+ active = cc_mask & (wt > 0.000001)
82
+ if np.any(active):
83
+ ratio = abs_res[active] / maxres_cc
84
+ np.clip(ratio, 0, 1, out=ratio)
85
+ wt[active] *= (1 - ratio**3)**3
86
+
87
+ # CT data
88
+ if maxdct != -999 and np.any(ct_mask):
89
+ ratio = offs[ct_mask] / (maxdct * 1000)
90
+ np.clip(ratio, 0, 1, out=ratio)
91
+ wt[ct_mask] *= (1 - ratio**3)**3
92
+
93
+ if maxres_net > 0 and np.any(ct_mask):
94
+ active = ct_mask & (wt > 0.000001)
95
+ if np.any(active):
96
+ ratio = abs_res[active] / maxres_ct
97
+ np.clip(ratio, 0, 1, out=ratio)
98
+ wt[active] *= (1 - ratio**3)**3
99
+
100
+ # Check for negative weights
101
+ ineg = 1 if np.any(wt < minwght) else 0
102
+
103
+ return ineg
File without changes
@@ -0,0 +1,286 @@
1
+ """Short distance conversion routines.
2
+
3
+ Exact reimplementation of the Fortran subroutines setorg.f, dist.f, redist.f,
4
+ and delaz2.f from hypoDD. All constants and formulas match the original Fortran
5
+ code, including the WGS72 ellipsoid parameters and integer division behavior.
6
+
7
+ The SDC class encapsulates the common block /GEO_COORSYSTEM/ from geocoord.inc.
8
+ """
9
+
10
+ import math
11
+
12
+ import numpy as np
13
+
14
+
15
+ class SDC:
16
+ """Short Distance Conversion on the WGS72 ellipsoid.
17
+
18
+ Reimplements setorg.f (constructor), dist.f (latlon_to_xy), and
19
+ redist.f (xy_to_latlon). The unrotated coordinate system has positive
20
+ x-axis toward WEST and positive y-axis toward NORTH. Positive z-axis
21
+ points toward Earth's center.
22
+
23
+ Parameters
24
+ ----------
25
+ orlat : float
26
+ Origin latitude in degrees (north positive).
27
+ orlon : float
28
+ Origin longitude in degrees (west positive, matching Fortran convention).
29
+ rotate : float, optional
30
+ Anticlockwise rotation of the coordinate system in degrees. Default 0.
31
+
32
+ Notes
33
+ -----
34
+ The Fortran common block variables (geocoord.inc) are stored as instance
35
+ attributes with the same names and matching precision:
36
+ - double precision: rearth, ellip, rlatc, rad
37
+ - real (float32 arithmetic): olat, olon, aa, bb, bc, sint, cost, rotate
38
+ """
39
+
40
+ def __init__(self, orlat: float, orlon: float, rotate: float = 0.0):
41
+ # --- setorg.f ---
42
+
43
+ # rad constant (double precision in Fortran)
44
+ self.rad = np.float64(0.017453292)
45
+
46
+ # Handle the Swiss coordinate system special case
47
+ if orlat == 0.0 and orlon == 0.0:
48
+ self.olat = np.float32(46.95240)
49
+ self.olon = np.float32(-7.439583)
50
+ self._rotate = np.float32(0.0)
51
+ else:
52
+ self.olat = np.float32(orlat)
53
+ self.olon = np.float32(orlon)
54
+ self._rotate = np.float32(rotate * self.rad)
55
+
56
+ # Convert to minutes (real/float32 in Fortran)
57
+ self.olat = np.float32(self.olat * np.float32(60.0))
58
+ self.olon = np.float32(self.olon * np.float32(60.0))
59
+
60
+ # WGS72 ellipsoid constants
61
+ self.rearth = np.float64(6378.135)
62
+ self.ellip = np.float64(298.26)
63
+
64
+ # RLATC: conversion factor from geographic to geocentric latitude
65
+ # PHI = OLAT*RAD/60.0 (phi = geographic latitude in radians)
66
+ # BETA = PHI - sin(2*PHI)/ELLIP (beta = geocentric latitude)
67
+ # RLATC = tan(BETA)/tan(PHI)
68
+ PHI = np.float64(self.olat) * self.rad / np.float64(60.0)
69
+ BETA = PHI - math.sin(PHI * 2.0) / self.ellip
70
+ self.rlatc = np.float64(math.tan(BETA) / math.tan(PHI))
71
+
72
+ # Calculate aa and bb: length of one minute of lat and lon in km
73
+ lat1 = np.float64(math.atan(self.rlatc * math.tan(
74
+ np.float64(self.olat) * self.rad / np.float64(60.0))))
75
+ lat2 = np.float64(math.atan(self.rlatc * math.tan(
76
+ np.float64(self.olat + np.float32(1.0)) * self.rad / np.float64(60.0))))
77
+ dela = lat2 - lat1
78
+ r = self.rearth * (np.float64(1.0) - math.sin(lat1) ** 2 / self.ellip)
79
+ self.aa = np.float32(r * dela)
80
+ delb = np.float64(math.acos(
81
+ math.sin(lat1) ** 2 + math.cos(self.rad / np.float64(60.0)) * math.cos(lat1) ** 2))
82
+ self.bc = np.float32(r * delb)
83
+ self.bb = np.float32(r * delb / math.cos(lat1))
84
+
85
+ # Rotation cosines (real/float32 in Fortran)
86
+ self.sint = np.float32(math.sin(float(self._rotate)))
87
+ self.cost = np.float32(math.cos(float(self._rotate)))
88
+
89
+ def latlon_to_xy(self, xlat: float, xlon: float):
90
+ """Convert latitude/longitude (degrees) to local Cartesian km.
91
+
92
+ Reimplements dist.f. Input lat/lon are double precision; output xkm/ykm
93
+ are single precision (float32), matching the Fortran declaration.
94
+
95
+ Parameters
96
+ ----------
97
+ xlat : float
98
+ Latitude in degrees.
99
+ xlon : float
100
+ Longitude in degrees.
101
+
102
+ Returns
103
+ -------
104
+ xkm : numpy.float32
105
+ Easting in km (positive toward west in unrotated system).
106
+ ykm : numpy.float32
107
+ Northing in km (positive toward north).
108
+ """
109
+ # --- dist.f ---
110
+ # q = 60*xlat - olat (real in Fortran: mixed double*real arithmetic)
111
+ xlat = np.float64(xlat)
112
+ xlon = np.float64(xlon)
113
+ q = np.float32(np.float32(60.0) * xlat - self.olat)
114
+ yp = np.float32(q + self.olat)
115
+
116
+ # Geocentric latitude conversions (double precision)
117
+ lat1 = np.float64(math.atan(
118
+ float(self.rlatc) * math.tan(float(self.rad) * float(yp) / 60.0)))
119
+ lat2 = np.float64(math.atan(
120
+ float(self.rlatc) * math.tan(float(self.rad) * float(self.olat) / 60.0)))
121
+ lat3 = (lat2 + lat1) / np.float64(2.0)
122
+
123
+ xx = np.float32(np.float32(60.0) * xlon - self.olon)
124
+ q = np.float32(q * self.aa)
125
+ xx = np.float32(xx * self.bb * np.float32(math.cos(lat3)))
126
+
127
+ if self._rotate != np.float32(0.0):
128
+ # Rotate coordinate system anticlockwise
129
+ yp = np.float32(self.cost * q + self.sint * xx)
130
+ xx = np.float32(self.cost * xx - self.sint * q)
131
+ q = yp
132
+
133
+ xkm = np.float32(xx)
134
+ ykm = np.float32(q)
135
+ return xkm, ykm
136
+
137
+ def xy_to_latlon(self, xkm: float, ykm: float):
138
+ """Convert local Cartesian km back to latitude/longitude (degrees).
139
+
140
+ Reimplements redist.f. Input xkm/ykm are single precision; output
141
+ lat/lon are double precision, matching the Fortran declaration.
142
+
143
+ Parameters
144
+ ----------
145
+ xkm : float
146
+ Easting in km.
147
+ ykm : float
148
+ Northing in km.
149
+
150
+ Returns
151
+ -------
152
+ xlat : numpy.float64
153
+ Latitude in degrees.
154
+ xlon : numpy.float64
155
+ Longitude in degrees.
156
+
157
+ Raises
158
+ ------
159
+ ZeroDivisionError
160
+ If aa or bb*cos(lat) is too small (matching Fortran stop condition).
161
+ """
162
+ # --- redist.f ---
163
+ xx = np.float32(xkm)
164
+ yy = np.float32(ykm)
165
+
166
+ # Rotate coordinates anticlockwise back
167
+ y = np.float32(yy * self.cost - xx * self.sint)
168
+ x = np.float32(yy * self.sint + xx * self.cost)
169
+
170
+ if abs(float(self.aa)) < 0.0000001:
171
+ raise ZeroDivisionError(
172
+ f"redist: aa={self.aa}, bb={self.bb} -- division by zero")
173
+
174
+ q = np.float32(y / self.aa)
175
+
176
+ # lat = int((q+olat)/60.) -- Fortran integer truncation
177
+ lat = int(np.float32(q + self.olat) / np.float32(60.0))
178
+ xlat_min = np.float32(q + self.olat - np.float32(60.0) * lat)
179
+ yp = np.float32(np.float32(60.0) * lat + xlat_min)
180
+
181
+ # Geocentric latitude conversions (double precision)
182
+ lat1 = np.float64(math.atan(
183
+ float(self.rlatc) * math.tan(float(yp) * float(self.rad) / 60.0)))
184
+ lat2 = np.float64(math.atan(
185
+ float(self.rlatc) * math.tan(float(self.olat) * float(self.rad) / 60.0)))
186
+ lat3 = (lat1 + lat2) / np.float64(2.0)
187
+ clat1 = np.float64(math.cos(lat3))
188
+
189
+ bcl = np.float32(self.bb * np.float32(clat1))
190
+ if abs(float(bcl)) < 0.000001:
191
+ raise ZeroDivisionError(
192
+ f"redist: aa={self.aa}, bb={self.bb}, cos(lat1)={clat1} "
193
+ "-- division by zero")
194
+
195
+ p = np.float32(x / np.float32(self.bb * np.float32(clat1)))
196
+
197
+ # lon = int((p+olon)/60.)
198
+ lon = int(np.float32(p + self.olon) / np.float32(60.0))
199
+ xlon_min = np.float32(p + self.olon - np.float32(60.0) * lon)
200
+
201
+ xlat = np.float64(lat) + np.float64(xlat_min) / np.float64(60.0)
202
+ xlon = np.float64(lon) + np.float64(xlon_min) / np.float64(60.0)
203
+
204
+ return xlat, xlon
205
+
206
+
207
+ def delaz(alat: float, alon: float, blat: float, blon: float):
208
+ """Compute distance and azimuth on a sphere.
209
+
210
+ Exact reimplementation of delaz2.f. Uses the WGS72-derived flattening
211
+ constant (flat=0.993231) for geocentric latitude conversion, and the
212
+ mean equatorial radius 6378.140 km (Bott, 1982).
213
+
214
+ IMPORTANT: The Fortran expression ``(1/3)`` uses integer division and
215
+ evaluates to 0, so the radius formula becomes:
216
+ radius = 6378.140 * (1.0 + 3.37853e-3 * (0 - cos(colat)**2))
217
+ = 6378.140 * (1.0 - 3.37853e-3 * cos(colat)**2)
218
+
219
+ Parameters
220
+ ----------
221
+ alat : float
222
+ Latitude of first point (degrees).
223
+ alon : float
224
+ Longitude of first point (degrees).
225
+ blat : float
226
+ Latitude of second point (degrees).
227
+ blon : float
228
+ Longitude of second point (degrees).
229
+
230
+ Returns
231
+ -------
232
+ del_deg : float
233
+ Central angle in degrees.
234
+ dist_km : float
235
+ Distance in km.
236
+ az_deg : float
237
+ Azimuth from point a to point b in degrees (0-360).
238
+ """
239
+ # --- delaz2.f ---
240
+ pi2 = np.float64(1.570796)
241
+ rad = np.float64(0.01745329)
242
+ flat = np.float64(0.993231)
243
+
244
+ # Convert to radians
245
+ alatr = np.float64(alat) * rad
246
+ alonr = np.float64(alon) * rad
247
+ blatr = np.float64(blat) * rad
248
+ blonr = np.float64(blon) * rad
249
+
250
+ # Convert latitudes to geocentric colatitudes
251
+ tana = flat * math.tan(alatr)
252
+ geoa = math.atan(tana)
253
+ acol = pi2 - geoa
254
+
255
+ tanb = flat * math.tan(blatr)
256
+ geob = math.atan(tanb)
257
+ bcol = pi2 - geob
258
+
259
+ # Calculate delta (central angle)
260
+ diflon = blonr - alonr
261
+ cosdel = (math.sin(acol) * math.sin(bcol) * math.cos(diflon)
262
+ + math.cos(acol) * math.cos(bcol))
263
+ # Clamp to [-1, 1] to guard against floating point issues in acos
264
+ cosdel = max(-1.0, min(1.0, float(cosdel)))
265
+ delr = math.acos(cosdel)
266
+
267
+ # Calculate azimuth from a to b
268
+ top = math.sin(diflon)
269
+ den = math.sin(acol) / math.tan(bcol) - math.cos(diflon) * math.cos(acol)
270
+ azr = math.atan2(top, den)
271
+
272
+ # Convert to degrees
273
+ del_deg = float(delr / rad)
274
+ az_deg = float(azr / rad)
275
+ if az_deg < 0.0:
276
+ az_deg = 360.0 + az_deg
277
+
278
+ # Compute distance in km
279
+ # NOTE: (1/3) is Fortran integer division = 0
280
+ colat = pi2 - (alatr + blatr) / np.float64(2.0)
281
+ radius = np.float64(6378.140) * (
282
+ np.float64(1.0) + np.float64(3.37853e-3) * (
283
+ np.float64(0) - math.cos(colat) ** 2))
284
+ dist_km = float(delr * radius)
285
+
286
+ return del_deg, dist_km, az_deg
@@ -0,0 +1,65 @@
1
+ """Vectorized distance and azimuth computation.
2
+
3
+ Batch version of delaz2.f operating on arrays of coordinates.
4
+ """
5
+
6
+ import numpy as np
7
+
8
+
9
+ def delaz_vec(alat, alon, blat, blon):
10
+ """Compute distance and azimuth for arrays of point pairs.
11
+
12
+ Parameters
13
+ ----------
14
+ alat, alon : array-like, shape (na,)
15
+ Latitudes and longitudes of source points (degrees).
16
+ blat, blon : array-like, shape (nb,)
17
+ Latitudes and longitudes of station points (degrees).
18
+
19
+ Returns
20
+ -------
21
+ dist : ndarray, shape (nb, na)
22
+ Distance in km. dist[i, j] = distance from source j to station i.
23
+ az : ndarray, shape (nb, na)
24
+ Azimuth in degrees (0-360) from source j to station i.
25
+ """
26
+ rad = 0.01745329
27
+ pi2 = 1.570796
28
+ flat = 0.993231
29
+
30
+ alat = np.asarray(alat, dtype=np.float64)
31
+ alon = np.asarray(alon, dtype=np.float64)
32
+ blat = np.asarray(blat, dtype=np.float64)
33
+ blon = np.asarray(blon, dtype=np.float64)
34
+
35
+ # Source coords (shape na) → add axis for broadcasting
36
+ alatr = alat[np.newaxis, :] * rad # (1, na)
37
+ alonr = alon[np.newaxis, :] * rad
38
+ # Station coords (shape nb)
39
+ blatr = blat[:, np.newaxis] * rad # (nb, 1)
40
+ blonr = blon[:, np.newaxis] * rad
41
+
42
+ # Geocentric colatitudes
43
+ acol = pi2 - np.arctan(flat * np.tan(alatr)) # (1, na)
44
+ bcol = pi2 - np.arctan(flat * np.tan(blatr)) # (nb, 1)
45
+
46
+ # Central angle
47
+ diflon = blonr - alonr # (nb, na)
48
+ cosdel = (np.sin(acol) * np.sin(bcol) * np.cos(diflon)
49
+ + np.cos(acol) * np.cos(bcol))
50
+ cosdel = np.clip(cosdel, -1.0, 1.0)
51
+ delr = np.arccos(cosdel)
52
+
53
+ # Azimuth
54
+ top = np.sin(diflon)
55
+ den = np.sin(acol) / np.tan(bcol) - np.cos(diflon) * np.cos(acol)
56
+ azr = np.arctan2(top, den)
57
+ az = azr / rad
58
+ az = np.where(az < 0.0, 360.0 + az, az)
59
+
60
+ # Distance in km
61
+ colat = pi2 - (alatr + blatr) / 2.0
62
+ radius = 6378.140 * (1.0 - 3.37853e-3 * np.cos(colat) ** 2)
63
+ dist = delr * radius
64
+
65
+ return dist, az