{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "os.listdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "now = int(datetime.now().timestamp())\n",
    "oml = int(datetime.now().timestamp()) + 60 # one minute later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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>start_unix</th>\n",
       "      <th>node_id</th>\n",
       "      <th>dphase</th>\n",
       "      <th>dura</th>\n",
       "      <th>signal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>grrgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>1</td>\n",
       "      <td>32</td>\n",
       "      <td>gGGgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>gyygrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>grrgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>grrgGGGgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>grrgyyygrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>grrgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>7</td>\n",
       "      <td>25</td>\n",
       "      <td>grrgrrrgGGgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>8</td>\n",
       "      <td>4</td>\n",
       "      <td>grrgrrrgyygrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>grrgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>10</td>\n",
       "      <td>29</td>\n",
       "      <td>grrgrrrgrrgGGG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>grrgrrrgrrgyyy</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    start_unix node_id  dphase  dura          signal\n",
       "0   1704384400      i0       0     1  grrgrrrgrrgrrr\n",
       "1   1704384400      i0       1    32  gGGgrrrgrrgrrr\n",
       "2   1704384400      i0       2     4  gyygrrrgrrgrrr\n",
       "3   1704384400      i0       3     1  grrgrrrgrrgrrr\n",
       "4   1704384400      i0       4    34  grrgGGGgrrgrrr\n",
       "5   1704384400      i0       5     4  grrgyyygrrgrrr\n",
       "6   1704384400      i0       6     1  grrgrrrgrrgrrr\n",
       "7   1704384400      i0       7    25  grrgrrrgGGgrrr\n",
       "8   1704384400      i0       8     4  grrgrrrgyygrrr\n",
       "9   1704384400      i0       9     1  grrgrrrgrrgrrr\n",
       "10  1704384400      i0      10    29  grrgrrrgrrgGGG\n",
       "11  1704384400      i0      11     4  grrgrrrgrrgyyy"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_cycle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "now = int(datetime.now().timestamp()) // 10 * 10\n",
    "oml = now + 60 # one minute later\n",
    "one_cycle = pd.read_csv('one_cycle.csv')\n",
    "one_cycle['start_unix'] = oml\n",
    "cycle = one_cycle.dura.sum()\n",
    "# display(one_cycle)\n",
    "cur_time = one_cycle.start_unix.unique()[0]\n",
    "sev_cycle = [] # several cycles\n",
    "for i in range(5):\n",
    "    cur_cycle = one_cycle.copy()\n",
    "    cur_time = cur_time + cycle\n",
    "    cur_cycle['start_unix'] = cur_time\n",
    "    sev_cycle.append(cur_cycle)\n",
    "sev_cycle = pd.concat(sev_cycle).reset_index(drop=True)\n",
    "sev_cycle.to_csv(\"sev_cycle.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "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>start_unix</th>\n",
       "      <th>node_id</th>\n",
       "      <th>dphase</th>\n",
       "      <th>dura</th>\n",
       "      <th>signal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>grrgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>1</td>\n",
       "      <td>32</td>\n",
       "      <td>gGGgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>gyygrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>grrgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>grrgGGGgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>grrgyyygrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>grrgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>7</td>\n",
       "      <td>25</td>\n",
       "      <td>grrgrrrgGGgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>8</td>\n",
       "      <td>4</td>\n",
       "      <td>grrgrrrgyygrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>grrgrrrgrrgrrr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>10</td>\n",
       "      <td>29</td>\n",
       "      <td>grrgrrrgrrgGGG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1704384400</td>\n",
       "      <td>i0</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>grrgrrrgrrgyyy</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    start_unix node_id  dphase  dura          signal\n",
       "0   1704384400      i0       0     1  grrgrrrgrrgrrr\n",
       "1   1704384400      i0       1    32  gGGgrrrgrrgrrr\n",
       "2   1704384400      i0       2     4  gyygrrrgrrgrrr\n",
       "3   1704384400      i0       3     1  grrgrrrgrrgrrr\n",
       "4   1704384400      i0       4    34  grrgGGGgrrgrrr\n",
       "5   1704384400      i0       5     4  grrgyyygrrgrrr\n",
       "6   1704384400      i0       6     1  grrgrrrgrrgrrr\n",
       "7   1704384400      i0       7    25  grrgrrrgGGgrrr\n",
       "8   1704384400      i0       8     4  grrgrrrgyygrrr\n",
       "9   1704384400      i0       9     1  grrgrrrgrrgrrr\n",
       "10  1704384400      i0      10    29  grrgrrrgrrgGGG\n",
       "11  1704384400      i0      11     4  grrgrrrgrrgyyy"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from xml.etree import ElementTree as ET\n",
    "\n",
    "one_cycle = pd.read_csv('one_cycle.csv')\n",
    "display(one_cycle)\n",
    "\n",
    "net_xml_path = 'example.net.xml'\n",
    "tree = ET.parse(net_xml_path)\n",
    "root = tree.getroot()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Function to create a traffic light phase element\n",
    "def create_phase_element(duration, state):\n",
    "    phase_elem = ET.Element(\"phase\")\n",
    "    phase_elem.set(\"duration\", str(duration))\n",
    "    phase_elem.set(\"state\", state)\n",
    "    return phase_elem\n",
    "\n",
    "# Create the traffic light logic container\n",
    "tlLogic = ET.Element(\"tlLogic\")\n",
    "tlLogic.set(\"id\", \"i0\")\n",
    "tlLogic.set(\"type\", \"static\")\n",
    "tlLogic.set(\"programID\", \"1\")\n",
    "tlLogic.set(\"offset\", \"0\")\n",
    "\n",
    "# Add phases from the CSV data to the traffic light logic\n",
    "for index, row in one_cycle.iterrows():\n",
    "    phase_elem = create_phase_element(row['dura'], row['signal'])\n",
    "    tlLogic.append(phase_elem)\n",
    "\n",
    "# Add the traffic light logic to the junction element if not already present\n",
    "# First, check if a tlLogic is already defined for junction i0 and remove it\n",
    "for tl in root.findall(\"./tlLogic[@id='i0']\"):\n",
    "    root.remove(tl)\n",
    "\n",
    "# Now, insert the new traffic light logic\n",
    "root.insert(0, tlLogic)\n",
    "\n",
    "# Write the modified XML back to a new file\n",
    "modified_net_xml_path = 'modified_example.net.xml'\n",
    "tree.write(modified_net_xml_path)\n",
    "\n",
    "modified_net_xml_path  # Return the path of the modified net.xml file for download"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rts",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
