diff --git a/PyCO2SYS/api/__init__.py b/PyCO2SYS/api/__init__.py index 87413c64..e6412617 100644 --- a/PyCO2SYS/api/__init__.py +++ b/PyCO2SYS/api/__init__.py @@ -2,8 +2,8 @@ # Copyright (C) 2020--2024 Matthew P. Humphreys et al. (GNU GPLv3) """Alternative APIs for executing the main CO2SYS function.""" +from ..engine import _CO2SYS, CO2SYS from . import ezio -from ..engine import CO2SYS, _CO2SYS def CO2SYS_wrap( @@ -114,9 +114,10 @@ def CO2SYS_wrap( system parameters. Note that output variables are labelled as the original CO2SYS output names, and not the wrapper inputs. """ - from autograd import numpy as np import inspect + import pandas as pd + from autograd import numpy as np try: import xarray as xr @@ -177,8 +178,9 @@ def printv(*args, **kwargs): df = pd.DataFrame(params, index=np.arange(max(sizes))) except ValueError: raise UserWarning( - "Your inputs must be length of 1 or n (sizes shown below)" - ":\n {}".format(str(sizes)) + "Your inputs must be length of 1 or n (sizes shown below)" ":\n {}".format( + str(sizes) + ) ) # DEFINE PARAMETER TYPES diff --git a/PyCO2SYS/buffers.py b/PyCO2SYS/buffers.py index e4c021a7..a887a320 100755 --- a/PyCO2SYS/buffers.py +++ b/PyCO2SYS/buffers.py @@ -2,8 +2,8 @@ # Copyright (C) 2020--2024 Matthew P. Humphreys et al. (GNU GPLv3) """Calculate various buffer factors of the marine carbonate system.""" -import jax from jax import numpy as np + from . import solubility, solve from .meta import egrad diff --git a/PyCO2SYS/constants.py b/PyCO2SYS/constants.py index 0eaf6e7c..c11044af 100644 --- a/PyCO2SYS/constants.py +++ b/PyCO2SYS/constants.py @@ -4,7 +4,6 @@ from autograd import numpy as np - RGasConstant_DOEv2 = 83.1451 # ml bar-1 K-1 mol-1, DOEv2 (always used by default) RGasConstant_DOEv3 = 83.14472 # ml bar-1 K-1 mol-1, DOEv3 (never used in PyCO2SYS) RGasConstant_CODATA2018 = 83.14462618 # 10^-1 J mol^-1 K^-1 (available from v1.4.1) diff --git a/PyCO2SYS/convert.py b/PyCO2SYS/convert.py index 0234af6f..6c4465a2 100644 --- a/PyCO2SYS/convert.py +++ b/PyCO2SYS/convert.py @@ -26,8 +26,11 @@ fCO2_to_CO2aq Convert CO2 fugacity to aqueous content. """ + import copy + from jax import numpy as np + from . import constants diff --git a/PyCO2SYS/engine/__init__.py b/PyCO2SYS/engine/__init__.py index f7562e16..e7240685 100644 --- a/PyCO2SYS/engine/__init__.py +++ b/PyCO2SYS/engine/__init__.py @@ -3,6 +3,7 @@ """Helpers for the main CO2SYS program.""" from autograd import numpy as np + from .. import convert, equilibria, salts, solve from . import nd, system diff --git a/PyCO2SYS/engine/nd.py b/PyCO2SYS/engine/nd.py index d30de16d..7bc10590 100644 --- a/PyCO2SYS/engine/nd.py +++ b/PyCO2SYS/engine/nd.py @@ -3,7 +3,9 @@ """Carbonate system solving in N dimensions.""" import itertools + from autograd import numpy as np + from .. import convert, equilibria, salts, solve, uncertainty, upsilon # Define function input keys that should be converted to floats diff --git a/PyCO2SYS/engine/system.py b/PyCO2SYS/engine/system.py index 1d46e3ee..94e48002 100644 --- a/PyCO2SYS/engine/system.py +++ b/PyCO2SYS/engine/system.py @@ -1,10 +1,12 @@ # PyCO2SYS: marine carbonate system calculations in Python. # Copyright (C) 2020--2024 Matthew P. Humphreys et al. (GNU GPLv3) import itertools + import networkx as nx -from jax import numpy as np import numpy as onp +from jax import numpy as np from matplotlib import pyplot as plt + from .. import ( bio, buffers, @@ -29,11 +31,17 @@ "k_CO2_1atm": equilibria.p1atm.k_CO2_W74, "k_H2S_total_1atm": equilibria.p1atm.k_H2S_total_YM95, # pH scale conversion factors at 1 atm - "free_to_sws_1atm": lambda total_fluoride, total_sulfate, k_HF_free_1atm, k_HSO4_free_1atm: convert.pH_free_to_sws( + "free_to_sws_1atm": lambda total_fluoride, + total_sulfate, + k_HF_free_1atm, + k_HSO4_free_1atm: convert.pH_free_to_sws( total_fluoride, total_sulfate, k_HF_free_1atm, k_HSO4_free_1atm ), "nbs_to_sws": convert.pH_nbs_to_sws, # because fH doesn't get pressure-corrected - "tot_to_sws_1atm": lambda total_fluoride, total_sulfate, k_HF_free_1atm, k_HSO4_free_1atm: convert.pH_tot_to_sws( + "tot_to_sws_1atm": lambda total_fluoride, + total_sulfate, + k_HF_free_1atm, + k_HSO4_free_1atm: convert.pH_tot_to_sws( total_fluoride, total_sulfate, k_HF_free_1atm, k_HSO4_free_1atm ), # Equilibrium constants at 1 atm and on the seawater pH scale @@ -491,7 +499,7 @@ ), 2: dict( k_BOH3_nbs_1atm=equilibria.p1atm.k_BOH3_nbs_LTB69, - k_BOH3_sws_1atm=lambda k_BOH3_total_1atm, nbs_to_sws: ( + k_BOH3_sws_1atm=lambda k_BOH3_nbs_1atm, nbs_to_sws: ( k_BOH3_nbs_1atm * nbs_to_sws ), ), @@ -902,7 +910,6 @@ def _assemble(self, icase, values): return graph, funcs, values def _get(self, parameters, graph, funcs, values, save_steps, verbose): - def printv(*args, **kwargs): if verbose: print(*args, **kwargs) @@ -1252,7 +1259,7 @@ def get_grad(self, var_of, var_wrt): def get_grads(self, vars_of, vars_wrt): for var_of in vars_of: for var_wrt in vars_wrt: - get_grad(var_of, var_wrt) + self.get_grad(var_of, var_wrt) def get_values_original(self): return {k: self.values[k] for k in self.nodes_original} diff --git a/PyCO2SYS/equilibria/__init__.py b/PyCO2SYS/equilibria/__init__.py index 9a833096..987f423d 100644 --- a/PyCO2SYS/equilibria/__init__.py +++ b/PyCO2SYS/equilibria/__init__.py @@ -3,8 +3,9 @@ """Calculate equilibrium constants from temperature, salinity and pressure.""" from autograd import numpy as np -from . import p1atm, pcx + from .. import constants, convert, gas, solubility +from . import p1atm, pcx, pressured def prepare(temperature, pressure, equilibria): diff --git a/PyCO2SYS/equilibria/p1atm.py b/PyCO2SYS/equilibria/p1atm.py index 6239851f..b7a7ff0e 100644 --- a/PyCO2SYS/equilibria/p1atm.py +++ b/PyCO2SYS/equilibria/p1atm.py @@ -143,7 +143,9 @@ Carbonic acid dissociation constants following PLR18. Used when opt_k_carbonic = 18. """ + from jax import numpy as np + from .. import convert @@ -1388,7 +1390,7 @@ def k_H2CO3_sws_MGH06(temperature, salinity): B_1 = -530.123 * salinity**0.5 - 6.103 * salinity C_1 = -2.06950 * salinity**0.5 pK1 = A_1 + B_1 / TempK + C_1 * np.log(TempK) + pK1_0 # pK1 sigma = 0.0054 - return 10.0 ** -(pK1) + return 10.0**-(pK1) def k_HCO3_sws_MGH06(temperature, salinity): @@ -1413,7 +1415,7 @@ def k_HCO3_sws_MGH06(temperature, salinity): B_2 = -772.483 * salinity**0.5 - 20.051 * salinity C_2 = -3.3336 * salinity**0.5 pK2 = A_2 + B_2 / TempK + C_2 * np.log(TempK) + pK2_0 # pK2 sigma = 0.011 - return 10.0 ** -(pK2) + return 10.0**-(pK2) def k_H2CO3_sws_M10(temperature, salinity): diff --git a/PyCO2SYS/equilibria/pcx.py b/PyCO2SYS/equilibria/pcx.py index 11b2fd80..ffa55b8a 100755 --- a/PyCO2SYS/equilibria/pcx.py +++ b/PyCO2SYS/equilibria/pcx.py @@ -56,6 +56,7 @@ factor_k_CO2 Calculate the pressure-correction factor for k_CO2 following W74 eq. 5. """ + # Original notes from CO2SYS-MATLAB regarding pressure corrections: # **************************************************************************** # Correct dissociation constants for pressure @@ -127,6 +128,7 @@ # Kappas are in cm3/mole/bar # **************************************************************************** from jax import numpy as np + from .. import convert diff --git a/PyCO2SYS/gas.py b/PyCO2SYS/gas.py index 192ae2f5..5c8225eb 100644 --- a/PyCO2SYS/gas.py +++ b/PyCO2SYS/gas.py @@ -14,6 +14,7 @@ """ from jax import numpy as np + from . import convert diff --git a/PyCO2SYS/meta.py b/PyCO2SYS/meta.py index b12a5f77..1215fb3e 100644 --- a/PyCO2SYS/meta.py +++ b/PyCO2SYS/meta.py @@ -1,6 +1,7 @@ # PyCO2SYS: marine carbonate system calculations in Python. # Copyright (C) 2020--2024 Matthew P. Humphreys et al. (GNU GPLv3) """Define metadata about PyCO2SYS.""" + import jax from jax import numpy as np @@ -44,9 +45,7 @@ def hello(): Lasciate ogni speranza, voi ch' entrate! Dante, Inferno iii, 9 sign on the entrance gates of hell -""".format( - version_xyz - ) +""".format(version_xyz) ) # (All hope abandon, ye who enter here!) diff --git a/PyCO2SYS/solubility.py b/PyCO2SYS/solubility.py index 7f9dc37f..dbff11e4 100644 --- a/PyCO2SYS/solubility.py +++ b/PyCO2SYS/solubility.py @@ -3,6 +3,7 @@ """Calculate saturation states of soluble solids.""" from autograd import numpy as np + from . import convert diff --git a/PyCO2SYS/uncertainty.py b/PyCO2SYS/uncertainty.py index e15a464b..f74cb76d 100644 --- a/PyCO2SYS/uncertainty.py +++ b/PyCO2SYS/uncertainty.py @@ -3,7 +3,9 @@ """Propagate uncertainties through marine carbonate system calculations.""" import copy + from autograd import numpy as np + from . import engine # Default uncertainties in pK values following OEDG18 diff --git a/PyCO2SYS/upsilon.py b/PyCO2SYS/upsilon.py index 14e4e995..a595a6a4 100644 --- a/PyCO2SYS/upsilon.py +++ b/PyCO2SYS/upsilon.py @@ -1,6 +1,7 @@ # PyCO2SYS: marine carbonate system calculations in Python. # Copyright (C) 2020--2024 Matthew P. Humphreys et al. (GNU GPLv3) from jax import numpy as np + from . import constants bh_TOG93_H24 = 28995 # J / mol