0
# Support Functions
1
2
Low-level support functions for conductivity calculations, temperature scale conversions, and specialized oceanographic computations. These functions provide the foundation for the main EOS-80 calculations and enable compatibility with different measurement standards.
3
4
## Capabilities
5
6
### Conductivity Calculations
7
8
Low-level conductivity ratio calculations for salinity computations.
9
10
```python { .api }
11
def cndr(s, t, p):
12
"""
13
Conductivity ratio from salinity, temperature, and pressure.
14
15
Calculates the conductivity ratio C(S,T,P)/C(35,15,0) used in
16
the PSS-78 salinity scale calculations.
17
18
Parameters:
19
- s: array_like, salinity [psu (PSS-78)]
20
- t: array_like, temperature [°C (ITS-90)]
21
- p: array_like, pressure [db]
22
23
Returns:
24
- array_like: conductivity ratio [dimensionless]
25
"""
26
27
def salrp(r, t, p):
28
"""
29
Conductivity ratio Rp(S,T,P) from conductivity measurements.
30
31
Calculates pressure-corrected conductivity ratio for salinity
32
determination using PSS-78 algorithms.
33
34
Parameters:
35
- r: array_like, conductivity ratio
36
- t: array_like, temperature [°C (ITS-90)]
37
- p: array_like, pressure [db]
38
39
Returns:
40
- array_like: pressure-corrected conductivity ratio
41
"""
42
43
def salrt(t):
44
"""
45
Conductivity ratio rt(t) for temperature correction.
46
47
Temperature-dependent conductivity ratio used in salinity
48
calculations following PSS-78 standard.
49
50
Parameters:
51
- t: array_like, temperature [°C (ITS-90)]
52
53
Returns:
54
- array_like: temperature conductivity ratio [dimensionless]
55
"""
56
57
def sals(rt, t):
58
"""
59
Salinity from conductivity ratio Rt and temperature.
60
61
Core PSS-78 salinity calculation from temperature-corrected
62
conductivity ratio.
63
64
Parameters:
65
- rt: array_like, conductivity ratio Rt
66
- t: array_like, temperature [°C (ITS-90)]
67
68
Returns:
69
- array_like: salinity [psu (PSS-78)]
70
"""
71
72
def salds(rtx, delt):
73
"""
74
Salinity differential dS/d(√Rt) for iterative calculations.
75
76
Calculates the derivative of salinity with respect to the square root
77
of conductivity ratio for Newton-Raphson salinity iterations.
78
79
Parameters:
80
- rtx: array_like, √Rt (square root of conductivity ratio)
81
- delt: array_like, temperature difference from 15°C
82
83
Returns:
84
- array_like: salinity differential
85
"""
86
```
87
88
89
### Standard Mean Ocean Water
90
91
Reference density calculations for oceanographic standards.
92
93
```python { .api }
94
def smow(t):
95
"""
96
Standard Mean Ocean Water (SMOW) density.
97
98
Calculates the density of Standard Mean Ocean Water as a function
99
of temperature, providing a reference for oceanographic calculations.
100
101
Parameters:
102
- t: array_like, temperature [°C (ITS-90)]
103
104
Returns:
105
- array_like: SMOW density [kg/m³]
106
"""
107
```
108
109
### Temperature Scale Conversions
110
111
Conversions between different international temperature scales for legacy data compatibility.
112
113
```python { .api }
114
def T68conv(T90):
115
"""
116
Convert ITS-90 temperature to IPTS-68.
117
118
Linear transformation: T68 = T90 * 1.00024
119
Accurate within 0.5°C over oceanographic temperature range.
120
121
Parameters:
122
- T90: array_like, temperature [°C (ITS-90)]
123
124
Returns:
125
- array_like: temperature [°C (IPTS-68)]
126
"""
127
128
def T90conv(t, t_type="T68"):
129
"""
130
Convert IPTS-68 or IPTS-48 temperature to ITS-90.
131
132
T68 applies to data collected between 01/10/1968 and 31/12/1989.
133
T48 applies to data collected prior to 31/12/1967.
134
135
Parameters:
136
- t: array_like, temperature [°C (IPTS-68) or (IPTS-48)]
137
- t_type: str, temperature scale type ("T68" or "T48"), default "T68"
138
139
Returns:
140
- array_like: temperature [°C (ITS-90)]
141
"""
142
```
143
144
### Bulk Modulus
145
146
Compressibility calculations for seawater.
147
148
```python { .api }
149
def seck(s, t, p=0):
150
"""
151
Secant bulk modulus of seawater.
152
153
Calculates the secant bulk modulus (K) which is the reciprocal
154
of compressibility, used in equation of state calculations.
155
156
Parameters:
157
- s: array_like, salinity [psu (PSS-78)]
158
- t: array_like, temperature [°C (ITS-90)]
159
- p: array_like, pressure [db], default 0
160
161
Returns:
162
- array_like: secant bulk modulus [bars]
163
"""
164
```
165
166
## Usage Examples
167
168
### Conductivity-Salinity Conversions
169
170
```python
171
import seawater as sw
172
import numpy as np
173
174
# Example: Converting conductivity measurements to salinity
175
# Typical CTD (Conductivity-Temperature-Depth) data processing
176
177
# Raw conductivity measurements (example data)
178
temperatures = np.array([20.0, 15.0, 10.0, 5.0, 2.0]) # °C
179
pressures = np.array([0, 50, 100, 200, 500]) # db
180
conductivity_ratios = np.array([1.000, 0.985, 0.970, 0.955, 0.940])
181
182
# Step 1: Calculate salinity from conductivity
183
salinities = sw.salt(conductivity_ratios, temperatures, pressures)
184
185
print("CTD Data Processing Example:")
186
print("Depth(m) | Temp(°C) | Cond.Ratio | Salinity(psu)")
187
print("-" * 50)
188
for i in range(len(temperatures)):
189
depth = sw.dpth(pressures[i], 45.0) # Convert pressure to depth at 45°N
190
print(f"{depth:6.0f} | {temperatures[i]:6.1f} | {conductivity_ratios[i]:8.3f} | {salinities[i]:10.3f}")
191
192
# Step 2: Verify by converting back to conductivity
193
calculated_ratios = sw.cndr(salinities, temperatures, pressures)
194
195
print("\\nVerification (Salinity → Conductivity):")
196
print("Original | Calculated | Difference")
197
print("-" * 35)
198
for orig, calc in zip(conductivity_ratios, calculated_ratios):
199
diff = abs(orig - calc)
200
print(f"{orig:.6f} | {calc:.6f} | {diff:.2e}")
201
```
202
203
204
### Bulk Modulus and Compressibility
205
206
```python
207
import seawater as sw
208
import numpy as np
209
import matplotlib.pyplot as plt
210
211
# Calculate bulk modulus across oceanographic conditions
212
salinities = [0, 20, 35] # Fresh, brackish, seawater
213
temperatures = np.linspace(0, 30, 31)
214
pressures = [0, 100, 500, 1000] # Surface to deep ocean
215
216
plt.figure(figsize=(12, 8))
217
218
for i, pressure in enumerate(pressures):
219
plt.subplot(2, 2, i+1)
220
221
for salinity in salinities:
222
bulk_modulus = sw.seck(salinity, temperatures, pressure)
223
plt.plot(temperatures, bulk_modulus, linewidth=2,
224
label=f'S = {salinity} psu')
225
226
plt.xlabel('Temperature (°C)')
227
plt.ylabel('Bulk Modulus (bars)')
228
plt.title(f'Bulk Modulus at {pressure} db')
229
plt.legend()
230
plt.grid(True, alpha=0.3)
231
232
plt.tight_layout()
233
plt.show()
234
235
# Calculate compressibility (inverse of bulk modulus)
236
# Standard oceanographic conditions
237
s_std, t_std, p_std = 35.0, 15.0, 100.0
238
bulk_mod = sw.seck(s_std, t_std, p_std)
239
compressibility = 1.0 / bulk_mod # bars⁻¹
240
241
print(f"\\nSeawater properties at S={s_std}, T={t_std}°C, P={p_std} db:")
242
print(f"Bulk modulus: {bulk_mod:.0f} bars")
243
print(f"Compressibility: {compressibility:.2e} bars⁻¹")
244
print(f"Compressibility: {compressibility*1e5:.2e} Pa⁻¹") # Convert to SI units
245
246
# Compare with pure water
247
smow_density = sw.smow(temperatures)
248
print(f"\\nSMOW density range: {smow_density.min():.3f} to {smow_density.max():.3f} kg/m³")
249
250
# Temperature effect on SMOW density
251
plt.figure(figsize=(10, 6))
252
plt.subplot(1, 2, 1)
253
plt.plot(temperatures, smow_density, 'b-', linewidth=2)
254
plt.xlabel('Temperature (°C)')
255
plt.ylabel('SMOW Density (kg/m³)')
256
plt.title('Standard Mean Ocean Water Density')
257
plt.grid(True, alpha=0.3)
258
259
# Compare SMOW with seawater density at same temperature
260
seawater_density_surface = sw.dens0(35.0, temperatures)
261
plt.subplot(1, 2, 2)
262
plt.plot(temperatures, smow_density, 'b-', linewidth=2, label='SMOW')
263
plt.plot(temperatures, seawater_density_surface, 'r-', linewidth=2, label='Seawater (S=35)')
264
plt.xlabel('Temperature (°C)')
265
plt.ylabel('Density (kg/m³)')
266
plt.title('SMOW vs Seawater Density')
267
plt.legend()
268
plt.grid(True, alpha=0.3)
269
270
plt.tight_layout()
271
plt.show()
272
273
# Density difference due to salinity
274
density_diff = seawater_density_surface - smow_density
275
print(f"\\nDensity increase due to S=35 psu:")
276
print(f"At 0°C: {density_diff[0]:.2f} kg/m³")
277
print(f"At 20°C: {density_diff[20]:.2f} kg/m³")
278
```
279
280
### Advanced Conductivity Analysis
281
282
```python
283
import seawater as sw
284
import numpy as np
285
286
# Detailed conductivity-salinity relationship analysis
287
# Simulate a range of oceanographic conditions
288
289
print("Detailed Conductivity-Salinity Analysis")
290
print("=" * 50)
291
292
# Test conditions
293
test_conditions = [
294
(35.000, 15.000, 0), # Standard seawater
295
(0.000, 15.000, 0), # Fresh water
296
(5.000, 10.000, 50), # Brackish water
297
(34.500, 2.000, 1000), # Deep cold water
298
(36.500, 25.000, 100), # Warm saline water
299
]
300
301
print("Condition | S(psu) | T(°C) | P(db) | C.Ratio | Salinity_calc | Error")
302
print("-" * 75)
303
304
for i, (s_true, t, p) in enumerate(test_conditions):
305
# Calculate conductivity ratio from known salinity
306
c_ratio = sw.cndr(s_true, t, p)
307
308
# Calculate salinity back from conductivity ratio
309
s_calc = sw.salt(c_ratio, t, p)
310
311
# Calculate error
312
error = abs(s_calc - s_true)
313
314
print(f"Test {i+1:1d} | {s_true:6.3f} | {t:5.1f} | {p:4.0f} | "
315
f"{c_ratio:7.4f} | {s_calc:11.6f} | {error:.2e}")
316
317
# Test the component functions
318
print("\\nComponent Function Testing:")
319
print("-" * 40)
320
321
s, t, p = 35.0, 15.0, 100.0
322
print(f"Test conditions: S={s}, T={t}°C, P={p} db")
323
324
# Calculate individual components
325
rt = sw.salrt(t)
326
print(f"salrt({t}) = {rt:.6f}")
327
328
# This would require access to intermediate calculations
329
# that aren't directly exposed in the public API
330
print("(Other component functions require intermediate values)")
331
332
# Note: Temperature scale conversions are handled internally
333
# The library expects ITS-90 input temperatures
334
print("\\nNote: Temperature scale conversions are handled internally by the library.")
335
print("All input temperatures should be in ITS-90 scale.")
336
```