-
Notifications
You must be signed in to change notification settings - Fork 17
/
cbsyst.py
950 lines (846 loc) · 28.1 KB
/
cbsyst.py
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
"""
Functions for calculating the carbon and boron chemistry of seawater.
"""
import numpy as np
import pandas as pd
from cbsyst.carbon import calc_C_species, calc_revelle_factor, pCO2_to_fCO2, fCO2_to_CO2
from cbsyst.boron import calc_B_species
from cbsyst.boron_isotopes import d11_to_A11, A11_to_d11, get_alphaB, calc_B_isotopes
from cbsyst.helpers import Bunch, NnotNone, calc_FT, calc_ST, calc_BT, calc_pH_scales
from kgen import calc_Ks
# C Speciation
# ------------
def Csys(
pHtot=None, DIC=None, TA=None,
CO2=None, HCO3=None, CO3=None,
pCO2=None, fCO2=None,
BT=None,
Ca=0.0102821, Mg=0.0528171,
T_in=25.0, T_out=None,
S_in=35.0, S_out=None,
P_in=0.0, P_out=None,
PT=0.0, SiT=0.0,
ST=None, FT=None,
pHsws=None, pHfree=None, pHNBS=None,
unit="umol", Ks=None,
pdict=None,
):
"""
Calculate the carbon chemistry of seawater from a minimal parameter set.
Constants calculated by MyAMI model (Hain et al, 2015; doi:10.1002/2014GB004986).
Speciation calculations from Zeebe & Wolf-Gladrow (2001; ISBN:9780444509468) Appendix B
Inputs must either be single values, arrays of equal length or a mixture of both.
If you use arrays of unequal length, it won't work.
Error propagation:
If inputs are ufloat or uarray (from uncertainties package) errors will
be propagated through all calculations, but:
**WARNING** Error propagation NOT IMPLEMENTED for carbon system calculations
with zero-finders (i.e. when pH is not given; cases 2-5 and 10-15).
Concentration Units
+++++++++++++++++++
* Ca and Mg must be in molar units.
* All other units must be the same, and can be specified in the 'unit' variable. Defaults to umolar.
Parameters
----------
pH, DIC, CO2, HCO3, CO3, TA : array-like
Carbon system parameters. Two of these must be provided.
BT : array-like
Total B at the input salinity used in Alkalinity calculations.
If missing, this is calculated as 0.000416 * Sal/35
(Uppstrom et al. 1974).
Ca, Mg : array-like
The [Ca] and [Mg] of STANDARD seawater (i.e. 35 salinity), in mol / kg.
Used in calculating MyAMI constants.
T_in, S_in : array-like
Temperature in Celcius and Salinity in PSU that the
measurements were conducted under.
Used in calculating constants.
P_in : array-like
Pressure in Bar that the measurements were conducted under.
Used in pressure-correcting constants.
T_out, S_out : array-like
Temperature in Celcius and Salinity in PSU of the desired
output conditions.
Used in calculating constants.
P_in : array-like
Pressure in Bar of the desired output conditions.
Used in pressure-correcting constants.
unit : str
Concentration units of C and B parameters (all must be in
the same units).
Can be 'mol', 'mmol', 'umol', 'nmol', 'pmol' or 'fmol'.
Used in calculating Alkalinity. Default is 'umol'.
Ks : dict
A dictionary of constants. Must contain keys
'K1', 'K2', 'KB' and 'KW'.
If None, Ks are calculated using MyAMI model.
pdict : dict
Optionally, you can provide some or all parameters as a dict,
with keys the same as the parameter names above. Any parameters
included in the dict will overwrite manually specified
parameters. This is particularly useful if you're including
this in other code.
Returns
-------
dict(/Bunch) containing all calculated parameters.
"""
# Bunch inputs
ps = Bunch(locals())
if isinstance(pdict, dict):
ps.update(pdict)
# convert unit to multiplier
udict = {
"mol": 1.0,
"mmol": 1.0e3,
"umol": 1.0e6,
"nmol": 1.0e9,
"pmol": 1.0e12,
"fmol": 1.0e15,
}
if isinstance(ps.unit, str):
ps.unit = udict[ps.unit]
upar = ["DIC", "TA", "CO2", "HCO3", "CO3", "BT", "PT", "SiT"]
if ps.unit != 1:
for p in upar:
if ps[p] is not None:
ps[p] = np.divide(ps[p], ps.unit) # convert to molar
gupar = ['pCO2', 'fCO2'] # special unit case: pCO2, fCO2 because pCO2 is always in ppm
for p in gupar:
if ps[p] is not None:
ps[p] = np.divide(ps[p], 1e6) # convert to mole fraction
# Conserved seawater chemistry
if ps.ST is None:
ps.ST = calc_ST(ps.S_in)
if ps.FT is None:
ps.FT = calc_FT(ps.S_in)
if ps.BT is None:
ps.BT = calc_BT(ps.S_in)
# Remove negative values
for p in ["DIC", "CO2", "HCO3", "CO3", "BT", "fCO2", "pCO2", "PT", "SiT"]:
if ps[p] is not None:
if isinstance(ps[p], (np.ndarray, pd.core.series.Series)):
ps[p][ps[p] < 0] = np.nan
elif ps[p] < 0:
ps[p] = np.nan
# Calculate Ks at input conditions
if isinstance(Ks, dict):
ps.Ks = Bunch(Ks)
else:
ps.Ks = Bunch(calc_Ks(temp_c=ps.T_in, sal=ps.S_in, p_bar=ps.P_in, magnesium=ps.Mg, calcium=ps.Ca, sulphate=ps.ST, fluorine=ps.FT))
# Calculate pH scales at input conditions (does nothing if no pH given)
ps.update(
calc_pH_scales(
pHtot=ps.pHtot,
pHfree=ps.pHfree,
pHsws=ps.pHsws,
pHNBS=ps.pHNBS,
ST=ps.ST,
FT=ps.FT,
TempK=ps.T_in + 273.15,
Sal=ps.S_in,
Ks=ps.Ks
)
)
# calculate C system at input conditions
ps.update(calc_C_species(**ps))
ps["revelle_factor"] = calc_revelle_factor(
TA=ps.TA,
DIC=ps.DIC,
BT=ps.BT,
PT=ps.PT,
SiT=ps.SiT,
ST=ps.ST,
FT=ps.FT,
Ks=ps.Ks,
)
# calc Omega
oCa = ps.Ca * ps.S_in / 35.
ps['OmegaA'] = ps['CO3'] * oCa / ps.Ks.KspA
ps['OmegaC'] = ps['CO3'] * oCa / ps.Ks.KspC
# clean up output
outputs = [
"BT", "CO2", "CO3", "Ca", "DIC", "H", "HCO3",
"Mg", "S_in", "T_in", "TA", "CAlk", "PAlk",
"SiAlk", "OH", 'OmegaA', 'OmegaC', 'revelle_factor']
for k in outputs:
if not isinstance(ps[k], np.ndarray):
# convert all outputs to (min) 1D numpy arrays.
ps[k] = np.array(ps[k], ndmin=1)
if ps.unit != 1:
for p in upar + ["CAlk", "BAlk", "PAlk", "SiAlk", "OH", "HSO4", "HF", "Hfree"]:
ps[p] *= ps.unit # convert back to input units
for p in gupar:
ps[p] = np.multiply(ps[p], 1e6) # convert to ppm
# Calculate Output Conditions
# ===========================
if ps.T_out is not None or ps.S_out is not None or ps.P_out is not None:
if ps.T_out is None:
ps.T_out = ps.T_in
if ps.S_out is None:
ps.S_out = ps.S_in
else:
# if salinity is modified, update salinity-dependent parameters
BT = ps.BT * ps.S_out / ps.S_in
ST = ps.ST * ps.S_out / ps.S_in
FT = ps.FT * ps.S_out / ps.S_in
if ps.P_out is None:
ps.P_out = ps.P_in
# assumes conserved alkalinity and DIC
# This needs to be different depending on whether T or S is changing.
out_cond = Csys(
TA=ps.TA,
DIC=ps.DIC,
T_in=ps.T_out,
S_in=ps.S_out,
P_in=ps.P_out,
unit=ps.unit,
Ca=Ca,
Mg=Mg,
BT=BT,
FT=FT,
ST=ST,
)
# rename parameters in output conditions
outputs = [
"BAlk", "BT", "CAlk", "CO2", "CO3", "DIC", "H", "HCO3",
"HF", "HSO4", "Hfree", "Ks", "OH", "PAlk", "SiAlk", "TA", "FT",
"PT", "ST", "SiT", "fCO2", "pCO2", "pHfree", "pHsws",
"pHtot", "pHNBS", 'OmegaA', 'OmegaC', "revelle_factor",
]
ps.update({k + "_in": ps[k] for k in outputs})
ps.update({k: out_cond[k] for k in outputs})
# remove some superfluous outputs
rem = ["pdict"]
for r in rem:
if r in ps:
del ps[r]
return ps
# B Speciation
# ------------
def Bsys(
pHtot=None,
BT=None,
BO3=None,
BO4=None,
ABT=None,
ABO3=None,
ABO4=None,
dBT=None,
dBO3=None,
dBO4=None,
alphaB=None,
T_in=25.0,
S_in=35.0,
P_in=0.0,
Ca=0.0102821,
Mg=0.0528171,
ST=None,
FT=None,
pHsws=None,
pHfree=None,
pHNBS=None,
Ks=None,
pdict=None,
):
"""
Calculate the boron chemistry of seawater from a minimal parameter set.
Constants calculated by MyAMI model (Hain et al, 2015; doi:10.1002/2014GB004986).
Speciation calculations from Zeebe & Wolf-Gladrow (2001; ISBN:9780444509468).
Inputs must either be single values, arrays of equal length or a mixture of both.
If you use arrays of unequal length, it won't work.
Error propagation:
If inputs are ufloat or uarray (from uncertainties package) errors will
be propagated through all calculations.
Concentration Units
+++++++++++++++++++
* All concentrations must be in the same units. Returned in the same units as inputs.
Parameters
----------
pH, BT, BO3, BO4 : array-like
Boron system parameters. Two of these must be provided.
dBT, dBO3, dBO4, ABT, ABO3, ABO4 : array-like
delta (d) or fractional abundance (A) values for the Boron
isotope system. One of these must be provided.
alphaB : array-like
The alpha value for BO3-BO4 isotope fractionation.
T, S : array-like
Temperature in Celcius and Salinity in PSU.
Used in calculating MyAMI constants.
P : array-like
Pressure in Bar.
Used in calculating MyAMI constants.
Ca, Mg : arra-like
The [Ca] and [Mg] of STANDARD seawater (i.e. 35 salinity), in mol / kg.
Used in calculating MyAMI constants.
Ks : dict
A dictionary of constants. Must contain keys
'K1', 'K2', 'KB' and 'KW'.
If None, Ks are calculated using MyAMI model.
pdict : dict
Optionally, you can provide some or all parameters as a dict,
with keys the same as the parameter names above. Any parameters
included in the dict will overwrite manually specified
parameters. This is particularly useful if you're including
this in other code.
Returns
-------
dict(/Bunch) containing all calculated parameters.
"""
# input checks
if NnotNone(BT, BO3, BO4) < 1:
raise ValueError("Must provide at least one of BT, BO3 or BO4")
if NnotNone(dBT, dBO3, dBO4, ABT, ABO3, ABO4) < 1:
raise ValueError("Must provide one of dBT, dBO3, dBO4, ABT, ABO3 or ABO4")
# Bunch inputs
ps = Bunch(locals())
if isinstance(pdict, dict):
ps.update(pdict)
# Conserved seawater chemistry
if ps.ST is None:
ps.ST = calc_ST(ps.S_in)
if ps.FT is None:
ps.FT = calc_FT(ps.S_in)
# Remove negative values
for p in ["BT", "BO3", "BO4", "ST", "FT"]:
if ps[p] is not None:
if isinstance(ps[p], (np.ndarray, pd.core.series.Series)):
ps[p][ps[p] < 0] = np.nan
elif ps[p] < 0:
ps[p] = np.nan
# Calculate Ks
ps.Ks = Bunch(calc_Ks(temp_c=ps.T_in, sal=ps.S_in, p_bar=ps.P_in, magnesium=ps.Mg, calcium=ps.Ca, sulphate=ps.ST, fluorine=ps.FT))
# Calculate pH scales (does nothing if no pH given)
ps.update(
calc_pH_scales(
pHtot=ps.pHtot,
pHfree=ps.pHfree,
pHsws=ps.pHsws,
pHNBS=ps.pHNBS,
ST=ps.ST,
FT=ps.FT,
TempK=ps.T_in + 273.15,
Sal=ps.S_in,
Ks=ps.Ks,
)
)
# calcualte pH if not provided.
if ps.pHtot is None:
if ps.dBT is None and ps.ABT is None:
ps.dBT = 39.61
if ps.dBT is not None:
ps.ABT = d11_to_A11(ps.dBT)
if ps.dBO3 is not None:
ps.ABO3 = d11_to_A11(ps.dBO3)
if ps.dBO4 is not None:
ps.ABO4 = d11_to_A11(ps.dBO4)
if ps.alphaB is None:
ps.alphaB = get_alphaB()
ps.update(calc_B_isotopes(**ps))
ps.update(calc_B_species(**ps))
# If pH not calced yet, calculate on all scales (does nothing if all pH scales already calculated)
ps.update(
calc_pH_scales(
pHtot=ps.pHtot,
pHfree=ps.pHfree,
pHsws=ps.pHsws,
pHNBS=ps.pHNBS,
ST=ps.ST,
FT=ps.FT,
TempK=ps.T_in + 273.15,
Sal=ps.S_in,
Ks=ps.Ks,
)
)
# If any isotope parameter specified, calculate the isotope systen.
if NnotNone(ps.ABT, ps.ABO3, ps.ABO4, ps.dBT, ps.dBO3, ps.dBO4) != 0:
ps.update(ABsys(pdict=ps))
for k in ["BT", "H", "BO3", "BO4", "Ca", "Mg", "S_in", "T_in"]:
# convert all outputs to (min) 1D numpy arrays.
if not isinstance(ps[k], np.ndarray):
# convert all outputs to (min) 1D numpy arrays.
ps[k] = np.array(ps[k], ndmin=1)
# remove some superfluous outputs
rem = ["pdict"]
for r in rem:
if r in ps:
del ps[r]
return ps
# B Isotopes
# ----------
def ABsys(
pHtot=None,
ABT=None,
ABO3=None,
ABO4=None,
dBT=None,
dBO3=None,
dBO4=None,
alphaB=None,
T_in=25.0,
S_in=35.0,
P_in=0.0,
Ca=0.0102821,
Mg=0.0528171,
ST=None,
FT=None,
pHsws=None,
pHfree=None,
pHNBS=None,
Ks=None,
pdict=None,
):
"""
Calculate the boron isotope chemistry of seawater from a minimal parameter set.
Constants calculated by MyAMI model (Hain et al, 2015; doi:10.1002/2014GB004986).
Speciation calculations from Zeebe & Wolf-Gladrow (2001; ISBN:9780444509468).
Inputs must either be single values, arrays of equal length or a mixture of both.
If you use arrays of unequal length, it won't work.
Error propagation:
If inputs are ufloat or uarray (from uncertainties package) errors will
be propagated through all calculations.
Concentration Units
+++++++++++++++++++
* 'A' is fractional abundance (11B / BT)
* 'd' are delta values
Either specified, both returned.
Parameters
----------
pH, ABT, ABO3, ABO4, dBT, dBO3, dBO4 : array-like
Boron isotope system parameters. Two of pH, {ABT, dBT},
{ABO4, dBO4}, {ABO3, dBO3} must be provided.
alphaB : array-like
Alpha value describing B fractionation (1.0XXX).
If missing, it's calculated using the temperature
sensitive formulation of Honisch et al (2008)
T, S : array-like
Temperature in Celcius and Salinity in PSU.
Used in calculating MyAMI constants.
P : array-like
Pressure in Bar.
Used in calculating MyAMI constants.
Ca, Mg : arra-like
The [Ca] and [Mg] of STANDARD seawater (i.e. 35 salinity), in mol / kg.
Used in calculating MyAMI constants.
Ks : dict
A dictionary of constants. Must contain keys
'K1', 'K2', 'KB' and 'KW'.
If None, Ks are calculated using MyAMI model.
pdict : dict
Optionally, you can provide some or all parameters as a dict,
with keys the same as the parameter names above. Any parameters
included in the dict will overwrite manually specified
parameters. This is particularly useful if you're including
this in other code.
Returns
-------
dict(/Bunch) containing all calculated parameters.
"""
# Bunch inputs
ps = Bunch(locals())
if isinstance(pdict, dict):
ps.update(pdict)
# Conserved seawater chemistry
if ps.ST is None:
ps.ST = calc_ST(ps.S_in)
if ps.FT is None:
ps.FT = calc_FT(ps.S_in)
# Calculate Ks
if isinstance(Ks, dict):
ps.Ks = Bunch(Ks)
else:
ps.Ks = Bunch(calc_Ks(temp_c=ps.T_in, sal=ps.S_in, p_bar=ps.P_in, magnesium=ps.Mg, calcium=ps.Ca, sulphate=ps.ST, fluorine=ps.FT))
# Calculate pH scales (does nothing if no pH given)
ps.update(
calc_pH_scales(
pHtot=ps.pHtot,
pHfree=ps.pHfree,
pHsws=ps.pHsws,
pHNBS=ps.pHNBS,
ST=ps.ST,
FT=ps.FT,
TempK=ps.T_in + 273.15,
Sal=ps.S_in,
Ks=ps.Ks,
)
)
# if deltas provided, calculate corresponding As
if ps.dBT is not None:
ps.ABT = d11_to_A11(ps.dBT)
if ps.dBO3 is not None:
ps.ABO3 = d11_to_A11(ps.dBO3)
if ps.dBO4 is not None:
ps.ABO4 = d11_to_A11(ps.dBO4)
# calculate alpha
if alphaB is None:
ps.alphaB = get_alphaB()
else:
ps.alphaB = alphaB
ps.update(calc_B_isotopes(**ps))
if ps.dBT is None:
ps.dBT = A11_to_d11(ps.ABT)
if ps.dBO3 is None:
ps.dBO3 = A11_to_d11(ps.ABO3)
if ps.dBO4 is None:
ps.dBO4 = A11_to_d11(ps.ABO4)
for k in [
"ABO3",
"ABO4",
"ABT",
"Ca",
"H",
"Mg",
"S_in",
"T_in",
"alphaB",
"dBO3",
"dBO4",
"dBT",
"pHtot",
]:
if not isinstance(ps[k], np.ndarray):
# convert all outputs to (min) 1D numpy arrays.
ps[k] = np.array(ps[k], ndmin=1)
# remove some superfluous outputs
rem = ["pdict"]
for r in rem:
if r in ps:
del ps[r]
return ps
# Whole C-B-Isotope System
# ------------------------
def CBsys(
pHtot=None,
DIC=None,
CO2=None,
HCO3=None,
CO3=None,
TA=None,
fCO2=None,
pCO2=None,
BT=None,
BO3=None,
BO4=None,
ABT=None,
ABO3=None,
ABO4=None,
dBT=None,
dBO3=None,
dBO4=None,
alphaB=None,
T_in=25.0,
S_in=35.0,
P_in=0.0,
T_out=None,
S_out=None,
P_out=None,
Ca=0.0102821,
Mg=0.0528171,
PT=0.0,
SiT=0.0,
ST=None,
FT=None,
pHsws=None,
pHfree=None,
pHNBS=None,
Ks=None,
pdict=None,
unit="umol",
):
"""
Calculate carbon, boron and boron isotope chemistry of seawater from a minimal parameter set.
Constants calculated by MyAMI model (Hain et al, 2015; doi:10.1002/2014GB004986).
Speciation calculations from Zeebe & Wolf-Gladrow (2001; ISBN:9780444509468) Appendix B
Inputs must either be single values, arrays of equal length or a mixture of both.
If you use arrays of unequal length, it won't work.
Note: Special Case! If pH is not known, you must provide either:
- Two of [DIC, CO2, HCO3, CO3], and one of [BT, BO3, BO4]
- One of [DIC, CO2, HCO3, CO3], and TA and BT
- Two of [BT, BO3, BO4] and one of [DIC, CO2, HCO3, CO3]
Isotopes will only be calculated if one of [ABT, ABO3, ABO4, dBT, dBO3, dBO4]
is provided.
Error propagation:
If inputs are ufloat or uarray (from uncertainties package) errors will
be propagated through all calculations, but:
**WARNING** Error propagation NOT IMPLEMENTED for carbon system calculations
with zero-finders (i.e. when pH is not given; cases 2-5 and 10-15).
Concentration Units
+++++++++++++++++++
* Ca and Mg must be in molar units.
* All other units must be the same, and can be specified in the 'unit' variable. Defaults to umolar.
* Isotopes can be in A (11B / BT) or d (delta). Either specified, both returned.
Parameters
----------
pH, DIC, CO2, HCO3, CO3, TA : array-like
Carbon system parameters. Two of these must be provided.
If TA is specified, a B species must also be specified.
pH, BT, BO3, BO4 : array-like
Boron system parameters. Two of these must be provided.
pH, ABT, ABO3, ABO4, dBT, dBO3, dBO4 : array-like
Boron isotope system parameters. pH and one other
parameter must be provided.
alphaB : array-like
Alpha value describing B fractionation (1.0XXX).
If missing, it's calculated using the temperature
sensitive formulation of Honisch et al (2008)
T, S : array-like
Temperature in Celcius and Salinity in PSU.
Used in calculating MyAMI constants.
P : array-like
Pressure in Bar.
Used in calculating MyAMI constants.
unit : str
Concentration units of C and B parameters (all must be in
the same units).
Can be 'mol', 'mmol', 'umol', 'nmol', 'pmol' or 'fmol'.
Used in calculating Alkalinity. Default is 'umol'.
Ca, Mg : arra-like
The [Ca] and [Mg] of STANDARD seawater (i.e. 35 salinity), in mol / kg.
Used in calculating MyAMI constants.
Ks : dict
A dictionary of constants. Must contain keys
'K1', 'K2', 'KB' and 'KW'.
If None, Ks are calculated using MyAMI model.
pdict : dict
Optionally, you can provide some or all parameters as a dict,
with keys the same as the parameter names above. Any parameters
included in the dict will overwrite manually specified
parameters. This is particularly useful if you're including
this in other code.
Returns
-------
dict(/Bunch) containing all calculated parameters.
"""
# Bunch inputs
ps = Bunch(locals())
if isinstance(pdict, dict):
ps.update(pdict)
# convert unit to multiplier
udict = {
"mol": 1.0,
"mmol": 1.0e3,
"umol": 1.0e6,
"nmol": 1.0e9,
"pmol": 1.0e12,
"fmol": 1.0e15,
}
if isinstance(ps.unit, str):
ps.unit = udict[ps.unit]
elif isinstance(ps.unit, (int, float)):
ps.unit = unit
upar = [
"DIC",
"CO2",
"HCO3",
"CO3",
"TA",
"BT",
"BO3",
"BO4",
"PT",
"SiT",
]
for p in upar:
if ps[p] is not None:
ps[p] = np.divide(ps[p], ps.unit) # convert to molar
gupar = ['pCO2', 'fCO2'] # special unit case: pCO2, fCO2 because pCO2 is always in ppm
for p in gupar:
if ps[p] is not None:
ps[p] = np.divide(ps[p], 1e6) # convert to mole fraction
# Conserved seawater chemistry
if ps.ST is None:
ps.ST = calc_ST(ps.S_in)
if ps.FT is None:
ps.FT = calc_FT(ps.S_in)
# Remove negative values
for p in ["DIC", "CO2", "HCO3", "CO3", "BT", "BO3", "BO4", "fCO2", "pCO2", "PT", "SiT"]:
if ps[p] is not None:
if isinstance(ps[p], (np.ndarray, pd.core.series.Series)):
ps[p][ps[p] < 0] = np.nan
elif ps[p] < 0:
ps[p] = np.nan
# Calculate Ks
if isinstance(Ks, dict):
ps.Ks = Bunch(Ks)
else:
ps.Ks = Bunch(calc_Ks(temp_c=ps.T_in, sal=ps.S_in, p_bar=ps.P_in, magnesium=ps.Mg, calcium=ps.Ca, sulphate=ps.ST, fluorine=ps.FT))
# calculate alpha
if alphaB is None:
ps.alphaB = get_alphaB()
else:
ps.alphaB = alphaB
# convert any B isotopes to A notation
if ps.dBT is None and ps.ABT is None:
ps.dBT = 39.61
if ps.dBT is not None:
ps.ABT = d11_to_A11(ps.dBT)
if ps.dBO3 is not None:
ps.ABO3 = d11_to_A11(ps.dBO3)
if ps.dBO4 is not None:
ps.ABO4 = d11_to_A11(ps.dBO4)
nBiso = NnotNone(ps.ABT) + NnotNone(ps.ABO4, ps.ABO3)
# Calculate all pH scales (does nothing if no pH given)
ps.update(
calc_pH_scales(
pHtot=ps.pHtot,
pHfree=ps.pHfree,
pHsws=ps.pHsws,
pHNBS=ps.pHNBS,
ST=ps.ST,
FT=ps.FT,
TempK=ps.T_in + 273.15,
Sal=ps.S_in,
Ks=ps.Ks,
)
)
# if fCO2 is given but CO2 is not, calculate CO2
if ps.CO2 is None:
if ps.fCO2 is not None:
ps.CO2 = fCO2_to_CO2(ps.fCO2, ps.Ks)
elif ps.pCO2 is not None:
ps.CO2 = fCO2_to_CO2(pCO2_to_fCO2(ps.pCO2, ps.T_in), ps.Ks)
# if no B info provided, assume modern conc.
nBspec = NnotNone(ps.BT, ps.BO3, ps.BO4)
if nBspec == 0:
ps.BT = calc_BT(ps.S_in)
# count number of not None C parameters
nCspec = NnotNone(ps.DIC, ps.CO2, ps.HCO3, ps.CO3) # used below
# if pH or two B species are given:
if ps.pHtot is not None or nBspec == 2:
ps.update(calc_B_species(**ps))
ps.update(calc_C_species(**ps))
ps.update(calc_B_isotopes(**ps))
# if pH can be calculated from B isotopes
elif nBiso == 2:
ps.update(calc_B_isotopes(**ps))
ps.update(calc_B_species(**ps))
ps.update(calc_C_species(**ps))
# if ther eare two carbon species, or one carbon species + TA and BT
elif (nCspec == 2) | ((nCspec == 1) & (NnotNone(ps.TA, ps.BT) == 2)):
ps.update(calc_C_species(**ps))
ps.update(calc_B_species(**ps))
ps.update(calc_B_isotopes(**ps))
else: # if neither condition is met, throw an error
raise ValueError(
(
"Impossible! You haven't provided enough information.\n"
+ "If you don't know pH, you must provide either:\n"
+ " - Two of [DIC, CO2, HCO3, CO3] and BT\n"
+ " - One of [DIC, CO2, HCO3, CO3], and TA and BT\n"
+ " - Two of [BT, BO3, BO4] and one of [DIC, CO2, HCO3, CO3]"
+ " - Two of [dBT, dBO3, dBO4] and one of [DIC, CO2, HCO3, CO3]"
)
)
# convert isotopes to delta notation
if ps.dBT is None:
ps.dBT = A11_to_d11(ps.ABT)
if ps.dBO3 is None:
ps.dBO3 = A11_to_d11(ps.ABO3)
if ps.dBO4 is None:
ps.dBO4 = A11_to_d11(ps.ABO4)
ps["revelle_factor"] = calc_revelle_factor(
TA=ps.TA,
DIC=ps.DIC,
BT=ps.BT,
PT=ps.PT,
SiT=ps.SiT,
ST=ps.ST,
FT=ps.FT,
Ks=ps.Ks,
)
# calc Omega
if Ca is None:
oCa = 0.0102821 * ps.S_in / 35.
else:
oCa = Ca * ps.S_in / 35.
ps['OmegaA'] = ps['CO3'] * oCa / ps.Ks.KspA
ps['OmegaC'] = ps['CO3'] * oCa / ps.Ks.KspC
# clean up output
outputs = [
"BAlk",
"BT",
"CAlk",
"CO2",
"CO3",
"DIC",
"H",
"HCO3",
"HF",
"HSO4",
"Hfree",
"Ks",
"OH",
"PAlk",
"SiAlk",
"TA",
"FT",
"PT",
"ST",
"SiT",
"fCO2",
"pCO2",
"pHfree",
"pHsws",
"pHtot",
"pHNBS",
'OmegaA',
'OmegaC',
'revelle_factor',
"BO3",
"BO4",
"ABO3",
"ABO4",
"dBO3",
"dBO4",
]
for k in outputs:
if k == 'Ks':
continue
if not isinstance(ps[k], np.ndarray):
# convert all outputs to (min) 1D numpy arrays.
ps[k] = np.array(ps[k], ndmin=1)
# Handle Units - at this point everything is in mol/kg-SW
for p in upar + ["CAlk", "BAlk", "PAlk", "SiAlk", "OH", "HSO4", "HF", "Hfree"]:
ps[p] *= ps.unit # convert back to input units
for p in gupar:
if ps[p] is not None:
ps[p] = np.multiply(ps[p], 1e6) # convert to ppm
# Calculate Output Conditions
# ===========================
# Recursive approach to calculate output params.
# if output conditions specified, calculate outputs.
if ps.T_out is not None or ps.S_out is not None or ps.P_out is not None:
if ps.T_out is None:
ps.T_out = ps.T_in
if ps.S_out is None:
ps.S_out = ps.S_in
else:
# if salinity is modified, update salinity-dependent parameters
BT = ps.BT * ps.S_out / ps.S_in
ST = ps.ST * ps.S_out / ps.S_in
FT = ps.FT * ps.S_out / ps.S_in
if ps.P_out is None:
ps.P_out = ps.P_in
# assumes conserved alkalinity, DIC and BT
out_cond = CBsys(
TA=ps.TA,
DIC=ps.DIC,
dBT=ps.dBT,
T_in=ps.T_out,
S_in=ps.S_out,
P_in=ps.P_out,
unit=ps.unit,
Ca=Ca,
Mg=Mg,
BT=BT,
FT=FT,
ST=ST,
)
# rename parameters in output conditions
ps.update({k + "_in": ps[k] for k in outputs})
ps.update({k: out_cond[k] for k in outputs})
# remove some superfluous outputs
rem = ["pdict", "unit"]
for r in rem:
if r in ps:
del ps[r]
return ps