{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculating Surface Roughness"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook demonstrates how to open a Nanosurf image file (*.nid) and calculate the surface roughness of the image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import required modules\n",
    "from NSFopen.read import read\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading Header\n",
      "Reading Data\n",
      "Elapsed Time: 0.02 sec\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# import the data\n",
    "data_file = \"sapphire.nid\"\n",
    "afm = read(data_file)\n",
    "data = afm.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# make a function to flatten the image\n",
    "def flatten(data, order=1):\n",
    "    data_out = np.copy(data)  # create copy of data\n",
    "    for idx, line in enumerate(data_out):\n",
    "        x = np.arange(len(line))\n",
    "        p = np.polyfit(x, line, order)  # fit data to polynomial\n",
    "        y = np.polyval(p, x)\n",
    "        data_out[idx] = line - y  # subtract fit from data\n",
    "    return data_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "Zaxis = data['Image']['Forward']['Z-Axis'] * 1e9  # height data scaled to nanometers\n",
    "\n",
    "Zaxis_ = flatten(Zaxis, order=1)  # flatten data with 1st order polynomial (i.e. line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.042123098751831574\n"
     ]
    }
   ],
   "source": [
    "# calculate the roughness as the Standard Deviation of the data array\n",
    "rms = np.std(Zaxis_)\n",
    "print(rms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Sample roughness (nm): 0.042')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 10016 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(Zaxis_.flatten(), bins = 256);  # this flatten is the numpy function that converts a 2D array to 1D\n",
    "plt.xlabel('Z-Axis [nm]');\n",
    "plt.ylabel('[Counts]');\n",
    "plt.title('Sample roughness (nm): %5.3f' % rms)"
   ]
  }
 ],
 "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.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
