# Path: smtk/sm_utils.py
# def _save_image(filename, fig, format='png', dpi=300, **kwargs):  # noqa
#     """
#     Saves the matplotlib figure `fig` to `filename`. Wrapper around `fig.savefig`
#     with `dpi=300` by default and `format` inferred from `filename` extension
#     or, if no extension is found, set as "png".
#     If filename is empty this function does nothing and return
# 
#     :param str filename: str, the file path
#     :param figure: a :class:`matplotlib.figure.Figure` (e.g. via
#         `matplotlib.pyplot.figure()`)
#     :param format: string, the image format. Default: 'png'. This argument is
#         ignored if `filename` has a file extension, as `format` will be set
#         equal to the extension without leading dot.
#     :param str kwargs: additional keyword arguments to pass to `fig.savefig`.
#         For details, see:
#         https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html
#     """
#     if not filename:
#         return
# 
#     name, ext = os.path.splitext(filename)
#     if ext:
#         format = ext[1:]  # noqa
#     else:
#         filename = name + '.' + format
# 
#     fig.savefig(filename, dpi=dpi, format=format, **kwargs)
# 
# def get_time_vector(time_step, number_steps):
#     """
#     General SMTK utils
#     """
#     return np.cumsum(time_step * np.ones(number_steps, dtype=float)) - time_step
# 
# def convert_accel_units(acceleration, from_, to_='cm/s/s'):  # noqa
#     """
#     Converts acceleration from/to different units
# 
#     :param acceleration: the acceleration (numeric or numpy array)
#     :param from_: unit of `acceleration`: string in "g", "m/s/s", "m/s**2",
#         "m/s^2", "cm/s/s", "cm/s**2" or "cm/s^2"
#     :param to_: new unit of `acceleration`: string in "g", "m/s/s", "m/s**2",
#         "m/s^2", "cm/s/s", "cm/s**2" or "cm/s^2". When missing, it defaults
#         to "cm/s/s"
# 
#     :return: acceleration converted to the given units (by default, 'cm/s/s')
#     """
#     m_sec_square = ("m/s/s", "m/s**2", "m/s^2")
#     cm_sec_square = ("cm/s/s", "cm/s**2", "cm/s^2")
#     acceleration = np.asarray(acceleration)
#     if from_ == 'g':
#         if to_ == 'g':
#             return acceleration
#         if to_ in m_sec_square:
#             return acceleration * g
#         if to_ in cm_sec_square:
#             return acceleration * (100 * g)
#     elif from_ in m_sec_square:
#         if to_ == 'g':
#             return acceleration / g
#         if to_ in m_sec_square:
#             return acceleration
#         if to_ in cm_sec_square:
#             return acceleration * 100
#     elif from_ in cm_sec_square:
#         if to_ == 'g':
#             return acceleration / (100 * g)
#         if to_ in m_sec_square:
#             return acceleration / 100
#         if to_ in cm_sec_square:
#             return acceleration
# 
#     raise ValueError("Unrecognised time history units. "
#                      "Should take either ''g'', ''m/s/s'' or ''cm/s/s''")
# 
# def get_velocity_displacement(time_step, acceleration, units="cm/s/s",
#                               velocity=None, displacement=None):
#     """
#     Returns the velocity and displacment time series using simple integration
#     :param float time_step:
#         Time-series time-step (s)
#     :param numpy.ndarray acceleration:
#         Acceleration time-history
#     :returns:
#         velocity - Velocity Time series (cm/s)
#         displacement - Displacement Time series (cm)
#     """
#     acceleration = convert_accel_units(acceleration, units)
#     if velocity is None:
#         velocity = time_step * cumtrapz(acceleration, initial=0.)
#     if displacement is None:
#         displacement = time_step * cumtrapz(velocity, initial=0.)
#     return velocity, displacement


import numpy as np
import matplotlib.pyplot as plt
from math import sqrt
from smtk.sm_utils import (_save_image, get_time_vector, convert_accel_units,
                           get_velocity_displacement)
    ax = plt.subplot(2, 2, 2)
    # Velocity
    PLOT_TYPE[axis_type](ax, spectra["Period"], spectra["Velocity"])
    PLOT_TYPE[axis_type](ax, spectra["Period"], spectra["Pseudo-Velocity"])
    ax.set_xlabel("Periods (s)", fontsize=12)
    ax.set_ylabel("Velocity (cm/s)", fontsize=12)
    ax.set_xlim(np.min(spectra["Period"]), np.max(spectra["Period"]))
    ax.grid()
    ax.legend(("Velocity", "PSV"), loc=0) 
    ax = plt.subplot(2, 2, 3)
    # Displacement
    PLOT_TYPE[axis_type](ax, spectra["Period"], spectra["Displacement"])
    ax.set_xlabel("Periods (s)", fontsize=12)
    ax.set_ylabel("Displacement (cm)", fontsize=12)
    ax.set_xlim(np.min(spectra["Period"]), np.max(spectra["Period"]))
    ax.grid()
    _save_image(filename, plt.gcf(), filetype, dpi)
    plt.show()


def plot_time_series(acceleration, time_step, velocity=[], displacement=[],
                     units="cm/s/s", figure_size=(8, 6), filename=None,
                     filetype="png", dpi=300, linewidth=1.5):
    """
    Creates a plot of acceleration, velocity and displacement for a specific
    ground motion record
    """
    acceleration = convert_accel_units(acceleration, units)
    accel_time = get_time_vector(time_step, len(acceleration))
    if not len(velocity):

