earth-refraction 0.1.0__tar.gz

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.
Files changed (100) hide show
  1. earth_refraction-0.1.0/CMakeLists.txt +15 -0
  2. earth_refraction-0.1.0/LICENSE +21 -0
  3. earth_refraction-0.1.0/PKG-INFO +317 -0
  4. earth_refraction-0.1.0/README.md +289 -0
  5. earth_refraction-0.1.0/cpp/CMakeLists.txt +13 -0
  6. earth_refraction-0.1.0/cpp/include/refraction/atmosphere/nrlmsis21.hpp +1684 -0
  7. earth_refraction-0.1.0/cpp/include/refraction/atmosphere/nrlmsis21_parm.hpp +5192 -0
  8. earth_refraction-0.1.0/cpp/include/refraction/ciddor.hpp +123 -0
  9. earth_refraction-0.1.0/cpp/include/refraction/constants.hpp +22 -0
  10. earth_refraction-0.1.0/cpp/include/refraction/geodetic.hpp +185 -0
  11. earth_refraction-0.1.0/cpp/include/refraction/interpolation.hpp +28 -0
  12. earth_refraction-0.1.0/cpp/include/refraction/itu_r_p453.hpp +37 -0
  13. earth_refraction-0.1.0/cpp/include/refraction/ocean/chen_millero.hpp +166 -0
  14. earth_refraction-0.1.0/cpp/include/refraction/ocean/del_grosso.hpp +99 -0
  15. earth_refraction-0.1.0/cpp/include/refraction/ocean/iapws_r9_97.hpp +74 -0
  16. earth_refraction-0.1.0/cpp/include/refraction/ocean/mackenzie.hpp +59 -0
  17. earth_refraction-0.1.0/cpp/include/refraction/ocean/meissner_wentz.hpp +206 -0
  18. earth_refraction-0.1.0/cpp/include/refraction/ocean/millard_seaver.hpp +96 -0
  19. earth_refraction-0.1.0/cpp/include/refraction/ocean/ocean_profile.hpp +207 -0
  20. earth_refraction-0.1.0/cpp/include/refraction/ocean/ocean_refraction.hpp +105 -0
  21. earth_refraction-0.1.0/cpp/include/refraction/ocean/quan_fry.hpp +35 -0
  22. earth_refraction-0.1.0/cpp/include/refraction/ocean/sound_speed.hpp +107 -0
  23. earth_refraction-0.1.0/cpp/include/refraction/profile.hpp +243 -0
  24. earth_refraction-0.1.0/cpp/include/refraction/ray_trace.hpp +691 -0
  25. earth_refraction-0.1.0/cpp/include/refraction/ray_trace_batch.hpp +218 -0
  26. earth_refraction-0.1.0/cpp/include/refraction/refraction.hpp +10 -0
  27. earth_refraction-0.1.0/cpp/include/refraction/species.hpp +136 -0
  28. earth_refraction-0.1.0/cpp/include/refraction/surface_anchor.hpp +204 -0
  29. earth_refraction-0.1.0/cpp/include/refraction/water_vapor.hpp +73 -0
  30. earth_refraction-0.1.0/cpp/tests/CMakeLists.txt +26 -0
  31. earth_refraction-0.1.0/cpp/tests/dump_reference.cpp +161 -0
  32. earth_refraction-0.1.0/cpp/tests/test_chen_millero.cpp +103 -0
  33. earth_refraction-0.1.0/cpp/tests/test_ciddor.cpp +134 -0
  34. earth_refraction-0.1.0/cpp/tests/test_del_grosso.cpp +94 -0
  35. earth_refraction-0.1.0/cpp/tests/test_fixtures.hpp +23 -0
  36. earth_refraction-0.1.0/cpp/tests/test_geodetic.cpp +177 -0
  37. earth_refraction-0.1.0/cpp/tests/test_itu.cpp +53 -0
  38. earth_refraction-0.1.0/cpp/tests/test_mackenzie.cpp +77 -0
  39. earth_refraction-0.1.0/cpp/tests/test_meissner_wentz.cpp +150 -0
  40. earth_refraction-0.1.0/cpp/tests/test_millard_seaver.cpp +107 -0
  41. earth_refraction-0.1.0/cpp/tests/test_nrlmsis.cpp +140 -0
  42. earth_refraction-0.1.0/cpp/tests/test_ocean_profile.cpp +92 -0
  43. earth_refraction-0.1.0/cpp/tests/test_ocean_refraction.cpp +150 -0
  44. earth_refraction-0.1.0/cpp/tests/test_profile.cpp +195 -0
  45. earth_refraction-0.1.0/cpp/tests/test_ray_trace.cpp +258 -0
  46. earth_refraction-0.1.0/cpp/tests/test_sound_speed.cpp +173 -0
  47. earth_refraction-0.1.0/cpp/tests/test_species.cpp +114 -0
  48. earth_refraction-0.1.0/cpp/tests/test_surface_anchor.cpp +246 -0
  49. earth_refraction-0.1.0/cpp/tests/test_utils.hpp +185 -0
  50. earth_refraction-0.1.0/cpp/tests/test_water_vapor.cpp +93 -0
  51. earth_refraction-0.1.0/pyproject.toml +54 -0
  52. earth_refraction-0.1.0/python/nanobind/CMakeLists.txt +11 -0
  53. earth_refraction-0.1.0/python/nanobind/refraction_native.cpp +227 -0
  54. earth_refraction-0.1.0/python/src/refraction/__init__.py +108 -0
  55. earth_refraction-0.1.0/python/src/refraction/atmosphere/__init__.py +25 -0
  56. earth_refraction-0.1.0/python/src/refraction/atmosphere/_nrlmsis21_parm.py +5162 -0
  57. earth_refraction-0.1.0/python/src/refraction/atmosphere/nrlmsis21.py +1757 -0
  58. earth_refraction-0.1.0/python/src/refraction/ciddor.py +353 -0
  59. earth_refraction-0.1.0/python/src/refraction/constants.py +13 -0
  60. earth_refraction-0.1.0/python/src/refraction/geodetic.py +215 -0
  61. earth_refraction-0.1.0/python/src/refraction/itu_r_p453.py +100 -0
  62. earth_refraction-0.1.0/python/src/refraction/native.py +285 -0
  63. earth_refraction-0.1.0/python/src/refraction/ocean/__init__.py +60 -0
  64. earth_refraction-0.1.0/python/src/refraction/ocean/chen_millero.py +199 -0
  65. earth_refraction-0.1.0/python/src/refraction/ocean/del_grosso.py +124 -0
  66. earth_refraction-0.1.0/python/src/refraction/ocean/iapws_r9_97.py +163 -0
  67. earth_refraction-0.1.0/python/src/refraction/ocean/mackenzie.py +77 -0
  68. earth_refraction-0.1.0/python/src/refraction/ocean/meissner_wentz.py +585 -0
  69. earth_refraction-0.1.0/python/src/refraction/ocean/millard_seaver.py +227 -0
  70. earth_refraction-0.1.0/python/src/refraction/ocean/ocean_profile.py +490 -0
  71. earth_refraction-0.1.0/python/src/refraction/ocean/ocean_refraction.py +198 -0
  72. earth_refraction-0.1.0/python/src/refraction/ocean/quan_fry.py +79 -0
  73. earth_refraction-0.1.0/python/src/refraction/ocean/sound_speed.py +165 -0
  74. earth_refraction-0.1.0/python/src/refraction/profile.py +595 -0
  75. earth_refraction-0.1.0/python/src/refraction/ray_trace.py +963 -0
  76. earth_refraction-0.1.0/python/src/refraction/species.py +314 -0
  77. earth_refraction-0.1.0/python/src/refraction/surface_anchor.py +395 -0
  78. earth_refraction-0.1.0/python/src/refraction/water_vapor.py +233 -0
  79. earth_refraction-0.1.0/python/tests/conftest.py +56 -0
  80. earth_refraction-0.1.0/python/tests/test_chen_millero.py +114 -0
  81. earth_refraction-0.1.0/python/tests/test_ciddor.py +114 -0
  82. earth_refraction-0.1.0/python/tests/test_del_grosso.py +106 -0
  83. earth_refraction-0.1.0/python/tests/test_geodetic.py +246 -0
  84. earth_refraction-0.1.0/python/tests/test_iapws_r9_97.py +85 -0
  85. earth_refraction-0.1.0/python/tests/test_itu.py +36 -0
  86. earth_refraction-0.1.0/python/tests/test_mackenzie.py +94 -0
  87. earth_refraction-0.1.0/python/tests/test_meissner_wentz.py +245 -0
  88. earth_refraction-0.1.0/python/tests/test_millard_seaver.py +146 -0
  89. earth_refraction-0.1.0/python/tests/test_native.py +320 -0
  90. earth_refraction-0.1.0/python/tests/test_nrlmsis.py +146 -0
  91. earth_refraction-0.1.0/python/tests/test_ocean_profile.py +110 -0
  92. earth_refraction-0.1.0/python/tests/test_ocean_refraction.py +154 -0
  93. earth_refraction-0.1.0/python/tests/test_profile.py +388 -0
  94. earth_refraction-0.1.0/python/tests/test_quan_fry.py +38 -0
  95. earth_refraction-0.1.0/python/tests/test_ray_trace.py +837 -0
  96. earth_refraction-0.1.0/python/tests/test_sound_speed.py +143 -0
  97. earth_refraction-0.1.0/python/tests/test_species.py +120 -0
  98. earth_refraction-0.1.0/python/tests/test_surface_anchor.py +258 -0
  99. earth_refraction-0.1.0/python/tests/test_version.py +9 -0
  100. earth_refraction-0.1.0/python/tests/test_water_vapor.py +121 -0
