/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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 .
*
* 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/utility.h"
class ADDNOSIE
{
public:
ADDNOSIE(){}
~ADDNOSIE(){}
int Addnoise(std::string infilename, std::string outfilename, int order);
int set_noise_para(char* noise_para);
int set_delimiter(std::string deli);
private:
double noise_mean_, noise_dev_;
char delimiter_;
std::vector > in_data_;
};
int ADDNOSIE::Addnoise(std::string infilename, std::string outfilename, int order)
{
//读入数据
gctl::text_descriptor desc;
desc.file_name_ = infilename;
desc.delimiter_ = delimiter_;
gctl::read_text2vector2d(desc, in_data_);
//添加噪声
int data_num = in_data_.size();
double tmp_dou;
gctl::array noises(data_num);
noises.random_float(noise_mean_, noise_dev_);
for (int i = 0; i < data_num; i++)
{
tmp_dou = in_data_.at(i).at(order) + noises.at(i);
in_data_.at(i).push_back(tmp_dou);
in_data_.at(i).push_back(noise_dev_);
}
//保存文件
std::vector head_info(1);
head_info[0] = "Generated by 'addnoise' program using " + infilename;
desc.file_name_ = outfilename;
gctl::save_vector2d2text(desc, in_data_);
return 0;
}
int ADDNOSIE::set_delimiter(std::string deli)
{
delimiter_ = deli.c_str()[0];
return 0;
}
int ADDNOSIE::set_noise_para(char* noise_para)
{
if (2 != sscanf(noise_para,"%lf/%lf",&noise_mean_,&noise_dev_))
throw noise_para;
else return 0;
}
int main(int argc, char *argv[])
{
const char* pro_name = "addnoise";
const char* pro_info = "1.0 - Read data table and add Gaussian noises to a data column. \
This program is a toolkit of the GCTL package. The GCTL comes with ABSOLUTE NO WARRANTY. \
Please see instructions or contact the author for more information.";
int col_num = -1;
char infilename[1024] = "NULL";
char outfilename[1024] = "NULL";
char noise_para[1024] = "NULL";
char col_delimiter[1024] = " ";
// option format
// char* info, char* format, bool manda
static struct gctl::option_info long_opts_help[] =
{
{"Input data table.", "", true},
{"Output filename.", "", true},
{"Column index of a data set which noises are going to added into.", "", true},
{"Mean value and standard deviation of the Gaussian noises.", "/", true},
{"Delimiter of the input data table. The default is space.", "", false},
{"Show help information.", 0, false},
{0, 0, 0}
};
static struct option long_opts[] =
{
{"input-file", required_argument, NULL, 'i'},
{"output-file", required_argument, NULL, 'o'},
{"data-column", required_argument, NULL, 'c'},
{"noise-level", required_argument, NULL, 'n'},
{"delimiter", required_argument, NULL, 'd'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
if (argc == 1){
gctl::display_logo(std::clog);
gctl::getopt_long_help(long_opts, long_opts_help, pro_name, pro_info);
return 0;
}
int curr;
while(1)
{
int optIndex = 0;
curr = getopt_long(argc, argv, "hi:o:c:n:d:", long_opts, &optIndex);
if (curr == -1) break;
switch (curr)
{
case 'h': //显示帮助信息
gctl::display_logo(std::clog);
gctl::getopt_long_help(long_opts, long_opts_help, pro_name, pro_info);
return 0;
case 'i':
sscanf(optarg,"%s",infilename);
break;
case 'o':
sscanf(optarg,"%s",outfilename);
break;
case 'c':
sscanf(optarg,"%d",&col_num);
break;
case 'n':
sscanf(optarg,"%s",noise_para);
break;
case 'd':
sscanf(optarg,"%s",col_delimiter);
break;
case '?':
gctl::getopt_long_help(long_opts, long_opts_help, pro_name, " - Unknown options. Please see the help information below.");
break;
default:
abort();
}
}
// check for mandatory option(s)
if (!strcmp(infilename, "NULL") || !strcmp(outfilename, "NULL") ||
!strcmp(noise_para, "NULL") || col_num == -1)
{
GCTL_ShowWhatError("Missing arguments for mandatory option(s).", GCTL_ERROR_ERROR,
0, "See tips below. Or use -h option to see the full instruction.", 0);
if (!strcmp(infilename, "NULL"))
gctl::getopt_long_option_info('i', long_opts, long_opts_help);
if (!strcmp(outfilename, "NULL"))
gctl::getopt_long_option_info('o', long_opts, long_opts_help);
if (!strcmp(noise_para, "NULL"))
gctl::getopt_long_option_info('n', long_opts, long_opts_help);
if (col_num == -1)
gctl::getopt_long_option_info('c', long_opts, long_opts_help);
return 0;
}
ADDNOSIE as;
as.set_delimiter(col_delimiter);
try {as.set_noise_para(noise_para);}
catch(const char* err_str){GCTL_ShowWhatError("Invalid noise parameter: ", GCTL_ERROR_ERROR, err_str, 0, 0);}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}
try {as.Addnoise(infilename, outfilename, col_num);}
catch(const char* err_str){GCTL_ShowWhatError(err_str, GCTL_ERROR_ERROR, infilename, 0, 0);}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}
return 0;
}