// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | contact@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The radiance calculation routine is converted from the Python code of the HITRAN Application
// Programming Interface (HAPI) distributed under the Academic Free License 3.0, http://hitran.org/hapi/
// R.V. Kochanov, I.E. Gordon, L.S. Rothman, P. Wcisło, C. Hill, J.S. Wilzewski, "HITRAN Application Programming Interface (HAPI):
// A comprehensive approach to working with spectroscopic data", JQSRT, Volume 177, July 2016, Pages 15–30; (hitran.org/hapi/)
//
// Appendix 4 of the theoretical section from the HITRAN-PC 4.0 manual was also consulted, www.cas.usf.edu/lidarlab/hitranpc/Hitran-PC_4.0_Manual.pdf
// Killinger D.K., Wilcos W.E., Pliutau D., \"HITRAN=PC: 25 years of academic development and commercialization of laser
// atmospheric transmission software for environmental remote sensing\", Technology & Innovation, Vol. 14, No. 3-4, pp. 303-327
//
// 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 calculate the radiance
#include "radiance.h"
#include "const.h"
#include "math.h"

Radiance::Radiance()
{

}

// Method to calculate radiance for horizontal path (based on HAPI)
void Radiance::calculateRadiance(Spectrum *odSpec, Spectrum *radiance, QVector<double> *wavelength, double temp)
{
    int i;  // Loop variable
    double A1w;     //
    double LBBTw;   //
    double const1;  // First constant
    double const2;  // Second constant


    // Alw = 1-exp(-AbsorptionCoefficient*l)
    // LBBTw = 2*hh*cc**2*Omegas**3 / (exp(hh*cc*Omegas/(cBolts*T)) - 1) * 1.0E-7
    // Xsect = Alw*LBBTw # W/sr/cm**2/cm**-1

    // Resizing the radiance spectrum
    (*radiance).getY()->resize(odSpec->getY()->count());
    const1 = 2 * Const::hCgs * Const::cCgs * Const::cCgs * 1.0E-7;
    const2 = Const::hCgs * Const::cCgs;
    for (i = 0; i < radiance->getY()->count(); i++)
    {
        A1w = 1 - exp(-((*odSpec).getYAt(i)));
        LBBTw = const1 * pow((*wavelength)[i], 3) / (exp(const2 * (*wavelength)[i] / (Const::BoltsCgs * temp)) - 1);
        (*radiance).setYAt(i, A1w * LBBTw);
    }
}

