{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9ff8d709-11fb-4337-bd2b-8d987f963c20",
   "metadata": {},
   "source": [
    "# Oxygen unit conversions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "87f327f8-71ea-4620-a146-0d553ead7031",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyrotoolbox.parsers import parse\n",
    "from pyrotoolbox.oxygen import *"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2659001a-93ac-46a2-8495-770893363570",
   "metadata": {},
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dfdc3b88-564f-4027-b332-26609b0275ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "df, m = parse('datademo4/A_Firesting Pro (4 Channels)_(A Ch.1)_Oxygen.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1adea1eb-b424-419c-9bc9-94a4b3912ccc",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.iloc[80:].copy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "549f1a0a-45ff-4579-8bf7-aee283a80bbf",
   "metadata": {},
   "source": [
    "## convert input unit (%O2) to hPa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8ba290d5-9f1d-4826-ba37-e85f1d44375a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['oxygen_hPa'] = i_only_think_in_hpa(df, m)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f86433e-b5e9-42cd-a7b5-571847160d48",
   "metadata": {},
   "source": [
    "### alternative:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9c7a5854-94ef-4aee-ba90-f731cae41946",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['oxygen_hPa_2'] = convert_to_hPa(df['oxygen_%O2'], unit='oxygen_%O2', \n",
    "                                   temperature=m['settings']['temperature'], \n",
    "                                   pressure=df['pressure'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbb12ea4-6af5-4cb6-987f-2f3657283ebf",
   "metadata": {},
   "source": [
    "## convert hPa to all other oxygen units"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "452cf887-0c7b-4f3c-b9a3-d1a7f9dd94ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['oxygen_torr'] = hPa_to_torr(df['oxygen_hPa'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "840ea1c7-6515-4985-b9e0-491f39af2456",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['oxygen_%O2'] = hPa_to_percentO2(df['oxygen_hPa'], df['pressure'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9d257655-af49-4aa4-be7d-7c7fe8528419",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['oxygen_%airsat'] = hPa_to_percent_airsat(df['oxygen_hPa'], df['pressure'], temperature=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "96e8e2da-0adf-40b7-ab06-b155a828966b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['oxygen_mgL'] = hPa_to_mgL(df['oxygen_hPa'], temperature=20, salinity=7.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ff8e1d08-1ef5-4b2f-ae80-a90a2656d0e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['oxygen_uM'] = hPa_to_uM(df['oxygen_hPa'], temperature=20, salinity=7.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d95493e4-5869-4cf4-ab00-cfce4ac4659f",
   "metadata": {},
   "source": [
    "## unit conversion mg/L for different environmental conditions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2116685c-5ad9-49e4-8704-283f269a89b2",
   "metadata": {},
   "source": [
    "Example: user has logged data with a fixed-T setting of 37°C and salinity of 7.5g/L, but the real temperature was 32°C and real salinity was 11g/L."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1a61b08b-6ddc-4e8f-8902-0f8f17353683",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date_time\n",
       "2023-04-03 13:16:28.485    0.247168\n",
       "2023-04-03 13:16:29.486    1.704633\n",
       "2023-04-03 13:16:30.486    2.777244\n",
       "2023-04-03 13:16:31.486    3.508465\n",
       "2023-04-03 13:16:32.486    3.947728\n",
       "2023-04-03 13:16:33.485    4.229732\n",
       "2023-04-03 13:16:34.485    4.379028\n",
       "2023-04-03 13:16:35.485    4.378365\n",
       "2023-04-03 13:16:36.485    4.379360\n",
       "2023-04-03 13:16:37.485    4.419836\n",
       "2023-04-03 13:16:38.485    4.491167\n",
       "2023-04-03 13:16:39.486    4.563492\n",
       "2023-04-03 13:16:40.487    4.635486\n",
       "Name: oxygen_hPa, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "po2 = i_only_think_in_hpa(df, m)\n",
    "hPa_to_mgL(po2, temperature=32, salinity=11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7d6b7b6-bd4c-40ff-a9de-46bc9dc78eeb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
