├── co2sys ├── tests │ ├── __init__.py │ └── test_core.py ├── __init__.py └── core.py ├── .coveragerc ├── ci ├── .coveragerc ├── requirements-py35.yml └── requirements-py36.yml ├── NEWS.md ├── .travis.yml ├── setup.py ├── .gitignore ├── README.md └── LICENSE /co2sys/tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | omit = co2sys/tests/* 3 | -------------------------------------------------------------------------------- /ci/.coveragerc: -------------------------------------------------------------------------------- 1 | [report] 2 | omit = co2sys/tests/* 3 | -------------------------------------------------------------------------------- /co2sys/__init__.py: -------------------------------------------------------------------------------- 1 | from co2sys.core import CO2SYS 2 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # co2syspy v0.0.1a1 2 | 3 | * 4 | 5 | 6 | # co2syspy v0.0.1a0 7 | 8 | * Initial release. 9 | -------------------------------------------------------------------------------- /ci/requirements-py35.yml: -------------------------------------------------------------------------------- 1 | name: test_env 2 | channels: 3 | - conda-forge 4 | - defaults 5 | dependencies: 6 | - attrs 7 | - coverage 8 | - coveralls 9 | - docutils 10 | - numpy 11 | - pytest 12 | - pytest-cov 13 | - python=3.5 14 | - tox 15 | -------------------------------------------------------------------------------- /ci/requirements-py36.yml: -------------------------------------------------------------------------------- 1 | name: test_env 2 | channels: 3 | - conda-forge 4 | - defaults 5 | dependencies: 6 | - attrs 7 | - coverage 8 | - coveralls 9 | - docutils 10 | - numpy 11 | - pytest 12 | - pytest-cov 13 | - python=3.5 14 | - tox 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | notifications: 3 | email: false 4 | 5 | matrix: 6 | fast_finish: true 7 | include: 8 | - python: 3.5 9 | env: 10 | - CONDA_ENV=py35 11 | - JOB_OS=Linux 12 | - python: 3.6 13 | env: 14 | - CONDA_ENV=py36 15 | - JOB_OS=Linux 16 | 17 | 18 | before_install: 19 | - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then 20 | wget https://repo.continuum.io/miniconda/Miniconda2-latest-$JOB_OS-x86_64.sh -O miniconda.sh; 21 | else 22 | wget https://repo.continuum.io/miniconda/Miniconda3-latest-$JOB_OS-x86_64.sh -O miniconda.sh; 23 | fi 24 | - bash miniconda.sh -b -p $HOME/miniconda 25 | - export PATH="$HOME/miniconda/bin:$PATH" 26 | - hash -r 27 | - conda config --set always_yes yes --set changeps1 no 28 | - conda update -q conda 29 | # Useful for debugging any issues with conda 30 | - conda info -a 31 | 32 | install: 33 | - conda env create -q -f ci/requirements-$CONDA_ENV.yml 34 | - source activate test_env 35 | - pip install --no-deps -e . 36 | 37 | script: 38 | - python -OO -c "import co2sys" 39 | - py.test --pyargs co2sys --cov=co2sys --cov-config ci/.coveragerc --cov-report term-missing --verbose $EXTRA_FLAGS 40 | 41 | after_success: 42 | - coveralls 43 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | 4 | setup( 5 | name='co2syspy', 6 | version='0.0.1a0', 7 | description='A Python interpretation of CO2SYS', 8 | license='GPLv3', 9 | 10 | author='S. Brewster Malevich', 11 | author_email='malevich@email.arizona.edu', 12 | url='https://github.com/brews/co2syspy', 13 | # See https://pypi.python.org/pypi?%3Aaction=list_classifiers 14 | classifiers=[ 15 | 'Development Status :: 1 - Planning', 16 | 17 | # Indicate who your project is intended for 18 | 'Intended Audience :: Developers', 19 | 'Intended Audience :: Science/Research', 20 | 'Topic :: Scientific/Engineering', 21 | 22 | # Pick your license as you wish (should match "license" above) 23 | 'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', 24 | 25 | # Specify the Python versions you support here. In particular, ensure 26 | # that you indicate whether you support Python 2, Python 3 or both. 27 | 'Programming Language :: Python :: 3', 28 | ], 29 | keywords='marine chemistry ocean seawater carbonate', 30 | 31 | packages=find_packages(exclude=['docs']), 32 | 33 | install_requires=['numpy', 'attrs'], 34 | tests_require=['pytest'] 35 | ) 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | 103 | # pycharm 104 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # co2syspy 2 | 3 | [![Travis-CI Build Status](https://travis-ci.org/brews/co2syspy.svg?branch=master)](https://travis-ci.org/brews/co2syspy) 4 | [![Coverage Status](https://coveralls.io/repos/github/brews/co2syspy/badge.svg?branch=master)](https://coveralls.io/github/brews/co2syspy?branch=coverage_fix) 5 | 6 | A Python interpretation of CO2SYS (http://cdiac.ess-dive.lbl.gov/ftp/co2sys/). 7 | 8 | This package is not stable, nor fully tested. It is also not very pythonic. 9 | 10 | ## Example 11 | 12 | First, import the package: 13 | 14 | ```python 15 | import co2sys 16 | ``` 17 | 18 | Use the old-school interface with `co2sys.CO2SYS()`. First, lets setup some variables to input: 19 | 20 | ```python 21 | par1type = 1 # The first parameter supplied is of type "1", which is "alkalinity" 22 | par2type = 2 # The first parameter supplied is of type "2", which is "DIC" 23 | par3type = 3 # The first parameter supplied is of type "3", which is "pH" 24 | presin = 4.036785269144779e3 # Pressure at input conditions 25 | tempout = 0 # Temperature at output conditions. 26 | presout = 0 # Pressure at output conditions. 27 | pHscale = 1 # pH scale at which the input pH is reported ("1" means "Total Scale") 28 | k1k2c = 4 # Choice of H2CO3 and HCO3- dissociation constants K1 and K2 ("4" means "Mehrbach refit") 29 | kso4c = 1 # Choice of HSO4- dissociation constants KSO4 ("1" means "Dickson") 30 | alk_s = 2.337701660156250e3 31 | dic_s = 2.186364257812500e3 32 | sal_s = 34.875812530517578 33 | temp_s = 2.197510004043579 34 | si_s = 49.758834838867188 35 | p_s = 1.458118438720703 36 | ``` 37 | 38 | Now, take all of this and run with it: 39 | 40 | ```python 41 | out, niceheaders = co2sys.CO2SYS(alk_s, dic_s, par1type, par2type, sal_s, 42 | temp_s, tempout, presin, presout, si_s, 43 | p_s, pHscale, k1k2c, kso4c) 44 | ``` 45 | 46 | Our output system variables are now held in `out` and the traditional CO2SYS "nice headers" are in `niceheaders`. 47 | 48 | ## Installation 49 | 50 | Install `co2syspy` from `conda` with: 51 | 52 | ```bash 53 | conda install co2syspy -c sbmalev 54 | ``` 55 | 56 | Install with `pip` using: 57 | 58 | ```bash 59 | pip install co2syspy 60 | ``` 61 | 62 | ## Support and development 63 | 64 | * Please feel free to report bugs and issues, or view the source code on GitHub (https://github.com/brews/co2syspy). 65 | 66 | ## License 67 | 68 | `co2syspy` is available under the Open Source GPLv3 (https://www.gnu.org/licenses). 69 | -------------------------------------------------------------------------------- /co2sys/tests/test_core.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import numpy as np 3 | import co2sys 4 | 5 | 6 | def test_CO2SYS(): 7 | """Very basic smoke test for CO2SYS 8 | """ 9 | 10 | # Conditions used in Mg/Ca forward model test case. 11 | par1type = 1 # The first parameter supplied is of type "1", which is "alkalinity" 12 | par2type = 2 # The first parameter supplied is of type "2", which is "DIC" 13 | par3type = 3 # The first parameter supplied is of type "3", which is "pH" 14 | presin = 4.036785269144779e3 # Pressure at input conditions 15 | tempout = 0 # Temperature at output conditions - doesn't matter in this example 16 | presout = 0 # Pressure at output conditions - doesn't matter in this example 17 | pHscale = 1 # pH scale at which the input pH is reported ("1" means "Total Scale") - doesn't matter in this example 18 | k1k2c = 4 # Choice of H2CO3 and HCO3- dissociation constants K1 and K2 ("4" means "Mehrbach refit") 19 | kso4c = 1 # Choice of HSO4- dissociation constants KSO4 ("1" means "Dickson") 20 | alk_s = 2.337701660156250e3 21 | dic_s = 2.186364257812500e3 22 | sal_s = 34.875812530517578 23 | temp_s = 2.197510004043579 24 | si_s = 49.758834838867188 25 | p_s = 1.458118438720703 26 | 27 | out, niceheaders = co2sys.CO2SYS(alk_s, dic_s, par1type, par2type, sal_s, temp_s, tempout, presin, presout, si_s, p_s, pHscale, k1k2c, kso4c) 28 | 29 | # Testing against CO2SYS (MATLAB v1.1) - God save us all. 30 | np.testing.assert_allclose(out['TAlk'], 2.337701660156250e+03, atol=1e-8) 31 | np.testing.assert_allclose(out['TCO2'], 2.186364257812500e+03, atol=1e-8) 32 | np.testing.assert_allclose(out['pHin'], 7.926358670659251, atol=1e-8) 33 | np.testing.assert_allclose(out['pCO2in'], 3.339176636623637e+02, atol=1e-8) 34 | np.testing.assert_allclose(out['fCO2in'], 3.324931306109210e+02, atol=1e-8) 35 | np.testing.assert_allclose(out['HCO3in'], 2.063984353536685e+03, atol=1e-8) 36 | np.testing.assert_allclose(out['CO3in'], 1.031503927006473e+02, atol=1e-8) 37 | np.testing.assert_allclose(out['CO2in'], 19.229511575167592, atol=1e-8) 38 | np.testing.assert_allclose(out['BAlkin'], 64.136510368697373, atol=1e-8) 39 | np.testing.assert_allclose(out['OHin'], 0.740362148764086, atol=1e-8) 40 | np.testing.assert_allclose(out['PAlkin'], 1.524484881016718, atol=1e-8) 41 | np.testing.assert_allclose(out['SiAlkin'], 1.027218899018952, atol=1e-8) 42 | np.testing.assert_allclose(out['Hfreein'], 0.010960202071109, atol=1e-8) 43 | np.testing.assert_allclose(out['RFin'], 14.447365360053329, atol=1e-8) 44 | np.testing.assert_allclose(out['OmegaCAin'], 1.109015460859011, atol=1e-8) 45 | np.testing.assert_allclose(out['OmegaARin'], 0.733349633282838, atol=1e-8) 46 | np.testing.assert_allclose(out['xCO2in'], 3.362462277278146e+02, atol=1e-8) 47 | np.testing.assert_allclose(out['pHout'], 8.122966233696896, atol=1e-8) 48 | np.testing.assert_allclose(out['pCO2out'], 3.212196594903900e+02, atol=1e-8) 49 | np.testing.assert_allclose(out['fCO2out'], 3.198082412817060e+02, atol=1e-8) 50 | np.testing.assert_allclose(out['HCO3out'], 2.055541950520533e+03, atol=1e-8) 51 | np.testing.assert_allclose(out['CO3out'], 1.107009226494601e+02, atol=1e-8) 52 | np.testing.assert_allclose(out['CO2out'], 20.121384642507195, atol=1e-8) 53 | np.testing.assert_allclose(out['BAlkout'], 57.722997496438055, atol=1e-8) 54 | np.testing.assert_allclose(out['OHout'], 0.653911606741942, atol=1e-8) 55 | np.testing.assert_allclose(out['PAlkout'], 1.515025862046792, atol=1e-8) 56 | np.testing.assert_allclose(out['SiAlkout'], 0.873517831066995, atol=1e-8) 57 | np.testing.assert_allclose(out['Hfreeout'], 0.006859198847034, atol=1e-8) 58 | np.testing.assert_allclose(out['RFout'], 14.288317572981429, atol=1e-8) 59 | np.testing.assert_allclose(out['OmegaCAout'], 2.655081296144144, atol=1e-8) 60 | np.testing.assert_allclose(out['OmegaARout'], 1.668150040994507, atol=1e-8) 61 | np.testing.assert_allclose(out['xCO2out'], 3.231299189119258e+02, atol=1e-8) 62 | np.testing.assert_allclose(out['pHinTOTAL'], 7.926358670659251, atol=1e-8) 63 | np.testing.assert_allclose(out['pHinSWS'], 7.920051143153011, atol=1e-8) 64 | np.testing.assert_allclose(out['pHinFREE'], 7.960181438775133, atol=1e-8) 65 | np.testing.assert_allclose(out['pHinNBS'], 8.016658362436013, atol=1e-8) 66 | np.testing.assert_allclose(out['pHoutTOTAL'], 8.122966233696896, atol=1e-8) 67 | np.testing.assert_allclose(out['pHoutSWS'], 8.116021698995990, atol=1e-8) 68 | np.testing.assert_allclose(out['pHoutFREE'], 8.163726606834221, atol=1e-8) 69 | np.testing.assert_allclose(out['pHoutNBS'], 8.208086818347853, atol=1e-8) 70 | np.testing.assert_allclose(out['TEMPIN'], 2.197510004043579, atol=1e-8) 71 | np.testing.assert_allclose(out['TEMPOUT'], 0, atol=1e-8) 72 | np.testing.assert_allclose(out['PRESIN'], 4.036785269144779e+03, atol=1e-8) 73 | np.testing.assert_allclose(out['PRESOUT'], 0, atol=1e-8) 74 | assert out['PAR1TYPE'] == 1 75 | assert out['PAR2TYPE'] == 2 76 | np.testing.assert_allclose(out['PRESOUT'], 0, atol=1e-8) 77 | assert out['K1K2CONSTANTS'] == 4 78 | assert out['KSO4CONSTANTS'] == 1 79 | assert out['pHSCALEIN'] == 1 80 | np.testing.assert_allclose(out['SAL'], 34.875812530517578, atol=1e-8) 81 | np.testing.assert_allclose(out['PO4'], 1.458118438720703, atol=1e-8) 82 | np.testing.assert_allclose(out['SI'], 49.758834838867188, atol=1e-8) 83 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /co2sys/core.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def CO2SYS(PAR1, PAR2, PAR1TYPE, PAR2TYPE, SAL, TEMPIN, TEMPOUT, PRESIN, PRESOUT, SI, PO4, pHSCALEIN, K1K2CONSTANTS, KSO4CONSTANTS): 5 | #************************************************************************** 6 | # 7 | # This is CO2SYS version 1.1 (SEPT-2011) 8 | # 9 | # CO2SYS is a MATLAB-version of the original CO2SYS for DOS. 10 | # CO2SYS calculates and returns the state of the carbonate system of 11 | # oceanographic water samples, if supplied with enough input. 12 | # 13 | # Please note that his software is intended to be exactly identical to the 14 | # DOS and Excel versions that have been released previously, meaning that 15 | # results obtained should be very nearly indentical for identical input. 16 | # Additionally, several of the dissociation constants K1 and K2 that have 17 | # been published since the original DOS version was written are implemented. 18 | # For a complete list of changes since version 1.0, see below. 19 | # 20 | # For much more info please have a look at: 21 | # Lewis, E., and D. W. R. Wallace. 1998. Program Developed for 22 | # CO2 System Calculations. ORNL/CDIAC-105. Carbon Dioxide Information 23 | # Analysis Center, Oak Ridge National Laboratory, U.S. Department of Energy, 24 | # Oak Ridge, Tennessee. 25 | # http://cdiac.ornl.gov/oceans/co2rprt.html 26 | # 27 | #************************************************************************** 28 | # 29 | # **** SYNTAX: 30 | # [RESULT,HEADERS,NICEHEADERS]=CO2SYS(PAR1,PAR2,PAR1TYPE,PAR2TYPE,... 31 | # ...SAL,TEMPIN,TEMPOUT,PRESIN,PRESOUT,SI,PO4,pHSCALEIN,... 32 | # ...K1K2CONSTANTS,KSO4CONSTANTS) 33 | # 34 | # **** SYNTAX EXAMPLES: 35 | # [Result] = CO2SYS(2400,2200,1,2,35,0,25,4200,0,15,1,1,4,1) 36 | # [Result,Headers] = CO2SYS(2400, 8,1,3,35,0,25,4200,0,15,1,1,4,1) 37 | # [Result,Headers,Niceheaders] = CO2SYS( 500, 8,5,3,35,0,25,4200,0,15,1,1,4,1) 38 | # [A] = CO2SYS(2400,2000:10:2400,1,2,35,0,25,4200,0,15,1,1,4,1) 39 | # [A] = CO2SYS(2400,2200,1,2,0:1:35,0,25,4200,0,15,1,1,4,1) 40 | # [A] = CO2SYS(2400,2200,1,2,35,0,25,0:100:4200,0,15,1,1,4,1) 41 | # 42 | # **** APPLICATION EXAMPLE (copy and paste this into command window): 43 | # tmps=0:40; sals=0:40; [X,Y]=meshgrid(tmps,sals); 44 | # A = CO2SYS(2300,2100,1,2,Y(:),X(:),nan,0,nan,1,1,1,9,1); 45 | # Z=nan(size(X)); Z(:)=A(:,4); figure; contourf(X,Y,Z,20); caxis([0 1200]); colorbar; 46 | # ylabel('Salinity [psu]'); xlabel('Temperature [degC]'); title('Dependence of pCO2 [uatm] on T and S') 47 | # 48 | #************************************************************************** 49 | # 50 | # INPUT: 51 | # 52 | # PAR1 (some unit) : scalar or vector of size n 53 | # PAR2 (some unit) : scalar or vector of size n 54 | # PAR1TYPE () : scalar or vector of size n (*) 55 | # PAR2TYPE () : scalar or vector of size n (*) 56 | # SAL () : scalar or vector of size n 57 | # TEMPIN (degr. C) : scalar or vector of size n 58 | # TEMPOUT (degr. C) : scalar or vector of size n 59 | # PRESIN (dbar) : scalar or vector of size n 60 | # PRESOUT (dbar) : scalar or vector of size n 61 | # SI (umol/kgSW) : scalar or vector of size n 62 | # PO4 (umol/kgSW) : scalar or vector of size n 63 | # pHSCALEIN : scalar or vector of size n (**) 64 | # K1K2CONSTANTS : scalar or vector of size n (***) 65 | # KSO4CONSTANTS : scalar or vector of size n (****) 66 | # 67 | # (*) Each element must be an integer, 68 | # indicating that PAR1 (or PAR2) is of type: 69 | # 1 = Total Alkalinity 70 | # 2 = DIC 71 | # 3 = pH 72 | # 4 = pCO2 73 | # 5 = fCO2 74 | # 75 | # (**) Each element must be an integer, 76 | # indicating that the pH-input (PAR1 or PAR2, if any) is at: 77 | # 1 = Total scale 78 | # 2 = Seawater scale 79 | # 3 = Free scale 80 | # 4 = NBS scale 81 | # 82 | # (***) Each element must be an integer, 83 | # indicating the K1 K2 dissociation constants that are to be used: 84 | # 1 = Roy, 1993 T: 0-45 S: 5-45. Total scale. Artificial seawater. 85 | # 2 = Goyet & Poisson T: -1-40 S: 10-50. Seaw. scale. Artificial seawater. 86 | # 3 = HANSSON refit BY DICKSON AND MILLERO T: 2-35 S: 20-40. Seaw. scale. Artificial seawater. 87 | # 4 = MEHRBACH refit BY DICKSON AND MILLERO T: 2-35 S: 20-40. Seaw. scale. Artificial seawater. 88 | # 5 = HANSSON and MEHRBACH refit BY DICKSON AND MILLERO T: 2-35 S: 20-40. Seaw. scale. Artificial seawater. 89 | # 6 = GEOSECS (i.e., original Mehrbach) T: 2-35 S: 19-43. NBS scale. Real seawater. 90 | # 7 = Peng (i.e., originam Mehrbach but without XXX) T: 2-35 S: 19-43. NBS scale. Real seawater. 91 | # 8 = Millero, 1979, FOR PURE WATER ONLY (i.e., Sal=0) T: 0-50 S: 0. 92 | # 9 = Cai and Wang, 1998 T: 2-35 S: 0-49. NBS scale. Real and artificial seawater. 93 | # 10 = Lueker et al, 2000 T: 2-35 S: 19-43. Total scale. Real seawater. 94 | # 11 = Mojica Prieto and Millero, 2002. T: 0-45 S: 5-42. Seaw. scale. Real seawater 95 | # 12 = Millero et al, 2002 T: -1.6-35 S: 34-37. Seaw. scale. Field measurements. 96 | # 13 = Millero et al, 2006 T: 0-50 S: 1-50. Seaw. scale. Real seawater. 97 | # 14 = Millero et al, 2010 T: 0-50 S: 1-50. Seaw. scale. Real seawater. 98 | # 99 | # (****) Each element must be an integer that 100 | # indicates the KSO4 dissociation constants that are to be used, 101 | # in combination with the formulation of the borate-to-salinity ratio to be used. 102 | # Having both these choices in a single argument is somewhat awkward, 103 | # but it maintains syntax compatibility with the previous version. 104 | # 1 = KSO4 of Dickson & TB of Uppstrom 1979 (PREFERRED) 105 | # 2 = KSO4 of Khoo & TB of Uppstrom 1979 106 | # 3 = KSO4 of Dickson & TB of Lee 2010 107 | # 4 = KSO4 of Khoo & TB of Lee 2010 108 | # 109 | #**************************************************************************% 110 | # 111 | # OUTPUT: * an array containing the following parameter values (one row per sample): 112 | # * a cell-array containing crudely formatted headers 113 | # * a cell-array containing nicely formatted headers 114 | # 115 | # POS PARAMETER UNIT 116 | # 117 | # 01 - TAlk (umol/kgSW) 118 | # 02 - TCO2 (umol/kgSW) 119 | # 03 - pHin () 120 | # 04 - pCO2 input (uatm) 121 | # 05 - fCO2 input (uatm) 122 | # 06 - HCO3 input (umol/kgSW) 123 | # 07 - CO3 input (umol/kgSW) 124 | # 08 - CO2 input (umol/kgSW) 125 | # 09 - BAlk input (umol/kgSW) 126 | # 10 - OH input (umol/kgSW) 127 | # 11 - PAlk input (umol/kgSW) 128 | # 12 - SiAlk input (umol/kgSW) 129 | # 13 - Hfree input (umol/kgSW) 130 | # 14 - RevelleFactor input () 131 | # 15 - OmegaCa input () 132 | # 16 - OmegaAr input () 133 | # 17 - xCO2 input (ppm) 134 | # 18 - pH output () 135 | # 19 - pCO2 output (uatm) 136 | # 20 - fCO2 output (uatm) 137 | # 21 - HCO3 output (umol/kgSW) 138 | # 22 - CO3 output (umol/kgSW) 139 | # 23 - CO2 output (umol/kgSW) 140 | # 24 - BAlk output (umol/kgSW) 141 | # 25 - OH output (umol/kgSW) 142 | # 26 - PAlk output (umol/kgSW) 143 | # 27 - SiAlk output (umol/kgSW) 144 | # 28 - Hfree output (umol/kgSW) 145 | # 29 - RevelleFactor output () 146 | # 30 - OmegaCa output () 147 | # 31 - OmegaAr output () 148 | # 32 - xCO2 output (ppm) 149 | # 33 - pH input (Total) () 150 | # 34 - pH input (SWS) () 151 | # 35 - pH input (Free) () 152 | # 36 - pH input (NBS) () 153 | # 37 - pH output (Total) () 154 | # 38 - pH output (SWS) () 155 | # 39 - pH output (Free) () 156 | # 40 - pH output (NBS) () 157 | # 41 - TEMP input (deg C) *** 158 | # 42 - TEMPOUT (deg C) *** 159 | # 43 - PRES input (dbar or m) *** 160 | # 44 - PRESOUT (dbar or m) *** 161 | # 45 - PAR1TYPE (integer) *** 162 | # 46 - PAR2TYPE (integer) *** 163 | # 47 - K1K2CONSTANTS (integer) *** 164 | # 48 - KSO4CONSTANTS (integer) *** 165 | # 49 - pHSCALE of input (integer) *** 166 | # 50 - SAL (psu) *** 167 | # 51 - PO4 (umol/kgSW) *** 168 | # 52 - SI (umol/kgSW) *** 169 | # 53 - K0 input () 170 | # 54 - K1 input () 171 | # 55 - K2 input () 172 | # 56 - pK1 input () 173 | # 57 - pK2 input () 174 | # 58 - KW input () 175 | # 59 - KB input () 176 | # 60 - KF input () 177 | # 61 - KS input () 178 | # 62 - KP1 input () 179 | # 63 - KP2 input () 180 | # 64 - KP3 input () 181 | # 65 - KSi input () 182 | # 66 - K0 output () 183 | # 67 - K1 output () 184 | # 68 - K2 output () 185 | # 69 - pK1 output () 186 | # 70 - pK2 output () 187 | # 71 - KW output () 188 | # 72 - KB output () 189 | # 73 - KF output () 190 | # 74 - KS output () 191 | # 75 - KP1 output () 192 | # 76 - KP2 output () 193 | # 77 - KP3 output () 194 | # 78 - KSi output () 195 | # 79 - TB (umol/kgSW) 196 | # 80 - TF (umol/kgSW) 197 | # 81 - TS (umol/kgSW) 198 | # 82 - TP (umol/kgSW) 199 | # 83 - TSi (umol/kgSW) 200 | # 201 | # *** SIMPLY RESTATES THE INPUT BY USER 202 | # 203 | # In all the above, the terms "input" and "output" may be understood 204 | # to refer to the 2 scenarios for which CO2SYS performs calculations, 205 | # each defined by its own combination of temperature and pressure. 206 | # For instance, one may use CO2SYS to calculate, from measured DIC and TAlk, 207 | # the pH that that sample will have in the lab (e.g., T=25 degC, P=0 dbar), 208 | # and what the in situ pH would have been (e.g., at T=1 degC, P=4500). 209 | # A = CO2SYS(2400,2200,1,2,35,25,1,0,4200,1,1,1,4,1) 210 | # pH_lab = A(3); % 7.84 211 | # pH_sea = A(18); % 8.05 212 | # 213 | #************************************************************************** 214 | # 215 | # This is version 1.1 (uploaded to CDIAC at SEP XXth, 2011): 216 | # 217 | # **** Changes since 1.01 (uploaded to CDIAC at June 11th, 2009): 218 | # - Function cleans up its global variables when done (if you loose variables, this may be the cause -- see around line 570) 219 | # - Added the outputting of K values 220 | # - Implementation of constants of Cai and Wang, 1998 221 | # - Implementation of constants of Lueker et al., 2000 222 | # - Implementation of constants of Mojica-Prieto and Millero, 2002 223 | # - Implementation of constants of Millero et al., 2002 (only their eqs. 19, 20, no TCO2 dependency) 224 | # - Implementation of constants of Millero et al., 2006 225 | # - Implementation of constants of Millero et al., 2010 226 | # - Properly listed Sal and Temp limits for the available constants 227 | # - added switch for using the new Lee et al., (2010) formulation of Total Borate (see KSO4CONSTANTS above) 228 | # - Minor corrections to the GEOSECS constants (gave NaN for some output in earlier version) 229 | # - Fixed decimal point error on [H+] (did not get converted to umol/kgSW from mol/kgSW). 230 | # - Changed 'Hfreein' to 'Hfreeout' in the 'NICEHEADERS'-output (typo) 231 | # 232 | # **** Changes since 1.00 (uploaded to CDIAC at May 29th, 2009): 233 | # - added a note explaining that all known bugs were removed before release of 1.00 234 | # 235 | #************************************************************************** 236 | # 237 | # CO2SYS originally by Lewis and Wallace 1998 238 | # Converted to MATLAB by Denis Pierrot at 239 | # CIMAS, University of Miami, Miami, Florida 240 | # Vectorization, internal refinements and speed improvements by 241 | # Steven van Heuven, University of Groningen, The Netherlands. 242 | # Questions, bug reports et cetera: svheuven@gmail.com 243 | # 244 | #************************************************************************** 245 | 246 | 247 | 248 | #************************************************************************** 249 | # NOTHING BELOW THIS SHOULD REQUIRE EDITING BY USER! 250 | #************************************************************************** 251 | 252 | 253 | 254 | # Global variables 255 | global pHScale, WhichKs, WhoseKSO4, Pbar 256 | global Sal, sqrSal, TempK, logTempK, TempCi, TempCo, Pdbari, Pdbaro 257 | global FugFac, VPFac, PengCorrection, ntps, RGasConstant 258 | global fH, RT 259 | global K0, K1, K2, KW, KB, KF, KS, KP1, KP2, KP3, KSi 260 | global TB, TF, TS, TP, TSi, F 261 | 262 | PAR1 = np.atleast_1d(PAR1) 263 | PAR2 = np.atleast_1d(PAR2) 264 | PAR1TYPE = np.atleast_1d(PAR1TYPE) 265 | PAR2TYPE = np.atleast_1d(PAR2TYPE) 266 | SAL = np.atleast_1d(SAL) 267 | TEMPIN = np.atleast_1d(TEMPIN) 268 | TEMPOUT = np.atleast_1d(TEMPOUT) 269 | PRESIN = np.atleast_1d(PRESIN) 270 | PRESOUT = np.atleast_1d(PRESOUT) 271 | SI = np.atleast_1d(SI) 272 | PO4 = np.atleast_1d(PO4) 273 | pHSCALEIN = np.atleast_1d(pHSCALEIN) 274 | K1K2CONSTANTS = np.atleast_1d(K1K2CONSTANTS) 275 | KSO4CONSTANTS = np.atleast_1d(KSO4CONSTANTS) 276 | 277 | veclengths= {len(PAR1), len(PAR2), len(PAR1TYPE), len(PAR2TYPE), len(SAL), len(TEMPIN), len(TEMPOUT), len(PRESIN), 278 | len(PRESOUT), len(SI), len(PO4), len(pHSCALEIN), len(K1K2CONSTANTS), len(KSO4CONSTANTS)} 279 | 280 | if len(veclengths) > 2: 281 | print('*** INPUT ERROR: Input vectors must all be of same length, or of length 1. ***') 282 | return 283 | 284 | # Find longest column vector 285 | ntps = max(veclengths) 286 | 287 | pHScale = pHSCALEIN 288 | WhichKs = K1K2CONSTANTS 289 | WhoseKSO4 = KSO4CONSTANTS 290 | p1 = PAR1TYPE 291 | p2 = PAR2TYPE 292 | TempCi = TEMPIN 293 | TempCo = TEMPOUT 294 | Pdbari = PRESIN 295 | Pdbaro = PRESOUT 296 | Sal = SAL 297 | sqrSal = np.sqrt(SAL) 298 | TP = PO4.copy() 299 | TSi = SI.copy() 300 | RGasConstant = 83.1451 # ml bar-1 K-1 mol-1, DOEv2 301 | # RGasConstant = 83.14472 # ml bar-1 K-1 mol-1, DOEv3 302 | 303 | # Empty variables for... 304 | # Talk 305 | TA = np.empty(ntps) 306 | TA[:] = np.nan 307 | 308 | # DIC 309 | TC = np.empty(ntps) 310 | TC[:] = np.nan 311 | 312 | # pH 313 | PH = np.empty(ntps) 314 | PH[:] = np.nan 315 | 316 | # pCO2 317 | PC = np.empty(ntps) 318 | PC[:] = np.nan 319 | 320 | # fCO2 321 | FC = np.empty(ntps) 322 | FC[:] = np.nan 323 | 324 | # Assign values to empty vectors. 325 | F = p1 == 1 326 | TA[F] = PAR1[F] / 1e6 # micromol/kg to mol/kg 327 | F = p1 == 2 328 | TC[F] = PAR1[F] / 1e6 # micromol/kg to mol/kg 329 | F = p1 == 3 330 | PH[F] = PAR1[F] 331 | F = p1 == 4 332 | PC[F] = PAR1[F] / 1e6 # microatm. to atm. 333 | F = p1 == 5 334 | FC[F] = PAR2[F] / 1e6 # microatm. to atm. 335 | F = p2 == 1 336 | TA[F] = PAR2[F] / 1e6 # micromol/kg to mol/kg 337 | F = p2 == 2 338 | TC[F] = PAR2[F] / 1e6 # micromol/kg to mol/kg 339 | F = p2 == 3 340 | PH[F] = PAR2[F] 341 | F = p2 == 4 342 | PC[F] = PAR2[F] / 1e6 # microatm. to atm. 343 | F = p2 == 5 344 | FC[F] = PAR2[F] / 1e6 # microatm. to atm. 345 | 346 | # Generate the columns holding Si, Phos and Sal. 347 | # Pure Water: 348 | F = WhichKs == 8 349 | Sal[F] = 0 350 | # GEOSECS and Pure Water: 351 | F = WhichKs == 8 or WhichKs == 6 352 | TP[F] = 0 353 | TSi[F] = 0 354 | # Other cases 355 | F = ~F 356 | TP[F] = TP[F] / 1e6 357 | TSi[F] = TSi[F] / 1e6 358 | 359 | # The vector 'PengCorrection' is used to modify the value of TA, for those 360 | # cases where WhichKs==7, since PAlk(Peng) = PAlk(Dickson) + TP. 361 | # Thus, PengCorrection is 0 for all cases where WhichKs is not 7 362 | PengCorrection = np.zeros(ntps) 363 | F = WhichKs == 7 364 | PengCorrection[F] = TP[F] 365 | 366 | # Calculate the constants for all samples at input conditions 367 | # The constants calculated for each sample will be on the appropriate pH scale! 368 | Constants(TempCi, Pdbari) 369 | 370 | # Make sure fCO2 is available for each sample that has pCO2. 371 | F = p1 == 4 or p2 == 4 372 | FC[F] = PC[F] * FugFac[F] 373 | 374 | # Generate vector for results, and copy the raw input values into them. This 375 | # copies ~60% NaNs, which will be replaced for calculated values later on. 376 | TAc = TA 377 | TCc = TC 378 | PHic = PH 379 | PCic = PC 380 | FCic = FC 381 | 382 | # Generate vector describing the combination of input parameters 383 | # So, the valid ones are: 12,13,15,23,25,35 384 | Icase = 10 * np.min([p1.min(), p2.min()]) + np.max([p1.max(), p2.max()]) 385 | 386 | # Calculate missing values for AT,CT,PH,FC: 387 | # pCO2 will be calculated later on, routines work with fCO2. 388 | F = Icase == 12 # input TA, TC 389 | if np.any(F): 390 | PHic[F], FCic[F] = CalculatepHfCO2fromTATC(TAc[F] - PengCorrection[F], TCc[F]) 391 | F = Icase == 13 # input TA, pH 392 | if np.any(F): 393 | TCc[F] = CalculateTCfromTApH(TAc[F] - PengCorrection[F], PHic[F]) 394 | FCic[F] = CalculatefCO2fromTCpH(TCc[F], PHic[F]) 395 | F = Icase == 14 or Icase == 15 # input TA, (pCO2 or fCO2) 396 | if np.any(F): 397 | PHic[F] = CalculatepHfromTAfCO2(TAc[F] - PengCorrection[F], FCic[F]) 398 | TCc[F] = CalculateTCfromTApH(TAc[F] - PengCorrection[F], PHic[F]) 399 | F = Icase == 23 # input TC, pH 400 | if np.any(F): 401 | TAc[F] = CalculateTAfromTCpH(TCc[F], PHic[F]) + PengCorrection[F] 402 | FCic[F] = CalculatefCO2fromTCpH(TCc[F], PHic[F]) 403 | F = Icase == 24 or Icase == 25 # input TC, (pCO2 or fCO2) 404 | if np.any(F): 405 | PHic[F] = CalculatepHfromTCfCO2(TCc[F], FCic[F]) 406 | TAc[F] = CalculateTAfromTCpH(TCc[F], PHic[F]) + PengCorrection[F] 407 | F = Icase == 34 or Icase == 35 # input pH, (pCO2 or fCO2) 408 | if np.any(F): 409 | TCc[F] = CalculateTCfrompHfCO2(PHic[F], FCic[F]) 410 | TAc[F] = CalculateTAfromTCpH(TCc[F], PHic[F]) + PengCorrection[F] 411 | 412 | # By now, an fCO2 value is available for each sample. 413 | # Generate the associated pCO2 value: 414 | PCic = FCic / FugFac 415 | 416 | # CalculateOtherParamsAtInputConditions: 417 | HCO3inp, CO3inp, BAlkinp, OHinp, PAlkinp, SiAlkinp, Hfreeinp, HSO4inp, HFinp = CalculateAlkParts(PHic, TCc) 418 | PAlkinp = PAlkinp + PengCorrection 419 | CO2inp = TCc - CO3inp - HCO3inp 420 | F = np.ones(ntps, dtype=bool) # i.e., do for all samples: 421 | Revelleinp = RevelleFactor(TAc - PengCorrection, TCc) 422 | OmegaCainp, OmegaArinp = CaSolubility(Sal, TempCi, Pdbari, TCc, PHic) 423 | xCO2dryinp = PCic / VPFac # ' this assumes pTot = 1 atm 424 | 425 | # Just for reference, convert pH at input conditions to the other scales, too. 426 | pHicT, pHicS, pHicF, pHicN = FindpHOnAllScales(PHic) 427 | 428 | # Merge the Ks at input into an array. Ks at output will be glued to this later. 429 | KIVEC = [K0, K1, K2 - np.log10(K1) - np.log10(K2), KW, KB, KF, KS, KP1, KP2, KP3, KSi] 430 | 431 | # Calculate the constants for all samples at output conditions 432 | Constants(TempCo, Pdbaro) 433 | 434 | # Calculate, for output conditions, using conservative TA and TC, pH, fCO2 and pCO2 435 | F = np.ones(ntps, dtype=bool) # i.e., do for all samples: 436 | PHoc, FCoc = CalculatepHfCO2fromTATC(TAc - PengCorrection, TCc) 437 | PCoc = FCoc / FugFac 438 | 439 | # Calculate Other Stuff At Output Conditions: 440 | HCO3out, CO3out, BAlkout,OHout, PAlkout, SiAlkout, Hfreeout, HSO4out, HFout = CalculateAlkParts(PHoc, TCc) 441 | PAlkout = PAlkout + PengCorrection 442 | CO2out = TCc - CO3out - HCO3out 443 | Revelleout = RevelleFactor(TAc, TCc) 444 | OmegaCaout, OmegaArout = CaSolubility(Sal, TempCo, Pdbaro, TCc, PHoc) 445 | xCO2dryout = PCoc / VPFac # ' this assumes pTot = 1 atm 446 | 447 | 448 | # Just for reference, convert pH at output conditions to the other scales, too. 449 | pHocT, pHocS, pHocF, pHocN = FindpHOnAllScales(PHoc) 450 | 451 | KOVEC = np.array([K0, K1, K2, -np.log10(K1) - np.log10(K2), KW, KB, KF, KS, KP1, KP2, KP3, KSi]) 452 | TVEC = np.array([TB, TF, TS]) 453 | 454 | 455 | # Saving data in array, 81 columns, as many rows as samples input 456 | # Multiplied Hfreeinp *1e6, svh20100827 457 | # Multiplied Hfreeout *1e6, svh20100827 458 | 459 | DATA = [TAc * 1e6, TCc * 1e6, PHic, PCic * 1e6, FCic * 1e6, 460 | HCO3inp * 1e6, CO3inp * 1e6, CO2inp * 1e6, BAlkinp * 1e6, OHinp * 1e6, 461 | PAlkinp * 1e6, SiAlkinp * 1e6, Hfreeinp * 1e6, Revelleinp, OmegaCainp, 462 | OmegaArinp, xCO2dryinp * 1e6, PHoc, PCoc * 1e6, FCoc * 1e6, 463 | HCO3out * 1e6, CO3out * 1e6, CO2out * 1e6, BAlkout * 1e6, OHout * 1e6, 464 | PAlkout * 1e6, SiAlkout * 1e6, Hfreeout * 1e6, Revelleout, OmegaCaout, 465 | OmegaArout, xCO2dryout * 1e6, pHicT, pHicS, pHicF, 466 | pHicN, pHocT, pHocS, pHocF, pHocN, TEMPIN, TEMPOUT, PRESIN, PRESOUT, PAR1TYPE, 467 | PAR2TYPE, K1K2CONSTANTS, KSO4CONSTANTS, pHSCALEIN, SAL, PO4, SI, KIVEC, KOVEC, 468 | TVEC * 1e6] 469 | 470 | HEADERS = ['TAlk', 'TCO2', 'pHin', 'pCO2in', 'fCO2in', 'HCO3in', 'CO3in', 471 | 'CO2in', 'BAlkin', 'OHin', 'PAlkin', 'SiAlkin', 'Hfreein', 'RFin', 472 | 'OmegaCAin', 'OmegaARin', 'xCO2in', 'pHout', 'pCO2out', 'fCO2out', 473 | 'HCO3out', 'CO3out', 'CO2out', 'BAlkout', 'OHout', 'PAlkout', 474 | 'SiAlkout', 'Hfreeout', 'RFout', 'OmegaCAout', 'OmegaARout', 'xCO2out', 475 | 'pHinTOTAL', 'pHinSWS', 'pHinFREE', 'pHinNBS', 476 | 'pHoutTOTAL', 'pHoutSWS', 'pHoutFREE', 'pHoutNBS', 'TEMPIN', 'TEMPOUT', 477 | 'PRESIN', 'PRESOUT', 'PAR1TYPE', 'PAR2TYPE', 'K1K2CONSTANTS', 'KSO4CONSTANTS', 478 | 'pHSCALEIN', 'SAL', 'PO4', 'SI', 'K0input', 'K1input', 'K2input', 'pK1input', 479 | 'pK2input', 'KWinput', 'KBinput', 'KFinput', 'KSinput', 'KP1input', 'KP2input', 480 | 'KP3input', 'KSiinput', 'K0output', 'K1output', 'K2output', 'pK1output', 481 | 'pK2output', 'KWoutput', 'KBoutput', 'KFoutput', 'KSoutput', 'KP1output', 482 | 'KP2output', 'KP3output', 'KSioutput', 'TB', 'TF', 'TS',] 483 | 484 | NICEHEADERS = ['01 - TAlk (umol/kgSW) ', 485 | '02 - TCO2 (umol/kgSW) ', 486 | '03 - pHin () ', 487 | '04 - pCO2in (uatm) ', 488 | '05 - fCO2in (uatm) ', 489 | '06 - HCO3in (umol/kgSW) ', 490 | '07 - CO3in (umol/kgSW) ', 491 | '08 - CO2in (umol/kgSW) ', 492 | '09 - BAlkin (umol/kgSW) ', 493 | '10 - OHin (umol/kgSW) ', 494 | '11 - PAlkin (umol/kgSW) ', 495 | '12 - SiAlkin (umol/kgSW) ', 496 | '13 - Hfreein (umol/kgSW) ', 497 | '14 - RevelleFactorin () ', 498 | '15 - OmegaCain () ', 499 | '16 - OmegaArin () ', 500 | '17 - xCO2in (ppm) ', 501 | '18 - pHout () ', 502 | '19 - pCO2out (uatm) ', 503 | '20 - fCO2out (uatm) ', 504 | '21 - HCO3out (umol/kgSW) ', 505 | '22 - CO3out (umol/kgSW) ', 506 | '23 - CO2out (umol/kgSW) ', 507 | '24 - BAlkout (umol/kgSW) ', 508 | '25 - OHout (umol/kgSW) ', 509 | '26 - PAlkout (umol/kgSW) ', 510 | '27 - SiAlkout (umol/kgSW) ', 511 | '28 - Hfreeout (umol/kgSW) ', # Changed 'Hfreein' to 'Hfreeout', svh20100827 512 | '29 - RevelleFactorout () ', 513 | '30 - OmegaCaout () ', 514 | '31 - OmegaArout () ', 515 | '32 - xCO2out (ppm) ', 516 | '33 - pHin (Total) () ', 517 | '34 - pHin (SWS) () ', 518 | '35 - pHin (Free) () ', 519 | '36 - pHin (NBS ) () ', 520 | '37 - pHout(Total) () ', 521 | '38 - pHout(SWS) () ', 522 | '39 - pHout(Free) () ', 523 | '40 - pHout(NBS ) () ', 524 | '41 - TEMPIN (Deg C) ', 525 | '42 - TEMPOUT (Deg C) ', 526 | '43 - PRESIN (dbar) ', 527 | '44 - PRESOUT (dbar) ', 528 | '45 - PAR1TYPE () ', 529 | '46 - PAR2TYPE () ', 530 | '47 - K1K2CONSTANTS () ', 531 | '48 - KSO4CONSTANTS () ', 532 | '49 - pHSCALEIN () ', 533 | '50 - SAL (umol/kgSW) ', 534 | '51 - PO4 (umol/kgSW) ', 535 | '52 - SI (umol/kgSW) ', 536 | '53 - K0input () ', 537 | '54 - K1input () ', 538 | '55 - K2input () ', 539 | '56 - pK1input () ', 540 | '57 - pK2input () ', 541 | '58 - KWinput () ', 542 | '59 - KBinput () ', 543 | '60 - KFinput () ', 544 | '61 - KSinput () ', 545 | '62 - KP1input () ', 546 | '63 - KP2input () ', 547 | '64 - KP3input () ', 548 | '65 - KSiinput () ', 549 | '66 - K0output () ', 550 | '67 - K1output () ', 551 | '68 - K2output () ', 552 | '69 - pK1output () ', 553 | '70 - pK2output () ', 554 | '71 - KWoutput () ', 555 | '72 - KBoutput () ', 556 | '73 - KFoutput () ', 557 | '74 - KSoutput () ', 558 | '75 - KP1output () ', 559 | '76 - KP2output () ', 560 | '77 - KP3output () ', 561 | '78 - KSioutput () ', 562 | '79 - TB (umol/kgSW) ', 563 | '80 - TF (umol/kgSW) ', 564 | '81 - TS (umol/kgSW) '] 565 | 566 | out = {k:v for (k, v) in zip(HEADERS, DATA)} 567 | return out, NICEHEADERS 568 | 569 | 570 | def Constants(TempC, Pdbar): 571 | # SUB Constants, version 04.01, 10-13-97, written by Ernie Lewis. 572 | # Inputs: pHScale%, WhichKs%, WhoseKSO4%, Sali, TempCi, Pdbar 573 | # Outputs: K0, K(), T(), fH, FugFac, VPFac 574 | # This finds the Constants of the CO2 system in seawater or freshwater, 575 | # corrects them for pressure, and reports them on the chosen pH scale. 576 | # The process is as follows: the Constants (except KS, KF which stay on the 577 | # free scale - these are only corrected for pressure) are 578 | # 1) evaluated as they are given in the literature 579 | # 2) converted to the SWS scale in mol/kg-SW or to the NBS scale 580 | # 3) corrected for pressure 581 | # 4) converted to the SWS pH scale in mol/kg-SW 582 | # 5) converted to the chosen pH scale 583 | # 584 | # PROGRAMMER'S NOTE: all logs are log base e 585 | # PROGRAMMER'S NOTE: all Constants are converted to the pH scale 586 | # pHScale% (the chosen one) in units of mol/kg-SW 587 | # except KS and KF are on the free scale 588 | # and KW is in units of (mol/kg-SW)^2 589 | global pHScale, WhichKs, WhoseKSO4, sqrSal, Pbar, RT 590 | global K0, fH, FugFac, VPFac, ntps, TempK, logTempK 591 | global K1, K2, KW, KB, KF, KS, KP1, KP2, KP3, KSi 592 | global TB, TF, TS, TP, TSi, RGasConstant, Sal 593 | 594 | TempK = TempC + 273.15 595 | RT = RGasConstant * TempK 596 | logTempK = np.log(TempK) 597 | Pbar = Pdbar / 10 598 | 599 | # Generate empty vectors for holding results 600 | TB = np.empty(ntps) 601 | TB[:] = np.nan 602 | TF = np.empty(ntps) 603 | TF[:] = np.nan 604 | TS = np.empty(ntps) 605 | TS[:] = np.nan 606 | 607 | # CalculateTB - Total Borate: 608 | F = WhichKs == 8 # Pure water case. 609 | if np.any(F): 610 | TB[F] = 0 611 | F = WhichKs == 6 or WhichKs == 7 612 | if np.any(F): 613 | TB[F] = 0.0004106 * Sal[F] / 35 # in mol/kg-sw 614 | # this is .00001173.*Sali 615 | # this is about 1% lower than Uppstrom's value 616 | # Culkin, F., in Chemical Oceanography, 617 | # ed. Riley and Skirrow, 1965: 618 | # GEOSECS references this, but this value is not explicitly 619 | # given here 620 | F = WhichKs != 6 and WhichKs != 7 and WhichKs != 8 # All other cases 621 | if np.any(F): 622 | FF = F and (WhoseKSO4 == 1 or WhoseKSO4 ==2) # If user opted for Uppstrom's values: 623 | if np.any(FF): 624 | # Uppstrom, L., Deep-Sea Research 21:161-162, 1974: 625 | # this is .000416.*Sali./35. = .0000119.*Sali 626 | # TB(FF) = (0.000232./10.811).*(Sal(FF)./1.80655); % in mol/kg-SW 627 | TB[FF] = 0.0004157 * Sal[FF] / 35 # in mol/kg-SW 628 | FF = F and (WhoseKSO4 == 3 or WhoseKSO4 == 4) # if user opted for the new Lee values: 629 | if np.any(FF): 630 | # Lee, Kim, Byrne, Millero, Feely, Yong-Ming Liu. 2010. 631 | # Geochimica Et Cosmochimica Acta 74 (6): 1801–1811. 632 | TB[FF] = 0.0004326 * Sal[FF] / 35 # in mol/kg-SW 633 | 634 | # CalculateTF 635 | # Riley, J. P., Deep-Sea Research 12:219-220, 1965: 636 | # this is .000068.*Sali./35. = .00000195.*Sali 637 | TF = (0.000067 / 18.998) * (Sal / 1.80655) # in mol/kg-SW 638 | 639 | # CalculateTS 640 | # Morris, A. W., and Riley, J. P., Deep-Sea Research 13:699-705, 1966: 641 | # this is .02824.*Sali./35. = .0008067.*Sali 642 | TS = (0.14 / 96.062) * (Sal / 1.80655) # in mol/kg-SW 643 | 644 | # CalculateK0 645 | # Weiss, R. F., Marine Chemistry 2:203-215, 1974. 646 | TempK100 = TempK / 100 647 | lnK0 = -60.2409 + 93.4517 / TempK100 + 23.3585 * np.log(TempK100) + Sal * (0.023517 - 0.023656 * TempK100 + 0.0047036 * TempK100 ** 2) 648 | K0 = np.exp(lnK0) 649 | 650 | # CalculateIonS: 651 | # This is from the DOE handbook, Chapter 5, p. 13/22, eq. 7.2.4: 652 | IonS = 19.924 * Sal / (1000 - 1.005 * Sal) 653 | 654 | # CalculateKS 655 | lnKS = np.empty(ntps) 656 | lnKS[:] = np.nan 657 | pKS = np.empty(ntps) 658 | pKS[:] = np.nan 659 | KS = np.empty(ntps) 660 | KS[:] = np.nan 661 | 662 | F = WhoseKSO4 == 1 or WhoseKSO4 == 3 663 | if np.any(F): 664 | # Dickson, A. G., J. Chemical Thermodynamics, 22:113-127, 1990 665 | # The goodness of fit is .021. 666 | # It was given in mol/kg-H2O. I convert it to mol/kg-SW. 667 | # TYPO on p. 121: the constant e9 should be e8. 668 | # This is from eqs 22 and 23 on p. 123, and Table 4 on p 121: 669 | lnKS[F] = -4276.1 / TempK[F] + 141.328 - 23.093 * logTempK[F] + (-13856 / TempK[F] + 324.57 - 47.986 * logTempK[F]) * np.sqrt(IonS[F]) + (35474 / TempK[F] - 771.54 + 114.723 * logTempK[F]) * IonS[F] + (-2698 / TempK[F]) * np.sqrt(IonS[F]) * IonS[F] + (1776 / TempK[F]) * IonS[F] ** 2 670 | KS[F] = np.exp(lnKS[F]) * (1 - 0.001005 * Sal[F]) # this first multiple is on the free pH scale in mol/kg-H2O; then convert to mol/kg-SW 671 | F = WhoseKSO4 == 2 or WhoseKSO4 == 4 672 | if np.any(F): 673 | # Khoo, et al, Analytical Chemistry, 49(1):29-34, 1977 674 | # KS was found by titrations with a hydrogen electrode 675 | # of artificial seawater containing sulfate (but without F) 676 | # at 3 salinities from 20 to 45 and artificial seawater NOT 677 | # containing sulfate (nor F) at 16 salinities from 15 to 45, 678 | # both at temperatures from 5 to 40 deg C. 679 | # KS is on the Free pH scale (inherently so). 680 | # It was given in mol/kg-H2O. I convert it to mol/kg-SW. 681 | # He finds log(beta) which = my pKS; 682 | # his beta is an association constant. 683 | # The rms error is .0021 in pKS, or about .5% in KS. 684 | # This is equation 20 on p. 33: 685 | pKS[F] = 647.59 / TempK[F] - 6.3451 + 0.019085 * TempK[F] - 0.5208 * np.sqrt(IonS[F]) 686 | KS[F] = 10 ** -pKS[F] * (1 - 0.001005 * Sal[F]) # this first multiple is on the free pH scale in mol/kg-H2O; then convert to mol/kg-SW 687 | 688 | # CalculateKF: 689 | # Dickson, A. G. and Riley, J. P., Marine Chemistry 7:89-99, 1979: 690 | lnKF = 1590.2 / TempK - 12.641 + 1.525 * IonS ** 0.5 691 | KF = (np.exp(lnKF) # this is on the free pH scale in mol/kg-H2O 692 | * (1 - 0.001005 * Sal)) # convert to mol/kg-SW 693 | # Another expression exists for KF: Perez and Fraga 1987. Not used here since ill defined for low salinity. (to be used for S: 10-40, T: 9-33) 694 | # Nonetheless, P&F87 might actually be better than the fit of D&R79 above, which is based on only three salinities: [0 26.7 34.6] 695 | # lnKF = 874./TempK - 9.68 + 0.111.*Sal.^0.5; 696 | # KF = exp(lnKF); % this is on the free pH scale in mol/kg-SW 697 | 698 | 699 | # CalculatepHScaleConversionFactors: 700 | # These are NOT pressure-corrected. 701 | SWStoTOT = (1 + TS / KS) / (1 + TS / KS + TF / KF) 702 | FREEtoTOT = 1 + TS / KS 703 | 704 | # CalculatefH 705 | fH = np.empty(ntps) 706 | fH[:] = np.nan 707 | # Use GEOSECS's value for cases 1,2,3,4,5 (and 6) to convert pH scales. 708 | F = WhichKs == 8 709 | if np.any(F): 710 | fH[F] = 1 # This shouldn't occur in the program for this case 711 | F = WhichKs == 7 712 | if np.any(F): 713 | fH[F] = 1.29 - 0.00204 * TempK[F] + (0.00046 - 0.00000148 * TempK[F]) * Sal[F] * Sal[F] 714 | # Peng et al, Tellus 39B:439-458, 1987: 715 | # They reference the GEOSECS report, but round the value 716 | # given there off so that it is about .008 (1%) lower. It 717 | # doesn't agree with the check value they give on p. 456. 718 | F = WhichKs != 7 and WhichKs != 8 719 | if np.any(F): 720 | fH[F] = 1.2948 - 0.002036 * TempK[F] + (0.0004607 - 0.000001475 * TempK[F]) * Sal[F] ** 2 721 | # Takahashi et al, Chapter 3 in GEOSECS Pacific Expedition, 722 | # v. 3, 1982 (p. 80); 723 | 724 | # CalculateKB 725 | KB = np.empty(ntps) 726 | KB[:] = np.nan 727 | logKB = np.empty(ntps) 728 | logKB[:] = np.nan 729 | lnKBtop = np.empty(ntps) 730 | lnKBtop[:] = np.nan 731 | lnKB = np.empty(ntps) 732 | lnKB[:] = np.nan 733 | F = WhichKs == 8 # Pure water case 734 | if np.any(F): 735 | KB[F] = 0 736 | F = WhichKs == 6 or WhichKs == 7 737 | if np.any(F): 738 | # This is for GEOSECS and Peng et al. 739 | # Lyman, John, UCLA Thesis, 1957 740 | # fit by Li et al, JGR 74:5507-5525, 1969: 741 | logKB[F] = -9.26 + 0.00886 * Sal[F] + 0.01 * TempC[F] 742 | logKB[F] = -9.26 + 0.00886 * Sal[F] + 0.01 * TempC[F] 743 | KB[F] = 10 **(logKB[F]) / fH[F] # Numerator is on NBS scale; denominator is converted to SWS scale 744 | F = WhichKs != 6 and WhichKs != 7 and WhichKs != 8 745 | if np.any(F): 746 | # Dickson, A. G., Deep-Sea Research 37:755-766, 1990: 747 | lnKBtop[F] = -8966.9 - 2890.53 * sqrSal[F] - 77.942 * Sal[F] + 1.728 * sqrSal[F] * Sal[F] - 0.0996 * Sal[F] ** 2 748 | lnKB[F] = lnKBtop[F] / TempK[F] + 148.0248 + 137.1942 * sqrSal[F] + 1.62142 * Sal[F] + (-24.4344 - 25.085 * sqrSal[F] - 0.2474 * Sal[F]) * logTempK[F] + 0.053105 * sqrSal[F] * TempK[F] 749 | KB[F] = np.exp(lnKB[F]) / SWStoTOT[F] # Numerator is on total pH scale in mol/kg-SW; denominator convert to SWS pH scale 750 | 751 | # CalculateKW 752 | lnKW = np.empty(ntps) 753 | lnKW[:] = np.nan 754 | KW = np.empty(ntps) 755 | KW[:] = np.nan 756 | 757 | F = WhichKs == 7 758 | if np.any(F): 759 | # Millero, Geochemica et Cosmochemica Acta 43:1651-1661, 1979 760 | lnKW[F] = 148.9802 - 13847.26 / TempK[F] - 23.6521 * logTempK[F] + (-79.2447 + 3298.72 / TempK[F] + 12.0408 * logTempK[F]) * sqrSal[F] - 0.019813 * Sal[F] 761 | F = WhichKs == 8 762 | if np.any(F): 763 | # Millero, Geochemica et Cosmochemica Acta 43:1651-1661, 1979 764 | # refit data of Harned and Owen, The Physical Chemistry of 765 | # Electrolyte Solutions, 1958 766 | lnKW[F] = 148.9802 - 13847.26 / TempK[F] - 23.6521 * logTempK[F] 767 | F = WhichKs != 6 and WhichKs != 7 and WhichKs != 8 768 | if np.any(F): 769 | # Millero, Geochemica et Cosmochemica Acta 59:661-677, 1995. 770 | # his check value of 1.6 umol/kg-SW should be 6.2 771 | lnKW[F] = 148.9802 - 13847.26 / TempK[F] - 23.6521 * logTempK[F] + (-5.977 + 118.67 / TempK[F] + 1.0495 * logTempK[F]) * sqrSal[F] - 0.01615 * Sal[F] 772 | KW = np.exp(lnKW) # This is on the SWS pH scale in (mol/kg-SW)^2 773 | F = WhichKs == 6 774 | if np.any(F): 775 | KW[F] = 0 # GEOSECS doesn't include OH effects 776 | 777 | # CalculateKP1KP2KP3KSi: 778 | KP1 = np.empty(ntps) 779 | KP1[:] = np.nan 780 | KP2 = np.empty(ntps) 781 | KP2[:] = np.nan 782 | KP3 = np.empty(ntps) 783 | KP3[:] = np.nan 784 | KSi = np.empty(ntps) 785 | KSi[:] = np.nan 786 | 787 | lnKP1 = np.empty(ntps) 788 | lnKP1[:] = np.nan 789 | lnKP2 = np.empty(ntps) 790 | lnKP2[:] = np.nan 791 | lnKP3 = np.empty(ntps) 792 | lnKP3[:] = np.nan 793 | lnKSi = np.empty(ntps) 794 | lnKSi[:] = np.nan 795 | 796 | F = WhichKs == 7 797 | if np.any(F): 798 | KP1[F] = 0.02 799 | # Peng et al don't include the contribution from this term, 800 | # but it is so small it doesn't contribute. It needs to be 801 | # kept so that the routines work ok. 802 | # KP2, KP3 from Kester, D. R., and Pytkowicz, R. M., 803 | # Limnology and Oceanography 12:243-252, 1967: 804 | # these are only for sals 33 to 36 and are on the NBS scale 805 | KP2[F] = np.exp(-9.039 - 1450 / TempK[F]) / fH[F] # Numerator is on NBS scale; denominator convert to SWS scale 806 | KP3[F] = np.exp(4.466 - 7276 / TempK[F]) / fH[F] # Numerator is on NBS scale; denominator convert to SWS scale 807 | # Sillen, Martell, and Bjerrum, Stability Constants of metal-ion complexes, 808 | # The Chemical Society (London), Special Publ. 17:751, 1964: 809 | KSi[F] = 0.0000000004 / fH[F] # Numerator is NBS scale; denominator convert to SWS scale 810 | F = WhichKs == 6 or WhichKs == 8 811 | if np.any(F): 812 | KP1[F] = 0 813 | KP2[F] = 0 814 | KP3[F] = 0 815 | KSi[F] = 0 816 | # Neither the GEOSECS choice nor the freshwater choice 817 | # include contributions from phosphate or silicate. 818 | F = WhichKs != 6 and WhichKs != 7 and WhichKs != 8 819 | if np.any(F): 820 | # Yao and Millero, Aquatic Geochemistry 1:53-88, 1995 821 | # KP1, KP2, KP3 are on the SWS pH scale in mol/kg-SW. 822 | # KSi was given on the SWS pH scale in molal units. 823 | lnKP1[F] = -4576.752 / TempK[F] + 115.54 - 18.453 * logTempK[F] + (-106.736 / TempK[F] + 0.69171) * sqrSal[F] + (-0.65643 / TempK[F] - 0.01844) * Sal[F] 824 | KP1[F] = np.exp(lnKP1[F]) 825 | lnKP2[F] = -8814.715 / TempK[F] + 172.1033 - 27.927 * logTempK[F] + (-160.34 / TempK[F] + 1.3566) * sqrSal[F] + (0.37335 / TempK[F] - 0.05778) * Sal[F] 826 | KP2[F] = np.exp(lnKP2[F]) 827 | lnKP3[F] = -3070.75 / TempK[F] - 18.126 + (17.27039 / TempK[F] + 2.81197) * sqrSal[F] + (-44.99486 / TempK[F] - 0.09984) * Sal[F] 828 | KP3[F] = np.exp(lnKP3[F]) 829 | lnKSi[F] = -8904.2 / TempK[F] + 117.4 - 19.334 * logTempK[F] + (-458.79 / TempK[F] + 3.5913) * np.sqrt(IonS[F]) + (188.74 / TempK[F] - 1.5998) * IonS[F] + (-12.1652 / TempK[F] + 0.07871) * IonS[F]**2 830 | KSi[F] = np.exp(lnKSi[F]) * (1 - 0.001005 * Sal[F]) # this is on the SWS pH scale in mol/kg-H2O; convert to mol/kg-SW 831 | 832 | # CalculateK1K2 833 | logK1 = np.empty(ntps) 834 | logK1[:] = np.nan 835 | lnK1 = np.empty(ntps) 836 | lnK1[:] = np.nan 837 | pK1 = np.empty(ntps) 838 | pK1[:] = np.nan 839 | K1 = np.empty(ntps) 840 | K1[:] = np.nan 841 | K2 = np.empty(ntps) 842 | K2[:] = np.nan 843 | logK2 = np.empty(ntps) 844 | logK2[:] = np.nan 845 | lnK2 = np.empty(ntps) 846 | lnK2[:] = np.nan 847 | pK2 = np.empty(ntps) 848 | pK2[:] = np.nan 849 | 850 | F = WhichKs == 1 851 | if np.any(F): 852 | # ROY et al, Marine Chemistry, 44:249-267, 1993 853 | # (see also: Erratum, Marine Chemistry 45:337, 1994 854 | # and Erratum, Marine Chemistry 52:183, 1996) 855 | # Typo: in the abstract on p. 249: in the eq. for lnK1* the 856 | # last term should have S raised to the power 1.5. 857 | # They claim standard deviations (p. 254) of the fits as 858 | # .0048 for lnK1 (.5% in K1) and .007 in lnK2 (.7% in K2). 859 | # They also claim (p. 258) 2s precisions of .004 in pK1 and 860 | # .006 in pK2. These are consistent, but Andrew Dickson 861 | # (personal communication) obtained an rms deviation of about 862 | # .004 in pK1 and .003 in pK2. This would be a 2s precision 863 | # of about 2% in K1 and 1.5% in K2. 864 | # T: 0-45 S: 5-45. Total Scale. Artificial sewater. 865 | # This is eq. 29 on p. 254 and what they use in their abstract: 866 | lnK1[F] = 2.83655 - 2307.1266 / TempK[F] - 1.5529413 * logTempK[F] + (-0.20760841 - 4.0484 / TempK[F]) * sqrSal[F] + 0.08468345 * Sal[F] - 0.00654208 * sqrSal[F] * Sal[F] 867 | K1[F] = (np.exp(lnK1[F]) # this is on the total pH scale in mol/kg-H2O 868 | * (1 - 0.001005 * Sal[F]) # convert to mol/kg-SW 869 | / SWStoTOT[F]) # convert to SWS pH scale 870 | # This is eq. 30 on p. 254 and what they use in their abstract: 871 | lnK2[F] = (-9.226508 - 3351.6106 / TempK[F] - 0.2005743 * logTempK[F] + 872 | (-0.106901773 - 23.9722 / TempK[F]) * sqrSal[F] + 0.1130822 * Sal[F] - 873 | 0.00846934 * sqrSal[F] * Sal[F]) 874 | K2[F] = (np.exp(lnK2[F]) # this is on the total pH scale in mol/kg-H2O 875 | * (1 - 0.001005 * Sal[F]) # convert to mol/kg-SW 876 | / SWStoTOT[F]) # convert to SWS pH scale 877 | F = WhichKs == 2 878 | if np.any(F): 879 | # GOYET AND POISSON, Deep-Sea Research, 36(11):1635-1654, 1989 880 | # The 2s precision in pK1 is .011, or 2.5% in K1. 881 | # The 2s precision in pK2 is .02, or 4.5% in K2. 882 | # This is in Table 5 on p. 1652 and what they use in the abstract: 883 | pK1[F] = (812.27 / TempK[F] + 3.356 - 0.00171 * Sal[F] * logTempK[F] 884 | + 0.000091 * Sal[F]**2) 885 | K1[F] = 10**-pK1[F] # this is on the SWS pH scale in mol/kg-SW 886 | # This is in Table 5 on p. 1652 and what they use in the abstract: 887 | pK2[F] = (1450.87 / TempK[F] + 4.604 - 0.00385 * Sal[F] * logTempK[F] 888 | + 0.000182 * Sal[F]**2) 889 | K2[F] = 10 ** -pK2[F] # this is on the SWS pH scale in mol/kg-SW 890 | F = WhichKs == 3 891 | if np.any(F): 892 | # HANSSON refit BY DICKSON AND MILLERO 893 | # Dickson and Millero, Deep-Sea Research, 34(10):1733-1743, 1987 894 | # (see also Corrigenda, Deep-Sea Research, 36:983, 1989) 895 | # refit data of Hansson, Deep-Sea Research, 20:461-478, 1973 896 | # and Hansson, Acta Chemica Scandanavia, 27:931-944, 1973. 897 | # on the SWS pH scale in mol/kg-SW. 898 | # Hansson gave his results on the Total scale (he called it 899 | # the seawater scale) and in mol/kg-SW. 900 | # Typo in DM on p. 1739 in Table 4: the equation for pK2* 901 | # for Hansson should have a .000132 *S^2 902 | # instead of a .000116 *S^2. 903 | # The 2s precision in pK1 is .013, or 3% in K1. 904 | # The 2s precision in pK2 is .017, or 4.1% in K2. 905 | # This is from Table 4 on p. 1739. 906 | pK1[F] = 851.4 / TempK[F] + 3.237 - 0.0106 * Sal[F] + 0.000105 * Sal[F]**2 907 | K1[F] = 10 ** -pK1[F] # this is on the SWS pH scale in mol/kg-SW 908 | # This is from Table 4 on p. 1739. 909 | pK2[F] = (-3885.4 / TempK[F] + 125.844 - 18.141 * logTempK[F] 910 | - 0.0192 * Sal[F] + 0.000132 * Sal[F] ** 2) 911 | K2[F] = 10**-pK2[F] # this is on the SWS pH scale in mol/kg-SW 912 | F = WhichKs == 4 913 | if np.any(F): 914 | # MEHRBACH refit BY DICKSON AND MILLERO 915 | # Dickson and Millero, Deep-Sea Research, 34(10):1733-1743, 1987 916 | # (see also Corrigenda, Deep-Sea Research, 36:983, 1989) 917 | # refit data of Mehrbach et al, Limn Oc, 18(6):897-907, 1973 918 | # on the SWS pH scale in mol/kg-SW. 919 | # Mehrbach et al gave results on the NBS scale. 920 | # The 2s precision in pK1 is .011, or 2.6% in K1. 921 | # The 2s precision in pK2 is .020, or 4.6% in K2. 922 | # Valid for salinity 20-40. 923 | # This is in Table 4 on p. 1739. 924 | pK1[F] = (3670.7 / TempK[F] - 62.008 + 9.7944 * logTempK[F] 925 | - 0.0118 * Sal[F] + 0.000116 * Sal[F] ** 2) 926 | K1[F] = 10 ** -pK1[F] # this is on the SWS pH scale in mol/kg-SW 927 | # This is in Table 4 on p. 1739. 928 | pK2[F] = 1394.7 / TempK[F] + 4.777 - 0.0184 * Sal[F] + 0.000118 * Sal[F] ** 2 929 | K2[F] = 10 ** -pK2[F] # this is on the SWS pH scale in mol/kg-SW 930 | F = WhichKs == 5 931 | if np.any(F): 932 | # HANSSON and MEHRBACH refit BY DICKSON AND MILLERO 933 | # Dickson and Millero, Deep-Sea Research,34(10):1733-1743, 1987 934 | # (see also Corrigenda, Deep-Sea Research, 36:983, 1989) 935 | # refit data of Hansson, Deep-Sea Research, 20:461-478, 1973, 936 | # Hansson, Acta Chemica Scandanavia, 27:931-944, 1973, 937 | # and Mehrbach et al, Limnol. Oceanogr.,18(6):897-907, 1973 938 | # on the SWS pH scale in mol/kg-SW. 939 | # Typo in DM on p. 1740 in Table 5: the second equation 940 | # should be pK2* =, not pK1* =. 941 | # The 2s precision in pK1 is .017, or 4% in K1. 942 | # The 2s precision in pK2 is .026, or 6% in K2. 943 | # Valid for salinity 20-40. 944 | # This is in Table 5 on p. 1740. 945 | pK1[F] = 845 / TempK[F] + 3.248 - 0.0098 * Sal[F] + 0.000087 * Sal[F] ** 2 946 | K1[F] = 10 ** -pK1[F] # this is on the SWS pH scale in mol/kg-SW 947 | # This is in Table 5 on p. 1740. 948 | pK2[F] = 1377.3 / TempK[F] + 4.824 - 0.0185 * Sal[F] + 0.000122 * Sal[F] ** 2 949 | K2[F] = 10 ** -pK2[F] # this is on the SWS pH scale in mol/kg-SW 950 | F = WhichKs == 6 or WhichKs == 7 951 | if np.any(F): 952 | # GEOSECS and Peng et al use K1, K2 from Mehrbach et al, 953 | # Limnology and Oceanography, 18(6):897-907, 1973. 954 | # I.e., these are the original Mehrbach dissociation constants. 955 | # The 2s precision in pK1 is .005, or 1.2% in K1. 956 | # The 2s precision in pK2 is .008, or 2% in K2. 957 | pK1[F] = (-13.7201 + 0.031334 * TempK[F] + 3235.76 / TempK[F] 958 | + 1.3e-5 * Sal[F] * TempK[F] - 0.1032 * Sal[F] ** 0.5) 959 | K1[F] = (10 ** -pK1(F) # this is on the NBS scale 960 | / fH[F]) # convert to SWS scale 961 | pK2[F] = (5371.9645 + 1.671221 * TempK[F] + 0.22913 * Sal[F] + 18.3802 * np.log10(Sal[F]) 962 | - 128375.28 / TempK[F] - 2194.3055 * np.log10(TempK[F]) - 8.0944e-4 * Sal[F] * TempK[F] 963 | - 5617.11 * np.log10(Sal[F]) / TempK[F] + 2.136 * Sal[F] / TempK[F]) # pK2 is not defined for Sal=0, since log10(0)=-inf 964 | K2[F] = (10 ** -pK2[F] # this is on the NBS scale 965 | / fH[F]) # convert to SWS scale 966 | F = WhichKs == 8 967 | if np.any(F): 968 | # PURE WATER CASE 969 | # Millero, F. J., Geochemica et Cosmochemica Acta 43:1651-1661, 1979: 970 | # K1 from refit data from Harned and Davis, 971 | # J American Chemical Society, 65:2030-2037, 1943. 972 | # K2 from refit data from Harned and Scholes, 973 | # J American Chemical Society, 43:1706-1709, 1941. 974 | # This is only to be used for Sal=0 water (note the absence of S in the below formulations) 975 | # These are the thermodynamic Constants: 976 | lnK1[F] = 290.9097 - 14554.21 / TempK[F] - 45.0575 * logTempK[F] 977 | K1[F] = np.exp(lnK1[F]) 978 | lnK2[F] = 207.6548 - 11843.79 / TempK[F] - 33.6485 * logTempK[F] 979 | K2[F] = np.exp(lnK2[F]) 980 | F = WhichKs == 9 981 | if np.any(F): 982 | # From Cai and Wang 1998, for estuarine use. 983 | # Data used in this work is from: 984 | # K1: Merhback (1973) for S>15, for S<15: Mook and Keone (1975) 985 | # K2: Merhback (1973) for S>20, for S<20: Edmond and Gieskes (1970) 986 | # Sigma of residuals between fits and above data: ±0.015, +0.040 for K1 and K2, respectively. 987 | # Sal 0-40, Temp 0.2-30 988 | # Limnol. Oceanogr. 43(4) (1998) 657-668 989 | # On the NBS scale 990 | # Their check values for F1 don't work out, not sure if this was correctly published... 991 | F1 = 200.1 / TempK[F] + 0.3220 992 | pK1[F] = 3404.71 / TempK[F] + 0.032786 * TempK[F] - 14.8435 - 0.071692 * F1 * Sal[F] ** 0.5 + 0.0021487 * Sal[F] 993 | K1[F] = (10 ** -pK1[F] # this is on the NBS scale 994 | / fH[F]) # convert to SWS scale (uncertain at low Sal due to junction potential); 995 | F2 = -129.24 / TempK[F] + 1.4381 996 | pK2[F] = 2902.39 / TempK[F] + 0.02379 * TempK[F] - 6.4980 - 0.3191 * F2 * Sal[F] ** 0.5 + 0.0198 * Sal[F] 997 | K2[F] = (10 ** -pK2[F] # this is on the NBS scale 998 | / fH[F]) # convert to SWS scale (uncertain at low Sal due to junction potential); 999 | F = WhichKs == 10 1000 | if np.any(F): 1001 | # From Lueker, Dickson, Keeling, 2000 1002 | # This is Mehrbach's data refit after conversion to the total scale, for comparison with their equilibrator work. 1003 | # Mar. Chem. 70 (2000) 105-119 1004 | # Total scale and kg-sw 1005 | pK1[F] = 3633.86 / TempK[F] - 61.2172 + 9.6777 * np.log(TempK[F]) - 0.011555 * Sal[F] + 0.0001152 * Sal[F] ** 2 1006 | K1[F] = (10 ** -pK1[F] # this is on the total pH scale in mol/kg-SW 1007 | / SWStoTOT[F]) # convert to SWS pH scale 1008 | pK2[F] = 471.78 / TempK[F] + 25.929 -3.16967 * np.log(TempK[F]) - 0.01781 * Sal[F] + 0.0001122 * Sal[F] ** 2 1009 | K2[F] = (10 ** -pK2[F] # this is on the total pH scale in mol/kg-SW 1010 | / SWStoTOT[F]) # convert to SWS pH scale 1011 | F = WhichKs == 11 1012 | if np.any(F): # STOPPED at ln 1032 1013 | # Mojica Prieto and Millero 2002. Geochim. et Cosmochim. Acta. 66(14) 2529-2540. 1014 | # sigma for pK1 is reported to be 0.0056 1015 | # sigma for pK2 is reported to be 0.010 1016 | # This is from the abstract and pages 2536-2537 1017 | pK1 = -43.6977 - 0.0129037 * Sal[F] + 1.364e-4 * Sal[F] ** 2 + 2885.378 / TempK[F] + 7.045159 * np.log(TempK[F]) 1018 | pK2 = (-452.0940 + 13.142162 * Sal[F] - 8.101e-4 * Sal[F] ** 2 + 21263.61 / TempK[F] + 68.483143 * np.log(TempK[F]) 1019 | + (-581.4428 * Sal[F] + 0.259601 * Sal[F] ** 2) / TempK[F] - 1.967035 * Sal[F] * np.log(TempK[F])) 1020 | K1[F] = 10 ** -pK1 # this is on the SWS pH scale in mol/kg-SW 1021 | K2[F] = 10 ** -pK2 # this is on the SWS pH scale in mol/kg-SW 1022 | F = WhichKs == 12 1023 | if np.any(F): 1024 | # Millero et al., 2002. Deep-Sea Res. I (49) 1705-1723. 1025 | # Calculated from overdetermined WOCE-era field measurements 1026 | # sigma for pK1 is reported to be 0.005 1027 | # sigma for pK2 is reported to be 0.008 1028 | # This is from page 1715 1029 | pK1 = 6.359 - 0.00664 * Sal[F] - 0.01322 * TempC[F] + 4.989e-5 * TempC[F] ** 2 1030 | pK2 = 9.867 - 0.01314 * Sal[F] - 0.01904 * TempC[F] + 2.448e-5 * TempC[F] ** 2 1031 | K1[F] = 10 ** -pK1 # this is on the SWS pH scale in mol/kg-SW 1032 | K2[F] = 10 ** -pK2 # this is on the SWS pH scale in mol/kg-SW 1033 | F = WhichKs == 13 1034 | if np.any(F): 1035 | # From Millero 2006 work on pK1 and pK2 from titrations 1036 | # Millero, Graham, Huang, Bustos-Serrano, Pierrot. Mar.Chem. 100 (2006) 80-94. 1037 | # S=1 to 50, T=0 to 50. On seawater scale (SWS). From titrations in Gulf Stream seawater. 1038 | pK1_0 = -126.34048 + 6320.813 / TempK[F] + 19.568224 * np.log(TempK[F]) 1039 | A_1 = 13.4191 * Sal[F] ** 0.5 + 0.0331 * Sal[F] - 5.33e-5 * Sal[F] ** 2 1040 | B_1 = -530.123 * Sal[F] ** 0.5 - 6.103 * Sal[F] 1041 | C_1 = -2.06950 * Sal[F] ** 0.5 1042 | pK1[F] = A_1 + B_1 / TempK[F] + C_1 * np.log(TempK[F]) + pK1_0 # pK1 sigma = 0.0054 1043 | K1[F] = 10 ** -pK1[F] 1044 | pK2_0= -90.18333 + 5143.692 / TempK[F] + 14.613358 * np.log(TempK[F]) 1045 | A_2 = 21.0894 * Sal[F] ** 0.5 + 0.1248 * Sal[F] - 3.687e-4 * Sal[F] ** 2 1046 | B_2 = -772.483 * Sal[F] ** 0.5 - 20.051 * Sal[F] 1047 | C_2 = -3.3336 * Sal[F] ** 0.5 1048 | pK2[F] = A_2 + B_2 / TempK[F] + C_2 * np.log(TempK[F]) + pK2_0 # pK2 sigma = 0.011 1049 | K2[F] = 10 ** -pK2[F] 1050 | F = WhichKs == 14 1051 | if np.any(F): 1052 | # From Millero, 2010, also for estuarine use. 1053 | # Marine and Freshwater Research, v. 61, p. 139–142. 1054 | # Fits through compilation of real seawater titration results: 1055 | # Mehrbach et al. (1973), Mojica-Prieto & Millero (2002), Millero et al. (2006) 1056 | # Constants for K's on the SWS; 1057 | # This is from page 141 1058 | pK10 = -126.34048 + 6320.813 / TempK[F] + 19.568224 * np.log(TempK[F]) 1059 | # This is from their table 2, page 140. 1060 | A1 = 13.4038 * Sal[F] ** 0.5 + 0.03206 * Sal[F] - 5.242e-5 * Sal[F] ** 2 1061 | B1 = -530.659 * Sal[F] ** 0.5 - 5.8210 * Sal[F] 1062 | C1 = -2.0664 * Sal[F] ** 0.5 1063 | pK1 = pK10 + A1 + B1 / TempK[F] + C1 * np.log(TempK[F]) 1064 | K1[F] = 10 ** -pK1 1065 | # This is from page 141 1066 | pK20 = -90.18333 + 5143.692 / TempK[F] + 14.613358 * np.log(TempK[F]) 1067 | # This is from their table 3, page 140. 1068 | A2 = 21.3728 * Sal[F] ** 0.5 + 0.1218 * Sal(F) - 3.688e-4 * Sal[F] ** 2 1069 | B2 = -788.289 * Sal[F] ** 0.5 - 19.189 * Sal[F] 1070 | C2 = -3.374 * Sal[F] ** 0.5 1071 | pK2 = pK20 + A2 + B2 / TempK[F] + C2 * np.log(TempK[F]) 1072 | K2[F] = 10 ** -pK2 1073 | 1074 | #CorrectKsForPressureNow: 1075 | # Currently: For WhichKs% = 1 to 7, all Ks (except KF and KS, which are on 1076 | # the free scale) are on the SWS scale. 1077 | # For WhichKs% = 6, KW set to 0, KP1, KP2, KP3, KSi don't matter. 1078 | # For WhichKs% = 8, K1, K2, and KW are on the "pH" pH scale 1079 | # (the pH scales are the same in this case); the other Ks don't matter. 1080 | # 1081 | # 1082 | # No salinity dependence is given for the pressure coefficients here. 1083 | # It is assumed that the salinity is at or very near Sali = 35. 1084 | # These are valid for the SWS pH scale, but the difference between this and 1085 | # the total only yields a difference of .004 pH units at 1000 bars, much 1086 | # less than the uncertainties in the values. 1087 | # The sources used are: 1088 | # Millero, 1995: 1089 | # Millero, F. J., Thermodynamics of the carbon dioxide system in the 1090 | # oceans, Geochemica et Cosmochemica Acta 59:661-677, 1995. 1091 | # See table 9 and eqs. 90-92, p. 675. 1092 | # TYPO: a factor of 10^3 was left out of the definition of Kappa 1093 | # TYPO: the value of R given is incorrect with the wrong units 1094 | # TYPO: the values of the a's for H2S and H2O are from the 1983 1095 | # values for fresh water 1096 | # TYPO: the value of a1 for B(OH)3 should be +.1622 1097 | # Table 9 on p. 675 has no values for Si. 1098 | # There are a variety of other typos in Table 9 on p. 675. 1099 | # There are other typos in the paper, and most of the check values 1100 | # given don't check. 1101 | # Millero, 1992: 1102 | # Millero, Frank J., and Sohn, Mary L., Chemical Oceanography, 1103 | # CRC Press, 1992. See chapter 6. 1104 | # TYPO: this chapter has numerous typos (eqs. 36, 52, 56, 65, 72, 1105 | # 79, and 96 have typos). 1106 | # Millero, 1983: 1107 | # Millero, Frank J., Influence of pressure on chemical processes in 1108 | # the sea. Chapter 43 in Chemical Oceanography, eds. Riley, J. P. and 1109 | # Chester, R., Academic Press, 1983. 1110 | # TYPO: p. 51, eq. 94: the value -26.69 should be -25.59 1111 | # TYPO: p. 51, eq. 95: the term .1700t should be .0800t 1112 | # these two are necessary to match the values given in Table 43.24 1113 | # Millero, 1979: 1114 | # Millero, F. J., The thermodynamics of the carbon dioxide system 1115 | # in seawater, Geochemica et Cosmochemica Acta 43:1651-1661, 1979. 1116 | # See table 5 and eqs. 7, 7a, 7b on pp. 1656-1657. 1117 | # Takahashi et al, in GEOSECS Pacific Expedition, v. 3, 1982. 1118 | # TYPO: the pressure dependence of K2 should have a 16.4, not 26.4 1119 | # This matches the GEOSECS results and is in Edmond and Gieskes. 1120 | # Culberson, C. H. and Pytkowicz, R. M., Effect of pressure on carbonic acid, 1121 | # boric acid, and the pH of seawater, Limnology and Oceanography 1122 | # 13:403-417, 1968. 1123 | # Edmond, John M. and Gieskes, J. M. T. M., The calculation of the degree of 1124 | # seawater with respect to calcium carbonate under in situ conditions, 1125 | # Geochemica et Cosmochemica Acta, 34:1261-1291, 1970. 1126 | #**************************************************************************** 1127 | # These references often disagree and give different fits for the same thing. 1128 | # They are not always just an update either; that is, Millero, 1995 may agree 1129 | # with Millero, 1979, but differ from Millero, 1983. 1130 | # For WhichKs% = 7 (Peng choice) I used the same factors for KW, KP1, KP2, 1131 | # KP3, and KSi as for the other cases. Peng et al didn't consider the 1132 | # case of P different from 0. GEOSECS did consider pressure, but didn't 1133 | # include Phos, Si, or OH, so including the factors here won't matter. 1134 | # For WhichKs% = 8 (freshwater) the values are from Millero, 1983 (for K1, K2, 1135 | # and KW). The other aren't used (TB = TS = TF = TP = TSi = 0.), so 1136 | # including the factors won't matter. 1137 | #**************************************************************************** 1138 | # deltaVs are in cm3/mole 1139 | # Kappas are in cm3/mole/bar 1140 | #**************************************************************************** 1141 | 1142 | # CorrectK1K2KBForPressure: 1143 | deltaV = np.empty(ntps) 1144 | deltaV[:] = np.nan 1145 | Kappa = np.empty(ntps) 1146 | Kappa[:] = np.nan 1147 | lnK1fac = np.empty(ntps) 1148 | lnK1fac[:] = np.nan 1149 | lnK2fac = np.empty(ntps) 1150 | lnK2fac[:] = np.nan 1151 | lnKBfac = np.empty(ntps) 1152 | lnKBfac[:] = np.nan 1153 | 1154 | F = WhichKs == 8 1155 | if np.any(F): 1156 | #***PressureEffectsOnK1inFreshWater: 1157 | # This is from Millero, 1983. 1158 | deltaV[F] = -30.54 + 0.1849 * TempC[F] - 0.0023366 * TempC[F] ** 2 1159 | Kappa[F] = (-6.22 + 0.1368 * TempC[F] - 0.001233 * TempC[F] ** 2) / 1000 1160 | lnK1fac[F] = (-deltaV[F] + 0.5 * Kappa[F] * Pbar[F]) * Pbar[F] / RT[F] 1161 | #***PressureEffectsOnK2inFreshWater: 1162 | # This is from Millero, 1983. 1163 | deltaV[F] = -29.81 + 0.115 * TempC[F] - 0.001816 * TempC[F] ** 2 1164 | Kappa[F] = (-5.74 + 0.093 * TempC[F] - 0.001896 * TempC[F] ** 2) / 1000 1165 | lnK2fac[F] = (-deltaV[F] + 0.5 * Kappa[F] * Pbar[F]) * Pbar[F] / RT[F] 1166 | lnKBfac[F] = 0 # this doesn't matter since TB = 0 for this case 1167 | F = WhichKs == 6 or WhichKs == 7 1168 | if np.any(F): 1169 | # GEOSECS Pressure Effects On K1, K2, KB (on the NBS scale) 1170 | # Takahashi et al, GEOSECS Pacific Expedition v. 3, 1982 quotes 1171 | # Culberson and Pytkowicz, L and O 13:403-417, 1968: 1172 | # but the fits are the same as those in 1173 | # Edmond and Gieskes, GCA, 34:1261-1291, 1970 1174 | # who in turn quote Li, personal communication 1175 | lnK1fac[F] = (24.2 - 0.085 * TempC[F]) * Pbar[F] / RT[F] 1176 | lnK2fac[F] = (16.4 - 0.04 * TempC[F]) * Pbar[F] / RT[F] 1177 | # Takahashi et al had 26.4, but 16.4 is from Edmond and Gieskes 1178 | # and matches the GEOSECS results 1179 | lnKBfac[F] = (27.5 - 0.095 * TempC[F]) * Pbar[F] / RT[F] 1180 | F = WhichKs != 6 and WhichKs != 7 and WhichKs != 8 1181 | if np.any(F): 1182 | #***PressureEffectsOnK1: 1183 | # These are from Millero, 1995. 1184 | # They are the same as Millero, 1979 and Millero, 1992. 1185 | # They are from data of Culberson and Pytkowicz, 1968. 1186 | deltaV[F] = -25.5 + 0.1271 * TempC[F] 1187 | # 'deltaV = deltaV - .151 * (Sali - 34.8); % Millero, 1979 1188 | Kappa[F] = (-3.08 + 0.0877 * TempC[F]) / 1000 1189 | # 'Kappa = Kappa - .578 * (Sali - 34.8)/1000.; % Millero, 1979 1190 | lnK1fac[F] = (-deltaV[F] + 0.5 * Kappa[F] * Pbar[F]) * Pbar[F] / RT[F] 1191 | # The fits given in Millero, 1983 are somewhat different. 1192 | 1193 | #***PressureEffectsOnK2: 1194 | # These are from Millero, 1995. 1195 | # They are the same as Millero, 1979 and Millero, 1992. 1196 | # They are from data of Culberson and Pytkowicz, 1968. 1197 | deltaV[F] = -15.82 - 0.0219 * TempC[F] 1198 | # 'deltaV = deltaV + .321 * (Sali - 34.8); % Millero, 1979 1199 | Kappa[F] = (1.13 - 0.1475 * TempC[F]) / 1000 1200 | # 'Kappa = Kappa - .314 * (Sali - 34.8) / 1000: % Millero, 1979 1201 | lnK2fac[F] = (-deltaV[F] + 0.5 * Kappa[F] * Pbar[F]) * Pbar[F] / RT[F] 1202 | # The fit given in Millero, 1983 is different. 1203 | # Not by a lot for deltaV, but by much for Kappa. % 1204 | 1205 | #***PressureEffectsOnKB: 1206 | # This is from Millero, 1979. 1207 | # It is from data of Culberson and Pytkowicz, 1968. 1208 | deltaV[F] = -29.48 + 0.1622 * TempC[F] - 0.002608 * TempC[F] ** 2 1209 | # Millero, 1983 has: 1210 | # 'deltaV = -28.56 + .1211 * TempCi - .000321 * TempCi * TempCi 1211 | # Millero, 1992 has: 1212 | # 'deltaV = -29.48 + .1622 * TempCi + .295 * (Sali - 34.8) 1213 | # Millero, 1995 has: 1214 | # 'deltaV = -29.48 - .1622 * TempCi - .002608 * TempCi * TempCi 1215 | # 'deltaV = deltaV + .295 * (Sali - 34.8); % Millero, 1979 1216 | Kappa[F] = -2.84 / 1000 # Millero, 1979 1217 | # Millero, 1992 and Millero, 1995 also have this. 1218 | # 'Kappa = Kappa + .354 * (Sali - 34.8) / 1000: % Millero,1979 1219 | # Millero, 1983 has: 1220 | # 'Kappa = (-3 + .0427 * TempCi) / 1000 1221 | lnKBfac[F] = (-deltaV[F] + 0.5 * Kappa[F] * Pbar[F]) * Pbar[F] / RT[F] 1222 | 1223 | # CorrectKWForPressure: 1224 | lnKWfac = np.empty(ntps) 1225 | lnKWfac[:] = np.nan 1226 | F = WhichKs == 8 1227 | if np.any(F): 1228 | # PressureEffectsOnKWinFreshWater: 1229 | # This is from Millero, 1983. 1230 | deltaV[F] = -25.6 + 0.2324 * TempC[F] - 0.0036246 * TempC[F] ** 2 1231 | Kappa[F] = (-7.33 + 0.1368 * TempC[F] - 0.001233 * TempC[F] ** 2) / 1000 1232 | lnKWfac[F] = (-deltaV[F] + 0.5 * Kappa[F] * Pbar[F]) * Pbar[F] / RT[F] 1233 | # NOTE the temperature dependence of KappaK1 and KappaKW 1234 | # for fresh water in Millero, 1983 are the same. 1235 | F = WhichKs != 8 1236 | if np.any(F): 1237 | # GEOSECS doesn't include OH term, so this won't matter. 1238 | # Peng et al didn't include pressure, but here I assume that the KW correction 1239 | # is the same as for the other seawater cases. 1240 | # PressureEffectsOnKW: 1241 | # This is from Millero, 1983 and his programs CO2ROY(T).BAS. 1242 | deltaV[F] = -20.02 + 0.1119 * TempC[F] - 0.001409 * TempC[F] ** 2 1243 | # Millero, 1992 and Millero, 1995 have: 1244 | Kappa[F] = (-5.13 + 0.0794 * TempC[F]) / 1000 # Millero, 1983 1245 | # Millero, 1995 has this too, but Millero, 1992 is different. 1246 | lnKWfac[F] = (-deltaV[F] + 0.5 * Kappa[F] * Pbar[F]) * Pbar[F] / RT[F] 1247 | # Millero, 1979 does not list values for these. 1248 | 1249 | # 1250 | # PressureEffectsOnKF: 1251 | # This is from Millero, 1995, which is the same as Millero, 1983. 1252 | # It is assumed that KF is on the free pH scale. 1253 | deltaV = -9.78 - 0.009 * TempC - 0.000942 * TempC ** 2 1254 | Kappa = (-3.91 + 0.054 * TempC) / 1000 1255 | lnKFfac = (-deltaV + 0.5 * Kappa * Pbar) * Pbar / RT 1256 | # PressureEffectsOnKS: 1257 | # This is from Millero, 1995, which is the same as Millero, 1983. 1258 | # It is assumed that KS is on the free pH scale. 1259 | deltaV = -18.03 + 0.0466 * TempC + 0.000316 * TempC ** 2 1260 | Kappa = (-4.53 + 0.09 * TempC) / 1000 1261 | lnKSfac = (-deltaV + 0.5 * Kappa * Pbar) * Pbar / RT 1262 | 1263 | # CorrectKP1KP2KP3KSiForPressure: 1264 | # These corrections don't matter for the GEOSECS choice (WhichKs% = 6) and 1265 | # the freshwater choice (WhichKs% = 8). For the Peng choice I assume 1266 | # that they are the same as for the other choices (WhichKs% = 1 to 5). 1267 | # The corrections for KP1, KP2, and KP3 are from Millero, 1995, which are the 1268 | # same as Millero, 1983. 1269 | # PressureEffectsOnKP1: 1270 | deltaV = -14.51 + 0.1211 * TempC - 0.000321 * TempC ** 2 1271 | Kappa = (-2.67 + 0.0427 * TempC) / 1000 1272 | lnKP1fac = (-deltaV + 0.5 * Kappa * Pbar) * Pbar / RT 1273 | # PressureEffectsOnKP2: 1274 | deltaV = -23.12 + 0.1758 * TempC - 0.002647 * TempC ** 2 1275 | Kappa = (-5.15 + 0.09 * TempC) / 1000 1276 | lnKP2fac = (-deltaV + 0.5 * Kappa * Pbar) * Pbar / RT 1277 | # PressureEffectsOnKP3: 1278 | deltaV = -26.57 + 0.202 * TempC - 0.003042 * TempC ** 2 1279 | Kappa = (-4.08 + 0.0714 * TempC) / 1000 1280 | lnKP3fac = (-deltaV + 0.5 * Kappa * Pbar) * Pbar / RT 1281 | # PressureEffectsOnKSi: 1282 | # The only mention of this is Millero, 1995 where it is stated that the 1283 | # values have been estimated from the values of boric acid. HOWEVER, 1284 | # there is no listing of the values in the table. 1285 | # I used the values for boric acid from above. 1286 | deltaV = -29.48 + 0.1622 * TempC - 0.002608 * TempC ** 2 1287 | Kappa = -2.84 / 1000 1288 | lnKSifac = (-deltaV + 0.5 * Kappa * Pbar) * Pbar / RT 1289 | 1290 | # CorrectKsForPressureHere: 1291 | K1fac = np.exp(lnK1fac) 1292 | K1 = K1 * K1fac 1293 | K2fac = np.exp(lnK2fac) 1294 | K2 = K2 * K2fac 1295 | KWfac = np.exp(lnKWfac) 1296 | KW = KW * KWfac 1297 | KBfac = np.exp(lnKBfac) 1298 | KB = KB * KBfac 1299 | KFfac = np.exp(lnKFfac) 1300 | KF = KF * KFfac 1301 | KSfac = np.exp(lnKSfac) 1302 | KS = KS * KSfac 1303 | KP1fac = np.exp(lnKP1fac) 1304 | KP1 = KP1 * KP1fac 1305 | KP2fac = np.exp(lnKP2fac) 1306 | KP2 = KP2 * KP2fac 1307 | KP3fac = np.exp(lnKP3fac) 1308 | KP3 = KP3 * KP3fac 1309 | KSifac = np.exp(lnKSifac) 1310 | KSi = KSi * KSifac 1311 | 1312 | # CorrectpHScaleConversionsForPressure: 1313 | # fH has been assumed to be independent of pressure. 1314 | SWStoTOT = (1 + TS / KS) / (1 + TS / KS + TF / KF) 1315 | FREEtoTOT = 1 + TS / KS 1316 | 1317 | # The values KS and KF are already pressure-corrected, so the pH scale 1318 | # conversions are now valid at pressure. 1319 | 1320 | # FindpHScaleConversionFactor: 1321 | # this is the scale they will be put on 1322 | pHfactor = np.empty(ntps) 1323 | pHfactor[:] = np.nan 1324 | F = pHScale == 1 # Total 1325 | pHfactor[F] = SWStoTOT[F] 1326 | F = pHScale == 2 # SWS, they are all on this now 1327 | pHfactor[F] = 1 1328 | F = pHScale == 3 # pHfree 1329 | pHfactor[F] = SWStoTOT[F] / FREEtoTOT[F] 1330 | F = pHScale == 4 # pHNBS 1331 | pHfactor[F] = fH[F] 1332 | 1333 | # ConvertFromSWSpHScaleToChosenScale: 1334 | K1 = K1 * pHfactor 1335 | K2 = K2 * pHfactor 1336 | KW = KW * pHfactor 1337 | KB = KB * pHfactor 1338 | KP1 = KP1 * pHfactor 1339 | KP2 = KP2 * pHfactor 1340 | KP3 = KP3 * pHfactor 1341 | KSi = KSi * pHfactor 1342 | 1343 | # CalculateFugacityConstants: 1344 | # This assumes that the pressure is at one atmosphere, or close to it. 1345 | # Otherwise, the Pres term in the exponent affects the results. 1346 | # Weiss, R. F., Marine Chemistry 2:203-215, 1974. 1347 | # Delta and B in cm3/mol 1348 | FugFac = np.ones(ntps) 1349 | Delta = (57.7 - 0.118 * TempK) 1350 | b = -1636.75 + 12.0408 * TempK - 0.0327957 * TempK ** 2 + 3.16528 * 0.00001 * TempK ** 3 1351 | # For a mixture of CO2 and air at 1 atm (at low CO2 concentrations); 1352 | P1atm = 1.01325 # in bar 1353 | FugFac = np.exp((b + 2 * Delta) * P1atm / RT) 1354 | F = WhichKs==6 or WhichKs==7 # GEOSECS and Peng assume pCO2 = fCO2, or FugFac = 1 1355 | FugFac[F] = 1 1356 | # CalculateVPFac: 1357 | # Weiss, R. F., and Price, B. A., Nitrous oxide solubility in water and 1358 | # seawater, Marine Chemistry 8:347-359, 1980. 1359 | # They fit the data of Goff and Gratch (1946) with the vapor pressure 1360 | # lowering by sea salt as given by Robinson (1954). 1361 | # This fits the more complicated Goff and Gratch, and Robinson equations 1362 | # from 273 to 313 deg K and 0 to 40 Sali with a standard error 1363 | # of .015%, about 5 uatm over this range. 1364 | # This may be on IPTS-29 since they didn't mention the temperature scale, 1365 | # and the data of Goff and Gratch came before IPTS-48. 1366 | # The references are: 1367 | # Goff, J. A. and Gratch, S., Low pressure properties of water from -160 deg 1368 | # to 212 deg F, Transactions of the American Society of Heating and 1369 | # Ventilating Engineers 52:95-122, 1946. 1370 | # Robinson, Journal of the Marine Biological Association of the U. K. 1371 | # 33:449-455, 1954. 1372 | # This is eq. 10 on p. 350. 1373 | # This is in atmospheres. 1374 | VPWP = np.exp(24.4543 - 67.4509 * (100 / TempK) - 4.8489 * np.log(TempK / 100)) 1375 | VPCorrWP = np.exp(-0.000544 * Sal) 1376 | VPSWWP = VPWP * VPCorrWP 1377 | VPFac = 1 - VPSWWP # this assumes 1 atmosphere 1378 | 1379 | 1380 | def CalculatepHfCO2fromTATC(TAx, TCx): 1381 | # Outputs pH fCO2, in that order 1382 | # SUB FindpHfCO2fromTATC, version 01.02, 10-10-97, written by Ernie Lewis. 1383 | # Inputs: pHScale%, WhichKs%, WhoseKSO4%, TA, TC, Sal, K(), T(), TempC, Pdbar 1384 | # Outputs: pH, fCO2 1385 | # This calculates pH and fCO2 from TA and TC at output conditions. 1386 | global FugFac, F 1387 | pHx = CalculatepHfromTATC(TAx, TCx) # pH is returned on the scale requested in "pHscale" (see 'constants'...) 1388 | fCO2x = CalculatefCO2fromTCpH(TCx, pHx) 1389 | return pHx, fCO2x 1390 | 1391 | 1392 | def CalculatepHfromTATC(TAx, TCx): 1393 | #Outputs pH 1394 | # SUB CalculatepHfromTATC, version 04.01, 10-13-96, written by Ernie Lewis. 1395 | # Inputs: TA, TC, K(), T() 1396 | # Output: pH 1397 | # This calculates pH from TA and TC using K1 and K2 by Newton's method. 1398 | # It tries to solve for the pH at which Residual = 0. 1399 | # The starting guess is pH = 8. 1400 | # Though it is coded for H on the total pH scale, for the pH values occuring 1401 | # in seawater (pH > 6) it will be equally valid on any pH scale (H terms 1402 | # negligible) as long as the K Constants are on that scale. 1403 | # 1404 | # Made this to accept vectors. It will continue iterating until all 1405 | # values in the vector are "abs(deltapH) < pHTol". SVH2007 1406 | 1407 | global pHScale, WhichKs, WhoseKSO4, sqrSal, Pbar, RT 1408 | global K0, fH, FugFac, VPFac, ntps, TempK, logTempK 1409 | global K1, K2, KW, KB, KF, KS, KP1, KP2, KP3, KSi 1410 | global TB, TF, TS, TP, TSi, F 1411 | 1412 | K1F = K1[F] 1413 | K2F = K2[F] 1414 | KWF = KW[F] 1415 | KP1F = KP1[F] 1416 | KP2F = KP2[F] 1417 | KP3F = KP3[F] 1418 | TPF = TP[F] 1419 | TSiF = TSi[F] 1420 | KSiF = KSi[F] 1421 | TBF = TB[F] 1422 | KBF = KB[F] 1423 | TSF = TS[F] 1424 | KSF = KS[F] 1425 | TFF = TF[F] 1426 | KFF = KF[F] 1427 | 1428 | vl = np.sum(F) # VectorLength 1429 | pHGuess = 8 # First guess 1430 | pHTol = 0.0001 # Tolerance for iterations end 1431 | ln10 = np.log(10) 1432 | pHx = np.empty(vl) 1433 | pHx[:] = pHGuess # Creates a vector holding the first guess for all samples 1434 | deltapH = pHTol + 1 1435 | 1436 | while np.any(np.abs(deltapH) > pHTol): 1437 | H = 10 ** -pHx 1438 | Denom = H * H + K1F * H + K1F * K2F 1439 | CAlk = TCx * K1F * (H + 2 * K2F) / Denom 1440 | BAlk = TBF * KBF / (KBF + H) 1441 | OH = KWF / H 1442 | PhosTop = KP1F * KP2F * H + 2 * KP1F * KP2F * KP3F - H * H * H 1443 | PhosBot = H * H * H + KP1F * H * H + KP1F * KP2F * H + KP1F * KP2F * KP3F 1444 | PAlk = TPF * PhosTop / PhosBot 1445 | SiAlk = TSiF * KSiF / (KSiF + H) 1446 | FREEtoTOT = (1 + TSF / KSF) # pH scale conversion factor 1447 | Hfree = H / FREEtoTOT # for H on the total scale 1448 | HSO4 = TSF / (1 + KSF / Hfree) # Since KS is on free scale 1449 | HF = TFF / (1 + KFF / Hfree) # Since KF is on free scale 1450 | Residual = TAx - CAlk - BAlk - OH - PAlk - SiAlk + Hfree + HSO4 + HF 1451 | # find Slope dTA/dpH 1452 | # (This is not exact but keeps all important terms) 1453 | Slope = ln10 * (TCx * K1F * H * (H * H + K1F * K2F + 4 * H * K2F) / Denom / Denom + BAlk * H / (KBF + H) + OH + H) 1454 | deltapH = Residual / Slope # This is Newton's method 1455 | 1456 | # to keep the jump from being too big 1457 | while np.any(np.abs(deltapH) > 1): 1458 | FF = np.abs(deltapH) > 1 1459 | deltapH[FF] = deltapH[FF] / 2 1460 | 1461 | pHx = pHx + deltapH # Is on the same scale as K1 and K2 were calculated... 1462 | 1463 | return pHx 1464 | 1465 | 1466 | def CalculatefCO2fromTCpH(TCx, pHx): 1467 | # ' SUB CalculatefCO2fromTCpH, version 02.02, 12-13-96, written by Ernie Lewis. 1468 | # ' Inputs: TC, pH, K0, K1, K2 1469 | # ' Output: fCO2 1470 | # ' This calculates fCO2 from TC and pH, using K0, K1, and K2. 1471 | global K0, K1, K2, F 1472 | 1473 | pHx = np.array(pHx) 1474 | H = 10 ** -pHx 1475 | fCO2x = TCx * H * H / (H * H + K1[F] * H + K1[F] * K2[F]) / K0[F] 1476 | 1477 | return fCO2x 1478 | 1479 | 1480 | def CalculateTCfromTApH(TAx, pHx): 1481 | # ' SUB CalculateTCfromTApH, version 02.03, 10-10-97, written by Ernie Lewis. 1482 | # ' Inputs: TA, pH, K(), T() 1483 | # ' Output: TC 1484 | # ' This calculates TC from TA and pH. 1485 | # ' Though it is coded for H on the total pH scale, for the pH values occuring 1486 | # ' in seawater (pH > 6) it will be equally valid on any pH scale (H terms 1487 | # ' negligible) as long as the K Constants are on that scale. 1488 | global K0, K1, K2, KW, KB, KF, KS, KP1, KP2, KP3, KSi 1489 | global TB, TF, TS, TP, TSi, F 1490 | 1491 | K1F = K1[F] 1492 | K2F = K2[F] 1493 | KWF = KW[F] 1494 | KP1F = KP1[F] 1495 | KP2F = KP2[F] 1496 | KP3F = KP3[F] 1497 | TPF = TP[F] 1498 | TSiF = TSi[F] 1499 | KSiF = KSi[F] 1500 | TBF = TB[F] 1501 | KBF = KB[F] 1502 | TSF = TS[F] 1503 | KSF = KS[F] 1504 | TFF = TF[F] 1505 | KFF = KF[F] 1506 | 1507 | H = 10 ** -pHx 1508 | BAlk = TBF * KBF / (KBF + H) 1509 | OH = KWF / H 1510 | PhosTop = KP1F * KP2F * H + 2 * KP1F * KP2F * KP3F - H * H * H 1511 | PhosBot = H * H * H + KP1F * H * H + KP1F * KP2F * H + KP1F * KP2F * KP3F 1512 | PAlk = TPF * PhosTop / PhosBot 1513 | SiAlk = TSiF * KSiF / (KSiF + H) 1514 | FREEtoTOT = (1 + TSF / KSF) # pH scale conversion factor 1515 | Hfree = H / FREEtoTOT # for H on total scale 1516 | HSO4 = TSF / (1 + KSF / Hfree) # Since KS is on free scale 1517 | HF = TFF / (1 + KFF / Hfree) # Since KF is on free scale 1518 | CAlk = TAx - BAlk - OH - PAlk - SiAlk + Hfree + HSO4 + HF 1519 | TCxtemp = CAlk * (H * H + K1F * H + K1F * K2F) / (K1F * (H + 2 * K2F)) 1520 | 1521 | return TCxtemp 1522 | 1523 | 1524 | def CalculatepHfromTAfCO2(TAi, fCO2i): 1525 | # ' SUB CalculatepHfromTAfCO2, version 04.01, 10-13-97, written by Ernie Lewis. 1526 | # ' Inputs: TA, fCO2, K0, K(), T() 1527 | # ' Output: pH 1528 | # ' This calculates pH from TA and fCO2 using K1 and K2 by Newton's method. 1529 | # ' It tries to solve for the pH at which Residual = 0. 1530 | # ' The starting guess is pH = 8. 1531 | # ' Though it is coded for H on the total pH scale, for the pH values occuring 1532 | # ' in seawater (pH > 6) it will be equally valid on any pH scale (H terms 1533 | # ' negligible) as long as the K Constants are on that scale. 1534 | global K0, K1, K2, KW, KB, KF, KS, KP1, KP2, KP3, KSi 1535 | global TB, TF, TS, TP, TSi, F 1536 | 1537 | K0F = K0[F] 1538 | K1F = K1[F] 1539 | K2F = K2[F] 1540 | KWF = KW[F] 1541 | KP1F = KP1[F] 1542 | KP2F = KP2[F] 1543 | KP3F = KP3[F] 1544 | TPF = TP[F] 1545 | TSiF = TSi[F] 1546 | KSiF = KSi[F] 1547 | TBF = TB[F] 1548 | KBF = KB[F] 1549 | TSF = TS[F] 1550 | KSF = KS[F] 1551 | TFF = TF[F] 1552 | KFF = KF[F] 1553 | 1554 | vl = np.sum(F) # Vectorlength 1555 | pHGuess = 8 # First guess 1556 | pHTol = 0.0001 # Tolerance 1557 | ln10 = np.log(10) 1558 | pH = pHGuess 1559 | deltapH = pHTol + pH 1560 | 1561 | while np.any(np.abs(deltapH) > pHTol): 1562 | H = 10 ** -pH 1563 | HCO3 = K0F * K1F * fCO2i / H 1564 | CO3 = K0F * K1F * K2F * fCO2i / (H * H) 1565 | CAlk = HCO3 + 2 * CO3 1566 | BAlk = TBF * KBF / (KBF + H) 1567 | OH = KWF / H 1568 | PhosTop = KP1F * KP2F * H + 2 * KP1F * KP2F * KP3F - H * H * H 1569 | PhosBot = H * H * H + KP1F * H * H + KP1F * KP2F * H + KP1F * KP2F * KP3F 1570 | PAlk = TPF * PhosTop / PhosBot 1571 | SiAlk = TSiF * KSiF / PhosBot 1572 | FREEtoTOT = (1 + TSF / KSF) # pH scale conversion factor 1573 | Hfree = H / FREEtoTOT # for H on the total scale 1574 | HSO4 = TSF / (1 + KSF / Hfree) # since KS is on free scale 1575 | HF = TFF / (1 + KFF / Hfree) # since KF is on the free scale 1576 | Residual = TAi - CAlk - BAlk - OH - PAlk - SiAlk + Hfree + HSO4 + HF 1577 | # find Slope dTA/dpH 1578 | # (This is not exact, but keeps all important terms) 1579 | Slope = ln10 * (HCO3 + 4 * CO3 + BAlk * H / (KBF + H) + OH + H) 1580 | deltapH = Residual / Slope # This is Newton's method 1581 | 1582 | # To keep the jump from being too big: 1583 | while np.any(np.abs(deltapH) > 1): 1584 | FF = np.abs(deltapH) > 1 1585 | deltapH[FF] = deltapH[FF] / 2 1586 | 1587 | pH = pH + deltapH 1588 | 1589 | return pH 1590 | 1591 | 1592 | def CalculateTAfromTCpH(TCi, pHi): 1593 | # ' SUB CalculateTAfromTCpH, version 02.02, 10-10-97, written by Ernie Lewis. 1594 | # ' Inputs: TC, pH, K(), T() 1595 | # ' Output: TA 1596 | # ' This calculates TA from TC and pH. 1597 | # ' Though it is coded for H on the total pH scale, for the pH values occuring 1598 | # ' in seawater (pH > 6) it will be equally valid on any pH scale (H terms 1599 | # ' negligible) as long as the K Constants are on that scale. 1600 | global K0, K1, K2, KW, KB, KF, KS, KP1, KP2, KP3, KSi 1601 | global TB, TF, TS, TP, TSi, F 1602 | 1603 | K1F = K1[F] 1604 | K2F = K2[F] 1605 | KWF = KW[F] 1606 | KP1F = KP1[F] 1607 | KP2F = KP2[F] 1608 | KP3F = KP3[F] 1609 | TPF = TP[F] 1610 | TSiF = TSi[F] 1611 | KSiF = KSi[F] 1612 | TBF = TB[F] 1613 | KBF = KB[F] 1614 | TSF = TS[F] 1615 | KSF = KS[F] 1616 | TFF = TF[F] 1617 | KFF = KF[F] 1618 | 1619 | H = 10 ** -pHi 1620 | CAlk = TCi * K1F * (H + 2 * K2F) / (H * H + K1F * H + K1F * K2F) 1621 | BAlk = TBF * KBF / (KBF + H) 1622 | OH = KWF / H 1623 | PhosTop = KP1F * KP2F * H + 2 * KP1F * KP2F * KP3F - H * H * H 1624 | PhosBot = H * H * H + KP1F * H * H + KP1F * KP2F * H + KP1F * KP2F * KP3F 1625 | PAlk = TPF * PhosTop / PhosBot 1626 | SiAlk = TSiF * KSiF / (KSiF + H) 1627 | FREEtoTOT = 1 + (TSF / KSF) # pH scale conversion factor 1628 | Hfree = H / FREEtoTOT # for H on total scale 1629 | HSO4 = TSF / (1 + KSF / Hfree) # since KS is on free sclae 1630 | HF = TFF / (1 + KFF / Hfree) # since KF is on the free scale 1631 | TActemp = CAlk + BAlk + OH + PAlk + SiAlk - Hfree - HSO4 - HF 1632 | 1633 | return TActemp 1634 | 1635 | 1636 | def CalculatepHfromTCfCO2(TCi, fCO2i): 1637 | # ' SUB CalculatepHfromTCfCO2, version 02.02, 11-12-96, written by Ernie Lewis. 1638 | # ' Inputs: TC, fCO2, K0, K1, K2 1639 | # ' Output: pH 1640 | # ' This calculates pH from TC and fCO2 using K0, K1, and K2 by solving the 1641 | # ' quadratic in H: fCO2.*K0 = TC.*H.*H / (K1.*H + H.*H + K1.*K2). 1642 | # ' if there is not a real root, then pH is returned as missingn. 1643 | global K0, K1, K2, F 1644 | 1645 | RR = K0[F] * fCO2i / TCi 1646 | Discr = (K1[F] * RR) * (K1[F] * RR) + 4 * (1 - RR) * (K1[F] * K2[F] * RR) 1647 | H = 0.5 * (K1[F] * RR + np.sqrt(Discr)) / (1 - RR) 1648 | pHctemp = np.log(H) / np.log(0.1) 1649 | 1650 | return pHctemp 1651 | 1652 | 1653 | def CalculateTCfrompHfCO2(pHi, fCO2i): 1654 | # ' SUB CalculateTCfrompHfCO2, version 01.02, 12-13-96, written by Ernie Lewis. 1655 | # ' Inputs: pH, fCO2, K0, K1, K2 1656 | # ' Output: TC 1657 | # ' This calculates TC from pH and fCO2, using K0, K1, and K2. 1658 | global K0, K1, K2, F 1659 | 1660 | H = 10 ** -pHi 1661 | TCctemp = K0[F] * fCO2i * (H * H + K1[F] * H + K1[F] * K2[F]) / (H * H) 1662 | 1663 | return TCctemp 1664 | 1665 | 1666 | def RevelleFactor(TAi, TCi): 1667 | # ' SUB RevelleFactor, version 01.03, 01-07-97, written by Ernie Lewis. 1668 | # ' Inputs: WhichKs%, TA, TC, K0, K(), T() 1669 | # ' Outputs: Revelle 1670 | # ' This calculates the Revelle factor (dfCO2/dTC)|TA/(fCO2/TC). 1671 | # ' It only makes sense to talk about it at pTot = 1 atm, but it is computed 1672 | # ' here at the given K(), which may be at pressure <> 1 atm. Care must 1673 | # ' thus be used to see if there is any validity to the number computed. 1674 | TC0 = TCi 1675 | dTC = 0.000001 # 1 umol/kg-SW 1676 | 1677 | # Find fCO2 at TA, TC + dTC 1678 | TCi = TC0 + dTC 1679 | pHc = CalculatepHfromTATC(TAi, TCi) 1680 | fCO2c = CalculatefCO2fromTCpH(TCi, pHc) 1681 | fCO2plus = fCO2c 1682 | 1683 | # Find fCO2 at TA, TC - dTC 1684 | TCi = TC0 - dTC 1685 | pHc = CalculatepHfromTATC(TAi, TCi) 1686 | fCO2c = CalculatefCO2fromTCpH(TCi, pHc) 1687 | fCO2minus = fCO2c 1688 | 1689 | # CalculateRevelleFactor: 1690 | Revelle = (fCO2plus - fCO2minus) / dTC / ((fCO2plus + fCO2minus) / TCi) 1691 | 1692 | return Revelle 1693 | 1694 | 1695 | def CalculateAlkParts(pHx, TCx): 1696 | # ' SUB CalculateAlkParts, version 01.03, 10-10-97, written by Ernie Lewis. 1697 | # ' Inputs: pH, TC, K(), T() 1698 | # ' Outputs: HCO3, CO3, BAlk, OH, PAlk, SiAlk, Hfree, HSO4, HF 1699 | # ' This calculates the various contributions to the alkalinity. 1700 | # ' Though it is coded for H on the total pH scale, for the pH values occuring 1701 | # ' in seawater (pH > 6) it will be equally valid on any pH scale (H terms 1702 | # ' negligible) as long as the K Constants are on that scale. 1703 | global K0, K1, K2, KW, KB, KF, KS, KP1, KP2, KP3, KSi 1704 | global TB, TF, TS, TP, TSi, F 1705 | 1706 | H = 10 ** -pHx 1707 | HCO3 = TCx * K1 * H / (K1 * H + H * H + K1 * K2) 1708 | CO3 = TCx * K1 * K2 / (K1 * H + H * H + K1 * K2) 1709 | BAlk = TB * KB / (KB + H) 1710 | OH = KW / H 1711 | PhosTop = KP1 * KP2 * H + 2 * KP1 * KP2 * KP3 - H * H * H 1712 | PhosBot = H * H * H + KP1 * H * H + KP1 * KP2 * H + KP1 * KP2 * KP3 1713 | PAlk = TP * PhosTop / PhosBot 1714 | SiAlk = TSi * KSi / (KSi + H) 1715 | FREEtoTOT = 1 + TS / KS # pH scale conversion factor 1716 | Hfree = H / FREEtoTOT # for H on total scale 1717 | HSO4 = TS / (1 + KS / Hfree) # since KS is on free scale 1718 | HF = TF / (1 + KF / Hfree) # since KF is on free scale 1719 | 1720 | return HCO3, CO3, BAlk, OH, PAlk, SiAlk, Hfree, HSO4, HF 1721 | 1722 | 1723 | def CaSolubility(Sal, TempC, Pdbar, TC, pH): 1724 | # '*********************************************************************** 1725 | # ' SUB CaSolubility, version 01.05, 05-23-97, written by Ernie Lewis. 1726 | # ' Inputs: WhichKs%, Sal, TempCi, Pdbari, TCi, pHi, K1, K2 1727 | # ' Outputs: OmegaCa, OmegaAr 1728 | # ' This calculates omega, the solubility ratio, for calcite and aragonite. 1729 | # ' This is defined by: Omega = [CO3--]*[Ca++]./Ksp, 1730 | # ' where Ksp is the solubility product (either KCa or KAr). 1731 | # '*********************************************************************** 1732 | # ' These are from: 1733 | # ' Mucci, Alphonso, The solubility of calcite and aragonite in seawater 1734 | # ' at various salinities, temperatures, and one atmosphere total 1735 | # ' pressure, American Journal of Science 283:781-799, 1983. 1736 | # ' Ingle, S. E., Solubility of calcite in the ocean, 1737 | # ' Marine Chemistry 3:301-319, 1975, 1738 | # ' Millero, Frank, The thermodynamics of the carbonate system in seawater, 1739 | # ' Geochemica et Cosmochemica Acta 43:1651-1661, 1979. 1740 | # ' Ingle et al, The solubility of calcite in seawater at atmospheric pressure 1741 | # ' and 35%o salinity, Marine Chemistry 1:295-307, 1973. 1742 | # ' Berner, R. A., The solubility of calcite and aragonite in seawater in 1743 | # ' atmospheric pressure and 34.5%o salinity, American Journal of 1744 | # ' Science 276:713-730, 1976. 1745 | # ' Takahashi et al, in GEOSECS Pacific Expedition, v. 3, 1982. 1746 | # ' Culberson, C. H. and Pytkowicz, R. M., Effect of pressure on carbonic acid, 1747 | # ' boric acid, and the pHi of seawater, Limnology and Oceanography 1748 | # ' 13:403-417, 1968. 1749 | # '*********************************************************************** 1750 | 1751 | # Note that dic(aka TC) needs to be in mol/kg, *not* micromol/kg. 1752 | # MATLAB has K1 K2 TempK logTempK sqrSal Pbar RT WhichKs ntps as global. 1753 | global K1, K2, TempK, logTempK, sqrSal, Pbar, RT, WhichKs, ntps 1754 | 1755 | Ca = np.empty(ntps) 1756 | Ca[:] = np.nan 1757 | 1758 | Ar = np.empty(ntps) 1759 | Ar[:] = np.nan 1760 | 1761 | KCa = np.empty(ntps) 1762 | KCa[:] = np.nan 1763 | 1764 | KAr = np.empty(ntps) 1765 | KAr[:] = np.nan 1766 | 1767 | F = (WhichKs != 6 and WhichKs != 7) 1768 | 1769 | if np.any(F): 1770 | # (below here, F isn't used, since almost always all rows match the above criterium, 1771 | # in all other cases the rows will be overwritten later on). 1772 | # CalculateCa: 1773 | # ' Riley, J. P. and Tongudai, M., Chemical Geology 2:263-269, 1967: 1774 | # ' this is .010285.*Sali./35 1775 | Ca = 0.02128 / 40.087 * (Sal / 1.80655) # in mol/kg-SW 1776 | 1777 | # CalciteSolubility: 1778 | # ' Mucci, Alphonso, Amer. J. of Science 283:781-799, 1983. 1779 | logKCa = -171.9065 - 0.077993 * TempK + 2839.319 / TempK 1780 | logKCa = logKCa + 71.595 * logTempK / np.log(10) 1781 | logKCa = logKCa + (-0.77712 + 0.0028426 * TempK + 178.34 / TempK) * sqrSal 1782 | logKCa = logKCa - 0.07711 * Sal + 0.0041249 * sqrSal * Sal 1783 | # ' sd fit = .01 (for Sal part, not part independent of Sal) 1784 | KCa = 10 ** logKCa # this is in (mol/kg-SW)^2 1785 | 1786 | # AragoniteSolubility: 1787 | # ' Mucci, Alphonso, Amer. J. of Science 283:781-799, 1983. 1788 | logKAr = -171.945 - 0.077993 * TempK + 2903.293 / TempK 1789 | logKAr = logKAr + 71.595 * logTempK / np.log(10) 1790 | logKAr = logKAr + (-0.068393 + 0.0017276 * TempK + 88.135 / TempK) * sqrSal 1791 | logKAr = logKAr - 0.10018 * Sal + 0.0059415 * sqrSal * Sal 1792 | # ' sd fit = .009 (for Sal part, not part independent of Sal) 1793 | KAr = 10 ** logKAr # this is in (mol/kg-SW)^2 1794 | 1795 | # PressureCorrectionForCalcite: 1796 | # ' Ingle, Marine Chemistry 3:301-319, 1975 1797 | # ' same as in Millero, GCA 43:1651-1661, 1979, but Millero, GCA 1995 1798 | # ' has typos (-.5304, -.3692, and 10^3 for Kappa factor) 1799 | deltaVKCa = -48.76 + 0.5304 * TempC 1800 | KappaKCa = (-11.76 + 0.3692 * TempC) / 1000 1801 | lnKCafac = (-deltaVKCa + 0.5 * KappaKCa * Pbar) * Pbar / RT 1802 | KCa = KCa * np.exp(lnKCafac) 1803 | 1804 | # PressureCorrectionForAragonite: 1805 | # ' Millero, Geochemica et Cosmochemica Acta 43:1651-1661, 1979, 1806 | # ' same as Millero, GCA 1995 except for typos (-.5304, -.3692, 1807 | # ' and 10^3 for Kappa factor) 1808 | deltaVKAr = deltaVKCa + 2.8 1809 | KappaKAr = KappaKCa 1810 | lnKArfac = (-deltaVKAr + 0.5 * KappaKAr * Pbar) * Pbar / RT 1811 | KAr = KAr * np.exp(lnKArfac) 1812 | 1813 | F = WhichKs == 6 or WhichKs == 7 1814 | 1815 | if np.any(F): 1816 | # *** CalculateCaforGEOSECS: 1817 | # Culkin, F, in Chemical Oceanography, ed. Riley and Skirrow, 1965: 1818 | # (quoted in Takahashi et al, GEOSECS Pacific Expedition v. 3, 1982) 1819 | Ca[F] = 0.01026 * Sal[F] / 35 1820 | # Culkin gives Ca = (.0213./40.078).*(Sal./1.80655) in mol/kg-SW 1821 | # which corresponds to Ca = .01030.*Sal./35. 1822 | 1823 | # *** CalculateKCaforGEOSECS: 1824 | # Ingle et al, Marine Chemistry 1:295-307, 1973 is referenced in 1825 | # (quoted in Takahashi et al, GEOSECS Pacific Expedition v. 3, 1982 1826 | # but the fit is actually from Ingle, Marine Chemistry 3:301-319, 1975) 1827 | KCa[F] = 0.0000001 * (-34.452 - 39.866 * Sal[F] ** (1 / 3) + 110.21 * np.log(Sal[F]) / np.log(10) - 0.0000075752 * TempK[F] ** 2) 1828 | # this is in (mol/kg-SW)^2 1829 | 1830 | # *** CalculateKArforGEOSECS: 1831 | # Berner, R. A., American Journal of Science 276:713-730, 1976: 1832 | # (quoted in Takahashi et al, GEOSECS Pacific Expedition v. 3, 1982) 1833 | KAr[F] = 1.45 * KCa[F] # This is in (mol/kg-SW)^2 1834 | # Berner (p. 722) states that he uses 1.48. 1835 | # It appears that 1.45 was used in the GEOSECS calculations 1836 | 1837 | # *** CalculatePressureEffectsOnKCaKArGEOSECS: 1838 | # Culberson and Pytkowicz, Limnology and Oceanography 13:403-417, 1968 1839 | # (quoted in Takahashi et al, GEOSECS Pacific Expedition v. 3, 1982 1840 | # but their paper is not even on this topic). 1841 | # The fits appears to be new in the GEOSECS report. 1842 | # I can't find them anywhere else. 1843 | KCa[F] = KCa[F] * np.exp((36 - 0.2 * TempC[F]) * Pbar[F] / RT[F]) 1844 | KAr[F] = KAr[F] * np.exp((33.3 - 0.22 * TempC[F]) * Pbar[F] / RT[F]) 1845 | 1846 | #CalculateOmegasHere: 1847 | H = 10 ** -pH 1848 | CO3 = TC * K1 * K2 / (K1 * H + H * H + K1 * K2) # I think we have a float-precision problem here in comparison with MATLAB. 1849 | 1850 | omega_ca = CO3 * Ca / KCa # OmegaCa, dimensionless 1851 | omega_ar = CO3 * Ca / KAr # OmegaAr, dimensionless 1852 | 1853 | return omega_ca, omega_ar 1854 | 1855 | 1856 | def FindpHOnAllScales(pH): 1857 | # ' SUB FindpHOnAllScales, version 01.02, 01-08-97, written by Ernie Lewis. 1858 | # ' Inputs: pHScale%, pH, K(), T(), fH 1859 | # ' Outputs: pHNBS, pHfree, pHTot, pHSWS 1860 | # ' This takes the pH on the given scale and finds the pH on all scales. 1861 | # TS = T(3); TF = T(2); 1862 | # KS = K(6); KF = K(5);% 'these are at the given T, S, P 1863 | global pHScale, K, T, TS, KS, TF, KF, fH, ntps 1864 | 1865 | FREEtoTOT = (1 + TS / KS) # pH scale conversion factor 1866 | SWStoTOT = (1 + TS / KS) / (1 + TS / KS + TF / KF) # pH scale conversion factor 1867 | 1868 | factor = np.empty(ntps) 1869 | factor[:] = np.nan 1870 | 1871 | # pHtot 1872 | F = pHScale == 1 1873 | factor[F] = 0 1874 | 1875 | # pHsws 1876 | F = pHScale == 2 1877 | factor[F] = -np.log(SWStoTOT[F]) / np.log(0.1) 1878 | 1879 | # pHfree 1880 | F = pHScale == 3 1881 | factor[F] = -np.log(FREEtoTOT[F]) / np.log(0.1) 1882 | 1883 | # pHNBS 1884 | F = pHScale == 4 1885 | factor[F] = -np.log(SWStoTOT[F]) / np.log(0.1) + np.log(fH[F]) / np.log(0.1) 1886 | 1887 | pHtot = pH - factor # pH comes into this sub on the given scale 1888 | pHNBS = pHtot - np.log(SWStoTOT) / np.log(0.1) + np.log(fH) / np.log(0.1) 1889 | pHfree = pHtot - np.log(FREEtoTOT) / np.log(0.1) 1890 | pHsws = pHtot - np.log(SWStoTOT) / np.log(0.1) 1891 | 1892 | return pHtot, pHsws, pHfree, pHNBS 1893 | --------------------------------------------------------------------------------