// 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 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 stores curently active wavelength units and performs conversion from / to the default wavelength units to the current wavelength units
#include "wavelength.h"
#include "math.h"
#include <QMessageBox>
#include <QLocale>

Wavelength::Wavelength()
{
    this->axisTitles.resize(4);
    this->axisTitles[0] = "Wavelength, nm";
    this->axisTitles[1] = "Wavelength, micron";
    this->axisTitles[2] = "Wavenumber, cm-1";
    this->axisTitles[3] = "Frequency, Hz";
}

// Method to get and set the current total pressure units
quint8 Wavelength::getUnits() { return this->currentUnits; }
void Wavelength::setUnits(quint8 newUnits) { this->currentUnits = newUnits; }

// Method to convert from default units to current units
double Wavelength::fromDefaultToCurrent(double defaultValue)
{
    // 0 - Wavelength, nm;  1 - Wavelength, micron, 2 - Wavenumber, cm-1;  3 - Frequency, Hz;  ( 4 - Attenuation, Db try to add later, this is not dB but something involving Db, need to check )
    switch(currentUnits)
    {
        case 0:
            return 10000000 / defaultValue;
            break;
        case 1:
            return 10000 / defaultValue;
            break;
        case 2:
            return defaultValue;
            break;
        case 3:
            return defaultValue * 29979245800;   // 100 is added in to take into account the conversion from centimeters to meters
            break;
        default:
            return defaultValue;
            break;
    }
}

// Method to convert from the current units to the default units
double Wavelength::fromCurrentToDefault(double currentValue)
{
    // 0 - Wavelength, nm;  1 - Wavelength, micron, 2 - Wavenumber, cm-1;  3 - Frequency, Hz;  ( 4 - Attenuation, Db try to add later, this is not dB but something involving Db, need to check )
    switch(currentUnits)
    {
        case 0:
            return 10000000 / currentValue;
            break;
        case 1:
            return 10000 / currentValue;
            break;
        case 2:
            return currentValue;
            break;
        case 3:
            return currentValue * 3.3356409519815204957557671447492 * 1.0e-11;  // 0.01 is added in to take into account the conversion from centimeters to meters
            break;
        default:
            return currentValue;
            break;
    }
}

// Minimum wavenumber
double Wavelength::getStartWn() { return this->startWn; }
void Wavelength::setStartWn(double newStartWn) { this->startWn = newStartWn; }
// Minimum wavelength in current units (setting and returning)
double Wavelength::getStartWlCurrUnits()
{
    // In case of wavelength the largest wavenumber corresponds to the smallest wavelength
    if (this->currentUnits < 2) { return fromDefaultToCurrent(endWn); }
    else { return fromDefaultToCurrent(startWn); }
}
void Wavelength::setStartWlCurrUnits(double newStartWlCurrUnits)
{
    if (this->currentUnits < 2) { endWn = fromCurrentToDefault(newStartWlCurrUnits); }
    else { startWn = fromCurrentToDefault(newStartWlCurrUnits); }
}

// Maximum wavenumber
double Wavelength::getEndWn() { return this->endWn; }
void Wavelength::setEndWn(double newEndWn) { this->endWn = newEndWn; }
// Minimum wavelength in current units (setting and returning)
double Wavelength::getEndWlCurrUnits()
{
    // In case of wavelength the largest wavenumber corresponds to the smallest wavelength
    if (this->currentUnits < 2) { return fromDefaultToCurrent(startWn); }
    else { return fromDefaultToCurrent(endWn); }
}
void Wavelength::setEndWlCurrUnits(double newEndWlCurrUnits)
{
    if (this->currentUnits < 2) { startWn = fromCurrentToDefault(newEndWlCurrUnits); }
    else { endWn = fromCurrentToDefault(newEndWlCurrUnits); }
}

