{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0e99383d",
   "metadata": {},
   "source": [
    "# Add extensions\n",
    "\n",
    "This notebook adds one satellite account row to the packaged IOT test table using an Excel extension workbook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63bba208",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:09.972371Z",
     "iopub.status.busy": "2026-04-26T22:41:09.971952Z",
     "iopub.status.idle": "2026-04-26T22:41:12.212314Z",
     "shell.execute_reply": "2026-04-26T22:41:12.211934Z"
    }
   },
   "outputs": [],
   "source": [
    "import mario"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0ef3658c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:12.215232Z",
     "iopub.status.busy": "2026-04-26T22:41:12.214928Z",
     "iopub.status.idle": "2026-04-26T22:41:12.321267Z",
     "shell.execute_reply": "2026-04-26T22:41:12.320947Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Parser: excel reading IOT flows from /Users/lorenzorinaldi/Documents/GitHub/MARIO/mario/test/tables/test_IOT_standard.xlsx.\n",
      "INFO Parser: state payload ready with 6 canonical blocks.\n",
      "INFO Parser: excel state ready for IOT.\n",
      "INFO Metadata: initialized.\n"
     ]
    }
   ],
   "source": [
    "db = mario.load_test(\"IOT\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d15bebc",
   "metadata": {},
   "source": [
    "## Generate an empty extension template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d869079a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:12.323289Z",
     "iopub.status.busy": "2026-04-26T22:41:12.323173Z",
     "iopub.status.idle": "2026-04-26T22:41:12.333470Z",
     "shell.execute_reply": "2026-04-26T22:41:12.333201Z"
    }
   },
   "outputs": [],
   "source": [
    "db.get_extensions_excel(\n",
    "    path=\"/path/to/add_extensions_template.xlsx\",\n",
    "    matrix=\"E\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "756ca62f",
   "metadata": {},
   "source": [
    "## Prepare the unit for the new row\n",
    "\n",
    "`add_extensions` reads the numeric row from Excel and receives the unit table separately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b74131ef",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:12.335240Z",
     "iopub.status.busy": "2026-04-26T22:41:12.335111Z",
     "iopub.status.idle": "2026-04-26T22:41:12.340055Z",
     "shell.execute_reply": "2026-04-26T22:41:12.339822Z"
    }
   },
   "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>unit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>CH4</th>\n",
       "      <td>kg</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    unit\n",
       "CH4   kg"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_units = db.units[\"Satellite account\"].iloc[:1].copy()\n",
    "new_units.index = [\"CH4\"]\n",
    "new_units.loc[\"CH4\", \"unit\"] = \"kg\"\n",
    "new_units"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45a6533e",
   "metadata": {},
   "source": [
    "### Download the packaged example workbooks\n",
    "\n",
    "The exact workbooks used in this example are available here:\n",
    "\n",
    "- [Empty extension template](../../_static/data/supporting_files/add_extensions_template.xlsx)\n",
    "- [Filled extension workbook](../../_static/data/supporting_files/add_extensions_filled.xlsx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b0e9b0a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:12.341581Z",
     "iopub.status.busy": "2026-04-26T22:41:12.341475Z",
     "iopub.status.idle": "2026-04-26T22:41:12.632980Z",
     "shell.execute_reply": "2026-04-26T22:41:12.632683Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving v for baseline.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: trying v via formula build_iot_v_from_V_X.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolved v via formula build_iot_v_from_V_X.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING Using add extensions will rewrite the new results on the baseline and delete other scenarios\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING baseline deleted from the database\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving z for baseline.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: trying z via formula build_iot_z_from_Z_X.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolved z via formula build_iot_z_from_Z_X.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving e for baseline.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: trying e via formula build_iot_e_from_E_X.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolved e via formula build_iot_e_from_E_X.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['Employment', 'CO2', 'CH4']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "extended_db = db.add_extensions(\n",
    "    io=\"/path/to/add_extensions_filled.xlsx\",\n",
    "    matrix=\"E\",\n",
    "    units=new_units,\n",
    "    inplace=False,\n",
    ")\n",
    "extended_db.get_index(\"Satellite account\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e758bdd3",
   "metadata": {},
   "source": [
    "## Inspect the new extension row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4ca9741e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:12.634675Z",
     "iopub.status.busy": "2026-04-26T22:41:12.634552Z",
     "iopub.status.idle": "2026-04-26T22:41:12.662481Z",
     "shell.execute_reply": "2026-04-26T22:41:12.662211Z"
    }
   },
   "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 tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>Region</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg1</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Level</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Item</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>CH4</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Region        Reg1                          Reg2                  \n",
       "Level       Sector                        Sector                  \n",
       "Item   Agriculture Industry Services Agriculture Industry Services\n",
       "CH4            1.0      2.0      3.0         4.0      5.0      6.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "extended_db.E.loc[[\"CH4\"]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mario",
   "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.13.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
