class Configuration(object):
    """Base class for the configuration

    can parse configuration files with 'param = value' syntax. Read params
    become the instance attributes.
    """

    def __init__(self, defaults):
        """"""
        self.settings = None
        self.set_defaults(defaults)

        self.cfg_file = None

        # рег. выражения
        self._re_q = re.compile(r"['\"](.*)['\"]")
        self._re_range = re.compile(r'\((\S+),\s*(\S+)\)')
        self._re_bool = re.compile(r'True', re.I)

        # проверка вхождения в диапазон
        self._in_r = lambda v, r: (r[0] <= v <= r[1]) or False

    def _get_bool(self, bstr):
        """_get_bool(str) -> True|False
        """

        if self._re_bool.match(bstr):
            return True
        else:
            return False

    def _get_str(self, in_str):
        """_get_str(str) -> str
        """

        if self._re_q.match(in_str):
            # pylint: disable=eval-used
            return eval('r' + in_str)
        else:
            return in_str

    def _get_path(self, in_str):
        """ _get_path(str) -> str
        """

        in_str = re.sub(r'\\', r'/', in_str)
        return self._get_str(in_str)

    # def _set_range(self, attr, a_range):
    #     """_set_range(attr, range) -> None
    #
    #     converts str('(str, str)') into tuple(float, float)
    #     """
    #     match = self._re_range.match(a_range)
    #
    #     if not match:
    #         raise CfgValueError(self.cfg_file, attr, a_range)
    #
    #     try:
    #         lft = float(match.group(1))
    #         rgt = float(match.group(2))
    #         setattr(self, attr, (lft, rgt))
    #     except ValueError:
    #         raise CfgValueError(self.cfg_file, attr, a_range)

    def set_defaults(self, defaults):
        """set_defaults(defaults)
        """
        self.settings = defaults.copy()
        for (name, val) in list(self.settings.items()):
            setattr(self, name, val)

    def read_cfg(self, filename):
        """read_cfg(filename)
        """
        self.cfg_file = filename

        try:
            fh = open(filename, 'r')
        except IOError as err:
            err_msg = "Can't open configuration file {}. {}\n"
            err_msg = err_msg.format(filename, str(err))
            raise CfgError(err_msg)

        for line in fh:

            line = line.rstrip()
            line = re.sub(r'#.*', '', line)

            if not len(line):
                continue

            (name, val) = re.split('=', line, 2)

            # убираем пробелы
            name = re.sub(r'\s+', '', name)

            val = re.sub(r'^\s+', '', val)
            val = re.sub(r'\s+$', '', val)

            if len(val):
                if name in self.settings:
                    setattr(self, name, val)
                    self.settings[name] = val
                else:
                    warn = 'Ignoring unknown parameter: <%s>\n' % name
                    sys.stderr.write(warn)
            else:
                warn = 'Ignoring empty value: %s = <%s>\n' % (name, val)
                sys.stderr.write(warn)

        fh.close() def sec2sec_ms(sec):
    """sec2sec_ms(sec) -> sec, microsec

    Parameters
    ----------
    sec : float

    Returns
    -------
    seconds : int
    microsec : int
    """
    microsec = (sec - int(sec)) * 1e+6
    microsec = float("%.1f" % microsec)

    return int(sec), int(microsec) def get_dir_list(line, delimiter=','):
    """get_dir_list(line, delimiter) -> list

    Parameters
    ----------
    line : str
    delimiter : str
        default ','
    """
    dlist = line.split(delimiter)

    dlist = [f.rstrip() for f in dlist]
    dlist = [f.lstrip() for f in dlist]

    return dlist R_DATETIME = 'datetime' R_ELEVATION = 'el' R_AZIMUTH = 'az' R_TEC_L1L2 = 'tec.l1l2' R_ALL = 'all' R_VALIDITY = 'validity' OUT_FILE_TEXT = 'text' class Rec(object):
    """ Base class for the output format record.

    Order corresponds to the output record; date/time value will be added later.
    """

    def __init__(self):
        self.formatDef = {
            label.R_TSN: {
                'format': '{0:11d}',
                'type': lambda v: int(v),
                'fortran': 'I11'
            },
            label.R_HOUR: {
                'format': '{1:14.11f}',
                'type': lambda v: float(v),
                'fortran': 'F14.11'
            },
            label.R_DATETIME: {
                'format': '{2}',
                'type': None,
                'fortran': None  # will set it later
            },
            label.R_ELEVATION: {
                'format': '{3: 10.5f}',
                'type': lambda v: float(v),
                'fortran': 'F10.5'
            },
            label.R_AZIMUTH: {
                'format': '{4: 11.5f}',
                'type': lambda v: float(v),
                'fortran': 'F11.5'
            },
            label.R_P1: {
                'format': '{5: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_P1_LLI: {
                'format': '{6:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_P2: {
                'format': '{7: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_P2_LLI: {
                'format': '{8:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_TEC_P1P2: {
                'format': '{9: 10.3f}',
                'type': lambda v: float(v),
                'fortran': 'F10.3'
            },
            label.R_L1: {
                'format': '{10: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_L1_LLI: {
                'format': '{11:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_L2: {
                'format': '{12: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_L2_LLI: {
                'format': '{13:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_TEC_L1L2: {
                'format': '{14: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
            label.R_VALIDITY: {
                'format': '{15:7d}',
                'type': lambda v: int(v),
                'fortran': 'I7'
            },
            label.R_S1: {
                'format': '{16: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_S1_LLI: {
                'format': '{17:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_S2: {
                'format': '{18: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_S2_LLI: {
                'format': '{19:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_S5: {
                'format': '{20: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_S5_LLI: {
                'format': '{21:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_C1: {
                'format': '{22: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_C1_LLI: {
                'format': '{23:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_C2: {
                'format': '{24: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_C2_LLI: {
                'format': '{25:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_TEC_C1P2: {
                'format': '{26: 10.3f}',
                'type': lambda v: float(v),
                'fortran': 'F10.3'
            },
            label.R_TEC_L1C1: {
                'format': '{27: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
            label.R_L5: {
                'format': '{28: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_L5_LLI: {
                'format': '{29:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_TEC_L1L5: {
                'format': '{30: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
            label.R_C5: {
                'format': '{31: 16.3f}',
                'type': lambda v: float(v),
                'fortran': 'F16.3'
            },
            label.R_C5_LLI: {
                'format': '{32:1d}',
                'type': lambda v: int(v),
                'fortran': 'I1'
            },
            label.R_TEC_C1C5: {
                'format': '{33: 10.3f}',
                'type': lambda v: float(v),
                'fortran': 'F10.3'
            },
            label.R_TEC_L2L5: {
                'format': '{34: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
            label.R_TEC_C1C2: {
                'format': '{35: 10.3f}',
                'type': lambda v: float(v),
                'fortran': 'F10.3'
            },
            label.R_TEC_C2C5: {
                'format': '{36: 10.3f}',
                'type': lambda v: float(v),
                'fortran': 'F10.3'
            },
            label.R_SAT_X: {
                'format': '{37: 23.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_SAT_Y: {
                'format': '{38: 23.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_SAT_Z: {
                'format': '{39: 23.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_SITE_X: {
                'format': '{40: 23.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_SITE_Y: {
                'format': '{41: 23.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_SITE_Z: {
                'format': '{42: 23.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_SITE_L: {
                'format': '{43: 17.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_SITE_B: {
                'format': '{44: 16.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_SITE_H: {
                'format': '{45: 18.12f}',
                'type': lambda v: float(v),
                'fortran': 'F23.12'
            },
            label.R_TEC_L2L6: {
                'format': '{46: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
            label.R_TEC_L2L7: {
                'format': '{47: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
            label.R_TEC_L6L7: {
                'format': '{48: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
            label.R_TEC_C2C6: {
                'format': '{49: 10.3f}',
                'type': lambda v: float(v),
                'fortran': 'F10.3'
            },
            label.R_TEC_C2C7: {
                'format': '{50: 10.3f}',
                'type': lambda v: float(v),
                'fortran': 'F10.3'
            },
            label.R_TEC_C6C7: {
                'format': '{51: 10.3f}',
                'type': lambda v: float(v),
                'fortran': 'F10.3'
            },
            label.R_TEC_L2C2: {
                'format': '{52: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
            label.R_TEC_L8C8: {
                'format': '{53: 21.3f}',
                'type': lambda v: float(v),
                'fortran': 'F21.3'
            },
        } SAT_SYS_GPS = 'G' SAT_SYS_GLO = 'R' SAT_SYS_GEO = 'S'  # Geostationary signal payload SAT_SYS_BDS = 'C'  # Compass/BeiDou SAT_SYS_GAL = 'E'  # Galileo
import datetime
import logging
import os
import os.path
import re
from tecs.config import Configuration
from tecs.rinex.common import sec2sec_ms
from tecs.rinex.futils import get_dir_list
from tecs.label import R_DATETIME, R_ELEVATION, R_AZIMUTH, R_TEC_L1L2, R_ALL
from tecs.label import R_VALIDITY, OUT_FILE_TEXT
from tecs.rec import Rec
from tecs.rinex.label import SAT_SYS_GPS, SAT_SYS_GLO, SAT_SYS_GEO, \
    SAT_SYS_BDS, SAT_SYS_GAL
#!/usr/bin/evn python
# coding=utf-8
#
# Copyright 2017 Ilya Zhivetiev <i.zhivetiev@gnss-lab.org>
#
# This file is part of tec-suite.
#
# tec-suite is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# tec-suite is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with tec-suite.  If not, see <http://www.gnu.org/licenses/>.

"""
config.py: configuration of the tecs
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals




NAME = 'tecs.gtb.config'
LOGGER = logging.getLogger(NAME)

# Defaults
DRF = '{}, {}, {}, {}, {}'

DRF = DRF.format(R_DATETIME, R_ELEVATION, R_AZIMUTH, R_TEC_L1L2, R_VALIDITY)