/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ * ██║ ██║██║ ██║ ██║ * ╚██████╔╝╚██████╗ ██║ ███████╗ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ * 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; } */