/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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 ele17ctronic and paper mail.
 ******************************************************/

#include "gctl/io.h"
#include "../lib/potential.h"

using namespace gctl;

int main(int argc, char const *argv[])
{
	array<vertex3dc> node;
	array<mag_tetrahedron_ren17> ele17;
	array<mag_tetrahedron> ele;

    gmshio fio;
    fio.init_file("data/tesseroid/shell3d", gctl::Input);
    fio.set_packed(gctl::NotPacked, gctl::Input);
	fio.read_node(node);
	fio.read_element(ele17, node);
	fio.read_element(ele, node);

	array<magtet_para_ren17> mag_para17;
	array<magtet_para> mag_para;

	// test for using magnetic susceptibility
	array<double> sus(ele17.size(), 0.08);
	array<point3dc> mag_vec;
	callink_magnetic_para_earth_sph(ele17, mag_para17, 60.0, 20.0, &mag_vec);
	callink_magnetic_para_earth_sph(ele, mag_para, 60.0, 20.0);

	//fio.init_file("out", gctl::Output);
	//fio.set_packed(gctl::NotPacked, gctl::Output);
	//fio.save_mesh(ele17, node);
	//fio.save_data("mag_vec", mag_vec, gctl::ElemData);

	// 设置观测点位
	double lon, lat;
	array<point3ds> obsp(101*101);
    for (int i = 0; i < 101; i++)
	{
		lat = 10.0 + 0.1*i;
		for (int j = 0; j < 101; j++)
		{
			lon = 20.0 + 0.1*j;
			obsp[i*101+j].lon = lon;
			obsp[i*101+j].lat = lat;
			obsp[i*101+j].rad = GCTL_Earth_Radius + 40000.0;
		}
	}

	// 正演计算
	array<double> obsval(obsp.size());
	array<point3dc> obsgrad, obsgrad2;
	magobser(obsgrad, ele17, obsp, sus, ShortMsg);

	//array<tensor> obstensor;
	//magobser(obstensor, ele17, obsp, sus, ShortMsg);

	// 保存网格
	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].x;
	}
	gctl::save_netcdf_grid("out_magobser_tetra_s", obsval, 101, 101, 20.0, 0.1, 10.0, 0.1, "x", "y", "Br_Ren");
    //system("gmtsph-regional -i out_magobser_tetra_s_Br.nc -c \"turbo -Z\" -u nT");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].y;
	}
    //system("gmtsph-regional -i out_magobser_tetra_s_Bt.nc -c \"turbo -Z\" -u nT");
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Bt_Ren");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].z;
	}
    //system("gmtsph-regional -i out_magobser_tetra_s_Bp.nc -c \"turbo -Z\" -u nT");
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Bp_Ren");

	magobser(obsgrad2, ele, obsp, sus, ShortMsg);

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad2[i].x;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Br");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad2[i].y;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Bt");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad2[i].z;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Bp");


	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].x - obsgrad2[i].x;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Diff_Br");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].y - obsgrad2[i].y;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Diff_Bt");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].z - obsgrad2[i].z;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Diff_Bp");

/*
	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obstensor[i][0][0];
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Brr");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obstensor[i][0][1];
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Brt");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obstensor[i][0][2];
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Brp");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obstensor[i][1][1];
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Btt");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obstensor[i][1][2];
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Btp");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obstensor[i][2][2];
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "Bpp");
*/
	gctl::_1d_array deltaT;
	gctl::_1d_array inclina(obsgrad2.size(), 60.0), denclina(obsgrad2.size(), 20.0);

	magnetic_components2deltaT_sph(obsgrad2, inclina, denclina, deltaT);
	gctl::append_netcdf_grid("out_magobser_tetra_s", deltaT, "x", "y", "DeltaT");
/*
	gctl::array<gctl::point3dc> deltaTs;
	gctl::magnetic_tensors2deltaTs_sph(obstensor, inclina, denclina, deltaTs);

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = deltaTs[i].x;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "DeltaTr");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = deltaTs[i].y;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "DeltaTt");

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = deltaTs[i].z;
	}
	gctl::append_netcdf_grid("out_magobser_tetra_s", obsval, "x", "y", "DeltaTp");
*/
	return 0;
}