This commit is contained in:
张壹 2025-02-09 13:12:55 +08:00
parent 428442629d
commit 86e0dcd50d
5 changed files with 51 additions and 90 deletions

View File

@ -30,5 +30,5 @@ add_example(text_io_ex OFF)
add_example(getoption_ex OFF)
add_example(process_ex OFF)
add_example(array_ex OFF)
add_example(gmt_ex ON)
add_example(gmt_ex OFF)
add_example(gnuplot_ex OFF)

View File

@ -26,7 +26,6 @@
******************************************************/
#include "../lib/graphic/gmt.h"
#include "../lib/io/netcdf_io.h"
int main(int argc, char *argv[])
{
@ -40,7 +39,6 @@ int main(int argc, char *argv[])
*/
// example 2
gctl::array<double> data(51*41);
double dist;
@ -53,23 +51,13 @@ int main(int argc, char *argv[])
}
}
gctl::save_netcdf_grid("image_plot", data, 41, 51, 0.0, 1.0, 0.0, 1.0, "x", "y", "z");
gt.begin_session("image_plot", "eps");
gt.call_module("set", "FONT_ANNOT_PRIMARY=10.5p,Times-Roman,black");
gt.begin_session("image_plot", "png,eps");
gt.call_module("set", "FONT_ANNOT_PRIMARY=10.5p,Times-Roman,black \
MAP_FRAME_PEN=thinnest,black \
MAP_GRID_PEN_PRIMARY=thinnest,black \
MAP_TICK_PEN_PRIMARY=thinnest,black \
MAP_TICK_LENGTH_PRIMARY=1p/0.5p \
MAP_TITLE_OFFSET=7.5p \
MAP_GRID_CROSS_SIZE_PRIMARY=2p \
FONT_LABEL=10.5p,Times-Roman,black \
MAP_LABEL_OFFSET=5p \
MAP_ANNOT_OFFSET_PRIMARY=2.5p \
MAP_FRAME_AXES=WesNZ");
std::string gname = gt.create_grid(data, 41, 51, 0.0, 1.0, 0.0, 1.0);
gt.call_module("grd2cpt", "image_plot.nc -R0/40/0/50 -Crainbow -Z -D");
gt.call_module("grdimage", "image_plot.nc -R0/40/0/50 -JX1.5i/1.5i -X0.5i -Y0.5i -Bxag+l\"x (m)\" -Byag+l\"y (m)\"");
gt.call_module("grd2cpt", gname + " -R0/40/0/50 -Crainbow -Z -D");
gt.call_module("grdimage", gname + " -R0/40/0/50 -JX1.5i/1.5i -X0.5i -Y0.5i -Bxag+l\"x (m)\" -Byag+l\"y (m)\"");
gt.call_module("psscale", "-Bxa -By+lm -Dx0.1i/-0.2i+w1.3i/0.05i+h");
gt.end_session();
gt.save_session("image_plot");

View File

