{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tlLogics>\n",
      "    <tlLogic id=\"i0\" type=\"static\" programID=\"i0\" offset=\"999\">\n",
      "      <phase duration=\"1\" state=\"grrgrrrgrrgrrr\"/>\n",
      "      <phase duration=\"32\" state=\"gGGgrrrgrrgrrr\"/>\n",
      "      <phase duration=\"4\" state=\"gyygrrrgrrgrrr\"/>\n",
      "      <phase duration=\"1\" state=\"grrgrrrgrrgrrr\"/>\n",
      "      <phase duration=\"34\" state=\"grrgGGGgrrgrrr\"/>\n",
      "      <phase duration=\"4\" state=\"grrgyyygrrgrrr\"/>\n",
      "      <phase duration=\"1\" state=\"grrgrrrgrrgrrr\"/>\n",
      "      <phase duration=\"25\" state=\"grrgrrrgGGgrrr\"/>\n",
      "      <phase duration=\"4\" state=\"grrgrrrgyygrrr\"/>\n",
      "      <phase duration=\"1\" state=\"grrgrrrgrrgrrr\"/>\n",
      "      <phase duration=\"29\" state=\"grrgrrrgrrgGGG\"/>\n",
      "      <phase duration=\"4\" state=\"grrgrrrgrrgyyy\"/>\n",
      "    </tlLogic>\n",
      "</tlLogics>\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "path = 'one_cycle.csv'\n",
    "df = pd.read_csv(path)\n",
    "\n",
    "# 적당히 지정하세요\n",
    "node_id = df['node_id'][0]\n",
    "offset = 999\n",
    "\n",
    "# xml양식대로 작성\n",
    "strings = ['<tlLogics>\\n']\n",
    "strings.append(f'    <tlLogic id=\"{node_id}\" type=\"static\" programID=\"{node_id}\" offset=\"{offset}\">\\n')\n",
    "for i, row in df.iterrows():\n",
    "    dur = row['dura']\n",
    "    state = row['signal']\n",
    "    strings.append(f'      <phase duration=\"{dur}\" state=\"{state}\"/>\\n')\n",
    "strings.append('    </tlLogic>\\n')\n",
    "strings.append('</tlLogics>')\n",
    "strings = ''.join(strings)\n",
    "print(strings)\n",
    "\n",
    "# 저장\n",
    "path_output = 'example.tll.xml'\n",
    "with open(path_output, 'w') as f:\n",
    "    f.write(strings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# netedit -s example.net.xml -i example.tll.xml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "sumo_config_content = \"\"\"\n",
    "<configuration>\n",
    "    <input>\n",
    "        <net-file value=\"example.net.xml\"/>\n",
    "        <additional-files value=\"test.tll.xml\"/>\n",
    "        <!-- <route-files value=\"your_routes.rou.xml\"/> -->\n",
    "    </input>\n",
    "    <time>\n",
    "        <begin value=\"0\"/>\n",
    "        <end value=\"3600\"/>\n",
    "    </time>\n",
    "</configuration>\n",
    "\"\"\"\n",
    "\n",
    "with open('your_configuration_file.sumocfg', 'w') as file:\n",
    "    file.write(sumo_config_content.strip())\n"
   ]
  }
 ],
 "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
}
