/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "emd.h"

#ifdef GCTL_EEMD

void libeemd_err_hander(libeemd_error_code err_code)
{
    if (err_code != EMD_SUCCESS)
    {
        switch (err_code)
        {
		case EMD_INVALID_ENSEMBLE_SIZE :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Invalid ensemble size (zero or negative)");
			break;
		case EMD_INVALID_NOISE_STRENGTH :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Invalid noise strength (negative)");
			break;
		case EMD_NOISE_ADDED_TO_EMD :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Positive noise strength but ensemble size is one (regular EMD)");
			break;
		case EMD_NO_NOISE_ADDED_TO_EEMD :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Ensemble size is more than one (EEMD) but noise strength is zero");
			break;
		case EMD_NO_CONVERGENCE_POSSIBLE :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Stopping criteria invalid: would never converge");
			break;
		case EMD_NOT_ENOUGH_POINTS_FOR_SPLINE :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Spline evaluation tried with insufficient points");
			break;
		case EMD_INVALID_SPLINE_POINTS :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Spline evaluation points invalid");
			break;
		case EMD_GSL_ERROR :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Error reported by GSL library");
			break;
		default :
			throw std::runtime_error("[GCTL] gathered from [libeemd] Error code with unknown meaning. Please file a bug!");
        }
    }
    return;
}

void gctl::eemd1d(const array<double> &in, matrix<double> &out, size_t M, 
    unsigned int ensemble_size, double noise_strength, unsigned int S_number, 
    unsigned int num_siftings, unsigned long int rng_seed)
{
    if (M == 0) M = emd_num_imfs(in.size());
    out.resize(M, in.size());

    libeemd_err_hander(eemd(in.get(), in.size(), out.get(), M, ensemble_size, noise_strength, S_number, num_siftings, rng_seed));
    return;
}

void gctl::ceemdan1d(const array<double> &in, matrix<double> &out, size_t M, 
    unsigned int ensemble_size, double noise_strength, unsigned int S_number, 
    unsigned int num_siftings, unsigned long int rng_seed)
{
    if (M == 0) M = emd_num_imfs(in.size());
    out.resize(M, in.size());

    libeemd_err_hander(ceemdan(in.get(), in.size(), out.get(), M, ensemble_size, noise_strength, S_number, num_siftings, rng_seed));
    return;
}

#endif // GCTL_EEMD