@ -29,34 +29,32 @@
gctl::gmt::gmt()
{
seesion_ok_ = false;
GMT_API_ = nullptr;
//GRID_ = nullptr;
//std::fill(gmt_gridname_, gmt_gridname_ + sizeof(gmt_gridname_), '\0');
session_name_ = "";
buffer_.clear();
tmpfiles_.clear();
}
gctl::gmt::~gmt()
{
//if (GRID_!= nullptr && GMT_API_!= nullptr)
//{
// GMT_Close_VirtualFile (GMT_API_, gmt_gridname_);
// GMT_Destroy_Data(GMT_API_, GRID_);
// GRID_ = nullptr;
//}
if (GMT_API_ != nullptr)
{
GMT_Destroy_Session(GMT_API_);
GMT_API_ = nullptr;
seesion_ok_ = false;
session_name_ = "";
}
if (!buffer_.empty()) buffer_.clear();
if (!tmpfiles_.empty()) tmpfiles_.clear();
}
void gctl::gmt::begin_session(const std::string& pic_name,
const std::string& pic_type, const std::string& name)
{
if (!buffer_.empty()) buffer_.clear();
session_name_ = pic_name;
GMT_API_ = GMT_Create_Session(name.c_str(), 2U, 0, nullptr);
if (GMT_API_ == nullptr) throw std::runtime_error("[gctl::gmt] Fail to begin GMT session.");
@ -69,10 +67,12 @@ void gctl::gmt::begin_session(const std::string& pic_name,
cmd = "begin " + cmd;
buffer_.push_back(cmd);
}
seesion_ok_ = true;
return;
}
void gctl::gmt::end_session(bool show)
void gctl::gmt::end_session(bool show, bool clear_tmp)
{
std::string cmd = "";
if (show) cmd = "show";
@ -85,18 +85,19 @@ void gctl::gmt::end_session(bool show)
buffer_.push_back(cmd);
}
//if (GRID_!= nullptr && GMT_API_!= nullptr)
//{
// GMT_Close_VirtualFile (GMT_API_, gmt_gridname_);
// GMT_Destroy_Data(GMT_API_, GRID_);
// GRID_ = nullptr;
//}
if (clear_tmp)
{
for (auto& file : tmpfiles_) remove(file.c_str());
}
if (GMT_API_ != nullptr)
{
GMT_Destroy_Session(GMT_API_);
GMT_API_ = nullptr;
}
seesion_ok_ = false;
session_name_ = "";
return;
}
@ -119,7 +120,7 @@ void gctl::gmt::save_session(const std::string& filename)
return;
}
void gctl::gmt::call_module(const std::string& module, const std::string& cmd, const std::string& opt)
void gctl::gmt::call_module(const std::string& module, const std::string& cmd)
{
if (GMT_Call_Module(GMT_API_, module.c_str(), GMT_MODULE_CMD, (char*) cmd.c_str()) != GMT_NOERROR)
throw std::runtime_error("[gctl::gmt] Fail to execute GMT command.");
@ -130,46 +131,18 @@ void gctl::gmt::call_module(const std::string& module, const std::string& cmd, c
}
}
/*
std::string gctl::gmt::create_virtual_grid(const array<double> &data,
double xmin, double xmax, double ymin, double ymax, int xnum, int ynum)
std::string gctl::gmt::create_grid(const array<double> &data, int xnum, int ynum,
double xmin, double dx, double ymin, double dy)
{
double wesn[4] = {xmin, xmax, ymin, ymax};
double inc[2] = {(xmax-xmin)/(xnum-1), (ymax-ymin)/(ynum-1)};
// create an empty data container of GMT_GRID type
// forcedly convert pointer type (C++ standard)
// you need to destroy the data later
GRID_ = reinterpret_cast<GMT_GRID*>(
GMT_Create_Data(GMT_API_, GMT_IS_GRID, GMT_IS_SURFACE,
GMT_CONTAINER_AND_DATA, nullptr, &wesn[0], &inc[0],
GMT_GRID_NODE_REG, -1, nullptr));
if (seesion_ok_ == false)
throw std::runtime_error("[gctl::gmt] GMT session is not started.");
// manipulate grid data
//double *x_coord = GMT_Get_Coord(GMT_API_, GMT_IS_GRID, GMT_X, GRID_);
//double *y_coord = GMT_Get_Coord(GMT_API_, GMT_IS_GRID, GMT_Y, GRID_);
if (xnum*ynum != data.size())
throw std::runtime_error("[gctl::gmt] The size of data does not match the grid size.");
int idx;
double minz = 1e+30, maxz = -1e+30;
for (int i = 0; i < GRID_->header->n_rows; i++)
{
for (int j = 0; j < GRID_->header->n_columns; j++)
{
idx = GMT_Get_Index(GMT_API_, GRID_->header, i, j);
GRID_->data[idx] = data[j+(ynum-1-i)*xnum];
std::string gridname = session_name_ + "_grid_" + std::to_string(tmpfiles_.size() + 1) + ".nc";
gctl::save_netcdf_grid(gridname, data, xnum, ynum, xmin, dx, ymin, dy);
if (GRID_->data[idx] < minz) minz = GRID_->data[idx];
if (GRID_->data[idx] > maxz) maxz = GRID_->data[idx];
}
}
GRID_->header->z_min = minz;
GRID_->header->z_max = maxz;
// load the grid to a virtual file for plotting
GMT_Open_VirtualFile(GMT_API_, GMT_IS_GRID, GMT_IS_SURFACE, GMT_IN, GRID_, gmt_gridname_);
// get string type of grid name
std::string grid_name_str = gmt_gridname_;
return grid_name_str;
}
*/
tmpfiles_.push_back(gridname);
return gridname;
}

View File

@ -36,6 +36,7 @@
#include "gmt/gmt.h"
#include "../utility/stream.h"
#include "../core/array.h"
#include "../io/netcdf_io.h"
#ifndef GMT_VF_LEN
#define GMT_VF_LEN 16
@ -70,8 +71,9 @@ namespace gctl
* @brief Destroy a session object
*
* @param show Whether to show the result after finish the plotting.
* @param clear_tmp Whether to clear the temporary files.
*/
void end_session(bool show = true);
void end_session(bool show = false, bool clear_tmp = true);
/**
* @brief Save the session to a file.
@ -85,35 +87,33 @@ namespace gctl
*
* @param module Name of the module.
* @param cmd Module command.
* @param opt Extra options.
*/
void call_module(const std::string& module, const std::string& cmd, const std::string& opt = "");
void call_module(const std::string& module, const std::string& cmd);
/**
* @brief Create a virtual grid file that could be used for plotting.
*
* @brief Create a temporary grid file that could be used for plotting.
*
* @param data Data array.
* @param xmin minimal x coordinate of the data.
* @param xmax maximal x coordinate of the data.
* @param ymin minimal y coordinate of the data.
* @param ymax maximal y coordinate of the data.
* @param xnum Number of data points in x direction.
* @param ynum Number of data points in y direction.
*
* @param xmin minimal x coordinate of the data.
* @param dx increment of x coordinate of the data.
* @param ymin minimal y coordinate of the data.
* @param dy increment of y coordinate of the data.
* @return Name of the virtual grid which could be used for plotting.
*/
//std::string create_virtual_grid(const array<double> &data, double xmin, double xmax,
// double ymin, double ymax, int xnum, int ynum);
std::string create_grid(const array<double> &data, int xnum, int ynum,
double xmin, double dx, double ymin, double dy);
private:
gmt(gmt const&) = delete;
void operator=(gmt const&) = delete;
bool seesion_ok_;
void* GMT_API_;
std::string session_name_;
std::vector<std::string> buffer_;
//char gmt_gridname_[GMT_VF_LEN];
//struct GMT_GRID* GRID_;
std::vector<std::string> tmpfiles_;
};
};

View File

@ -58,7 +58,7 @@ namespace gctl
void to_buffer();
/**
* @brief Send a line to gnuplot and execute it. If send2buffer is called before, then the line will be stored in the buffer.
* @brief Send a line to gnuplot and execute it. If to_buffer is called before, then the line will be stored in the buffer.
*
* @param cmd command line.
* @param next_block If true, an empty line will be added to the buffer after the cmd.