@@ -0,0 +1,15 @@
1
+ cmake_minimum_required(VERSION 3.20)
2
+ project(refraction LANGUAGES CXX)
3
+
4
+ set(CMAKE_CXX_STANDARD 20)
5
+ set(CMAKE_CXX_STANDARD_REQUIRED ON)
6
+
7
+ find_package(Python 3.10 COMPONENTS Interpreter Development.Module REQUIRED)
8
+ find_package(nanobind CONFIG REQUIRED)
9
+
10
+ nanobind_add_module(refraction_native
11
+ python/nanobind/refraction_native.cpp
12
+ )
13
+ target_include_directories(refraction_native PRIVATE cpp/include)
14
+
15
+ install(TARGETS refraction_native LIBRARY DESTINATION refraction)
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nen Huynh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,317 @@
1
+ Metadata-Version: 2.4
2
+ Name: earth-refraction
3
+ Version: 0.1.0
4
+ Summary: Atmospheric and ocean refractive index computation with 3D ray tracing on WGS-84
5
+ Keywords: refraction,ray-tracing,eikonal,atmosphere,ocean,WGS84,radio,sonar,acoustics,NRLMSIS
6
+ Author: Nen Huynh
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Programming Language :: Python :: 3.14
16
+ Classifier: Programming Language :: C++
17
+ Classifier: Topic :: Scientific/Engineering :: Physics
18
+ Classifier: Topic :: Scientific/Engineering :: Atmospheric Science
19
+ Project-URL: Homepage, https://github.com/nvhuynh16/earth_refraction
20
+ Project-URL: Source, https://github.com/nvhuynh16/earth_refraction
21
+ Project-URL: Issues, https://github.com/nvhuynh16/earth_refraction/issues
22
+ Requires-Python: >=3.10
23
+ Requires-Dist: numpy>=1.24
24
+ Requires-Dist: scipy>=1.10
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=7.0; extra == "dev"
27
+ Description-Content-Type: text/markdown
28
+
29
+ # Refraction Index
30
+
31
+ [![CI](https://github.com/nvhuynh16/earth_refraction/actions/workflows/ci.yml/badge.svg)](https://github.com/nvhuynh16/earth_refraction/actions/workflows/ci.yml)
32
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/nvhuynh16/earth_refraction/blob/main/LICENSE)
33
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
34
+ [![C++20](https://img.shields.io/badge/C%2B%2B-20-blue.svg)](https://en.cppreference.com/w/cpp/20)
35
+
36
+ Refractive index computation and 3D ray tracing for atmospheric and ocean media on the WGS-84 ellipsoid.
37
+
38
+ Decades of research have produced accurate refraction models for the atmosphere (Ciddor 1996, ITU-R P.453, NRLMSIS 2.1) and ocean (Millard & Seaver 1990, Meissner & Wentz 2004, Del Grosso 1974), but no modern, readily available library brings them together in a form that engineers and scientists can use directly. This project fills that gap: a single library that computes refractive index profiles, traces rays through the real Earth geometry (WGS-84 ellipsoid), and provides the derivatives needed for inversion and uncertainty analysis — all with cross-validated Python and C++ implementations.
39
+
40
+ ## Features
41
+
42
+ **Atmospheric refraction** (surface to 122 km)
43
+ - Optical mode (Ciddor 1996, 300–1700 nm) and radio mode (ITU-R P.453-14)
44
+ - Species-sum approach: n − 1 = Σ nᵢ × Kᵢ(λ) using NRLMSIS 2.1
45
+ - Surface anchoring to weather observations via temperature offset and density scaling
46
+
47
+ **Ocean refraction** (surface to 11 km depth)
48
+ - Radio: Meissner & Wentz 2004/2012 (double-Debye permittivity)
49
+ - Optical with pressure: Millard & Seaver 1990 (500–700 nm)
50
+ - Optical surface: Quan & Fry 1995 (400–700 nm)
51
+ - Pure water: IAPWS R9-97 (200–1100 nm)
52
+
53
+ **Ocean sound speed**
54
+ - Del Grosso 1974 / Wong & Zhu 1995 (ITS-90, ±0.05 m/s)
55
+ - Chen-Millero / UNESCO 1977 / Wong & Zhu 1995 (ITS-90)
56
+ - Mackenzie 1981 (9-term, ±0.070 m/s)
57
+
58
+ **3D ray tracing** (eikonal equation from Fermat's Principle)
59
+ - ECEF coordinates on WGS-84 ellipsoid — no flat-Earth approximation
60
+ - Slowness momentum formulation — smooth through turning points, no sign tracking
61
+ - Travel time as ODE state — stop at measured radio/sonar round-trip time
62
+ - First and second derivatives (curvature, acceleration) from solver output
63
+ - Travel-time derivatives (dr/dT, d²r/dT², dp/dT) for radar/sonar inversion
64
+ - Sensitivity derivatives w.r.t. 5 initial-condition parameters (h₀, θ₀, α₀, φ₀, λ₀)
65
+ - Batch tracing — trace thousands of rays; pure Python or C++ via nanobind
66
+
67
+ **Three implementation tiers** (same interface for Python and nanobind)
68
+ - **Pure Python** — easy to use, full features including batch tracing
69
+ - **Header-only C++20** — fast, for integration into C++ applications
70
+ - **Native Python (nanobind)** — C++ speed from Python, same API as pure Python
71
+
72
+ ## Installation
73
+
74
+ ### Python
75
+
76
+ ```bash
77
+ pip install earth-refraction
78
+ ```
79
+
80
+ The package is imported as `refraction`. Prebuilt wheels include the C++ native extension.
81
+
82
+ To build from source (requires CMake 3.20+ and a C++20 compiler; [scikit-build-core](https://github.com/scikit-build/scikit-build-core) and [nanobind](https://github.com/wjakob/nanobind) are fetched automatically):
83
+
84
+ ```bash
85
+ git clone https://github.com/nvhuynh16/earth_refraction.git
86
+ cd earth_refraction
87
+ pip install .
88
+ ```
89
+
90
+ For development (editable install with test dependencies):
91
+
92
+ ```bash
93
+ pip install -e ".[dev]"
94
+ ```
95
+
96
+ ### C++ (header-only, no installation needed)
97
+
98
+ ```cpp
99
+ #include <refraction/ray_trace.hpp> // ray tracing
100
+ #include <refraction/profile.hpp> // atmospheric profiles
101
+ #include <refraction/geodetic.hpp> // WGS-84 geodetic utilities
102
+ ```
103
+
104
+ ## Quick Start
105
+
106
+ ### Atmospheric refraction
107
+
108
+ ```python
109
+ from refraction import AtmosphericConditions, RefractionProfile, Mode
110
+
111
+ atm = AtmosphericConditions(day_of_year=172, latitude_deg=45.0)
112
+
113
+ # Use the model directly (NRLMSIS prediction, no anchoring).
114
+ # NRLMSIS has no water vapor, so this profile is dry air only.
115
+ profile = RefractionProfile(atm)
116
+
117
+ # Or anchor to surface weather observations for higher accuracy.
118
+ # The surface temperature offset fades out over temperature_taper_km
119
+ # (Gaussian scale, default 2 km); the pressure offset is carried upward
120
+ # hydrostatically.
121
+ from refraction import SurfaceObservation
122
+ obs = SurfaceObservation(altitude_km=0.0, temperature_C=15.0,
123
+ pressure_kPa=101.325, relative_humidity=0.5,
124
+ temperature_taper_km=2.0)
125
+ profile = RefractionProfile(atm, obs)
126
+
127
+ result = profile.compute(10.0, Mode.Ciddor, wavelength_nm=633.0)
128
+ print(f"n = {result.n:.8f}, N = {result.N:.2f} N-units")
129
+ ```
130
+
131
+ ### Ocean sound speed
132
+
133
+ ```python
134
+ from refraction.ocean import SoundSpeedProfile, SoundMode, OceanConditions
135
+
136
+ # From ocean conditions (temperature, salinity)
137
+ cond = OceanConditions(sst_C=25.0, sss_psu=35.0)
138
+ profile = SoundSpeedProfile(cond)
139
+ result = profile.compute(500.0, SoundMode.DelGrosso) # 500 m depth
140
+ print(f"Sound speed = {result.sound_speed_m_s:.1f} m/s")
141
+ ```
142
+
143
+ ### Ray tracing — atmospheric
144
+
145
+ ```python
146
+ from refraction import (
147
+ EikonalTracer, EikonalInput, EikonalStopCondition, Mode,
148
+ RefractionProfile, AtmosphericConditions,
149
+ )
150
+
151
+ profile = RefractionProfile(AtmosphericConditions(day_of_year=172, latitude_deg=45.0))
152
+ tracer = EikonalTracer.from_profile(profile, Mode.Ciddor)
153
+
154
+ inp = EikonalInput(lat_deg=45.0, lon_deg=0.0, h_m=0.0, elevation_deg=10.0, azimuth_deg=0.0)
155
+ stop = EikonalStopCondition(target_altitude_m=100_000.0)
156
+ result = tracer.trace(inp, stop)
157
+
158
+ print(f"Arc length: {result.s[-1]:.0f} m")
159
+ print(f"Travel time: {result.T[-1]*1e6:.1f} µs")
160
+ print(f"Bending: {result.bending_deg*60:.2f} arcmin") # negative: ray exits shallower
161
+ ```
162
+
163
+ ### Ray tracing — from measured sound velocity profile
164
+
165
+ ```python
166
+ from refraction import EikonalTracer, EikonalInput, EikonalStopCondition
167
+
168
+ # Measured SVP data (depth positive downward, speed in m/s)
169
+ depths = [0, 50, 100, 200, 500, 1000, 2000]
170
+ speeds = [1520, 1515, 1490, 1495, 1500, 1510, 1520]
171
+ tracer = EikonalTracer.from_depth_speed_table(depths, speeds)
172
+
173
+ inp = EikonalInput(lat_deg=30.0, lon_deg=-80.0, h_m=-10.0,
174
+ elevation_deg=-5.0, azimuth_deg=90.0)
175
+ stop = EikonalStopCondition(target_travel_time_s=0.5, detect_ground_hit=False)
176
+ result = tracer.trace(inp, stop)
177
+ ```
178
+
179
+ ### Batch tracing
180
+
181
+ ```python
182
+ from refraction.native import NativeTracer, TableSpeedProfile
183
+ from refraction import BatchInput
184
+ import numpy as np
185
+
186
+ # From atmospheric speed table
187
+ h = np.linspace(0, 200_000, 2000)
188
+ v = 299_792_458.0 / (1.000315 * np.exp(-39e-9 * h))
189
+ tracer = NativeTracer.from_table(TableSpeedProfile(h, v))
190
+
191
+ # Or from measured SVP (depth positive downward)
192
+ # depths = [0, 50, 100, 500, 1000, 2000]
193
+ # speeds = [1520, 1515, 1490, 1500, 1510, 1520]
194
+ # tracer = NativeTracer.from_table(TableSpeedProfile.from_depth(depths, speeds))
195
+
196
+ # 1000 rays at different elevations, same travel time
197
+ inp = BatchInput(
198
+ lat_deg=np.full(1000, 45.0),
199
+ lon_deg=np.zeros(1000),
200
+ h_m=np.zeros(1000),
201
+ elevation_deg=np.linspace(1, 89, 1000),
202
+ azimuth_deg=np.zeros(1000),
203
+ travel_time_s=np.full(1000, 0.001),
204
+ )
205
+ result = tracer.trace_batch(inp) # single C++ call → BatchResult (endpoint only)
206
+
207
+ print(f"Positions shape: {result.r.shape}") # (1000, 3)
208
+ print(f"Velocities shape: {result.dr_dT.shape}") # (1000, 3)
209
+
210
+ # Full ray paths (list of EikonalResult, one per ray)
211
+ full = tracer.trace_batch(inp, endpoint=False)
212
+ print(f"Ray 0 path length: {len(full[0].s)} steps")
213
+ ```
214
+
215
+ > **Note:** `EikonalTracer` also has `trace_batch()` with the same interface,
216
+ > just slower (Python loop internally). Use `NativeTracer` for performance.
217
+
218
+ ### C++ ray tracing
219
+
220
+ ```cpp
221
+ #include <refraction/ray_trace.hpp>
222
+
223
+ auto tracer = refraction::EikonalTracer{
224
+ [](double h) { return 1500.0 + 0.017 * h; }, // v(h)
225
+ [](double) { return 0.017; }, // v'(h)
226
+ };
227
+
228
+ refraction::EikonalInput inp{30.0, -80.0, -10.0, -5.0, 90.0};
229
+ refraction::EikonalStopCondition stop{.target_travel_time_s = 0.5};
230
+ auto result = tracer.trace(inp, stop);
231
+ ```
232
+
233
+ ## Building and Testing
234
+
235
+ ### C++ tests
236
+
237
+ Requires CMake 3.20+ and a C++20 compiler (GCC 10+, Clang 10+, MSVC 19.29+):
238
+
239
+ ```bash
240
+ cd cpp
241
+ cmake -B build
242
+ cmake --build build --config Release
243
+ ctest --test-dir build -C Release --output-on-failure
244
+ ```
245
+
246
+ ### Python tests
247
+
248
+ ```bash
249
+ pip install -e ".[dev]"
250
+ python -m pytest python/tests/ -q
251
+ ```
252
+
253
+ ## Project Structure
254
+
255
+ ```
256
+ cpp/
257
+ include/refraction/ # Header-only C++20 library
258
+ profile.hpp # Atmospheric refraction profiles
259
+ ray_trace.hpp # Eikonal ray tracer + sensitivities
260
+ ray_trace_batch.hpp # Type-erased tracer + batch API
261
+ geodetic.hpp # WGS-84 coordinate utilities
262
+ constants.hpp # Physical constants + WGS-84 ellipsoid
263
+ ocean/ # Ocean refraction + sound speed
264
+ atmosphere/ # NRLMSIS 2.1 model
265
+ tests/ # 17 test executables
266
+
267
+ python/
268
+ src/refraction/
269
+ ray_trace.py # Eikonal tracer, BatchInput/Result, EikonalResult
270
+ geodetic.py # WGS-84 utilities
271
+ native.py # Nanobind wrapper (NativeTracer, TableSpeedProfile)
272
+ profile.py # Atmospheric profiles
273
+ ocean/ # Ocean refraction + sound speed
274
+ nanobind/ # C++ nanobind extension source
275
+ tests/ # 736 Python tests
276
+
277
+ docs/
278
+ Ray Trace/ # Derivation from Fermat's Principle (PDF)
279
+ ```
280
+
281
+ ## Citation
282
+
283
+ If you use this library in your research, please cite:
284
+
285
+ ```bibtex
286
+ @software{huynh2026refraction,
287
+ author = {Huynh, Nen},
288
+ title = {Refraction Index: Atmospheric and Ocean Refractive Index with 3D Ray Tracing},
289
+ year = {2026},
290
+ url = {https://github.com/nvhuynh16/earth_refraction},
291
+ license = {MIT},
292
+ }
293
+ ```
294
+
295
+ ## References
296
+
297
+ ### Atmospheric
298
+ - Ciddor, P. E. (1996). "Refractive index of air: new equations for the visible and near infrared." *Applied Optics*, 35(9), 1566–1573.
299
+ - ITU-R P.453-14 (2019). "The radio refractive index: its formula and refractivity data."
300
+ - Emmert, J. T., et al. (2021). "NRLMSIS 2.0: A whole-atmosphere empirical model of temperature and neutral species densities." *Earth and Space Science*, 8(3), e2020EA001321.
301
+ - Emmert, J. T., et al. (2022). "NRLMSIS 2.1: An empirical model of nitric oxide incorporated into MSIS." *Journal of Geophysical Research: Space Physics*, 127(10), e2022JA030896.
302
+
303
+ ### Ocean
304
+ - Meissner, T. & Wentz, F. J. (2004/2012). "The complex dielectric constant of pure and sea water from microwave satellite observations." *IEEE TGRS*.
305
+ - Millard, R. C. & Seaver, G. (1990). "An index of refraction algorithm for seawater over temperature, pressure, salinity, density, and wavelength." *Deep-Sea Research*, 37(12), 1909–1926.
306
+ - Quan, X. & Fry, E. S. (1995). "Empirical equation for the index of refraction of seawater." *Applied Optics*, 34(18), 3477–3480.
307
+ - IAPWS (1997). "Release on the Refractive Index of Ordinary Water Substance as a Function of Wavelength, Temperature and Pressure." R9-97.
308
+ - Del Grosso, V. A. (1974). "New equation for the speed of sound in natural waters." *JASA*, 56(4), 1084–1091.
309
+ - Chen, C.-T. & Millero, F. J. (1977). "Speed of sound in seawater at high pressures." *JASA*, 62(5), 1129–1135.
310
+ - Mackenzie, K. V. (1981). "Nine-term equation for sound speed in the oceans." *JASA*, 70(3), 807–812.
311
+
312
+ ### Ray Tracing
313
+ - Huynh, N. (2026). "Deriving Atmospheric and Acoustic Refraction from Fermat's Principle." (included in `docs/Ray Trace/`)
314
+
315
+ ## License
316
+
317
+ [MIT](https://github.com/nvhuynh16/earth_refraction/blob/main/LICENSE) © 2026 Nen Huynh
@@ -0,0 +1,289 @@
1
+ # Refraction Index
2
+
3
+ [![CI](https://github.com/nvhuynh16/earth_refraction/actions/workflows/ci.yml/badge.svg)](https://github.com/nvhuynh16/earth_refraction/actions/workflows/ci.yml)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/nvhuynh16/earth_refraction/blob/main/LICENSE)
5
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
6
+ [![C++20](https://img.shields.io/badge/C%2B%2B-20-blue.svg)](https://en.cppreference.com/w/cpp/20)
7
+
8
+ Refractive index computation and 3D ray tracing for atmospheric and ocean media on the WGS-84 ellipsoid.
9
+
10
+ Decades of research have produced accurate refraction models for the atmosphere (Ciddor 1996, ITU-R P.453, NRLMSIS 2.1) and ocean (Millard & Seaver 1990, Meissner & Wentz 2004, Del Grosso 1974), but no modern, readily available library brings them together in a form that engineers and scientists can use directly. This project fills that gap: a single library that computes refractive index profiles, traces rays through the real Earth geometry (WGS-84 ellipsoid), and provides the derivatives needed for inversion and uncertainty analysis — all with cross-validated Python and C++ implementations.
11
+
12
+ ## Features
13
+
14
+ **Atmospheric refraction** (surface to 122 km)
15
+ - Optical mode (Ciddor 1996, 300–1700 nm) and radio mode (ITU-R P.453-14)
16
+ - Species-sum approach: n − 1 = Σ nᵢ × Kᵢ(λ) using NRLMSIS 2.1
17
+ - Surface anchoring to weather observations via temperature offset and density scaling
18
+
19
+ **Ocean refraction** (surface to 11 km depth)
20
+ - Radio: Meissner & Wentz 2004/2012 (double-Debye permittivity)
21
+ - Optical with pressure: Millard & Seaver 1990 (500–700 nm)
22
+ - Optical surface: Quan & Fry 1995 (400–700 nm)
23
+ - Pure water: IAPWS R9-97 (200–1100 nm)
24
+
25
+ **Ocean sound speed**
26
+ - Del Grosso 1974 / Wong & Zhu 1995 (ITS-90, ±0.05 m/s)
27
+ - Chen-Millero / UNESCO 1977 / Wong & Zhu 1995 (ITS-90)
28
+ - Mackenzie 1981 (9-term, ±0.070 m/s)
29
+
30
+ **3D ray tracing** (eikonal equation from Fermat's Principle)
31
+ - ECEF coordinates on WGS-84 ellipsoid — no flat-Earth approximation
32
+ - Slowness momentum formulation — smooth through turning points, no sign tracking
33
+ - Travel time as ODE state — stop at measured radio/sonar round-trip time
34
+ - First and second derivatives (curvature, acceleration) from solver output
35
+ - Travel-time derivatives (dr/dT, d²r/dT², dp/dT) for radar/sonar inversion
36
+ - Sensitivity derivatives w.r.t. 5 initial-condition parameters (h₀, θ₀, α₀, φ₀, λ₀)
37
+ - Batch tracing — trace thousands of rays; pure Python or C++ via nanobind
38
+
39
+ **Three implementation tiers** (same interface for Python and nanobind)
40
+ - **Pure Python** — easy to use, full features including batch tracing
41
+ - **Header-only C++20** — fast, for integration into C++ applications
42
+ - **Native Python (nanobind)** — C++ speed from Python, same API as pure Python
43
+
44
+ ## Installation
45
+
46
+ ### Python
47
+
48
+ ```bash
49
+ pip install earth-refraction
50
+ ```
51
+
52
+ The package is imported as `refraction`. Prebuilt wheels include the C++ native extension.
53
+
54
+ To build from source (requires CMake 3.20+ and a C++20 compiler; [scikit-build-core](https://github.com/scikit-build/scikit-build-core) and [nanobind](https://github.com/wjakob/nanobind) are fetched automatically):
55
+
56
+ ```bash
57
+ git clone https://github.com/nvhuynh16/earth_refraction.git
58
+ cd earth_refraction
59
+ pip install .
60
+ ```
61
+
62
+ For development (editable install with test dependencies):
63
+
64
+ ```bash
65
+ pip install -e ".[dev]"
66
+ ```
67
+
68
+ ### C++ (header-only, no installation needed)
69
+
70
+ ```cpp
71
+ #include <refraction/ray_trace.hpp> // ray tracing
72
+ #include <refraction/profile.hpp> // atmospheric profiles
73
+ #include <refraction/geodetic.hpp> // WGS-84 geodetic utilities
74
+ ```
75
+
76
+ ## Quick Start
77
+
78
+ ### Atmospheric refraction
79
+
80
+ ```python
81
+ from refraction import AtmosphericConditions, RefractionProfile, Mode
82
+
83
+ atm = AtmosphericConditions(day_of_year=172, latitude_deg=45.0)
84
+
85
+ # Use the model directly (NRLMSIS prediction, no anchoring).
86
+ # NRLMSIS has no water vapor, so this profile is dry air only.
87
+ profile = RefractionProfile(atm)
88
+
89
+ # Or anchor to surface weather observations for higher accuracy.
90
+ # The surface temperature offset fades out over temperature_taper_km
91
+ # (Gaussian scale, default 2 km); the pressure offset is carried upward
92
+ # hydrostatically.
93
+ from refraction import SurfaceObservation
94
+ obs = SurfaceObservation(altitude_km=0.0, temperature_C=15.0,
95
+ pressure_kPa=101.325, relative_humidity=0.5,
96
+ temperature_taper_km=2.0)
97
+ profile = RefractionProfile(atm, obs)
98
+
99
+ result = profile.compute(10.0, Mode.Ciddor, wavelength_nm=633.0)
100
+ print(f"n = {result.n:.8f}, N = {result.N:.2f} N-units")
101
+ ```
102
+
103
+ ### Ocean sound speed
104
+
105
+ ```python
106
+ from refraction.ocean import SoundSpeedProfile, SoundMode, OceanConditions
107
+
108
+ # From ocean conditions (temperature, salinity)
109
+ cond = OceanConditions(sst_C=25.0, sss_psu=35.0)
110
+ profile = SoundSpeedProfile(cond)
111
+ result = profile.compute(500.0, SoundMode.DelGrosso) # 500 m depth
112
+ print(f"Sound speed = {result.sound_speed_m_s:.1f} m/s")
113
+ ```
114
+
115
+ ### Ray tracing — atmospheric
116
+
117
+ ```python
118
+ from refraction import (
119
+ EikonalTracer, EikonalInput, EikonalStopCondition, Mode,
120
+ RefractionProfile, AtmosphericConditions,
121
+ )
122
+
123
+ profile = RefractionProfile(AtmosphericConditions(day_of_year=172, latitude_deg=45.0))
124
+ tracer = EikonalTracer.from_profile(profile, Mode.Ciddor)
125
+
126
+ inp = EikonalInput(lat_deg=45.0, lon_deg=0.0, h_m=0.0, elevation_deg=10.0, azimuth_deg=0.0)
127
+ stop = EikonalStopCondition(target_altitude_m=100_000.0)
128
+ result = tracer.trace(inp, stop)
129
+
130
+ print(f"Arc length: {result.s[-1]:.0f} m")
131
+ print(f"Travel time: {result.T[-1]*1e6:.1f} µs")
132
+ print(f"Bending: {result.bending_deg*60:.2f} arcmin") # negative: ray exits shallower
133
+ ```
134
+
135
+ ### Ray tracing — from measured sound velocity profile
136
+
137
+ ```python
138
+ from refraction import EikonalTracer, EikonalInput, EikonalStopCondition
139
+
140
+ # Measured SVP data (depth positive downward, speed in m/s)
141
+ depths = [0, 50, 100, 200, 500, 1000, 2000]
142
+ speeds = [1520, 1515, 1490, 1495, 1500, 1510, 1520]
143
+ tracer = EikonalTracer.from_depth_speed_table(depths, speeds)
144
+
145
+ inp = EikonalInput(lat_deg=30.0, lon_deg=-80.0, h_m=-10.0,
146
+ elevation_deg=-5.0, azimuth_deg=90.0)
147
+ stop = EikonalStopCondition(target_travel_time_s=0.5, detect_ground_hit=False)
148
+ result = tracer.trace(inp, stop)
149
+ ```
150
+
151
+ ### Batch tracing
152
+
153
+ ```python
154
+ from refraction.native import NativeTracer, TableSpeedProfile
155
+ from refraction import BatchInput
156
+ import numpy as np
157
+
158
+ # From atmospheric speed table
159
+ h = np.linspace(0, 200_000, 2000)
160
+ v = 299_792_458.0 / (1.000315 * np.exp(-39e-9 * h))
161
+ tracer = NativeTracer.from_table(TableSpeedProfile(h, v))
162
+
163
+ # Or from measured SVP (depth positive downward)
164
+ # depths = [0, 50, 100, 500, 1000, 2000]
165
+ # speeds = [1520, 1515, 1490, 1500, 1510, 1520]
166
+ # tracer = NativeTracer.from_table(TableSpeedProfile.from_depth(depths, speeds))
167
+
168
+ # 1000 rays at different elevations, same travel time
169
+ inp = BatchInput(
170
+ lat_deg=np.full(1000, 45.0),
171
+ lon_deg=np.zeros(1000),
172
+ h_m=np.zeros(1000),
173
+ elevation_deg=np.linspace(1, 89, 1000),
174
+ azimuth_deg=np.zeros(1000),
175
+ travel_time_s=np.full(1000, 0.001),
176
+ )
177
+ result = tracer.trace_batch(inp) # single C++ call → BatchResult (endpoint only)
178
+
179
+ print(f"Positions shape: {result.r.shape}") # (1000, 3)
180
+ print(f"Velocities shape: {result.dr_dT.shape}") # (1000, 3)
181
+
182
+ # Full ray paths (list of EikonalResult, one per ray)
183
+ full = tracer.trace_batch(inp, endpoint=False)
184
+ print(f"Ray 0 path length: {len(full[0].s)} steps")
185
+ ```
186
+
187
+ > **Note:** `EikonalTracer` also has `trace_batch()` with the same interface,
188
+ > just slower (Python loop internally). Use `NativeTracer` for performance.
189
+
190
+ ### C++ ray tracing
191
+
192
+ ```cpp
193
+ #include <refraction/ray_trace.hpp>
194
+
195
+ auto tracer = refraction::EikonalTracer{
196
+ [](double h) { return 1500.0 + 0.017 * h; }, // v(h)
197
+ [](double) { return 0.017; }, // v'(h)
198
+ };
199
+
200
+ refraction::EikonalInput inp{30.0, -80.0, -10.0, -5.0, 90.0};
201
+ refraction::EikonalStopCondition stop{.target_travel_time_s = 0.5};
202
+ auto result = tracer.trace(inp, stop);
203
+ ```
204
+
205
+ ## Building and Testing
206
+
207
+ ### C++ tests
208
+
209
+ Requires CMake 3.20+ and a C++20 compiler (GCC 10+, Clang 10+, MSVC 19.29+):
210
+
211
+ ```bash
212
+ cd cpp
213
+ cmake -B build
214
+ cmake --build build --config Release
215
+ ctest --test-dir build -C Release --output-on-failure
216
+ ```
217
+
218
+ ### Python tests
219
+
220
+ ```bash
221
+ pip install -e ".[dev]"
222
+ python -m pytest python/tests/ -q
223
+ ```
224
+
225
+ ## Project Structure
226
+
227
+ ```
228
+ cpp/
229
+ include/refraction/ # Header-only C++20 library
230
+ profile.hpp # Atmospheric refraction profiles
231
+ ray_trace.hpp # Eikonal ray tracer + sensitivities
232
+ ray_trace_batch.hpp # Type-erased tracer + batch API
233
+ geodetic.hpp # WGS-84 coordinate utilities
234
+ constants.hpp # Physical constants + WGS-84 ellipsoid
235
+ ocean/ # Ocean refraction + sound speed
236
+ atmosphere/ # NRLMSIS 2.1 model
237
+ tests/ # 17 test executables
238
+
239
+ python/
240
+ src/refraction/
241
+ ray_trace.py # Eikonal tracer, BatchInput/Result, EikonalResult
242
+ geodetic.py # WGS-84 utilities
243
+ native.py # Nanobind wrapper (NativeTracer, TableSpeedProfile)
244
+ profile.py # Atmospheric profiles
245
+ ocean/ # Ocean refraction + sound speed
246
+ nanobind/ # C++ nanobind extension source
247
+ tests/ # 736 Python tests
248
+
249
+ docs/
250
+ Ray Trace/ # Derivation from Fermat's Principle (PDF)
251
+ ```
252
+
253
+ ## Citation
254
+
255
+ If you use this library in your research, please cite:
256
+
257
+ ```bibtex
258
+ @software{huynh2026refraction,
259
+ author = {Huynh, Nen},
260
+ title = {Refraction Index: Atmospheric and Ocean Refractive Index with 3D Ray Tracing},
261
+ year = {2026},
262
+ url = {https://github.com/nvhuynh16/earth_refraction},
263
+ license = {MIT},
264
+ }
265
+ ```
266
+
267
+ ## References
268
+
269
+ ### Atmospheric
270
+ - Ciddor, P. E. (1996). "Refractive index of air: new equations for the visible and near infrared." *Applied Optics*, 35(9), 1566–1573.
271
+ - ITU-R P.453-14 (2019). "The radio refractive index: its formula and refractivity data."
272
+ - Emmert, J. T., et al. (2021). "NRLMSIS 2.0: A whole-atmosphere empirical model of temperature and neutral species densities." *Earth and Space Science*, 8(3), e2020EA001321.
273
+ - Emmert, J. T., et al. (2022). "NRLMSIS 2.1: An empirical model of nitric oxide incorporated into MSIS." *Journal of Geophysical Research: Space Physics*, 127(10), e2022JA030896.
274
+
275
+ ### Ocean
276
+ - Meissner, T. & Wentz, F. J. (2004/2012). "The complex dielectric constant of pure and sea water from microwave satellite observations." *IEEE TGRS*.
277
+ - Millard, R. C. & Seaver, G. (1990). "An index of refraction algorithm for seawater over temperature, pressure, salinity, density, and wavelength." *Deep-Sea Research*, 37(12), 1909–1926.
278
+ - Quan, X. & Fry, E. S. (1995). "Empirical equation for the index of refraction of seawater." *Applied Optics*, 34(18), 3477–3480.
279
+ - IAPWS (1997). "Release on the Refractive Index of Ordinary Water Substance as a Function of Wavelength, Temperature and Pressure." R9-97.
280
+ - Del Grosso, V. A. (1974). "New equation for the speed of sound in natural waters." *JASA*, 56(4), 1084–1091.
281
+ - Chen, C.-T. & Millero, F. J. (1977). "Speed of sound in seawater at high pressures." *JASA*, 62(5), 1129–1135.
282
+ - Mackenzie, K. V. (1981). "Nine-term equation for sound speed in the oceans." *JASA*, 70(3), 807–812.
283
+
284
+ ### Ray Tracing
285
+ - Huynh, N. (2026). "Deriving Atmospheric and Acoustic Refraction from Fermat's Principle." (included in `docs/Ray Trace/`)
286
+
287
+ ## License
288
+
289
+ [MIT](https://github.com/nvhuynh16/earth_refraction/blob/main/LICENSE) © 2026 Nen Huynh
@@ -0,0 +1,13 @@
1
+ cmake_minimum_required(VERSION 3.20)
2
+ project(RefractionIndex LANGUAGES CXX)
3
+
4
+ set(CMAKE_CXX_STANDARD 20)
5
+ set(CMAKE_CXX_STANDARD_REQUIRED ON)
6
+
7
+ # Header-only library
8
+ add_library(refraction INTERFACE)
9
+ target_include_directories(refraction INTERFACE include)
10
+
11
+ # Tests
12
+ enable_testing()
13
+ add_subdirectory(tests)