/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  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 "meshdata_tensor.h"

gctl::meshdata_tensor::meshdata_tensor(std::string in_name, mesh_data_type_e in_type, 
	int d_size, bool if_output, gctl::tensor init_val) : meshdata::meshdata(in_name, in_type, if_output)
{
	if (d_size <= 0)
	{
		throw std::runtime_error("[gctl::meshdata_tensor] Invalid data size.");
	}

	valtype = Tensor;
	datval.resize(d_size, init_val);

	//std::clog << "A new meshdata_tensor object is created." << std::endl;
}

gctl::meshdata_tensor::~meshdata_tensor()
{
	datval.clear();

	//std::clog << "A meshdata_tensor object is destroyed." << std::endl;
}

gctl::array<gctl::tensor> &gctl::meshdata_tensor::get_datval()
{
	return datval;
}

void gctl::meshdata_tensor::show_stats(std::ostream &os)
{
	double m[9], s[9], r[9];
	array<double> t(datval.size());

	for (size_t i = 0; i < 3; i++)
	{
		for (size_t j = 0; j < 3; j++)
		{
			for (size_t n = 0; n < datval.size(); n++)
			{
				t[n] = datval[n].at(i, j);
			}

			m[j + i*3] = t.mean();
			s[j + i*3] = t.std();
			r[j + i*3] = t.rms();
		}
	}
	
	os << "Mean = (" << m[0];
	for (size_t i = 1; i < 9; i++)
	{
		os << "," << m[i];
	}
	 
	os << ")\nSTD = (" << s[0];
	for (size_t i = 1; i < 9; i++)
	{
		os << "," << s[i];
	}

	os << ")\nRMS = (" << r[0];
	for (size_t i = 1; i < 9; i++)
	{
		os << "," << r[i];
	}
	os << ")" << std::endl;
}

void *gctl::meshdata_tensor::get_datval_ptr()
{
	return &datval;
}

void gctl::meshdata_tensor::load_binary(std::ifstream &infile)
{
	infile.read((char*) datval.get(), sizeof(gctl::tensor)*datval.size());
	return;
}

void gctl::meshdata_tensor::save_binary(std::ofstream &outfile)
{
	// 我们首先输出数据类型，赋值类型与数据长度 这三个值都是整形变量
	// 我们在读入二进制文件时会在函数外先读入这三个值以新建数据对象
	outfile.write((char*)&dattype, sizeof(int));
	outfile.write((char*)&valtype, sizeof(int));
	// 输出数据对象的名称
	int name_size = datname.size();
	outfile.write((char*)&name_size, sizeof(int));
	outfile.write((char*)datname.c_str(), name_size);
	// 输出数据
	outfile.write((char*) datval.get(), sizeof(gctl::tensor)*datval.size());
	return;
}