Skip to content

Commit

Permalink
Add different temperature conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdh7 committed Jan 22, 2024
1 parent b7c66b7 commit a569636
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 26 deletions.
18 changes: 1 addition & 17 deletions PyCO2SYS/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,9 @@
solubility,
solve,
uncertainty,
upsilon,
)

__all__ = [
"api",
"bio",
"buffers",
"constants",
"convert",
"engine",
"equilibria",
"gas",
"meta",
"minimal",
"original",
"salts",
"solubility",
"solve",
"uncertainty",
]
__author__ = meta.authors
__version__ = meta.version

Expand Down
112 changes: 103 additions & 9 deletions PyCO2SYS/engine/nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import itertools
from autograd import numpy as np
from .. import convert, equilibria, salts, solve, uncertainty
from .. import convert, equilibria, salts, solve, uncertainty, upsilon

# Define function input keys that should be converted to floats
input_floats = {
Expand Down Expand Up @@ -69,6 +69,8 @@
# Added in v1.8.0:
"pressure_atmosphere_out",
"pressure_atmosphere",
# Added in v1.8.3:
"bh_upsilon",
}


Expand Down Expand Up @@ -300,6 +302,9 @@ def _get_results_dict(
"pressure_atmosphere_out": args["pressure_atmosphere_out"],
# Added in v1.8.3 (but should have been in v1.8.2):
"opt_pressured_kCO2": args["opt_pressured_kCO2"],
# Also added in v1.8.3:
"opt_adjust_temperature": args["opt_adjust_temperature"],
"bh_upsilon": args["bh_upsilon"],
}
)
results.update(_get_in_out(core_in, others_in, k_constants_in, suffix=""))
Expand Down Expand Up @@ -499,6 +504,7 @@ def _get_results_dict(
"dlnfCO2_dT",
"dlnpCO2_dT_out",
"dlnpCO2_dT",
"bh_upsilon",
]


Expand Down Expand Up @@ -580,6 +586,9 @@ def CO2SYS(
pressure_atmosphere_out=1.0, # atm
# Added in v1.8.2:
opt_pressured_kCO2=0,
# Added in v1.8.3:
opt_adjust_temperature=1,
bh_upsilon=28995.0, # J / mol
):
"""Run CO2SYS with n-dimensional args allowed."""
args = locals()
Expand Down Expand Up @@ -724,7 +733,8 @@ def CO2SYS(
"pHN": pH_nbs,
}
)
# One of pCO2, fCO2, CO2(aq) or xCO2 only
# One of pCO2, fCO2, CO2(aq) or xCO2 only --- at this point, just inter-convert
# them all into each other
if np.any(np.isin(args["par1_type"], [4, 5, 8, 9])):
fCO2 = (
np.where(
Expand Down Expand Up @@ -847,18 +857,102 @@ def CO2SYS(
)
others_out["dlnfCO2_dT"] = solve.get_dlnfCO2_dT(*dln_args)
others_out["dlnpCO2_dT"] = solve.get_dlnpCO2_dT(*dln_args)

elif par1 is not None and par2 is None:
core_out = {}
others_out = {}
# One of pCO2, fCO2, CO2(aq) or xCO2 only
if np.any(np.isin(args["par1_type"], [4, 5, 8, 9])):
# Takahashi et al. (2009) DSR2 Eq. 2
core_out["PC"] = core_in["PC"] * np.exp(
0.0433 * (temperature_out - temperature)
- 4.35e-5 * (temperature_out**2 - temperature**2)
)
core_out["FC"] = convert.pCO2_to_fCO2(core_out["PC"], k_constants_out)
core_out["FC"] = core_in["FC"] * 1
core_out["PC"] = core_in["PC"] * 1
# H24 adjustments are applied to fCO2
L = args["opt_adjust_temperature"] == 1
if np.any(L):
core_out["FC"] = np.where(
L,
core_in["FC"]
* upsilon.expUps_parameterised_H24(
args["temperature"],
args["temperature_out"],
args["salinity"],
core_in["FC"],
k_constants_in["RGas"],
),
core_out["FC"],
)
L = args["opt_adjust_temperature"] == 2
if np.any(L):
core_out["FC"] = np.where(
L,
core_in["FC"]
* upsilon.expUps_TOG93_H24(
args["temperature"],
args["temperature_out"],
k_constants_in["RGas"],
),
core_out["FC"],
)
L = args["opt_adjust_temperature"] == 3
if np.any(L):
core_out["FC"] = np.where(
L,
core_in["FC"]
* upsilon.expUps_enthalpy_H24(
args["temperature"],
args["temperature_out"],
k_constants_in["RGas"],
),
core_out["FC"],
)
L = args["opt_adjust_temperature"] == 4
if np.any(L):
core_out["FC"] = np.where(
L,
core_in["FC"]
* upsilon.expUps_Hoff_H24(
args["temperature"],
args["temperature_out"],
k_constants_in["RGas"],
args["bh_upsilon"],
),
core_out["FC"],
)
L = np.isin(args["opt_adjust_temperature"], [1, 2, 3, 4])
if np.any(L):
core_out["PC"] = np.where(
L,
convert.fCO2_to_pCO2(core_out["FC"], k_constants_out),
core_out["PC"],
)
# TOG93 adustments are applied to pCO2
L = args["opt_adjust_temperature"] == 5
if np.any(L):
core_out["PC"] = np.where(
L,
core_in["PC"]
* upsilon.expUps_linear_TOG93(
args["temperature"],
args["temperature_out"],
),
core_out["PC"],
)
L = args["opt_adjust_temperature"] == 6
if np.any(L):
core_out["PC"] = np.where(
L,
core_in["PC"]
* upsilon.expUps_quadratic_TOG93(
args["temperature"],
args["temperature_out"],
),
core_out["PC"],
)
L = np.isin(args["opt_adjust_temperature"], [5, 6])
if np.any(L):
core_out["FC"] = np.where(
L,
convert.pCO2_to_fCO2(core_out["PC"], k_constants_out),
core_out["FC"],
)
core_out["CO2"] = convert.fCO2_to_CO2aq(core_out["FC"], k_constants_out)
core_out["XC"] = convert.fCO2_to_xCO2(core_out["FC"], k_constants_out)
else:
Expand Down
Loading

0 comments on commit a569636

Please sign in to comment.