# Path: smtk/sm_utils.py
# def _save_image(filename, fig, format='png', dpi=300, **kwargs):  # noqa
#     """
#     Saves the matplotlib figure `fig` to `filename`. Wrapper around `fig.savefig`
#     with `dpi=300` by default and `format` inferred from `filename` extension
#     or, if no extension is found, set as "png".
#     If filename is empty this function does nothing and return
# 
#     :param str filename: str, the file path
#     :param figure: a :class:`matplotlib.figure.Figure` (e.g. via
#         `matplotlib.pyplot.figure()`)
#     :param format: string, the image format. Default: 'png'. This argument is
#         ignored if `filename` has a file extension, as `format` will be set
#         equal to the extension without leading dot.
#     :param str kwargs: additional keyword arguments to pass to `fig.savefig`.
#         For details, see:
#         https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html
#     """
#     if not filename:
#         return
# 
#     name, ext = os.path.splitext(filename)
#     if ext:
#         format = ext[1:]  # noqa
#     else:
#         filename = name + '.' + format
# 
#     fig.savefig(filename, dpi=dpi, format=format, **kwargs)
# 
# def get_time_vector(time_step, number_steps):
#     """
#     General SMTK utils
#     """
#     return np.cumsum(time_step * np.ones(number_steps, dtype=float)) - time_step
# 
# def convert_accel_units(acceleration, from_, to_='cm/s/s'):  # noqa
#     """
#     Converts acceleration from/to different units
# 
#     :param acceleration: the acceleration (numeric or numpy array)
#     :param from_: unit of `acceleration`: string in "g", "m/s/s", "m/s**2",
#         "m/s^2", "cm/s/s", "cm/s**2" or "cm/s^2"
#     :param to_: new unit of `acceleration`: string in "g", "m/s/s", "m/s**2",
#         "m/s^2", "cm/s/s", "cm/s**2" or "cm/s^2". When missing, it defaults
#         to "cm/s/s"
# 
#     :return: acceleration converted to the given units (by default, 'cm/s/s')
#     """
#     m_sec_square = ("m/s/s", "m/s**2", "m/s^2")
#     cm_sec_square = ("cm/s/s", "cm/s**2", "cm/s^2")
#     acceleration = np.asarray(acceleration)
#     if from_ == 'g':
#         if to_ == 'g':
#             return acceleration
#         if to_ in m_sec_square:
#             return acceleration * g
#         if to_ in cm_sec_square:
#             return acceleration * (100 * g)
#     elif from_ in m_sec_square:
#         if to_ == 'g':
#             return acceleration / g
#         if to_ in m_sec_square:
#             return acceleration
#         if to_ in cm_sec_square:
#             return acceleration * 100
#     elif from_ in cm_sec_square:
#         if to_ == 'g':
#             return acceleration / (100 * g)
#         if to_ in m_sec_square:
#             return acceleration / 100
#         if to_ in cm_sec_square:
#             return acceleration
# 
#     raise ValueError("Unrecognised time history units. "
#                      "Should take either ''g'', ''m/s/s'' or ''cm/s/s''")
# 
# def get_velocity_displacement(time_step, acceleration, units="cm/s/s",
#                               velocity=None, displacement=None):
#     """
#     Returns the velocity and displacment time series using simple integration
#     :param float time_step:
#         Time-series time-step (s)
#     :param numpy.ndarray acceleration:
#         Acceleration time-history
#     :returns:
#         velocity - Velocity Time series (cm/s)
#         displacement - Displacement Time series (cm)
#     """
#     acceleration = convert_accel_units(acceleration, units)
#     if velocity is None:
#         velocity = time_step * cumtrapz(acceleration, initial=0.)
#     if displacement is None:
#         displacement = time_step * cumtrapz(velocity, initial=0.)
#     return velocity, displacement


# Path: smtk/response_spectrum.py
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt
from smtk.sm_utils import (_save_image, get_time_vector, convert_accel_units,
                           get_velocity_displacement)

    ax = plt.subplot(2, 2, 2)
    # Velocity
    PLOT_TYPE[axis_type](ax, spectra["Period"], spectra["Velocity"])
    PLOT_TYPE[axis_type](ax, spectra["Period"], spectra["Pseudo-Velocity"])
    ax.set_xlabel("Periods (s)", fontsize=12)
    ax.set_ylabel("Velocity (cm/s)", fontsize=12)
    ax.set_xlim(np.min(spectra["Period"]), np.max(spectra["Period"]))
    ax.grid()
    ax.legend(("Velocity", "PSV"), loc=0) 
    ax = plt.subplot(2, 2, 3)
    # Displacement
    PLOT_TYPE[axis_type](ax, spectra["Period"], spectra["Displacement"])
    ax.set_xlabel("Periods (s)", fontsize=12)
    ax.set_ylabel("Displacement (cm)", fontsize=12)
    ax.set_xlim(np.min(spectra["Period"]), np.max(spectra["Period"]))
    ax.grid()
    _save_image(filename, plt.gcf(), filetype, dpi)
    plt.show()


def plot_time_series(acceleration, time_step, velocity=[], displacement=[],
                     units="cm/s/s", figure_size=(8, 6), filename=None,
                     filetype="png", dpi=300, linewidth=1.5):
    """
    Creates a plot of acceleration, velocity and displacement for a specific
    ground motion record
    """
    acceleration = convert_accel_units(acceleration, units)
    accel_time = get_time_vector(time_step, len(acceleration))
    if not len(velocity):

        velocity, dspl = get_velocity_displacement(time_step, acceleration)