/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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"
#include "iostream"

int main(int argc, char const *argv[]) try
{
	// set up observation parameters and block parameters
	double lon, lat;
	gctl::array<gctl::point3ds> obes(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;
			obes[i*101+j].lon = lon;
			obes[i*101+j].lat = lat;
			obes[i*101+j].rad = GCTL_Earth_Radius + 40000.0;
		}
	}

	gctl::array<gctl::mag_tesseroid> tesses(1);
	gctl::array<gctl::magtess_para> mtess(1);
	gctl::_1d_array inclina(1, 60.0), declina(1, 20.0);

	tesses[0].set(GCTL_Earth_Radius - 21000.0, GCTL_Earth_Radius - 1000.0, 24, 26, 14, 16);

	gctl::callink_magnetic_para_earth_sph(tesses, mtess, 60, 20);
	gctl::set_geomag_angles(mtess, inclina, declina);

	gctl::array<double> sus(1, 0.08);
	gctl::array<double> data_x, data_y, data_z, deltaT;

	magobser(data_z, tesses, obes, sus, gctl::Za, 4, gctl::ShortMsg);
	gctl::save_netcdf_grid("data/tesseroid_mag", data_z, 101, 101, 20.0, 0.1, 10.0, 0.1, "x", "y", "Za");

	magobser(data_x, tesses, obes, sus, gctl::Hax, 4, gctl::ShortMsg);
	gctl::append_netcdf_grid("data/tesseroid_mag", data_x, "x", "y", "Hax");

	magobser(data_y, tesses, obes, sus, gctl::Hay, 4, gctl::ShortMsg);
	gctl::append_netcdf_grid("data/tesseroid_mag", data_y, "x", "y", "Hay");

	magobser(deltaT, tesses, obes, sus, gctl::DeltaT, 4, gctl::ShortMsg);
	gctl::append_netcdf_grid("data/tesseroid_mag", deltaT, "x", "y", "DeltaT");

	gctl::array<gctl::point3dc> obsgrad(obes.size());
	gctl::_1d_array obs_inclina(obes.size(), 60.0), obs_declina(obes.size(), 20.0);

	for (size_t i = 0; i < obsgrad.size(); i++)
	{
		obsgrad[i].x = data_z[i];
		obsgrad[i].y = data_x[i];
		obsgrad[i].z = data_y[i];
	}

	magnetic_components2deltaT_sph(obsgrad, obs_inclina, obs_declina, deltaT);
	gctl::append_netcdf_grid("data/tesseroid_mag", deltaT, "x", "y", "DeltaT2");

	return 0;
}
catch (std::exception &e)
{
	GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}