{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9925bece-9ef8-4585-9e12-8e3765e69dc2",
   "metadata": {},
   "source": [
    "# Load multiple files and time adjustments"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "564310bb-005c-4e64-b60e-04ebcdc93f94",
   "metadata": {},
   "source": [
    "First the aerosoltools package is loaded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db93a15e-d69e-422a-993d-107fae852f5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os;\n",
    "import sys;\n",
    "\n",
    "# Adjust this to the relative path from your notebook to the source directory\n",
    "sys.path.insert(0, os.path.abspath('../../../src'));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bb798a7-41ee-40fc-9f0a-a4547c87df3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import aerosoltools as at\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b715c1c-f7c4-4689-a1e9-119d3efbed1e",
   "metadata": {},
   "source": [
    "The path to the folder containing relevant files is specified and passed to the load_data_from_folder function along with the load function to use when reading the data files. Here a keyword (search_word) can also be passed to only load e.g. .txt files or only load files containg the word \"OPS\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "97c139fa-e103-490e-a3bc-a20f46e2548c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading: ..\\..\\..\\tests\\data\\OPS_data\\OPS_data1.csv\n",
      "Loading: ..\\..\\..\\tests\\data\\OPS_data\\OPS_data2.csv\n",
      "Loading: ..\\..\\..\\tests\\data\\OPS_data\\OPS_data3.csv\n",
      "Loading: ..\\..\\..\\tests\\data\\OPS_data\\OPS_data4.csv\n"
     ]
    }
   ],
   "source": [
    "folderpath = r\"..\\..\\..\\tests\\data\\OPS_data\";\n",
    "OPS_data = at.Load_data_from_folder(folderpath,at.Load_OPS_file,search_word = \"OPS\");"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9b7d4f0-21d2-47ba-848f-173bd89b3966",
   "metadata": {},
   "source": [
    "It is seen that the Load_data_from_folder function loaded 4 different files all from an OPS instrument. These are combined to a single data variable and sorted based on their time and dates. The files therefore do not have to be ordered when loading. However, the function will only combine files that come from the same instrument (it checks the instrument serial number - if given in the metadata).\n",
    "\n",
    "We can now get an overview of the data by plotting a timeseries plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1ddc78cc-d547-48e3-a1e9-cc22b1455804",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<Figure size 1000x600 with 3 Axes>,\n",
       " array([<Axes: ylabel='dN, cm$^{-3}$'>,\n",
       "        <Axes: xlabel='Time', ylabel='Dp, nm'>,\n",
       "        <matplotlib.colorbar.Colorbar object at 0x000002A786B41D60>],\n",
       "       dtype=object))"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 41904 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "OPS_data.plot_timeseries(y_3d=(1,0));"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34aa8d7c-a739-470c-9420-4e9547af429d",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "We see that the data stretches over several days and generally has quite low levels.\n",
    "\n",
    "Let's try to crop the data to only include data from the 25th and 26th, then shift the data by 120 minutes, smooth the data with a rolling window of 60 minutes width before rebinning the data into 2.5 hr timebins. Finally, we plot the mean particle size distribution from all time points of the treated dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "49de38d8-c0b0-485f-b0fa-162b135e5f62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 291520 characters]",
      "text/plain": [
       "<Figure size 1900x1000 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Generate a plot\n",
    "fig,axs = plt.subplots(nrows=2,ncols=3)\n",
    "\n",
    "# Crop the data to only include datapoint after the 1st of April at 8:00:00\n",
    "\n",
    "# Plot the original data and mark the activities\n",
    "OPS_data.plot_total_conc(axs[0,0])\n",
    "axs[0,0].set_title(\"Original data\")\n",
    "\n",
    "# Smooth the data by taking the mean of every 3 datapoints. This does not rebin the data, it is just smoothing\n",
    "OPS_data.timecrop(start = \"2023-10-25 08:00:00\",end = \"2023-10-26 22:00:00\")\n",
    "OPS_data.plot_total_conc(axs[0,1])\n",
    "axs[0,1].set_title(\"Cropped data\")\n",
    "\n",
    "# Shift the time 30 minutes\n",
    "OPS_data.timeshift(minutes=120)\n",
    "OPS_data.plot_total_conc(axs[0,2])\n",
    "axs[0,2].set_title(\"Shifted data\")\n",
    "\n",
    "# Crop the data to only include datapoint after the 1st of April at 8:00:00\n",
    "OPS_data.timesmooth(60,method=\"mean\")\n",
    "OPS_data.plot_total_conc(axs[1,0])\n",
    "axs[1,0].set_title(\"Smoothed data\")\n",
    "\n",
    "# Rebin the data to 2.5h bins. There are also options to specify minutes or seconds instead of hours\n",
    "OPS_data.timerebin(\"2.5h\")\n",
    "OPS_data.plot_total_conc(axs[1,1])\n",
    "axs[1,1].set_title(\"Rebinned data\")\n",
    "\n",
    "OPS_data.plot_psd(normalize=True, ax = axs[1,2])\n",
    "axs[1,2].set_title(\"PSD\")\n",
    "plt.tight_layout()"
   ]
  }
 ],
 "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.9.22"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
