<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import (division, print_function, absolute_import,
                        unicode_literals)

__all__ = ["get_quad_coeffs", "LDCoeffAdaptor"]

import os
import sqlite3
import urllib2
import logging
from tempfile import NamedTemporaryFile

from .config import KPLR_ROOT

DB_FILENAME = "ldcoeffs.db"


def get_quad_coeffs(teff=5778, logg=None, feh=None, data_root=None):
    """
    Get the quadratic coefficients for the standard Kepler limb-darkening
    profile.

    :param teff: (optional)
        The effective temperature in degrees K.

    :param logg: (optional)
        The log10 surface gravity in cm/s/s.

    :param feh: (optional)
        The metallicity [Fe/H].

    :param data_root: (optional)
        The local base directory where the grids will be downloaded to. This
        can also be set using the ``KPLR_ROOT`` environment variable. The
        default value is ``~/.kplr``.

    """
    # Find the LD coefficients from the theoretical models.
    a = LDCoeffAdaptor(data_root=data_root)
    return a.get_coeffs(teff, logg=logg, feh=feh)


class LDCoeffAdaptor(object):
    """
    Wrapper around the `Claret & Bloemen (2011)
    <http://adsabs.harvard.edu/abs/2011A%26A...529A..75C>`_ values for limb
    darkening coefficients. The data is stored in a SQLite database that will
    be downloaded from the server if it isn't already stored locally.

    :param data_root: (optional)
        The local base directory where the grids will be downloaded to. This
        can also be set using the ``KPLR_ROOT`` environment variable. The
        default value is ``~/.kplr``.

    :param clobber: (optional)
        Should the database file be overwritten if it already exists?
        (default: False)

    """

    def __init__(self, data_root=None, clobber=False):
        self.db_filename = download_database(data_root=data_root,
                                             clobber=clobber)

    def get_coeffs(self, teff, logg=None, feh=None):
        """
        Get the coefficients of the quadratic limb darkening profile given by
        theory.

        :param teff:
            The effective temperature in degrees K.

        :param logg: (optional)
            The log10 surface gravity in cm/s/s.

        :param feh: (optional)
            The metallicity [Fe/H].

        """
        assert teff is not None

        q = """
        SELECT mu1,mu2 FROM claret11 WHERE
        teff=(SELECT teff FROM claret11 ORDER BY abs(teff-?) LIMIT 1)
        ORDER BY (logg-?) * (logg-?) + (feh-?) * (feh-?) LIMIT 1
        """
        pars = [teff, logg, logg, feh, feh]

        with sqlite3.connect(self.db_filename) as conn:
            c = conn.cursor()
            rows = c.execute(q, pars)
            mu1, mu2 = rows.fetchone()

        return mu1, mu2


def download_database(data_root=None, clobber=False):
    """
    Download a SQLite database containing the limb darkening coefficients
    computed by `<NAME> (2011)
    <http://adsabs.harvard.edu/abs/2011A%26A...529A..75C>`_. The table is
    available online on `Vizier
    <http://vizier.cfa.harvard.edu/viz-bin/VizieR?-source=J/A+A/529/A75>`_.
    Using the ASCII data table, the SQLite database was generated with the
    following Python commands:

    .. code-block:: python

        import sqlite3
        import numpy as np

        with sqlite3.connect("ldcoeffs.db") as conn:<commit_msg>forget about LDCoeff whatever<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import (division, print_function, absolute_import,
                        unicode_literals)

__all__ = ["get_quad_coeffs"]

import os
import sqlite3
import urllib2
import logging
from tempfile import NamedTemporaryFile

from .config import KPLR_ROOT

DB_FILENAME = "ldcoeffs.db"


def get_quad_coeffs(teff=5778, logg=None, feh=None, data_root=None,
                    clobber=False):
    """
    Get the quadratic coefficients for the standard Kepler limb-darkening
    profile.

    :param teff: (optional)
        The effective temperature in degrees K.

    :param logg: (optional)
        The log10 surface gravity in cm/s/s.

    :param feh: (optional)
        The metallicity [Fe/H].

    :param data_root: (optional)
        The local base directory where the grids will be downloaded to. This
        can also be set using the ``KPLR_ROOT`` environment variable. The
        default value is ``~/.kplr``.

    :param clobber: (optional)
        Should the database file be overwritten even if it already exists?
        (default: False)

    """
    assert teff is not None

    # Make sure that the database is saved locally.
    filename = download_database(data_root=data_root, clobber=clobber)

    # Construct the SQL query.
    q = """
    SELECT mu1,mu2 FROM claret11 WHERE
    teff=(SELECT teff FROM claret11 ORDER BY abs(teff-?) LIMIT 1)
    ORDER BY (logg-?) * (logg-?) + (feh-?) * (feh-?) LIMIT 1
    """
    pars = [teff, logg, logg, feh, feh]

    # Execute the command.
    with sqlite3.connect(filename) as conn:
        c = conn.cursor()
        rows = c.execute(q, pars)
        mu1, mu2 = rows.fetchone()

    return mu1, mu2


def download_database(data_root=None, clobber=False):
    """
    Download a SQLite database containing the limb darkening coefficients
    computed by `<NAME> (2011)
    <http://adsabs.harvard.edu/abs/2011A%26A...529A..75C>`_. The table is
    available online on `Vizier
    <http://vizier.cfa.harvard.edu/viz-bin/VizieR?-source=J/A+A/529/A75>`_.
    Using the ASCII data table, the SQLite database was generated with the
    following Python commands:

    .. code-block:: python

        import sqlite3
        import numpy as np

        with sqlite3.connect("ldcoeffs.db") as conn:
{'fraction_non_alphanumeric': 0.0984001376225701, 'fraction_numerical': 0.018062962325821434, 'mean_word_length': 2.5756457564575648, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4059954', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1794, 'n_words': 578}