// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The following resource has been consulted to convert the relative humidity to ppmv and vise versa
// https://en.wikipedia.org/wiki/Arden_Buck_equation
// and
// http://dx.doi.org/10.1175/1520-0450(1981)020%3C1527:NEFCVP%3E2.0.CO;2
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Object is used to convert from humidity to ppmv and vise versa
#include "humidityconvert.h"
#include "math.h"

HumidityConvert::HumidityConvert()
{
}

// From and to relative humidity (to/from ppmv respectively)
double HumidityConvert::fromRelativeHumidityToPpmv(double rh, double tempC, double totalPressPa)
{
    double pWaterPa;   // Partial pressure of the water vapor in Pa
    double pWaterSaturPa;  // Saturated water vapor pressure at current temperature in Pa


    // Calculating the total pressure of the saturated water vapor at the current temperature
    if (tempC >= 0) { pWaterSaturPa = getPaWaterSaturationPressAboveFreezing(tempC); }
               else { pWaterSaturPa = getPaWaterSaturationPressBelowFreezing(tempC); }

    // Converting the relative humidity to the partial pressure of water vapor and then the ppmv
    pWaterPa = rh * 0.01 * pWaterSaturPa;
    return (pWaterPa / totalPressPa) * 1E+6;
}
double HumidityConvert::fromPpmvToRelativeHumidity(double ppmv, double tempC, double totalPressPa)
{
    double pWaterPa;   // Partial pressure of the water vapor in Pa
    double pWaterSaturPa;  // Saturated water vapor pressure at current temperature in Pa


    // Calculating the total pressure of the saturated water vapor at the current temperature
    if (tempC >= 0) { pWaterSaturPa = getPaWaterSaturationPressAboveFreezing(tempC); }
               else { pWaterSaturPa = getPaWaterSaturationPressBelowFreezing(tempC); }

    // Converting the ppmv value to partial pressure in Pa and then to th erelative humidity
    pWaterPa = ppmv * 1E-6 * totalPressPa;
    return (pWaterPa / pWaterSaturPa) * 100;
}

// Saturation water pressure in Pa for temperatures above freezing (> 0 Celsius)
double HumidityConvert::getPaWaterSaturationPressAboveFreezing(double tempC)
{
    double kPa;

    kPa = 0.61121 * exp((18.678 - (tempC / 234.5)) * (tempC / (257.14 + tempC)));
    return kPa * 1000;
}
// Saturation water pressure in Pa for temperatures above freezing (< 0 Celsius)
double HumidityConvert::getPaWaterSaturationPressBelowFreezing(double tempC)
{
    double kPa;

    kPa = 0.61115 * exp((23.036 - (tempC / 333.7)) * (tempC / (279.82 + tempC)));
    return kPa * 1000;
}
