/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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/io.h"
#include "gctl/geometry.h"
#include "../lib/potential.h"

using namespace gctl;

bool clockwise(const triangle &t)
{
	if (dot(cross(*t.vert[1] - *t.vert[0], *t.vert[2] - *t.vert[0]), *t.vert[0]) < 0) return true;
	else return false;
}

int main(int argc, char const *argv[]) try
{
	array<vertex3dc> top_node, btm_node;
	array<magcone_ren17> top_ele, btm_ele;

    gmshio fio;
    fio.init_file("data/stt/sph_rect.msh", gctl::Input);
    fio.set_packed(gctl::NotPacked, gctl::Input);
    fio.read_mesh(top_ele, top_node);
	fio.read_mesh(btm_ele, btm_node);

	// 确定三角形顶点排序
	triangle t;
	vertex3dc *v = nullptr;
	for (size_t i = 0; i < top_ele.size(); i++)
	{
		t.set(*top_ele[i].vert[0], *top_ele[i].vert[1], *top_ele[i].vert[2]);
		if (clockwise(t))
		{
			v = top_ele[i].vert[0]; top_ele[i].vert[0] = top_ele[i].vert[1]; top_ele[i].vert[1] = v;
			v = btm_ele[i].vert[0]; btm_ele[i].vert[0] = btm_ele[i].vert[1]; btm_ele[i].vert[1] = v;
		}
	}

	vertex3dc origin(point3dc(0.0, 0.0, 0.0), top_node.size());
	for (size_t i = 0; i < top_ele.size(); i++)
	{
		top_ele[i].set_origin(origin);
		btm_ele[i].set_origin(origin);
	}

	for (size_t i = 0; i < top_node.size(); i++)
    {
		top_node[i].set2module(6371200);
        btm_node[i].set2module(6371200 - 26500);
    }

	mag_dipole md;
    md.M = 7.61365e+22;
    md.n.set(point3dc(0.0, 0.0, 1.0));

	point3dc cen_c;
	array<point3dc> mag_B(top_ele.size());
	for (size_t i = 0; i < top_ele.size(); i++)
	{
		cen_c = 1.0/6.0*(*top_ele[i].vert[0] + *top_ele[i].vert[1] + *top_ele[i].vert[2] + 
			*btm_ele[i].vert[0] + *btm_ele[i].vert[1] + *btm_ele[i].vert[2]);
		mag_B[i] = magkernel_single(md, cen_c);		
	}

	// 计算磁体参数
	array<magcone_para_ren17> top_para, btm_para;
    array<double> sus(top_ele.size(), 0.01);
	callink_magnetic_para(top_ele, top_para, mag_B);
	callink_magnetic_para(btm_ele, btm_para, mag_B);

	// 设置观测点位
	array<point3ds> obsp;
	grid_points_2d(obsp, 10.0, 55.0, 20.0, 65.0, 0.25, 0.25, 6371200.0 + 100000);

	// 正演计算
	array<point3dc> obsgrad;

	// 正演磁分量数据
	set_magcone_ren17_tolerance(1e-30);
	magobser(obsgrad, top_ele, btm_ele, obsp, sus, ShortMsg);

	geodsv_io fout;
	fout.init_table(obsp.size(), 6);
	fout.set_column_names({"rad", "lon", "lat", "Br", "Bt", "Bp"});
	fout.fill_column_point3ds(obsp, "rad", "lon", "lat");
	fout.fill_column_point3dc(obsgrad, "Br", "Bt", "Bp", 12);
	fout.save_csv("data/stt/mag_obs");

	system("xyz2nc -t data/stt/mag_obs.csv -g data/stt/mag_obs_ren17.nc -r 10/55/20/65 -i 0.25/0.25 -c 1,2,3,4,5 -l x,y,Br,Bt,Bp -d ','");
	return 0;
}
catch (std::exception &e)
{
	GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}