Goby3  3.1.5a
2024.05.23
salinity_impl.h
Go to the documentation of this file.
1 // Copyright 2013-2021:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // The Goby Libraries are distributed in the hope that they will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 // modified for C++ by s. petillo spetillo@mit.edu
26 // ocean engineering graduate student - mit / whoi joint program
27 // massachusetts institute of technology (mit)
28 // laboratory for autonomous marine sensing systems (lamss)
29 
30 //Algorithm from S. Petillo salinity.m
31 // Function for calculation of salinity from conductivity.
32 //
33 // S.Petillo (spetillo@mit.edu) - 02 Aug. 2010
34 //
35 // Taken directly from:
36 // 'Algorithms for computation of fundamental properties of seawater'
37 // UNESCO 1983 equation
38 // conductivity ratio to practical salinity conversion (SAL78)
39 //
40 // Valid over the ranges:
41 // Temperaturature: -2<T<35 degC
42 // Salinity: 2<S<42 psu
43 // (from Prekin and Lewis, 1980)
44 //
45 // Units:
46 // conductivity Cond mS/cm
47 // salinity S (PSS-78)
48 // temperature T (IPTS-68) degC
49 // pressure P decibars
50 
51 // Cond(35,15,0) = 42.914 mS/cm
52 // the electrical conductivity of the standard seawater.
53 // Given by Jan Schultz (23 May, 2008) in
54 // 'Conversion between conductivity and PSS-78 salinity' at
55 // http://www.code10.info/index.php?option=com_content&view=category&id=54&Itemid=79
56 
57 #ifndef GOBY_UTIL_SEAWATER_DETAIL_SALINITY_IMPL_H
58 #define GOBY_UTIL_SEAWATER_DETAIL_SALINITY_IMPL_H
59 
60 namespace goby
61 {
62 namespace util
63 {
64 namespace seawater
65 {
66 namespace detail
67 {
69 {
70  public:
71  enum
72  {
73  TO_SALINITY = false,
75  };
76  // returns salinity or conductivity, based on value of M
77  static double compute(double CND, double T, double P, bool M)
78  {
79  double SAL78 = 0;
80 
81  // ZERO SALINITY/CONDUCTIVITY TRAP
82  if (((M == 0) && (CND <= 5e-4)) || ((M == 1) && (CND <= 0.2)))
83  return SAL78;
84 
85  double DT = T - 15;
86 
87  // SELECT BRANCH FOR SALINITY (M=0) OR CONDUCTIVITY (M=1)
88  if (M == 0)
89  {
90  // CONVERT CONDUCTIVITY TO SALINITY
91  double Res = CND;
92  double RT = Res / (RT35(T) * (1.0 + C(P) / (B(T) + A(T) * Res)));
93  RT = std::sqrt(std::abs(RT));
94  SAL78 = SAL(RT, DT);
95  return SAL78;
96  }
97  else
98  {
99  // INVERT SALINITY TO CONDUCTIVITY BY THE
100  // NEWTON-RAPHSON ITERATIVE METHOD
101  // FIRST APPROXIMATION
102 
103  double RT = std::sqrt(CND / 35);
104  double SI = SAL(RT, DT);
105  double N = 0;
106 
107  // TIERATION LOOP BEGINS HERE WITH A MAXIMUM OF 10 CYCLES
108  double DELS = 0;
109  do
110  {
111  RT = RT + (CND - SI) / DSAL(RT, DT);
112  SI = SAL(RT, DT);
113  N = N + 1;
114  DELS = std::abs(SI - CND);
115 
116  } while ((DELS > 1e-4) && (N < 10));
117 
118  //COMPUTE CONDUCTIVITY RATIO
119  double RTT = RT35(T) * RT * RT;
120  double AT = A(T);
121  double BT = B(T);
122  double CP = C(P);
123  CP = RTT * (CP + BT);
124  BT = BT - RTT * AT;
125 
126  // SOLVE QUADRATIC EQUATION FOR R: R=RT35*RT*(1+C/AR+B)
127  // R := SQRT (ABS (BT * BT + 4.0 * AT * CP)) - BT;
128  double Res = std::sqrt(std::abs(BT * BT + 4 * AT * CP)) - BT;
129  // CONDUCTIVITY RETURN
130  SAL78 = 0.5 * Res / AT;
131  return SAL78;
132  }
133  }
134 
135  private:
136  static double SAL(double XR, double XT)
137  {
138  // PRACTICAL SALINITY SCALE 1978 DEFINITION WITH TEMPERATURE
139  // CORRECTION;XT :=T-15.0; XR:=SQRT(RT);
140  double sal =
141  ((((2.7081 * XR - 7.0261) * XR + 14.0941) * XR + 25.3851) * XR - 0.1692) * XR + 0.0080 +
142  (XT / (1.0 + 0.0162 * XT)) *
143  (((((-0.0144 * XR + 0.0636) * XR - 0.0375) * XR - 0.0066) * XR - 0.0056) * XR +
144  0.0005);
145  return sal;
146  }
147 
148  static double DSAL(double XR, double XT)
149  {
150  // FUNCTION FOR DERIVATIVE OF SAL(XR,XT) WITH XR
151  double dsal = ((((13.5405 * XR - 28.1044) * XR + 42.2823) * XR + 50.7702) * XR - 0.1692) +
152  (XT / (1.0 + 0.0162 * XT)) *
153  ((((-0.0720 * XR + 0.2544) * XR - 0.1125) * XR - 0.0132) * XR - 0.0056);
154  return dsal;
155  }
156 
157  static double RT35(double XT)
158  {
159  // FUNCTION RT35: C(35,T,0)/C(35,15,0) VARIATION WITH TEMPERATURE
160  double rt35 =
161  (((1.0031E-9 * XT - 6.9698E-7) * XT + 1.104259E-4) * XT + 2.00564E-2) * XT + 0.6766097;
162  return rt35;
163  }
164 
165  static double C(double XP)
166  {
167  // C(XP) POLYNOMIAL CORRESPONDS TO A1-A3 CONSTANTS: LEWIS 1980
168  double c = ((3.989E-15 * XP - 6.370E-10) * XP + 2.070E-5) * XP;
169  return c;
170  }
171 
172  static double B(double XT)
173  {
174  double b = (4.464E-4 * XT + 3.426E-2) * XT + 1.0;
175  return b;
176  }
177 
178  static double A(double XT)
179  {
180  //A(XT) POLYNOMIAL CORRESPONDS TO B3 AND B4 CONSTANTS: LEWIS 1980
181  double a = -3.107E-3 * XT + 0.4215;
182  return a;
183  }
184 
185  SalinityCalculator();
186  ~SalinityCalculator();
187  SalinityCalculator(const SalinityCalculator&);
188  SalinityCalculator& operator=(const SalinityCalculator&);
189 };
190 } // namespace detail
191 } // namespace seawater
192 } // namespace util
193 } // namespace goby
194 
195 #endif
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::util::e
constexpr T e
Definition: constants.h:35
detail
detail namespace with internal helper functions
Definition: json.hpp:246
goby::util::seawater::detail::SalinityCalculator::TO_SALINITY
@ TO_SALINITY
Definition: salinity_impl.h:73
goby::util::seawater::detail::SalinityCalculator::compute
static double compute(double CND, double T, double P, bool M)
Definition: salinity_impl.h:77
goby::util::seawater::detail::SalinityCalculator
Definition: salinity_impl.h:68
goby::util::seawater::detail::SalinityCalculator::FROM_SALINITY
@ FROM_SALINITY
Definition: salinity_impl.h:74
B