# Path: smtk/parsers/base_database_parser.py
# def get_float(xval):
#     """
#     Returns a float value, or none
#     """
#     if xval.strip():
#         try:
#             return float(xval)
#         except:
#             return None
#     else:
#         return None
# 
# def get_int(xval):
#     """
#     Returns an int value or none
#     """
#     if xval.strip():
#         try:
#             return int(xval)
#         except:
#             return None
#     else:
#         return None
# 
# class SMDatabaseReader(with_metaclass(abc.ABCMeta)):
#     """
#     Abstract base class for strong motion database parser
#     """
# 
#     def __init__(self, db_id, db_name, filename, record_folder=None):
#         """
#         Instantiate and conduct folder checks
#         """
#         self.id = db_id
#         self.name = db_name
#         self.filename = filename
#         self.database = None
#         if record_folder:
#             self.record_folder = record_folder
#         else:
#             self.record_folder = self.filename
# 
#     @abc.abstractmethod
#     def parse(self):
#         """
#         Parses the database
#         """
# 
# class SMTimeSeriesReader(with_metaclass(abc.ABCMeta)):
#     """
#     Abstract base class for a reader of a ground motion time series
#     """
#     def __init__(self, input_files, folder_name=None, units="cm/s/s"):
#         """
#         Instantiate and conduct folder checks
#         """
#         self.input_files = []
#         for fname in input_files:
#             if folder_name:
#                 filename = os.path.join(folder_name, fname)
#                 if os.path.exists(filename):
#                     self.input_files.append(filename)
#             else:
#                 if os.path.exists(fname):
#                     self.input_files.append(fname)
#         self.time_step = None
#         self.number_steps = None
#         self.units = units
#         self.metadata = None
# 
#     @abc.abstractmethod
#     def parse_records(self, record=None):
#         """
#         Parse the strong motion record
#         """
# 
# class SMSpectraReader(with_metaclass(abc.ABCMeta)):
#     """
#     Abstract Base Class for a reader of a ground motion spectra record
#     """
#     def __init__(self, input_files, folder_name=None):
#         """
#         Intantiate with basic file checks
#         """
#         self.input_files = []
#         for fname in input_files:
#             if folder_name:
#                 filename = os.path.join(folder_name, fname)
#                 if os.path.exists(filename):
#                     self.input_files.append(filename)
#             else:
#                 if os.path.exists(fname):
#                     self.input_files.append(fname)
# 
#     @abc.abstractmethod
#     def parse_spectra(self):
#         """
#         Parses the spectra
#         """


import os
import csv
import numpy as np
import h5py
from collections import OrderedDict
from datetime import datetime
from smtk.sm_database import *
from smtk.parsers.base_database_parser import (get_float, get_int,
                                               SMDatabaseReader,
                                               SMTimeSeriesReader,
                                               SMSpectraReader)
        """
        Parses the distance data
        """
        return RecordDistance(get_float(metadata['distance_repi']),
                              get_float(metadata['distance_rhyp']),
                              rjb = get_float(metadata['distance_rjb']),
                              rrup = get_float(metadata['distance_rrup']),
                              r_x = None,
                              flag = get_int(metadata['distance_flag']))
                        
                        
    def _parse_site_data(self, metadata):
        """
        Parse the site data
        """
        site = RecordSite(
            metadata['station.oid'],
            metadata['station.name'],
            metadata['station.code'],
            get_float(metadata['station.longitude']),
            get_float(metadata['station.latitude']),
            get_float(metadata['station.altitude']),
            vs30=get_float(metadata['station.vs30']),
            vs30_measured=get_int(metadata['station.vs30_measured']),
            network_code=metadata['station.agency.name'],
            country=metadata['station.country.name'])
        site.vs30_measured_type = metadata['station.vs30_measured_type']
        site.instrument_type = metadata['recording_type']
        site.digitiser = metadata['digitalizer']
        site.building_structure = metadata['station.building_struct.name']

