# Path: brew/constants.py
# HYDROMETER_ADJUSTMENT_TEMP = 59.0
# 
# Path: brew/constants.py
# IMPERIAL_UNITS = u"imperial"
# 
# Path: brew/constants.py
# SI_UNITS = u"metric"
# 
# Path: brew/exceptions.py
# class SugarException(BrewdayException):
#     pass
# 
# Path: brew/validators.py
# def validate_units(units):
#     """
#     Validate units
# 
#     :param str units: Unit type
#     :return: units
#     :rtype: str
#     :raises ValidatorException: If units is unknown
#     """
#     if units in [IMPERIAL_UNITS, SI_UNITS]:
#         return units
#     raise ValidatorException(
#         u"Unkown units '{}', must use {} or {}".format(units, IMPERIAL_UNITS, SI_UNITS)
#     )
# 
# Path: brew/utilities/temperature.py
# def celsius_to_fahrenheit(temp):
#     """
#     Convert degrees Celsius to degrees Fahrenheit
# 
#     :param float temp: The temperature in Celsius
#     :return: The temperature in Fahrenheit
#     :rtype: float
#     """
#     return (temp * 1.8) + 32.0


from ..constants import HYDROMETER_ADJUSTMENT_TEMP
from ..constants import IMPERIAL_UNITS
from ..constants import SI_UNITS
from ..exceptions import SugarException
from ..validators import validate_units
from .temperature import celsius_to_fahrenheit


def hydrometer_adjustment(sg, temp, units=IMPERIAL_UNITS):
    """
    Adjust the Hydrometer if the temperature deviates from 59degF.

    :param float sg: Specific Gravity
    :param float temp: Temperature
    :param str units: The units
    :return: Specific Gravity corrected for temperature
    :rtype: float
    :raises SugarException: If temperature outside freezing to boiling range of water

    The correction formula is from Lyons (1992), who used the following formula
    to fit data from the Handbook of Chemistry and Physics (CRC):

    :math:`\\text{Correction(@59F)} = 1.313454 - 0.132674 \\times T + 2.057793e^{-3} \\times T^2 - 2.627634e^{-6} \\times T^3`

    where T is in degrees F.

    Sources:

    * http://www.topdownbrew.com/SGCorrection.html
    * http://hbd.org/brewery/library/HydromCorr0992.html
    * http://www.brewersfriend.com/hydrometer-temp/
    * http://www.primetab.com/formulas.html
    """  # noqa
    validate_units(units)
    if units == SI_UNITS:
        if temp < 0.0 or 100.0 < temp:

# Path: brew/constants.py
# HYDROMETER_ADJUSTMENT_TEMP = 59.0
# 
# Path: brew/constants.py
# IMPERIAL_UNITS = u"imperial"
# 
# Path: brew/constants.py
# SI_UNITS = u"metric"
# 
# Path: brew/exceptions.py
# class SugarException(BrewdayException):
#     pass
# 
# Path: brew/validators.py
# def validate_units(units):
#     """
#     Validate units
# 
#     :param str units: Unit type
#     :return: units
#     :rtype: str
#     :raises ValidatorException: If units is unknown
#     """
#     if units in [IMPERIAL_UNITS, SI_UNITS]:
#         return units
#     raise ValidatorException(
#         u"Unkown units '{}', must use {} or {}".format(units, IMPERIAL_UNITS, SI_UNITS)
#     )
# 
# Path: brew/utilities/temperature.py
# def celsius_to_fahrenheit(temp):
#     """
#     Convert degrees Celsius to degrees Fahrenheit
# 
#     :param float temp: The temperature in Celsius
#     :return: The temperature in Fahrenheit
#     :rtype: float
#     """
#     return (temp * 1.8) + 32.0


# Path: brew/utilities/sugar.py
from ..constants import HYDROMETER_ADJUSTMENT_TEMP
from ..constants import IMPERIAL_UNITS
from ..constants import SI_UNITS
from ..exceptions import SugarException
from ..validators import validate_units
from .temperature import celsius_to_fahrenheit



def hydrometer_adjustment(sg, temp, units=IMPERIAL_UNITS):
    """
    Adjust the Hydrometer if the temperature deviates from 59degF.

    :param float sg: Specific Gravity
    :param float temp: Temperature
    :param str units: The units
    :return: Specific Gravity corrected for temperature
    :rtype: float
    :raises SugarException: If temperature outside freezing to boiling range of water

    The correction formula is from Lyons (1992), who used the following formula
    to fit data from the Handbook of Chemistry and Physics (CRC):

    :math:`\\text{Correction(@59F)} = 1.313454 - 0.132674 \\times T + 2.057793e^{-3} \\times T^2 - 2.627634e^{-6} \\times T^3`

    where T is in degrees F.

    Sources:

    * http://www.topdownbrew.com/SGCorrection.html
    * http://hbd.org/brewery/library/HydromCorr0992.html
    * http://www.brewersfriend.com/hydrometer-temp/
    * http://www.primetab.com/formulas.html
    """  # noqa
    validate_units(units)
    if units == SI_UNITS:
        if temp < 0.0 or 100.0 < temp:

            raise SugarException(