{
 "cells": [
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ],
   "id": "746c5b9e4c0226e7",
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "import pickle\n",
    "import numpy as np\n",
    "import winkelumrechnungen as wu\n",
    "from itertools import product\n",
    "import pandas as pd\n",
    "from ellipsoide import EllipsoidTriaxial\n",
    "import plotly.graph_objects as go"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# ellips = \"KarneyTest2024\"\n",
    "ellips = \"BursaSima1980\"\n",
    "# ellips = \"Fiction\"\n",
    "ell: EllipsoidTriaxial = EllipsoidTriaxial.init_name(ellips)"
   ],
   "id": "7b05ca89fcd7b331",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "def deg_range(start, stop, step):\n",
    "    return [float(x) for x in range(start, stop + step, step)]\n",
    "\n",
    "def asymptotic_range(start, direction=\"up\", max_decimals=4):\n",
    "    values = []\n",
    "    for d in range(0, max_decimals + 1):\n",
    "        step = 10 ** -d\n",
    "        if direction == \"up\":\n",
    "            values.append(start + (1 - step))\n",
    "        else:\n",
    "            values.append(start - (1 - step))\n",
    "    return values"
   ],
   "id": "61a6b14fef0180ad",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "beta_5_85 = deg_range(5, 85, 5)\n",
    "lambda_5_85 = deg_range(5, 85, 5)\n",
    "beta_5_90 = deg_range(5, 90, 5)\n",
    "lambda_5_90 = deg_range(5, 90, 5)\n",
    "beta_0_90 = deg_range(0, 90, 5)\n",
    "lambda_0_90 = deg_range(0, 90, 5)\n",
    "beta_90 = [90.0]\n",
    "lambda_90 = [90.0]\n",
    "beta_0 = [0.0]\n",
    "lambda_0 = [0.0]\n",
    "beta_asym_89 = asymptotic_range(89.0, direction=\"up\")\n",
    "lambda_asym_0 = asymptotic_range(1.0, direction=\"down\")"
   ],
   "id": "f7184980a4b930b7",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "groups = {\n",
    "    1: list(product(beta_5_85, lambda_5_85)),\n",
    "    2: list(product(beta_0, lambda_0_90)),\n",
    "    3: list(product(beta_5_85, lambda_0)),\n",
    "    4: list(product(beta_90, lambda_5_90)),\n",
    "    5: list(product(beta_asym_89, lambda_asym_0)),\n",
    "    6: list(product(beta_5_85, lambda_90)),\n",
    "    7: list(product(lambda_asym_0, lambda_0_90)),\n",
    "    8: list(product(beta_0_90, lambda_asym_0)),\n",
    "    9: list(product(beta_asym_89, lambda_0_90)),\n",
    "    10: list(product(beta_0_90, beta_asym_89)),\n",
    "}"
   ],
   "id": "cea9fd9cce6a4fd1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "for nr, points in groups.items():\n",
    "    points_cart = []\n",
    "    for point in points:\n",
    "        beta, lamb = point\n",
    "        cart = ell.ell2cart(wu.deg2rad(beta), wu.deg2rad(lamb))\n",
    "        points_cart.append(cart)\n",
    "    groups[nr] = points_cart"
   ],
   "id": "17a6a130782a89ce",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "results = {}\n",
    "\n",
    "for nr, points in groups.items():\n",
    "    group_results = {\"ell\": [],\n",
    "                     \"para\": [],\n",
    "                     \"geod\": []}\n",
    "    for point in points:\n",
    "        elli = ell.cart2ell(point)\n",
    "        cart_elli = ell.ell2cart(elli[0], elli[1])\n",
    "        group_results[\"ell\"].append(np.linalg.norm(point - cart_elli, axis=-1))\n",
    "\n",
    "        para = ell.cart2para(point)\n",
    "        cart_para = ell.para2cart(para[0], para[1])\n",
    "        group_results[\"para\"].append(np.linalg.norm(point - cart_para, axis=-1))\n",
    "\n",
    "        geod = ell.cart2geod(point, \"ligas3\")\n",
    "        cart_geod = ell.geod2cart(geod[0], geod[1], geod[2])\n",
    "        group_results[\"geod\"].append(np.linalg.norm(point - cart_geod, axis=-1))\n",
    "\n",
    "    group_results[\"ell\"] = np.array(group_results[\"ell\"])\n",
    "    group_results[\"para\"] = np.array(group_results[\"para\"])\n",
    "    group_results[\"geod\"] = np.array(group_results[\"geod\"])\n",
    "    results[nr] = group_results"
   ],
   "id": "c3298ea233bca274",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# with open(f\"conversion_results_{ellips}.pkl\", \"wb\") as f:\n",
    "#     pickle.dump(results, f)"
   ],
   "id": "e1285860be416ad3",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# with open(f\"conversion_results_{ellips}.pkl\", \"rb\") as f:\n",
    "#     results = pickle.load(f)"
   ],
   "id": "d26720e34595ccbc",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame({\n",
    "    \"Gruppe\": [nr for nr in results.keys()],\n",
    "    \"max_Δr_ell\": [f\"{max(result[\"ell\"]):.3g}\" for result in results.values()],\n",
    "    \"max_Δr_para\": [f\"{max(result[\"para\"]):.3g}\" for result in results.values()],\n",
    "    \"max_Δr_geod\": [f\"{max(result[\"geod\"]):.3g}\" for result in results.values()]\n",
    "})"
   ],
   "id": "4e2e55e4699ec81e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "fig = go.Figure(data=[go.Table(\n",
    "    header=dict(\n",
    "        values=list(df.columns),\n",
    "        fill_color=\"lightgrey\",\n",
    "        align=\"left\"\n",
    "    ),\n",
    "    cells=dict(\n",
    "        values=[df[col] for col in df.columns],\n",
    "        align=\"left\"\n",
    "    )\n",
    ")])\n",
    "fig.update_layout(\n",
    "    template=\"simple_white\",\n",
    "    width=650,\n",
    "    height=len(groups)*20+80,\n",
    "    margin=dict(l=20, r=20, t=20, b=20))\n",
    "\n",
    "fig.show()\n",
    "# fig.write_image(f\"conversion_results_{ellips}.png\", width=650, height=len(groups)*20+80, scale=2)"
   ],
   "id": "c2fa82afef2d6e0e",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
