diff --git a/.gitignore b/.gitignore index 496ee2c..aaab5ad 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.DS_Store \ No newline at end of file +.DS_Store +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..844c9ba --- /dev/null +++ b/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/example1/compile.sh b/example1/compile.sh index 466f541..d0a0586 100755 --- a/example1/compile.sh +++ b/example1/compile.sh @@ -3,4 +3,4 @@ inc=`gmt-config --cflags` lib=`gmt-config --libs` g++ gridding.cpp $inc $lib -o gridding -./gridding \ No newline at end of file +#./gridding \ No newline at end of file diff --git a/example1/gmt.history b/example1/gmt.history new file mode 100644 index 0000000..3d58eae --- /dev/null +++ b/example1/gmt.history @@ -0,0 +1,4 @@ +# GMT 6 Session common arguments shelf +BEGIN GMT 6.1.1 +R 0/1000/0/1000 +END diff --git a/example1/gridding b/example1/gridding index 48d50a8..17ae623 100755 Binary files a/example1/gridding and b/example1/gridding differ diff --git a/example1/gridding.cpp b/example1/gridding.cpp index ab0950a..2bdf858 100644 --- a/example1/gridding.cpp +++ b/example1/gridding.cpp @@ -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_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_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); diff --git a/example1/table.nc b/example1/table.nc new file mode 100644 index 0000000..23b0c29 Binary files /dev/null and b/example1/table.nc differ