{
 "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\n",
    "from moncenterlib.gnss.quality_check import Anubis"
   ]
  },
  {
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_nskn = {\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': 'C23050038862',\n",
    "    'antenna_type': 'HXCCGX611A      HXCM',\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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_buzz = {\n",
    "    'format': 'ubx',\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': 'BUZZ',\n",
    "    'marker_number': '00',\n",
    "    'marker_type': 'GEODETIC',\n",
    "    'about_name': '',\n",
    "    'about_agency': 'SSUGT',\n",
    "    'receiver_number': '0000000001',\n",
    "    'receiver_type': 'U-Blox M8N',\n",
    "    'receiver_version': '',\n",
    "    'antenna_number': '00000000000',\n",
    "    'antenna_type': 'ASH111660 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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_snsk00rus = {\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': 'SNSK',\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': '00000000000',\n",
    "    'antenna_type': 'ASH111660 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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_snsk01rus = {\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': 'SNSK',\n",
    "    'marker_number': '01',\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': '00000000000',\n",
    "    'antenna_type': 'TRM55971.00 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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_snsk02rus = {\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': 'SNSK',\n",
    "    'marker_number': '02',\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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "conf_snsk03rus = {\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': 'SNSK',\n",
    "    'marker_number': '03',\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': '0000000000',\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": "markdown",
   "metadata": {},
   "source": [
    "Запускаем процесс конфертации "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_conv = \"2024-08-31\"\n",
    "\n",
    "converter.start(f\"/home/danisimo/Summer_Experiment/data/NSKN/{data_conv}\",\n",
    "                f\"/home/danisimo/Summer_Experiment/data/NSKN/{data_conv}\", conf_nskn)\n",
    "\n",
    "converter.start(f\"/home/danisimo/Summer_Experiment/data/SNSK00RUS/{data_conv}\",\n",
    "                f\"/home/danisimo/Summer_Experiment/data/SNSK00RUS/{data_conv}\", conf_snsk00rus)\n",
    "\n",
    "converter.start(f\"/home/danisimo/Summer_Experiment/data/SNSK01RUS/{data_conv}\",\n",
    "                f\"/home/danisimo/Summer_Experiment/data/SNSK01RUS/{data_conv}\", conf_snsk01rus)\n",
    "\n",
    "converter.start(f\"/home/danisimo/Summer_Experiment/data/SNSK02RUS/{data_conv}\",\n",
    "                f\"/home/danisimo/Summer_Experiment/data/SNSK02RUS/{data_conv}\", conf_snsk02rus)\n",
    "\n",
    "converter.start(f\"/home/danisimo/Summer_Experiment/data/SNSK03RUS/{data_conv}\",\n",
    "                f\"/home/danisimo/Summer_Experiment/data/SNSK03RUS/{data_conv}\", conf_snsk03rus)\n",
    "\n",
    "converter.start(f\"/home/danisimo/Summer_Experiment/data/BUZZ/{data_conv}\",\n",
    "                f\"/home/danisimo/Summer_Experiment/data/BUZZ/{data_conv}\", conf_buzz)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Postprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Kinematic\n",
    "conf_post_kinematic = postproc.get_default_config()\n",
    "conf_post_kinematic[\"pos1-posmode\"] = \"2\"\n",
    "conf_post_kinematic[\"pos1-frequency\"] = \"5\"\n",
    "conf_post_kinematic[\"pos1-soltype\"] = \"0\"\n",
    "conf_post_kinematic[\"pos1-elmask\"] = \"10\"\n",
    "conf_post_kinematic[\"pos1-tidecorr\"] = \"0\"\n",
    "conf_post_kinematic[\"pos1-ionoopt\"] = \"1\"\n",
    "conf_post_kinematic[\"pos1-tropopt\"] = \"1\"\n",
    "conf_post_kinematic[\"pos1-sateph\"] = \"0\"\n",
    "conf_post_kinematic[\"pos1-posopt1\"] = \"1\"\n",
    "conf_post_kinematic[\"pos1-posopt2\"] = \"1\"\n",
    "conf_post_kinematic[\"pos1-posopt3\"] = \"1\"\n",
    "conf_post_kinematic[\"pos1-posopt4\"] = \"1\"\n",
    "conf_post_kinematic[\"pos1-posopt5\"] = \"1\"\n",
    "conf_post_kinematic[\"pos1-posopt6\"] = \"1\"\n",
    "conf_post_kinematic[\"pos1-navsys\"] = \"1\"\n",
    "\n",
    "conf_post_kinematic[\"pos2-armode\"] = \"1\"\n",
    "conf_post_kinematic[\"pos2-gloarmode\"] = \"1\"\n",
    "conf_post_kinematic[\"pos2-bdsarmode\"] = \"1\"\n",
    "\n",
    "conf_post_kinematic[\"out-solformat\"] = \"2\"\n",
    "conf_post_kinematic[\"out-timesys\"] = \"0\"\n",
    "conf_post_kinematic[\"out-timeform\"] = \"1\"\n",
    "conf_post_kinematic[\"out-degform\"] = \"0\"\n",
    "\n",
    "conf_post_kinematic[\"ant1-anttype\"] = \"*\"\n",
    "\n",
    "conf_post_kinematic[\"ant2-postype\"] = \"0\"\n",
    "conf_post_kinematic[\"ant2-anttype\"] = \"*\"\n",
    "conf_post_kinematic[\"ant2-pos1\"] = \"54.98736596111111\"    # NSKN\n",
    "conf_post_kinematic[\"ant2-pos2\"] = \"82.8647646861111\"     # NSKN\n",
    "conf_post_kinematic[\"ant2-pos3\"] = \"107.3126\"             # NSKN\n",
    "\n",
    "conf_post_kinematic[\"file-satantfile\"] = f\"/home/danisimo/Summer_Experiment/data/igs20.atx\"\n",
    "conf_post_kinematic[\"file-rcvantfile\"] = f\"/home/danisimo/Summer_Experiment/data/igs20.atx\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "postproc.get_default_config()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Static\n",
    "conf_post_static = postproc.get_default_config()\n",
    "conf_post_static[\"pos1-posmode\"] = \"3\"\n",
    "conf_post_static[\"pos1-frequency\"] = \"5\"\n",
    "conf_post_static[\"pos1-soltype\"] = \"2\"\n",
    "conf_post_static[\"pos1-elmask\"] = \"10\"\n",
    "conf_post_static[\"pos1-tidecorr\"] = \"0\"\n",
    "conf_post_static[\"pos1-ionoopt\"] = \"1\"\n",
    "conf_post_static[\"pos1-tropopt\"] = \"1\"\n",
    "conf_post_static[\"pos1-sateph\"] = \"0\"\n",
    "conf_post_static[\"pos1-posopt1\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt2\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt3\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt4\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt5\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt6\"] = \"1\"\n",
    "conf_post_static[\"pos1-navsys\"] = \"1\"\n",
    "\n",
    "conf_post_static[\"pos2-armode\"] = \"1\"\n",
    "conf_post_static[\"pos2-gloarmode\"] = \"1\"\n",
    "conf_post_static[\"pos2-bdsarmode\"] = \"1\"\n",
    "\n",
    "conf_post_static[\"out-solformat\"] = \"2\"\n",
    "conf_post_static[\"out-timesys\"] = \"0\"\n",
    "conf_post_static[\"out-timeform\"] = \"1\"\n",
    "conf_post_static[\"out-degform\"] = \"0\"\n",
    "conf_post_static[\"out-outstat\"] = \"2\"\n",
    "\n",
    "\n",
    "conf_post_static[\"ant1-anttype\"] = \"*\"\n",
    "\n",
    "conf_post_static[\"ant2-postype\"] = \"0\"\n",
    "conf_post_static[\"ant2-anttype\"] = \"*\"\n",
    "conf_post_static[\"ant2-pos1\"] = \"54.98736596111111\"    # NSKN\n",
    "conf_post_static[\"ant2-pos2\"] = \"82.8647646861111\"     # NSKN\n",
    "conf_post_static[\"ant2-pos3\"] = \"107.3126\"             # NSKN\n",
    "\n",
    "conf_post_static[\"file-satantfile\"] = f\"/home/danisimo/Summer_Experiment/data/igs20.atx\"\n",
    "conf_post_static[\"file-rcvantfile\"] = f\"/home/danisimo/Summer_Experiment/data/igs20.atx\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "date_post = \"2024-08-31\"\n",
    "interval = 1\n",
    "\n",
    "base_station_path = f\"/home/danisimo/Summer_Experiment/data/NSKN/{date_post}/\"\n",
    "navigation_path = f\"/home/danisimo/Summer_Experiment/data/nav/{date_post}/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Внутренняя сеть"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_snsk00rus = f\"/home/danisimo/Summer_Experiment/data/SNSK00RUS/{date_post}\"\n",
    "input_rnx = {\n",
    "    \"rover\": path_snsk00rus,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx,path_snsk00rus, conf_post_static, interval, time_start=\"2024/08/29 04:00:00\", time_end=\"2024/08/29 13:00:00\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_snsk01rus = f\"/home/danisimo/Summer_Experiment/data/SNSK01RUS/{date_post}\"\n",
    "\n",
    "input_rnx = {\n",
    "    \"rover\": path_snsk01rus,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx, path_snsk01rus, conf_post_kinematic, interval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_snsk02rus = f\"/home/danisimo/Summer_Experiment/data/SNSK02RUS/{date_post}\"\n",
    "input_rnx = {\n",
    "    \"rover\": path_snsk02rus,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx,path_snsk02rus, conf_post_kinematic, interval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_snsk03rus = f\"/home/danisimo/Summer_Experiment/data/SNSK03RUS/{date_post}\"\n",
    "input_rnx = {\n",
    "    \"rover\": path_snsk03rus,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx,path_snsk03rus, conf_post_kinematic, interval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_buzz = f\"/home/danisimo/Summer_Experiment/data/BUZZ/{date_post}\"\n",
    "\n",
    "input_rnx = {\n",
    "    \"rover\": path_buzz,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx, path_buzz, conf_post_kinematic, interval)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Внешняя сеть"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = [\"2024-08-27\", \"2024-08-29\", \"2024-08-30\", \"2024-08-31\"]\n",
    "interval = 1\n",
    "\n",
    "for date_post in dates:\n",
    "    base_station_path = f\"/home/danisimo/Summer_Experiment/data/NSKN/{date_post}/\"\n",
    "    navigation_path = f\"/home/danisimo/Summer_Experiment/data/nav/{date_post}/\"\n",
    "\n",
    "    path_nsk1 = f\"/home/danisimo/Summer_Experiment/data/NSK1/{date_post}\"\n",
    "\n",
    "    input_rnx = {\n",
    "        \"rover\": path_nsk1,\n",
    "        \"base\": base_station_path,\n",
    "        \"nav\": navigation_path\n",
    "    }\n",
    "    postproc.start(input_rnx, path_nsk1, conf_post_static, interval)\n",
    "\n",
    "    path_nvs2 = f\"/home/danisimo/Summer_Experiment/data/NVS2/{date_post}\"\n",
    "\n",
    "    input_rnx = {\n",
    "        \"rover\": path_nvs2,\n",
    "        \"base\": base_station_path,\n",
    "        \"nav\": navigation_path\n",
    "    }\n",
    "    postproc.start(input_rnx, path_nvs2, conf_post_static, interval)\n",
    "\n",
    "    path_novm = f\"/home/danisimo/Summer_Experiment/data/NOVM/{date_post}\"\n",
    "\n",
    "    input_rnx = {\n",
    "        \"rover\": path_novm,\n",
    "        \"base\": base_station_path,\n",
    "        \"nav\": navigation_path\n",
    "    }\n",
    "    postproc.start(input_rnx, path_novm, conf_post_static, interval)\n",
    "\n",
    "    path_nsvb = f\"/home/danisimo/Summer_Experiment/data/NSVB/{date_post}\"\n",
    "\n",
    "    input_rnx = {\n",
    "        \"rover\": path_nsvb,\n",
    "        \"base\": base_station_path,\n",
    "        \"nav\": navigation_path\n",
    "    }\n",
    "    postproc.start(input_rnx, path_nsvb, conf_post_static, interval)\n",
    "\n",
    "    path_nskp = f\"/home/danisimo/Summer_Experiment/data/NSKP/{date_post}\"\n",
    "\n",
    "    input_rnx = {\n",
    "        \"rover\": path_nskp,\n",
    "        \"base\": base_station_path,\n",
    "        \"nav\": navigation_path\n",
    "    }\n",
    "    postproc.start(input_rnx, path_nskp, conf_post_static, interval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_nvs2 = f\"/home/danisimo/Summer_Experiment/data/NVS2/{date_post}\"\n",
    "\n",
    "input_rnx = {\n",
    "    \"rover\": path_nvs2,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx, path_nvs2, conf_post_kinematic, interval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_novm = f\"/home/danisimo/Summer_Experiment/data/NOVM/{date_post}\"\n",
    "\n",
    "input_rnx = {\n",
    "    \"rover\": path_novm,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx, path_novm, conf_post_kinematic, interval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_nsvb = f\"/home/danisimo/Summer_Experiment/data/NSVB/{date_post}\"\n",
    "\n",
    "input_rnx = {\n",
    "    \"rover\": path_nsvb,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx, path_nsvb, conf_post_kinematic, interval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_nskp = f\"/home/danisimo/Summer_Experiment/data/NSKP/{date_post}\"\n",
    "\n",
    "input_rnx = {\n",
    "    \"rover\": path_nskp,\n",
    "    \"base\": base_station_path,\n",
    "    \"nav\": navigation_path\n",
    "}\n",
    "postproc.start(input_rnx, path_nskp, conf_post_kinematic, interval)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Кусочная статика"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Сложный скрипт. Скоро в библиотеку будет внедрен модуль, который облегчик постобработку кусочной статикой"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Конфиг"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Static\n",
    "conf_post_static = postproc.get_default_config()\n",
    "conf_post_static[\"pos1-posmode\"] = \"3\"\n",
    "conf_post_static[\"pos1-frequency\"] = \"5\"\n",
    "conf_post_static[\"pos1-soltype\"] = \"2\"\n",
    "conf_post_static[\"pos1-elmask\"] = \"10\"\n",
    "conf_post_static[\"pos1-tidecorr\"] = \"0\"\n",
    "conf_post_static[\"pos1-ionoopt\"] = \"1\"\n",
    "conf_post_static[\"pos1-tropopt\"] = \"1\"\n",
    "conf_post_static[\"pos1-sateph\"] = \"0\"\n",
    "conf_post_static[\"pos1-posopt1\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt2\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt3\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt4\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt5\"] = \"1\"\n",
    "conf_post_static[\"pos1-posopt6\"] = \"1\"\n",
    "conf_post_static[\"pos1-navsys\"] = \"1\"\n",
    "\n",
    "conf_post_static[\"pos2-armode\"] = \"1\"\n",
    "conf_post_static[\"pos2-gloarmode\"] = \"1\"\n",
    "conf_post_static[\"pos2-bdsarmode\"] = \"1\"\n",
    "\n",
    "conf_post_static[\"out-solformat\"] = \"2\"\n",
    "conf_post_static[\"out-timesys\"] = \"0\"\n",
    "conf_post_static[\"out-timeform\"] = \"1\"\n",
    "conf_post_static[\"out-degform\"] = \"0\"\n",
    "conf_post_static[\"out-outstat\"] = \"2\"\n",
    "\n",
    "\n",
    "conf_post_static[\"ant1-anttype\"] = \"*\"\n",
    "\n",
    "conf_post_static[\"ant2-postype\"] = \"0\"\n",
    "conf_post_static[\"ant2-anttype\"] = \"*\"\n",
    "conf_post_static[\"ant2-pos1\"] = \"54.98736596111111\"    # NSKN\n",
    "conf_post_static[\"ant2-pos2\"] = \"82.8647646861111\"     # NSKN\n",
    "conf_post_static[\"ant2-pos3\"] = \"107.3126\"             # NSKN\n",
    "\n",
    "conf_post_static[\"file-satantfile\"] = f\"/home/danisimo/Summer_Experiment/data/igs20.atx\"\n",
    "conf_post_static[\"file-rcvantfile\"] = f\"/home/danisimo/Summer_Experiment/data/igs20.atx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gps_time import GPSTime\n",
    "import datetime\n",
    "import numpy as np\n",
    "import math\n",
    "from moncenterlib.gnss.gnss_time_series import parse_pos_file\n",
    "def parse_stats_file(filename, date):\n",
    "    gps_week = GPSTime.from_datetime(datetime.datetime.strptime(date, \"%Y/%m/%d %H:%M:%S.%f\"))\n",
    "    with open(filename, mode='r') as f:\n",
    "        lines = f.readlines()\n",
    "        indexs = []\n",
    "        for i, line in enumerate(lines):\n",
    "            if line.startswith(f\"$POS,{gps_week.week_number},{gps_week.time_of_week}\"):\n",
    "                indexs.append(i)\n",
    "        \n",
    "        row_index = indexs[-1] + 1\n",
    "        carrier_phase_residual_list = []\n",
    "        while True:\n",
    "            if row_index >= len(lines) or lines[row_index].startswith(\"$POS\"):\n",
    "                break\n",
    "            \n",
    "            if lines[row_index].startswith(f\"$SAT,{gps_week.week_number},{gps_week.time_of_week}\"):\n",
    "                row_data = lines[row_index].split(\",\")\n",
    "                if row_data[9] == \"1\":\n",
    "                    carrier_phase_residual_list.append(float(row_data[8]))\n",
    "            row_index += 1\n",
    "        \n",
    "\n",
    "        residual_cp_list = np.array(carrier_phase_residual_list)\n",
    "        sigma_0 = round(math.sqrt(residual_cp_list.transpose().dot(residual_cp_list)/(len(residual_cp_list)-4)), 5)\n",
    "        return sigma_0\n",
    "\n",
    "def run_processing(date, station, start_date, iters, window_seconds, conf_post_static):\n",
    "    inputs = {\n",
    "        \"rover\": f\"/home/danisimo/Summer_Experiment/data/{station}/{date}\",\n",
    "        \"base\": f\"/home/danisimo/Summer_Experiment/data/NSKN/{date}\",\n",
    "        \"nav\": f\"/home/danisimo/Summer_Experiment/data/nav/{date}\"\n",
    "    }\n",
    "\n",
    "    temp_sd = start_date\n",
    "    temp_ed = start_date + datetime.timedelta(seconds=window_seconds)\n",
    "    \n",
    "    with open(f\"{inputs['rover']}/piece_sigma.pos\", mode='a') as f:\n",
    "        f.write(\"%  GPST  e-baseline(m)  n-baseline(m)  u-baseline(m)   Q  ns   sde(m)   sdn(m)   sdu(m)  sden(m)  sdnu(m)  sdue(m) age(s)  ratio sigma_0\\n\")\n",
    "    \n",
    "    for _ in range(0, iters):\n",
    "        postproc = RtkLibPost(False)\n",
    "        res = postproc.start(inputs,\n",
    "                            inputs[\"rover\"],\n",
    "                            conf_post_static,\n",
    "                            1,\n",
    "                            time_start=temp_sd.strftime(\"%Y/%m/%d %H:%M:%S\"),\n",
    "                            time_end=temp_ed.strftime(\"%Y/%m/%d %H:%M:%S\"), show_info_rtklib=False)\n",
    "        path = res[\"done\"]\n",
    "        if path != []:\n",
    "            header, data = parse_pos_file(path[0])\n",
    "            df = pd.DataFrame(data, columns=header[\"name_columns\"])\n",
    "            df = df[df[\"Q\"] == \"1\"]\n",
    "            if len(df) != 0:\n",
    "                with open(f\"{inputs['rover']}/piece_sigma.pos\", mode='a') as f:\n",
    "                    row = f'{temp_ed.strftime(\"%Y/%m/%d %H:%M:%S\")}.000 '\n",
    "                    last_row = df.iloc[-1].to_list()\n",
    "                 \n",
    "                    sigma_0 = parse_stats_file(path[0] + \".stat\", last_row[0])\n",
    "                    f.write(row + ' '.join(last_row [1:]) + f\" {sigma_0}\" +\"\\n\")\n",
    "\n",
    "        temp_sd = temp_sd + datetime.timedelta(seconds=window_seconds)\n",
    "        temp_ed = temp_ed + datetime.timedelta(seconds=window_seconds)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "import pandas as pd\n",
    "from threading import Thread\n",
    "\n",
    "stations = [\"NSK1\", \"NVS2\", \"NSKP\", \"NSVB\", \"NOVM\", \"SNSK00RUS\", \"SNSK01RUS\", \"SNSK02RUS\", \"SNSK03RUS\", \"BUZZ\"]\n",
    "dates = [\"2024-08-27\", \"2024-08-29\", \"2024-08-30\", \"2024-08-31\"]\n",
    "\n",
    "\n",
    "window_seconds = 600\n",
    "\n",
    "for date in dates:\n",
    "    start_date = datetime.datetime.strptime(date, \"%Y-%m-%d\")\n",
    "    start_date = start_date.replace(hour=4)\n",
    "    end_date = datetime.datetime.strptime(date, \"%Y-%m-%d\")\n",
    "    end_date = end_date.replace(hour=13)\n",
    "\n",
    "    iters = int(((end_date-start_date).seconds) / window_seconds) + 1\n",
    "\n",
    "    threads = []\n",
    "    for station in stations:\n",
    "        th = Thread(target=run_processing, args=(date, station, start_date, iters, window_seconds, conf_post_static))\n",
    "        threads.append(th)\n",
    "    for th in threads:\n",
    "        th.start()\n",
    "    for th in threads:\n",
    "        th.join()\n",
    "    \n",
    "\n"
   ]
  }
 ],
 "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
}
