add cmakelists

This commit is contained in:
张壹 2021-01-10 14:23:03 +08:00
parent 758824ca22
commit ad5ad5cee3
7 changed files with 36 additions and 17 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
.DS_Store
.DS_Store
build/

13
CMakeLists.txt Normal file
View File

@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15.2 FATAL_ERROR)
project(GMT_API_EX VERSION 0.1.0 LANGUAGES CXX)
include_directories("/usr/local/include/gmt")
find_library(${GMT_LIB} gmt "/usr/local/lib")
macro(add_example name file)
add_executable(${name} ${file}.cpp)
target_link_libraries(${name} PUBLIC ${GMT_LIB})
endmacro()
add_example(ex1 example1/gridding)

View File

@ -3,4 +3,4 @@
inc=`gmt-config --cflags`
lib=`gmt-config --libs`
g++ gridding.cpp $inc $lib -o gridding
./gridding
#./gridding

4
example1/gmt.history Normal file
View File

@ -0,0 +1,4 @@
# GMT 6 Session common arguments shelf
BEGIN GMT 6.1.1
R 0/1000/0/1000
END

Binary file not shown.

View File

@ -4,42 +4,43 @@
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.txt", NULL));
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
// data type of a GMT virtual file must be GMT_STR16
char table_file[GMT_STR16] = {""};
GMT_Open_VirtualFile(API, GMT_IS_DATASET, GMT_IS_PLP, GMT_IN, D, table_file);
// 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_file[GMT_STR16] = {""};
GMT_Open_VirtualFile(API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_OUT, NULL, grid_file);
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_file_str = table_file;
std::string grid_file_str = grid_file;
std::string args_gridding = "-R0/1000/0/1000 -I10 -D1 -St0.3 " +
table_file_str + " -G" + grid_file_str;
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_file));
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,
"table.nc", G);
grid_name.c_str(), G);
// close virtual files
GMT_Close_VirtualFile (API, table_file);
GMT_Close_VirtualFile (API, grid_file);
GMT_Close_VirtualFile (API, table_vir_file);
GMT_Close_VirtualFile (API, grid_vir_file);
// end the GMT session
GMT_Destroy_Session(API);

BIN
example1/table.nc Normal file

Binary file not shown.