{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Applying age models\n\nIn this scenario, the challenge is to apply an age model to a new selection of\ndepths. This situation may occur when sampling a core. \n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pathlib import Path\nimport pandas as pd\nimport plotly\nimport plotly.graph_objects as go\nimport paleos.agemodel as pam\nimport paleos.common as pcm"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Begin by defining the location of our age model data and the selection of\ndepths for which to calculate ages.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_path = Path(\"data\")\nage_model_path = data_path / \"667.csv\"\ndepths_path = data_path / \"depth_667.csv\"\n\nif not age_model_path.exists():\n    raise FileExistsError(f\"Unable to find age model {age_model_path}\")\nif not data_path.exists():\n    raise FileExistsError(f\"Unable to find data file {data_path}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, read in the age model, remove NaNs and provide column names\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "age_model_df = pd.read_csv(age_model_path, header=None)\nage_model_df = age_model_df.dropna()\nage_model_df.columns = [\"depth\", \"age\"]\nprint(age_model_df)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set the index to depth, and create a pandas Series by explicitly taking the\nage column\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "age_model_df = age_model_df.set_index(\"depth\")\nage_model = age_model_df[\"age\"]\nprint(age_model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Add a (0,0) origin point and remove any duplicated depths \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "age_model = pam.add_point(age_model, depth=0, age=0, sort_result=True)\nage_model = pcm.average_duplicated(age_model)\nprint(age_model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The next step is to get the depths to calculate ages for\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "new_depths = pd.read_csv(depths_path, header=None).squeeze(\"columns\")\nprint(new_depths)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we have both the age model and the depths we want to apply it to, we can\ndo the application using linear resampling from paleos. In this example, \nlinear resampling on to depths is used but there are other options.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "new_depth_age = pam.resample_linear(age_model, new_depths.values)\nnew_depth_age.name = \"age\"\nnew_depth_age.index.name = \"depth\"\nprint(new_depth_age)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally plot the original age model and the interpolated values on the new\ndepths\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = go.Figure()\nfig.add_trace(\n    go.Scatter(\n        x=age_model.values,\n        y=age_model.index,\n        mode=\"lines+markers\",\n        marker=dict(color=\"red\", symbol=\"circle\", size=10),\n        name=\"age model\",\n    )\n)\nfig.add_trace(\n    go.Scatter(\n        x=new_depth_age.values,\n        y=new_depth_age.index,\n        mode=\"markers\",\n        marker=dict(color=\"blue\", symbol=\"x\"),\n        name=\"depth with age model\",\n    )\n)\nfig.update_layout(\n    title_text=f\"{data_path.stem} with age model {age_model_path.stem}\",\n    height=800,\n    yaxis_title=\"Depth (m)\",\n    xaxis_title=\"Age (Ma)\",\n)\nfig.update_yaxes(autorange=\"reversed\")\nplotly.io.show(fig)"
      ]
    }
  ],
  "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
}