/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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 .
*
* 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_scalar.h"
gctl::meshdata_scalar::meshdata_scalar(std::string in_name, mesh_data_type_e in_type,
int d_size, bool if_output, double init_val) : meshdata::meshdata(in_name, in_type, if_output)
{
if (d_size <= 0)
{
throw std::runtime_error("[gctl::meshdata_scalar] Invalid data size.");
}
nan_val = GCTL_BDL_MAX;
valtype = Scalar;
datval.resize(d_size, init_val);
//std::clog << "A new meshdata_scalar object is created." << std::endl;
}
gctl::meshdata_scalar::~meshdata_scalar()
{
datval.clear();
//std::clog << "A meshdata_scalar object is destroyed." << std::endl;
}
gctl::array &gctl::meshdata_scalar::get_datval()
{
return datval;
}
void gctl::meshdata_scalar::set_nan(double nan)
{
nan_val = nan;
return;
}
void gctl::meshdata_scalar::show_stats(std::ostream &os)
{
int vn = 0;
for (size_t i = 0; i < datval.size(); i++)
{
if (fabs(datval[i] - nan_val) > 1e-10) vn++;
}
array tmp(vn);
vn = 0;
for (size_t i = 0; i < datval.size(); i++)
{
if (fabs(datval[i] - nan_val) > 1e-10)
{
tmp[vn] = datval[i];
vn++;
}
}
os << "Mean = " << tmp.mean() << ", STD = " << tmp.std() << ", RMS = " << tmp.rms() << std::endl;
return;
}
void *gctl::meshdata_scalar::get_datval_ptr()
{
return &datval;
}
void gctl::meshdata_scalar::load_binary(std::ifstream &infile)
{
infile.read((char*) &nan_val, sizeof(double));
infile.read((char*) datval.get(), sizeof(double)*datval.size());
return;
}
void gctl::meshdata_scalar::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*) &nan_val, sizeof(double));
outfile.write((char*) datval.get(), sizeof(double)*datval.size());
return;
}