diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c9f7bf..6138a00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ option(GCTL_FFTW3 "Use the FFTW3 library" ON) option(GCTL_EEMD "Use the EEMD library" ON) option(GCTL_OPENBLAS "Use the Openblas library" OFF) option(GCTL_EXPRTK "Use the ExprTK library" ON) +option(GCTL_GMT "Use the GMT library" ON) option(GCTL_CHECK_BOUNDER "Check array's index" OFF) option(GCTL_CHECK_SIZE "Check array's size" OFF) # 传递安装地址给编译期宏变量 @@ -29,6 +30,7 @@ message(STATUS "[GCTL] Use the FFTW3 library: " ${GCTL_FFTW3}) message(STATUS "[GCTL] Use the EEMD library: " ${GCTL_EEMD}) message(STATUS "[GCTL] Use the Openblas library: " ${GCTL_OPENBLAS}) message(STATUS "[GCTL] Use the ExprTK library: " ${GCTL_EXPRTK}) +message(STATUS "[GCTL] Use the GMT library: " ${GCTL_GMT}) message(STATUS "[GCTL] Check Bounder: " ${GCTL_CHECK_BOUNDER}) message(STATUS "[GCTL] Check Size: " ${GCTL_CHECK_SIZE}) @@ -40,6 +42,12 @@ if(GCTL_FFTW3) endif() endif() +if(GCTL_GMT) + find_package(GMT REQUIRED) + message(STATUS "Found GMT") + include_directories(${GMT_INC_DIR}) +endif() + if(GCTL_EEMD) if(NOT EEMD_FOUND) find_package(EEMD REQUIRED) diff --git a/GCTLConfig.cmake.in b/GCTLConfig.cmake.in index a151443..20c4a00 100644 --- a/GCTLConfig.cmake.in +++ b/GCTLConfig.cmake.in @@ -16,6 +16,7 @@ set(@PROJECT_NAME@_EEMD @GCTL_EEMD@) set(@PROJECT_NAME@_OPENMP @GCTL_OPENMP@) set(@PROJECT_NAME@_OPENBLAS @GCTL_OPENBLAS@) set(@PROJECT_NAME@_EXPRTK @GCTL_EXPRTK@) +set(@PROJECT_NAME@_GMT @GCTL_GMT@) set(@PROJECT_NAME@_CHECK_BOUNDER @GCTL_CHECK_BOUNDER@) set(@PROJECT_NAME@_CHECK_SIZE @GCTL_CHECK_SIZE@) @@ -25,6 +26,7 @@ message(STATUS "[GCTL] Use the EEMD library: " @GCTL_EEMD@) message(STATUS "[GCTL] Use the OpenMP library: " @GCTL_OPENMP@) message(STATUS "[GCTL] Use the Openblas library: " @GCTL_OPENBLAS@) message(STATUS "[GCTL] Use the ExprTK library: " @GCTL_EXPRTK@) +message(STATUS "[GCTL] Use the GMT library: " @GCTL_GMT@) message(STATUS "[GCTL] Check Bounder: " @GCTL_CHECK_BOUNDER@) message(STATUS "[GCTL] Check Size: " @GCTL_CHECK_SIZE@) @@ -51,6 +53,13 @@ if(@PROJECT_NAME@_FFTW3) endif() endif() +if(@PROJECT_NAME@_GMT) + if(NOT GMT_FOUND) + find_package(GMT REQUIRED) + include_directories(${GMT_INC_DIR}) + endif() +endif() + if(@PROJECT_NAME@_EEMD) if(NOT EEMD_FOUND) find_package(EEMD REQUIRED) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index fd48dbb..2be42d4 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -23,10 +23,12 @@ add_example(windowfunc_ex OFF) add_example(legendre_ex OFF) add_example(refellipsoid_ex OFF) add_example(kde_ex OFF) -add_example(meshio_ex ON) +add_example(meshio_ex OFF) add_example(autodiff_ex OFF) add_example(multinary_ex OFF) add_example(text_io_ex OFF) add_example(getoption_ex OFF) add_example(process_ex OFF) -add_example(array_ex OFF) \ No newline at end of file +add_example(array_ex OFF) +add_example(gmt_ex ON) +add_example(gnuplot_ex OFF) \ No newline at end of file diff --git a/example/gmt_ex.cpp b/example/gmt_ex.cpp new file mode 100644 index 0000000..6f2e691 --- /dev/null +++ b/example/gmt_ex.cpp @@ -0,0 +1,77 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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 "../lib/graphic/gmt.h" +#include "../lib/io/netcdf_io.h" + +int main(int argc, char *argv[]) +{ + gctl::gmt gt; + /* + gt.begin_session("line_plot", "eps,png"); + gt.call_module("basemap", "-R0/10/0/10 -JX5i/5i -Baf"); + gt.call_module("plot", "tmp/data.txt -R0/10/0/10 -JX5i/5i -W1p,blue"); + gt.end_session(false); + gt.save_session("line_plot"); + */ + + // example 2 + + gctl::array data(51*41); + + double dist; + for (int i = 0; i < 51; ++i) + { + for (int j = 0; j < 41; ++j) + { + dist = sqrt((j-20)*(j-20) + (i-25)*(i-25)); + data[j+i*41] = sin(dist/GCTL_Pi); + } + } + + 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", "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"); + + 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("psscale", "-Bxa -By+lm -Dx0.1i/-0.2i+w1.3i/0.05i+h"); + gt.end_session(); + gt.save_session("image_plot"); + return 0; +} diff --git a/example/gnuplot_ex.cpp b/example/gnuplot_ex.cpp new file mode 100644 index 0000000..7132345 --- /dev/null +++ b/example/gnuplot_ex.cpp @@ -0,0 +1,84 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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 "../lib/graphic/gnuplot.h" + +int main(int argc, char *argv[]) +{ + gctl::gnuplot gt; + + //one line test + //gt.send("plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))"); + + //buffer test (bessel animation) + gt.to_buffer(); + gt.send("set terminal gif animate delay 10 size 600,400"); + gt.send("set output 'bessel.gif'"); + gt.send("set palette rgb 3,9,9"); + gt.send("unset key; unset colorbox; unset border; unset tics"); + gt.send("set lmargin at screen 0.03"); + gt.send("set bmargin at screen 0"); + gt.send("set rmargin at screen 0.97"); + gt.send("set tmargin at screen 1"); + gt.send("set parametric", true); + gt.send("bessel(x,t) = besj0(x) * cos(2*pi*t)"); + gt.send("n = 6 # number of zeros"); + gt.send("k = (n*pi-1.0/4*pi)"); + gt.send("u_0 = k + 1/(8*k) - 31/(384*k)**3 + 3779/(15360*k)**5"); + gt.send("set urange [0:u_0]"); + gt.send("set vrange[0:1.5*pi]"); + gt.send("set cbrange [-1:1]"); + gt.send("set zrange[-1:1]"); + gt.send("set isosamples 200,100"); + gt.send("set pm3d depthorder"); + gt.send("set view 40,200"); + + std::string cmd; + for (float t = 0.0f; t < 2.0f; t += 0.02f) + { + cmd = "splot u*sin(v),u*cos(v),bessel(u," + std::to_string(t) + ") w pm3d ls 1"; + gt.send(cmd); + } + gt.send("set output"); + + gt.save_buffer("bessel"); + gt.send_buffer(); + + //data test + /* + gt.to_buffer(); + gt.send("$Data << EOD"); + gt.send("0 0 0"); + gt.send("1 1 0.5"); + gt.send("2 4 2"); + gt.send("3 9 3"); + gt.send("EOD"); + gt.send("plot $Data using 1:2 with lines title 'y1', '' using 1:3 with lines title 'y2'"); + gt.send_buffer(); + */ + return 0; +} diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ec2b6f3..e26edde 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -10,7 +10,7 @@ aux_source_directory(algorithms/ GCTL_ALGORITHMS_SRC) aux_source_directory(io/ GCTL_IO_SRC) aux_source_directory(utility/ GCTL_UTILITY_SRC) aux_source_directory(maths/ GCTL_MATHS_SRC) -aux_source_directory(geodesy/ GCTL_GEODESY_SRC) +aux_source_directory(graphic/ GCTL_GRAPHIC_SRC) # 在windows系统下使用改编过的getopt_win源文件替代GNU getopt # 若在linux系统下则不适用此源文件 if(NOT WIN32) @@ -24,10 +24,10 @@ endif() # 以下部分为库的编译 # 注意目标名必须唯一 所以不能直接生成相同名称的动态库与静态库 # 注意此处不必为目标名称添加lib前缀和相应后缀,cmake会自行添加 -add_library(gctl SHARED ${GCTL_UTILITY_SRC} ${GCTL_ALGORITHMS_SRC} ${GCTL_GEODESY_SRC} +add_library(gctl SHARED ${GCTL_UTILITY_SRC} ${GCTL_ALGORITHMS_SRC} ${GCTL_GRAPHIC_SRC} ${GCTL_GEOMETRY_SRC} ${GCTL_IO_SRC} ${GCTL_MATHS_SRC}) # 首先添加静态库的生成命令 -add_library(gctl_static STATIC ${GCTL_UTILITY_SRC} ${GCTL_ALGORITHMS_SRC} ${GCTL_GEODESY_SRC} +add_library(gctl_static STATIC ${GCTL_UTILITY_SRC} ${GCTL_ALGORITHMS_SRC} ${GCTL_GRAPHIC_SRC} ${GCTL_GEOMETRY_SRC} ${GCTL_IO_SRC} ${GCTL_MATHS_SRC}) # 设置静态库的输出名称从而获得与动态库名称相同的静态库 set_target_properties(gctl_static PROPERTIES OUTPUT_NAME "gctl") @@ -58,6 +58,12 @@ if(GCTL_FFTW3) target_link_libraries(gctl_static ${FFTW_LIB}) endif() +if(GCTL_GMT) + find_library(GMT_LIB_TAR ${GMT_LIB} HINTS ${GMT_LIB_DIR}) + target_link_libraries(gctl PUBLIC ${GMT_LIB_TAR}) + target_link_libraries(gctl_static ${GMT_LIB_TAR}) +endif() + if(GCTL_EEMD) target_link_libraries(gctl PUBLIC ${EEMD_LIB}) target_link_libraries(gctl_static ${EEMD_LIB}) @@ -108,7 +114,7 @@ file(GLOB GCTL_MATHS_HEAD maths/*.h) file(GLOB GCTL_GEOMETRY_HEAD geometry/*.h) file(GLOB GCTL_UTILITY_HEAD utility/*.h) file(GLOB GCTL_MESH_HEAD mesh/*.h) -file(GLOB GCTL_GEODESY_HEAD geodesy/*.h) +file(GLOB GCTL_GRAPHIC_HEAD graphic/*.h) if(NOT WIN32) list(REMOVE_ITEM GCTL_UTILITY_HEAD "${PROJECT_SOURCE_DIR}/lib/utility/getopt_win.h") @@ -130,4 +136,4 @@ install(FILES ${GCTL_ALGORITHMS_HEAD} DESTINATION include/gctl/algorithms) install(FILES ${GCTL_GEOMETRY_HEAD} DESTINATION include/gctl/geometry) install(FILES ${GCTL_UTILITY_HEAD} DESTINATION include/gctl/utility) install(FILES ${GCTL_MESH_HEAD} DESTINATION include/gctl/mesh) -install(FILES ${GCTL_GEODESY_HEAD} DESTINATION include/gctl/geodesy) \ No newline at end of file +install(FILES ${GCTL_GRAPHIC_HEAD} DESTINATION include/gctl/graphic) \ No newline at end of file diff --git a/lib/graphic.h b/lib/graphic.h new file mode 100644 index 0000000..c83887c --- /dev/null +++ b/lib/graphic.h @@ -0,0 +1,34 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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. + ******************************************************/ + +#ifndef _GCTL_GRAPHIC_H +#define _GCTL_GRAPHIC_H + +#include "graphic/gnuplot.h" +#include "graphic/gmt.h" + +#endif // _GCTL_GRAPHIC_H \ No newline at end of file diff --git a/lib/graphic/gmt.cpp b/lib/graphic/gmt.cpp new file mode 100644 index 0000000..e07a750 --- /dev/null +++ b/lib/graphic/gmt.cpp @@ -0,0 +1,175 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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 "gmt.h" + +gctl::gmt::gmt() +{ + GMT_API_ = nullptr; + //GRID_ = nullptr; + //std::fill(gmt_gridname_, gmt_gridname_ + sizeof(gmt_gridname_), '\0'); + buffer_.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; + } + + if (!buffer_.empty()) buffer_.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(); + + 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."); + + std::string cmd = pic_name + " " + pic_type; + if (GMT_Call_Module(GMT_API_, "begin", GMT_MODULE_CMD, (char*) cmd.c_str()) != GMT_NOERROR) + throw std::runtime_error("[gctl::gmt] Fail to execute GMT command."); + else + { + cmd = "begin " + cmd; + buffer_.push_back(cmd); + } + return; +} + +void gctl::gmt::end_session(bool show) +{ + std::string cmd = ""; + if (show) cmd = "show"; + + if (GMT_Call_Module(GMT_API_, "end", GMT_MODULE_CMD, (char*) cmd.c_str()) != GMT_NOERROR) + throw std::runtime_error("[gctl::gmt] Fail to end GMT session."); + else + { + cmd = "end " + cmd; + 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 (GMT_API_ != nullptr) + { + GMT_Destroy_Session(GMT_API_); + GMT_API_ = nullptr; + } + return; +} + +void gctl::gmt::save_session(const std::string& filename) +{ + time_t now = time(0); + char* dt = ctime(&now); + + std::ofstream outfile; + open_outfile(outfile, filename, ".sh"); + + outfile << "#!/bin/bash" << std::endl; + outfile << "# This script is saved by gctl::gmt on " << dt; + outfile << "# For more information please connect yizhang-geo@zju.edu.cn" << std::endl; + + for (auto& line : buffer_) outfile << "gmt " << line << std::endl; + outfile.close(); + + buffer_.clear(); + return; +} + +void gctl::gmt::call_module(const std::string& module, const std::string& cmd, const std::string& opt) +{ + 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."); + else + { + std::string cmds = module + " " + cmd; + buffer_.push_back(cmds); + } +} + +/* +std::string gctl::gmt::create_virtual_grid(const array &data, + double xmin, double xmax, double ymin, double ymax, int xnum, int ynum) +{ + 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_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)); + + // 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_); + + 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]; + + 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; +} +*/ \ No newline at end of file diff --git a/lib/graphic/gmt.h b/lib/graphic/gmt.h new file mode 100644 index 0000000..df9dade --- /dev/null +++ b/lib/graphic/gmt.h @@ -0,0 +1,120 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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. + ******************************************************/ + +#ifndef _GCTL_GMT_H +#define _GCTL_GMT_H + +#include +#include +#include +#include + +#include "gmt/gmt.h" +#include "../utility/stream.h" +#include "../core/array.h" + +#ifndef GMT_VF_LEN +#define GMT_VF_LEN 16 +#endif + +namespace gctl +{ + /** + * @brief 这是一个gmt命令管道库 + */ + class gmt + { + public: + /** + * @brief Construct a new gmt object + */ + gmt(); + // destructor + virtual ~gmt(); + + /** + * @brief Start a session. + * + * @param pic_name Output picture name. + * @param pic_type Output picture type. Multiple types can be specified by comma. + * @param name Session name. + */ + void begin_session(const std::string& pic_name, const std::string& pic_type = "eps", + const std::string& name = "gctl_gmt_session"); + + /** + * @brief Destroy a session object + * + * @param show Whether to show the result after finish the plotting. + */ + void end_session(bool show = true); + + /** + * @brief Save the session to a file. + * + * @param filename File name. + */ + void save_session(const std::string& filename); + + /** + * @brief Call a GMT module. + * + * @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 = ""); + + /** + * @brief Create a virtual 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. + * + * @return Name of the virtual grid which could be used for plotting. + */ + //std::string create_virtual_grid(const array &data, double xmin, double xmax, + // double ymin, double ymax, int xnum, int ynum); + + private: + gmt(gmt const&) = delete; + void operator=(gmt const&) = delete; + + void* GMT_API_; + std::vector buffer_; + + //char gmt_gridname_[GMT_VF_LEN]; + //struct GMT_GRID* GRID_; + }; +}; + +#endif // _GCTL_GMT_H \ No newline at end of file diff --git a/lib/graphic/gnuplot.cpp b/lib/graphic/gnuplot.cpp new file mode 100644 index 0000000..0c73243 --- /dev/null +++ b/lib/graphic/gnuplot.cpp @@ -0,0 +1,89 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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 "gnuplot.h" + +gctl::gnuplot::gnuplot(bool persist) +{ + _2buffer = false; + pipe = popen(persist ? "gnuplot -persist" : "gnuplot", "w"); + if (!pipe) throw std::runtime_error("gnuplot pipe failed to open. Try install gnuplot first."); +} + +gctl::gnuplot::~gnuplot() +{ + if (pipe) pclose(pipe); +} + +void gctl::gnuplot::to_buffer() +{ + _2buffer = true; + return; +} + +void gctl::gnuplot::send(const std::string& cmd, bool next_block) +{ + if (!pipe) return; + + if (!_2buffer) fputs((cmd + "\n").c_str(), pipe); + else + { + if (next_block) buffer.push_back(cmd + "\n\n"); + else buffer.push_back(cmd + "\n"); + } + return; +} + +void gctl::gnuplot::send_buffer(size_t repeat) +{ + if (!pipe) return; + for (size_t i = 0; i < repeat; i++) + { + for (auto& line : buffer) fputs(line.c_str(), pipe); + fputs("\n", pipe); + } + + fflush(pipe); + buffer.clear(); + return; +} + +void gctl::gnuplot::save_buffer(const std::string& filename) +{ + time_t now = time(0); + char* dt = ctime(&now); + + std::ofstream outfile; + open_outfile(outfile, filename, ".gp"); + + outfile << "# This script is saved by gctl::gnuplot on " << dt; + outfile << "# For more information please connect yizhang-geo@zju.edu.cn" << std::endl; + + for (auto& line : buffer) outfile << line; + outfile.close(); + return; +} \ No newline at end of file diff --git a/lib/graphic/gnuplot.h b/lib/graphic/gnuplot.h new file mode 100644 index 0000000..0b8cd20 --- /dev/null +++ b/lib/graphic/gnuplot.h @@ -0,0 +1,92 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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. + ******************************************************/ + +#ifndef _GCTL_GNUPLOT_H +#define _GCTL_GNUPLOT_H + +#include +#include +#include +#include + +#include "../utility/stream.h" + +namespace gctl +{ + /** + * @brief 这是一个gnuplot命令管道库 + */ + class gnuplot + { + public: + /** + * @brief Construct a new gnuplot object + * + * @param persist Gnuplot will hold the window after plot if true. + */ + gnuplot(bool persist = true); + // destructor + virtual ~gnuplot(); + + /** + * @brief Set this to send all lines to buffer. + */ + 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. + * + * @param cmd command line. + * @param next_block If true, an empty line will be added to the buffer after the cmd. + */ + void send(const std::string& cmd, bool next_block = false); + + /** + * @brief Send the buffer to gnuplot and execute it at once. Then clear the buffer. + * + * @param repeat How many times to repeat the buffer. + */ + void send_buffer(size_t repeat = 1); + + /** + * @brief Save the buffer to a file. This will not clear the buffer. + * + * @param filename File name. + */ + void save_buffer(const std::string& filename); + + private: + gnuplot(gnuplot const&) = delete; + void operator=(gnuplot const&) = delete; + + FILE* pipe; + std::vector buffer; + bool _2buffer; + }; +}; + +#endif // _GCTL_GNUPLOT_H \ No newline at end of file diff --git a/lib/utility/stream.h b/lib/utility/stream.h index 6dc284b..6778305 100644 --- a/lib/utility/stream.h +++ b/lib/utility/stream.h @@ -33,7 +33,7 @@ #include #include #include -#include "stdio.h" +#include #ifdef _WINDOWS #include