// Method to get and set the number of data points
quint32 Wavelength::getNumPoints() { return this->numPoints; }
void Wavelength::setNumPoints(quint32 newNumPoints)
{
    // Saving the number of datapoints
    this->numPoints = newNumPoints;

    // Setting the wavenumber resolution
    this->wnResol = (this->endWn - this->startWn) / (this->numPoints - 1);
}

// Wavenumber resolution for calculations
double Wavelength::getWnResol() { return this->wnResol; }
void Wavelength::setWnResol(double newWnResol) { this->wnResol = newWnResol; }
// Wavenumber resolution for calculations
double Wavelength::getWlResolCurrUnits()
{
    double midWn;  // Middle wavenumber, cm-1
    double maxWn;  // Wavenumber value shifted by the current resolution value
    double currUnitsWlOne;  // First wavelength in current units
    double currUnitsWlTwo;  // Second wavelength in current units
    double currUnitsResol;  // Resolution in current units


    if (currentUnits == 2)
    {
        return this->wnResol;
    }
    else  // In the conversion to other units the resolution will depend on the location of the spectral region specified
    {
        midWn = (startWn + endWn) * 0.5;
        maxWn = midWn + this->wnResol;
        currUnitsWlOne = fromDefaultToCurrent(midWn);
        currUnitsWlTwo = fromDefaultToCurrent(maxWn);
        currUnitsResol = fabs((double) (currUnitsWlOne - currUnitsWlTwo));
        return currUnitsResol;
    }
}
void Wavelength::setWlResolCurrUnits(double newWnResol)
{
    double midWn;  // Middle wavenumber, cm-1
    double midWlCurrUnits;  // Middle wavelength in current units
    double maxWlCurrUnits;  // Wavelength value shifted by the current resolution value
    double oneWn;  // First wavenumber value used to determine the resolution
    double twoWn;  // Second wavenumber value used to determine the resolution

    if (currentUnits == 2)
    {
        this->wnResol = newWnResol;
    }
    else  // In the conversion to other units the resolution will depend on the location of the spectral region specified
    {
        midWn = (startWn + endWn) * 0.5;
        midWlCurrUnits = fromDefaultToCurrent(midWn);
        maxWlCurrUnits = midWlCurrUnits + newWnResol;
        oneWn = fromCurrentToDefault(midWlCurrUnits);
        twoWn = fromCurrentToDefault(maxWlCurrUnits);
        this->wnResol = fabs((double) (oneWn - twoWn));
    }
}

// Distance from the line center in wavenumbers to run calculations up to
double Wavelength::getWnWing() { return this->wnWing; }
void Wavelength::setWnWing(double newWnWing) { this->wnWing = newWnWing; }

// Distance from the line center in half widths
double Wavelength::getWnWingHw() { return this->wnWingHw; }
void Wavelength::setWnWingHw(double newWnWingHw) { this->wnWingHw = newWnWingHw; }

// Variable indicating if the the spectral calcualtion range in the wings is expressed in wavenumbers or fractions of half widths
bool Wavelength::getWnWingAbs() { return this->wnWingAbs; }
void Wavelength::setWnWingAbs(bool newWnWingAbs) { this->wnWingAbs = newWnWingAbs; }

// Array with the main wavenumber grid values
QVector<double> *Wavelength::getWns() { return &(this->wns); }
double Wavelength::getWnsAt(int index) { return this->wns[index]; }

// Method to initialize the wavenumber grid based on the staring and ending wavenumbers as well as the wavenumber step
void Wavelength::initWavenumberGrid()
{
    int i;  // Loop variable


    // Setting the wavenumber resolution
    this->wnResol = (this->endWn - this->startWn) / (this->numPoints - 1);

    this->wns.resize(this->numPoints);
    for (i = 0; i < this->numPoints; i++)
    {
        this->wns[i] = this->startWn + this->wnResol * i;
    }
}

// Method to get the axis title for the currently selected units
QString Wavelength::getAxisTitle()
{
    return this->axisTitles[currentUnits];
}
