# 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)
                proc_y[key.replace("()/1", "")] = metadata[key]
            elif 'comp_ordered()/2' in key:
                proc_z[key.replace("()/2", "")] = metadata[key]
            else:
                pass
        x_comp = self._parse_component_data(wfid, proc_x)
        y_comp = self._parse_component_data(wfid, proc_y)
        if len(proc_z) > 0:
            z_comp = self._parse_component_data(wfid, proc_z)
        else:
            z_comp = None
        return x_comp, y_comp, z_comp
            
    def _parse_component_data(self, wfid, proc_data):
        """
        Parses the information for each component
        """
        filter_params = {
            'Type': proc_data['comp_ordered.filter_type'],
            'Order': proc_data['comp_ordered.filter_order'],
            'Passes': get_int(
                proc_data['comp_ordered.filter_number_of_passes']),
            'NRoll': get_int(proc_data['comp_ordered.nroll']),
            'High-Cut': get_float(proc_data['comp_ordered.high_cut_freq']),
            'Low-Cut': get_float(proc_data['comp_ordered.low_cut_freq'])}

        intensity_measures = {
            # All m - convert to cm
            'PGA': get_float(proc_data['comp_ordered.pga']),
            'PGV': get_float(proc_data['comp_ordered.pgv']),

# 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)

                proc_y[key.replace("()/1", "")] = metadata[key]
            elif 'comp_ordered()/2' in key:
                proc_z[key.replace("()/2", "")] = metadata[key]
            else:
                pass
        x_comp = self._parse_component_data(wfid, proc_x)
        y_comp = self._parse_component_data(wfid, proc_y)
        if len(proc_z) > 0:
            z_comp = self._parse_component_data(wfid, proc_z)
        else:
            z_comp = None
        return x_comp, y_comp, z_comp
            
    def _parse_component_data(self, wfid, proc_data):
        """
        Parses the information for each component
        """
        filter_params = {
            'Type': proc_data['comp_ordered.filter_type'],
            'Order': proc_data['comp_ordered.filter_order'],
            'Passes': get_int(
                proc_data['comp_ordered.filter_number_of_passes']),
            'NRoll': get_int(proc_data['comp_ordered.nroll']),
            'High-Cut': get_float(proc_data['comp_ordered.high_cut_freq']),
            'Low-Cut': get_float(proc_data['comp_ordered.low_cut_freq'])}

        intensity_measures = {
            # All m - convert to cm
            'PGA': get_float(proc_data['comp_ordered.pga']),
            'PGV': get_float(proc_data['comp_ordered.pgv']),

            'PGD': get_float(proc_data['comp_ordered.pgd'])