<commit_before>"""
Module dealing with visualizing OpSim results
"""
from __future__ import absolute_import, print_function, division
import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon 
import matplotlib.pyplot as plt
from .trig import (pixelsForAng,
                   convertToCelestialCoordinates)
from .healpix import healpix_boundaries                   
from .healpixTiles import HealpixTiles
import healpy as hp

__all__ = ['plot_south_steradian_view', 'HPTileVis']
def plot_south_steradian_view(ra, dec, numPoints=100, radius=1.75, boundary=20.,
                              ax=None, show_frame=True):
    """
    """
    if ax is None:
        fig, ax = plt.subplots()
    m = Basemap(projection='spstere', boundinglat=boundary, lon_0=0., ax=ax)
    if show_frame:
        m.drawparallels(np.arange(-80.,81.,20.))
        m.drawmeridians(np.arange(-180.,181.,20.))
    for ra_val, dec_val in zip(ra, dec):
            m.tissot(ra_val, dec_val, radius, numPoints, ax,
                     **dict(fill=False))

    return fig


class HPTileVis(object):
    """
    Class useful for visualizing a combination of OpSim Pointings and Healpix
    Tiles

    Parameters
    ----------
    hpTile :
    opsout :

    Methods
    -------
    
    """
    def __init__(self, hpTile, opsout):
        """
        """
        self.hpTile = hpTile
        self.opsout = opsout
        self.nside = self.hpTile.nside

    @staticmethod
    def tileIDfromCelestialCoordinates(ra, dec, nside, units='degrees'):
        """
        Parameters
        -----------
        ra : float, mandatory
            ra for the point
        dec : float, mandatory
            dec for the point
        units: {'degrees', 'radians'}
        """
        return pixelsForAng(lon=ra,lat=dec, nside=nside, unit=units)

    def tileCenter(self, tileID):
        theta, phi = hp.pix2ang(self.nside, tileID, nest=True)
        ra, dec = convertToCelestialCoordinates(theta, phi, input_unit='radians',
                                                output_unit='degrees')
        return ra, dec

    def pointingSummary(self,
                        tileID=None,
                        ra=None,
                        dec=None,
                        angularUnits='degrees',
                        allPointings=None):
        """
        Parameters
        -----------
        ra : float, mandatory
            ra for the point
        dec : float, mandatory
            dec for the point
        units: {'degrees', 'radians'}
        """
        if tileID is None:
            if ra is None or dec is None:
                raise ValueError("Both tileID and ra, dec cannot be None")

            # Set tileID from ra, dec
            tileID = self.tileIDfromCelestialCoordinates(ra,
                                                         dec,
                                                         self.nside,
                                                         units=angularUnits)

        obsHistIDs = self.hpTile.pointingSequenceForTile(tileID=tileID,
                                                         allPointings=allPointings)
        s = self.opsout.summary.ix[obsHistIDs]
        return s

    def pointingCenters(self,
                tileID,
                raCol='ditheredRA',
                decCol='ditheredDec',
                query=None,
                groupby=None):
        summary = self.pointingSummary(tileID)#, columns=[raCol, decCol])
        
        if query is not None:
            summary = summary.query(query)
        ra = summary[raCol].apply(np.degrees).values
        dec = summary[decCol].apply(np.degrees).values
        return ra, dec

    def plotTilePointings(self,
                          tileID,
                          raCol='ditheredRA', decCol='ditheredDec',
                          radius=1.75,
                          paddingFactors=1,
                          query=None,
                          ax=None,
                          projection='cyl',
                          tile_centers=None,
                          corners=None,
                          **kwargs):
        """
        Parameters
        ----------
        tileID : int, mandatory
            Healpix tileID in the nested scheme
        raCol : string, defaults to 'ditheredRA'
            column name with ra that should be used 
        decCol : string, defaults to 'ditheredDec'
            column name with dec that should be used 
        radius : float, defaults to 1.75, degrees
            radius of the field of view
        paddingFactors: float, defaults to 1.0
            controls the size of the figure wrt angular dimensions of the tile.
            paddingFactors=1 is designed to get all the centers of pointings<commit_msg>fixing docstrings 	modified:   opsimsummary/visualization.py<commit_after>"""
