{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import tarfile\n",
    "import io\n",
    "# %matplotlib inline\n",
    "%matplotlib tk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tar2array(fname, aname=\"proj2d_0.npy\"):\n",
    "    tar = tarfile.open(fname,\"r:gz\")\n",
    "    array_file = io.BytesIO()\n",
    "    array_file.write(tar.extractfile(aname).read())\n",
    "    array_file.seek(0)\n",
    "    x = np.load(array_file)\n",
    "    x = np.transpose(x, (1, 0))\n",
    "    print(x.shape)\n",
    "    return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(9592, 8256)\n",
      "117\n",
      "(array([  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
      "         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
      "         0, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109,\n",
      "       109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109,\n",
      "       109, 109, 218, 218, 218, 218, 218, 218, 218, 218, 218, 218, 218,\n",
      "       218, 218, 218, 218, 218, 218, 218, 218, 218, 218, 218, 218, 218,\n",
      "       218, 218, 218, 218, 218, 327, 327, 327, 327, 327, 327, 327, 327,\n",
      "       327, 327, 327, 327, 327, 327, 327, 327, 327, 327, 327, 327, 327,\n",
      "       327, 327, 327, 327, 327, 327, 327, 436, 436, 436, 436, 436, 436]), array([4329, 4330, 4331, 4332, 4333, 4334, 4335, 4336, 4337, 4338, 4339,\n",
      "       4340, 4341, 4342, 4343, 4344, 4345, 4346, 4347, 4348, 4349, 4350,\n",
      "       4351, 4352, 4353, 4368, 4369, 4348, 4349, 4350, 4351, 4352, 4353,\n",
      "       4354, 4355, 4356, 4357, 4358, 4359, 4360, 4361, 4362, 4363, 4364,\n",
      "       4365, 4366, 4367, 4368, 4369, 4370, 4371, 4372, 4373, 4374, 4368,\n",
      "       4369, 4370, 4371, 4372, 4373, 4374, 4375, 4376, 4377, 4378, 4379,\n",
      "       4380, 4381, 4382, 4383, 4384, 4385, 4386, 4387, 4388, 4389, 4390,\n",
      "       4391, 4392, 4393, 4395, 4399, 4400, 4388, 4389, 4390, 4391, 4392,\n",
      "       4393, 4394, 4395, 4396, 4397, 4398, 4399, 4400, 4401, 4402, 4403,\n",
      "       4404, 4405, 4406, 4407, 4408, 4409, 4410, 4411, 4412, 4413, 4414,\n",
      "       4415, 4409, 4411, 4412, 4413, 4414, 4415]))\n",
      "(9592, 8256)\n",
      "14\n",
      "(array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), array([4340, 4341, 4342, 4343, 4344, 4345, 4346, 4347, 4348, 4349, 4350,\n",
      "       4351, 4352, 4353]))\n"
     ]
    }
   ],
   "source": [
    "%matplotlib tk\n",
    "ref = tar2array(\"ref_1.tar.gz\")\n",
    "print(np.count_nonzero(ref))\n",
    "print(np.nonzero(ref))\n",
    "tar = tar2array(\"tar_3.tar.gz\")\n",
    "print(np.count_nonzero(tar))\n",
    "print(np.nonzero(tar))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0x7f144523f320>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plt.figure()\n",
    "plt.imshow(ref,clim=(0,1e3),aspect='auto',origin='lower')\n",
    "plt.xlim(4000,4600)\n",
    "plt.ylim(0,10)\n",
    "plt.colorbar()\n",
    "plt.figure()\n",
    "plt.imshow(tar,clim=(0,1e3),aspect='auto',origin='lower')\n",
    "plt.xlim(4000,4600)\n",
    "plt.ylim(0,10)\n",
    "plt.colorbar()"
   ]
  },
  {
   "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.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
