/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2022  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 "gctl/core.h"
#include "gctl/io.h"
#include "../lib/potential.h"

using namespace gctl;

int main(int argc, char const *argv[]) try
{
    array<IGRF_para> IGRFs;
    read_IGRF_table("data/IGRFs/Gard21_ECM1_EMM_output", IGRFs);

    std::cout << IGRFs.size() << "\n";
    std::cout <<  IGRFs.front().Year << "-" <<  IGRFs.front().Month << "-" <<  IGRFs.front().Day << " " 
        <<  IGRFs.front().CSystem << " " <<  IGRFs.front().AltiCode << " " <<  IGRFs.front().Altitude << " " 
        << IGRFs.front().X_nT << " " << IGRFs.front().Y_nT << " " << IGRFs.front().Z_nT << " " << IGRFs.front().dF_nT << "\n";
    std::cout <<  IGRFs.back().Year << "-" <<  IGRFs.back().Month << "-" <<  IGRFs.back().Day << " " 
        <<  IGRFs.back().CSystem << " " <<  IGRFs.back().AltiCode << " " <<  IGRFs.back().Altitude << " " 
        << IGRFs.back().X_nT << " " << IGRFs.back().Y_nT << " " << IGRFs.back().Z_nT << " " << IGRFs.back().dF_nT << "\n";

    gmshio gio;
    gio.init_file("data/IGRFs/Gard21_ECM1.msh", Input);
    gio.init_file("data/IGRFs/Gard21_ECM1_EMM.msh", Output);
    gio.set_packed(NotPacked, Input);
    gio.set_packed(NotPacked, Output);

    array<vertex3dc> nodes;
    array<triangle> eles;
    array<double> top, btm, bz;
    gio.read_mesh(eles, nodes);
    gio.read_data(top, "crust_top (m)");
    gio.read_data(btm, "curie_btm (m)");
    gio.read_data(bz,  "Bz (nT)");

    array<double> Bx(eles.size()), By(eles.size()), Bz(eles.size()), F(eles.size());
    array<point3ds> obs_ps(eles.size());
    array<point3dc> loc_mag(eles.size());
    for (size_t i = 0; i < eles.size(); i++)
    {
        obs_ps[i] = eles[i].center().c2s();

        loc_mag[i].x = IGRFs[i].Z_nT; // radial
        loc_mag[i].y = IGRFs[i].Y_nT; // latitudinal
        loc_mag[i].z = IGRFs[i].X_nT; // longitudinal

        Bx[i] = IGRFs[i].X_nT;
        By[i] = IGRFs[i].Y_nT;
        Bz[i] = IGRFs[i].Z_nT;
        F[i]  = IGRFs[i].F_nT;
    }

    array<point3dc> abs_mag;
    geomag_local2Cartesian(abs_mag, loc_mag, obs_ps);

    gio.save_mesh(eles, nodes);
    gio.save_data("crust_top (m)", top, NodeData);
    gio.save_data("curie_btm (m)", btm, NodeData);
    gio.save_data("Bz (nT)", bz, ElemData);
    gio.save_data("EMM_B", abs_mag, ElemData);
    //gio.save_data("EMM_Bx", Bx, ElemData);
    //gio.save_data("EMM_By", By, ElemData);
    //gio.save_data("EMM_Bz", Bz, ElemData);
    //gio.save_data("EMM_F",  F,  ElemData);
    return 0;
}
catch (std::exception &e)
{
	GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}