Module dealing with visualizing OpSim results
"""
from __future__ import absolute_import, print_function, division
import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon 
import matplotlib.pyplot as plt
from .trig import (pixelsForAng,
                   convertToCelestialCoordinates)
from .healpix import healpix_boundaries                   
from .healpixTiles import HealpixTiles
import healpy as hp

__all__ = ['plot_south_steradian_view', 'HPTileVis']
def plot_south_steradian_view(ra, dec, numPoints=100, radius=1.75, boundary=20.,
                              ax=None, show_frame=True):
    """
    """
    if ax is None:
        fig, ax = plt.subplots()
    m = Basemap(projection='spstere', boundinglat=boundary, lon_0=0., ax=ax)
    if show_frame:
        m.drawparallels(np.arange(-80.,81.,20.))
        m.drawmeridians(np.arange(-180.,181.,20.))
    for ra_val, dec_val in zip(ra, dec):
            m.tissot(ra_val, dec_val, radius, numPoints, ax,
                     **dict(fill=False))

    return fig


class HPTileVis(object):
    """
    Class useful for visualizing a combination of OpSim Pointings and Healpix
    Tiles

    Parameters
    ----------
    hpTile :
    opsout :

    Methods
    -------
    
    """
    def __init__(self, hpTile, opsout):
        """
        """
        self.hpTile = hpTile
        self.opsout = opsout
        self.nside = self.hpTile.nside

    @staticmethod
    def tileIDfromCelestialCoordinates(ra, dec, nside, units='degrees'):
        """
        Obtain the tile id for a Healpix tile in the nested convention from
        the ra and dec in degrees
        Parameters
        -----------
        ra : float, mandatory
            ra for the point
        dec : float, mandatory
            dec for the point
        units: {'degrees', 'radians'}
        """
        return pixelsForAng(lon=ra,lat=dec, nside=nside, unit=units)

    def tileCenter(self, ipix):
        """
        return a tuple of arrays returning the ra, dec in degrees of center of
        the healpix tile with id tileID in the nest convention for NSIDE given
        by `self.nside`

        Parameters
        ----------
        ipix : 
        """
        theta, phi = hp.pix2ang(self.nside, ipix, nest=True)
        ra, dec = convertToCelestialCoordinates(theta, phi, input_unit='radians',
                                                output_unit='degrees')
        return ra, dec

    def pointingSummary(self,
                        tileID=None,
                        ra=None,
                        dec=None,
                        angularUnits='degrees',
                        allPointings=None):
        """
        returns the OpSim Summary table corresponding to the maximal set of
        pointings intersecting with the Healpix tile with tileID.
        Parameters
        -----------
        ra : float, mandatory
            ra for the point
        dec : float, mandatory
            dec for the point
        units: {'degrees', 'radians'}
        """
        if tileID is None:
            if ra is None or dec is None:
                raise ValueError("Both tileID and ra, dec cannot be None")

            # Set tileID from ra, dec
            tileID = self.tileIDfromCelestialCoordinates(ra,
                                                         dec,
                                                         self.nside,
                                                         units=angularUnits)

        obsHistIDs = self.hpTile.pointingSequenceForTile(tileID=tileID,
                                                         allPointings=allPointings)
        s = self.opsout.summary.ix[obsHistIDs]
        return s

    def pointingCenters(self,
                tileID,
                raCol='ditheredRA',
                decCol='ditheredDec',
                query=None,
                groupby=None):
        """
        """
        summary = self.pointingSummary(tileID)#, columns=[raCol, decCol])
        
        if query is not None:
            summary = summary.query(query)
        ra = summary[raCol].apply(np.degrees).values
        dec = summary[decCol].apply(np.degrees).values
        return ra, dec

    def plotTilePointings(self,
                          tileID,
                          raCol='ditheredRA', decCol='ditheredDec',
                          radius=1.75,
                          paddingFactors=1,
                          query=None,
                          ax=None,
                          projection='cyl',
                          tile_centers=None,
                          corners=None,
                          **kwargs):
        """
        Plot the Healpix Tile and the maximal set of pointings overlapping with
        it.
        Parameters
        ----------
        tileID : int, mandatory
            Healpix tileID in the nested scheme
        raCol : string, defaults to 'ditheredRA'
            column name with ra that should be used 
        decCol : string, defaults to 'ditheredDec'
            column name with dec that should be used 
        radius : float, defaults to 1.75, degrees
            radius of the field of view
        paddingFactors: float, defaults to 1.0
            controls the size of the figure wrt angular dimensions of the tile.
            paddingFactors=1 is designed to get all the centers of pointings
{'fraction_non_alphanumeric': 0.08527583527583528, 'fraction_numerical': 0.006798756798756799, 'mean_word_length': 1.4669381887877335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2619967', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2654, 'n_words': 758}