#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 21 10:52:21 2017

@author: Neo

Retrieve the estimates of positions of global stations and the formal
uncertainties of these estimates from .sta file which is generated by
the program getpar.

"""

from astropy import units as u
from astropy.time import Time
from astropy.table import Table, Column
import numpy as np
import sys


__all__ = ["calc_sess_epoch_from_name", "read_rms"]


# ------------------------------  FUNCTIONS  ---------------------------
def calc_sess_epoch_from_name(sess_id):
    """Calculate the epoch from session identifiers.
    """

    # Mapping table between letter and number for month
    mon_str2num = {
        "JAN": "01",
        "FEB": "02",
        "MAR": "03",
        "APR": "04",
        "MAY": "05",
        "JUN": "06",
        "JUL": "07",
        "AUG": "08",
        "SEP": "09",
        "OCT": "10",
        "NOV": "11",
        "DEC": "12"
    }

    year = int(sess_id[1:3])
    mon_str = sess_id[3:6]
    date = sess_id[6:8]

    if year >= 79:
        year += 1900
    else:
        year += 2000

    epoch = Time(
        "{}-{}-{}".format(year, mon_str2num[mon_str], date), format="iso")

    return epoch.jyear


def read_rms(rms_file):
    """Retrieve the result from .rms file.

    Parameters
    ----------
    rms_file : string
        name of data file

    Returns
    ----------
    dbname : array, string
       database name with leading dollar sign
    obsnum : array, int
        number of observations used
    epo : array, float
        epoch
    wrmsd : array, float
        overall wrms of postfit delay residuals, psec
    wrmsr : array, float
        overall wrms of postfit delay rate residuals, fsec/s
    """

    rms_table = Table.read(rms_file,
                           format="ascii.fixed_width_no_header",
                           data_start=3,
                           col_starts=[10, 22, 31, 48],
                           col_ends=[20, 28, 42, 58],
                           names=["db_name", "obs_num", "delay_rms", "delay_rate_rms"])

    # Add unit information
    rms_table["delay_rms"].unit = u.second / 1e12
    rms_table["delay_rate_rms"].unit = 1 / 1e15

    # Calculate the epoch
    epo = [calc_sess_epoch_from_name(db) for db in rms_table["db_name"]]

    epo_col = Column(np.array(epo) << u.yr, name="ses_epo")
    rms_table.add_column(epo_col, index=0)
    rms_table.sort(["ses_epo"])

    return rms_table


def get_stats_from_rms(rms_file):
    """Get main statistics from .rms file

    Parameters
    ----------
    rms_file : string
        name of data file

    Returns
    ----------
    sol_stats : dict
        -num_sess : number of sessions used in the solutio
        -num_obs : number of observations (delays) used in the solution
        -postfit_rms: Postfit RMS
        -chi_ndg: Chi per numder of degrees of freedom
    """

    sol_stats = {}

    with open(rms_file, "r") as f_rms:
        lines = f_rms.readlines()

        # Number of sessions used in the solution
        sol_stats["num_sess"] = len(lines) - 3

        data = lines[1].split()

        # Number of delays used in the solution
        sol_stats["num_obs"] = int(data[2]+data[3])

        # Postfit RMS
        sol_stats["postfit_rms"] = float(data[4])

        # Chi per numder of degrees of freedom
        sol_stats["chi_ndg"] = float(data[8])

    return sol_stats


def main():
    print("Nothing to do :-)")


if __name__ == "__main__":
    main()
# ------------------------------ END -----------------------------------
