131 lines
5.0 KiB
C++
131 lines
5.0 KiB
C++
/********************************************************
|
|
* ██████╗ ██████╗████████╗██╗
|
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
|
* ██║ ███╗██║ ██║ ██║
|
|
* ██║ ██║██║ ██║ ██║
|
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
|
* 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/io.h"
|
|
#include "gctl/utility.h"
|
|
|
|
int main(int argc, char *argv[]) try
|
|
{
|
|
gctl::flags_parser fp;
|
|
fp.set_proname("grd2xyz");
|
|
fp.set_proinfo("Convert a Golden Software Surfer 6|7 grid file to a text file. \
|
|
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.");
|
|
fp.add_opt('i', "in-file", required_argument, NULL, "Input grid file.", "<grid-name>", true);
|
|
fp.add_opt('o', "out-file", required_argument, NULL, "Output file name. The input file name will be used if this option was not set.", "<file-name>", false);
|
|
fp.add_opt('f', "format", required_argument, NULL, "Input grid format. 'ascii6' for Surfer6 ASCII format (default), 'binary6' for Surfer6 binary format and 'binary7' for Surfer7 binary format.", "<type>", false);
|
|
fp.add_opt('u', "upper-left", no_argument, NULL, "Output the grid data from the upper-left corner to the down-right corner. The default is from the down-left corner to the upper-right corner.", 0, false);
|
|
fp.add_opt('h', "help", no_argument, NULL, "Show help information.", 0, false);
|
|
fp.configure(argc, argv);
|
|
|
|
if (argc == 1 || fp.set_opt('h'))
|
|
{
|
|
fp.show_help_page();
|
|
return 0;
|
|
}
|
|
|
|
std::string in_name, out_name, type_str;
|
|
fp.get_argv({'i', 'o', 'f'}, {&in_name, &out_name, &type_str});
|
|
// 查看是否通过强制参数检查
|
|
if (!fp.pass_mandatory()) return 0;
|
|
|
|
if (out_name == "NULL") out_name = in_name.substr(0, in_name.length()-4);
|
|
if (type_str == "NULL") type_str = "ascii6";
|
|
|
|
bool from_tl = false;
|
|
if (fp.set_opt('u')) from_tl = true;
|
|
|
|
gctl::array<double> data;
|
|
int xnum, ynum;
|
|
double xmin, xmax, ymin, ymax, zmin, zmax, dx, dy, blank_val;
|
|
|
|
if (type_str == "ascii6")
|
|
{
|
|
gctl::read_surfer6_grid(in_name, data, xnum, ynum, xmin, xmax, ymin, ymax, zmin, zmax);
|
|
|
|
dx = (xmax - xmin)/(xnum - 1);
|
|
dy = (ymax - ymin)/(ynum - 1);
|
|
}
|
|
else if (type_str == "binary6")
|
|
{
|
|
gctl::read_surfer6_grid(in_name, data, xnum, ynum, xmin, xmax, ymin, ymax, zmin, zmax, gctl::Surfer6Binary);
|
|
|
|
dx = (xmax - xmin)/(xnum - 1);
|
|
dy = (ymax - ymin)/(ynum - 1);
|
|
}
|
|
else if (type_str == "binary7")
|
|
{
|
|
gctl::read_surfer7_grid(in_name, data, xnum, ynum, xmin, ymin, dx, dy, zmin, zmax, blank_val);
|
|
|
|
xmax = xmin + dx*(xnum - 1);
|
|
ymax = ymin + dy*(ynum - 1);
|
|
}
|
|
else throw std::runtime_error("Invalid grid file format.");
|
|
|
|
// 添加网格节点坐标
|
|
gctl::_2d_vector out_data;
|
|
out_data.resize(3);
|
|
for (size_t i = 0; i < 3; i++)
|
|
{
|
|
out_data[i].resize(xnum*ynum);
|
|
}
|
|
|
|
for (int j = 0; j < ynum; j++)
|
|
{
|
|
for (int i = 0; i < xnum; i++)
|
|
{
|
|
out_data[0][i+j*xnum] = xmin + i*dx;
|
|
out_data[1][i+j*xnum] = ymin + j*dy;
|
|
out_data[2][i+j*xnum] = data[i+j*xnum];
|
|
}
|
|
}
|
|
|
|
if (from_tl)
|
|
{
|
|
double tmp_data;
|
|
int half_y = ynum/2;
|
|
// swap y corrdinates and data
|
|
for (size_t j = 0; j < half_y; j++)
|
|
{
|
|
for (size_t i = 0; i < xnum; i++)
|
|
{
|
|
tmp_data = out_data[1][i+j*xnum]; out_data[1][i+j*xnum] = out_data[1][i+(ynum - 1 - j)*xnum]; out_data[1][i+(ynum - 1 - j)*xnum] = tmp_data;
|
|
tmp_data = out_data[2][i+j*xnum]; out_data[2][i+j*xnum] = out_data[2][i+(ynum - 1 - j)*xnum]; out_data[2][i+(ynum - 1 - j)*xnum] = tmp_data;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 保存文本文件
|
|
gctl::text_descriptor desc;
|
|
desc.file_name_ = out_name;
|
|
gctl::save_vector2d2text(desc, out_data, gctl::ColMajor);
|
|
return 0;
|
|
}
|
|
catch(std::exception &e)
|
|
{
|
|
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
|
|
} |