{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Basic File I/O\n",
    "\n",
    "## Numpy\n",
    "* ```np.savetxt```\n",
    "* ```np.loadtxt```\n",
    "* ```np.column_stack```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Create some data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "x     = np.linspace(0,10,25)\n",
    "y_exp = x**0.6 + (np.random.rand(25) -0.5)        # ploting data series below\n",
    "y_1   = x**0.6\n",
    "y_2   = np.exp(-0.2*x)*np.cos(2*x)*2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Create a single 2-D array (matrix) for saving to a file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.         -0.32389329  0.          2.        ]\n",
      " [ 0.41666667  0.74336245  0.59138902  1.23729826]\n",
      " [ 0.83333333  0.81505521  0.89637813 -0.16205647]\n",
      " [ 1.25        0.6690392   1.14326263 -1.24786255]\n",
      " [ 1.66666667  1.31372968  1.35865518 -1.40680032]\n",
      " [ 2.08333333  1.47338264  1.5532997  -0.68433875]\n",
      " [ 2.5         1.62951114  1.73286211  0.34409962]\n",
      " [ 2.91666667  2.12909154  1.90078099  1.00503411]\n",
      " [ 3.33333333  1.5916393   2.05933617  0.95225291]\n",
      " [ 3.75        2.4290252   2.21013479  0.32747786]\n",
      " [ 4.16666667  2.5185093   2.35436208 -0.4008769 ]\n",
      " [ 4.58333333  2.12316273  2.49292301 -0.77320831]\n",
      " [ 5.          3.00216109  2.6265278  -0.61735433]\n",
      " [ 5.41666667  3.12816232  2.75574651 -0.10934473]\n",
      " [ 5.83333333  2.86776065  2.88104523  0.38728711]\n",
      " [ 6.25        2.93408616  3.00281108  0.57174799]\n",
      " [ 6.66666667  3.06685286  3.12136995  0.37959133]\n",
      " [ 7.08333333  3.14361379  3.23699932 -0.01430654]\n",
      " [ 7.5         3.39452026  3.34993791 -0.33901857]\n",
      " [ 7.91666667  3.86639188  3.46039293 -0.40735687]\n",
      " [ 8.33333333  3.4006333   3.56854561 -0.21704892]\n",
      " [ 8.75        3.5441406   3.67455567  0.0762659 ]\n",
      " [ 9.16666667  4.20887605  3.7785647   0.2780916 ]\n",
      " [ 9.58333333  3.40124267  3.88069898  0.27952457]\n",
      " [10.          3.96130053  3.98107171  0.1104558 ]]\n"
     ]
    }
   ],
   "source": [
    "data = np.column_stack([x,y_exp, y_1, y_2])\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Save to file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# x, y_exp, y_1, y_2\n",
      "     0.000,    -0.324,     0.000,     2.000\n",
      "     0.417,     0.743,     0.591,     1.237\n",
      "     0.833,     0.815,     0.896,    -0.162\n",
      "     1.250,     0.669,     1.143,    -1.248\n",
      "     1.667,     1.314,     1.359,    -1.407\n",
      "     2.083,     1.473,     1.553,    -0.684\n",
      "     2.500,     1.630,     1.733,     0.344\n",
      "     2.917,     2.129,     1.901,     1.005\n",
      "     3.333,     1.592,     2.059,     0.952\n",
      "     3.750,     2.429,     2.210,     0.327\n",
      "     4.167,     2.519,     2.354,    -0.401\n",
      "     4.583,     2.123,     2.493,    -0.773\n",
      "     5.000,     3.002,     2.627,    -0.617\n",
      "     5.417,     3.128,     2.756,    -0.109\n",
      "     5.833,     2.868,     2.881,     0.387\n",
      "     6.250,     2.934,     3.003,     0.572\n",
      "     6.667,     3.067,     3.121,     0.380\n",
      "     7.083,     3.144,     3.237,    -0.014\n",
      "     7.500,     3.395,     3.350,    -0.339\n",
      "     7.917,     3.866,     3.460,    -0.407\n",
      "     8.333,     3.401,     3.569,    -0.217\n",
      "     8.750,     3.544,     3.675,     0.076\n",
      "     9.167,     4.209,     3.779,     0.278\n",
      "     9.583,     3.401,     3.881,     0.280\n",
      "    10.000,     3.961,     3.981,     0.110\n"
     ]
    }
   ],
   "source": [
    "np.savetxt(\"data.dat\", data)\n",
    "\n",
    "#----- OR THIS\n",
    "\n",
    "np.savetxt(\"data.dat\", data, fmt='%10.3f', \n",
    "           header=\"x, y_exp, y_1, y_2\",\n",
    "           delimiter = ',')\n",
    "\n",
    "!cat data.dat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Load from file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.    -0.324  0.     2.   ]\n",
      " [ 0.417  0.743  0.591  1.237]\n",
      " [ 0.833  0.815  0.896 -0.162]\n",
      " [ 1.25   0.669  1.143 -1.248]\n",
      " [ 1.667  1.314  1.359 -1.407]\n",
      " [ 2.083  1.473  1.553 -0.684]\n",
      " [ 2.5    1.63   1.733  0.344]\n",
      " [ 2.917  2.129  1.901  1.005]\n",
      " [ 3.333  1.592  2.059  0.952]\n",
      " [ 3.75   2.429  2.21   0.327]\n",
      " [ 4.167  2.519  2.354 -0.401]\n",
      " [ 4.583  2.123  2.493 -0.773]\n",
      " [ 5.     3.002  2.627 -0.617]\n",
      " [ 5.417  3.128  2.756 -0.109]\n",
      " [ 5.833  2.868  2.881  0.387]\n",
      " [ 6.25   2.934  3.003  0.572]\n",
      " [ 6.667  3.067  3.121  0.38 ]\n",
      " [ 7.083  3.144  3.237 -0.014]\n",
      " [ 7.5    3.395  3.35  -0.339]\n",
      " [ 7.917  3.866  3.46  -0.407]\n",
      " [ 8.333  3.401  3.569 -0.217]\n",
      " [ 8.75   3.544  3.675  0.076]\n",
      " [ 9.167  4.209  3.779  0.278]\n",
      " [ 9.583  3.401  3.881  0.28 ]\n",
      " [10.     3.961  3.981  0.11 ]]\n"
     ]
    }
   ],
   "source": [
    "loaded_data = np.loadtxt(\"data.dat\", delimiter = ',')    # delimiter needed if used above\n",
    "print(loaded_data)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## General File IO\n",
    "### Input\n",
    "#### ```readline``` function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# x, y_exp, y_1, y_2\n",
      "\n",
      "     0.000,    -0.324,     0.000,     2.000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "ifile = open(\"data.dat\", \"r\")   # open a file for reading\n",
    "s = ifile.readline()\n",
    "print(s)\n",
    "s = ifile.readline()            # readline function will read the next line in the file\n",
    "print(s)\n",
    "ifile.close()                   # close the file when you're done\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### ```readlines``` function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# x, y_exp, y_1, y_2\n",
      "     0.000,    -0.324,     0.000,     2.000\n",
      "     0.417,     0.743,     0.591,     1.237\n",
      "     0.833,     0.815,     0.896,    -0.162\n",
      "     1.250,     0.669,     1.143,    -1.248\n",
      "     1.667,     1.314,     1.359,    -1.407\n",
      "     2.083,     1.473,     1.553,    -0.684\n",
      "     2.500,     1.630,     1.733,     0.344\n",
      "     2.917,     2.129,     1.901,     1.005\n",
      "     3.333,     1.592,     2.059,     0.952\n",
      "     3.750,     2.429,     2.210,     0.327\n",
      "     4.167,     2.519,     2.354,    -0.401\n",
      "     4.583,     2.123,     2.493,    -0.773\n",
      "     5.000,     3.002,     2.627,    -0.617\n",
      "     5.417,     3.128,     2.756,    -0.109\n",
      "     5.833,     2.868,     2.881,     0.387\n",
      "     6.250,     2.934,     3.003,     0.572\n",
      "     6.667,     3.067,     3.121,     0.380\n",
      "     7.083,     3.144,     3.237,    -0.014\n",
      "     7.500,     3.395,     3.350,    -0.339\n",
      "     7.917,     3.866,     3.460,    -0.407\n",
      "     8.333,     3.401,     3.569,    -0.217\n",
      "     8.750,     3.544,     3.675,     0.076\n",
      "     9.167,     4.209,     3.779,     0.278\n",
      "     9.583,     3.401,     3.881,     0.280\n",
      "    10.000,     3.961,     3.981,     0.110\n"
     ]
    }
   ],
   "source": [
    "ifile = open(\"data.dat\", \"r\")\n",
    "lines = ifile.readlines()             # readlines function gets a list of all lines\n",
    "for line in lines :\n",
    "    print(line, end='')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### ```split``` function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    10.000,     3.961,     3.981,     0.110\n",
      "\n",
      "['10.000,', '3.961,', '3.981,', '0.110']\n",
      "10.000,\n",
      "3.961,\n",
      "3.981,\n",
      "0.110\n"
     ]
    }
   ],
   "source": [
    "words = line.split()\n",
    "print(line)\n",
    "print(words)\n",
    "for w in words:\n",
    "    print(w)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Some header\n",
      "some new line. This isnt a new line\n",
      "The third line"
     ]
    }
   ],
   "source": [
    "ofile = open(\"output.dat\", \"w\")         # Open a file for writing: ofile can be any name\n",
    "ofile.write(\"Some header\\n\")            # write some text: note the \"\\n\" makes a new line\n",
    "ofile.write(\"some new line.\")           # write another line of text (note there is no \\n)\n",
    "ofile.write(\" This isnt a new line\\n\")  # and another\n",
    "ofile.write(\"The third line\")           # and other\n",
    "ofile.close()                           # close the file\n",
    "\n",
    "!cat output.dat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Triple strings preserve white space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi there,\n",
      "How is your day going so far?\n",
      "\n",
      "    I hope you are good."
     ]
    }
   ],
   "source": [
    "ofile = open(\"triplestring.dat\", \"w\")\n",
    "s = \"\"\"Hi there,\n",
    "How is your day going so far?\n",
    "\n",
    "    I hope you are good.\"\"\"\n",
    "\n",
    "ofile.write(s)\n",
    "ofile.close()\n",
    "!cat triplestring.dat"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "base",
   "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.14.6"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
