{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from moncenterlib.gnss.tools4rnx import RtkLibConvbin\n",
    "from moncenterlib.gnss.postprocessing import RtkLibPost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "converter = RtkLibConvbin()\n",
    "postproc = RtkLibPost()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Convert raw data to RINEX format"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create configuration for every stations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_netr5 = {\n",
    "    'format': 'rtcm3',\n",
    "    'rinex_v': '3.04',\n",
    "    'start_time': '',\n",
    "    'end_time': '',\n",
    "    'interval': '0',\n",
    "    'freq': '32',\n",
    "    'system': 'G,R,E,J,S,C,I',\n",
    "    'output_o': '1',\n",
    "    'output_n': '0',\n",
    "    'output_g': '0',\n",
    "    'output_h': '0',\n",
    "    'output_q': '0',\n",
    "    'output_l': '0',\n",
    "    'output_b': '0',\n",
    "    'output_i': '0',\n",
    "    'output_s': '0',\n",
    "    'other_od': '1',\n",
    "    'other_os': '1',\n",
    "    'other_oi': '1',\n",
    "    'other_ot': '1',\n",
    "    'other_ol': '1',\n",
    "    'other_halfc': '1',\n",
    "    'comment': '',\n",
    "    'marker_name': 'NSKN',\n",
    "    'marker_number': '00',\n",
    "    'marker_type': 'GEODETIC',\n",
    "    'about_name': '',\n",
    "    'about_agency': 'SSUGT',\n",
    "    'receiver_number': '4733K10360',\n",
    "    'receiver_type': 'TRIMBLE NET R5',\n",
    "    'receiver_version': '4.41',\n",
    "    'antenna_number': '000000000',\n",
    "    'antenna_type': 'HXCCSX601A NONE',\n",
    "    'approx_position_x': '',\n",
    "    'approx_position_y': '',\n",
    "    'approx_position_z': '',\n",
    "    'antenna_delta_h': '',\n",
    "    'antenna_delta_e': '',\n",
    "    'antenna_delta_n': ''\n",
    " }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_zedf9p = {\n",
    "    'format': 'ubx',\n",
    "    'rinex_v': '3.04',\n",
    "    'start_time': '',\n",
    "    'end_time': '',\n",
    "    'interval': '1',\n",
    "    'freq': '32',\n",
    "    'system': 'G,R,E,J,S,C,I',\n",
    "    'output_o': '1',\n",
    "    'output_n': '0',\n",
    "    'output_g': '0',\n",
    "    'output_h': '0',\n",
    "    'output_q': '0',\n",
    "    'output_l': '0',\n",
    "    'output_b': '0',\n",
    "    'output_i': '0',\n",
    "    'output_s': '0',\n",
    "    'other_od': '1',\n",
    "    'other_os': '1',\n",
    "    'other_oi': '1',\n",
    "    'other_ot': '1',\n",
    "    'other_ol': '1',\n",
    "    'other_halfc': '1',\n",
    "    'comment': '',\n",
    "    'marker_name': 'NSKN',\n",
    "    'marker_number': '00',\n",
    "    'marker_type': 'GEODETIC',\n",
    "    'about_name': '',\n",
    "    'about_agency': 'SSUGT',\n",
    "    'receiver_number': '0000000001',\n",
    "    'receiver_type': 'U-Blox ZED-F9P-01B-01',\n",
    "    'receiver_version': '',\n",
    "    'antenna_number': '000000000',\n",
    "    'antenna_type': 'HXCCSX601A NONE',\n",
    "    'approx_position_x': '',\n",
    "    'approx_position_y': '',\n",
    "    'approx_position_z': '',\n",
    "    'antenna_delta_h': '',\n",
    "    'antenna_delta_e': '',\n",
    "    'antenna_delta_n': ''\n",
    " }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_simple = {\n",
    "    'format': 'ubx',\n",
    "    'rinex_v': '3.04',\n",
    "    'start_time': '',\n",
    "    'end_time': '',\n",
    "    'interval': '1',\n",
    "    'freq': '32',\n",
    "    'system': 'G,R,E,J,S,C,I',\n",
    "    'output_o': '1',\n",
    "    'output_n': '0',\n",
    "    'output_g': '0',\n",
    "    'output_h': '0',\n",
    "    'output_q': '0',\n",
    "    'output_l': '0',\n",
    "    'output_b': '0',\n",
    "    'output_i': '0',\n",
    "    'output_s': '0',\n",
    "    'other_od': '1',\n",
    "    'other_os': '1',\n",
    "    'other_oi': '1',\n",
    "    'other_ot': '1',\n",
    "    'other_ol': '1',\n",
    "    'other_halfc': '1',\n",
    "    'comment': '',\n",
    "    'marker_name': 'NSKN',\n",
    "    'marker_number': '00',\n",
    "    'marker_type': 'GEODETIC',\n",
    "    'about_name': '',\n",
    "    'about_agency': 'SSUGT',\n",
    "    'receiver_number': '0000000001',\n",
    "    'receiver_type': 'Simple ST901P-L1105',\n",
    "    'receiver_version': '',\n",
    "    'antenna_number': '000000000',\n",
    "    'antenna_type': 'HXCCSX601A NONE',\n",
    "    'approx_position_x': '',\n",
    "    'approx_position_y': '',\n",
    "    'approx_position_z': '',\n",
    "    'antenna_delta_h': '',\n",
    "    'antenna_delta_e': '',\n",
    "    'antenna_delta_n': ''\n",
    " }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_nav = {\n",
    "    'format': 'rtcm3',\n",
    "    'rinex_v': '3.04',\n",
    "    'start_time': '',\n",
    "    'end_time': '',\n",
    "    'interval': '1',\n",
    "    'freq': '32',\n",
    "    'system': 'G,R,E,J,S,C,I',\n",
    "    'output_o': '0',\n",
    "    'output_n': '1',\n",
    "    'output_g': '0',\n",
    "    'output_h': '0',\n",
    "    'output_q': '0',\n",
    "    'output_l': '0',\n",
    "    'output_b': '0',\n",
    "    'output_i': '0',\n",
    "    'output_s': '0',\n",
    "    'other_od': '1',\n",
    "    'other_os': '1',\n",
    "    'other_oi': '1',\n",
    "    'other_ot': '1',\n",
    "    'other_ol': '1',\n",
    "    'other_halfc': '1',\n",
    "    'comment': '',\n",
    "    'marker_name': '',\n",
    "    'marker_number': '',\n",
    "    'marker_type': '',\n",
    "    'about_name': '',\n",
    "    'about_agency': '',\n",
    "    'receiver_number': '',\n",
    "    'receiver_type': '',\n",
    "    'receiver_version': '',\n",
    "    'antenna_number': '',\n",
    "    'antenna_type': '',\n",
    "    'approx_position_x': '',\n",
    "    'approx_position_y': '',\n",
    "    'approx_position_z': '',\n",
    "    'antenna_delta_h': '',\n",
    "    'antenna_delta_e': '',\n",
    "    'antenna_delta_n': ''\n",
    " }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# converter.start(f\"/home/danisimo/Summer_Experiment/Obs/NETR5/obs.log\",\n",
    "#                 f\"/home/danisimo/Summer_Experiment/Obs/NETR5/\", conf_netr5)\n",
    "# converter.start(f\"/home/danisimo/Summer_Experiment/Obs/NETR5/nav.log\",\n",
    "#                 f\"/home/danisimo/Summer_Experiment/Obs/NETR5/\", conf_nav)\n",
    "\n",
    "# converter.start(f\"/home/danisimo/Summer_Experiment/Obs/Simple/obs.log\",\n",
    "#                 f\"/home/danisimo/Summer_Experiment/Obs/Simple/\", conf_simple)\n",
    "# converter.start(f\"/home/danisimo/Summer_Experiment/Obs/Simple/nav.log\",\n",
    "#                 f\"/home/danisimo/Summer_Experiment/Obs/Simple/\", conf_nav)\n",
    "\n",
    "# converter.start(f\"/home/danisimo/Summer_Experiment/Research_simple/NETR5_R_2024323075156_01D_01S_MO.log\",\n",
    "#                 f\"/home/danisimo/Summer_Experiment/Research_simple/\", conf_netr5)\n",
    "# converter.start(f\"/home/danisimo/Summer_Experiment/Research_simple/NAV_R_2024323075157_01D_01S_MO.log\",\n",
    "#                 f\"/home/danisimo/Summer_Experiment/Research_simple/\", conf_nav)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from moncenterlib.gnss.rgs_client import RGSClient\n",
    "rgs = RGSClient(\"qdZsTWSBRlTXvgZRsyesXgst5l09iUJUnt2jGa7bEXwLa3RwXqfaI6Qa079O\", False)\n",
    "query = {\n",
    "    \"working_center\": \"NSK1\",\n",
    "    \"date\": \"2024-11-19\",\n",
    "    \"type\": \"O\"\n",
    "}\n",
    "\n",
    "output_path = \"/home/danisimo/Summer_Experiment/Research_simple/\"\n",
    "rgs.download_files(output_path, query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from moncenterlib.gnss.cddis_client import CDDISClient\n",
    "\n",
    "cdddis = CDDISClient()\n",
    "query = {\n",
    "    \"dates\": {\"start\": \"2024-11-19\", \"end\": \"2024-11-19\"},\n",
    "    \"station\": \"NOVM\",\n",
    "    \"type\": \"o\",\n",
    "    \"rinex_v\": \"auto\"\n",
    "         }\n",
    "cdddis.get_daily_30s_data(\"./Base/NOVM/\", query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from moncenterlib.gnss.gnss_time_series import parse_pos_file\n",
    "import pandas as pd\n",
    "import math\n",
    "import numpy as np\n",
    "from pprint import pprint\n",
    "from moncenterlib.gnss.quality_check import Anubis\n",
    "\n",
    "def create_df(path):\n",
    "    header, data = parse_pos_file(path)\n",
    "    df = pd.DataFrame(data, columns=header[\"name_columns\"])\n",
    "    df[\"GPST\"] = pd.to_datetime(df[\"GPST\"], format=\"%Y/%m/%d %H:%M:%S.%f\")\n",
    "    df.set_index('GPST', inplace=True)\n",
    "    df['e-baseline(m)'] = df['e-baseline(m)'].astype(float)\n",
    "    df['n-baseline(m)'] = df['n-baseline(m)'].astype(float)\n",
    "    df['u-baseline(m)'] = df['u-baseline(m)'].astype(float)\n",
    "    df['Q'] = df['Q'].astype(int)\n",
    "    df['ns'] = df['ns'].astype(int)\n",
    "    df['sde(m)'] = df['sde(m)'].astype(float)\n",
    "    df['sdn(m)'] = df['sdn(m)'].astype(float)\n",
    "    df['sdu(m)'] = df['sdu(m)'].astype(float)\n",
    "    df['sden(m)'] = df['sden(m)'].astype(float)\n",
    "    df['sdnu(m)'] = df['sdnu(m)'].astype(float)\n",
    "    df['sdue(m)'] = df['sdue(m)'].astype(float)\n",
    "    df['age(s)'] = df['age(s)'].astype(float)\n",
    "    df['ratio'] = df['ratio'].astype(float)\n",
    "\n",
    "    return df\n",
    "\n",
    "def calc_kinematic(df: pd.DataFrame, ref: list):\n",
    "    print(\"++++++++++\")\n",
    "    print(\"KINEMATIC\")\n",
    "    df_q1 = df[df[\"Q\"] == 1]\n",
    "\n",
    "    STD_E = round(df_q1[\"e-baseline(m)\"].std(), 4)\n",
    "    STD_N = round(df_q1[\"n-baseline(m)\"].std(), 4)\n",
    "    STD_U = round(df_q1[\"u-baseline(m)\"].std(), 4)\n",
    "\n",
    "    coord_means = df_q1[[\"e-baseline(m)\", \"n-baseline(m)\", \"u-baseline(m)\"]].mean().to_list()\n",
    "    coord_means = list(map(lambda x: round(x, 4), coord_means))\n",
    "    print(f\"Coordinates E: {coord_means[0]} N: {coord_means[1]} U: {coord_means[2]}\")\n",
    "\n",
    "    print(f\"STD E: {STD_E} N: {STD_N} U: {STD_U}\")\n",
    "\n",
    "def calc_RMSE(df: pd.DataFrame, ref: list, is_kinematic: bool):\n",
    "    if is_kinematic:\n",
    "        df_q1 = df[df[\"Q\"] == 1]\n",
    "    else:\n",
    "        df_q1 = df\n",
    "    RMSE_E = round(math.sqrt(((df_q1[\"e-baseline(m)\"] - ref[0]) ** 2).sum() / (len(df_q1))), 4)\n",
    "    RMSE_N = round(math.sqrt(((df_q1[\"n-baseline(m)\"] - ref[1]) ** 2).sum() / len(df_q1)), 4)\n",
    "    RMSE_U = round(math.sqrt(((df_q1[\"u-baseline(m)\"] - ref[2]) ** 2).sum() / len(df_q1)), 4)\n",
    "    print(f\"RMSE E: {RMSE_E} N: {RMSE_N} U: {RMSE_U}\")\n",
    "\n",
    "def calc_last_obs(df: pd.DataFrame):\n",
    "    print(\"++++++++++\")\n",
    "    print(\"STATIC\")\n",
    "    coords = df.iloc[-1][0:3].to_list()\n",
    "    errors = df.iloc[-1][5:8].to_list()\n",
    "    print(f\"Coordinates E: {coords[0]} N: {coords[1]} U: {coords[2]}\")\n",
    "    print(f\"STD E: {errors[0]} N: {errors[1]} U: {errors[2]}\")\n",
    "\n",
    "def calc_fix(df: pd.DataFrame):\n",
    "    per = int((len(df[df[\"Q\"] == 1]) * 100) / len(df))\n",
    "    print(f\"FIX {per}%\")\n",
    "\n",
    "def calc_MAE(df: pd.DataFrame, ref: list, is_kinematic: bool):\n",
    "    if is_kinematic:\n",
    "        df_q1 = df[df[\"Q\"] == 1]\n",
    "    else:\n",
    "        df_q1 = df\n",
    "    MAE_E = round(((df_q1[\"e-baseline(m)\"] - ref[0]).abs()).mean(), 4)\n",
    "    MAE_N = round(((df_q1[\"n-baseline(m)\"] - ref[1]).abs()).mean(), 4)\n",
    "    MAE_U = round(((df_q1[\"u-baseline(m)\"] - ref[2]).abs()).mean(), 4)\n",
    "    print(f\"MAE E: {MAE_E}, N: {MAE_N}, U: {MAE_U}\")\n",
    "\n",
    "def calc(path_static, path_kinematic, name_rover, name_base, ref):\n",
    "    print(\"==========\")\n",
    "    \n",
    "    print(f\"Rover: {name_rover}, Base: {name_base}\")\n",
    "    df_static = create_df(path_static)\n",
    "    calc_last_obs(df_static)\n",
    "    calc_RMSE(df_static, ref, False)\n",
    "    calc_MAE(df_static, ref, False)\n",
    "    calc_fix(df_static)\n",
    "\n",
    "    df_kinematic = create_df(path_kinematic)\n",
    "    calc_kinematic(df_kinematic, ref)\n",
    "    calc_RMSE(df_kinematic, ref, True)\n",
    "    calc_MAE(df_kinematic, ref, True)\n",
    "    calc_fix(df_kinematic)\n",
    "\n",
    "    print(\"==========\\n\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Base NSK1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_netr5_ref = create_df(\"./Ref/pos/NETR5_NSK1.pos\")\n",
    "coords_netr5_ref = df_netr5_ref.iloc[-1][0:3].to_list()\n",
    "std_netr5_ref = df_netr5_ref.iloc[-1][5:8].to_list()\n",
    "offset_H = 0.041\n",
    "coords_netr5_ref[2] -= offset_H\n",
    "coords_netr5_ref"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calc(\"./Simple/pos_NSK1/obs_static.pos\", \"./Simple/pos_NSK1/obs_kinematic.pos\", \"Simple\", \"NSK1\", coords_netr5_ref)\n",
    "calc(\"./ZEDF9P/pos_NSK1/obs_static.pos\", \"./ZEDF9P/pos_NSK1/obs_kinematic.pos\", \"ZedF9P\", \"NSK1\", coords_netr5_ref)\n",
    "calc(\"./NETR5/pos_NSK1/obs_static.pos\", \"./NETR5/pos_NSK1/obs_kinematic.pos\", \"NETR5\", \"NSK1\", coords_netr5_ref)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Base NVS2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_netr5_ref = create_df(\"./Ref/pos/NETR5_NVS2.pos\")\n",
    "coords_netr5_ref = df_netr5_ref.iloc[-1][0:3].to_list()\n",
    "std_netr5_ref = df_netr5_ref.iloc[-1][5:8].to_list()\n",
    "offset_H = 0.043\n",
    "coords_netr5_ref[2] -= offset_H\n",
    "coords_netr5_ref"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calc(\"./Simple/pos_NVS2/obs_static.pos\", \"./Simple/pos_NVS2/obs_kinematic.pos\", \"Simple\", \"NVS2\", coords_netr5_ref)\n",
    "calc(\"./ZEDF9P/pos_NVS2/obs_static.pos\", \"./ZEDF9P/pos_NVS2/obs_kinematic.pos\", \"ZedF9P\", \"NVS2\", coords_netr5_ref)\n",
    "calc(\"./NETR5/pos_NVS2/obs_static.pos\", \"./NETR5/pos_NVS2/obs_kinematic.pos\", \"NETR5\", \"NVS2\", coords_netr5_ref)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Base NOVM\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_netr5_ref = create_df(\"./Ref/pos/NETR5_NOVM.pos\")\n",
    "coords_netr5_ref = df_netr5_ref.iloc[-1][0:3].to_list()\n",
    "std_netr5_ref = df_netr5_ref.iloc[-1][5:8].to_list()\n",
    "offset_H = 0.0458\n",
    "coords_netr5_ref[2] -= offset_H\n",
    "coords_netr5_ref"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calc(\"./Simple/pos_NOVM/obs_static.pos\", \"./Simple/pos_NOVM/obs_kinematic.pos\", \"Simple\", \"NOVM\", coords_netr5_ref)\n",
    "calc(\"./ZEDF9P/pos_NOVM/obs_static.pos\", \"./ZEDF9P/pos_NOVM/obs_kinematic.pos\", \"ZedF9P\", \"NOVM\", coords_netr5_ref)\n",
    "calc(\"./NETR5/pos_NOVM/obs_static.pos\", \"./NETR5/pos_NOVM/obs_kinematic.pos\", \"NETR5\", \"NOVM\", coords_netr5_ref)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
