# Path: thrifty/tdoa_est.py
# SPEED_OF_LIGHT = 2.997e8
# MAX_TDOA = 30e3 / SPEED_OF_LIGHT
# TDOA_DTYPE = {'names': ('rx0', 'rx1', 'tdoa', 'snr', 'model_quality',
#                         'det0_idx', 'det1_idx'),
#               'formats': ('i4', 'i4', 'f8', 'f8', 'f8', 'i4', 'i4')}
# MATRIX_DTYPE = {'names': ('group_id', 'timestamp', 'tx') + TDOA_DTYPE['names'],
#                 'formats': ('i4', 'f8', 'i4') + TDOA_DTYPE['formats']}
# def make_detection_extractor(detections, matches):
#     def extract(rxid0, rxid1, timestamp_start, timestamp_stop):
# def estimate_model_quality(model, detection_pairs):
# def build_model_poly(detection_pairs, beacon_sdoa, nominal_sample_rate, deg=2):
#     def evaluate(det0, det1):
# def build_model_weighted_poly(detection_pairs, beacon_sdoa,
#                               nominal_sample_rate, deg=2):
#     def evaluate(det0, det1):
# def find_nearest_value(list_, value):
# def test_find_nearest_value():
# def build_model_nearest(detection_pairs, beacon_sdoa, nominal_sample_rate):
#     def evaluate(det0, det1):
# def build_model_linear(detection_pairs, beacon_sdoa, nominal_sample_rate):
#     def evaluate(det0, det1):
# def _dist(vector1, vector2):
# def estimate_tdoas(detections, matches, window_size,
#                    beacon_pos, rx_pos, sample_rate,
#                    model_builder=default_model, model_params=None):
#     def _beacon_tdoa(rxid0, rxid1, beaconid):
# def save_tdoa_groups(output, tdoa_groups):
# def load_tdoa_matrix(fname):
# def groups_to_matrix(groups):
# def load_tdoa_groups(fname):
# def load_pos_config(file_):
# def _main():


import scipy.optimize
import numpy as np
import itertools
    import argparse
from thrifty import tdoa_est


def load_positions(fname):
    num_fields = len(np.genfromtxt(fname, max_rows=1))  # FIXME
    dtype = {
        'names': POSITION_INFO_DTYPE['names'][:num_fields],
        'formats': POSITION_INFO_DTYPE['formats'][:num_fields]
    }
    data = np.genfromtxt(fname, dtype=dtype)
    return data


def _main():

    formatter = argparse.RawDescriptionHelpFormatter
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=formatter)

    parser.add_argument('tdoa', nargs='?',
                        type=argparse.FileType('r'), default='data.tdoa',
                        help="tdoa data (\"-\" streams from stdin)")
    parser.add_argument('-o', '--output', dest='output',
                        type=argparse.FileType('w'), default='data.pos',
                        help="output file (\'-\' for stdout)")
    parser.add_argument('-r', '--rx-coordinates', dest='rx_pos',
                        type=argparse.FileType('r'), default='pos-rx.cfg',
                        help="path to config file that contains the "
                             "coordinates of the receivers")
    args = parser.parse_args()


# Path: thrifty/tdoa_est.py
# SPEED_OF_LIGHT = 2.997e8
# MAX_TDOA = 30e3 / SPEED_OF_LIGHT
# TDOA_DTYPE = {'names': ('rx0', 'rx1', 'tdoa', 'snr', 'model_quality',
#                         'det0_idx', 'det1_idx'),
#               'formats': ('i4', 'i4', 'f8', 'f8', 'f8', 'i4', 'i4')}
# MATRIX_DTYPE = {'names': ('group_id', 'timestamp', 'tx') + TDOA_DTYPE['names'],
#                 'formats': ('i4', 'f8', 'i4') + TDOA_DTYPE['formats']}
# def make_detection_extractor(detections, matches):
#     def extract(rxid0, rxid1, timestamp_start, timestamp_stop):
# def estimate_model_quality(model, detection_pairs):
# def build_model_poly(detection_pairs, beacon_sdoa, nominal_sample_rate, deg=2):
#     def evaluate(det0, det1):
# def build_model_weighted_poly(detection_pairs, beacon_sdoa,
#                               nominal_sample_rate, deg=2):
#     def evaluate(det0, det1):
# def find_nearest_value(list_, value):
# def test_find_nearest_value():
# def build_model_nearest(detection_pairs, beacon_sdoa, nominal_sample_rate):
#     def evaluate(det0, det1):
# def build_model_linear(detection_pairs, beacon_sdoa, nominal_sample_rate):
#     def evaluate(det0, det1):
# def _dist(vector1, vector2):
# def estimate_tdoas(detections, matches, window_size,
#                    beacon_pos, rx_pos, sample_rate,
#                    model_builder=default_model, model_params=None):
#     def _beacon_tdoa(rxid0, rxid1, beaconid):
# def save_tdoa_groups(output, tdoa_groups):
# def load_tdoa_matrix(fname):
# def groups_to_matrix(groups):
# def load_tdoa_groups(fname):
# def load_pos_config(file_):
# def _main():


# Path: thrifty/pos_est.py
import scipy.optimize
import numpy as np
import itertools
    import argparse
from thrifty import tdoa_est



def load_positions(fname):
    num_fields = len(np.genfromtxt(fname, max_rows=1))  # FIXME
    dtype = {
        'names': POSITION_INFO_DTYPE['names'][:num_fields],
        'formats': POSITION_INFO_DTYPE['formats'][:num_fields]
    }
    data = np.genfromtxt(fname, dtype=dtype)
    return data


def _main():

    formatter = argparse.RawDescriptionHelpFormatter
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=formatter)

    parser.add_argument('tdoa', nargs='?',
                        type=argparse.FileType('r'), default='data.tdoa',
                        help="tdoa data (\"-\" streams from stdin)")
    parser.add_argument('-o', '--output', dest='output',
                        type=argparse.FileType('w'), default='data.pos',
                        help="output file (\'-\' for stdout)")
    parser.add_argument('-r', '--rx-coordinates', dest='rx_pos',
                        type=argparse.FileType('r'), default='pos-rx.cfg',
                        help="path to config file that contains the "
                             "coordinates of the receivers")
    args = parser.parse_args()


    tdoa_groups = tdoa_est.load_tdoa_groups(args.tdoa)