{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0.2          7.           2.64575131]\n",
      " [  0.4         12.           3.46410162]\n",
      " [  0.6         18.           4.24264069]\n",
      " [  0.8        132.          11.48912529]\n",
      " [  1.           3.           1.73205081]]\n",
      "2.6457513110645907\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Generate array with \"nlabels\" columns of numbers, each containing \"ndata\"\n",
    "\n",
    "ndata = 5\n",
    "nlabels = 3\n",
    "\n",
    "data = np.zeros([ndata,nlabels])\n",
    "positions = np.array([0.2, 0.4, 0.6, 0.8, 1.0])\n",
    "counts = np.array([7.0, 12.0, 18.0, 132.0, 3.0])\n",
    "\n",
    "data[:,0] = positions\n",
    "data[:,1] = counts\n",
    "data[:,2] = np.sqrt(counts)\n",
    "\n",
    "print(data)\n",
    "print(data[0,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save array in ASCII file\n",
    "fname = \"datafile.txt\"\n",
    "np.savetxt(fname,data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#==============================================\n",
    "# Read array from datafile.txt\n",
    "datain = np.zeros( [ndata,nlabels] )\n",
    "datain = np.loadtxt(fname,dtype=float)\n",
    "\n",
    "print(datain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save array in ASCII file with labeled columns\n",
    "\n",
    "labels = ['Positions', 'Counts', 'Errors']\n",
    "\n",
    "fname = \"datafile2.txt\"\n",
    "f=open(fname,\"w\")  #\"w\" means you are writing on file\n",
    "f.write(labels[0] + \" \" + labels[1] + \" \" + labels[2] + \"\\n\") # + is concatenating strings, \\n ends line\n",
    "f.write(str(data))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reading data line by line\n",
    "f=open(fname,\"r\") #\"r\" means you are reading from file\n",
    "for line in f:\n",
    "    print(line)\n",
    "f.close() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Same as above, but using \"with\" construction to close automatically the file at the end\n",
    "with open(fname,\"r\") as f: # In this way I don't need f.close() at the end; file gets automatically closed \n",
    "    for line in f:\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now I want to do the same as above and then at the end also convert the above result into an array. \n",
    "# Let us first check how the strings are represented\n",
    "f=open(fname,\"r\")\n",
    "for line in f:\n",
    "    print(repr(line))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now I start illustrating array conversion procedure, step by step. \n",
    "# Step 1. Remove the '\\n' at the end and square parenthesis\n",
    "f=open(fname,\"r\")\n",
    "for line in f:\n",
    "    # With strip I remove \\n and parenthesis\n",
    "    line = line.strip()\n",
    "    line = line.strip('[')\n",
    "    line = line.strip(']')\n",
    "    line = line.strip(']]')\n",
    "    print(repr(line))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Step 2. Now I also want to remove header, that is, read from line 3 onwards (also still want to remove \\n ) \n",
    "f=open(fname,\"r\")\n",
    "# Storing everything at once in \"lines\" (memory inefficient for big files)\n",
    "lines = f.readlines()\n",
    "# Selecting lines from 3 onwards\n",
    "lines = lines[1:]\n",
    "# Removing \\n and parenthesis as before\n",
    "for line in(lines):\n",
    "    line = line.strip()\n",
    "    line = line.strip('[')\n",
    "    line = line.strip(']')\n",
    "    line = line.strip(']]')\n",
    "    print(repr(line))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Step 3. Nearly there. I finally want to convert string into arrays. Starting from lines, obtained above...\n",
    "\n",
    "# Define blank list to which I will append the numbers read from file\n",
    "datain = [ ]\n",
    "for line in(lines):\n",
    "    line = line.strip()\n",
    "    line = line.strip('[')\n",
    "    line = line.strip(']')\n",
    "    line = line.strip(']]')\n",
    "    \n",
    "#####################################################################################\n",
    "    # Split line in columns by separating it into items with split command.\n",
    "    # Assumes here that items are separated by whitespaces\n",
    "    columns = line.split()\n",
    "    #print(line, columns)\n",
    "    # Now convert each column entry to float and append to datatemp list\n",
    "    datatemp = [ ]\n",
    "    for i in range(len(columns)):\n",
    "        datatemp.append(float(columns[i]))\n",
    "    # Append line to overall datain list \n",
    "    datain.append(datatemp)\n",
    "    \n",
    "# Finally convert to array    \n",
    "datain = np.array(datain)\n",
    "print(datain)\n",
    "\n",
    "f.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0.4         12.           3.46410162]\n",
      " [  0.6         18.           4.24264069]\n",
      " [  0.8        132.          11.48912529]\n",
      " [  1.           3.           1.73205081]]\n"
     ]
    }
   ],
   "source": [
    "# This is how the full thing reads (i.e., script to start with datafile2.txt and read it into array) \n",
    "#############################################\n",
    "\n",
    "f=open(fname,\"r\")\n",
    "# Storing everything at once in \"lines\" (memory inefficient for big files)\n",
    "lines = f.readlines()\n",
    "# Selecting lines from 3 onwards\n",
    "lines = lines[2:]\n",
    "# Splitting lines into colums, converting columns entry to float and append to list\n",
    "datain = [ ]\n",
    "for line in(lines):\n",
    "    # Removing \\n \n",
    "    line = line.strip()\n",
    "    line = line.strip('[')\n",
    "    line = line.strip(']')\n",
    "    line = line.strip(']]')\n",
    "    # Split line in columns by separating it into items with split command.\n",
    "    # Assumes here that items are separated by whitespaces\n",
    "    columns = line.split()\n",
    "    # Now convert each column entry to float and append to datatemp list\n",
    "    datatemp = [ ]\n",
    "    for i in range(len(columns)):\n",
    "        datatemp.append(float(columns[i]))\n",
    "    # Append datatemp to overall datain list \n",
    "    datain.append(datatemp)\n",
    "    \n",
    "# Finally convert to array    \n",
    "datain = np.array(datain)\n",
    "print(datain)\n",
    "\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save array in ASCII file with labeled columns, but now with better formatting to avoid the previous mess when reading\n",
    "\n",
    "labels = ['Positions', 'Counts', 'Errors']\n",
    "\n",
    "fname = \"datafile3.txt\"\n",
    "f=open(fname,\"w\")  #\"w\" means you are writing on file\n",
    "\n",
    "f.write(labels[0] +  \"         \" + labels[1] + \"       \" + labels[2] + \"\\n\") # + is concatenating strings, \\n ends line\n",
    "f.write(\"-------------------------------------------\" + \"\\n\")\n",
    "for i in range(ndata):\n",
    "    f.write(\"  \" + str(data[i,0]) + \"               \"  + '%-3s' % str(int(data[i,1])) + \"         \" + str('%.2f' % data[i,2]) + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0.2    7.     2.65]\n",
      " [  0.4   12.     3.46]\n",
      " [  0.6   18.     4.24]\n",
      " [  0.8  132.    11.49]\n",
      " [  1.     3.     1.73]]\n"
     ]
    }
   ],
   "source": [
    "# Reading data using np.genfromtxt, skip first line in reading and save the rest in array\n",
    "datain3 = np.genfromtxt(fname,dtype=float,skip_header=2)\n",
    "print(datain3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0.2    7.     2.65]\n",
      " [  0.4   12.     3.46]\n",
      " [  0.6   18.     4.24]\n",
      " [  0.8  132.    11.49]\n",
      " [  1.     3.     1.73]]\n"
     ]
    }
   ],
   "source": [
    "# Similar, with np.loadtxt\n",
    "datain4 = np.loadtxt(fname,dtype=float,skiprows =2)\n",
    "print(datain4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
