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


import numpy as np
import matplotlib.pyplot as plt
from math import sqrt, pi, sin, cos, fabs
from copy import deepcopy
from openquake.hazardlib.geo import Point, Line, Polygon, Mesh, PlanarSurface
from openquake.hazardlib.scalerel.wc1994 import WC1994
from openquake.hazardlib.site import Site, SiteCollection
from openquake.hazardlib.source.rupture import BaseRupture as Rupture
from openquake.hazardlib.source.point import PointSource
from openquake.hazardlib.gsim.base import (
    SitesContext, RuptureContext, DistancesContext)
from smtk.sm_utils import _save_image
from openquake.hazardlib.contexts import get_distances

    def plot_model_configuration(self, marker_style="o", figure_size=(7, 5), 
            filename=None, filetype="png", dpi=300):
        """
        Produces a 3D plot of the current model configuration
        """
        fig = plt.figure(figsize=figure_size)
        ax = fig.add_subplot(111, projection='3d')
        # Wireframe rupture surface mesh
        lons = []
        lats = []
        depths = []
        for pnt in [self.surface.top_left, self.surface.top_right,
                    self.surface.bottom_right, self.surface.bottom_left,
                    self.surface.top_left]:
            lons.append(pnt.longitude)
            lats.append(pnt.latitude)
            depths.append(-pnt.depth)
        ax.plot(lons, lats, depths, "k-", lw=2)

        # Scatter the target sites
        ax.scatter(self.target_sites.mesh.lons,
                   self.target_sites.mesh.lats,
                   np.ones_like(self.target_sites.mesh.lons),
                   c='r',
                   marker=marker_style)
        ax.set_xlabel('Longitude')
        ax.set_ylabel('Latitude')
        ax.set_zlabel('Depth (km)')
        ax.set_zlim(np.floor(min(depths)), 0.0)

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


# Path: smtk/trellis/configure.py
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt, pi, sin, cos, fabs
from copy import deepcopy
from openquake.hazardlib.geo import Point, Line, Polygon, Mesh, PlanarSurface
from openquake.hazardlib.scalerel.wc1994 import WC1994
from openquake.hazardlib.site import Site, SiteCollection
from openquake.hazardlib.source.rupture import BaseRupture as Rupture
from openquake.hazardlib.source.point import PointSource
from openquake.hazardlib.gsim.base import (
    SitesContext, RuptureContext, DistancesContext)
from smtk.sm_utils import _save_image
from openquake.hazardlib.contexts import get_distances


    def plot_model_configuration(self, marker_style="o", figure_size=(7, 5), 
            filename=None, filetype="png", dpi=300):
        """
        Produces a 3D plot of the current model configuration
        """
        fig = plt.figure(figsize=figure_size)
        ax = fig.add_subplot(111, projection='3d')
        # Wireframe rupture surface mesh
        lons = []
        lats = []
        depths = []
        for pnt in [self.surface.top_left, self.surface.top_right,
                    self.surface.bottom_right, self.surface.bottom_left,
                    self.surface.top_left]:
            lons.append(pnt.longitude)
            lats.append(pnt.latitude)
            depths.append(-pnt.depth)
        ax.plot(lons, lats, depths, "k-", lw=2)

        # Scatter the target sites
        ax.scatter(self.target_sites.mesh.lons,
                   self.target_sites.mesh.lats,
                   np.ones_like(self.target_sites.mesh.lons),
                   c='r',
                   marker=marker_style)
        ax.set_xlabel('Longitude')
        ax.set_ylabel('Latitude')
        ax.set_zlabel('Depth (km)')
        ax.set_zlim(np.floor(min(depths)), 0.0)

        _save_image(filename, plt.gcf(), filetype, dpi)