{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Extracting sample depths\n\nThis example merges core section top depth and sample depth to provide depths\nfor individual samples. It does not require paleos at all and is here to serve\nas an example for those who have a similar challenge.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pathlib import Path\nimport pandas as pd"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Begin by defining the location of our data\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_path = Path(\"data\")\nsamples_path = data_path / \"samples_biom_730.csv\"\ndepth_path = data_path / \"section_summary_730.csv\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read in the sample data\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "df_samples = pd.read_csv(samples_path)\ndf_samples[\"Hole\"] = [x.strip(\"'\") for x in df_samples[\"Hole\"]]\ndf_samples.loc[df_samples[\"Section\"] == \"CC\", \"Section\"] = 20\ndf_samples[\"Section\"] = df_samples[\"Section\"].astype(int)\nprint(df_samples)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read in depth data for the core\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "df_depth = pd.read_csv(depth_path)\ndf_depth = df_depth.dropna(subset=[\"Section\"])\ndf_depth.loc[df_depth[\"Section\"].str.contains(\"CC\"), \"Section\"] = 20\ndf_depth[\"Section\"] = df_depth[\"Section\"].astype(int)\nprint(df_depth)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use pandas merging to put the two together and get sample depth\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "df = pd.merge(\n    df_samples,\n    df_depth,\n    how=\"left\",\n    left_on=[\"Hole\", \"Core\", \"Section\"],\n    right_on=[\"Hole\", \"Core\", \"Section\"],\n)\ndf[\"Depth\"] = df[\"Top depth CSF-A (m)\"] + (df[\"Top (cm)\"]/100)\ndf.to_csv(\"samples_with_depth.csv\", index=False)\nprint(df)"
      ]
    }
  ],
  "metadata": {
    "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.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}