gmt_api_example/example1/gridding.cpp
2021-01-10 14:23:03 +08:00

48 lines
1.7 KiB
C++

#include "gmt.h"
#include "iostream"
#include "string"
int main(int argc, char *argv[])
{
std::string table_name = "table.txt";
std::string grid_name = "table.nc";
// Initiate a new GMT session
void *API = GMT_Create_Session("gridding_example", 2U, 0, NULL);
// Read table file from disk
struct GMT_DATASET *D = reinterpret_cast<GMT_DATASET*>(
GMT_Read_Data (API, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_PLP, GMT_READ_NORMAL, NULL, table_name.c_str(), NULL));
// load dataset to a virtual file for gridding
// length of a GMT virtual file's name must be GMT_VF_LEN
char table_vir_file[GMT_VF_LEN] = {""};
GMT_Open_VirtualFile(API, GMT_IS_DATASET, GMT_IS_PLP, GMT_IN, D, table_vir_file);
// create a virtual file to hold result
char grid_vir_file[GMT_VF_LEN] = {""};
GMT_Open_VirtualFile(API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_OUT, NULL, grid_vir_file);
// prepare CMD arguments for the gridding module
std::string table_vir_file_str = table_vir_file;
std::string grid_vir_file_str = grid_vir_file;
std::string args_gridding = "-R0/1000/0/1000 -I10 -D1 -St0.3 " + table_vir_file_str + " -G" + grid_vir_file_str;
// call the greenspline module for gridding
GMT_Call_Module(API, "greenspline", GMT_MODULE_CMD, (char*) args_gridding.c_str());
// get grid file from the virtual file for outputting
struct GMT_GRID *G = reinterpret_cast<GMT_GRID*>(
GMT_Read_VirtualFile(API, grid_vir_file));
// write the grid to file
GMT_Write_Data(API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_READ_NORMAL, NULL,
grid_name.c_str(), G);
// close virtual files
GMT_Close_VirtualFile (API, table_vir_file);
GMT_Close_VirtualFile (API, grid_vir_file);
// end the GMT session
GMT_Destroy_Session(API);
return 0;
}