Skip to content

Commit 7471f2d

Browse files
committed
Continued scale conversion
1 parent 2d603b2 commit 7471f2d

File tree

6 files changed

+67
-15
lines changed

6 files changed

+67
-15
lines changed

PyCO2SYS/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# api,
2020
# bio,
2121
# buffers,
22-
# constants,
22+
constants,
2323
convert,
2424
engine,
2525
equilibria,

PyCO2SYS/convert.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
# PyCO2SYS: marine carbonate system calculations in Python.
22
# Copyright (C) 2020--2024 Matthew P. Humphreys et al. (GNU GPLv3)
3-
"""Convert units and calculate conversion factors."""
3+
"""
4+
PyCO2SYS.convert
5+
================
6+
Convert units and calculate conversion factors.
7+
8+
pH scale conversions
9+
--------------------
10+
There is a function for every variant of pH_<scale1>_to_<scale2>, where <scale1> and
11+
<scale2> are any of free, total, sws and nbs. Each function has a different set of
12+
arguments, depending on what is needed. For example, to get the conversion factor to
13+
go from the total to the NBS scale, use:
14+
15+
>>> factor = pyco2.convert.pH_total_to_nbs(
16+
total_fluoride, total_sulfate, k_HF_free, k_HSO4_free, fH
17+
)
418
19+
``factor`` should be multiplied by [H+] or K value(s) on the total scale to convert to
20+
the NBS scale.
21+
"""
522
import copy
623
from jax import numpy as np
724
from . import constants

PyCO2SYS/engine/system.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# PyCO2SYS: marine carbonate system calculations in Python.
22
# Copyright (C) 2020--2024 Matthew P. Humphreys et al. (GNU GPLv3)
3+
from operator import mul
34
import itertools
45
import networkx as nx
56
from jax import numpy as np
67
from matplotlib import pyplot as plt
7-
from . import convert, equilibria, salts
8+
from .. import constants, convert, equilibria, salts
89

910
# Define functions for calculations that depend neither on icase nor opts:
1011
get_funcs = {
@@ -27,7 +28,11 @@
2728
k_H2S_total_1atm * total_to_sws_1atm
2829
),
2930
# Pressure correction factors for equilibrium constants
31+
"factor_k_H2S": equilibria.pcx.factor_k_H2S,
3032
# Equilibrium constants at pressure and on the seawater pH scale
33+
"k_H2S_sws": lambda k_H2S_sws_1atm, factor_k_H2S: (
34+
k_H2S_sws_1atm, factor_k_H2S
35+
)
3136
# Equilibrium constants at pressure and on the requested pH scale (YES, HERE!)
3237
}
3338

@@ -48,6 +53,11 @@
4853
# Define functions for calculations that depend on opts:
4954
# (unlike in previous versions, each opt may only affect one parameter)
5055
get_funcs_opts = {}
56+
get_funcs_opts["opt_gas_constant"] = {
57+
1: dict(gas_constant=lambda: constants.RGasConstant_DOEv2),
58+
2: dict(gas_constant=lambda: constants.RGasConstant_DOEv3),
59+
3: dict(gas_constant=lambda: constants.RGasConstant_CODATA2018),
60+
}
5161
get_funcs_opts["opt_fH"] = {
5262
1: dict(fH=convert.fH_TWB82),
5363
2: dict(fH=convert.fH_PTBO87),
@@ -128,6 +138,7 @@
128138
}
129139

