opsimsummaryv2.summary_opsim
============================

.. py:module:: opsimsummaryv2.summary_opsim

.. autoapi-nested-parse::

   Main module to read OpSim output database.



Attributes
----------

.. autoapisummary::

   opsimsummaryv2.summary_opsim.use_geopandas


Classes
-------

.. autoapisummary::

   opsimsummaryv2.summary_opsim.OpSimSurvey


Module Contents
---------------

.. py:class:: OpSimSurvey(db_path, mjd_range=None, host_file=None, host_config={})

   A class to manipulate OpSim db data and turn them into simulation inputs.

   .. attribute:: db_path

      Path to the database file

      :type: pathlib.Path

   .. attribute:: sql_engine

      The sqlalchemy engine link to the db.

      :type: sqlalchemy.engine.base.Engine

   .. attribute:: opsimdf

      Dataframe of the simulated observations.

      :type: pandas.DataFrame

   .. attribute:: tree

      A BallTree used to select observations on the healpy representation of the survey.

      :type: sklearn.neighbors.BallTree

   .. attribute:: host

      Dataframe of the hosts.

      :type: pandas.DataFrame

   .. attribute:: hp_rep

      The healpy representation oif the survey

      :type: pandas.DataFrame

   .. attribute:: survey

      The healpy representation oif the survey

      :type: pandas.DataFrame

   .. attribute:: survey_hosts

      The hosts that are inside the survey.

      :type: pandas.DataFrame


   .. py:method:: _get_df_from_sql(sql_engine, mjd_range=None)
      :staticmethod:


      Load data from the db file.

      :param sql_engine: The sqlalchemy engine link to the db.
      :type sql_engine: sqlalchemy.engine.base.Engine
      :param mjd_range: Min and Max date to query if float assumed to be mjd, by default None
      :type mjd_range: ( , ) str or float, optional

      :returns: Dataframe of the simulated observations.
      :rtype: pandas.DataFrame



   .. py:method:: _get_sql_engine(dbname)
      :staticmethod:


      Read a sql db Opsim output file.

      :param dbname: Path to sql file.
      :type dbname: str

      :returns: The sqlalchemy engine link to the db.
      :rtype: sqlalchemy.engine.base.Engine



   .. py:method:: _read_host_file(host_file, col_ra='ra', col_dec='dec', ra_dec_unit='radians', wgt_map=None, add_SNMAGSHIFT=False)

      Read a parquet file containing hosts.

      :param host_file: Path to the parquet file
      :type host_file: str
      :param col_ra: Key of column containing RA, by default 'ra'
      :type col_ra: str, optional
      :param col_dec: Key of column containing Dec, by default 'dec'
      :type col_dec: str, optional
      :param ra_dec_unit: Unit of ra_dec (radians or degrees), by default 'radians'
      :type ra_dec_unit: str, optional

      :returns: Dataframe of the hosts.
      :rtype: pandas.DataFrame



   .. py:method:: compute_hp_rep(nside=256, minVisits=500, maxVisits=100000, ddf_nobs_thresh=1100, add_ddf_tag=False, angle_sep_tol=10)

      Compute a healpy version of the survey.

      :param nside: Healpy nside, by default 256
      :type nside: int, optional
      :param minVisits: Minimum number of observations required in the survey, by default 500
      :type minVisits: int, optional
      :param maxVisits: Maximum number of observations required in the survey, by default 100000
      :type maxVisits: int, optional



   .. py:method:: formatObs(OpSimObs, keep_keys=[])

      Format function to get quantities of interest for simulation.

      :param OpSimObs: Dataframe of OpSim output
      :type OpSimObs: pandas.DataFrame
      :param keep_keys: List of keys to keep in addition to formatted quantities
      :type keep_keys: list(str)

      :returns: Dataframe that contains quantities for simulation.
      :rtype: pandas.DataFrame

      .. rubric:: Notes

      Quantities are obtained following arxiv:1905.02887

      Details of the calculs:

      The :math:`\sigma_\mathrm{PSF}` in units of :math:`\mathrm{arcsec}^{-1}` is obtained as

      .. math::
          \sigma_\mathrm{PSF} = \frac{\mathbf{seeingFwhmEff}}{2\sqrt{2\ln2}}.

      The :math:`\mathbf{PSF}` is computed in units of :math:`\mathrm{pixel}^{-1}` as

      .. math::
          \mathbf{PSF} = \frac{\sigma_\mathrm{PSF}}{\mathrm{PixelSize}},

      where :math:`\mathrm{PixelSize}` is the pixel size in :math:`\mathrm{arcsec}^{-1}`.

      The zero-point :math:`\mathbf{ZPT}` is computed as

      .. math::
          \mathbf{ZPT} = 2 m_5 - m_\mathrm{sky} + 2.5 \log\left[25 A \left(1 + A^{-1}10^{-0.4(m_5-m_\mathrm{sky})}\right)\right],

      where :math:`m_5 = \mathbf{fiveSigmaDepth}`, :math:`m_\mathrm{sky} = \mathbf{skyBrightness}` and :math:`A` is the noise equivalent area given by
      :math:`A = 4 \pi \sigma_\mathrm{PSF}^2`.

      The sky noise :math:`\mathbf{SKYSIG}` in unit of :math:`\mathrm{ADU}.\mathrm{pixel}^{-1}` is computed such as

      .. math::
          \mathbf{SKYSIG}^2 = 10^{-0.4\left(m_\mathrm{sky} - \mathbf{ZPT}\right)} \times \mathrm{PixelSize}^2.



   .. py:method:: get_obs_from_coords(ra, dec, is_deg=True, formatobs=False, keep_keys=[])

      Get observations at ra, dec coordinates.

      :param ra: RA coordinate
      :type ra: numpy.ndarray(float)
      :param dec: Dec coordinate
      :type dec: numpy.ndarray(float)
      :param is_deg: is RA, Dec given in degrees, by default True
      :type is_deg: bool, optional
      :param formatobs: format obs for simulation, by default False
      :type formatobs: bool, optional
      :param keep_keys: List of keys to keep in addition to formatted quantities
      :type keep_keys: list(str)

      :Yields: *pandas.DatFrame* -- Dataframes of observations.



   .. py:method:: get_survey_hosts(nworkers=10)

      Get survey hosts.

      :param nworkers: Number of cores used to run multiprocessing, by default 10
      :type nworkers: int, optional

      :returns: Dataframe of host inside the survey, matched to their field indicated by GROUPID.
      :rtype: pandas.DataFrame



   .. py:method:: get_survey_obs(formatobs=True, keep_keys=[])

      Get survey observations.

      :param formatobs: Format the observation to get only quantities of interest for simulation, by default True
      :type formatobs: bool, optional
      :param keep_keys: List of keys to keep in addition to formatted quantities
      :type keep_keys: list(str)

      :Yields: *pandas.DatFrame* -- Dataframes of observations.



   .. py:method:: plot_hp_rep(**hp_mollview_kwargs)

      Plot the healpy mollview of the survey representation.

      :returns: The mollview figure
      :rtype: matplotlib.Figure



   .. py:method:: sample_survey(N_fields, random_seed=None, nworkers=10)

      Sample Nfields inside the survey's healpy representation.

      :param N_fields: Number of fields to sample
      :type N_fields: int
      :param random_seed: The random  seed used to sample the fields, by default None
      :type random_seed: int or numpy.random.SeedSquence, optional
      :param nworkers: Number of cores used to run multiprocessing on host matching, by default 10
      :type nworkers: int, optional

      .. rubric:: Notes

      Random seed only apply on field sampling.



   .. py:attribute:: __LSST_DDF_TAGS__


   .. py:attribute:: __LSST_FIELD_RADIUS__


   .. py:attribute:: __LSST_pixelSize__
      :value: 0.2



   .. py:attribute:: _hp_rep
      :value: None



   .. py:attribute:: _survey
      :value: None



   .. py:attribute:: db_path


   .. py:attribute:: host
      :value: None



   .. py:property:: hp_rep


   .. py:attribute:: opsimdf


   .. py:attribute:: sql_engine
      :value: None



   .. py:property:: survey


   .. py:property:: survey_hosts


   .. py:attribute:: tree


.. py:data:: use_geopandas
   :value: True


