gmt_api_example/example1/gridding.cpp

48 lines
1.7 KiB
C++
Raw Normal View History

2020-09-04 10:28:52 +08:00
#include "gmt.h"
#include "iostream"
#include "string"
int main(int argc, char *argv[])
{
2021-01-10 14:23:03 +08:00
std::string table_name = "table.txt";
std::string grid_name = "table.nc";
2020-09-04 10:28:52 +08:00
// 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*>(
2021-01-10 14:23:03 +08:00
GMT_Read_Data (API, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_PLP, GMT_READ_NORMAL, NULL, table_name.c_str(), NULL));
2020-09-04 10:28:52 +08:00
// load dataset to a virtual file for gridding
2021-01-10 14:23:03 +08:00
// 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);
2020-09-04 10:28:52 +08:00
// create a virtual file to hold result
2021-01-10 14:23:03 +08:00
char grid_vir_file[GMT_VF_LEN] = {""};
GMT_Open_VirtualFile(API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_OUT, NULL, grid_vir_file);
2020-09-04 10:28:52 +08:00
// prepare CMD arguments for the gridding module
2021-01-10 14:23:03 +08:00
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;
2020-09-04 10:28:52 +08:00
// 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*>(
2021-01-10 14:23:03 +08:00
GMT_Read_VirtualFile(API, grid_vir_file));
2020-09-04 10:28:52 +08:00
// write the grid to file
GMT_Write_Data(API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_READ_NORMAL, NULL,
2021-01-10 14:23:03 +08:00
grid_name.c_str(), G);
2020-09-04 10:28:52 +08:00
// close virtual files
2021-01-10 14:23:03 +08:00
GMT_Close_VirtualFile (API, table_vir_file);
GMT_Close_VirtualFile (API, grid_vir_file);
2020-09-04 10:28:52 +08:00
// end the GMT session
GMT_Destroy_Session(API);
return 0;
}