# Path: pygeons/mjd.py
# @_memoize
# def mjd_inv(m,fmt):
#   ''' 
#   Converts Modified Julian Date (MJD) to a date string 
#   
#   Parameters
#   ----------
#   m : int
#     Modified Julian Date
# 
#   fmt : string
#     format string indicating how to form *out*
#       
#   Returns
#   -------
#   out : str
#     Date string
#     
#   '''
#   m = int(m)
#   d = _REFERENCE_DATETIME + timedelta(m)
#   out = d.strftime(fmt)
#   return out
# 
# Path: pygeons/io/datacheck.py
# def check_data(data):
#   ''' 
#   Runs all data consistency check
#   '''
#   logger.debug('Checking data consistency ... ')
#   check_entries(data)
#   check_shapes(data)
#   check_positive_uncertainties(data)
#   check_missing_data(data)
#   check_unique_stations(data)
#   check_unique_dates(data)
#   logger.debug('OK')
# 
# Path: pygeons/io/parser.py
# PARSER_DICT = {'csv':parse_csv,
#                'pbocsv':parse_pbocsv,
#                'tdecsv':parse_tdecsv,
#                'pbopos':parse_pbopos}


import numpy as np
import logging
import h5py
from pygeons.mjd import mjd_inv
from pygeons.io.datacheck import check_data
from pygeons.io.parser import PARSER_DICT
  for d in dicts:
    if np.min(d['time']) < start_time:
      start_time = np.min(d['time'])
    if np.max(d['time']) > stop_time:
      stop_time = np.max(d['time'])

  # form an array of times ranging from the time of the earliest 
  # observation to the time of the latest observation. count by days
  out = {}
  out['time_exponent'] = dicts[0]['time_exponent']
  out['space_exponent'] = dicts[0]['space_exponent']
  out['time'] = np.arange(int(start_time),int(stop_time)+1,1)
  out['longitude'] = np.array([d['longitude'] for d in dicts])
  out['latitude'] = np.array([d['latitude'] for d in dicts])
  out['id'] = np.array([d['id'] for d in dicts])
  Nt,Nx = len(out['time']),len(out['id'])
  # make a lookup table associating times with indices
  time_dict = dict(zip(out['time'],range(Nt)))
  for key in ['east','north','vertical']:
    # initiate the data arrays with nans or infs. then fill in the 
    # elements where there is
    out[key] = np.empty((Nt,Nx))
    out[key + '_std_dev'] = np.empty((Nt,Nx))
    out[key][:,:] = np.nan
    out[key + '_std_dev'][:,:] = np.inf 
    for i,d in enumerate(dicts):
      idx = [time_dict[t] for t in d['time']]
      out[key][idx,i] = d[key]
      out[key + '_std_dev'][idx,i] = d[key + '_std_dev']


# Path: pygeons/mjd.py
# @_memoize
# def mjd_inv(m,fmt):
#   ''' 
#   Converts Modified Julian Date (MJD) to a date string 
#   
#   Parameters
#   ----------
#   m : int
#     Modified Julian Date
# 
#   fmt : string
#     format string indicating how to form *out*
#       
#   Returns
#   -------
#   out : str
#     Date string
#     
#   '''
#   m = int(m)
#   d = _REFERENCE_DATETIME + timedelta(m)
#   out = d.strftime(fmt)
#   return out
# 
# Path: pygeons/io/datacheck.py
# def check_data(data):
#   ''' 
#   Runs all data consistency check
#   '''
#   logger.debug('Checking data consistency ... ')
#   check_entries(data)
#   check_shapes(data)
#   check_positive_uncertainties(data)
#   check_missing_data(data)
#   check_unique_stations(data)
#   check_unique_dates(data)
#   logger.debug('OK')
# 
# Path: pygeons/io/parser.py
# PARSER_DICT = {'csv':parse_csv,
#                'pbocsv':parse_pbocsv,
#                'tdecsv':parse_tdecsv,
#                'pbopos':parse_pbopos}


# Path: pygeons/io/convert.py
import numpy as np
import logging
import h5py
from pygeons.mjd import mjd_inv
from pygeons.io.datacheck import check_data
from pygeons.io.parser import PARSER_DICT

  for d in dicts:
    if np.min(d['time']) < start_time:
      start_time = np.min(d['time'])
    if np.max(d['time']) > stop_time:
      stop_time = np.max(d['time'])

  # form an array of times ranging from the time of the earliest 
  # observation to the time of the latest observation. count by days
  out = {}
  out['time_exponent'] = dicts[0]['time_exponent']
  out['space_exponent'] = dicts[0]['space_exponent']
  out['time'] = np.arange(int(start_time),int(stop_time)+1,1)
  out['longitude'] = np.array([d['longitude'] for d in dicts])
  out['latitude'] = np.array([d['latitude'] for d in dicts])
  out['id'] = np.array([d['id'] for d in dicts])
  Nt,Nx = len(out['time']),len(out['id'])
  # make a lookup table associating times with indices
  time_dict = dict(zip(out['time'],range(Nt)))
  for key in ['east','north','vertical']:
    # initiate the data arrays with nans or infs. then fill in the 
    # elements where there is
    out[key] = np.empty((Nt,Nx))
    out[key + '_std_dev'] = np.empty((Nt,Nx))
    out[key][:,:] = np.nan
    out[key + '_std_dev'][:,:] = np.inf 
    for i,d in enumerate(dicts):
      idx = [time_dict[t] for t in d['time']]
      out[key][idx,i] = d[key]
      out[key + '_std_dev'][idx,i] = d[key + '_std_dev']


  check_data(out)