130140
default_opts = {
141+
"opt_gas_constant": 3,
131142
"opt_fH": 1,
132143
"opt_k_HF": 1,
133144
"opt_k_HSO4": 1,

PyCO2SYS/equilibria/p1atm.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
with the corrections of WMW14. Used when opt_k_HSO4 = 3.
2727
"""
2828
from jax import numpy as np
29+
from .. import convert
2930

3031

3132
def kCO2_W74(temperature, salinity):
@@ -68,7 +69,7 @@ def k_HSO4_free_D90a(temperature, salinity, ionic_strength):
6869
# TYPO on p. 121: the constant e9 should be e8.
6970
# Output KS is on the free pH scale in mol/kg-sw.
7071
# This is from eqs 22 and 23 on p. 123, and Table 4 on p 121:
71-
TempK = temperature + 273.15
72+
TempK = convert.celsius_to_kelvin(temperature)
7273
logTempK = np.log(TempK)
7374
lnk_HSO4 = (
7475
-4276.1 / TempK
@@ -137,7 +138,7 @@ def k_HSO4_free_WM13(temperature, salinity):
137138
float
138139
HSO4 dissociation constant.
139140
"""
140-
TempK = temperature + 273.15
141+
TempK = convert.celsius_to_kelvin(temperature)
141142
logKS0 = (
142143
562.69486
143144
- 102.5154 * np.log(TempK)
@@ -866,7 +867,7 @@ def k_H2S_total_YM95(temperature, salinity):
866867
# they agree with Millero 1988 which are on Total Scale.
867868
# Also, calculations agree at high H2S with AquaEnv when assuming it is on
868869
# Total Scale.
869-
TempK = temperature + 273.15
870+
TempK = convert.celsius_to_kelvin(temperature)
870871
lnkH2S = (
871872
225.838
872873
- 13275.3 / TempK

PyCO2SYS/equilibria/pcx.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,30 @@
66
from .. import convert
77

88

9-
def Kfac(deltaV, Kappa, Pbar, TempK, RGas):
10-
"""Calculate pressure correction factor for equilibrium constants."""
11-
return np.exp((-deltaV + 0.5 * Kappa * Pbar) * Pbar / (RGas * TempK))
9+
def pressure_factor(deltaV, kappa, pressure, temperature, gas_constant):
10+
"""Calculate pressure-correction factors for equilibrium constants.
11+
12+
Parameters
13+
----------
14+
deltaV : float
15+
The deltaV constant for the pressure correction.
16+
kappa : float
17+
The kappa constant for the pressure correction.
18+
pressure : float
19+
Hydrostatic pressure in dbar.
20+
temperature : float
21+
Temperature in °C.
22+
gas_constant : float
23+
The universal gas constant in ml / (bar * K * mol).
24+
25+
Returns
26+
-------
27+
float
28+
The correction factor, to be multiplied by the K value to correct it.
29+
"""
30+
Pbar = convert.decibar_to_bar(pressure)
31+
TempK = convert.celsius_to_kelvin(temperature)
32+
return np.exp((-deltaV + 0.5 * kappa * Pbar) * Pbar / (gas_constant * TempK))
1233

1334

1435
def KSO4fac(TempK, Pbar, RGas):
@@ -134,16 +155,15 @@ def KSifac(TempK, Pbar, RGas):
134155
return Kfac(deltaV, Kappa, Pbar, TempK, RGas)
135156

136157

137-
def KH2Sfac(TempK, Pbar, RGas):
138-
"""Calculate pressure correction factor for KH2S."""
158+
def factor_k_H2S(temperature, pressure, gas_constant):
159+
"""Calculate pressure correction factor for k_H2S."""
139160
# === CO2SYS.m comments: =======
140161
# Millero 1995 gives values for deltaV in fresh water instead of SW.
141162
# Millero 1995 gives -b0 as -2.89 instead of 2.89.
142163
# Millero 1983 is correct for both.
143-
TempC = convert.kelvin_to_celsius(TempK)
144-
deltaV = -11.07 - 0.009 * TempC - 0.000942 * TempC**2
145-
Kappa = (-2.89 + 0.054 * TempC) / 1000
146-
return Kfac(deltaV, Kappa, Pbar, TempK, RGas)
164+
deltaV = -11.07 - 0.009 * temperature - 0.000942 * temperature**2
165+
kappa = (-2.89 + 0.054 * temperature) / 1000
166+
return pressure_factor(deltaV, kappa, pressure, temperature, gas_constant)
147167

148168

149169
def KNH3fac(TempK, Pbar, RGas):

newclass_v2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import PyCO2SYS as pyco2
12
from PyCO2SYS import CO2System, system
23
import networkx as nx
34

@@ -15,6 +16,8 @@
1516
# "total_to_sws_1atm",
1617
# "nbs_to_sws",
1718
"k_H2S_sws_1atm",
19+
# 'gas_constant',
20+
"factor_k_H2S",
1821
)
1922
)
2023
sys.plot_graph(

0 commit comments

Comments
 (0)