{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9be37a8e",
   "metadata": {},
   "source": [
    "## 12/08/2022 for use in google cloud\n",
    "## load registered images in chunks (~10 images), calculate max shift, merge/crop\n",
    "## MP & CP, save outputs and delete images\n",
    "## repeat next chunks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dbc7c512",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.patches as patches\n",
    "from pathlib import Path\n",
    "import numpy as np\n",
    "import os\n",
    "import sys\n",
    "import glob\n",
    "import pandas as pd\n",
    "import xml.etree.ElementTree as et\n",
    "import datetime\n",
    "from skimage.io import imread, imsave\n",
    "from imageio import volread as imread\n",
    "\n",
    "import tifffile\n",
    "import pystackreg\n",
    "from pystackreg import StackReg\n",
    "from skimage.filters import threshold_otsu\n",
    "\n",
    "from pystackreg.util import to_uint16   # make sure version 0.2.5 (not anything below)\n",
    "#from ims_to_tiff import convert_to_tif   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c2e1b5f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/mnt/disks/store/101222_D10_Coverslip1_Processed'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7f0e548f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '010', '011', '012', '013', '014', '015', '016', '017', '018', '019', '020', '021', '022', '023', '024', '025', '026', '027', '028', '029', '030', '031', '032', '033', '034', '035', '036', '037', '038', '039', '040', '041', '042', '043', '044', '045', '046', '047', '048', '049', '050', '051', '052', '053', '054', '055', '056', '057', '058', '059', '060', '061', '062', '063', '064', '065', '066', '067', '068', '069', '070', '071', '072', '073', '074', '075', '076', '077', '078', '079', '080', '081', '082', '083', '084', '085', '086', '087', '088', '089', '090', '091', '092', '093', '094', '095', '096', '097', '098', '099', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '200', '201', '202', '203', '204', '205', '206', '207', '208', '209', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '220', '221', '222', '223', '224']\n"
     ]
    }
   ],
   "source": [
    "_allCycles = sorted(glob.glob('tif/*'))\n",
    "allCycles = [x.split('_')[-1] for x in _allCycles]\n",
    "\n",
    "_allFOVs = sorted(glob.glob('reg_bin_Cyc_1/*'))\n",
    "allFOVs = [x.split('F')[-1][:3] for x in _allFOVs]\n",
    "print(allFOVs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "577319bd",
   "metadata": {},
   "source": [
    "## Calculate Shift and Crop/Merge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b27f2099",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/mnt/disks/store/101222_D10_Coverslip1_Processed'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1d189752",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 0.3275351598299494 4.500903524792193\n",
      "1 -0.9583836021045045 -0.44820092214229135\n",
      "2 0.29864077215961515 5.751225257532951\n",
      "2 -1.3560409842999661 -0.15284640260711058\n",
      "3 -0.03568706094949903 6.5948287338535465\n",
      "3 -1.6414119052524256 0.6316795219620417\n",
      "4 -0.467081646920974 6.8334207284015065\n",
      "4 -2.007391351792421 1.3366557613785517\n",
      "5 -0.7004231580135638 7.375345304351185\n",
      "5 -2.2403888374799408 1.188182166372144\n",
      "6 -0.4421562897352942 7.379404985129213\n",
      "6 -2.79938918404207 1.1617264974026966\n",
      "7 -0.7410122431177797 7.372888164832375\n",
      "7 -2.191070769676571 0.8614782631693743\n",
      "8 16.600637519919633 21.239065904115137\n",
      "8 14.08536357011603 14.951059444361931\n",
      "9 11.253359553525847 14.837170938090594\n",
      "9 9.038744668311523 8.692753269887135\n",
      "17 -3 22 -1\n"
     ]
    }
   ],
   "source": [
    "# define data directories\n",
    "def round_shift(val):\n",
    "    import math\n",
    "    if val <0: # if negative,\n",
    "        val = math.floor(val)\n",
    "    else:\n",
    "        val = math.ceil(val)\n",
    "    return val\n",
    "\n",
    "X_SHIFT_df = pd.DataFrame(index = allFOVs, columns = allCycles[1:])\n",
    "Y_SHIFT_df = pd.DataFrame(index = allFOVs, columns = allCycles[1:])\n",
    "\n",
    "for fov in allFOVs:\n",
    "    for cycle in allCycles[1:]:\n",
    "        tmat_loaded = np.load(f'tmat_Cyc_{cycle}/Cycle_{cycle}_F{fov}_tmat.npy')\n",
    "        x_shift = tmat_loaded[0,2]\n",
    "        y_shift = tmat_loaded[1,2]\n",
    "        if x_shift > 0: \n",
    "            x_shift = x_shift + 2048*np.tan(np.arcsin(tmat_loaded[0,1]))\n",
    "        if y_shift < 0:\n",
    "            y_shift = y_shift - 2048*np.tan(np.arcsin(tmat_loaded[0,1]))\n",
    "            \n",
    "        X_SHIFT_df.loc[fov, cycle] = x_shift\n",
    "        Y_SHIFT_df.loc[fov, cycle] = y_shift\n",
    "\n",
    "# print max among FOV across each cycle\n",
    "for cycle in allCycles[1:]:\n",
    "    print(cycle, X_SHIFT_df.loc[:, cycle].max(), Y_SHIFT_df.loc[:,cycle].max())\n",
    "    print(cycle, X_SHIFT_df.loc[:, cycle].min(), Y_SHIFT_df.loc[:,cycle].min())\n",
    "\n",
    "X_max_total = round_shift(X_SHIFT_df.max().max())\n",
    "X_min_total = round_shift(X_SHIFT_df.min().min())\n",
    "Y_max_total = round_shift(Y_SHIFT_df.max().max())\n",
    "Y_min_total = round_shift(Y_SHIFT_df.min().min()) \n",
    "print(X_max_total, X_min_total, Y_max_total, Y_min_total)    \n",
    "    \n",
    "# export X_SHIFT_df and Y_SHIFT_df\n",
    "X_SHIFT_df.to_csv('X_SHIFT_df.csv',sep=',')\n",
    "Y_SHIFT_df.to_csv('Y_SHIFT_df.csv',sep=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd4c3a7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000</th>\n",
       "      <td>-0.152338</td>\n",
       "      <td>-0.89795</td>\n",
       "      <td>-1.281005</td>\n",
       "      <td>-1.511238</td>\n",
       "      <td>-1.617661</td>\n",
       "      <td>-1.792489</td>\n",
       "      <td>-1.713663</td>\n",
       "      <td>15.914194</td>\n",
       "      <td>10.111194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>001</th>\n",
       "      <td>-0.24369</td>\n",
       "      <td>-1.00069</td>\n",
       "      <td>-1.315684</td>\n",
       "      <td>-1.586609</td>\n",
       "      <td>-1.717246</td>\n",
       "      <td>-1.955525</td>\n",
       "      <td>-1.898173</td>\n",
       "      <td>15.80237</td>\n",
       "      <td>10.125609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002</th>\n",
       "      <td>-0.191594</td>\n",
       "      <td>-0.937483</td>\n",
       "      <td>-1.306485</td>\n",
       "      <td>-1.533941</td>\n",
       "      <td>-1.592316</td>\n",
       "      <td>-1.904734</td>\n",
       "      <td>-1.842047</td>\n",
       "      <td>14.890083</td>\n",
       "      <td>9.778474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>003</th>\n",
       "      <td>-0.241864</td>\n",
       "      <td>-1.025016</td>\n",
       "      <td>-1.348329</td>\n",
       "      <td>-1.564217</td>\n",
       "      <td>-1.742842</td>\n",
       "      <td>-1.931903</td>\n",
       "      <td>-1.887727</td>\n",
       "      <td>15.471202</td>\n",
       "      <td>9.938571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>004</th>\n",
       "      <td>-0.27009</td>\n",
       "      <td>-1.006407</td>\n",
       "      <td>-1.403724</td>\n",
       "      <td>-1.642142</td>\n",
       "      <td>-1.750505</td>\n",
       "      <td>-1.984392</td>\n",
       "      <td>-1.933997</td>\n",
       "      <td>15.367487</td>\n",
       "      <td>9.863567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>220</th>\n",
       "      <td>-0.340386</td>\n",
       "      <td>-0.79435</td>\n",
       "      <td>-1.104711</td>\n",
       "      <td>-1.409183</td>\n",
       "      <td>-1.523407</td>\n",
       "      <td>-1.593827</td>\n",
       "      <td>-1.670901</td>\n",
       "      <td>15.42138</td>\n",
       "      <td>10.651064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>221</th>\n",
       "      <td>-0.341611</td>\n",
       "      <td>-0.754843</td>\n",
       "      <td>-1.072821</td>\n",
       "      <td>-1.418585</td>\n",
       "      <td>-1.451943</td>\n",
       "      <td>-1.554978</td>\n",
       "      <td>-1.672475</td>\n",
       "      <td>16.060501</td>\n",
       "      <td>10.677404</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>222</th>\n",
       "      <td>-0.330365</td>\n",
       "      <td>-0.745227</td>\n",
       "      <td>-1.015614</td>\n",
       "      <td>-1.37362</td>\n",
       "      <td>-1.515575</td>\n",
       "      <td>-1.55451</td>\n",
       "      <td>-1.668704</td>\n",
       "      <td>15.406434</td>\n",
       "      <td>10.691012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>223</th>\n",
       "      <td>-0.369397</td>\n",
       "      <td>-0.828257</td>\n",
       "      <td>-1.1223</td>\n",
       "      <td>-1.418966</td>\n",
       "      <td>-1.537321</td>\n",
       "      <td>-1.613196</td>\n",
       "      <td>-1.720416</td>\n",
       "      <td>15.706314</td>\n",
       "      <td>10.495703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>224</th>\n",
       "      <td>-0.345868</td>\n",
       "      <td>-0.869841</td>\n",
       "      <td>-1.127348</td>\n",
       "      <td>-1.42394</td>\n",
       "      <td>-1.590678</td>\n",
       "      <td>-1.653081</td>\n",
       "      <td>-1.772393</td>\n",
       "      <td>15.031115</td>\n",
       "      <td>10.403402</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>225 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            1         2         3         4         5         6         7  \\\n",
       "000 -0.152338  -0.89795 -1.281005 -1.511238 -1.617661 -1.792489 -1.713663   \n",
       "001  -0.24369  -1.00069 -1.315684 -1.586609 -1.717246 -1.955525 -1.898173   \n",
       "002 -0.191594 -0.937483 -1.306485 -1.533941 -1.592316 -1.904734 -1.842047   \n",
       "003 -0.241864 -1.025016 -1.348329 -1.564217 -1.742842 -1.931903 -1.887727   \n",
       "004  -0.27009 -1.006407 -1.403724 -1.642142 -1.750505 -1.984392 -1.933997   \n",
       "..        ...       ...       ...       ...       ...       ...       ...   \n",
       "220 -0.340386  -0.79435 -1.104711 -1.409183 -1.523407 -1.593827 -1.670901   \n",
       "221 -0.341611 -0.754843 -1.072821 -1.418585 -1.451943 -1.554978 -1.672475   \n",
       "222 -0.330365 -0.745227 -1.015614  -1.37362 -1.515575  -1.55451 -1.668704   \n",
       "223 -0.369397 -0.828257   -1.1223 -1.418966 -1.537321 -1.613196 -1.720416   \n",
       "224 -0.345868 -0.869841 -1.127348  -1.42394 -1.590678 -1.653081 -1.772393   \n",
       "\n",
       "             8          9  \n",
       "000  15.914194  10.111194  \n",
       "001   15.80237  10.125609  \n",
       "002  14.890083   9.778474  \n",
       "003  15.471202   9.938571  \n",
       "004  15.367487   9.863567  \n",
       "..         ...        ...  \n",
       "220   15.42138  10.651064  \n",
       "221  16.060501  10.677404  \n",
       "222  15.406434  10.691012  \n",
       "223  15.706314  10.495703  \n",
       "224  15.031115  10.403402  \n",
       "\n",
       "[225 rows x 9 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_SHIFT_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2bc5e8a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000</th>\n",
       "      <td>2.52146</td>\n",
       "      <td>3.846212</td>\n",
       "      <td>4.929632</td>\n",
       "      <td>5.34078</td>\n",
       "      <td>5.753712</td>\n",
       "      <td>5.867421</td>\n",
       "      <td>5.703006</td>\n",
       "      <td>19.739337</td>\n",
       "      <td>13.434808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>001</th>\n",
       "      <td>3.967372</td>\n",
       "      <td>5.479736</td>\n",
       "      <td>4.167602</td>\n",
       "      <td>6.833421</td>\n",
       "      <td>7.375345</td>\n",
       "      <td>7.335183</td>\n",
       "      <td>7.124973</td>\n",
       "      <td>20.992142</td>\n",
       "      <td>12.81051</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002</th>\n",
       "      <td>2.608865</td>\n",
       "      <td>1.974139</td>\n",
       "      <td>4.510214</td>\n",
       "      <td>4.971309</td>\n",
       "      <td>3.525255</td>\n",
       "      <td>5.605487</td>\n",
       "      <td>5.439646</td>\n",
       "      <td>19.064216</td>\n",
       "      <td>12.974836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>003</th>\n",
       "      <td>4.134146</td>\n",
       "      <td>5.34611</td>\n",
       "      <td>4.651769</td>\n",
       "      <td>4.940239</td>\n",
       "      <td>7.14748</td>\n",
       "      <td>5.347646</td>\n",
       "      <td>5.133203</td>\n",
       "      <td>19.698515</td>\n",
       "      <td>14.782095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>004</th>\n",
       "      <td>2.459904</td>\n",
       "      <td>2.220791</td>\n",
       "      <td>3.025208</td>\n",
       "      <td>3.534291</td>\n",
       "      <td>4.083188</td>\n",
       "      <td>3.993419</td>\n",
       "      <td>3.88666</td>\n",
       "      <td>19.398335</td>\n",
       "      <td>13.072539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>220</th>\n",
       "      <td>1.705548</td>\n",
       "      <td>2.488617</td>\n",
       "      <td>2.963595</td>\n",
       "      <td>3.680981</td>\n",
       "      <td>3.806705</td>\n",
       "      <td>3.707128</td>\n",
       "      <td>3.561901</td>\n",
       "      <td>16.776622</td>\n",
       "      <td>11.315806</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>221</th>\n",
       "      <td>1.542876</td>\n",
       "      <td>1.057641</td>\n",
       "      <td>1.455092</td>\n",
       "      <td>3.43656</td>\n",
       "      <td>2.424104</td>\n",
       "      <td>2.101588</td>\n",
       "      <td>1.952296</td>\n",
       "      <td>17.001311</td>\n",
       "      <td>9.603125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>222</th>\n",
       "      <td>1.492436</td>\n",
       "      <td>2.122249</td>\n",
       "      <td>0.63168</td>\n",
       "      <td>3.21889</td>\n",
       "      <td>3.35716</td>\n",
       "      <td>1.161726</td>\n",
       "      <td>3.13294</td>\n",
       "      <td>16.337732</td>\n",
       "      <td>8.692753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>223</th>\n",
       "      <td>3.906703</td>\n",
       "      <td>4.688158</td>\n",
       "      <td>5.070148</td>\n",
       "      <td>5.744735</td>\n",
       "      <td>5.862725</td>\n",
       "      <td>3.301323</td>\n",
       "      <td>5.596448</td>\n",
       "      <td>18.997781</td>\n",
       "      <td>13.201592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>224</th>\n",
       "      <td>1.826635</td>\n",
       "      <td>2.627348</td>\n",
       "      <td>2.916587</td>\n",
       "      <td>1.336656</td>\n",
       "      <td>3.712279</td>\n",
       "      <td>1.35847</td>\n",
       "      <td>3.458967</td>\n",
       "      <td>16.525679</td>\n",
       "      <td>11.302238</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>225 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            1         2         3         4         5         6         7  \\\n",
       "000   2.52146  3.846212  4.929632   5.34078  5.753712  5.867421  5.703006   \n",
       "001  3.967372  5.479736  4.167602  6.833421  7.375345  7.335183  7.124973   \n",
       "002  2.608865  1.974139  4.510214  4.971309  3.525255  5.605487  5.439646   \n",
       "003  4.134146   5.34611  4.651769  4.940239   7.14748  5.347646  5.133203   \n",
       "004  2.459904  2.220791  3.025208  3.534291  4.083188  3.993419   3.88666   \n",
       "..        ...       ...       ...       ...       ...       ...       ...   \n",
       "220  1.705548  2.488617  2.963595  3.680981  3.806705  3.707128  3.561901   \n",
       "221  1.542876  1.057641  1.455092   3.43656  2.424104  2.101588  1.952296   \n",
       "222  1.492436  2.122249   0.63168   3.21889   3.35716  1.161726   3.13294   \n",
       "223  3.906703  4.688158  5.070148  5.744735  5.862725  3.301323  5.596448   \n",
       "224  1.826635  2.627348  2.916587  1.336656  3.712279   1.35847  3.458967   \n",
       "\n",
       "             8          9  \n",
       "000  19.739337  13.434808  \n",
       "001  20.992142   12.81051  \n",
       "002  19.064216  12.974836  \n",
       "003  19.698515  14.782095  \n",
       "004  19.398335  13.072539  \n",
       "..         ...        ...  \n",
       "220  16.776622  11.315806  \n",
       "221  17.001311   9.603125  \n",
       "222  16.337732   8.692753  \n",
       "223  18.997781  13.201592  \n",
       "224  16.525679  11.302238  \n",
       "\n",
       "[225 rows x 9 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y_SHIFT_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "64565303",
   "metadata": {},
   "outputs": [],
   "source": [
    "###############################\n",
    "X_abs_shift = [abs(X_max_total), abs(X_min_total)]\n",
    "Y_abs_shift = [abs(Y_max_total), abs(Y_min_total)] \n",
    "\n",
    "def crop(X_x, X_n, Y_x, Y_n, img, pad):\n",
    "    \n",
    "    Y_total = img.shape[3]    # 2048\n",
    "    X_total = img.shape[-1]     # 2048\n",
    "    print('image shape', Y_total, X_total)\n",
    "    \n",
    "    if X_x * X_n > 0:   # same signs --> both negative or both positive\n",
    "        Xlength = X_total - (max(X_abs_shift))\n",
    "        print(Xlength)\n",
    "        if X_x > 0: # moving left\n",
    "            img = img[...,:, 0+pad:Xlength-pad]\n",
    "        if X_x < 0:  # moving right \n",
    "            img = img[...,:, abs(X_n)+pad:X_total-pad]\n",
    "\n",
    "    if X_x * X_n < 0:   # diff signs --> one is positive and other is negative\n",
    "        Xlength = X_total  - (abs(X_x) + abs(X_n))\n",
    "        print(Xlength)\n",
    "        img = img[...,:, abs(X_n)+pad: X_total - abs(X_x)-pad] # X_total?\n",
    "        \n",
    "    if X_x == 0 and X_n < 0:\n",
    "        Xlength = X_total  - abs(X_n)\n",
    "        print(Xlength)\n",
    "        img = img[...,:, abs(X_n)+pad:X_total-pad]\n",
    "    if X_x == 0 and X_n == 0:\n",
    "        Xlength = X_total\n",
    "        print(Xlength)\n",
    "        img = img[...,:,pad:X_total-pad]\n",
    "    if X_x > 0 and X_n == 0:\n",
    "        Xlength = X_total - X_x\n",
    "        print(Xlength)\n",
    "        img = img[...,:, 0+pad:Xlength-pad]\n",
    "### Y ## #\n",
    "    if Y_x * Y_n > 0:   \n",
    "        Ylength = Y_total - (max(Y_abs_shift))\n",
    "        print(Ylength)\n",
    "        if Y_x > 0: # up\n",
    "            img = img[..., 0+pad:Ylength-pad, :]\n",
    "        if Y_x < 0: # down\n",
    "            img = img[...,abs(Y_n)+pad:Y_total-pad, :]\n",
    "            \n",
    "    if Y_x * Y_n < 0:  # Y_x > 0 , Y_n < 0\n",
    "        Ylength = Y_total  - (abs(Y_x) + abs(Y_n)) # \n",
    "        print(Ylength)\n",
    "        img = img[...,abs(Y_n)+pad:Y_total-abs(Y_x)-pad, :] \n",
    "        \n",
    "    if Y_x == 0 and Y_n < 0: # down\n",
    "        Ylength = Y_total  - abs(Y_n)\n",
    "        print(Ylength)\n",
    "        img = img[..., abs(Y_n)+pad:Y_total-pad, :]\n",
    "        \n",
    "    if Y_x == 0 and Y_n == 0:\n",
    "        Ylength = Y_total\n",
    "        print(Ylength)\n",
    "        img = img[...,pad:Y_total-pad,:]\n",
    "        \n",
    "    if Y_x > 0 and Y_n == 0:\n",
    "        Ylength = Y_total - Y_x ## \n",
    "        print(Ylength)\n",
    "        img = img[..., 0+pad:Ylength-pad, :] ## up\n",
    "        \n",
    "    return Xlength, Ylength, img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "73ac0a20",
   "metadata": {},
   "outputs": [],
   "source": [
    "# define input directory\n",
    "DATA_DIR = '101222_D10_Coverslip1_Processed'\n",
    "pad = 5 # 5 pixel padding to be extra careful\n",
    "\n",
    "SOURCE = f'gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/{DATA_DIR}/'\n",
    "IN_DIR = 'reg'\n",
    "REF_DIR = 'tif'\n",
    "MERGE_DIR = 'merged' # output directory to save \n",
    "Z = 10\n",
    "final_ch = 38\n",
    "\n",
    "\n",
    "if not os.path.exists(f'{MERGE_DIR}'): \n",
    "    os.mkdir(f'{MERGE_DIR}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8df3c480",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['000', '001', '002', '003', '004', '005', '006', '007', '008', '009']\n",
      "['010', '011', '012', '013', '014', '015', '016', '017', '018', '019']\n",
      "['020', '021', '022', '023', '024', '025', '026', '027', '028', '029']\n",
      "['030', '031', '032', '033', '034', '035', '036', '037', '038', '039']\n",
      "['040', '041', '042', '043', '044', '045', '046', '047', '048', '049']\n",
      "['050', '051', '052', '053', '054', '055', '056', '057', '058', '059']\n",
      "['060', '061', '062', '063', '064', '065', '066', '067', '068', '069']\n",
      "['070', '071', '072', '073', '074', '075', '076', '077', '078', '079']\n",
      "['080', '081', '082', '083', '084', '085', '086', '087', '088', '089']\n",
      "['090', '091', '092', '093', '094', '095', '096', '097', '098', '099']\n",
      "['100', '101', '102', '103', '104', '105', '106', '107', '108', '109']\n",
      "['110', '111', '112', '113', '114', '115', '116', '117', '118', '119']\n",
      "['120', '121', '122', '123', '124', '125', '126', '127', '128', '129']\n",
      "['130', '131', '132', '133', '134', '135', '136', '137', '138', '139']\n",
      "['140', '141', '142', '143', '144', '145', '146', '147', '148', '149']\n",
      "['150', '151', '152', '153', '154', '155', '156', '157', '158', '159']\n",
      "['160', '161', '162', '163', '164', '165', '166', '167', '168', '169']\n",
      "['170', '171', '172', '173', '174', '175', '176', '177', '178', '179']\n",
      "['180', '181', '182', '183', '184', '185', '186', '187', '188', '189']\n",
      "['190', '191', '192', '193', '194', '195', '196', '197', '198', '199']\n",
      "['200', '201', '202', '203', '204', '205', '206', '207', '208', '209']\n",
      "['210', '211', '212', '213', '214', '215', '216', '217', '218', '219']\n",
      "['220', '221', '222', '223', '224']\n"
     ]
    }
   ],
   "source": [
    "for ii in range(0,len(allFOVs),10): # For each CHUNK (10 FOVs per loop)\n",
    "    print(allFOVs[ii:ii+10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a8ea93f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['000', '001', '002', '003', '004', '005', '006', '007', '008', '009']\n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/tif/Cycle_0/Cycle_F000.tif tif/Cycle_0\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/tif/Cycle_0/Cycle_F000.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_1/Cycle_1_F000_reg.tif reg_Cyc_1\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_1/Cycle_1_F000_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_2/Cycle_2_F000_reg.tif reg_Cyc_2\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_2/Cycle_2_F000_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_3/Cycle_3_F000_reg.tif reg_Cyc_3\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_3/Cycle_3_F000_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_4/Cycle_4_F000_reg.tif reg_Cyc_4\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_4/Cycle_4_F000_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_5/Cycle_5_F000_reg.tif reg_Cyc_5\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_5/Cycle_5_F000_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_6/Cycle_6_F000_reg.tif reg_Cyc_6\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_6/Cycle_6_F000_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_7/Cycle_7_F000_reg.tif reg_Cyc_7\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_7/Cycle_7_F000_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_8/Cycle_8_F000_reg.tif reg_Cyc_8\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_8/Cycle_8_F000_reg.tif...\n",
      "| [1 files][160.0 MiB/160.0 MiB]                                                \n",
      "Operation completed over 1 objects/160.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_9/Cycle_9_F000_reg.tif reg_Cyc_9\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_9/Cycle_9_F000_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "Appending...  reg_Cyc_1/Cycle_1_F000_reg.tif\n",
      "Shape = (10, 8, 2048, 2048)\n",
      "Appending...  reg_Cyc_2/Cycle_2_F000_reg.tif\n",
      "Shape = (10, 12, 2048, 2048)\n",
      "Appending...  reg_Cyc_3/Cycle_3_F000_reg.tif\n",
      "Shape = (10, 16, 2048, 2048)\n",
      "Appending...  reg_Cyc_4/Cycle_4_F000_reg.tif\n",
      "Shape = (10, 20, 2048, 2048)\n",
      "Appending...  reg_Cyc_5/Cycle_5_F000_reg.tif\n",
      "Shape = (10, 24, 2048, 2048)\n",
      "Appending...  reg_Cyc_6/Cycle_6_F000_reg.tif\n",
      "Shape = (10, 28, 2048, 2048)\n",
      "Appending...  reg_Cyc_7/Cycle_7_F000_reg.tif\n",
      "Shape = (10, 32, 2048, 2048)\n",
      "Appending...  reg_Cyc_8/Cycle_8_F000_reg.tif\n",
      "Shape = (10, 34, 2048, 2048)\n",
      "Appending...  reg_Cyc_9/Cycle_9_F000_reg.tif\n",
      "Shape = (10, 38, 2048, 2048)\n",
      "saving ./merged/F000.tif\n",
      "image shape 2048 2048\n",
      "2028\n",
      "2025\n",
      "gsutil cp merged/F000.tif gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/merged\n",
      "Copying file://merged/F000.tif [Content-Type=image/tiff]...\n",
      "==> NOTE: You are uploading one or more large file(s), which would run          \n",
      "significantly faster if you enable parallel composite uploads. This\n",
      "feature can be enabled by editing the\n",
      "\"parallel_composite_upload_threshold\" value in your .boto\n",
      "configuration file. However, note that if you do this large files will\n",
      "be uploaded as `composite objects\n",
      "<https://cloud.google.com/storage/docs/composite-objects>`_,which\n",
      "means that any user who downloads such objects will need to have a\n",
      "compiled crcmod installed (see \"gsutil help crcmod\"). This is because\n",
      "without a compiled crcmod, computing checksums on composite objects is\n",
      "so slow that gsutil disables downloads of composite objects.\n",
      "\n",
      "- [1 files][  2.9 GiB/  2.9 GiB]   53.9 MiB/s                                   \n",
      "Operation completed over 1 objects/2.9 GiB.                                      \n",
      "##############################################################################\n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/tif/Cycle_0/Cycle_F001.tif tif/Cycle_0\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/tif/Cycle_0/Cycle_F001.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_1/Cycle_1_F001_reg.tif reg_Cyc_1\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_1/Cycle_1_F001_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_2/Cycle_2_F001_reg.tif reg_Cyc_2\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_2/Cycle_2_F001_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_3/Cycle_3_F001_reg.tif reg_Cyc_3\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_3/Cycle_3_F001_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_4/Cycle_4_F001_reg.tif reg_Cyc_4\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_4/Cycle_4_F001_reg.tif...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_5/Cycle_5_F001_reg.tif reg_Cyc_5\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_5/Cycle_5_F001_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_6/Cycle_6_F001_reg.tif reg_Cyc_6\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_6/Cycle_6_F001_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_7/Cycle_7_F001_reg.tif reg_Cyc_7\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_7/Cycle_7_F001_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_8/Cycle_8_F001_reg.tif reg_Cyc_8\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_8/Cycle_8_F001_reg.tif...\n",
      "| [1 files][160.0 MiB/160.0 MiB]                                                \n",
      "Operation completed over 1 objects/160.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_9/Cycle_9_F001_reg.tif reg_Cyc_9\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_9/Cycle_9_F001_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "Appending...  reg_Cyc_1/Cycle_1_F001_reg.tif\n",
      "Shape = (10, 8, 2048, 2048)\n",
      "Appending...  reg_Cyc_2/Cycle_2_F001_reg.tif\n",
      "Shape = (10, 12, 2048, 2048)\n",
      "Appending...  reg_Cyc_3/Cycle_3_F001_reg.tif\n",
      "Shape = (10, 16, 2048, 2048)\n",
      "Appending...  reg_Cyc_4/Cycle_4_F001_reg.tif\n",
      "Shape = (10, 20, 2048, 2048)\n",
      "Appending...  reg_Cyc_5/Cycle_5_F001_reg.tif\n",
      "Shape = (10, 24, 2048, 2048)\n",
      "Appending...  reg_Cyc_6/Cycle_6_F001_reg.tif\n",
      "Shape = (10, 28, 2048, 2048)\n",
      "Appending...  reg_Cyc_7/Cycle_7_F001_reg.tif\n",
      "Shape = (10, 32, 2048, 2048)\n",
      "Appending...  reg_Cyc_8/Cycle_8_F001_reg.tif\n",
      "Shape = (10, 34, 2048, 2048)\n",
      "Appending...  reg_Cyc_9/Cycle_9_F001_reg.tif\n",
      "Shape = (10, 38, 2048, 2048)\n",
      "saving ./merged/F001.tif\n",
      "image shape 2048 2048\n",
      "2028\n",
      "2025\n",
      "gsutil cp merged/F001.tif gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/merged\n",
      "Copying file://merged/F001.tif [Content-Type=image/tiff]...\n",
      "==> NOTE: You are uploading one or more large file(s), which would run          \n",
      "significantly faster if you enable parallel composite uploads. This\n",
      "feature can be enabled by editing the\n",
      "\"parallel_composite_upload_threshold\" value in your .boto\n",
      "configuration file. However, note that if you do this large files will\n",
      "be uploaded as `composite objects\n",
      "<https://cloud.google.com/storage/docs/composite-objects>`_,which\n",
      "means that any user who downloads such objects will need to have a\n",
      "compiled crcmod installed (see \"gsutil help crcmod\"). This is because\n",
      "without a compiled crcmod, computing checksums on composite objects is\n",
      "so slow that gsutil disables downloads of composite objects.\n",
      "\n",
      "\\ [1 files][  2.9 GiB/  2.9 GiB]   66.8 MiB/s                                   \n",
      "Operation completed over 1 objects/2.9 GiB.                                      \n",
      "##############################################################################\n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/tif/Cycle_0/Cycle_F002.tif tif/Cycle_0\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/tif/Cycle_0/Cycle_F002.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_1/Cycle_1_F002_reg.tif reg_Cyc_1\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_1/Cycle_1_F002_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_2/Cycle_2_F002_reg.tif reg_Cyc_2\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_2/Cycle_2_F002_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_3/Cycle_3_F002_reg.tif reg_Cyc_3\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_3/Cycle_3_F002_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_4/Cycle_4_F002_reg.tif reg_Cyc_4\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_4/Cycle_4_F002_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_5/Cycle_5_F002_reg.tif reg_Cyc_5\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_5/Cycle_5_F002_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_6/Cycle_6_F002_reg.tif reg_Cyc_6\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_6/Cycle_6_F002_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_7/Cycle_7_F002_reg.tif reg_Cyc_7\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_7/Cycle_7_F002_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_8/Cycle_8_F002_reg.tif reg_Cyc_8\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_8/Cycle_8_F002_reg.tif...\n",
      "| [1 files][160.0 MiB/160.0 MiB]                                                \n",
      "Operation completed over 1 objects/160.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_9/Cycle_9_F002_reg.tif reg_Cyc_9\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_9/Cycle_9_F002_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "Appending...  reg_Cyc_1/Cycle_1_F002_reg.tif\n",
      "Shape = (10, 8, 2048, 2048)\n",
      "Appending...  reg_Cyc_2/Cycle_2_F002_reg.tif\n",
      "Shape = (10, 12, 2048, 2048)\n",
      "Appending...  reg_Cyc_3/Cycle_3_F002_reg.tif\n",
      "Shape = (10, 16, 2048, 2048)\n",
      "Appending...  reg_Cyc_4/Cycle_4_F002_reg.tif\n",
      "Shape = (10, 20, 2048, 2048)\n",
      "Appending...  reg_Cyc_5/Cycle_5_F002_reg.tif\n",
      "Shape = (10, 24, 2048, 2048)\n",
      "Appending...  reg_Cyc_6/Cycle_6_F002_reg.tif\n",
      "Shape = (10, 28, 2048, 2048)\n",
      "Appending...  reg_Cyc_7/Cycle_7_F002_reg.tif\n",
      "Shape = (10, 32, 2048, 2048)\n",
      "Appending...  reg_Cyc_8/Cycle_8_F002_reg.tif\n",
      "Shape = (10, 34, 2048, 2048)\n",
      "Appending...  reg_Cyc_9/Cycle_9_F002_reg.tif\n",
      "Shape = (10, 38, 2048, 2048)\n",
      "saving ./merged/F002.tif\n",
      "image shape 2048 2048\n",
      "2028\n",
      "2025\n",
      "gsutil cp merged/F002.tif gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/merged\n",
      "Copying file://merged/F002.tif [Content-Type=image/tiff]...\n",
      "==> NOTE: You are uploading one or more large file(s), which would run          \n",
      "significantly faster if you enable parallel composite uploads. This\n",
      "feature can be enabled by editing the\n",
      "\"parallel_composite_upload_threshold\" value in your .boto\n",
      "configuration file. However, note that if you do this large files will\n",
      "be uploaded as `composite objects\n",
      "<https://cloud.google.com/storage/docs/composite-objects>`_,which\n",
      "means that any user who downloads such objects will need to have a\n",
      "compiled crcmod installed (see \"gsutil help crcmod\"). This is because\n",
      "without a compiled crcmod, computing checksums on composite objects is\n",
      "so slow that gsutil disables downloads of composite objects.\n",
      "\n",
      "\\ [1 files][  2.9 GiB/  2.9 GiB]   77.0 MiB/s                                   \n",
      "Operation completed over 1 objects/2.9 GiB.                                      \n",
      "##############################################################################\n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/tif/Cycle_0/Cycle_F003.tif tif/Cycle_0\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/tif/Cycle_0/Cycle_F003.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_1/Cycle_1_F003_reg.tif reg_Cyc_1\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_1/Cycle_1_F003_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_2/Cycle_2_F003_reg.tif reg_Cyc_2\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_2/Cycle_2_F003_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_3/Cycle_3_F003_reg.tif reg_Cyc_3\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_3/Cycle_3_F003_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_4/Cycle_4_F003_reg.tif reg_Cyc_4\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_4/Cycle_4_F003_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_5/Cycle_5_F003_reg.tif reg_Cyc_5\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_5/Cycle_5_F003_reg.tif...\n",
      "\\ [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_6/Cycle_6_F003_reg.tif reg_Cyc_6\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_6/Cycle_6_F003_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_7/Cycle_7_F003_reg.tif reg_Cyc_7\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_7/Cycle_7_F003_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_8/Cycle_8_F003_reg.tif reg_Cyc_8\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_8/Cycle_8_F003_reg.tif...\n",
      "/ [1 files][160.0 MiB/160.0 MiB]                                                \n",
      "Operation completed over 1 objects/160.0 MiB.                                    \n",
      "gsutil cp gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_9/Cycle_9_F003_reg.tif reg_Cyc_9\n",
      "Copying gs://fc-secure-9289bfef-e5cb-493a-83d5-e604cd429e39/Brian/101222_D10_Coverslip1_Processed/reg_Cyc_9/Cycle_9_F003_reg.tif...\n",
      "| [1 files][320.0 MiB/320.0 MiB]                                                \n",
      "Operation completed over 1 objects/320.0 MiB.                                    \n",
      "Appending...  reg_Cyc_1/Cycle_1_F003_reg.tif\n"
     ]
    }
   ],
   "source": [
    "for ii in range(0,len(allFOVs),10): # For each CHUNK (10 FOVs per loop)\n",
    "    print(allFOVs[ii:ii+10])\n",
    "    \n",
    "    # download files \n",
    "    for fov in allFOVs[ii:ii+10]: # for each fov in CHUNK\n",
    "        ref_cmd = 'gsutil cp ' + SOURCE + REF_DIR + f'/Cycle_0/Cycle_F{fov}.tif {REF_DIR}/Cycle_0'\n",
    "        print(ref_cmd)\n",
    "        ! {ref_cmd}\n",
    "        \n",
    "        for cycle in allCycles[1:]: # for all cycle\n",
    "            data_cmd = 'gsutil cp ' + SOURCE + f'{IN_DIR}_Cyc_{cycle}/Cycle_{cycle}_F{fov}_reg.tif '\n",
    "            data_cmd2 = f'{IN_DIR}_Cyc_{cycle}'\n",
    "            print(data_cmd+data_cmd2)\n",
    "            ! {data_cmd+data_cmd2}\n",
    "            \n",
    "            \n",
    "        img = imread(f'{REF_DIR}/Cycle_0/Cycle_F{fov}.tif') # reference cycle 0\n",
    "        img = img.astype(np.uint16)\n",
    "        for cycle in allCycles[1:]:\n",
    "            fname = f'{IN_DIR}_Cyc_{cycle}/Cycle_{cycle}_F{fov}_{IN_DIR}.tif' \n",
    "            print('Appending... ', fname)\n",
    "            im_to_add = imread(fname).astype(np.uint16)\n",
    "            im_to_add = im_to_add[:,:,...]\n",
    "            img = np.append(img, im_to_add, axis=1)\n",
    "            print(f\"Shape = {img.shape}\")\n",
    "            \n",
    "            \n",
    "            \n",
    "        fname = f'/F{fov}.tif'\n",
    "        print('saving', f'./{MERGE_DIR}' + fname)\n",
    "        \n",
    "        ########## CROP ################ --> CHANGE everytime depending on shifts\n",
    "        Xlength, Ylength, img = crop(X_max_total, X_min_total, Y_max_total, Y_min_total, img, pad)\n",
    "    \n",
    "        assert img.shape[3] == Xlength-pad*2, \"Check X size\"\n",
    "        assert img.shape[2] == Ylength-pad*2, \"Check Y size\"\n",
    "    \n",
    "    \n",
    "        ### FINAL CHECK before saving ### \n",
    "        assert img.shape[0] == Z, \"Check ZCYX\"\n",
    "        assert img.shape[1] == final_ch, \"check final merge size\"\n",
    "        #print(img.max())\n",
    "        tifffile.imwrite(\n",
    "            f'./{MERGE_DIR}'+fname, img, imagej=True, photometric='minisblack',metadata={'axes': 'ZCYX'})\n",
    "    \n",
    "        im_to_export = f'./{MERGE_DIR}'+fname\n",
    "    \n",
    "        export = f'gsutil cp ' + MERGE_DIR+fname + ' ' + SOURCE + MERGE_DIR\n",
    "        print(export)\n",
    "        ! {export}\n",
    "    \n",
    "        print('##############################################################################')\n",
    "  \n",
    "    print('!!!!!!!!!!!!!!!!END OF CHUNK!!!!!!!!!!!')\n",
    "    print('Export Complete - remove files')\n",
    "    \n",
    "    rmv = 'rm merged/*' # remove all merged\n",
    "    ! {rmv}\n",
    "    for cycle in allCycles[1:]:\n",
    "        rmv_cyc = f'rm {IN_DIR}_Cyc_{cycle}/*'\n",
    "        ! {rmv_cyc}\n",
    "    rmv_ref = f'rm {REF_DIR}/Cycle_0/*'\n",
    "    ! {rmv_ref}\n",
    "    \n",
    "    if not os.path.exists(f'{MERGE_DIR}'): \n",
    "        os.mkdir(f'{MERGE_DIR}')\n",
    "    # return to next chunk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "65a2140f",
   "metadata": {},
   "outputs": [],
   "source": [
    "! {'rm -r test'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b43ace5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "s"
   ]
  }
 ],
 "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.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
