# 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
''' 
Module for converting between hdf5 files, text files and data
dictionaries.
'''
logger = logging.getLogger(__name__)

## Write files from DataDict instances
#####################################################################
def _write_csv(data):
  ''' 
  Write data for a single station to a csv file
  '''
  time = data['time']
  out  = '4-character id, %s\n' % data['id']
  out += 'begin date, %s\n' % mjd_inv(time[0],'%Y-%m-%d')
  out += 'end date, %s\n' % mjd_inv(time[-1],'%Y-%m-%d')
  out += 'longitude, %s E\n' % data['longitude']
  out += 'latitude, %s N\n' % data['latitude']
  out += ('units, meters**%s days**%s\n' % 
          (data['space_exponent'],data['time_exponent']))
  out += ('date, north, east, vertical, north std. deviation, '
          'east std. deviation, vertical std. deviation\n')
  # convert displacements and uncertainties to strings
  for i in range(len(data['time'])):

# 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

''' 
Module for converting between hdf5 files, text files and data
dictionaries.
'''
logger = logging.getLogger(__name__)

## Write files from DataDict instances
#####################################################################
def _write_csv(data):
  ''' 
  Write data for a single station to a csv file
  '''
  time = data['time']
  out  = '4-character id, %s\n' % data['id']
  out += 'begin date, %s\n' % mjd_inv(time[0],'%Y-%m-%d')
  out += 'end date, %s\n' % mjd_inv(time[-1],'%Y-%m-%d')
  out += 'longitude, %s E\n' % data['longitude']
  out += 'latitude, %s N\n' % data['latitude']
  out += ('units, meters**%s days**%s\n' % 
          (data['space_exponent'],data['time_exponent']))
  out += ('date, north, east, vertical, north std. deviation, '
          'east std. deviation, vertical std. deviation\n')
  # convert displacements and uncertainties to strings
  for i in range(len(data['time'])):

    date_str = mjd_inv(time[i],'%Y-%m-%d')