/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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 "gctl/mesh.h"

#include "vector"

int main(int argc, char const *argv[])
{
	try
	{
		// read topography data
		gctl::_2d_vector xyz_content;
		gctl::text_descriptor desc;
		gctl::read_text2vector2d("data/random_topo.xyz", xyz_content, desc);
		// convert to 2D points and values
		gctl::array<gctl::point2dc> topo_posi(xyz_content.size());
		gctl::array<double> topo_val(xyz_content.size());
		for (int i = 0; i < xyz_content.size(); i++)
		{
			topo_posi[i].x = xyz_content.at(i).at(0);
			topo_posi[i].y = xyz_content.at(i).at(1);
			topo_val[i]    = xyz_content.at(i).at(2);
		}
		// destroy vector
		gctl::destroy_vector(xyz_content);

		// griding
		double s_xlen = 15, s_ylen = 20;

		gctl::regular_grid topo_grid("Topography", "null", 101, 101, 0, 0, 10, 10);
		topo_grid.load_data_cloud(topo_posi, topo_val, 15, 20, 0, "random_topo", gctl::ElemData);
		topo_grid.load_data_cloud(topo_posi, topo_val, 25, 25, 0, "random_topo_node", gctl::NodeData);
		topo_grid.gradient("random_topo", "random_topo_dx", gctl::Dx);
		topo_grid.gradient("random_topo", "random_topo_dx2", gctl::Dx, 2);
		topo_grid.gradient("random_topo_node", "random_topo_node_dx", gctl::Dx);
		topo_grid.gradient("random_topo_node", "random_topo_node_dy", gctl::Dy);

		topo_grid.save_gmsh("../data/random_topo_out", gctl::ElemData, gctl::OverWrite, gctl::NotPacked);
		topo_grid.save_gmsh("../data/random_topo_out", gctl::NodeData, gctl::Append, gctl::NotPacked);

		gctl::point2dc sp(20, 20), ep(980, 960);
		gctl::array<gctl::point2dc> profile_posi;
		gctl::array<double> profile_val;
		topo_grid.extract_profile("random_topo", sp, ep, 201, profile_posi, profile_val);

		std::vector<std::string> head_info(1);
		head_info[0] = "x,y,data";
		gctl::save_arrays2text("../data/random_topo_profile.csv", ',', '#', &head_info, nullptr, profile_posi, profile_val);
	}
	catch(std::exception &e)
	{
		GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
	}
	return 0;
}