// Created with MonoDevelop
//
//    Copyright (C) 2021 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;

namespace Hiperscan.Spectroscopy
{
    public static class WavelengthDeviation
    {
        private static readonly double EPSILON = 1e-20;

        public static DataSet CalculateNistDeviation(Spectrum spectrum, bool inRawNist = false)
        {
            if (spectrum.HasAbsorbance == false)
                return null;

            spectrum = spectrum.Clone();

            if(inRawNist == true)
                spectrum.WavelengthCorrection = null;

            spectrum.ApplyCorrectionsToRawData(CorrectionMask.All);
            WavelengthCorrection wlc;

            if (System.Math.Abs(spectrum.SpectralResolution - 15.0) < WavelengthDeviation.EPSILON)
                wlc = WavelengthCorrection.CreateFromExtremumDeviation(spectrum, WavelengthCorrection.ReferenceType.NIST15nm, false);
            else
                wlc = WavelengthCorrection.CreateFromExtremumDeviation(spectrum, WavelengthCorrection.ReferenceType.NIST10nm, true);

            DataSet ds = new DataSet(wlc.Wavelengths, wlc.Corrections)
            {
                IsContinuous = false
            };

            return ds;
        }
    }
}