diff --git a/CMakeLists.txt b/CMakeLists.txt index 844c9ba..55d255c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,12 +2,14 @@ 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") +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 +add_example(ex1 src/gridding) +add_example(ex2 src/plot_grid) +add_example(ex3 src/plot_memory) \ No newline at end of file diff --git a/README.md b/README.md index acd3d83..a90766c 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,15 @@ For those who wants to know more, please go to [GMT_site](https://www.generic-ma ## What are these examples? -* **example1**: call a GMT module to gridding a table data. -* **example2**: read a grid file and call GMT modules to plot the grid to an image file. -* **example3**: create a GMT grid container and call GMT modules to plot the grid to an image file. +* **griding**: call a GMT module to griding a table data. +* **plot_grid**: read a grid file and call GMT modules to plot the grid to an image file. +* **plot_memory**: create a GMT grid container and call GMT modules to plot the grid to an image file. ## How to run the examples? -Simply go to a example folder and run the `compile.sh`. \ No newline at end of file +Compile the executables and then run sequentially. + +```shell +mkdir build && cd build && cmake .. && make +``` + diff --git a/example2/user.eps b/data/ex2.eps similarity index 98% rename from example2/user.eps rename to data/ex2.eps index 4f15bde..66c3bf5 100644 --- a/example2/user.eps +++ b/data/ex2.eps @@ -1,11 +1,11 @@ %!PS-Adobe-3.0 %%BoundingBox: 0 0 159 156 %%HiResBoundingBox: 0 0 158.8500 155.9520 -%%Title: GMT v6.0.0 [64-bit] Document from grdimage +%%Title: GMT v6.1.1 [64-bit] Document from grdimage %%Creator: GMT6 %%For: unknown %%DocumentNeededResources: font Helvetica Times-Roman -%%CreationDate: Fri Sep 4 10:11:40 2020 +%%CreationDate: Sun Jan 10 14:45:17 2021 %%LanguageLevel: 2 %%DocumentData: Clean7Bit %%Orientation: Portrait @@ -654,7 +654,8 @@ V 0.06 0.06 scale /PSL_page_xsize 9917 def /PSL_page_ysize 14033 def /PSL_plot_completion {} def -/PSL_movie_completion {} def +/PSL_movie_label_completion {} def +/PSL_movie_prog_indicator_completion {} def %PSL_End_Header gsave 0 A @@ -662,12 +663,12 @@ FQ O0 1800 1800 TM % PostScript produced by: -%@GMT: gmt grdimage @GMTAPI@-000000 -R0/1000/0/1000 -Cuser.cpt '-Bxag+lx (m)' '-Byag+ly (m)' -JX1.5i/1.5i -X1.5i -Y1.5i -K -P --MAP_FRAME_AXES=WesNZ user.ps +%@GMT: gmt grdimage @GMTAPI@-S-I-G-G-G-N-000000 -R0/1000/0/1000 -C../data/ex2.cpt '-Bxag+lx (m)' '-Byag+ly (m)' -JX1.5i/1.5i -X1.5i -Y1.5i -K -P --MAP_FRAME_AXES=WesNZ ../data/ex2.ps %@PROJ: xy 0.00000000 1000.00000000 0.00000000 1000.00000000 0.000 1000.000 0.000 1000.000 +xy %%BeginObject PSL_Layer_1 0 setlinecap 0 setlinejoin -3.32551 setmiterlimit +3.32550952342 setmiterlimit clipsave 0 0 M 1800 0 D @@ -953,6 +954,7 @@ N 1440 1783 M 0 34 D S N 1783 1800 M 34 0 D S N 1800 1783 M 0 34 D S PSL_cliprestore +/PSL_slant_y 0 def 2 setlinecap N 0 1800 M 0 -1800 D S /PSL_A0_y 17 def @@ -964,8 +966,8 @@ N 0 1080 M -17 0 D S N 0 1440 M -17 0 D S N 0 1800 M -17 0 D S PSL_font_encode 4 get 0 eq {ISOLatin1+_Encoding /Times-Roman /Times-Roman PSL_reencode PSL_font_encode 4 1 put} if -/PSL_AH0 0 /MM {neg exch M} def +/PSL_AH0 0 175 F4 (0) sw mx (200) sw mx @@ -1024,8 +1026,8 @@ N 720 0 M 0 17 D S N 1080 0 M 0 17 D S N 1440 0 M 0 17 D S N 1800 0 M 0 17 D S -/PSL_AH0 0 /MM {M} def +/PSL_AH0 0 (0) sh mx (200) sh mx (400) sh mx @@ -1059,12 +1061,12 @@ FQ O0 0 0 TM % PostScript produced by: -%@GMT: gmt psscale -Dx0.1i/-0.2i+w1.3i/0.05i+h -Cuser.cpt -Bxa -By+lm -O >user.ps +%@GMT: gmt psscale -Dx0.1i/-0.2i+w1.3i/0.05i+h -C../data/ex2.cpt -Bxa -By+lm -O >../data/ex2.ps %@PROJ: xy -668.08288574 -39.34982681 0.00000000 0.05000000 -668.083 -39.350 0.000 0.050 +xy %%BeginObject PSL_Layer_2 0 setlinecap 0 setlinejoin -3.32551 setmiterlimit +3.32550952342 setmiterlimit 120 -240 T 4 W V N 0 0 T 1560 60 scale /DeviceRGB setcolorspace @@ -1097,8 +1099,8 @@ N 0 0 M 1560 0 D S N 169 0 M 0 -17 D S N 665 0 M 0 -17 D S N 1161 0 M 0 -17 D S -/PSL_AH0 0 /MM {neg M} def +/PSL_AH0 0 PSL_font_encode 4 get 0 eq {ISOLatin1+_Encoding /Times-Roman /Times-Roman PSL_reencode PSL_font_encode 4 1 put} if 175 F4 (-600) sh mx @@ -1118,7 +1120,8 @@ def -120 240 T %%EndObject grestore -PSL_movie_completion /PSL_movie_completion {} def +PSL_movie_label_completion /PSL_movie_label_completion {} def +PSL_movie_prog_indicator_completion /PSL_movie_prog_indicator_completion {} def %PSL_Begin_Trailer %%PageTrailer U diff --git a/example2/user.png b/data/ex2.png similarity index 98% rename from example2/user.png rename to data/ex2.png index 2620958..a474b05 100644 Binary files a/example2/user.png and b/data/ex2.png differ diff --git a/example3/user.eps b/data/ex3.eps similarity index 98% rename from example3/user.eps rename to data/ex3.eps index 8d19ab1..745675c 100644 --- a/example3/user.eps +++ b/data/ex3.eps @@ -1,11 +1,11 @@ %!PS-Adobe-3.0 %%BoundingBox: 0 0 146 156 %%HiResBoundingBox: 0 0 145.7280 155.9520 -%%Title: GMT v6.0.0 [64-bit] Document from grdimage +%%Title: GMT v6.1.1 [64-bit] Document from grdimage %%Creator: GMT6 %%For: unknown %%DocumentNeededResources: font Helvetica Times-Roman -%%CreationDate: Fri Sep 4 10:14:23 2020 +%%CreationDate: Sun Jan 10 14:45:19 2021 %%LanguageLevel: 2 %%DocumentData: Clean7Bit %%Orientation: Portrait @@ -654,7 +654,8 @@ V 0.06 0.06 scale /PSL_page_xsize 9917 def /PSL_page_ysize 14033 def /PSL_plot_completion {} def -/PSL_movie_completion {} def +/PSL_movie_label_completion {} def +/PSL_movie_prog_indicator_completion {} def %PSL_End_Header gsave 0 A @@ -662,12 +663,12 @@ FQ O0 600 600 TM % PostScript produced by: -%@GMT: gmt grdimage @GMTAPI@-000000 -R0/100/0/80 -Cuser.cpt '-Bxag+lx (m)' '-Byag+ly (m)' -JX1.5i/1.5i -X0.5i -Y0.5i -K -P --MAP_FRAME_AXES=WesNZ user.ps +%@GMT: gmt grdimage @GMTAPI@-S-I-G-G-G-N-000000 -R0/100/0/80 -C../data/ex3.cpt '-Bxag+lx (m)' '-Byag+ly (m)' -JX1.5i/1.5i -X0.5i -Y0.5i -K -P --MAP_FRAME_AXES=WesNZ ../data/ex3.ps %@PROJ: xy 0.00000000 100.00000000 0.00000000 80.00000000 0.000 100.000 0.000 80.000 +xy %%BeginObject PSL_Layer_1 0 setlinecap 0 setlinejoin -3.32551 setmiterlimit +3.32550952342 setmiterlimit clipsave 0 0 M 1800 0 D @@ -862,6 +863,7 @@ N 1440 1783 M 0 34 D S N 1783 1800 M 34 0 D S N 1800 1783 M 0 34 D S PSL_cliprestore +/PSL_slant_y 0 def 2 setlinecap N 0 1800 M 0 -1800 D S /PSL_A0_y 17 def @@ -872,8 +874,8 @@ N 0 900 M -17 0 D S N 0 1350 M -17 0 D S N 0 1800 M -17 0 D S PSL_font_encode 4 get 0 eq {ISOLatin1+_Encoding /Times-Roman /Times-Roman PSL_reencode PSL_font_encode 4 1 put} if -/PSL_AH0 0 /MM {neg exch M} def +/PSL_AH0 0 175 F4 (0) sw mx (20) sw mx @@ -928,8 +930,8 @@ N 720 0 M 0 17 D S N 1080 0 M 0 17 D S N 1440 0 M 0 17 D S N 1800 0 M 0 17 D S -/PSL_AH0 0 /MM {M} def +/PSL_AH0 0 (0) sh mx (20) sh mx (40) sh mx @@ -963,12 +965,12 @@ FQ O0 0 0 TM % PostScript produced by: -%@GMT: gmt psscale -Dx0.1i/-0.2i+w1.3i/0.05i+h -Cuser.cpt -Bxa -By+lm -O >user.ps +%@GMT: gmt psscale -Dx0.1i/-0.2i+w1.3i/0.05i+h -C../data/ex3.cpt -Bxa -By+lm -O >../data/ex3.ps %@PROJ: xy 0.00000000 8000.00000000 0.00000000 0.05000000 0.000 8000.000 0.000 0.050 +xy %%BeginObject PSL_Layer_2 0 setlinecap 0 setlinejoin -3.32551 setmiterlimit +3.32550952342 setmiterlimit 120 -240 T 4 W V N 0 0 T 1560 60 scale /DeviceRGB setcolorspace @@ -1004,8 +1006,8 @@ N 390 0 M 0 -17 D S N 780 0 M 0 -17 D S N 1170 0 M 0 -17 D S N 1560 0 M 0 -17 D S -/PSL_AH0 0 /MM {neg M} def +/PSL_AH0 0 PSL_font_encode 4 get 0 eq {ISOLatin1+_Encoding /Times-Roman /Times-Roman PSL_reencode PSL_font_encode 4 1 put} if 175 F4 (0) sh mx @@ -1031,7 +1033,8 @@ def -120 240 T %%EndObject grestore -PSL_movie_completion /PSL_movie_completion {} def +PSL_movie_label_completion /PSL_movie_label_completion {} def +PSL_movie_prog_indicator_completion /PSL_movie_prog_indicator_completion {} def %PSL_Begin_Trailer %%PageTrailer U diff --git a/example3/user.png b/data/ex3.png similarity index 99% rename from example3/user.png rename to data/ex3.png index d115beb..5db5f9d 100644 Binary files a/example3/user.png and b/data/ex3.png differ diff --git a/example1/table.nc b/data/table.nc similarity index 100% rename from example1/table.nc rename to data/table.nc diff --git a/example1/table.txt b/data/table.txt similarity index 100% rename from example1/table.txt rename to data/table.txt diff --git a/example1/compile.sh b/example1/compile.sh deleted file mode 100755 index d0a0586..0000000 --- a/example1/compile.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -inc=`gmt-config --cflags` -lib=`gmt-config --libs` -g++ gridding.cpp $inc $lib -o gridding -#./gridding \ No newline at end of file diff --git a/example1/gmt.history b/example1/gmt.history deleted file mode 100644 index 3d58eae..0000000 --- a/example1/gmt.history +++ /dev/null @@ -1,4 +0,0 @@ -# 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 deleted file mode 100755 index 17ae623..0000000 Binary files a/example1/gridding and /dev/null differ diff --git a/example2/compile.sh b/example2/compile.sh deleted file mode 100755 index 20d9ef4..0000000 --- a/example2/compile.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -inc=`gmt-config --cflags` -lib=`gmt-config --libs` -g++ plot_grid.cpp $inc $lib -o plot_grid -./plot_grid \ No newline at end of file diff --git a/example2/plot_grid b/example2/plot_grid deleted file mode 100755 index 55cd077..0000000 Binary files a/example2/plot_grid and /dev/null differ diff --git a/example2/table.nc b/example2/table.nc deleted file mode 100644 index 42a21dd..0000000 Binary files a/example2/table.nc and /dev/null differ diff --git a/example3/compile.sh b/example3/compile.sh deleted file mode 100755 index 92ccc7d..0000000 --- a/example3/compile.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -inc=`gmt-config --cflags` -lib=`gmt-config --libs` -g++ plot_memory.cpp $inc $lib -o plot_memory -./plot_memory \ No newline at end of file diff --git a/example3/plot_memory b/example3/plot_memory deleted file mode 100755 index 1747772..0000000 Binary files a/example3/plot_memory and /dev/null differ diff --git a/example1/gridding.cpp b/src/gridding.cpp similarity index 82% rename from example1/gridding.cpp rename to src/gridding.cpp index 2bdf858..66b40a8 100644 --- a/example1/gridding.cpp +++ b/src/gridding.cpp @@ -1,11 +1,12 @@ #include "gmt.h" #include "iostream" #include "string" +#include "unistd.h" int main(int argc, char *argv[]) { - std::string table_name = "table.txt"; - std::string grid_name = "table.nc"; + std::string table_name = "../data/table.txt"; + std::string grid_name = "../data/table.nc"; // Initiate a new GMT session void *API = GMT_Create_Session("gridding_example", 2U, 0, NULL); @@ -44,5 +45,13 @@ int main(int argc, char *argv[]) // end the GMT session GMT_Destroy_Session(API); + + // if exist, delete + std::string hist_file = "gmt.history"; + std::string conf_file = "gmt.conf"; + if (!access(hist_file.c_str(), F_OK)) + remove(hist_file.c_str()); + if (!access(conf_file.c_str(), F_OK)) + remove(conf_file.c_str()); return 0; } \ No newline at end of file diff --git a/example2/plot_grid.cpp b/src/plot_grid.cpp similarity index 93% rename from example2/plot_grid.cpp rename to src/plot_grid.cpp index a2f86a1..5163cc0 100644 --- a/example2/plot_grid.cpp +++ b/src/plot_grid.cpp @@ -6,8 +6,8 @@ int main(int argc, char *argv[]) { - std::string cpt_file = "user.cpt"; - std::string ps_file = "user.ps"; + std::string cpt_file = "../data/ex2.cpt"; + std::string ps_file = "../data/ex2.ps"; // Initiate a new GMT session // you need to destroy the session later @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) struct GMT_GRID *G = reinterpret_cast( GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, - GMT_READ_NORMAL, NULL, "table.nc", NULL)); + GMT_READ_NORMAL, NULL, "../data/table.nc", NULL)); // start the plotting // 1. set GMT defaults @@ -33,7 +33,7 @@ MAP_ANNOT_OFFSET_PRIMARY=2.5p"; GMT_Call_Module (API, "gmtset", GMT_MODULE_CMD, (char*) args_defaults.c_str()); // load the grid to a virtual file for plotting - char grid_name[GMT_STR16] = {""}; + char grid_name[GMT_VF_LEN] = {""}; GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_IN, G, grid_name); // get string type of grid name std::string grid_name_str = grid_name; diff --git a/example3/plot_memory.cpp b/src/plot_memory.cpp similarity index 96% rename from example3/plot_memory.cpp rename to src/plot_memory.cpp index ea56593..3767044 100644 --- a/example3/plot_memory.cpp +++ b/src/plot_memory.cpp @@ -6,8 +6,8 @@ int main(int argc, char *argv[]) { - std::string cpt_file = "user.cpt"; - std::string ps_file = "user.ps"; + std::string cpt_file = "../data/ex3.cpt"; + std::string ps_file = "../data/ex3.ps"; // Initiate a new GMT session // you need to destroy the session later @@ -62,7 +62,7 @@ MAP_ANNOT_OFFSET_PRIMARY=2.5p"; GMT_Call_Module (API, "gmtset", GMT_MODULE_CMD, (char*) args_defaults.c_str()); // load the grid to a virtual file for plotting - char grid_name[GMT_STR16] = {""}; + char grid_name[GMT_VF_LEN] = {""}; GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_IN, G, grid_name); // get string type of grid name std::string grid_name_str = grid_name;