{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sensitivity of DIC to pH and *p*CO2\n",
"\n",
"*Based on **CO2SYSExample1.m** for MATLAB by Steven van Heuven.*\n",
"\n",
"## Introduction\n",
"\n",
"This is an example of the use of PyCO2SYS that uses its ability to process arrays of data.\n",
"\n",
"We will generate a figure that shows the sensitivity of pH and *p*CO2 to changes in DIC, while keeping everything else constant.\n",
"\n",
"You can find further information about this way of using PyCO2SYS [in its documentation](https://pyco2sys.readthedocs.io/en/latest/co2sys_nd/).\n",
"\n",
"## Define input conditions\n",
"\n",
"The first step is to define the input conditions that we want to use with PyCO2SYS. In this case, every input has a single constant value except for DIC (`par2`), which is a NumPy array of values increasing from 2100 to 2300 μmol·kg-1 in increments of 5 μmol·kg-1:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import NumPy to make the DIC array\n",
"import numpy as np\n",
"\n",
"# Define input conditions\n",
"kwargs = dict(\n",
" par1 = 2400, # Value of the first parameter\n",
" par2 = np.arange(2100, 2305, 5), # Value of the second parameter, which is a long vector of different DIC's!\n",
" par1_type = 1, # The first parameter supplied is of type \"1\", which is \"alkalinity\"\n",
" par2_type = 2, # The second parameter supplied is of type \"2\", which is \"DIC\"\n",
" salinity = 35, # Salinity of the sample\n",
" temperature = 10, # Temperature at input conditions\n",
" total_silicate = 50, # Concentration of silicate in the sample (in umol/kg)\n",
" total_phosphate = 2, # Concentration of phosphate in the sample (in umol/kg)\n",
" opt_k_carbonic = 4, # Choice of H2CO3 and HCO3- dissociation constants K1 and K2 (\"4\" means \"Mehrbach refit\")\n",
" opt_k_bisulfate = 1, # Choice of HSO4- dissociation constants KSO4 (\"1\" means \"Dickson\")\n",
")\n",
"print(\"Input conditions have been set!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run PyCO2SYS\n",
"\n",
"Once we have defined the input conditions above, solving the marine carbonate system is as simple as importing and running the `pyco2.sys` function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import PyCO2SYS\n",
"import PyCO2SYS as pyco2\n",
"\n",
"# Run CO2SYS!\n",
"results = pyco2.sys(**kwargs)\n",
"print('PyCO2SYS ran successfully!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualise the results\n",
"\n",
"Finally, we can easily visualise the results using a plotting package such as Matplotlib:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import plotting package\n",
"from matplotlib import pyplot as plt\n",
"%matplotlib notebook\n",
"\n",
"# Prepare an empty figure\n",
"fig, ax = plt.subplots(2, 1, figsize=(6, 7))\n",
"\n",
"# The calculated pCO2's are in the field 'pCO2' of the results CO2dict\n",
"# Show these in the first subplot\n",
"ax[0].plot('par2', 'pCO2', data=results, c='r', marker='o')\n",
"ax[0].set_xlabel(\"DIC [umol/kg]\")\n",
"ax[0].set_ylabel(\"pCO2 [uatm]\")\n",
"\n",
"# The calculated pH's are in the field 'pH' of the results CO2dict\n",
"# Show these in the second subplot\n",
"ax[1].plot('par2', 'pH', data=results, c='r', marker='o')\n",
"ax[1].set_xlabel(\"DIC [umol/kg]\")\n",
"ax[1].set_ylabel(\"pH\");"
]
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}