# Path: smtk/parsers/base_database_parser.py
# def get_float(xval):
#     """
#     Returns a float value, or none
#     """
#     if xval.strip():
#         try:
#             return float(xval)
#         except:
#             return None
#     else:
#         return None
# 
# def get_int(xval):
#     """
#     Returns an int value or none
#     """
#     if xval.strip():
#         try:
#             return int(xval)
#         except:
#             return None
#     else:
#         return None
# 
# class SMDatabaseReader(with_metaclass(abc.ABCMeta)):
#     """
#     Abstract base class for strong motion database parser
#     """
# 
#     def __init__(self, db_id, db_name, filename, record_folder=None):
#         """
#         Instantiate and conduct folder checks
#         """
#         self.id = db_id
#         self.name = db_name
#         self.filename = filename
#         self.database = None
#         if record_folder:
#             self.record_folder = record_folder
#         else:
#             self.record_folder = self.filename
# 
#     @abc.abstractmethod
#     def parse(self):
#         """
#         Parses the database
#         """
# 
# class SMTimeSeriesReader(with_metaclass(abc.ABCMeta)):
#     """
#     Abstract base class for a reader of a ground motion time series
#     """
#     def __init__(self, input_files, folder_name=None, units="cm/s/s"):
#         """
#         Instantiate and conduct folder checks
#         """
#         self.input_files = []
#         for fname in input_files:
#             if folder_name:
#                 filename = os.path.join(folder_name, fname)
#                 if os.path.exists(filename):
#                     self.input_files.append(filename)
#             else:
#                 if os.path.exists(fname):
#                     self.input_files.append(fname)
#         self.time_step = None
#         self.number_steps = None
#         self.units = units
#         self.metadata = None
# 
#     @abc.abstractmethod
#     def parse_records(self, record=None):
#         """
#         Parse the strong motion record
#         """
# 
# class SMSpectraReader(with_metaclass(abc.ABCMeta)):
#     """
#     Abstract Base Class for a reader of a ground motion spectra record
#     """
#     def __init__(self, input_files, folder_name=None):
#         """
#         Intantiate with basic file checks
#         """
#         self.input_files = []
#         for fname in input_files:
#             if folder_name:
#                 filename = os.path.join(folder_name, fname)
#                 if os.path.exists(filename):
#                     self.input_files.append(filename)
#             else:
#                 if os.path.exists(fname):
#                     self.input_files.append(fname)
# 
#     @abc.abstractmethod
#     def parse_spectra(self):
#         """
#         Parses the spectra
#         """


# Path: smtk/parsers/sigma_database_parser.py
import os
import csv
import numpy as np
import h5py
from collections import OrderedDict
from datetime import datetime
from smtk.sm_database import *
from smtk.parsers.base_database_parser import (get_float, get_int,
                                               SMDatabaseReader,
                                               SMTimeSeriesReader,
                                               SMSpectraReader)

        """
        Parses the distance data
        """
        return RecordDistance(get_float(metadata['distance_repi']),
                              get_float(metadata['distance_rhyp']),
                              rjb = get_float(metadata['distance_rjb']),
                              rrup = get_float(metadata['distance_rrup']),
                              r_x = None,
                              flag = get_int(metadata['distance_flag']))
                        
                        
    def _parse_site_data(self, metadata):
        """
        Parse the site data
        """
        site = RecordSite(
            metadata['station.oid'],
            metadata['station.name'],
            metadata['station.code'],
            get_float(metadata['station.longitude']),
            get_float(metadata['station.latitude']),
            get_float(metadata['station.altitude']),
            vs30=get_float(metadata['station.vs30']),
            vs30_measured=get_int(metadata['station.vs30_measured']),
            network_code=metadata['station.agency.name'],
            country=metadata['station.country.name'])
        site.vs30_measured_type = metadata['station.vs30_measured_type']
        site.instrument_type = metadata['recording_type']
        site.digitiser = metadata['digitalizer']
        site.building_structure = metadata['station.building_struct.name']

        site.number_floors = get_int(metadata['station.number_of_floor'])