-
Notifications
You must be signed in to change notification settings - Fork 18
/
gsw_oceanographic_toolbox.c
12333 lines (11155 loc) · 502 KB
/
gsw_oceanographic_toolbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** $Id: gsw_oceanographic_toolbox-head,v c61271a7810d 2016/08/19 20:04:03 fdelahoyde $
** Version: 3.05.0-3
**
** This is a translation of the original f90 source code into C
** by the Shipboard Technical Support Computing Resources group
** at Scripps Institution of Oceanography -- sts-cr@sio.ucsd.edu.
** The original notices follow.
**
==========================================================================
Gibbs SeaWater (GSW) Oceanographic Toolbox of TEOS-10 (Fortran)
==========================================================================
This is a subset of functions contained in the Gibbs SeaWater (GSW)
Oceanographic Toolbox of TEOS-10.
Version 1.0 written by David Jackett
Modified by Paul Barker (version 3.02)
Modified by Glenn Hyland (version 3.04+)
For help with this Oceanographic Toolbox email: help@teos-10.org
This software is available from http://www.teos-10.org
==========================================================================
gsw_data_v3_0.nc
NetCDF file that contains the global data set of Absolute Salinity Anomaly
Ratio, the global data set of Absolute Salinity Anomaly atlas, and check
values and computation accuracy values for use in gsw_check_function.
The data set gsw_data_v3_0.nc must not be tampered with.
gsw_check_function.f90
Contains the check functions. We suggest that after downloading, unzipping
and installing the toolbox the user runs this program to ensure that the
toolbox is installed correctly and there are no conflicts. This toolbox has
been tested to compile and run with gfortran.
cd test
make
./gsw_check
Note that gfortran is the name of the GNU Fortran project, developing a
free Fortran 95/2003/2008 compiler for GCC, the GNU Compiler Collection.
It is available from http://gcc.gnu.org/fortran/
==========================================================================
*/
#include "gswteos-10.h"
#ifdef __cplusplus
# define DCOMPLEX std::complex<double>
#else
# define DCOMPLEX double complex
# define real(x) creal(x)
# define log(x) clog(x)
#endif
#include "gsw_internal_const.h"
/*
!==========================================================================
subroutine gsw_add_barrier(input_data,lon,lat,long_grid,lat_grid,dlong_grid,dlat_grid,output_data)
!==========================================================================
! Adds a barrier through Central America (Panama) and then averages
! over the appropriate side of the barrier
!
! data_in : data [unitless]
! lon : Longitudes of data degrees east [0 ... +360]
! lat : Latitudes of data degrees north [-90 ... +90]
! longs_grid : Longitudes of regular grid degrees east [0 ... +360]
! lats_grid : Latitudes of regular grid degrees north [-90 ... +90]
! dlongs_grid : Longitude difference of regular grid degrees [deg longitude]
! dlats_grid : Latitude difference of regular grid degrees [deg latitude]
!
! output_data : average of data depending on which side of the
! Panama canal it is on [unitless]
*/
void
gsw_add_barrier(double *input_data, double lon, double lat,
double long_grid, double lat_grid, double dlong_grid,
double dlat_grid, double *output_data)
{
GSW_SAAR_DATA;
int above_line[4];
int k, nmean, above_line0, kk;
double r, lats_line, data_mean;
k = gsw_util_indx(longs_pan,npan,lon);
/* the lon/lat point */
r = (lon-longs_pan[k])/(longs_pan[k+1]-longs_pan[k]);
lats_line = lats_pan[k] + r*(lats_pan[k+1]-lats_pan[k]);
above_line0 = (lats_line <= lat);
k = gsw_util_indx(longs_pan,npan,long_grid);
/*the 1 & 4 lon/lat points*/
r = (long_grid-longs_pan[k])/
(longs_pan[k+1]-longs_pan[k]);
lats_line = lats_pan[k] + r*(lats_pan[k+1]-lats_pan[k]);
above_line[0] = (lats_line <= lat_grid);
above_line[3] = (lats_line <= lat_grid+dlat_grid);
k = gsw_util_indx(longs_pan,6,long_grid+dlong_grid);
/*the 2 & 3 lon/lat points */
r = (long_grid+dlong_grid-longs_pan[k])/
(longs_pan[k+1]-longs_pan[k]);
lats_line = lats_pan[k] + r*(lats_pan[k+1]-lats_pan[k]);
above_line[1] = (lats_line <= lat_grid);
above_line[2] = (lats_line <= lat_grid+dlat_grid);
nmean = 0;
data_mean = 0.0;
for (kk=0; kk<4; kk++) {
if ((fabs(input_data[kk]) <= 100.0) &&
above_line0 == above_line[kk]) {
nmean = nmean+1;
data_mean = data_mean+input_data[kk];
}
}
if (nmean == 0)
data_mean = 0.0; /*errorreturn*/
else
data_mean = data_mean/nmean;
for (kk=0; kk<4; kk++) {
if ((fabs(input_data[kk]) >= 1.0e10) ||
above_line0 != above_line[kk])
output_data[kk] = data_mean;
else
output_data[kk] = input_data[kk];
}
return;
}
/*
!==========================================================================
subroutine gsw_add_mean(data_in,data_out)
!==========================================================================
! Replaces NaN's with non-nan mean of the 4 adjacent neighbours
!
! data_in : data set of the 4 adjacent neighbours
!
! data_out : non-nan mean of the 4 adjacent neighbours [unitless]
*/
void
gsw_add_mean(double *data_in, double *data_out)
{
int k, nmean;
double data_mean;
nmean = 0;
data_mean = 0.0;
for (k=0; k<4; k++) {
if (fabs(data_in[k]) <= 100.0) {
nmean++;
data_mean = data_mean+data_in[k];
}
}
if (nmean == 0.0)
data_mean = 0.0; /*errorreturn*/
else
data_mean = data_mean/nmean;
for (k=0; k<4; k++) {
if (fabs(data_in[k]) >= 100.0)
data_out[k] = data_mean;
else
data_out[k] = data_in[k];
}
return;
}
/*
!==========================================================================
function gsw_infunnel(sa,ct,p)
!==========================================================================
! "oceanographic funnel" check for the 75-term equation
!
! sa : Absolute Salinity [g/kg]
! ct : Conservative Temperature [deg C]
! p : sea pressure [dbar]
!
! gsw_infunnel : 0, if SA, CT and p are outside the "funnel"
! 1, if SA, CT and p are inside the "funnel"
!
! Note. The term "funnel" (McDougall et al., 2003) describes the range of
! SA, CT and p over which the error in the fit of the computationally
! efficient 75-term expression for specific volume in terms of SA, CT
! and p was calculated (Roquet et al., 2015).
*/
int
gsw_infunnel(double sa, double ct, double p)
{
return !(p > 8000 ||
sa < 0 ||
sa > 42 ||
(p < 500 && ct < gsw_ct_freezing(sa, p, 0)) ||
(p >= 500 && p < 6500 && sa < p * 5e-3 - 2.5) ||
(p >= 500 && p < 6500 && ct > (31.66666666666667 - p * 3.333333333333334e-3)) ||
(p >= 500 && ct < gsw_ct_freezing(sa, 500, 0)) ||
(p >= 6500 && sa < 30) ||
(p >= 6500 && ct > 10.0)
);
}
/*
!==========================================================================
function gsw_adiabatic_lapse_rate_from_ct(sa,ct,p)
!==========================================================================
! Calculates the adiabatic lapse rate from Conservative Temperature
!
! sa : Absolute Salinity [g/kg]
! ct : Conservative Temperature [deg C]
! p : sea pressure [dbar]
!
! gsw_adiabatic_lapse_rate_from_ct : adiabatic lapse rate [K/Pa]
*/
double
gsw_adiabatic_lapse_rate_from_ct(double sa, double ct, double p)
{
int n0=0, n1=1, n2=2;
double pt0, pr0=0.0, t;
pt0 = gsw_pt_from_ct(sa,ct);
t = gsw_pt_from_t(sa,pt0,pr0,p);
return (-gsw_gibbs(n0,n1,n1,sa,t,p)/gsw_gibbs(n0,n2,n0,sa,t,p));
}
/*
!==========================================================================
elemental function gsw_adiabatic_lapse_rate_ice (t, p)
!==========================================================================
!
! Calculates the adiabatic lapse rate of ice.
!
! t = in-situ temperature (ITS-90) [ deg C ]
! p = sea pressure [ dbar ]
! ( i.e. absolute pressure - 10.1325 dbar )
!
! Note. The output is in unit of degrees Celsius per Pa,
! (or equivalently K/Pa) not in units of K/dbar.
!--------------------------------------------------------------------------
*/
double
gsw_adiabatic_lapse_rate_ice(double t, double p)
{
return (-gsw_gibbs_ice(1,1,t,p)/gsw_gibbs_ice(2,0,t,p));
}
/*
!==========================================================================
function gsw_alpha(sa,ct,p)
!==========================================================================
! Calculates the thermal expansion coefficient of seawater with respect to
! Conservative Temperature using the computationally-efficient 48-term
! expression for density in terms of SA, CT and p (IOC et al., 2010)
!
! sa : Absolute Salinity [g/kg]
! ct : Conservative Temperature [deg C]
! p : sea pressure [dbar]
!
! gsw_alpha : thermal expansion coefficient of seawater (48 term equation)
*/
double
gsw_alpha(double sa, double ct, double p)
{
GSW_TEOS10_CONSTANTS;
GSW_SPECVOL_COEFFICIENTS;
double xs, ys, z, v_ct_part;
xs = sqrt(gsw_sfac*sa + offset);
ys = ct*0.025;
z = p*1e-4;
v_ct_part = a000
+ xs*(a100 + xs*(a200 + xs*(a300 + xs*(a400 + a500*xs))))
+ ys*(a010 + xs*(a110 + xs*(a210 + xs*(a310 + a410*xs)))
+ ys*(a020 + xs*(a120 + xs*(a220 + a320*xs)) + ys*(a030
+ xs*(a130 + a230*xs) + ys*(a040 + a140*xs + a050*ys ))))
+ z*(a001 + xs*(a101 + xs*(a201 + xs*(a301 + a401*xs)))
+ ys*(a011 + xs*(a111 + xs*(a211 + a311*xs)) + ys*(a021
+ xs*(a121 + a221*xs) + ys*(a031 + a131*xs + a041*ys)))
+ z*(a002 + xs*(a102 + xs*(a202 + a302*xs)) + ys*(a012
+ xs*(a112 + a212*xs) + ys*(a022 + a122*xs + a032*ys))
+ z*(a003 + a103*xs + a013*ys + a004*z)));
return (0.025*v_ct_part/gsw_specvol(sa,ct,p));
}
/*
!==========================================================================
function gsw_alpha_on_beta(sa,ct,p)
!==========================================================================
! Calculates alpha divided by beta, where alpha is the thermal expansion
! coefficient and beta is the saline contraction coefficient of seawater
! from Absolute Salinity and Conservative Temperature. This function uses
! the computationally-efficient expression for specific volume in terms of
! SA, CT and p (Roquet et al., 2014).
!
! sa : Absolute Salinity [g/kg]
! ct : Conservative Temperature [deg C]
! p : sea pressure [dbar]
!
! alpha_on_beta
! : thermal expansion coefficient with respect to [kg g^-1 K^-1]
! Conservative Temperature divided by the saline
! contraction coefficient at constant Conservative
! Temperature
*/
double
gsw_alpha_on_beta(double sa, double ct, double p)
{
GSW_TEOS10_CONSTANTS;
GSW_SPECVOL_COEFFICIENTS;
double xs, ys, z, v_ct_part, v_sa_part;
xs = sqrt(gsw_sfac*sa + offset);
ys = ct*0.025;
z = p*1e-4;
v_ct_part = a000
+ xs*(a100 + xs*(a200 + xs*(a300 + xs*(a400 + a500*xs))))
+ ys*(a010 + xs*(a110 + xs*(a210 + xs*(a310 + a410*xs)))
+ ys*(a020 + xs*(a120 + xs*(a220 + a320*xs)) + ys*(a030
+ xs*(a130 + a230*xs) + ys*(a040 + a140*xs + a050*ys ))))
+ z*(a001 + xs*(a101 + xs*(a201 + xs*(a301 + a401*xs)))
+ ys*(a011 + xs*(a111 + xs*(a211 + a311*xs)) + ys*(a021
+ xs*(a121 + a221*xs) + ys*(a031 + a131*xs + a041*ys)))
+ z*(a002 + xs*(a102 + xs*(a202 + a302*xs)) + ys*(a012
+ xs*(a112 + a212*xs) + ys*(a022 + a122*xs + a032*ys))
+ z*(a003 + a103*xs + a013*ys + a004*z)));
v_sa_part = b000
+ xs*(b100 + xs*(b200 + xs*(b300 + xs*(b400 + b500*xs))))
+ ys*(b010 + xs*(b110 + xs*(b210 + xs*(b310 + b410*xs)))
+ ys*(b020 + xs*(b120 + xs*(b220 + b320*xs)) + ys*(b030
+ xs*(b130 + b230*xs) + ys*(b040 + b140*xs + b050*ys))))
+ z*(b001 + xs*(b101 + xs*(b201 + xs*(b301 + b401*xs)))
+ ys*(b011 + xs*(b111 + xs*(b211 + b311*xs)) + ys*(b021
+ xs*(b121 + b221*xs) + ys*(b031 + b131*xs + b041*ys)))
+ z*(b002 + xs*(b102 + xs*(b202 + b302*xs))+ ys*(b012
+ xs*(b112 + b212*xs) + ys*(b022 + b122*xs + b032*ys))
+ z*(b003 + b103*xs + b013*ys + b004*z)));
return (-(v_ct_part*xs)/(20.0*gsw_sfac*v_sa_part));
}
/*
!==========================================================================
function gsw_alpha_wrt_t_exact(sa,t,p)
!==========================================================================
! Calculates thermal expansion coefficient of seawater with respect to
! in-situ temperature
!
! sa : Absolute Salinity [g/kg]
! t : insitu temperature [deg C]
! p : sea pressure [dbar]
!
! gsw_alpha_wrt_t_exact : thermal expansion coefficient [1/K]
! wrt (in-situ) temperature
*/
double
gsw_alpha_wrt_t_exact(double sa, double t, double p)
{
int n0=0, n1=1;
return (gsw_gibbs(n0,n1,n1,sa,t,p)/gsw_gibbs(n0,n0,n1,sa,t,p));
}
/*
!==========================================================================
elemental function gsw_alpha_wrt_t_ice (t, p)
!==========================================================================
!
! Calculates the thermal expansion coefficient of ice with respect to
! in-situ temperature.
!
! t = in-situ temperature (ITS-90) [ deg C ]
! p = sea pressure [ dbar ]
! ( i.e. absolute pressure - 10.1325 dbar )
!
! alpha_wrt_t_ice = thermal expansion coefficient of ice with respect
! to in-situ temperature [ 1/K ]
!--------------------------------------------------------------------------
*/
double
gsw_alpha_wrt_t_ice(double t, double p)
{
return (gsw_gibbs_ice(1,1,t,p)/gsw_gibbs_ice(0,1,t,p));
}
/*
!==========================================================================
function gsw_beta(sa,ct,p)
!==========================================================================
! Calculates the saline (i.e. haline) contraction coefficient of seawater
! at constant Conservative Temperature using the computationally-efficient
! expression for specific volume in terms of SA, CT and p
! (Roquet et al., 2014).
!
! sa : Absolute Salinity [g/kg]
! ct : Conservative Temperature (ITS-90) [deg C]
! p : sea pressure [dbar]
! ( i.e. absolute pressure - 10.1325 dbar )
!
! beta : saline contraction coefficient of seawater [kg/g]
! at constant Conservative Temperature
*/
double
gsw_beta(double sa, double ct, double p)
{
GSW_TEOS10_CONSTANTS;
GSW_SPECVOL_COEFFICIENTS;
double xs, ys, z, v_sa_part;
xs = sqrt(gsw_sfac*sa + offset);
ys = ct*0.025;
z = p*1e-4;
v_sa_part = b000
+ xs*(b100 + xs*(b200 + xs*(b300 + xs*(b400 + b500*xs))))
+ ys*(b010 + xs*(b110 + xs*(b210 + xs*(b310 + b410*xs)))
+ ys*(b020 + xs*(b120 + xs*(b220 + b320*xs)) + ys*(b030
+ xs*(b130 + b230*xs) + ys*(b040 + b140*xs + b050*ys))))
+ z*(b001 + xs*(b101 + xs*(b201 + xs*(b301 + b401*xs)))
+ ys*(b011 + xs*(b111 + xs*(b211 + b311*xs)) + ys*(b021
+ xs*(b121 + b221*xs) + ys*(b031 + b131*xs + b041*ys)))
+ z*(b002 + xs*(b102 + xs*(b202 + b302*xs))+ ys*(b012
+ xs*(b112 + b212*xs) + ys*(b022 + b122*xs + b032*ys))
+ z*(b003 + b103*xs + b013*ys + b004*z)));
return (-v_sa_part*0.5*gsw_sfac/(gsw_specvol(sa,ct,p)*xs));
}
/*
!==========================================================================
function gsw_beta_const_t_exact(sa,t,p)
!==========================================================================
! Calculates saline (haline) contraction coefficient of seawater at
! constant in-situ temperature.
!
! sa : Absolute Salinity [g/kg]
! t : in-situ temperature [deg C]
! p : sea pressure [dbar]
!
! beta_const_t_exact : haline contraction coefficient [kg/g]
*/
double
gsw_beta_const_t_exact(double sa, double t, double p)
{
int n0=0, n1=1;
return (-gsw_gibbs(n1,n0,n1,sa,t,p)/gsw_gibbs(n0,n0,n1,sa,t,p));
}
/*
!==========================================================================
function gsw_c_from_sp(sp,t,p)
!==========================================================================
! Calculates conductivity, C, from (SP,t,p) using PSS-78 in the range
! 2 < SP < 42. If the input Practical Salinity is less than 2 then a
! modified form of the Hill et al. (1986) formula is used for Practical
! Salinity. The modification of the Hill et al. (1986) expression is to
! ensure that it is exactly consistent with PSS-78 at SP = 2.
!
! The conductivity ratio returned by this function is consistent with the
! input value of Practical Salinity, SP, to 2x10^-14 psu over the full
! range of input parameters (from pure fresh water up to SP = 42 psu).
! This error of 2x10^-14 psu is machine precision at typical seawater
! salinities. This accuracy is achieved by having four different
! polynomials for the starting value of Rtx (the square root of Rt) in
! four different ranges of SP, and by using one and a half iterations of
! a computationally efficient modified Newton-Raphson technique (McDougall
! and Wotherspoon, 2012) to find the root of the equation.
!
! Note that strictly speaking PSS-78 (Unesco, 1983) defines Practical
! Salinity in terms of the conductivity ratio, R, without actually
! specifying the value of C(35,15,0) (which we currently take to be
! 42.9140 mS/cm).
!
! sp : Practical Salinity [unitless]
! t : in-situ temperature [ITS-90] [deg C]
! p : sea pressure [dbar]
!
! c : conductivity [ mS/cm ]
*/
double
gsw_c_from_sp(double sp, double t, double p)
{
GSW_TEOS10_CONSTANTS;
GSW_SP_COEFFICIENTS;
double p0 = 4.577801212923119e-3, p1 = 1.924049429136640e-1,
p2 = 2.183871685127932e-5, p3 = -7.292156330457999e-3,
p4 = 1.568129536470258e-4, p5 = -1.478995271680869e-6,
p6 = 9.086442524716395e-4, p7 = -1.949560839540487e-5,
p8 = -3.223058111118377e-6, p9 = 1.175871639741131e-7,
p10 = -7.522895856600089e-5, p11 = -2.254458513439107e-6,
p12 = 6.179992190192848e-7, p13 = 1.005054226996868e-8,
p14 = -1.923745566122602e-9, p15 = 2.259550611212616e-6,
p16 = 1.631749165091437e-7, p17 = -5.931857989915256e-9,
p18 = -4.693392029005252e-9, p19 = 2.571854839274148e-10,
p20 = 4.198786822861038e-12,
q0 = 5.540896868127855e-5, q1 = 2.015419291097848e-1,
q2 = -1.445310045430192e-5, q3 = -1.567047628411722e-2,
q4 = 2.464756294660119e-4, q5 = -2.575458304732166e-7,
q6 = 5.071449842454419e-3, q7 = -9.081985795339206e-5,
q8 = -3.635420818812898e-6, q9 = 2.249490528450555e-8,
q10 = -1.143810377431888e-3, q11 = 2.066112484281530e-5,
q12 = 7.482907137737503e-7, q13 = 4.019321577844724e-8,
q14 = -5.755568141370501e-10, q15 = 1.120748754429459e-4,
q16 = -2.420274029674485e-6, q17 = -4.774829347564670e-8,
q18 = -4.279037686797859e-9, q19 = -2.045829202713288e-10,
q20 = 5.025109163112005e-12,
s0 = 3.432285006604888e-3, s1 = 1.672940491817403e-1,
s2 = 2.640304401023995e-5, s3 = 1.082267090441036e-1,
s4 = -6.296778883666940e-5, s5 = -4.542775152303671e-7,
s6 = -1.859711038699727e-1, s7 = 7.659006320303959e-4,
s8 = -4.794661268817618e-7, s9 = 8.093368602891911e-9,
s10 = 1.001140606840692e-1, s11 = -1.038712945546608e-3,
s12 = -6.227915160991074e-6, s13 = 2.798564479737090e-8,
s14 = -1.343623657549961e-10, s15 = 1.024345179842964e-2,
s16 = 4.981135430579384e-4, s17 = 4.466087528793912e-6,
s18 = 1.960872795577774e-8, s19 = -2.723159418888634e-10,
s20 = 1.122200786423241e-12,
u0 = 5.180529787390576e-3, u1 = 1.052097167201052e-3,
u2 = 3.666193708310848e-5, u3 = 7.112223828976632e0,
u4 = -3.631366777096209e-4, u5 = -7.336295318742821e-7,
u6 = -1.576886793288888e+2, u7 = -1.840239113483083e-3,
u8 = 8.624279120240952e-6, u9 = 1.233529799729501e-8,
u10 = 1.826482800939545e+3, u11 = 1.633903983457674e-1,
u12 = -9.201096427222349e-5, u13 = -9.187900959754842e-8,
u14 = -1.442010369809705e-10, u15 = -8.542357182595853e+3,
u16 = -1.408635241899082e0, u17 = 1.660164829963661e-4,
u18 = 6.797409608973845e-7, u19 = 3.345074990451475e-10,
u20 = 8.285687652694768e-13;
double t68, ft68, x, rtx=0.0, dsp_drtx, sqrty,
part1, part2, hill_ratio, sp_est,
rtx_old, rt, aa, bb, cc, dd, ee, ra,r, rt_lc, rtxm,
sp_hill_raw;
t68 = t*1.00024e0;
ft68 = (t68 - 15e0)/(1e0 + k*(t68 - 15e0));
x = sqrt(sp);
/*
|--------------------------------------------------------------------------
! Finding the starting value of Rtx, the square root of Rt, using four
! different polynomials of SP and t68.
!--------------------------------------------------------------------------
*/
if (sp >= 9.0) {
rtx = p0 + x*(p1 + p4*t68 + x*(p3 + p7*t68 + x*(p6
+ p11*t68 + x*(p10 + p16*t68 + x*p15))))
+ t68*(p2+ t68*(p5 + x*x*(p12 + x*p17) + p8*x
+ t68*(p9 + x*(p13 + x*p18)+ t68*(p14 + p19*x + p20*t68))));
} else if (sp >= 0.25 && sp < 9.0) {
rtx = q0 + x*(q1 + q4*t68 + x*(q3 + q7*t68 + x*(q6
+ q11*t68 + x*(q10 + q16*t68 + x*q15))))
+ t68*(q2+ t68*(q5 + x*x*(q12 + x*q17) + q8*x
+ t68*(q9 + x*(q13 + x*q18)+ t68*(q14 + q19*x + q20*t68))));
} else if (sp >= 0.003 && sp < 0.25) {
rtx = s0 + x*(s1 + s4*t68 + x*(s3 + s7*t68 + x*(s6
+ s11*t68 + x*(s10 + s16*t68 + x*s15))))
+ t68*(s2+ t68*(s5 + x*x*(s12 + x*s17) + s8*x
+ t68*(s9 + x*(s13 + x*s18)+ t68*(s14 + s19*x + s20*t68))));
} else if (sp < 0.003) {
rtx = u0 + x*(u1 + u4*t68 + x*(u3 + u7*t68 + x*(u6
+ u11*t68 + x*(u10 + u16*t68 + x*u15))))
+ t68*(u2+ t68*(u5 + x*x*(u12 + x*u17) + u8*x
+ t68*(u9 + x*(u13 + x*u18)+ t68*(u14 + u19*x + u20*t68))));
}
/*
!--------------------------------------------------------------------------
! Finding the starting value of dSP_dRtx, the derivative of SP with respect
! to Rtx.
!--------------------------------------------------------------------------
*/
dsp_drtx = a1 + (2e0*a2 + (3e0*a3 +
(4e0*a4 + 5e0*a5*rtx)*rtx)*rtx)*rtx
+ ft68*(b1 + (2e0*b2 + (3e0*b3 + (4e0*b4 +
5e0*b5*rtx)*rtx)*rtx)*rtx);
if (sp < 2.0) {
x = 400e0*(rtx*rtx);
sqrty = 10.0*rtx;
part1 = 1e0 + x*(1.5e0 + x);
part2 = 1e0 + sqrty*(1e0 + sqrty*(1e0 + sqrty));
hill_ratio = gsw_hill_ratio_at_sp2(t);
dsp_drtx = dsp_drtx
+ a0*800e0*rtx*(1.5e0 + 2e0*x)/(part1*part1)
+ b0*ft68*(10e0 + sqrty*(20e0 + 30e0*sqrty))/
(part2*part2);
dsp_drtx = hill_ratio*dsp_drtx;
}
/*
!--------------------------------------------------------------------------
! One iteration through the modified Newton-Raphson method (McDougall and
! Wotherspoon, 2012) achieves an error in Practical Salinity of about
! 10^-12 for all combinations of the inputs. One and a half iterations of
! the modified Newton-Raphson method achevies a maximum error in terms of
! Practical Salinity of better than 2x10^-14 everywhere.
!
! We recommend one and a half iterations of the modified Newton-Raphson
! method.
!
! Begin the modified Newton-Raphson method.
!--------------------------------------------------------------------------
*/
sp_est = a0 + (a1 + (a2 + (a3 + (a4 + a5*rtx)*rtx)*rtx)*rtx)*rtx
+ ft68*(b0 + (b1 + (b2+ (b3 + (b4 + b5*rtx)*rtx)*rtx)*rtx)*rtx);
if (sp_est < 2.0) {
x = 400e0*(rtx*rtx);
sqrty = 10e0*rtx;
part1 = 1e0 + x*(1.5e0 + x);
part2 = 1e0 + sqrty*(1e0 + sqrty*(1e0 + sqrty));
sp_hill_raw = sp_est - a0/part1 - b0*ft68/part2;
hill_ratio = gsw_hill_ratio_at_sp2(t);
sp_est = hill_ratio*sp_hill_raw;
}
rtx_old = rtx;
rtx = rtx_old - (sp_est - sp)/dsp_drtx;
rtxm = 0.5e0*(rtx + rtx_old); /*This mean value of Rtx, Rtxm, is the
value of Rtx at which the derivative dSP_dRtx is evaluated.*/
dsp_drtx= a1 + (2e0*a2 + (3e0*a3 + (4e0*a4 +
5e0*a5*rtxm)*rtxm)*rtxm)*rtxm
+ ft68*(b1 + (2e0*b2 + (3e0*b3 + (4e0*b4 +
5e0*b5*rtxm)*rtxm)*rtxm)*rtxm);
if (sp_est < 2.0) {
x = 400e0*(rtxm*rtxm);
sqrty = 10e0*rtxm;
part1 = 1e0 + x*(1.5e0 + x);
part2 = 1e0 + sqrty*(1e0 + sqrty*(1e0 + sqrty));
dsp_drtx = dsp_drtx
+ a0*800e0*rtxm*(1.5e0 + 2e0*x)/(part1*part1)
+ b0*ft68*(10e0 + sqrty*(20e0 + 30e0*sqrty))/
(part2*part2);
hill_ratio = gsw_hill_ratio_at_sp2(t);
dsp_drtx = hill_ratio*dsp_drtx;
}
/*
!--------------------------------------------------------------------------
! The line below is where Rtx is updated at the end of the one full
! iteration of the modified Newton-Raphson technique.
!--------------------------------------------------------------------------
*/
rtx = rtx_old - (sp_est - sp)/dsp_drtx;
/*
!--------------------------------------------------------------------------
! Now we do another half iteration of the modified Newton-Raphson
! technique, making a total of one and a half modified N-R iterations.
!--------------------------------------------------------------------------
*/
sp_est = a0 + (a1 + (a2 + (a3 + (a4 + a5*rtx)*rtx)*rtx)*rtx)*rtx
+ ft68*(b0 + (b1 + (b2+ (b3 + (b4 + b5*rtx)*rtx)*rtx)*rtx)*rtx);
if (sp_est < 2.0) {
x = 400e0*(rtx*rtx);
sqrty = 10e0*rtx;
part1 = 1e0 + x*(1.5e0 + x);
part2 = 1e0 + sqrty*(1e0 + sqrty*(1e0 + sqrty));
sp_hill_raw = sp_est - a0/part1 - b0*ft68/part2;
hill_ratio = gsw_hill_ratio_at_sp2(t);
sp_est = hill_ratio*sp_hill_raw;
}
rtx = rtx - (sp_est - sp)/dsp_drtx;
/*
!--------------------------------------------------------------------------
! Now go from Rtx to Rt and then to the conductivity ratio R at pressure p.
!--------------------------------------------------------------------------
*/
rt = rtx*rtx;
aa = d3 + d4*t68;
bb = 1e0 + t68*(d1 + d2*t68);
cc = p*(e1 + p*(e2 + e3*p));
/* rt_lc (i.e. rt_lower_case) corresponds to rt as defined in
the UNESCO 44 (1983) routines. */
rt_lc = c0 + (c1 + (c2 + (c3 + c4*t68)*t68)*t68)*t68;
dd = bb - aa*rt_lc*rt;
ee = rt_lc*rt*aa*(bb + cc);
ra = sqrt(dd*dd + 4e0*ee) - dd;
r = 0.5e0*ra/aa;
/*
! The dimensionless conductivity ratio, R, is the conductivity input, C,
! divided by the present estimate of C(SP=35, t_68=15, p=0) which is
! 42.9140 mS/cm (=4.29140 S/m^).
*/
return (gsw_c3515*r);
}
/*
!==========================================================================
function gsw_cabbeling(sa,ct,p)
!==========================================================================
! Calculates the cabbeling coefficient of seawater with respect to
! Conservative Temperature. This function uses the computationally-
! efficient expression for specific volume in terms of SA, CT and p
! (Roquet et al., 2014).
!
! sa : Absolute Salinity [g/kg]
! ct : Conservative Temperature (ITS-90) [deg C]
! p : sea pressure [dbar]
!
! cabbeling : cabbeling coefficient with respect to [1/K^2]
! Conservative Temperature.
*/
double
gsw_cabbeling(double sa, double ct, double p)
{
double alpha_ct, alpha_on_beta, alpha_sa, beta_sa, rho,
v_sa, v_ct, v_sa_sa, v_sa_ct, v_ct_ct;
gsw_specvol_first_derivatives(sa,ct,p,&v_sa,&v_ct, NULL);
gsw_specvol_second_derivatives(sa,ct,p,&v_sa_sa,&v_sa_ct,&v_ct_ct,
NULL, NULL);
rho = gsw_rho(sa,ct,p);
alpha_ct = rho*(v_ct_ct - rho*v_ct*v_ct);
alpha_sa = rho*(v_sa_ct - rho*v_sa*v_ct);
beta_sa = -rho*(v_sa_sa - rho*v_sa*v_sa);
alpha_on_beta = gsw_alpha_on_beta(sa,ct,p);
return (alpha_ct +
alpha_on_beta*(2.0*alpha_sa - alpha_on_beta*beta_sa));
}
/*
!==========================================================================
elemental function gsw_chem_potential_water_ice (t, p)
!==========================================================================
!
! Calculates the chemical potential of water in ice from in-situ
! temperature and pressure.
!
! t = in-situ temperature (ITS-90) [ deg C ]
! p = sea pressure [ dbar ]
! ( i.e. absolute pressure - 10.1325 dbar )
!
! chem_potential_water_ice = chemical potential of ice [ J/kg ]
!--------------------------------------------------------------------------
*/
double
gsw_chem_potential_water_ice(double t, double p)
{
return (gsw_gibbs_ice(0,0,t,p));
}
/*
!==========================================================================
elemental function gsw_chem_potential_water_t_exact (sa, t, p)
!==========================================================================
!
! Calculates the chemical potential of water in seawater.
!
! SA = Absolute Salinity [ g/kg ]
! t = in-situ temperature (ITS-90) [ deg C ]
! p = sea pressure [ dbar ]
! ( i.e. absolute pressure - 10.1325 dbar )
!
! chem_potential_water_t_exact = chemical potential of water in seawater
! [ J/g ]
!--------------------------------------------------------------------------
*/
double
gsw_chem_potential_water_t_exact(double sa, double t, double p)
{
GSW_TEOS10_CONSTANTS;
double g03_g, g08_g, g_sa_part, x, x2, y, z, kg2g = 1e-3;
x2 = gsw_sfac*sa;
x = sqrt(x2);
y = t*0.025;
z = p*1e-4;
g03_g = 101.342743139674 + z*(100015.695367145 +
z*(-2544.5765420363 + z*(284.517778446287 +
z*(-33.3146754253611 + (4.20263108803084 - 0.546428511471039*z)*z)))) +
y*(5.90578347909402 + z*(-270.983805184062 +
z*(776.153611613101 + z*(-196.51255088122 +
(28.9796526294175 - 2.13290083518327*z)*z))) +
y*(-12357.785933039 + z*(1455.0364540468 +
z*(-756.558385769359 + z*(273.479662323528 +
z*(-55.5604063817218 + 4.34420671917197*z)))) +
y*(736.741204151612 + z*(-672.50778314507 +
z*(499.360390819152 + z*(-239.545330654412 +
(48.8012518593872 - 1.66307106208905*z)*z))) +
y*(-148.185936433658 + z*(397.968445406972 +
z*(-301.815380621876 + (152.196371733841 - 26.3748377232802*z)*z)) +
y*(58.0259125842571 + z*(-194.618310617595 +
z*(120.520654902025 + z*(-55.2723052340152 + 6.48190668077221*z))) +
y*(-18.9843846514172 + y*(3.05081646487967 - 9.63108119393062*z) +
z*(63.5113936641785 + z*(-22.2897317140459 + 8.17060541818112*z))))))));
g08_g = x2*(1416.27648484197 +
x*(-2432.14662381794 + x*(2025.80115603697 +
y*(543.835333000098 + y*(-68.5572509204491 +
y*(49.3667694856254 + y*(-17.1397577419788 +
2.49697009569508*y))) - 22.6683558512829*z) +
x*(-1091.66841042967 - 196.028306689776*y +
x*(374.60123787784 - 48.5891069025409*x +
36.7571622995805*y) + 36.0284195611086*z) +
z*(-54.7919133532887 + (-4.08193978912261 -
30.1755111971161*z)*z)) +
z*(199.459603073901 + z*(-52.2940909281335 +
(68.0444942726459 - 3.41251932441282*z)*z)) +
y*(-493.407510141682 + z*(-175.292041186547 +
(83.1923927801819 - 29.483064349429*z)*z) +
y*(-43.0664675978042 + z*(383.058066002476 +
z*(-54.1917262517112 + 25.6398487389914*z)) +
y*(-10.0227370861875 - 460.319931801257*z +
y*(0.875600661808945 + 234.565187611355*z))))) +
y*(168.072408311545));
g_sa_part = 8645.36753595126 +
x*(-7296.43987145382 + x*(8103.20462414788 +
y*(2175.341332000392 + y*(-274.2290036817964 +
y*(197.4670779425016 + y*(-68.5590309679152 +
9.98788038278032*y))) - 90.6734234051316*z) +
x*(-5458.34205214835 - 980.14153344888*y +
x*(2247.60742726704 - 340.1237483177863*x +
220.542973797483*y) + 180.142097805543*z) +
z*(-219.1676534131548 + (-16.32775915649044 -
120.7020447884644*z)*z)) +
z*(598.378809221703 + z*(-156.8822727844005 +
(204.1334828179377 - 10.23755797323846*z)*z)) +
y*(-1480.222530425046 + z*(-525.876123559641 +
(249.57717834054571 - 88.449193048287*z)*z) +
y*(-129.1994027934126 + z*(1149.174198007428 +
z*(-162.5751787551336 + 76.9195462169742*z)) +
y*(-30.0682112585625 - 1380.9597954037708*z +
y*(2.626801985426835 + 703.695562834065*z))))) +
y*(1187.3715515697959);
return (kg2g*(g03_g + g08_g - 0.5*x2*g_sa_part));
}
/*
!==========================================================================
elemental function gsw_cp_ice (t, p)
!==========================================================================
!
! Calculates the isobaric heat capacity of seawater.
!
! t = in-situ temperature (ITS-90) [ deg C ]
! p = sea pressure [ dbar ]
! ( i.e. absolute pressure - 10.1325 dbar )
!
! gsw_cp_ice = heat capacity of ice [J kg^-1 K^-1]
!--------------------------------------------------------------------------
*/
double
gsw_cp_ice(double t, double p)
{
GSW_TEOS10_CONSTANTS;
return (-(t + gsw_t0)*gsw_gibbs_ice(2,0,t,p));
}
/*
!==========================================================================
function gsw_cp_t_exact(sa,t,p)
!==========================================================================
! Calculates isobaric heat capacity of seawater
!
! sa : Absolute Salinity [g/kg]
! t : in-situ temperature [deg C]
! p : sea pressure [dbar]
!
! gsw_cp_t_exact : heat capacity [J/(kg K)]
*/
double
gsw_cp_t_exact(double sa, double t, double p)
{
int n0, n2;
n0 = 0;
n2 = 2;
return (-(t+273.15e0)*gsw_gibbs(n0,n2,n0,sa,t,p));
}
/*
!==========================================================================
elemental subroutine gsw_ct_first_derivatives (sa, pt, ct_sa, ct_pt)
!==========================================================================
!
! Calculates the following two derivatives of Conservative Temperature
! (1) CT_SA, the derivative with respect to Absolute Salinity at
! constant potential temperature (with pr = 0 dbar), and
! 2) CT_pt, the derivative with respect to potential temperature
! (the regular potential temperature which is referenced to 0 dbar)
! at constant Absolute Salinity.
!
! SA = Absolute Salinity [ g/kg ]
! pt = potential temperature (ITS-90) [ deg C ]
! (whose reference pressure is 0 dbar)
!
! CT_SA = The derivative of Conservative Temperature with respect to
! Absolute Salinity at constant potential temperature
! (the regular potential temperature which has reference
! sea pressure of 0 dbar).
! The CT_SA output has units of: [ K/(g/kg)]
! CT_pt = The derivative of Conservative Temperature with respect to
! potential temperature (the regular one with pr = 0 dbar)
! at constant SA. CT_pt is dimensionless. [ unitless ]
!--------------------------------------------------------------------------
*/
void
gsw_ct_first_derivatives(double sa, double pt, double *ct_sa, double *ct_pt)
{
GSW_TEOS10_CONSTANTS;
double abs_pt, g_sa_mod, g_sa_t_mod, x, y_pt;
abs_pt = gsw_t0 + pt ;
if (ct_pt != NULL)
*ct_pt = -(abs_pt*gsw_gibbs_pt0_pt0(sa,pt))/gsw_cp0;
if (ct_sa == NULL)
return;
x = sqrt(gsw_sfac*sa);
y_pt = 0.025*pt;
g_sa_t_mod = 1187.3715515697959 + x*(-1480.222530425046
+ x*(2175.341332000392 + x*(-980.14153344888
+ 220.542973797483*x) + y_pt*(-548.4580073635929
+ y_pt*(592.4012338275047 + y_pt*(-274.2361238716608
+ 49.9394019139016*y_pt)))) + y_pt*(-258.3988055868252
+ y_pt*(-90.2046337756875 + y_pt*10.50720794170734)))
+ y_pt*(3520.125411988816 + y_pt*(-1351.605895580406
+ y_pt*(731.4083582010072 + y_pt*(-216.60324087531103
+ 25.56203650166196*y_pt))));
g_sa_t_mod = 0.5*gsw_sfac*0.025*g_sa_t_mod;
g_sa_mod = 8645.36753595126 + x*(-7296.43987145382
+ x*(8103.20462414788 + y_pt*(2175.341332000392
+ y_pt*(-274.2290036817964 + y_pt*(197.4670779425016
+ y_pt*(-68.5590309679152 + 9.98788038278032*y_pt))))
+ x*(-5458.34205214835 - 980.14153344888*y_pt
+ x*(2247.60742726704 - 340.1237483177863*x
+ 220.542973797483*y_pt))) + y_pt*(-1480.222530425046
+ y_pt*(-129.1994027934126 + y_pt*(-30.0682112585625
+ y_pt*(2.626801985426835 ))))) + y_pt*(1187.3715515697959
+ y_pt*(1760.062705994408 + y_pt*(-450.535298526802
+ y_pt*(182.8520895502518 + y_pt*(-43.3206481750622
+ 4.26033941694366*y_pt)))));
g_sa_mod = 0.5*gsw_sfac*g_sa_mod;
*ct_sa = (g_sa_mod - abs_pt*g_sa_t_mod)/gsw_cp0;
}
/*
!==========================================================================
elemental subroutine gsw_ct_first_derivatives_wrt_t_exact (sa, t, p, &
ct_sa_wrt_t, ct_t_wrt_t, ct_p_wrt_t)
!==========================================================================
!
! Calculates the following three derivatives of Conservative Temperature.
! These derivatives are done with respect to in-situ temperature t (in the
! case of CT_T_wrt_t) or at constant in-situ tempertature (in the cases of
! CT_SA_wrt_t and CT_P_wrt_t).
! (1) CT_SA_wrt_t, the derivative of CT with respect to Absolute Salinity
! at constant t and p, and
! (2) CT_T_wrt_t, derivative of CT with respect to in-situ temperature t
! at constant SA and p.
! (3) CT_P_wrt_t, derivative of CT with respect to pressure P (in Pa) at
! constant SA and t.
!
! This function uses the full Gibbs function. Note that this function
! avoids the NaN that would exist in CT_SA_wrt_t at SA = 0 if it were
! evaluated in the straightforward way from the derivatives of the Gibbs
! function function.
!
! SA = Absolute Salinity [ g/kg ]
! t = in-situ temperature (ITS-90) [ deg C ]
! p = sea pressure [ dbar ]
! ( i.e. absolute pressure - 10.1325 dbar)
!
! CT_SA_wrt_t = The first derivative of Conservative Temperature with