Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

api.rst 191 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
  1. .. set default highlighting language for this document:
  2. .. highlight:: c
  3. .. _api:
  4. =========
  5. GMT C API
  6. =========
  7. Introduction
  8. ============
  9. .. index:: ! API
  10. Preamble
  11. --------
  12. .. figure:: /_images/GMT4_mode.png
  13. :height: 554 px
  14. :width: 1122 px
  15. :align: center
  16. :scale: 50 %
  17. GMT 4 programs contain all the high-level functionality.
  18. Prior to version 5, the bulk of GMT functionality was coded directly
  19. in the standard GMT C program modules (e.g., ``surface.c``, ``grdimage.c``, etc.). The
  20. GMT library only offered access to low-level functions from which
  21. those high-level GMT programs were built. The standard GMT programs
  22. have been very successful, with tens of thousands of users world-wide.
  23. However, the design of the main programs prevented developers from
  24. leveraging GMT functionality from within other programming
  25. environments since access to GMT tools could only be achieved via
  26. system calls [1]_. Consequently, all data i/o had to be done via
  27. temporary files. The design also prevented the GMT developers
  28. themselves from taking advantage of these modules directly. For
  29. instance, the tool :doc:`legend` needed to
  30. make extensive use of system calls to :doc:`plot` and
  31. :doc:`text` in order to plot the lines,
  32. symbols and text that make up a map legend, making it a very awkward
  33. program to maintain.
  34. .. figure:: /_images/GMT5_mode.png
  35. :height: 399 px
  36. :width: 1116 px
  37. :align: center
  38. :scale: 50 %
  39. GMT 5 programs contain all the high-level functionality.
  40. Starting with GMT version 5, all standard GMT programs have been
  41. rewritten into separate function "modules" invoked by a single
  42. driver program called ``gmt.c``.
  43. The :doc:`gmt` executable simply calls the corresponding
  44. GMT modules; it is these modules that do all the work. These new
  45. functions have been placed in a new GMT high-level API library and can
  46. be called from a variety of environments (C/C++, Fortran, Julia, Python,
  47. MATLAB, Visual Basic, R, etc.) [2]_. For example, the main
  48. program ``blockmean.c`` has been reconfigured as a high-level function
  49. ``GMT_blockmean()``, which does the actual spatial averaging and can
  50. pass the result back to the calling program (or write it to file). The
  51. previous behavior of ``blockmean.c`` is achieved by calling ``gmt blockmean``,
  52. i.e., the module is now just the first argument to the :doc:`gmt` executable.
  53. For backwards compatibility with older GMT (4) scripts we optionally
  54. install numerous symbolic links to the gmt executable with names such
  55. as blockmean, plot, surface, etc. The gmt executable is smart enough to
  56. understand when it is being invoked via one of these links and then knows
  57. which module to call upon.
  58. Consequently, ``blockmean.c`` and other files do in
  59. fact no longer exist.
  60. .. figure:: /_images/GMT5_external.png
  61. :height: 616 px
  62. :width: 1193 px
  63. :align: center
  64. :scale: 50 %
  65. GMT 5 API showing current and future external environments.
  66. The i/o abstraction layer
  67. -------------------------
  68. In order for the API to be as flexible as possible we have
  69. generalized the notions of input and output. Data that already reside in
  70. an application's memory may serve as input to a GMT module and we refer
  71. to such data as "Virtual Files". Other
  72. sources of input may be file pointers and file descriptors (as well as
  73. the standard mechanism for passing file names). For standard
  74. data table i/o, the GMT API takes care of the task of assembling any
  75. combination of files, pointers, and memory locations into *a single
  76. virtual data set* from which the GMT module may read (a) all
  77. records at once into memory, or (b) read one record at a time. Likewise,
  78. GMT functions may write their output to a virtual destination, which
  79. might be a memory location in the user's application (another Virtual File), a file pointer or
  80. descriptor, or an output file. The GMT modules are unaware of these
  81. details and simply read from a "source" and write to a "destination".
  82. Thus, the standard concept of file-based input/output so familiar to
  83. any GMT user carries over to the API, except for the generalization
  84. that files can be virtual files already in memory. Because of this
  85. design we will see that we need to associate these virtual files
  86. with special filenames that we may pass to modules, and the modules
  87. will faithfully treat these as real files. However, under the hood
  88. the API layer will take care of the differences between real and
  89. virtual files.
  90. Users who wish to maintain their own data types and memory management
  91. can also use the GMT modules, but some limitations and requirements do
  92. apply: The user's data can either be provided as (1) a 2-D matrix (of
  93. any data type, e.g., float, integer, etc.) and in any memory layout
  94. configuration (e.g., row-major or column-major layout) or as (2) a
  95. set of column vectors that each may be of any type. These custom arrays
  96. will need to be hooked onto the GMT containers :ref:`GMT_MATRIX <struct-matrix>`
  97. and :ref:`GMT_VECTOR <struct-vector>`, respectively.
  98. Such objects can then be treated as virtual files for either input of output.
  99. Our audience
  100. ------------
  101. Here, we document the new functions in the GMT API library for
  102. application developers who wish to call these functions from their own
  103. custom programs. At this point, only the new high-level GMT API is
  104. fully documented and intended for public use. The structure and
  105. documentation of the under-lying lower-level GMT library is not
  106. finalized. Developers using these functions may risk disruption to their
  107. programs due to changes we may make in the library in support of the
  108. GMT API. However, developers who wish to make supplemental packages to
  109. be distributed as part of GMT will (other than talk to us) probably
  110. want to access the entire low-level GMT library as well. It is
  111. unlikely that the low-level library will ever be fully documented.
  112. There are two classes of development that users can pursue:
  113. #. Building stand-alone custom executables that link with the shared GMT
  114. API. Our examples in this documentation are of this kind. There programs
  115. are likely to address a user's special data formats or processing needs
  116. by leveraging high-level GMT modules to do some of the heavy lifting.
  117. #. Building shared library plugins to extend the breath of GMT. Users who
  118. wish to build one or more new modules and distributed then via a plugin
  119. that is dynamically loaded at run-time can now do so. At the present,
  120. all the modules in the official GMT supplement are compiled into a single
  121. plugin that can be accessed at run-time. Similarly, developers may add
  122. additional plugin libraries with any number of GMT-like modules and these
  123. will then be available from the gmt command (as well as from derived
  124. interfaces such as the GMT/MATLAB toolbox and the Python module). An
  125. example of plugin development is given by the
  126. `GSFML extension to GMT <http://www.soest.hawaii.edu/PT/GSFML/>`_.
  127. Definitions
  128. -----------
  129. For the purpose of this documentation a few definitions are needed:
  130. #. "Standard GMT program" refers to one of the traditional stand-alone
  131. command-line executables known to all GMT users, e.g.,
  132. :doc:`blockmean`, :doc:`plot`,
  133. :doc:`grdimage`, etc. Prior to version 5,
  134. these were the only GMT executables available. In GMT 5 and up, these are
  135. accessed via the :doc:`gmt` executable.
  136. #. "\ GMT module" refers to the function in the GMT API library that
  137. is responsible for all the action taken by the corresponding
  138. standard GMT program. All such modules are given the same names as the
  139. corresponding programs e.g., "blockmean", but are invoked via the
  140. ``GMT_Call_Module`` function.
  141. #. "\ GMT application" refers to a new application written by any
  142. developer. It uses the API, perhaps for custom i/o, and may call one
  143. or more GMT functions to create a new GMT-compatible executable.
  144. #. "\ GMT plugin library" refers to a collection of one or more new custom
  145. GMT-like modules that are presented as a plugin library. It such libraries
  146. are placed in the official GMT plugin directory or their path is added to
  147. the GMT defaults parameter :term:`GMT_CUSTOM_LIBS` then the :doc:`gmt` executable can find them.
  148. #. "Family" refers to one of the many high-level GMT data types (e.g., grids, CPTs)
  149. and is typically a required argument to some API functions.
  150. #. "Method" refers to one of several ways in which data can be read or written
  151. in the API, including from existing memory variables.
  152. #. "Direction" is typically either GMT_IN (for reading) or GMT_OUT (for writing).
  153. #. In the API description that follows we will use the type ``int`` to
  154. mean a 4-byte integer. All integers used in the API are 4-byte
  155. integers with the exception of one function where an 8-byte integer is
  156. used. Since different operating systems have their own way of
  157. defining 8-byte integers we use C99's ``int64_t`` for this purpose;
  158. it is guaranteed to yield the correct type that the GMT function
  159. expects.
  160. In version 5, the standard GMT programs are themselves simple invocations
  161. of the :doc:`gmt` application with the function name as argument.
  162. However, some of these modules, such as
  163. :doc:`legend`, :doc:`gmtconvert`,
  164. :doc:`grdblend`,
  165. :doc:`grdfilter` and others may call several additional modules.
  166. API changes from GMT5 to GMT 6
  167. ------------------------------
  168. The API released with GMT5 was considered experimental as our usage of it in GMT proper
  169. as well as in the GMT/MATLAB toolbox and the GMT/Python package would undoubtably lead to
  170. revisions. We developed API to enable GMT access from other environments hence we want
  171. the library to address the needs of such developers. Here are the changes in the GMT 6
  172. API that are not backwards compatible with GMT 5:
  173. #. There is no longer a GMT_TEXTSET resource. Data records are now generalized to
  174. contain an optional leading numerical array followed by an optional trailing text.
  175. A "TEXTSET" in this context is simply a DATASET that has no leading numerical array.
  176. This change was necessary so that all modules reading tables expect the same fundamental
  177. GMT_DATASET resource. The alternative (which we lived to regret) was that developers
  178. calling modules from their environment would have to format their data in different ways
  179. depending on the module, and in some case depending on module options. Now, all table
  180. modules expect GMT_DATASET.
  181. #. The function GMT_Alloc_Segment no longer takes the family of the segment (since there are
  182. now only DATASET segments) but the family variable has been reused as a mode which is
  183. passed as either GMT_WITH_STRINGS or GMT_NO_STRINGS so that data segments can be allocated
  184. with or without the optional string array.
  185. #. We introduce a new structure GMT_RECORD which is used by GMT_Get_Record and GMT_Put_Record.
  186. Because such records may have both leading numerical columns and a trailing string these
  187. functions needed to work with such a structure rather than either an array or string.
  188. #. The unused function GMT_Set_Columns needed to accept *direction* so it could be used for
  189. either input or output. It is rarely needed but some tools that must only read *N* numerical
  190. columns and treat anything beyond that as trailing text (even if numbers) must set the
  191. fixed input columns before reading. We also added one more mode (GMT_COL_FIX_NO_TEXT) to
  192. enforce reading of a fixed number of numerical columns and skip any trailing text.
  193. #. The GMT_DATASET structure has gained a new (hidden) enum GMT_enum_read ``type`` which indicates what
  194. record types were read to produce this dataset (GMT_READ_DATA, GMT_READ_TEXT, GMT_READ_MIXED).
  195. We also changed the geometry from unsigned int to enum GMT_enum_geometry.
  196. #. The long obsolete enums GMT_READ_DOUBLE and GMT_WRITE_DOUBLE have now fully been removed;
  197. use GMT_READ_DATA and GMT_WRITE_DATA instead.
  198. #. The GMT_Convert_Data function's flag array is now of length 2 instead of 3 (because there are no
  199. longer any TEXTSET settings), with what used to be flag3 now being given as flag2.
  200. GMT resources
  201. -------------
  202. The GMT API knows how to create, duplicate, read and write five types of data objects common to
  203. GMT operations: Pure data tables (ASCII or binary), grids, images, color
  204. palette tables (also known as CPT), PostScript documents, and text tables (ASCII,
  205. usually a mix of data and free-form text). In addition, we
  206. provide two data objects to facilitate the passing of simple user arrays
  207. (one or more equal-length data columns of any data type, e.g., double,
  208. char) and 2-D or 3-D user matrices (of any data type and column/row
  209. organization [3]_). We refer to these data types as GMT *resources*.
  210. There are many attributes for each of these resources and therefore we
  211. use a top-level structure for each object to keep them all within one
  212. container. These containers are given or returned by GMT API
  213. functions using opaque pointers (``void *``). Below we provide a brief
  214. overview of these containers, listing only the most critical members.
  215. For complete details, see Appendix A. We will later present how they are used when
  216. importing or exporting them to or from files, memory locations, or
  217. streams. The first five are the standard GMT objects, while the latter
  218. two are special data containers to facilitate the passing of user
  219. data in and out of GMT modules. These resources are defined in the include
  220. file ``gmt_resources.h``; please consult this file to ensure correctness
  221. in case the documentation is not up-to-date. Note than in all instances
  222. the fundamental data variable is called "data".
  223. Data tables
  224. ~~~~~~~~~~~
  225. Much data processed in GMT come in the form of ASCII, netCDF, or
  226. native binary data tables. These may have any number of header records
  227. (ASCII files only) and perhaps segment headers that separate groups of points
  228. or lines and polygons. GMT programs will read
  229. one or more such tables when importing data. However, to avoid memory
  230. duplication or data limitations some programs may prefer to read such records one
  231. at the time. The GMT API has functions that let you read your data
  232. record-by-record by presenting a *virtual* data set that combines all the
  233. data tables specified as input. This simplifies record processing
  234. considerably. Programs reading an entire data set will encounter several
  235. structures: A data set (``struct`` :ref:`GMT_DATASET <struct-dataset>`) may contain any number of
  236. tables (``struct`` :ref:`GMT_DATATABLE <struct-datatable>`), each with any number of segments
  237. (``struct`` :ref:`GMT_DATASEGMENT <struct-datasegment>`), each segment with any number of
  238. records, and each record with any number of (fixed) columns. Thus, the arguments
  239. to GMT API functions that handle such data sets expect a struct :ref:`GMT_DATASET <struct-dataset>`.
  240. All segments are expected to have the same number of columns.
  241. .. _struct-dataset2:
  242. .. code-block:: c
  243. struct GMT_DATASET { /* Single container for an array of GMT tables (files) */
  244. uint64_t n_tables; /* The total number of tables contained */
  245. uint64_t n_columns; /* The number of data columns */
  246. uint64_t n_segments; /* The total number of segments across all tables */
  247. uint64_t n_records; /* The total number of data records across all tables */
  248. double *min; /* Minimum coordinate for each column */
  249. double *max; /* Maximum coordinate for each column */
  250. struct GMT_DATATABLE **table; /* Pointer to array of tables */
  251. };
  252. The top-level dataset structure for pure data tables contains the table structure, as defined below:
  253. .. _struct-datatable2:
  254. .. code-block:: c
  255. struct GMT_DATATABLE { /* Single container for an array of data segments */
  256. unsigned int n_headers; /* Number of table header records (0 if no header) */
  257. uint64_t n_columns; /* Number of columns (fields) in each record */
  258. uint64_t n_segments; /* Number of segments in the array */
  259. uint64_t n_records; /* Total number of data records across all segments */
  260. double *min; /* Minimum coordinate for each column */
  261. double *max; /* Maximum coordinate for each column */
  262. char **header; /* Array with all table header records, if any) */
  263. struct GMT_DATASEGMENT **segment; /* Pointer to array of segments */
  264. };
  265. Finally, the table structure depends on a structure for individual data segments:
  266. .. _struct-datasegment2:
  267. .. code-block:: c
  268. struct GMT_DATASEGMENT { /* For holding segment lines in memory */
  269. uint64_t n_rows; /* Number of points in this segment */
  270. uint64_t n_columns; /* Number of fields in each record (>= 2) */
  271. double *min; /* Minimum coordinate for each column */
  272. double *max; /* Maximum coordinate for each column */
  273. double **data; /* Data x,y, and possibly other columns */
  274. char **text; /* trailing text strings beyond the data */
  275. char *label; /* Label string (if applicable) */
  276. char *header; /* Segment header (if applicable) */
  277. };
  278. Data sets may have different geometries, such as representing a set of points,
  279. one or more lines, or closed polygons.
  280. GMT grids
  281. ~~~~~~~~~
  282. GMT grids are used to represent equidistant and organized 2-D
  283. surfaces. These can be processed or plotted as contour maps, color images, or
  284. perspective surfaces. Because the native GMT grid is simply a 1-D
  285. float array with metadata kept in a separate ``struct`` :ref:`GMT_GRID_HEADER <struct-gridheader>` header, we pass
  286. this information via a ``struct`` :ref:`GMT_GRID <struct-grid>`, which is a container that
  287. holds both items. Thus, the arguments to GMT API functions that handle
  288. GMT grids expect this type of variable.
  289. .. _struct-grid2:
  290. .. code-block:: c
  291. struct GMT_GRID { /* A GMT float grid and header in one container */
  292. struct GMT_GRID_HEADER *header; /* The full GMT header for the grid */
  293. float *data; /* Pointer to the float grid array */
  294. };
  295. The top-level grid structure, holding both header and data array, depends on the grid header structure:
  296. .. code-block:: c
  297. struct GMT_GRID_HEADER {
  298. uint32_t n_columns; /* Number of columns */
  299. uint32_t n_rows; /* Number of rows */
  300. uint32_t registration; /* GMT_GRID_NODE_REG (0) for node grids,
  301. GMT_GRID_PIXEL_REG (1) for pixel grids */
  302. double wesn[4]; /* Min/max x and y coordinates */
  303. double z_min; /* Minimum z value */
  304. double z_max; /* Maximum z value */
  305. double inc[2]; /* The x and y increments */
  306. double z_scale_factor; /* Grid values must be multiplied by this factor */
  307. double z_add_offset; /* After scaling, add this */
  308. char x_units[GMT_GRID_UNIT_LEN80]; /* Units in x-direction */
  309. char y_units[GMT_GRID_UNIT_LEN80]; /* Units in y-direction */
  310. char z_units[GMT_GRID_UNIT_LEN80]; /* Grid value units */
  311. char title[GMT_GRID_TITLE_LEN80]; /* Name of data set */
  312. char command[GMT_GRID_COMMAND_LEN320];/* Name of generating command */
  313. char remark[GMT_GRID_REMARK_LEN160]; /* Comments regarding this data set */
  314. };
  315. The basic grid header holds the metadata written to grid files.
  316. GMT images
  317. ~~~~~~~~~~
  318. GMT images are used to represent bit-mapped images typically obtained
  319. via the GDAL bridge. These can be reprojected internally, such as when
  320. used in :doc:`grdimage`. Since images and grids share the concept of a header,
  321. we use the same header structure for grids as for images; however, some
  322. additional metadata attributes are also needed. Finally, the image
  323. itself may be of any data type and have more than one band (channel).
  324. Both image and header information are passed via a ``struct`` :ref:`GMT_IMAGE <struct-image>`,
  325. which is a container that holds both items. Thus, the arguments to
  326. GMT API functions that handle GMT images expect this type of
  327. variable. Unlike the other objects, writing images has only partial
  328. support via :doc:`grdimage` [4]_.
  329. For the full definition, see :ref:`GMT_IMAGE <struct-image>`.
  330. .. _struct-image2:
  331. .. code-block:: c
  332. struct GMT_IMAGE { /* A GMT char image, header, and colormap in one container */
  333. enum GMT_enum_type type; /* Data type, e.g. GMT_FLOAT */
  334. int *colormap; /* Array with color lookup values */
  335. int n_indexed_colors; /* Number of colors in a color-mapped image */
  336. struct GMT_GRID_HEADER *header; /* Pointer to full GMT header for the image */
  337. unsigned char *data; /* Pointer to actual image */
  338. };
  339. Color palette tables (CPT)
  340. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  341. The color palette table files, or just CPTs, contain colors and
  342. patterns used for plotting data such as surfaces (i.e., GMT grids) or
  343. symbols, lines and polygons (i.e., GMT tables). GMT programs will
  344. generally read in a color palette table, make it the current palette, do
  345. the plotting, and destroy the table when done. The information is
  346. accessed via a pointer to ``struct`` :ref:`GMT_PALETTE <struct-palette>`. Thus, the arguments
  347. to GMT API functions that handle palettes expect this type of
  348. variable. It is not expected that users will wish to manipulate the CPT
  349. directly, but rather use this mechanism to hold them in memory and
  350. pass as arguments to GMT modules. Developers are unlikely to actually
  351. manipulate the contents of CPT structures but if needed then
  352. the full definition can be found in :ref:`GMT_PALETTE <struct-palette>`.
  353. .. _struct-palette2:
  354. .. code-block:: c
  355. struct GMT_PALETTE { /* Holds color-related parameters for look-up */
  356. unsigned int n_headers; /* Number of CPT header records (0 if no header) */
  357. unsigned int n_colors; /* Number of colors in the data array */
  358. unsigned int mode; /* Flags controlling use of BFN colors */
  359. struct GMT_LUT *data; /* CPT lookup data with color information */
  360. struct GMT_BFN bfn[3]; /* Structures with back/fore/nan fills */
  361. char **header; /* Array with all CPT header records, if any) */
  362. };
  363. PostScript document
  364. ~~~~~~~~~~~~~~~~~~~
  365. Normally, GMT modules producing PostScript will write to standard output
  366. or a designated file. Alternatively, you can tell the API to write to a
  367. memory buffer instead and then receive a structure with the final
  368. plot (or partial plot) represented as a long text string.
  369. The full structure definition can be found in :ref:`GMT_POSTSCRIPT <struct-postscript>`.
  370. .. _struct-postscript2:
  371. .. code-block:: c
  372. struct GMT_POSTSCRIPT { /* Single container for a chunk of PostScript text */
  373. unsigned int n_headers; /* Number of PostScript header records (0 if no header) */
  374. size_t n_bytes; /* Length of data array so far */
  375. unsigned int mode; /* Bit-flag for header (1) and trailer (2) */
  376. char *data; /* Pointer to actual PostScript text */
  377. char **header; /* Array with all PostScript header records, if any) */
  378. };
  379. User data matrices
  380. ~~~~~~~~~~~~~~~~~~
  381. Users may write programs that need to call GMT modules but may keep their data in separate
  382. 2-D arrays that the allocate and maintain independent of GMT.
  383. For instance, a program may have built an integer 2-D matrix in memory and wish to
  384. use that as the input grid to the ``grdfilter`` module, which
  385. normally expects a ``struct`` :ref:`GMT_GRID <struct-grid>` with floating point data via an actual or virtual
  386. file. To handle this case we create a ``struct`` :ref:`GMT_MATRIX <struct-matrix>` container (see :ref:`Create empty resources <sec-create>`),
  387. assign the appropriate union pointer to your data matrix and provide information on dimensions
  388. and data type. We then open this container as a virtual file and pass its filename to any module.
  389. The full structure definition can be found in :ref:`GMT_MATRIX <struct-matrix>`.
  390. .. _struct-matrix2:
  391. .. code-block:: c
  392. struct GMT_MATRIX { /* Single container to hold a user matrix */
  393. uint64_t n_rows; /* Number of rows in the matrix */
  394. uint64_t n_columns; /* Number of columns in the matrix */
  395. uint64_t n_layers; /* Number of layers in a 3-D matrix */
  396. enum GMT_enum_fmt shape; /* 0 = C (rows) and 1 = Fortran (cols) */
  397. enum GMT_enum_reg registration; /* 0 for gridline and 1 for pixel registration */
  398. size_t dim; /* Allocated length of longest C or Fortran dim */
  399. size_t size; /* Byte length of data */
  400. enum GMT_enum_type type; /* Data type, e.g. GMT_FLOAT */
  401. double range[6]; /* Contains xmin/xmax/ymin/ymax[/zmin/zmax] */
  402. union GMT_UNIVECTOR data; /* Pointer to actual matrix of the chosen type */
  403. char **text; /* Pointer to optional array of strings [NULL] */
  404. };
  405. The ``enum`` types referenced in :ref:`GMT_VECTOR <struct-vector>` and
  406. Table :ref:`GMT_MATRIX <struct-matrix>` and summarized in Table :ref:`types <tbl-types>`.
  407. User data columns
  408. ~~~~~~~~~~~~~~~~~
  409. Likewise, programs may instead be manipulating a set of custom column vectors.
  410. For instance, the user's program may have allocated and populated
  411. three column arrays of type float and wishes to use these as the input
  412. source to the ``surface`` module, which normally expects double
  413. precision triplets via a ``struct`` :ref:`GMT_DATASET <struct-dataset>` read from an actual or virtual file
  414. Simply create a new :ref:`GMT_VECTOR <struct-vector>` container
  415. (see section :ref:`Create empty resources <sec-create>`) and assign the union array pointers (see
  416. :ref:`univector <struct-univector>`) to your data columns and provide the required
  417. information on length, data types, and optionally range. Again, once we open this data
  418. as a virtual file we can pass its filename to any module expecting such data.
  419. The full structure definition can be found in :ref:`GMT_VECTOR <struct-vector>`.
  420. .. _struct-vector2:
  421. .. code-block:: c
  422. struct GMT_VECTOR { /* Single container to hold user vectors */
  423. uint64_t n_columns; /* Number of vectors */
  424. uint64_t n_rows; /* Number of rows in each vector */
  425. enum GMT_enum_reg registration; /* 0 for gridline and 1 for pixel registration */
  426. enum GMT_enum_type *type; /* Array with data type for each vector */
  427. union GMT_UNIVECTOR *data; /* Array with unions for each column */
  428. double range[2]; /* The min and max limits on t-range (or 0,0) */
  429. char **text; /* Pointer to optional array of strings [NULL] */
  430. };
  431. Data record
  432. ~~~~~~~~~~~
  433. For record-by-record i/o we use the GMT_RECORD structure.
  434. .. _struct-record:
  435. .. code-block:: c
  436. struct GMT_RECORD { /* Single container for an array of GMT tables (files) */
  437. double *data; /* Pointer to array of double-precision numbers [NULL] */
  438. char *text; /* Pointer to the trailing string [NULL] */
  439. };
  440. .. _chapter-overview:
  441. Overview of the GMT C Application Program Interface
  442. ===================================================
  443. Users who wish to create their own GMT application based on the API
  444. must make sure their program goes through the steps below. The details for
  445. each step will be revealed in the following chapter. We have kept the
  446. API simple: In addition to the GMT modules, there are only 57 public
  447. functions to become familiar with, but most applications will only use a
  448. very small subset of this selection. Functions either return an integer error
  449. code (when things go wrong; otherwise it is set to ``GMT_NOERROR (0)``), or they
  450. return a void pointer to a GMT resource (or NULL if things go wrong).
  451. In either case, the API will report what the error is. The layout here
  452. assumes you wish to use virtual files as input sources (i.e., data you already
  453. have in memory); if the data must be
  454. read from actual data files then things simplify considerably.
  455. To keep things as simple as possible we will assume you are writing an
  456. application that will read in table data, call a module using the data in
  457. memory as input, and then save the output from the module back into
  458. another memory location. No actual processing of the data or further
  459. calculation will be done here (so a bit of a boring program, but the
  460. point is to develop something short we can test). Also, to keep the code
  461. short we completely ignore
  462. the return codes of the modules for now. We will call our program
  463. :ref:`example1.c <example-code1>`. Here are the steps:
  464. #. Initialize a new GMT session with GMT_Create_Session_, which
  465. allocates a hidden GMT API control structure and returns an opaque
  466. pointer to it. This pointer is a *required* argument to all subsequent
  467. GMT API function calls within the session.
  468. #. Read a data set (or grid, etc.) into memory with GMT_Read_Data_,
  469. which, depending on data type, returns one of the data structures
  470. discussed earlier.
  471. #. Associate your data with a virtual file using GMT_Open_VirtualFile_.
  472. This steps returns a special filename that you can use to tell a module where
  473. to read its input. No actual file is created.
  474. #. Open a new virtual file to hold the output using GMT_Open_VirtualFile_.
  475. This step also returns a special filename for the module to send its output.
  476. #. Prepare required arguments (including the two virtual file names) and
  477. call the GMT module you wish to use via GMT_Call_Module.
  478. #. Obtain the desired output object via GMT_Read_VirtualFile_, which
  479. returns a data structure of requested type.
  480. #. Close the virtual files you have been using with GMT_Close_VirtualFile_.
  481. #. We terminate the GMT session by calling GMT_Destroy_Session_.
  482. Example code
  483. ------------
  484. For the example code to run you must have Internet access. Compile and run
  485. this program:
  486. .. _example-code1:
  487. .. code-block:: c
  488. #include "gmt.h"
  489. int main (int argc, char *argv[]) {
  490. void *API; /* The API control structure */
  491. struct GMT_DATASET *D = NULL; /* Structure to hold input dataset */
  492. struct GMT_GRID *G = NULL; /* Structure to hold output grid */
  493. char input[GMT_VF_LEN] = {""}; /* String to hold virtual input filename */
  494. char output[GMT_VF_LEN] = {""}; /* String to hold virtual output filename */
  495. char args[128] = {""}; /* String to hold module command arguments */
  496. /* Initialize the GMT session */
  497. API = GMT_Create_Session ("test", 2U, 0, NULL);
  498. /* Read in our data table to memory */
  499. D = GMT_Read_Data (API, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_PLP, GMT_READ_NORMAL, NULL,
  500. "@Table_5_11.txt", NULL);
  501. /* Associate our data table with a virtual file */
  502. GMT_Open_VirtualFile (API, GMT_IS_DATASET, GMT_IS_PLP, GMT_IN, D, input);
  503. /* Create a virtual file to hold the resulting grid */
  504. GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_OUT, NULL, output);
  505. /* Prepare the module arguments */
  506. sprintf (args, "-R0/7/0/7 -I0.2 -D1 -St0.3 %s -G%s", input, output);
  507. /* Call the greenspline module */
  508. GMT_Call_Module (API, "greenspline", GMT_MODULE_CMD, args);
  509. /* Obtain the grid from the virtual file */
  510. G = GMT_Read_VirtualFile (API, output);
  511. /* Close the virtual files */
  512. GMT_Close_VirtualFile (API, input);
  513. GMT_Close_VirtualFile (API, output);
  514. /* Write the grid to file */
  515. GMT_Write_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_READ_NORMAL, NULL,
  516. "junk.nc", G);
  517. /* Destroy the GMT session */
  518. GMT_Destroy_Session (API);
  519. };
  520. Compilation
  521. -----------
  522. To compile this program (we assume it is called example1.c), we use the
  523. gmt-config script to determine the correct compile and link flags and then run
  524. gcc:
  525. .. _example-comp:
  526. .. code-block:: bash
  527. inc=`gmt-config --cflags`
  528. lib=`gmt-config --libs`
  529. gcc example1.c $inc $lib -o example1
  530. ./example1
  531. This obviously assumes you have already installed GMT and that it is in your path.
  532. If you run example1 it will take a moment (this is mostly due to the gridding
  533. performed by :doc:`greenspline`) and then it stops. You should find the resulting
  534. grid junk.nc in the current directory. Plot it to see if it makes sense, e.g.
  535. .. _example-view:
  536. .. code-block:: bash
  537. gmt grdimage junk.nc > junk.ps
  538. If you intend to write applications that take any number of data files
  539. via the command line then there will be more book-keeping to deal with,
  540. and we will discuss those steps later.
  541. Likewise, if you need to process a file record-by-record then more lines
  542. of code will be required.
  543. Plugins
  544. -------
  545. Developers who wish to make custom plugin libraries that are compatible
  546. with GMT should examine the fully functioning examples of more involved
  547. code, available from the repository gmt-custom, obtainable via
  548. .. code-block:: bash
  549. git clone https://github.com/GenericMappingTools/gmt-custom.git
  550. List of API functions
  551. ---------------------
  552. The following is an alphabetical listing of all the public API functions in GMT. Click on
  553. any of them to see the full syntax of each function.
  554. The C/C++ API is deliberately kept small to make it easy to use.
  555. .. _tbl-API:
  556. +--------------------------+-------------------------------------------------------+
  557. | constant | description |
  558. +==========================+=======================================================+
  559. | GMT_Alloc_Segment_ | Allocate data segments |
  560. +--------------------------+-------------------------------------------------------+
  561. | GMT_Append_Option_ | Append new option structure to linked list |
  562. +--------------------------+-------------------------------------------------------+
  563. | GMT_Begin_IO_ | Enable record-by-record i/o |
  564. +--------------------------+-------------------------------------------------------+
  565. | GMT_Call_Module_ | Call any of the GMT modules |
  566. +--------------------------+-------------------------------------------------------+
  567. | GMT_Convert_Data_ | Convert between compatible data types |
  568. +--------------------------+-------------------------------------------------------+
  569. | GMT_Close_VirtualFile_ | Close a virtual file |
  570. +--------------------------+-------------------------------------------------------+
  571. | GMT_Create_Args_ | Convert linked list of options to text array |
  572. +--------------------------+-------------------------------------------------------+
  573. | GMT_Create_Cmd_ | Convert linked list of options to command line |
  574. +--------------------------+-------------------------------------------------------+
  575. | GMT_Create_Data_ | Create an empty data resource |
  576. +--------------------------+-------------------------------------------------------+
  577. | GMT_Create_Options_ | Convert command line options to linked list |
  578. +--------------------------+-------------------------------------------------------+
  579. | GMT_Create_Session_ | Initialize a new GMT session |
  580. +--------------------------+-------------------------------------------------------+
  581. | GMT_Delete_Option_ | Delete an option structure from the linked list |
  582. +--------------------------+-------------------------------------------------------+
  583. | GMT_Destroy_Args_ | Delete text array of arguments |
  584. +--------------------------+-------------------------------------------------------+
  585. | GMT_Destroy_Cmd_ | Delete text command of arguments |
  586. +--------------------------+-------------------------------------------------------+
  587. | GMT_Destroy_Data_ | Delete a data resource |
  588. +--------------------------+-------------------------------------------------------+
  589. | GMT_Destroy_Group_ | Delete a group of data resources |
  590. +--------------------------+-------------------------------------------------------+
  591. | GMT_Destroy_Options_ | Delete the linked list of option structures |
  592. +--------------------------+-------------------------------------------------------+
  593. | GMT_Destroy_Session_ | Terminate a GMT session |
  594. +--------------------------+-------------------------------------------------------+
  595. | GMT_Duplicate_Data_ | Make an identical copy of a data resources |
  596. +--------------------------+-------------------------------------------------------+
  597. | GMT_Encode_Options_ | Encode option arguments for external interfaces |
  598. +--------------------------+-------------------------------------------------------+
  599. | GMT_Error_Message_ | Return character pointer to last API error message |
  600. +--------------------------+-------------------------------------------------------+
  601. | GMT_Expand_Option_ | Expand option with explicit memory references |
  602. +--------------------------+-------------------------------------------------------+
  603. | GMT_End_IO_ | Disable further record-by-record i/o |
  604. +--------------------------+-------------------------------------------------------+
  605. | GMT_FFT_ | Take the Fast Fourier Transform of data object |
  606. +--------------------------+-------------------------------------------------------+
  607. | GMT_FFT_1D_ | Take the Fast Fourier Transform of 1-D float data |
  608. +--------------------------+-------------------------------------------------------+
  609. | GMT_FFT_2D_ | Take the Fast Fourier Transform of 2-D float data |
  610. +--------------------------+-------------------------------------------------------+
  611. | GMT_FFT_Create_ | Initialize the FFT machinery |
  612. +--------------------------+-------------------------------------------------------+
  613. | GMT_FFT_Destroy_ | Terminate the FFT machinery |
  614. +--------------------------+-------------------------------------------------------+
  615. | GMT_FFT_Option_ | Explain the FFT options and modifiers |
  616. +--------------------------+-------------------------------------------------------+
  617. | GMT_FFT_Parse_ | Parse argument with FFT options and modifiers |
  618. +--------------------------+-------------------------------------------------------+
  619. | GMT_FFT_Wavenumber_ | Return wavenumber given data index |
  620. +--------------------------+-------------------------------------------------------+
  621. | GMT_Find_Option_ | Find an option in the linked list |
  622. +--------------------------+-------------------------------------------------------+
  623. | GMT_Get_Common_ | Determine if a GMT common option was set |
  624. +--------------------------+-------------------------------------------------------+
  625. | GMT_Get_Coord_ | Create a coordinate array |
  626. +--------------------------+-------------------------------------------------------+
  627. | GMT_Get_Default_ | Obtain one of the API or GMT default settings |
  628. +--------------------------+-------------------------------------------------------+
  629. | GMT_Get_Enum_ | Obtain one of the API enum constants |
  630. +--------------------------+-------------------------------------------------------+
  631. | GMT_Get_FilePath_ | Verify input file exist and replace with full path |
  632. +--------------------------+-------------------------------------------------------+
  633. | GMT_Get_Index_ | Convert row, col into a grid or image index |
  634. +--------------------------+-------------------------------------------------------+
  635. | GMT_Get_Info_ | Obtain meta data (range, dimension), ... from object |
  636. +--------------------------+-------------------------------------------------------+
  637. | GMT_Get_Matrix_ | Obtain pointer to user matrix from container |
  638. +--------------------------+-------------------------------------------------------+
  639. | GMT_Get_Pixel_ | Get grid or image node |
  640. +--------------------------+-------------------------------------------------------+
  641. | GMT_Get_Record_ | Import a single data record |
  642. +--------------------------+-------------------------------------------------------+
  643. | GMT_Get_Row_ | Import a single grid row |
  644. +--------------------------+-------------------------------------------------------+
  645. | GMT_Get_Status_ | Check status of record-by-record i/o |
  646. +--------------------------+-------------------------------------------------------+
  647. | GMT_Get_Strings_ | Obtain pointer to user strings from matrix or vector |
  648. +--------------------------+-------------------------------------------------------+
  649. | GMT_Get_Values_ | Convert string into coordinates or dimensions |
  650. +--------------------------+-------------------------------------------------------+
  651. | GMT_Get_Vector_ | Obtain pointer to user vector from container |
  652. +--------------------------+-------------------------------------------------------+
  653. | GMT_Get_Version_ | Return the current lib version as a float |
  654. +--------------------------+-------------------------------------------------------+
  655. | GMT_Init_IO_ | Initialize i/o given registered resources |
  656. +--------------------------+-------------------------------------------------------+
  657. | GMT_Init_VirtualFile_ | Reset a virtual file for reuse |
  658. +--------------------------+-------------------------------------------------------+
  659. | GMT_Inquire_VirtualFile_ | Get family of a virtual file |
  660. +--------------------------+-------------------------------------------------------+
  661. | GMT_Make_Option_ | Create an option structure |
  662. +--------------------------+-------------------------------------------------------+
  663. | GMT_Message_ | Issue a message, optionally with time stamp |
  664. +--------------------------+-------------------------------------------------------+
  665. | GMT_Open_VirtualFile_ | Select memory location as input or output for module |
  666. +--------------------------+-------------------------------------------------------+
  667. | GMT_Option_ | Explain one or more GMT common options |
  668. +--------------------------+-------------------------------------------------------+
  669. | GMT_Parse_Common_ | Parse the GMT common options |
  670. +--------------------------+-------------------------------------------------------+
  671. | GMT_Put_Matrix_ | Put user matrix into container |
  672. +--------------------------+-------------------------------------------------------+
  673. | GMT_Put_Record_ | Export a data record |
  674. +--------------------------+-------------------------------------------------------+
  675. | GMT_Put_Row_ | Export a grid row |
  676. +--------------------------+-------------------------------------------------------+
  677. | GMT_Put_Strings_ | Put user strings into vector or matrix container |
  678. +--------------------------+-------------------------------------------------------+
  679. | GMT_Put_Vector_ | Put user vector into container |
  680. +--------------------------+-------------------------------------------------------+
  681. | GMT_Read_Data_ | Import a data resource or file |
  682. +--------------------------+-------------------------------------------------------+
  683. | GMT_Read_Group_ | Import a group of data resources or files |
  684. +--------------------------+-------------------------------------------------------+
  685. | GMT_Read_VirtualFile_ | Access the output from a module via memory |
  686. +--------------------------+-------------------------------------------------------+
  687. | GMT_Register_IO_ | Register a resources for i/o |
  688. +--------------------------+-------------------------------------------------------+
  689. | GMT_Report_ | Issue a message contingent upon verbosity level |
  690. +--------------------------+-------------------------------------------------------+
  691. | GMT_Set_Default_ | Set one of the API or GMT default settings |
  692. +--------------------------+-------------------------------------------------------+
  693. | GMT_Set_Comment_ | Assign a comment to a data resource |
  694. +--------------------------+-------------------------------------------------------+
  695. | GMT_Set_Columns_ | Specify how many columns to use for rec-by-rec i/o |
  696. +--------------------------+-------------------------------------------------------+
  697. | GMT_Set_Geometry_ | Specify data geometry for rec-by-rec i/o |
  698. +--------------------------+-------------------------------------------------------+
  699. | GMT_Set_Index_ | Convert row, col into a grid or image index |
  700. +--------------------------+-------------------------------------------------------+
  701. | GMT_Update_Option_ | Modify an option structure |
  702. +--------------------------+-------------------------------------------------------+
  703. | GMT_Write_Data_ | Export a data resource |
  704. +--------------------------+-------------------------------------------------------+
  705. Summary of all the API functions and their purpose.
  706. The GMT C Application Program Interface
  707. =======================================
  708. Initialize a new GMT session
  709. ----------------------------
  710. Advanced programs may be calling more than one GMT session and thus
  711. run several sessions, perhaps concurrently as different threads on
  712. multi-core machines. We will now discuss these steps in more detail.
  713. Throughout, we will introduce upper-case GMT C enum constants *in
  714. lieu* of simple integer constants. These are considered part of the API
  715. and are available for developers via the ``gmt_resources.h`` include file.
  716. Most applications will need to initialize only a single GMT session.
  717. This is true of all the standard GMT programs since they only call one
  718. GMT module and then exit. Most user-developed GMT applications are
  719. likely to only initialize one session even though they may call many
  720. GMT modules. However, the GMT API supports any number of
  721. simultaneous sessions should the programmer wish to take advantage of
  722. it. This might be useful when you have access to several CPUs and want
  723. to spread the computing load [5]_. In the following discussion we will
  724. simplify our treatment to the use of a single session only.
  725. To initiate the new GMT session we use
  726. .. _GMT_Create_Session:
  727. ::
  728. void *GMT_Create_Session (const char *tag, unsigned int pad, unsigned int mode,
  729. int (*print_func) (FILE *, const char *));
  730. and you will typically call it like this:
  731. ::
  732. void *API = NULL; /* Opaque pointer to GMT controls */
  733. API = GMT_Create_Session ("Session name", 2, 0, NULL);
  734. where ``API`` is an opaque pointer to the hidden GMT API control
  735. structure. You will need to pass this pointer to *all* subsequent
  736. GMT API functions; this is how essential internal information is
  737. passed around. The key task of this initialization is to
  738. set up the GMT machinery and internal variables used for map
  739. projections, plotting, i/o, etc. The initialization also allocates space
  740. for internal structures used to keep track of data. The ``pad`` argument
  741. specifies how many rows and columns should be used as padding for grids and
  742. images so that boundary conditions can be applied. GMT uses 2 and we strongly
  743. recommend that you use that value. In particular, if you choose 0 or 1 there may be certain
  744. GMT modules that will be unable to do their work properly as they count on those
  745. boundary rows and columns in the grids. Note that this setting has no effect
  746. on what is written to a grid file; the padding is an internal feature.
  747. The ``mode`` argument is only used for external APIs that need
  748. to communicate their special needs during the session creation. This integer argument
  749. is a sum of bit flags and the various bits control the following settings:
  750. #. Bit 1 (1 or GMT_SESSION_NOEXIT): If set, then GMT will not call the system exit function when a
  751. serious problem has been detected but instead will simply return control
  752. to the calling environment. For instance, this is required by the GMT/MATLAB toolbox
  753. since calling exit would also exit MATLAB itself. Unless your environment
  754. has this feature you should leave this bit alone.
  755. #. Bit 2 (2 or GMT_SESSION_EXTERNAL): If set, then it means we are calling the GMT API from an external
  756. API, such as MATLAB, Octave, or Python. Normal C/C++ programs should
  757. leave this bit alone. Its effect is to enable two additional modules
  758. for reading and writing GMT resources from these environments (those modules
  759. would not make any sense in a Unix command-line environment).
  760. #. Bit 3 (4 or GMT_SESSION_COLMAJOR): If set, then it means the external API uses a column-major format for
  761. matrices (e.g., MATLAB, Fortran). If not set we default to row-major
  762. format (C/C++, Python, etc.).
  763. #. Big 4 (8 or GMT_SESSION_LOGERRORS): If set, we redirect all error messages to a log file based on the
  764. session name (we append ".log").
  765. #. Bit 5 (16 or GMT_SESSION_RUNMODE): If set, the we enable GMT's modern run-mode (where -O -K are
  766. not allowed and PostScript is written to hidden temp file). Default
  767. is the GMT classic run-mode.
  768. #. Bit 6 (32 or GMT_SESSION_NOHISTORY): If set, the we disable GMT's command shorthand via gmt.history files.
  769. The default is to allow this communication between GMT modules.
  770. The ``print_func`` argument is a pointer to a function that is used to print
  771. messages from GMT via GMT_Message_ or GMT_Report_ from external environments that cannot use the
  772. standard printf function (this is the case for the GMT/MATLAB toolbox, for instance).
  773. For all other uses you should simply pass NULL for this argument. You can also access
  774. the last cached error message by calling GMT_Error_Message_ which returns a pointer to
  775. the internal character buffer with that message. Pass NULL and set the mode bit if you
  776. want writing to a log file instead.
  777. Should something go wrong during the API initialization then ``API`` will be returned as ``NULL``.
  778. Finally, GMT_Create_Session_ will examine the environmental parameter TMPDIR (TEMP on Windows)
  779. to set the GMT temporary directory [/tmp on Unix, current directory on Windows].
  780. Below is a bare-bones minimalistic GMT program hello.c that initializes and destroys
  781. a GMT session:
  782. .. _example-code2:
  783. .. code-block:: c
  784. #include "gmt.h"
  785. int main (int argc, char *argv[]) {
  786. void *API; /* The API control structure */
  787. /* Initialize the GMT session */
  788. API = GMT_Create_Session ("test", 2U, 0, NULL);
  789. /* And now for something original: */
  790. GMT_Message (API, GMT_TIME_NONE, "hello, world\n");
  791. /* Destroy the GMT session */
  792. GMT_Destroy_Session (API);
  793. };
  794. While not super-exiting, this code demonstrates the two essential API calls
  795. required to initiate and later terminate a GMT session. In between we do what
  796. all basic programs are supposed to do: print "Hello, world". The user is of course
  797. allowed to do whatever custom processing before the GMT session is created
  798. and can do all sorts of stuff after the GMT session is destroyed, as long as
  799. no GMT functions or resources are accessed. It may be convenient to isolate
  800. the GMT-specific processing from the custom part of the program and only
  801. maintain an active GMT session when needed.
  802. Get full path to local or remote files
  803. --------------------------------------
  804. If given a filename, GMT will look in several directories to find the given
  805. input file. However, GMT can also look for files remotely, either via the
  806. remote file mechanism or URLs. When you have a remote file (@filename) you
  807. may wish to have GMT automatically download the file and provide you with the
  808. local path. This is a job for GMT_Get_FilePath_, whose prototype is
  809. .. _GMT_Get_FilePath:
  810. ::
  811. int GMT_Get_FilePath (void *API, unsigned int family, unsigned int direction,
  812. unsigned int mode, char **ptr);
  813. where :ref:`family <tbl-family>` and ``direction`` set the data file type and whether it is
  814. for input or output, ``mode`` modifies the behavior of the function, and
  815. ``*ptr`` is a pointer to a character string with the filename in question. Normally,
  816. we only look for local files (GMT_FILE_LOCAL [0]), but if ``mode`` contains
  817. the bit flag GMT_FILE_REMOTE [1] we will try to download any remote files given
  818. to the function. By default, we will replace the filename with the full
  819. path. Add the bit flag GMT_FILE_CHECK [2] to only check for the files and return
  820. error codes but leave ``*ptr`` alone.
  821. Register input or output resources
  822. ----------------------------------
  823. When using the standard GMT programs, it is common to specify input files on the
  824. command line or via special program options (e.g.,
  825. **-I**\ *intensity.nc*). The outputs of the programs are either written
  826. to standard output (which you may redirect to files or pipes into other
  827. programs) or to files specified by specific program options (e.g.,
  828. **-G**\ *output.nc*). Alternatively, the GMT API allows you to specify
  829. input (and output) to be associated with open file handles or virtual files.
  830. We will examine this more closely below. Registering a
  831. resource is a required step before attempting to import or export data
  832. that *do not* come from files or standard input/output.
  833. .. _sec-res_init:
  834. Resource initialization
  835. ~~~~~~~~~~~~~~~~~~~~~~~
  836. All GMT programs dealing with input or output files given on the
  837. command line, and perhaps defaulting to the standard input or output
  838. streams if no files are given, must call the i/o initializer function
  839. GMT_Init_IO_ once for each direction required (i.e., input and output
  840. separately). For input it determines how many input sources have already
  841. been registered. If none has been registered then it scans the program
  842. arguments for any filenames given on the command line and register these
  843. input resources. Finally, if we still have found no input sources we
  844. assign the standard input stream as the single input source. For output
  845. it is similar: If no single destination has been registered we specify
  846. the standard output stream as the output destination. Only one main
  847. output destination is allowed to be active when a module writes data
  848. (some modules also write additional output via program-specific
  849. options). The prototype for this function is
  850. .. _GMT_Init_IO:
  851. ::
  852. int GMT_Init_IO (void *API, unsigned int family, unsigned int geometry,
  853. unsigned int direction, unsigned int mode, unsigned int n_args, void *args);
  854. where :ref:`family <tbl-family>` specifies what kind of resource is to be registered,
  855. :ref:`geometry <tbl-geometry>` specifies the geometry of the data, ``direction`` is either
  856. ``GMT_IN`` or ``GMT_OUT``, and ``mode`` is a bit flag that determines
  857. what we do if no resources have been registered. The choices are
  858. **GMT_ADD_FILES_IF_NONE** (1) means "add command line (option)
  859. files if none have been registered already".
  860. **GMT_ADD_FILES_ALWAYS** (2) means "always add any command line files".
  861. **GMT_ADD_STDIO_IF_NONE** (4) means "add std\* if no other
  862. input/output have been specified".
  863. **GMT_ADD_DEFAULT** (6) means "always add any command line files first, and then
  864. add std\* if no other input/output were specified".
  865. **GMT_ADD_STDIO_ALWAYS** (8) means "always add std\* even if
  866. resources have been registered".
  867. **GMT_ADD_EXISTING** (16) means "only use already registered resources".
  868. The standard behavior is ``GMT_ADD_DEFAULT`` (6). Next, ``n_args`` is 0
  869. if ``args`` is the head of a linked list of options (further discussed
  870. in :ref:`Prepare modules opts <sec-func>`); otherwise ``args`` is an array of ``n_args``
  871. strings (i.e., the int argc, char \*argv[] model)
  872. Many programs will register an export location where results of a GMT function (say, a filtered grid)
  873. should be returned, but may then wish to use that variable as an *input* resource in a subsequent module
  874. call. This is accomplished by re-registering the resource as an *input* source, thereby changing the
  875. *direction* of the data set. The function returns 1 if there is an error; otherwise it returns 0. |ex_resource_init|
  876. Resource registration
  877. ~~~~~~~~~~~~~~~~~~~~~
  878. Should your program need to add additional sources (or a destination) to the list of items
  879. to be considered you will need to register them manually. This is considered a low-level
  880. activity and most applications are unlikely to have to resort to this step. We document
  881. it here in case your situation calls for such action.
  882. Registration involves a direct or indirect call to
  883. .. _GMT_Register_IO:
  884. ::
  885. int GMT_Register_IO (void *API, unsigned int family, unsigned int method,
  886. unsigned int geometry, unsigned int direction, double wesn[], void *ptr);
  887. where :ref:`family <tbl-family>` specifies what kind of resource is to be registered,
  888. :ref:`method <tbl-methods>` specifies
  889. how we to access this resource (see Table :ref:`methods <tbl-methods>` for recognized
  890. methods), :ref:`geometry <tbl-geometry>` specifies the geometry of the data, ``ptr`` is the address of the
  891. pointer to the named resource. If ``direction`` is ``GMT_OUT`` and the
  892. ``method`` is not related to a file (filename, stream, or handle), then
  893. ``ptr`` must be NULL. Note there are some limitations on when you may pass a file pointer
  894. as the method. Many grid file formats cannot be read via a stream (e.g., netCDF files) so in
  895. those situations you cannot pass a file pointer [and GMT_Register_IO would have no way of knowing
  896. this]. For grid (and image)
  897. resources you may request to obtain a subset via the :ref:`wesn <tbl-wesn>` array; otherwise, pass NULL
  898. (or an array with at least 4 items all set to 0) to obtain the
  899. entire grid (or image). The ``direction`` indicates input or output and
  900. is either ``GMT_IN`` or ``GMT_OUT``. Finally, the function returns a
  901. unique resource ID, or ``GMT_NOTSET`` if there was an error.
  902. .. _tbl-family:
  903. +-------------------+---------------------------------+
  904. | family | source points to |
  905. +===================+=================================+
  906. | GMT_IS_DATASET | A [multi-segment] data file |
  907. +-------------------+---------------------------------+
  908. | GMT_IS_GRID | A grid file |
  909. +-------------------+---------------------------------+
  910. | GMT_IS_IMAGE | An image |
  911. +-------------------+---------------------------------+
  912. | GMT_IS_PALETTE | A color palette table [CPT] |
  913. +-------------------+---------------------------------+
  914. | GMT_IS_POSTSCRIPT | A GMT PostScript object |
  915. +-------------------+---------------------------------+
  916. | GMT_IS_MATRIX | A custom user data matrix |
  917. +-------------------+---------------------------------+
  918. | GMT_IS_VECTOR | A custom user data vector |
  919. +-------------------+---------------------------------+
  920. | GMT_VIA_MATRIX | Modifier for grids and datasets |
  921. +-------------------+---------------------------------+
  922. | GMT_VIA_VECTOR | Modifier for grids and datasets |
  923. +-------------------+---------------------------------+
  924. GMT constants used to specify a data family.
  925. .. _tbl-methods:
  926. +------------------+------------------------------------------------+
  927. | method | how to read/write data |
  928. +==================+================================================+
  929. | GMT_IS_FILE | Pointer to name of a file |
  930. +------------------+------------------------------------------------+
  931. | GMT_IS_STREAM | Pointer to open stream (or process) |
  932. +------------------+------------------------------------------------+
  933. | GMT_IS_FDESC | Pointer to integer file descriptor |
  934. +------------------+------------------------------------------------+
  935. | GMT_IS_DUPLICATE | Pointer to memory we may *duplicate* data from |
  936. +------------------+------------------------------------------------+
  937. | GMT_IS_REFERENCE | Pointer to memory we may *reference* data from |
  938. +------------------+------------------------------------------------+
  939. GMT constants used to specify how data will be read or written.
  940. .. _tbl-geometry:
  941. +----------------+-----------------------------------------+
  942. | geometry | description |
  943. +================+=========================================+
  944. | GMT_IS_NONE | Not a geographic feature |
  945. +----------------+-----------------------------------------+
  946. | GMT_IS_POINT | Multi-dimensional point data |
  947. +----------------+-----------------------------------------+
  948. | GMT_IS_LINE | Geographic or Cartesian line segments |
  949. +----------------+-----------------------------------------+
  950. | GMT_IS_POLYGON | Geographic or Cartesian closed polygons |
  951. +----------------+-----------------------------------------+
  952. | GMT_IS_PLP | Either points, lines, or polygons |
  953. +----------------+-----------------------------------------+
  954. | GMT_IS_SURFACE | 2-D gridded surface |
  955. +----------------+-----------------------------------------+
  956. GMT constants used to specify the geometry of the data object.
  957. .. _tbl-wesn:
  958. +---------+----------------------------------------------+
  959. | index | description |
  960. +=========+==============================================+
  961. | GMT_XLO | x_min (west) boundary of grid subset |
  962. +---------+----------------------------------------------+
  963. | GMT_XHI | x_max (east) boundary of grid subset |
  964. +---------+----------------------------------------------+
  965. | GMT_YLO | y_min (south) boundary of grid subset |
  966. +---------+----------------------------------------------+
  967. | GMT_YHI | y_max (north) boundary of grid subset |
  968. +---------+----------------------------------------------+
  969. | GMT_ZLO | z_min (bottom) boundary of 3-D matrix subset |
  970. +---------+----------------------------------------------+
  971. | GMT_ZHI | z_max (top) boundary of 3-D matrix subset |
  972. +---------+----------------------------------------------+
  973. GMT constants used for domain array indexing.
  974. .. _sec-create:
  975. Create empty resources
  976. ----------------------
  977. If your application needs to build and populate GMT resources in ways
  978. that do not depend on external resources (files, memory locations,
  979. etc.), or you have data read in separately and you wish to build a
  980. GMT resource from scratch, then you can obtain an empty object by calling
  981. .. _GMT_Create_Data:
  982. ::
  983. void *GMT_Create_Data (void *API, unsigned int family, unsigned int geometry,
  984. unsigned int mode, uint64_t par[], double *wesn, double *inc,
  985. unsigned int registration, int pad, void *data)
  986. which returns a pointer to the allocated resource. Pass a valid :ref:`family <tbl-family>` selection.
  987. Also pass a compatible :ref:`geometry <tbl-geometry>`. Depending on the family and your particular way of
  988. representing dimensions you may pass the additional parameters in one of
  989. two ways:
  990. #. Actual integer dimensions of items needed (which depends on the ``family``).
  991. #. Physical distances and increments of each dimension.
  992. For the first case you should pass both ``wesn`` and ``inc`` as NULL (or as arrays with elements all set to 0),
  993. and pass the ``par`` array with contents as indicated below:
  994. **GMT_IS_GRID**.
  995. An empty :ref:`GMT_GRID <struct-grid>` structure with a header is allocated; the data
  996. array is NULL. Use ``registration`` to choose either gridline (``GMT_GRID_PIXEL_REG``) or pixel
  997. (``GMT_GRID_NODE_REG``) registration. The domain can be prescribed on one of two ways:
  998. (1) The ``par`` argument is NULL. Then ``wesn`` and ``inc`` can also be NULL but only if the common GMT options
  999. **-R** and **-I** have been set because they are required to get the necessary info. If they
  1000. were not set, then ``wesn`` and ``inc`` must in fact be transmitted. If ``wesn`` and ``inc``
  1001. are set (directly or indirectly) then ``par`` is ignored, even if not NULL.
  1002. (2) The ``par`` argument is not NULL but both ``wesn`` and ``inc`` are NULL.
  1003. Now, ``par[0]`` must have the number of columns and ``par[1]`` must have the number of rows in the grid. Internally,
  1004. ``inc`` will be set to 1/1 and ``wesn`` will be set to 0/n_columns/0/n_rows. As an option, add ``GMT_GRID_XY`` to ``mode``
  1005. and we also allocate the grids's *x* and *y* coordinate vectors.
  1006. **GMT_IS_IMAGE**.
  1007. Same procedure as for **GMT_IS_GRID** but we return an empty :ref:`GMT_IMAGE <struct-image>` object. In either
  1008. way of specification you may use ``par[2]`` to pass the number of image bands [1].
  1009. **GMT_IS_DATASET**.
  1010. We allocate an empty :ref:`GMT_DATASET <struct-dataset>` structure consisting of ``par[0]`` tables,
  1011. each with ``par[1]`` segments, each with ``par[2]`` rows, all with ``par[3]`` columns.
  1012. The ``wesn``, ``inc``, and ``registration`` argument are ignored. The ``data`` argument should be NULL.
  1013. **GMT_IS_PALETTE**.
  1014. We allocate an empty :ref:`GMT_PALETTE <struct-palette>` structure with ``par[0]`` palette entries.
  1015. The ``wesn``, ``inc``, and ``registration`` arguments are ignored and should be NULL/0. The ``data`` argument should be NULL.
  1016. **GMT_IS_POSTSCRIPT**.
  1017. We allocate an empty :ref:`GMT_POSTSCRIPT <struct-postscript>` structure with a text buffer of length ``par[0]``.
  1018. Give ``par[0]`` = 0 if the PostScript string is allocated or obtained by other means.
  1019. The ``wesn``, ``inc``, and ``registration`` arguments are ignored and should be NULL/0. The ``data`` argument should be NULL.
  1020. **GMT_IS_VECTOR**.
  1021. We allocate an empty :ref:`GMT_VECTOR <struct-vector>` structure with ``par[0]`` column entries.
  1022. The number of rows can be specified in one of two ways: (1) Set the number of rows via ``par[1]``. Then,
  1023. ``wesn``, ``inc``, and ``registration`` arguments are ignored.
  1024. (2) Specify ``wesn``, ``inc``, and ``registration`` and the number of rows will be computed from these
  1025. parameters instead. Finally, ``par[2]`` holds the data type of all vectors, if you are allocating them here.
  1026. The ``data`` argument should be NULL. If you have custom vectors you wish to use then
  1027. pass ``par`` but make sure to select mode GMT_CONTAINER_ONLY so that no memory is allocated. Furthermore,
  1028. if you are manually setting up output containers then pass mode as GMT_IS_OUTPUT instead.
  1029. Use GMT_Put_Vector_ to hook up your vectors.
  1030. **GMT_IS_MATRIX**.
  1031. We allocate an empty :ref:`GMT_MATRIX <struct-matrix>` structure. The domain can be prescribed on one of two ways:
  1032. (1) Here, ``par[0]`` is the number of columns while ``par[1]`` has the number of rows. Also,
  1033. ``par[2]`` indicates the number of layers for a 3-D matrix, or pass 0, 1, or NULL for a 2-D matrix.
  1034. Finally, ``par[3]`` holds the data type of the matrix, if you are allocating one.
  1035. (2) Pass ``wesn``, ``inc``, ``registration`` and we compute the dimensions of the matrix.
  1036. The ``data`` argument should be NULL. As for vectors, to use custom data you must (for input) pass the
  1037. mode as GMT_CONTAINER_ONLY and hook your custom matrix in via a call to GMT_Put_Matrix_. The matrix may either
  1038. be row- or column-oriented and this is normally determined when you created the session with GMT_Create_Session_ (see the bit 3 setting).
  1039. However, you can pass ``pad`` = 1 (GMT_IS_ROW_FORMAT; set row major) or ``pad`` = 2 (GMT_IS_COL_FORMAT; set col major) to override the default.
  1040. As for vectors, if this container is for output then pass mode as GMT_IS_OUTPUT instead.
  1041. Users wishing to pass their own data matrices and vectors to GMT modules will need to do so via
  1042. the **GMT_IS_MATRIX** and **GMT_IS_VECTOR** containers. However, no module deals with such containers
  1043. directly (they either expect **GMT_IS_GRID** or **GMT_IS_DATASET**, for instance).
  1044. The solution is to specify the container type the GMT module expects but add in the special
  1045. flags **GMT_VIA_MATRIX** or **GMT_VIA*VECTOR**. This will create the **GMT_IS_MATRIX** or
  1046. **GMT_IS_VECTOR** container the user needs to add the user data, but will also tell GMT how
  1047. they should be considered by the module.
  1048. For grids and images you may pass ``pad`` to set the padding, or -1 to
  1049. accept the prevailing GMT default. The ``mode`` determines what is actually
  1050. allocated when you have chosen grids or images. As for GMT_Read_Data_
  1051. you can pass ``GMT_CONTAINER_AND_DATA`` to initialize the header *and* allocate
  1052. space for the array; here ``data`` must be NULL. Alternatively, you can pass
  1053. ``GMT_CONTAINER_ONLY`` to just initialize the grid or image header,
  1054. and later call GMT_Create_Data a second time, now passing ``GMT_DATA_ONLY``, to allocate
  1055. space for the array. In that second call you pass the pointer returned
  1056. by the first call as ``data`` and specify the family; all other
  1057. arguments should be NULL or 0. Normally, resources created by this
  1058. function are considered to be input (i.e., have a direction that is ``GMT_IN``).
  1059. The exception to this is for containers to hold results from GMT which need have a direction
  1060. set to ``GMT_OUT``. Such empty containers are requested by passing mode = ``GMT_IS_OUTPUT``
  1061. and setting all dimension arguments to 0 or NULL.
  1062. The function returns a pointer to the
  1063. data container. In case of an error we return a NULL pointer and pass an
  1064. error code via ``API->error``.
  1065. Hooking user arrays to objects
  1066. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1067. If you have custom column vector or matrices and you want them to be used as
  1068. input to GMT modules, you will need to create a :ref:`GMT_VECTOR <struct-vector>` or :ref:`GMT_MATRIX <struct-matrix>` container
  1069. and hook your items to them. Likewise, if you want to receive the output of GMT modules
  1070. into user arrays or matrices then you will need to access those data.
  1071. The following utility functions are used for these tasks:
  1072. .. _GMT_Put_Matrix:
  1073. ::
  1074. int GMT_Put_Matrix (void *API, struct GMT_MATRIX *M, unsigned int type, int pad, void *matrix);
  1075. where ``M`` is a :ref:`GMT_MATRIX <struct-matrix>` created by GMT_Create_Data_, the ``type`` is one of the
  1076. recognized data :ref:`types <tbl-types>`, ``pad`` indicates if the matrix has or should have padding,
  1077. and ``matrix`` is your custom matrix. The ``pad`` entry is typically 0 (no pad present), but if you
  1078. intend the matrix to serve as grid input to a module then GMT will expect 2. If your matrix already has
  1079. been extended by 2 extra rows and columns then pass ``pad`` = 2.
  1080. To extract a custom matrix from an output :ref:`GMT_MATRIX <struct-matrix>` you can use
  1081. .. _GMT_Get_Matrix:
  1082. ::
  1083. void *GMT_Get_Matrix (void *API, struct GMT_MATRIX *M);
  1084. which simply returns a pointer to the right union pointer.
  1085. For vectors the same principles apply:
  1086. .. _GMT_Put_Vector:
  1087. ::
  1088. int GMT_Put_Vector (void *API, struct GMT_VECTOR *V, unsigned int col,
  1089. unsigned int type, void *vector);
  1090. where ``V`` is the :ref:`GMT_VECTOR <struct-vector>` created by GMT_Create_Data_, ``col`` is the vector
  1091. column in question, ``type`` is one of the
  1092. recognized data :ref:`types <tbl-types>` used for this vector, and ``vector`` is
  1093. a pointer to this custom vector. In addition, ``type`` may be also **GMT_DATETIME**, in which case
  1094. we expect an array of strings with ISO datetime strings and we do the conversion to internal
  1095. GMT time and allocate a vector to hold the result in the given ``col``.
  1096. To extract a custom vector from an output :ref:`GMT_VECTOR <struct-vector>` you can use
  1097. .. _GMT_Get_Vector:
  1098. ::
  1099. void *GMT_Get_Vector (void *API, struct GMT_VECTOR *V, unsigned int col);
  1100. where ``col`` is the vector number you wish to obtain a pointer to.
  1101. .. _GMT_Get_Version:
  1102. ::
  1103. void *GMT_Get_Version (void *API, unsigned int *major, unsigned int *minor, unsigned int *patch);
  1104. Returns the current lib version as a float, e.g. *6.0*, and optionally its constituints. Either one or all
  1105. of in *\ *major*, *\ *minor*, *\ *patch* args can be NULL. If they are not, one gets the corresponding
  1106. version component. The *API* pointer is actually not used in this function, so passing NULL is the best
  1107. option.
  1108. Finally, for either vectors or matrices you may optionally add a pointer to an
  1109. array of text strings, one per row. This is done via
  1110. .. _GMT_Put_Strings:
  1111. ::
  1112. int GMT_Put_Strings (void *API, unsigned int family, void *X, char **array);
  1113. where ``family`` is either GMT_IS_VECTOR or GMT_IS_MATRIX, ``X`` is either a
  1114. :ref:`GMT_VECTOR <struct-vector>` or :ref:`GMT_MATRIX <struct-matrix>`, and
  1115. ``array`` is the a pointer to your string array.
  1116. To extract the string array from an output vector or matrix container you will use
  1117. .. _GMT_Get_Strings:
  1118. ::
  1119. char **GMT_Get_Strings (void *API, unsigned int family, void *X);
  1120. where again ``family`` is either GMT_IS_VECTOR or GMT_IS_MATRIX and ``X`` is either a
  1121. :ref:`GMT_VECTOR <struct-vector>` or :ref:`GMT_MATRIX <struct-matrix>`.
  1122. Manually add segments
  1123. ~~~~~~~~~~~~~~~~~~~~~
  1124. If you do not know the number of rows in the segments or you expect different segments to have different
  1125. lengths then you should set the row dimension to zero in GMT_Create_Data and add the segments
  1126. manually with ``GMT_Alloc_Segment``, which allocates a new :ref:`GMT_DATASET <struct-dataset>` segment
  1127. for such multi-segment tables.
  1128. .. _GMT_Alloc_Segment:
  1129. ::
  1130. void *GMT_Alloc_Segment (void *API, unsigned int mode,
  1131. uint64_t n_rows, uint64_t n_columns, char *header, void *S);
  1132. where ``header`` is the segment's desired header (or NULL) and `mode` determines if the
  1133. segment should allocate a string array, which in this case should either be ``GMT_NO_STRINGS``
  1134. or ``GMT_WITH_STRINGS``. If ``S`` is not NULL then we simply reallocate the lengths
  1135. of the segment; otherwise a new segment is first allocated.
  1136. There is also the option of controlling the allocation of the segment
  1137. array by setting n_rows = 0. This would allow external arrays (double-precision only) to connect to
  1138. the S->data[col] arrays and not be freed by GMT's garbage collector.
  1139. Get information (meta data) about object
  1140. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1141. If you are creating objects in an environment where the objects are opaque pointers, then it may
  1142. be necessary to inquire about an objects dimension, range, registration, padding, etc. We can
  1143. do this with
  1144. .. _GMT_Get_Info:
  1145. ::
  1146. void *_GMT_Get_Info (void *API, unsigned int family, void *data, unsigned int *geometry,
  1147. uint64_t dim[], double *range, double *inc, unsigned int *registration, int *pad)
  1148. where ``family`` is the type of object referenced by ``data``. Depending on the type of object,
  1149. one or more of ``dim``, ``range``, ``inc``, ``registration``, and ``pad`` will be initialized,
  1150. but only if they do not point to NULL. The function returns an error code if an invalid family
  1151. was selected.
  1152. Duplicate resources
  1153. -------------------
  1154. Often you have read or created a data resource and then need an
  1155. identical copy, presumably to make modifications to. Or, you want a copy
  1156. with the same dimensions and allocated memory, except data values should
  1157. not be duplicated. Alternatively, perhaps you just want to duplicate the
  1158. header and skip the allocation and duplication of the data entirely. These tasks
  1159. are addressed by
  1160. .. _GMT_Duplicate_Data:
  1161. ::
  1162. void *GMT_Duplicate_Data (void *API, unsigned int family, unsigned int mode,
  1163. void *data);
  1164. which returns a pointer to the allocated resource. Specify which
  1165. :ref:`family <tbl-family>` and select ``mode`` from ``GMT_DUPLICATE_DATA``,
  1166. ``GMT_DUPLICATE_ALLOC``, and ``GMT_DUPLICATE_NONE``, as discussed above
  1167. (also see ``mode`` discussion above). For :ref:`GMT_GRID <struct-grid>`
  1168. you may add ``GMT_DUPLICATE_RESET`` which will ensure the duplicate grid
  1169. will have normal padding (useful when the original has non-standard padding).
  1170. For :ref:`GMT_DATASET <struct-dataset>` you can
  1171. add modifiers ``GMT_ALLOC_VERTICAL`` or ``GMT_ALLOC_HORIZONTAL`` to the ``mode`` if you
  1172. wish to put all the data into a single long table or to paste all tables
  1173. side-by-side, respectively (thus getting one wide table instead).
  1174. Additional note for :ref:`GMT_DATASET <struct-dataset>`: Normally we allocate the output given the
  1175. corresponding input dimensions. You can override these by specifying your
  1176. alternative dimensions in the input dataset's variable ``dim[]``.
  1177. The ``data`` is a pointer to the resource you wish to duplicate. In case
  1178. of an error we return a NULL pointer and pass an error code via
  1179. ``API->error``.
  1180. Convert between resource types
  1181. ------------------------------
  1182. Having a resource in memory you may want to convert it to an alternative
  1183. representation. For instance, you may have a :ref:`GMT_DATASET <struct-dataset>`
  1184. but need to strip the information from the
  1185. data into a VECTOR format, dropping all the segment header information, so
  1186. that your custom algorithm or other non-GMT functions can be used on the data.
  1187. In this case you will use
  1188. .. _GMT_Convert_Data:
  1189. ::
  1190. void *GMT_Convert_Data (void *API, void *In, unsigned int family_in,
  1191. void *Out, unsigned int family_out, unsigned int flag[]);
  1192. which returns a pointer to the converted resource. Specify the needed
  1193. :ref:`family <tbl-family>` for both the input and output resources and set the
  1194. (up to) two flags passed via the ``flag`` array. The first ``flag[0]``
  1195. determines how table headers and segment headers should be handled.
  1196. By default (``flag[0]`` = 0) they are preserved (to the extent possible).
  1197. E.g., converting a :ref:`GMT_DATASET <struct-dataset>` to MATRIX always means table headers are
  1198. skipped whereas segment headers are converted to NaN-records. Other
  1199. values for this flag is 1 (Table headers are not copied, segment headers are preserved),
  1200. 2 (Headers are preserved, segment headers are reset to blank), or
  1201. 3 (All headers headers are eliminated). Note that this flag only
  1202. affects duplication of headers. If the new object is written to file at
  1203. a later stage then it is up to the GMT default setting if headers are written
  1204. to file or not.
  1205. The second ``flag[1]`` controls restructuring of tables and segments within
  1206. a set. For ``flag[1]`` = 0 we retain the original layout. Other selections
  1207. are ``GMT_WRITE_TABLE_SEGMENT`` (combine all segments into a *single* segment in a *single* table),
  1208. ``GMT_WRITE_TABLE`` (collect all segments into a *single* table), and ``GMT_WRITE_SEGMENT``
  1209. (combine segments into *one* segment per table).
  1210. Many family combinations are simply not allowed, such as grid to color palette, dataset to image,
  1211. etc.
  1212. Import Data Sets
  1213. ----------------
  1214. If your program needs to import any of the five recognized data types
  1215. (data table, grid, image, CPT, or PostScript) you will use
  1216. the GMT_Read_Data_ or GMT_Read_VirtualFile_ functions. The former
  1217. is typically used when reading from files, streams (e.g., ``stdin``), or
  1218. an open file handle, while the latter is only used to read from memory.
  1219. Because of the similarities of these five
  1220. import functions we use an generic form that covers all of them.
  1221. All input functions takes a parameter called ``mode``. The ``mode``
  1222. parameter generally has different meanings for the different data types
  1223. and will be discussed below. However, one bit setting is common to all
  1224. types: By default, you are only allowed to read a data source once; the
  1225. source is then flagged as having been read and subsequent attempts to
  1226. read from the same source will result in a warning and no reading takes
  1227. place. In the unlikely event you need to re-read a source you can
  1228. override this default behavior by adding ``GMT_IO_RESET`` to your ``mode``
  1229. parameter. Note that this override does not apply to sources that are
  1230. streams or file handles, as it may not be possible to re-read their
  1231. contents.
  1232. Import from a file, stream, or handle
  1233. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1234. To read an entire resource from a file, stream, or file handle, use
  1235. .. _GMT_Read_Data:
  1236. ::
  1237. void *GMT_Read_Data (void *API, unsigned int family, unsigned int method,
  1238. unsigned int geometry, unsigned int mode, double wesn[], const char *input, void *ptr);
  1239. * :ref:`API <GMT_Create_Session>`
  1240. * :ref:`family <tbl-family>`
  1241. * :ref:`method <tbl-methods>`
  1242. * :ref:`geometry <tbl-geometry>`
  1243. * mode -- *see below*
  1244. * :ref:`wesn <tbl-wesn>`
  1245. * input -- a pointer to char holding the file name to read, or NULL if ``stdin``
  1246. * ptr -- NULL or the pointer returned by this function after a first call (when reading grids in two steps)
  1247. * Return: Pointer to data container, or NULL if there were errors (passed back via API->error)
  1248. where ``ptr`` is NULL except when reading grids in two steps (i.e.,
  1249. first get a grid structure with a header, then read the data). Most of
  1250. these arguments have been discussed earlier. This function can be called
  1251. in three different situations:
  1252. #. If you have a single source (filename, stream pointer, etc.) you can
  1253. call GMT_Read_Data_ directly; there is no need to first register
  1254. the source with GMT_Register_IO_ or gather the sources with
  1255. GMT_Init_IO_. Furthermore, for :ref:`GMT_DATASET <struct-dataset>` you can also
  1256. specify a filename that contains UNIX wildcards (e.g., "all_*_[ab]?.txt")
  1257. and these will all be read to produce a single multi-table :ref:`GMT_DATASET <struct-dataset>`
  1258. (for other datatypes, see GMT_Read_Group_ instead).
  1259. #. If you want to specify ``stdin`` as source then pass ``input`` as NULL.
  1260. #. If you already registered all desired sources with GMT_Init_IO_
  1261. then you indicate this choice by passing the invalid ``geometry`` = 0.
  1262. Space will be allocated to hold the results, as needed, and a pointer to
  1263. the object is returned. If there are errors we simply return NULL and
  1264. report the error. The ``mode`` parameter has different meanings for
  1265. different data types.
  1266. **Color palette table**.
  1267. ``mode`` contains bit-flags that control how the CPT's back-,
  1268. fore-, and NaN-colors should be initialized. Select 0 to use the
  1269. CPT resource's back-, fore-, and NaN-colors, 2 to replace these with the current
  1270. GMT default values, or 4 to replace them with the color table's
  1271. entries for highest and lowest value.
  1272. **Data table**.
  1273. ``mode`` is currently not used.
  1274. **Text table**.
  1275. ``mode`` is currently not used.
  1276. **GMT grid** or **image**.
  1277. Here, ``mode`` determines how we read the grid: To read the entire
  1278. grid and its header, pass ``GMT_CONTAINER_AND_DATA``. However, if you may need to
  1279. extract a sub-region you must first read the header by passing
  1280. ``GMT_CONTAINER_ONLY`` with ``wesn`` = NULL, then examine the header structure range
  1281. attributes, specify a subset via the array ``wesn``, and
  1282. finally call GMT_Read_Data_ a second time, now with ``mode`` =
  1283. ``GMT_DATA_ONLY``, passing your ``wesn`` array and the grid
  1284. structure returned from the first call as ``ptr``. In the event your
  1285. data array should be allocated to hold both the real and imaginary
  1286. parts of a complex data set you must add either
  1287. ``GMT_GRID_IS_COMPLEX_REAL`` or ``GMT_GRID_IS_COMPLEX_IMAG`` to
  1288. ``mode`` so as to allow for the extra memory needed and to stride
  1289. the complex value-pairs correctly. If your grid is huge and you must read
  1290. it row-by-row, set ``mode`` to ``GMT_CONTAINER_ONLY`` \|
  1291. ``GMT_GRID_ROW_BY_ROW``. You can then access the grid row-by-row
  1292. using GMT_Get_Row_. By default, the rows will be automatically
  1293. processed in sequential order. To completely specify which row to be read, pass
  1294. ``GMT_GRID_ROW_BY_ROW_MANUAL`` instead.
  1295. Finally, as an option you may add ``GMT_GRID_XY`` to the mode and we also
  1296. allocate the *x* and *y* coordinate vectors for the grid or image.
  1297. *PostScript*.
  1298. ``mode`` is currently not used.
  1299. If you need to read the same resource more than once you should add the
  1300. bit flag ``GMT_IO_RESET`` to the given ``mode``.
  1301. Import a group of data sets
  1302. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1303. To read a group of resources, you may instead use
  1304. .. _GMT_Read_Group:
  1305. ::
  1306. void *GMT_Read_Group (void *API, unsigned int family, unsigned int method,
  1307. unsigned int geometry, unsigned int mode, double wesn[],
  1308. void *input, unsigned int *n_items, void *ptr);
  1309. * :ref:`API <GMT_Create_Session>`
  1310. * :ref:`family <tbl-family>`
  1311. * :ref:`method <tbl-methods>`
  1312. * :ref:`geometry <tbl-geometry>`
  1313. * mode -- *see below*
  1314. * :ref:`wesn <tbl-wesn>`
  1315. * input -- Contents depends on the value of *n_items*. If it is zero then we expect
  1316. a pointer to char holding UNIX wildcard file name(s) to read, otherwise we expect
  1317. a pointer to an array of character strings (*n_items* in total) with names of all
  1318. the files to read. If *n_items* is NULL then we assume 0 but cannot return the number
  1319. found.
  1320. * ptr -- NULL or the pointer returned by this function after a first call (applies when reading grids or images in two steps)
  1321. * Return: Pointer to array of data container, or NULL if there were errors (passed back via API->error)
  1322. where ``ptr`` is NULL except when reading grids in two steps (i.e.,
  1323. first get a grid structures with a header, then read the data arrays). Most of
  1324. these arguments have been discussed earlier. It is useful when you need to read
  1325. a series of files (e.g., from a list with filenames) or want to specify the items
  1326. to read using a UNIX wildcard specification. **Note**: If used with :ref:`GMT_DATASET <struct-dataset>`
  1327. then you will receive an array of structures as well. Typically, many data files
  1328. are read into separate tables that all form part of a single SET (this is what GMT_Read_Data_ does),
  1329. but if GMT_Read_Group_ is used on the same arguments then an array of one-table sets will
  1330. be returned instead. The purpose of your application will dictate which form is more convenient.
  1331. Using user arrays in GMT
  1332. ~~~~~~~~~~~~~~~~~~~~~~~~
  1333. If your program uses a matrix or a set of column vectors to hold data
  1334. and you wish to use such data in a GMT module, you must first create a
  1335. GMT_MATRIX (for matrices) or GMT_VECTOR (for vectors) to hold your arrays.
  1336. In this situation you must pass ``dim`` with the final dimensions of
  1337. your rows and columns when you call GMT_Create_Data_ to make the empty
  1338. containers. You can then use GMT_Put_Matrix_ and GMT_Put_Vector_ to hook
  1339. up your own allocated arrays. It is then these containers that you
  1340. will pass to GMT via *virtual files*. For receiving output from GMT it is
  1341. normal to simply use Open_VirtualFile and have GMT allocate the space needed.
  1342. However, if you want the result to be written to your own arrays or matrix
  1343. then you must call GMT_Create_Data yourself with mode = GMT_IS_OUTPUT and
  1344. specify the dimensions of your array, then (as for input) assign your memory
  1345. to the container using GMT_Put_Matrix_ or GMT_Put_Vector_. Finally, if
  1346. you also need to pass record of strings then see GMT_Put_Strings_ and
  1347. GMT_Get_Strings_.
  1348. Open a virtual file (memory location)
  1349. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1350. If you have read in or otherwise obtained a data object in memory and you
  1351. now wish for it to serve as input to a GMT module, you will have to associate
  1352. that object with a "Virtual File". This step assigns a special filename to the
  1353. memory location and you can then pass this filename to any module that
  1354. needs to read that data. It is similar for writing, except you may pass
  1355. NULL as the object to have GMT automatically allocate the output resource.
  1356. If you want GMT to write to your preallocated memory then you must instead create a
  1357. suitable container first (and pass the dimensions of the arrays) and then
  1358. attach your array(s) using GMT_Put_Matrix_ or GMT_Put_Vector_.
  1359. The full syntax is
  1360. .. _GMT_Open_VirtualFile:
  1361. ::
  1362. void *GMT_Open_VirtualFile (void *API, unsigned int family, unsigned int geometry,
  1363. unsigned int direction, void *data, char *filename);
  1364. Here, ``data`` is the pointer to your memory object. The function returns the
  1365. desired filename via ``filename``. This string must be at least ``GMT_VF_LEN`` bytes (16).
  1366. The other arguments have been discussed earlier. Specifically for direction, use
  1367. GMT_IN for reading and GMT_OUT for writing. Simply pass this filename in
  1368. the calling sequence to the module you want to use to indicate which file should
  1369. be used for reading or writing. Note that if you plan to pass a matrix or vectors
  1370. instead of grids or dataset you must add the modifiers GMT_IS_MATRIX or GMT_IS_VECTOR
  1371. to ``family`` so that the module knows what to do. Finally, in the case of passing
  1372. ``data`` as NULL you may also control what type of matrix or vector will be created in
  1373. GMT for the output by adding in the modifiers GMT_VIA_type, as listed in :ref:`types <tbl-viatypes>`.
  1374. **Note**: GMT tries to minimize data duplication if possible, so if your input arrays are
  1375. compatible with the data type used by the modules then we could use your array directly.
  1376. This *may* have the side-effect that your input array is modified by the module, especially
  1377. if the module writes the results to a netCDF grid file.
  1378. If that is a price you are willing to pay then you can add GMT_IS_REFERENCE to the ``direction``
  1379. argument and we will pass the array internally to avoid duplicating memory. For output it is
  1380. best to pass GMT_IS_REFERENCE as well.
  1381. Import from a virtual file
  1382. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  1383. Once the module completes it will have written its output to the virtual file
  1384. you initialized with GMT_Open_VirtualFile_. To use the actual
  1385. data you will need to "read" it into your program. Of course, the data are already
  1386. in memory but to access it you need to use GMT_Read_VirtualFile_, which expects
  1387. the output filename you obtained from GMT_Open_VirtualFile_. The syntax is
  1388. .. _GMT_Read_VirtualFile:
  1389. ::
  1390. void *GMT_Read_VirtualFile (void *API, char *filename);
  1391. The function requires the output filename via ``filename`` and then returns
  1392. the data object, similar to what GMT_Read_Data_ does.
  1393. Inquire a virtual file for family
  1394. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1395. If you do not know what family is being represented by a virtual file
  1396. then you should first obtain the family via GMT_Inquire_VirtualFile_. The syntax is
  1397. .. _GMT_Inquire_VirtualFile:
  1398. ::
  1399. int GMT_Inquire_VirtualFile (void *API, const char *filename);
  1400. The function requires the virtual file's ``filename`` and then returns the
  1401. family of the data object.
  1402. Reset a virtual file for reuse
  1403. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1404. Should you need to read a virtual file again then you must first reset
  1405. it to its original state with GMT_Init_VirtualFile_. The syntax is
  1406. .. _GMT_Init_VirtualFile:
  1407. ::
  1408. int GMT_Init_VirtualFile (void *API, unsigned int mode, const char *filename);
  1409. The function requires the virtual file's ``filename`` and then resets the
  1410. internal counters (e.g., record numbers and other book-keeping parameters).
  1411. The ``mode`` is presently not used.
  1412. Close a virtual file
  1413. ~~~~~~~~~~~~~~~~~~~~
  1414. Once you have finished using a virtual file you need to close it.
  1415. This will reset its internal settings back to what it was before you
  1416. used it as a virtual file. The syntax is
  1417. .. _GMT_Close_VirtualFile:
  1418. ::
  1419. int GMT_Close_VirtualFile (void *API, char *filename);
  1420. where ``filename`` is the name of the virtual file.
  1421. Record-by-record input
  1422. ----------------------
  1423. In the case of data tables you have the option of selecting
  1424. record-by-record reading or writing. As a general rule, your program
  1425. development simplifies if you can read entire resources into memory with
  1426. GMT_Read_Data_ or GMT_Read_VirtualFile_. However, if this leads to
  1427. unacceptable memory usage or if the program logic is particularly simple,
  1428. you may obtain one data record at the time via GMT_Get_Record_ and write
  1429. one at the time with GMT_Put_Record_. For row-by-row i/o for grids there
  1430. is the corresponding function GMT_Get_Row_. There are additional overhead involved
  1431. in setting up record-by-record processing, which is the topic of this section.
  1432. Enable Data Import
  1433. ~~~~~~~~~~~~~~~~~~
  1434. Once all input resources have been registered, we signal the API that we
  1435. are done with the registration phase and are ready to start the actual
  1436. data import. This step is only required when reading one record at the
  1437. time. We initialize record-by-record reading by calling
  1438. GMT_Begin_IO_. This function enables data
  1439. record-by-record reading and prepares the registered sources for the
  1440. upcoming import. The prototype is
  1441. .. _GMT_Begin_IO:
  1442. ::
  1443. int GMT_Begin_IO (void *API, unsigned int family, unsigned int direction,
  1444. unsigned int header);
  1445. where :ref:`family <tbl-family>` specifies the resource type to be read or written
  1446. (only ``GMT_IS_DATASET`` is
  1447. available for record-by-record handling). The ``direction`` is either
  1448. ``GMT_IN`` or ``GMT_OUT``, so for import we obviously use ``GMT_IN``. The
  1449. function determines the first input source and sets up procedures for
  1450. skipping to the next input source in a virtual data set. The
  1451. GMT_Get_Record_ function will not be able to read any data before
  1452. GMT_Begin_IO_ has been called. As you might guess, there is a
  1453. companion GMT_End_IO_ function that completes, then disables
  1454. record-by-record data access. You can use these several times to switch
  1455. modes between registering data resources, doing the importing/exporting,
  1456. and disabling further data access, perhaps to do more registration. We
  1457. will discuss GMT_End_IO_ once we are done with the data import. The final
  1458. ``header`` argument determines if the common header-block should be
  1459. written during initialization; choose between ``GMT_HEADER_ON`` and
  1460. ``GMT_HEADER_OFF``. The function returns 1 if there is an
  1461. error; otherwise it returns 0.
  1462. Set data geometry
  1463. ~~~~~~~~~~~~~~~~~
  1464. Typically only done for output data written record by record, we designate
  1465. the data set's geometry by calling
  1466. .. _GMT_Set_Geometry:
  1467. ::
  1468. int _GMT_Set_Geometry (void *API, unsigned int direction, unsigned int geometry);
  1469. where ``direction`` is either ``GMT_IN`` or ``GMT_OUT`` and :ref:`geometry <tbl-geometry>`
  1470. sets the geometry that will be produced (or read).
  1471. Importing a data record
  1472. ~~~~~~~~~~~~~~~~~~~~~~~
  1473. If your program will read data table records one-by-one you must first
  1474. enable this input mechanism with GMT_Begin_IO_ and then read the
  1475. records within a loop, repeatedly using
  1476. .. _GMT_Get_Record:
  1477. ::
  1478. void *GMT_Get_Record (void *API, unsigned int mode, int *nfields);
  1479. where the returned value is a pointer to a GMT_RECORD structure, whose
  1480. member pointers data and text point to ephemeral memory
  1481. internal to GMT and should be considered read-only. When we reach
  1482. end-of-file, encounter conversion problems, read header comments, or
  1483. identify segment headers we instead return a NULL pointer. The ``nfields``
  1484. integer pointer will return the number of fields returned; pass NULL if your
  1485. program should ignore this information.
  1486. Normally (i.e., ``mode`` = ``GMT_READ_DATA``), we return a pointer to
  1487. a double array. To read text records, supply instead ``mode`` =
  1488. ``GMT_READ_TEXT`` and we will return a pointer to the text
  1489. record. However, if you have input records that mixes organized
  1490. floating-point columns with text items you could pass ``mode`` =
  1491. ``GMT_READ_MIXED``. Then, GMT will attempt to extract the
  1492. floating-point values from as many columns as needed; you can still access the original record string, as
  1493. discussed below. Finally, if your application needs to be notified when
  1494. GMT closes one file and opens the next, add ``GMT_FILE_BREAK`` to
  1495. ``mode`` and check for the status code ``GMT_IO_NEXT_FILE`` (by default,
  1496. we treat the concatenation of many input files as a single virtual
  1497. file). Using GMT_Get_Record_ requires you to first initialize the
  1498. source(s) with GMT_Init_IO_. For certain records, GMT_Get_Record_
  1499. will return NULL and sets status codes that your program will need to
  1500. examine to take appropriate response. Table :ref:`IO-status <tbl-iostatus>` lists the
  1501. various status codes you can check for, using the ``GMT_Get_Status`` function (see
  1502. next section).
  1503. Examining record status
  1504. ~~~~~~~~~~~~~~~~~~~~~~~
  1505. Programs that read record-by-record must be aware of what the current
  1506. record represents. Given the presence of headers, data gaps, NaN-record,
  1507. etc., the developer may want to check the status after reading the current
  1508. record. The internal i/o status mode can be interrogated with the function
  1509. .. _GMT_Get_Status:
  1510. ::
  1511. int GMT_Get_Status (void *API, unsigned int mode);
  1512. which returns 0 (false) or 1 (true) if the current status is reflected
  1513. by the specified ``mode``. There are 11 different modes available to
  1514. programmers; for a list see Table :ref:`IO-status <tbl-iostatus>` For an example of how
  1515. these may be used, see the test program ``testgmtio.c``. Developers who plan to import
  1516. data on a record-by-record basis may also consult the source code of,
  1517. say, :doc:`blockmean` or :doc:`text`, to see examples of working code.
  1518. .. _tbl-iostatus:
  1519. +-----------------------+--------------------------------------------------------+
  1520. | mode | description and return value |
  1521. +=======================+========================================================+
  1522. | GMT_IO_DATA_RECORD | 1 if we read a data record |
  1523. +-----------------------+--------------------------------------------------------+
  1524. | GMT_IO_TABLE_HEADER | 1 if we read a table header |
  1525. +-----------------------+--------------------------------------------------------+
  1526. | GMT_IO_SEGMENT_HEADER | 1 if we read a segment header |
  1527. +-----------------------+--------------------------------------------------------+
  1528. | GMT_IO_ANY_HEADER | 1 if we read either header record |
  1529. +-----------------------+--------------------------------------------------------+
  1530. | GMT_IO_MISMATCH | 1 if we read incorrect number of columns |
  1531. +-----------------------+--------------------------------------------------------+
  1532. | GMT_IO_EOF | 1 if we reached the end of the file (EOF) |
  1533. +-----------------------+--------------------------------------------------------+
  1534. | GMT_IO_NAN | 1 if we only read NaNs |
  1535. +-----------------------+--------------------------------------------------------+
  1536. | GMT_IO_GAP | 1 if this record implies a data gap |
  1537. +-----------------------+--------------------------------------------------------+
  1538. | GMT_IO_NEW_SEGMENT | 1 if we enter a new segment |
  1539. +-----------------------+--------------------------------------------------------+
  1540. | GMT_IO_LINE_BREAK | 1 if we encountered a segment header, EOF, NaNs or gap |
  1541. +-----------------------+--------------------------------------------------------+
  1542. | GMT_IO_NEXT_FILE | 1 if we finished one file but not the last |
  1543. +-----------------------+--------------------------------------------------------+
  1544. The various modes used to test the status of the record-by-record machinery.
  1545. Importing a grid row
  1546. ~~~~~~~~~~~~~~~~~~~~
  1547. If your program must read a grid file row-by-row you must first enable
  1548. row-by-row reading with GMT_Read_Data_ and then use the
  1549. GMT_Get_Row_ function in a loop; the prototype is
  1550. .. _GMT_Get_Row:
  1551. ::
  1552. int GMT_Get_Row (void *API, int row_no, struct GMT_GRID *G, float *row);
  1553. where ``row`` is a pointer to a pre-allocated single-precision array to receive the
  1554. current row, ``G`` is the grid in question, and ``row_no`` is the number
  1555. of the current row to be read. Note this value is only considered if the
  1556. row-by-row mode was initialized with ``GMT_GRID_ROW_BY_ROW_MANUAL``.
  1557. The user must allocate enough space to hold the entire row in memory.
  1558. Disable Data Import
  1559. ~~~~~~~~~~~~~~~~~~~
  1560. Once the record-by-record input processing has completed we disable
  1561. further input to prevent accidental reading from occurring (due to poor
  1562. program structure, bugs, etc.). We do so by calling GMT_End_IO_. This
  1563. function disables further record-by-record data import; its prototype is
  1564. .. _GMT_End_IO:
  1565. ::
  1566. int GMT_End_IO (void *API, unsigned int direction, unsigned int mode);
  1567. and we specify ``direction`` = ``GMT_IN``. At the moment, ``mode`` is not
  1568. used. This call will also reallocate any arrays obtained into their
  1569. proper lengths. The function returns 1 if there is an error
  1570. (whose code is passed back with ``API->error``), otherwise it returns 0 (``GMT_NOERROR``).
  1571. .. _sec-manipulate:
  1572. Manipulate data
  1573. ---------------
  1574. Once you have created and allocated empty resources, or read in
  1575. resources from the outside, you may wish to manipulate their contents.
  1576. This section discusses how to set up loops and access the important
  1577. variables for each of the supported families. For grids and images it may in addition
  1578. be required to determine what the coordinates are at each node point. This information
  1579. can be obtained via arrays of coordinates for each dimension, obtained by
  1580. .. _GMT_Get_Coord:
  1581. ::
  1582. double *GMT_Get_Coord (void *API, unsigned int family, unsigned int dim,
  1583. void *data);
  1584. where :ref:`family <tbl-family>` must be ``GMT_IS_GRID`` or ``GMT_IS_DATASET``, ``dim`` is either
  1585. ``GMT_IS_X`` or ``GMT_IS_Y``, and ``data`` is the grid or image pointer. This
  1586. function will be used below in our example on grid manipulation.
  1587. Another aspect of dealing with grids and images is to convert a row and column
  1588. 2-D reference to our 1-D array index. Because of grid and image boundary padding
  1589. the relationship is not straightforward, hence we supply
  1590. .. _GMT_Get_Index:
  1591. ::
  1592. int64_t GMT_Get_Index (struct GMT_GRID_HEADER *header, int row, int col);
  1593. where the ``header`` is the header of either a grid or image, and ``row`` and
  1594. ``col`` is the 2-D position in the grid or image. We return the 1-D array
  1595. position; again this function is used below in our example. Likewise, for images
  1596. with many layers we also define
  1597. .. _GMT_Get_Pixel:
  1598. ::
  1599. int64_t GMT_Get_Pixel (struct GMT_GRID_HEADER *header, int row,
  1600. int col, int layer);
  1601. where the ``header`` is the header of an image, and ``row``, ``col`` and
  1602. ``layer`` (= 1 for grids) is the position in the grid or image.
  1603. Manipulate grids
  1604. ~~~~~~~~~~~~~~~~
  1605. Most applications wishing to manipulate grids will want to loop over all
  1606. the nodes, typically in a manner organized by rows and columns. In doing
  1607. so, the coordinates at each node may also be required for a calculation.
  1608. Below is a snippet of code that shows how to do visit all nodes in a
  1609. grid and assign each node the product x \* y:
  1610. ::
  1611. int row, col, node;
  1612. double *x_coord = NULL, *y_coord = NULL;
  1613. /*... create a grid G or read one ... */
  1614. x_coord = GMT_Get_Coord (API, GMT_IS_GRID, GMT_X, G);
  1615. y_coord = GMT_Get_Coord (API, GMT_IS_GRID, GMT_Y, G);
  1616. for (row = 0; row < G->header->n_rows) {
  1617. for (col = 0; col < G->header->n_columns; col++) {
  1618. node = GMT_Get_Index (G->header, row, col);
  1619. G->data[node] = x_coord[col] * y_coord[row];
  1620. }
  1621. }
  1622. Note the use of GMT_Get_Index_ to get the grid node number associated
  1623. with the ``row`` and ``col`` we are visiting. Because GMT grids have
  1624. padding (for boundary conditions) the relationship between rows,
  1625. columns, and node indices is more complicated and hence we hide that
  1626. complexity in GMT_Get_Index_. Note that for trivial procedures such
  1627. setting all grid nodes to a constant (e.g., -9999.0) where the row and
  1628. column does not enter you can instead do a single loop:
  1629. ::
  1630. int node;
  1631. /*... create a grid G or read one ... */
  1632. for (node = 0; node < G->header->size) G->data[node] = -9999.0;
  1633. Note we must use ``G->header->size`` (size of allocated array) and not
  1634. ``G->header->nm`` (number of nodes in grid) since the latter is smaller
  1635. due to the padding and a single loop like the above treats the pad as
  1636. part of the "inside" grid. Replacing ``size`` by ``nm`` would be a bug.
  1637. Manipulate data tables
  1638. ~~~~~~~~~~~~~~~~~~~~~~
  1639. Another common application is to process the records in a data table.
  1640. Because GMT considers the :ref:`GMT_DATASET <struct-dataset>` resources to contain one or more
  1641. tables, each of which may contain one or more segments, all of which may
  1642. contain one or more columns, you will need to have multiple nested loops to
  1643. visit all entries. The following code snippet will visit all data
  1644. records and add 1 to all columns beyond the first two (x and y), and if
  1645. the data has a trailing string it will print it to stdout:
  1646. ::
  1647. uint64_t tbl, seg, row, col;
  1648. struct GMT_DATATABLE *T = NULL;
  1649. struct GMT_DATASEGMENT *S = NULL;
  1650. /* ... create a dataset D or read one ... */
  1651. for (tbl = 0; tbl < D->n_tables; tbl++) { /* For each table */
  1652. T = D->table[tbl]; /* Convenient shorthand for current table */
  1653. for (seg = 0; seg < T->n_segments; seg++) { /* For all segments */
  1654. S = T->segment[seg]; /* Convenient shorthand for current segment */
  1655. for (row = 0; row < S->n_rows; row++) { /* For all rows in segment */
  1656. for (col = 2; col < T->n_columns; col++) { /* For all cols > 1 */
  1657. S->data[col][row] += 1.0; /* Just add one */
  1658. }
  1659. if (S->text) printf ("Row %d has string: %s\n", (int)row, S->text[row]);
  1660. }
  1661. }
  1662. }
  1663. Message and Verbose Reporting
  1664. -----------------------------
  1665. The API provides two functions for your program to present information
  1666. to the user during the run of the program. One is used for messages that
  1667. are always written (optionally with a time stamp) while the other is used
  1668. for reports whose verbosity level must exceed the verbosity settings specified via **-V**.
  1669. Verbose reporting
  1670. ~~~~~~~~~~~~~~~~~
  1671. .. _GMT_Report:
  1672. ::
  1673. int GMT_Report (void *API, unsigned int level, const char *message, ...);
  1674. This function takes a verbosity level and a multi-part message (e.g., a
  1675. format statement and zero or more variables as required by the format string). The verbosity ``level`` is
  1676. an integer in the 0–5 range; these levels are listed in Table :ref:`timemodes <tbl-verbosity>`
  1677. You assign an appropriate verbosity level to your message, and depending
  1678. on the chosen run-time verbosity level set via **-V** your message may
  1679. or may not be reported. Only messages whose stated verbosity level is
  1680. lower or equal to the **-V**\ *level* will be printed. These messages are typically
  1681. progress reports, etc., and are sent to standard error.
  1682. .. _tbl-verbosity:
  1683. +----------------------+--------------------------------------+
  1684. | constant | description |
  1685. +======================+======================================+
  1686. | GMT_MSG_QUIET | Quiet; no messages whatsoever |
  1687. +----------------------+--------------------------------------+
  1688. | GMT_MSG_ERROR | Error messages only |
  1689. +----------------------+--------------------------------------+
  1690. | GMT_MSG_WARNING | Warnings |
  1691. +----------------------+--------------------------------------+
  1692. | GMT_MSG_TICTOC | Time usage for slow algorithms |
  1693. +----------------------+--------------------------------------+
  1694. | GMT_MSG_INFORMATION | Informational messages |
  1695. +----------------------+--------------------------------------+
  1696. | GMT_MSG_COMPAT | Compatibility warnings |
  1697. +----------------------+--------------------------------------+
  1698. | GMT_MSG_DEBUG | Debug messages for developers mostly |
  1699. +----------------------+--------------------------------------+
  1700. The different levels of verbosity that can be selected.
  1701. Error string
  1702. ~~~~~~~~~~~~
  1703. .. _GMT_Error_Message:
  1704. ::
  1705. char * GMT_Error_Message (void *API);
  1706. This function simply returns a character pointer to the internal error message
  1707. buffer holding the last error message generated.
  1708. User messages
  1709. ~~~~~~~~~~~~~
  1710. For custom messages to the user that should always be printed, we use
  1711. .. _GMT_Message:
  1712. ::
  1713. int GMT_Message (void *API, unsigned int mode, const char *format, ...);
  1714. This function always prints its message to the standard output. Use the
  1715. ``mode`` value to control if a time stamp should preface the message,
  1716. and if selected how the time information should be formatted. See
  1717. Table :ref:`timemodes <tbl-timemodes>` for the various modes.
  1718. .. _tbl-timemodes:
  1719. +------------------+---------------------------------------+
  1720. | constant | description |
  1721. +==================+=======================================+
  1722. | GMT_TIME_NONE | Display no time information |
  1723. +------------------+---------------------------------------+
  1724. | GMT_TIME_CLOCK | Display current local time |
  1725. +------------------+---------------------------------------+
  1726. | GMT_TIME_ELAPSED | Display elapsed time since last reset |
  1727. +------------------+---------------------------------------+
  1728. | GMT_TIME_RESET | Reset the elapsed time to 0 |
  1729. +------------------+---------------------------------------+
  1730. The different types of message modes.
  1731. Special GMT modules
  1732. -------------------
  1733. There are some differences between calling
  1734. modules on the command line and using them via the API. These are discussed here.
  1735. API-only modules
  1736. ~~~~~~~~~~~~~~~~
  1737. There are two general-purpose modules that are not part of the command-line version of
  1738. GMT. These are the read and write modules. Both take an option to specify what GMT
  1739. resource is being read of written: **-Tc**\|\ **d**\|\ **g**\|\ **i**\|\ **p**,
  1740. which selects CPT, dataset, grid, image, or PostScript, respectively. In addition
  1741. both modules accept the *infile* and *outfile* argument for source and destination. These
  1742. may be actual files of memory locations, of course.
  1743. PostScript Access
  1744. ~~~~~~~~~~~~~~~~~
  1745. The GMT module :doc:`psconvert` is normally given one or more PostScript files that may be
  1746. converted to other formats. When accessed by the API it may also be given the special
  1747. file name "=", which means we are to use the internal PostScript string produced by
  1748. the latest GMT plotting instead of any actual file name. The module can access this
  1749. string which must be a complete plot (i.e., it must have header, middle, and trailer
  1750. and thus be a valid PostScript file). This allows the API to convert plots to a
  1751. suitable image format without any duplication and manipulation of the PostScript
  1752. itself.
  1753. Adjusting headers and comments
  1754. ------------------------------
  1755. All header records in incoming datasets are stored in memory. You may
  1756. wish to replace these records with new information, or append new
  1757. information to the existing headers. This is achieved with
  1758. .. _GMT_Set_Comment:
  1759. ::
  1760. int GMT_Set_Comment (void *API, unsigned int family, unsigned int mode,
  1761. void *arg, void *data)
  1762. Again, :ref:`family <tbl-family>` selects which kind of resource is passed via ``data``.
  1763. The ``mode`` determines what kind of comment is being considered, how it
  1764. should be included, and in what form the comment passed via ``arg`` is provided.
  1765. Table :ref:`comments <tbl-comments>` lists the available options, which may be combined
  1766. by adding (bitwise "or"). The GMT_Set_Comment_ function does not actually
  1767. output anything but sets the relevant comment and header records in the
  1768. relevant structure. When a file is written out the information will be
  1769. output as well (**Note**: Users can always decide if they wish to turn
  1770. header output on or off via the common GMT option ``-h``. For
  1771. record-by-record writing you must enable the header block output when
  1772. you call GMT_Begin_IO_.
  1773. .. _tbl-comments:
  1774. +-------------------------+---------------------------------------------------+
  1775. | constant | description |
  1776. +=========================+===================================================+
  1777. | GMT_COMMENT_IS_TEXT | Comment is a text string |
  1778. +-------------------------+---------------------------------------------------+
  1779. | GMT_COMMENT_IS_OPTION | Comment is a linked list of GMT_OPTION structures |
  1780. +-------------------------+---------------------------------------------------+
  1781. | GMT_COMMENT_IS_COMMAND | Comment is the command |
  1782. +-------------------------+---------------------------------------------------+
  1783. | GMT_COMMENT_IS_REMARK | Comment is the remark |
  1784. +-------------------------+---------------------------------------------------+
  1785. | GMT_COMMENT_IS_TITLE | Comment is the title |
  1786. +-------------------------+---------------------------------------------------+
  1787. | GMT_COMMENT_IS_NAME_X | Comment is the x variable name (grids only) |
  1788. +-------------------------+---------------------------------------------------+
  1789. | GMT_COMMENT_IS_NAME_Y | Comment is the y variable name (grids only) |
  1790. +-------------------------+---------------------------------------------------+
  1791. | GMT_COMMENT_IS_NAME_Z | Comment is the z variable name (grids only) |
  1792. +-------------------------+---------------------------------------------------+
  1793. | GMT_COMMENT_IS_COLNAMES | Comment is the column names header |
  1794. +-------------------------+---------------------------------------------------+
  1795. | GMT_COMMENT_IS_RESET | Comment replaces existing information |
  1796. +-------------------------+---------------------------------------------------+
  1797. The modes for setting various comment types.
  1798. The named modes (*command*, *remark*, *title*, *name_x,y,z* and
  1799. *colnames* are used to distinguish regular text comments from specific
  1800. fields in the header structures of the data resources, such as
  1801. :ref:`GMT_GRID <struct-grid>`. For the various table resources (e.g., :ref:`GMT_DATASET <struct-dataset>`)
  1802. these modifiers result in a specially formatted comments beginning with
  1803. "Command: " or "Remark: ", reflecting how this type of information is
  1804. encoded in the headers.
  1805. Export Data Sets
  1806. ----------------
  1807. If your program needs to write any of the five recognized data types
  1808. (CPTs, data tables, grids, images, or PostScript) you can use the
  1809. GMT_Write_Data_ function.
  1810. Both of these output functions takes a parameter called ``mode``. The
  1811. ``mode`` parameter generally takes on different meanings for the
  1812. different data types and will be discussed below. However, one bit
  1813. setting is common to all types: By default, you are only allowed to
  1814. write a data resource once; the resource is then flagged to have been
  1815. written and subsequent attempts to write to the same resource will
  1816. quietly be ignored. In the unlikely event you need to re-write a
  1817. resource you can override this default behavior by adding ``GMT_IO_RESET``
  1818. to your ``mode`` parameter.
  1819. Exporting a data set
  1820. ~~~~~~~~~~~~~~~~~~~~
  1821. To have your program accept results from GMT modules and write them
  1822. separately requires you to use the GMT_Write_Data_ function. It is very similar to the
  1823. GMT_Read_Data_ function encountered earlier.
  1824. Exporting a data set to a file, stream, or handle
  1825. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1826. The prototype for writing to a file (via name, stream, or file handle) is
  1827. .. _GMT_Write_Data:
  1828. ::
  1829. int GMT_Write_Data (void *API, unsigned int family, unsigned int method,
  1830. unsigned int geometry, unsigned int mode, double wesn[], void *output, void *data);
  1831. * :ref:`API <GMT_Create_Session>`
  1832. * :ref:`family <tbl-family>`
  1833. * :ref:`method <tbl-methods>`
  1834. * :ref:`geometry <tbl-geometry>`
  1835. * mode -- specific to each data type (\ *see below*)
  1836. * :ref:`wesn <tbl-wesn>`
  1837. * output --
  1838. * data -- A pointer to any of the five families.
  1839. * Return: 0 on success, otherwise return -1 and set API->error to reflect to cause.
  1840. where ``data`` is a pointer to any of the four structures discussed previously.
  1841. **Color palette table**
  1842. ``mode`` controls if the CPT's back-, fore-, and NaN-colors
  1843. should be written (1) or not (0).
  1844. **Data table**
  1845. If ``method`` is ``GMT_IS_FILE``, then the value of ``mode`` affects
  1846. how the data set is written:
  1847. **GMT_WRITE_SET**
  1848. The entire data set will be written to the single file [0].
  1849. **GMT_WRITE_TABLE**
  1850. Each table in the data set is written to individual files [1].
  1851. You can either specify an output file name that *must* contain
  1852. one C-style format specifier for an int variable (e.g.,
  1853. "New_Table_%06d.txt"), which will be replaced with the table
  1854. number (a running number from 0) *or* you must assign to each
  1855. table *i* a unique output file name via the
  1856. ``D->table[i]->file[GMT_OUT]`` variables prior to calling the
  1857. function.
  1858. **GMT_WRITE_SEGMENT**
  1859. Each segment in the data set is written to an individual file
  1860. [2]. Same setup as for ``GMT_WRITE_TABLE`` except we use
  1861. sequential segment numbers to build the file names.
  1862. **GMT_WRITE_TABLE_SEGMENT**
  1863. Each segment in the data set is written to an individual file
  1864. [3]. You can either specify an output file name that *must*
  1865. contain two C-style format specifiers for two int variables
  1866. (e.g., "New_Table_%06d_Segment_%03d.txt"), which will be
  1867. replaced with the table and segment numbers, *or* you must
  1868. assign to each segment *j* in each table *i* a unique output
  1869. file name via the ``D->table[i]->segment[j]->file[GMT_OUT]``
  1870. variables prior to calling the function.
  1871. **GMT_WRITE_OGR**
  1872. Writes the dataset in OGR/GMT format in conjunction with the
  1873. ``-a`` setting [4].
  1874. **Text table**
  1875. The ``mode`` is used the same way as for data tables.
  1876. **GMT grid**
  1877. Here, ``mode`` may be ``GMT_CONTAINER_ONLY`` to only update a
  1878. file's header structure, but normally it is simply ``GMT_CONTAINER_AND_DATA``
  1879. so the entire grid and its header will be exported (a subset is
  1880. not allowed during export). However, in the event your data array
  1881. holds both the real and imaginary parts of a complex data set you
  1882. must add either ``GMT_GRID_IS_COMPLEX_REAL`` or
  1883. ``GMT_GRID_IS_COMPLEX_IMAG`` to ``mode`` so as to export the
  1884. corresponding grid values correctly. Finally, for native binary
  1885. grids you may skip writing the grid header by adding
  1886. ``GMT_GRID_NO_HEADER``; this setting is ignored for all other grid
  1887. formats. If your output grid is huge and you are building it
  1888. row-by-row, set ``mode`` to ``GMT_CONTAINER_ONLY`` \|
  1889. ``GMT_GRID_ROW_BY_ROW``. You can then write the grid row-by-row
  1890. using GMT_Put_Row_. By default the rows will be automatically
  1891. processed in order. To completely specify which row to be written,
  1892. use ``GMT_GRID_ROW_BY_ROW_MANUAL`` instead; this requires a file format
  1893. that supports direct writes, such as netCDF. Finally, if you are
  1894. preparing a geographic grid outside of GMT you need to add the mode
  1895. ``GMT_GRID_IS_GEO`` to ensure that the proper metadata will be written
  1896. to the netCDF header, thus letting the grid be recognized as such.
  1897. **Note**: If ``method`` is GMT_IS_FILE, :ref:`family <tbl-family>` is ``GMT_IS_GRID``,
  1898. and the filename implies a change from NaN to another value then the grid is
  1899. modified accordingly. If you continue to use that grid after writing please be
  1900. aware that the changes you specified were applied to the grid.
  1901. Record-by-record output
  1902. -----------------------
  1903. In the case of data tables, you may also
  1904. consider the GMT_Put_Record_ function for record-by-record writing. As a general rule, your
  1905. program organization may simplify if you can write the entire
  1906. resource with GMT_Write_Data_. However, if the program logic is simple
  1907. or already involves using GMT_Get_Record_, it may be better to export
  1908. one data record at the time via GMT_Put_Record_. For grids there is the
  1909. corresponding GMT_Put_Row_ function.
  1910. Enable Data Export
  1911. ~~~~~~~~~~~~~~~~~~
  1912. Similar to the data import procedures, once all output destinations have
  1913. been registered, we signal the API that we are done with the
  1914. registration phase and are ready to start the actual data export. As for
  1915. input, this step is only needed when dealing with record-by-record
  1916. writing. Again, we enable record-by-record writing by calling
  1917. GMT_Begin_IO_, this time with ``direction`` = ``GMT_OUT``. This function
  1918. enables data export and prepares the registered destinations for the
  1919. upcoming writing.
  1920. Specifying the number of output columns
  1921. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1922. For record-based ASCII input/output you will need to specify the number of
  1923. columns, unless for output it equals the number of input columns. This is done with
  1924. the GMT_Set_Columns_ function:
  1925. .. _GMT_Set_Columns:
  1926. ::
  1927. void *GMT_Set_Columns (void *API, unsigned int direction, unsigned int n_columns, unsigned int mode);
  1928. The ``n_columns`` is a number related to the number of columns you plan to read/write, while
  1929. ``mode`` controls what that number means. For input, ``mode`` = ``GMT_COL_FIX`` sets the actual
  1930. number of numerical columns to read. Anything beyond is considered trailing text and is parsed unless
  1931. you use ``GMT_COL_FIX_NO_TEXT`` instead. If your records have variable number of numerical columns
  1932. then you may use ``GMT_COL_VAR``. For output, you can also select from
  1933. other modes. Here, ``mode`` = ``GMT_COL_ADD`` means it should be added to the known number
  1934. of input columns to arrive at the number of final output columns, while ``mode`` = ``GMT_COL_SUB``
  1935. means this value should be subtracted from the number of input columns to find the number of
  1936. output columns.
  1937. Exporting a data record
  1938. ~~~~~~~~~~~~~~~~~~~~~~~
  1939. If your program must write data table records one-by-one you must first
  1940. enable record-by-record writing with GMT_Begin_IO_ and then use the
  1941. ``GMT_Put_Record`` function in a loop; the prototype is
  1942. .. _GMT_Put_Record:
  1943. ::
  1944. int GMT_Put_Record (void *API, unsigned int mode, void *rec);
  1945. where ``rec`` is a pointer to (a) a GMT_RECORD structure for
  1946. the current row. Alternatively (b), ``rec``
  1947. points to a text string. The ``mode`` parameter must be set to reflect
  1948. what is passed. Using GMT_Put_Record_ requires you to first
  1949. initialize the destination with GMT_Init_IO_. Note that for
  1950. ``GMT_IS_DATASET`` the methods ``GMT_IS_DUPLICATE`` and
  1951. ``GMT_IS_REFERENCE`` are not supported since you can simply populate the
  1952. :ref:`GMT_DATASET <struct-dataset>` structure directly. As mentioned, ``mode`` affects what is
  1953. actually written:
  1954. **GMT_WRITE_DATA**.
  1955. Normal operation that builds the current output record from the numerical values in ``rec``.
  1956. **GMT_WRITE_TABLE_HEADER**.
  1957. For ASCII output mode we write the text string ``rec``. If ``rec``
  1958. is NULL then we write the last read header record. If binary
  1959. output mode we quietly skip writing this record.
  1960. **GMT_WRITE_SEGMENT_HEADER**.
  1961. For ASCII output mode we use the text string ``rec`` as the
  1962. segment header. If ``rec`` is NULL then we use the current (last
  1963. read) segment header record. If binary output mode instead we write
  1964. a record composed of NaNs.
  1965. The function returns 1 if there was an error associated with the
  1966. writing (which is passed back with ``API->error``), otherwise it returns
  1967. 0 (``GMT_NOERROR``).
  1968. Exporting a grid row
  1969. ~~~~~~~~~~~~~~~~~~~~
  1970. If your program must write a grid file row-by-row you must first enable
  1971. row-by-row writing with GMT_Read_Data_ and then use the
  1972. GMT_Put_Row_ function in a loop; the prototype is
  1973. .. _GMT_Put_Row:
  1974. ::
  1975. int GMT_Put_Row (void *API, int row_no, struct GMT_GRID *G, float *row);
  1976. where ``row`` is a pointer to a single-precision array with the current
  1977. row, ``G`` is the grid in question, and ``row_no`` is the number of the
  1978. current row to be written. Note this value is only considered if the
  1979. row-by-row mode was initialized with ``GMT_GRID_ROW_BY_ROW_MANUAL``.
  1980. Disable Data Export
  1981. ~~~~~~~~~~~~~~~~~~~
  1982. Once the record-by-record output has completed we disable further output
  1983. to prevent accidental writing from occurring (due to poor program
  1984. structure, bugs, etc.). We do so by calling GMT_End_IO_. This
  1985. function disables further record-by-record data export; here, we
  1986. obviously pass ``direction`` as ``GMT_OUT``.
  1987. Destroy allocated resources
  1988. ---------------------------
  1989. If your session imported any data sets into memory then you may
  1990. explicitly free this memory once it is no longer needed and before
  1991. terminating the session. This is done with the GMT_Destroy_Data_
  1992. function, whose prototype is
  1993. .. _GMT_Destroy_Data:
  1994. ::
  1995. int GMT_Destroy_Data (void *API, void *data);
  1996. where ``data`` is the address of the pointer to a data container, i.e., not
  1997. the pointer to the container but the *address* of that pointer (e.g. &pointer). Note that
  1998. when each module completes it will automatically free memory created by
  1999. the API; similarly, when the session is destroyed we also automatically
  2000. free up memory. Thus, ``GMT_Destroy_Data`` is therefore generally only
  2001. needed when you wish to directly free up memory to avoid running out of
  2002. it. The function returns 1 if there is an error when trying to
  2003. free the memory (the error code is passed back with ``API->error``),
  2004. otherwise it returns 0 (``GMT_NOERROR``).
  2005. Destroy groups of allocated resources
  2006. -------------------------------------
  2007. If you obtained an array of resources via GMT_Read_Group_ then
  2008. you will need to destroy these resources with GMT_Destroy_Group_ instead,
  2009. whose prototype is
  2010. .. _GMT_Destroy_Group:
  2011. ::
  2012. int GMT_Destroy_Group (void *API, void *data, unsigned int n);
  2013. where ``data`` is the address of the array with data containers, i.e., not
  2014. the array to the containers but the *address* of that array (e.g. &array),
  2015. and ``n`` is the number of containers.
  2016. Terminate a GMT session
  2017. -----------------------
  2018. Before your program exits it should properly terminate the
  2019. GMT session, which involves a call to
  2020. .. _GMT_Destroy_Session:
  2021. ::
  2022. int GMT_Destroy_Session (void *API);
  2023. which simply takes the pointer to the GMT API control structure as its
  2024. only arguments. It terminates the GMT machinery and deallocates all
  2025. memory used by the GMT API book-keeping. It also unregisters any
  2026. remaining resources previously registered with the session. The
  2027. GMT API will only close files that it was responsible for opening in
  2028. the first place. Finally, the API structure itself is freed so your main
  2029. program does not need to do so. The function returns 1 if there
  2030. is an error when trying to free the memory (the error code is passed
  2031. back with ``API->error``), otherwise it returns 0 (``GMT_NOERROR``).
  2032. .. _sec-parsopt:
  2033. Presenting and accessing GMT options
  2034. ------------------------------------
  2035. As you develop a program you may wish to rely on some of
  2036. the GMT common options. For instance, you may wish to have your
  2037. program present the ``-R`` option to the user, let GMT handle the
  2038. parsing, and examine the values. You may also wish to encode your own
  2039. custom options that may require you to parse user text into the
  2040. corresponding floating point dimensions, constants, coordinates, absolute time, etc.
  2041. The API provides several functions to simplify these tedious parsing
  2042. tasks. This section is intended to show how the programmer will obtain
  2043. information from the user that is necessary to do the task at hand
  2044. (e.g., special options to provide values and settings for the program).
  2045. In the following section we will concern ourselves with preparing
  2046. arguments for calling any of the GMT modules.
  2047. Display usage syntax for GMT common options
  2048. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2049. You can have your program menu display the standard usage message for a
  2050. GMT common option by calling the function
  2051. .. _GMT_Option:
  2052. ::
  2053. int GMT_Option (void *API, const char *options);
  2054. where ``options`` is a comma-separated list of GMT common options
  2055. (e.g., "R,J,O,X"). You can repeat this function with different sets of
  2056. options in order to intersperse your own custom options within an
  2057. overall alphabetical order; see any GMT module for examples of typical
  2058. layouts.
  2059. Parsing the GMT common options
  2060. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2061. The parsing of all GMT common option is done by on call to
  2062. .. _GMT_Parse_Common:
  2063. ::
  2064. int GMT_Parse_Common (void *API, const char *args, struct GMT_OPTION *list);
  2065. where ``args`` is a string of the common GMT options your program is allowed to use.
  2066. An error will be reported if any of the common GMT options fail
  2067. to parse, and if so we return 1; if no errors we return 0. All
  2068. other options, including file names, will be silently ignored. The
  2069. parsing will update the internal GMT information structure that
  2070. affects module operations.
  2071. Inquiring about the GMT common options
  2072. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2073. The API provide only a limited window into the full GMT machinery
  2074. accessible to the modules. You can determine if a particular common
  2075. option has been parsed and in some cases determine the values that were set with
  2076. .. _GMT_Get_Common:
  2077. ::
  2078. int GMT_Get_Common (void *API, unsigned int option, double *par);
  2079. where ``option`` is a single option character (e.g., 'R') and ``par`` is
  2080. a double array with at least a length of 6. If the particular option has
  2081. been parsed then the function returns the number of parameters passed
  2082. back via ``par``; otherwise we return -1. For instance, to determine if
  2083. the ``-R`` was set and to obtain the specified region you may call
  2084. ::
  2085. if (GMT_Get_Common (API, 'R', wesn)) != -1) {
  2086. /* wesn now contains the boundary information */
  2087. }
  2088. The ``wesn`` array could now be passed to the various read and create
  2089. functions for GMT resources.
  2090. Parsing text values
  2091. ~~~~~~~~~~~~~~~~~~~
  2092. Your program may need to request values from the user, such as
  2093. distances, plot dimensions, coordinates, date/time strings and other data. The conversion
  2094. from such text to actual distances, taking units into account, is
  2095. tedious to program. You can simplify this by using
  2096. .. _GMT_Get_Values:
  2097. ::
  2098. int GMT_Get_Values (void *API, const char *arg, double par[], int maxpar);
  2099. where ``arg`` is the text item with one or more values that are
  2100. separated by commas, spaces, tabs, semi-colons, or slashes, and ``par`` is an array of length ``maxpar`` long
  2101. enough to hold all the items you are parsing. The function returns the
  2102. number of items parsed with a maximum of ``maxpar``, or -1 if there is an error. For instance, assume
  2103. the character string ``origin`` was given by the user as two geographic
  2104. coordinates separated by a slash (e.g., ``"35:45W/19:30:55.3S"``). We
  2105. obtain the two coordinates in decimal degrees by calling
  2106. ::
  2107. n = GMT_Get_Values (API, origin, pair, 2);
  2108. Your program can now check that ``n`` equals 2 and then use the values
  2109. in ``pairs`` separately. **Note**: Dimensions given with units of inches, cm, or points
  2110. are converted to the current default unit set via :term:`PROJ_LENGTH_UNIT`,
  2111. while distances given in km, nautical miles, miles, feet, or
  2112. survey feet are returned in meters. Arc lengths in minutes and seconds
  2113. are returned in decimal degrees, and date/time values are returned in
  2114. seconds since the current epoch [1970].
  2115. Get or set an API or GMT default parameter
  2116. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2117. If your program needs to determine one or more of the current
  2118. API or GMT default settings you can do so via
  2119. .. _GMT_Get_Default:
  2120. ::
  2121. int GMT_Get_Default (void *API, const char *keyword, char *value);
  2122. where ``keyword`` is one such keyword (e.g., :term:`PROJ_LENGTH_UNIT`) and
  2123. ``value`` must be a character string long enough to hold the answer. In
  2124. addition to the long list of GMT defaults you can also inquire about the
  2125. API parameters ``API_PAD`` (the current pad setting), ``API_IMAGE_LAYOUT`` (the
  2126. order and structure of image memory storage), ``API_GRID_LAYOUT`` (order of
  2127. grid memory storage), ``API_VERSION`` (the API version string),
  2128. ``API_CORES`` (the number of cores seen by the API),
  2129. ``API_BINDIR`` (the API (GMT) executable path),
  2130. ``API_SHAREDIR`` (the API (GMT) shared directory path),
  2131. ``API_DATADIR`` (the API (GMT) data directory path), and
  2132. ``API_PLUGINDIR`` (the API (GMT) plugin path).
  2133. Depending on what parameter you selected you could further convert it to
  2134. a numerical value with GMT_Get_Values_ or just use it in a text comparison.
  2135. To change any of the API or
  2136. GMT default settings programmatically you would use
  2137. .. _GMT_Set_Default:
  2138. ::
  2139. int GMT_Set_Default (void *API, const char *keyword, const char *value);
  2140. where as before ``keyword`` is one such keyword (e.g., :term:`PROJ_LENGTH_UNIT`) and
  2141. ``value`` must be a character string with the new setting.
  2142. Note that all settings must be passed as text strings even if many are
  2143. inherently integers or floats.
  2144. Get an API enum constant
  2145. ~~~~~~~~~~~~~~~~~~~~~~~~
  2146. The GMT API enum constants that are part of the API are defined in the
  2147. include file gmt_resources.h, which is included by gmt.h. So, if you are
  2148. writing an application in C/C++ you are including gmt.h and thus have
  2149. access to all the API enums directly. However, if your application is
  2150. written in other languages and you are perhaps just interfacing with the
  2151. shared GMT API library, then you can access any GMT enum via
  2152. .. _GMT_Get_Enum:
  2153. ::
  2154. int GMT_Get_Enum (void *API, const char *enumname);
  2155. where ``enumname`` is the name of one such enum (e.g., GMT_SESSION_EXTERNAL, GMT_IS_DATASET, etc.),
  2156. including the ones listed in :ref:`types <tbl-types>` and :ref:`types <tbl-viatypes>`; see
  2157. gmt_resources.h for the full listing.
  2158. The function returns the corresponding integer value. For unrecognized names we return -99999.
  2159. **Note**: You may pass a NULL pointer as API if you need to obtain enum values prior to calling GMT_Create_Session_.
  2160. For indexed access to custom grids and images we may need to know the internal matrix layout.
  2161. You can change this information via
  2162. .. _GMT_Set_Index:
  2163. ::
  2164. int64_t GMT_Set_Index (struct GMT_GRID_HEADER *header, char *code);
  2165. where the ``header`` is the header of either a grid or image, and ``code`` is a three-character
  2166. code indication ...
  2167. .. _sec-func:
  2168. Call a module
  2169. -------------
  2170. One of the advantages of programming with the API is that you
  2171. have access to the high-level GMT modules. For example, if your
  2172. program must compute the distance from a node to all other nodes in the grid
  2173. then you can simply set up options and call :doc:`grdmath` to do it
  2174. for you and accept the result back as an input grid. All the module
  2175. interfaces are identical and are called via
  2176. .. _GMT_Call_Module:
  2177. ::
  2178. int GMT_Call_Module (void *API, const char *module, int mode, void *args);
  2179. Here, ``module`` is the name of any of the GMT modules, such as
  2180. :doc:`plot` or :doc:`grdvolume`. All GMT modules may be called with one of
  2181. three sets of ``args`` depending on ``mode``. The three modes differ in
  2182. how the options are passed to the module:
  2183. *mode* = ``GMT_MODULE_EXIST``.
  2184. Return GMT_NOERROR (0) if the module exists, nonzero otherwise.
  2185. *mode* = ``GMT_MODULE_PURPOSE``.
  2186. Just print the one-line purpose of the module; args must be NULL.
  2187. *mode* = ``GMT_MODULE_LIST``.
  2188. Just prints a list of all modules (including those given as plugins); args must be NULL.
  2189. *mode* = ``GMT_MODULE_OPT``.
  2190. Expects ``args`` to be a pointer to a doubly-linked list of objects with individual
  2191. options for the current program. We will see
  2192. how API functions can help prepare and maintain such lists.
  2193. *mode* = ``GMT_MODULE_CMD``.
  2194. Expects ``args`` to be a single text string with all needed options.
  2195. *mode > 0*.
  2196. Expects ``args`` to be an array of text strings and ``mode`` to be a count of how many
  2197. options are passed (i.e., the ``argc, argv[]`` model used by the GMT programs themselves).
  2198. Set program options via text array arguments
  2199. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2200. When ``mode > 0`` we expect an array ``args`` of character
  2201. strings that each holds a single command line option (e.g.,
  2202. "-R120:30/134:45/8S/3N") and interpret ``mode`` to be the count of how
  2203. many options are passed. This, of course, is almost exactly how the
  2204. stand-alone GMT programs are called (and reflects how they themselves
  2205. are activated internally). We call this the "argc-argv" mode. Depending
  2206. on how your program obtains the necessary options you may find that this
  2207. interface offers all you need.
  2208. Set program options via text command
  2209. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2210. If ``mode =`` 0 then ``args`` will be examined to see if it contains
  2211. several options within a single command string. If so we will break
  2212. these into separate options. This is useful if you wish to pass a single
  2213. string such as "-R120:30/134:45/8S/3N -JM6i mydata.txt -Sc0.2c". We call
  2214. this the "command" mode and it is extensively used by the modules themselves.
  2215. Set program options via linked structures
  2216. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2217. The third, linked-list interface allows developers using higher-level
  2218. programming languages to pass all command options via a pointer to a
  2219. NULL-terminated, doubly-linked list of option structures, each
  2220. containing information about a single option. Here, instead of text
  2221. arguments we pass the pointer to the linked list of options mentioned
  2222. above, and ``mode`` must be passed as ``GMT_MODULE_OPT``. Using
  2223. this interface can be more involved since you need to generate the
  2224. linked list of program options; however, utility functions exist to
  2225. simplify its use. This interface is intended for programs whose internal
  2226. workings are better suited to generate such arguments -- we call this the
  2227. "options" mode. The order in the list is not important as GMT will
  2228. sort it internally according to need. The option structure is defined below.
  2229. .. _options:
  2230. ::
  2231. struct GMT_OPTION {
  2232. char option; /* Single option character (e.g., 'G' for -G) */
  2233. char *arg; /* String with arguments (NULL if not used) */
  2234. struct GMT_OPTION *next; /* Next option pointer (NULL for last option) */
  2235. struct GMT_OPTION *prev; /* Previous option (NULL for first option) */
  2236. };
  2237. Convert between text and linked structures
  2238. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2239. To assist programmers there are also two convenience functions that
  2240. allow you to convert between the two argument formats. They are
  2241. .. _GMT_Create_Options:
  2242. ::
  2243. struct GMT_OPTION *GMT_Create_Options (void *API, int argc, void *args);
  2244. This function accepts your array of text arguments (cast via a void
  2245. pointer), allocates the necessary space, performs the conversion, and
  2246. returns a pointer to the head of the linked list of program options.
  2247. However, in case of an error we return a NULL pointer and set
  2248. ``API->error`` to indicate the nature of the problem. Otherwise, the
  2249. pointer may now be passed to the relevant GMT module. Note that if
  2250. your list of text arguments were obtained from a C ``main()`` function
  2251. then ``argv[0]`` will contain the name of the calling program. To avoid
  2252. passing this as a bad file name option, call GMT_Create_Options_ with
  2253. ``argc-1`` and ``argv+1`` instead. If you wish to pass a single text string with
  2254. multiple options (in lieu of an array of text strings), then pass
  2255. ``argc`` = 0. When no longer needed you can remove the entire list by calling
  2256. .. _GMT_Destroy_Options:
  2257. ::
  2258. int GMT_Destroy_Options (void *API, struct GMT_OPTION **list);
  2259. The function returns 1 if there is an error (which is passed back
  2260. with ``API->error``), otherwise it returns 0 (``GMT_NOERROR``).
  2261. The inverse function prototype is
  2262. .. _GMT_Create_Args:
  2263. ::
  2264. char **GMT_Create_Args (void *API, int *argc, struct GMT_OPTION *list);
  2265. which allocates space for the text strings and performs the conversion;
  2266. it passes back the count of the arguments via ``argc`` and returns a
  2267. pointer to the text array. In the case of an error we return a NULL
  2268. pointer and set ``API->error`` to reflect the error type. Note that
  2269. ``argv[0]`` will not contain the name of the program as is the case the
  2270. arguments presented by a C ``main()`` function. When you no longer have
  2271. any use for the text array, call
  2272. .. _GMT_Destroy_Args:
  2273. ::
  2274. int GMT_Destroy_Args (void *API, int argc, char **argv[]);
  2275. to deallocate the space used. This function returns 1 if there is
  2276. an error (which is passed back with ``API->error``), otherwise it returns 0 (``GMT_NOERROR``).
  2277. Finally, to convert the linked list of option structures to a single
  2278. text string command, use
  2279. .. _GMT_Create_Cmd:
  2280. ::
  2281. char *GMT_Create_Cmd (void *API, struct GMT_OPTION *list);
  2282. Developers who plan to import and export GMT shell scripts might find
  2283. it convenient to use these functions. In case of an error we return a
  2284. NULL pointer and set ``API->error``, otherwise a pointer to an allocated
  2285. string is returned. When you no longer have
  2286. any use for the text string, call
  2287. .. _GMT_Destroy_Cmd:
  2288. ::
  2289. int GMT_Destroy_Cmd (void *API, char **string);
  2290. to deallocate the space used. This function returns 1 if there is
  2291. an error (which is passed back with ``API->error``), otherwise it
  2292. returns 0 (``GMT_NOERROR``).
  2293. Manage the linked list of options
  2294. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2295. Several additional utility functions are available for programmers who
  2296. wish to manipulate program option structures within their own programs.
  2297. These allow you to create new option structures, append them to the
  2298. linked list, replace existing options with new values, find a particular
  2299. option, and remove options from the list. **Note**: The order in which the
  2300. options appear in the linked list is of no consequence to GMT.
  2301. Internally, GMT will sort and process the options in the manner
  2302. required. Externally, you are free to maintain your own order.
  2303. Make a new option structure
  2304. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2305. GMT_Make_Option_ will allocate a new option structure, assign
  2306. values given the ``option`` and ``arg`` parameters (pass NULL if there is
  2307. no argument for this option), and return a pointer to the allocated
  2308. structure. The prototype is
  2309. .. _GMT_Make_Option:
  2310. ::
  2311. struct GMT_OPTION *GMT_Make_Option (void *API, char option, const char *arg);
  2312. Should memory allocation fail the function will print an error message
  2313. pass an error code via ``API->error``, and return NULL.
  2314. Append an option to the linked list
  2315. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2316. GMT_Append_Option_ will append the specified ``option`` to the end of
  2317. the doubly-linked ``list``. The prototype is
  2318. .. _GMT_Append_Option:
  2319. ::
  2320. struct GMT_OPTION *GMT_Append_Option (void *API, struct GMT_OPTION *option,
  2321. struct GMT_OPTION *list);
  2322. We return the list back, and if ``list`` is given as NULL we return
  2323. ``option`` as the start of the new list. Any errors result in a NULL
  2324. pointer with ``API->error`` holding the error type.
  2325. Find an option in the linked list
  2326. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2327. GMT_Find_Option_ will return a pointer ``ptr`` to the first option in
  2328. the linked list starting at ``list`` whose option character equals
  2329. ``option``. If not found we return NULL. While this is not necessarily
  2330. an error we still set ``API->error`` accordingly. The prototype is
  2331. .. _GMT_Find_Option:
  2332. ::
  2333. struct GMT_OPTION *GMT_Find_Option (void *API, char option,
  2334. struct GMT_OPTION *list);
  2335. If you need to look for multiple occurrences of a certain option you
  2336. will need to call GMT_Find_Option_ again, passing the option
  2337. following the previously found option as the ``list`` entry, i.e.,
  2338. ::
  2339. list = *ptr->next;
  2340. Update an existing option in the list
  2341. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2342. GMT_Update_Option_ will replace the argument of ``current`` with the
  2343. new argument ``arg`` and otherwise leave the option at its place in the
  2344. list. The prototype is
  2345. .. _GMT_Update_Option:
  2346. ::
  2347. int GMT_Update_Option (void *API, struct GMT_OPTION *current, const char *arg);
  2348. An error will be reported if (a) ``current`` is NULL or (b) ``arg`` is
  2349. NULL. The function returns 1 if there is an error, otherwise it returns 0 (``GMT_NOERROR``).
  2350. Delete an existing option in the linked list
  2351. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2352. You may use GMT_Delete_Option_ to remove the ``current`` option from the linked
  2353. ``list``. The prototype is
  2354. .. _GMT_Delete_Option:
  2355. ::
  2356. int GMT_Delete_Option (void *API, struct GMT_OPTION *current, struct GMT_OPTION **head);
  2357. We return 1 if the option is not found in the list and set
  2358. ``API->error`` accordingly. **Note**: Only the first occurrence of the
  2359. specified option will be deleted. If you need to delete all such options
  2360. you will need to call this function in a loop until it returns a
  2361. non-zero status.
  2362. Specify a file via a linked option
  2363. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2364. To specify an input file name via an option, simply use < as the
  2365. option (this is what GMT_Create_Options_ does when it finds filenames
  2366. on the command line). Likewise, > can be used to explicitly
  2367. indicate an output file. In order to append to an existing file, use
  2368. ). For example the following command would read from file.A and
  2369. append to file.B:
  2370. ::
  2371. gmt convert -<file.A -)file.B
  2372. These options also work on the command line but usually one would have
  2373. to escape the special characters <, > and ) as they are normally
  2374. used for file redirection.
  2375. Encode option arguments for external interfaces
  2376. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2377. Developers writing interfaces between GMT and external platforms such
  2378. as other languages (Python, Java, Julia, etc.) or tools (MATLAB, Octave,
  2379. etc.) need to manipulate linked options in a special way. For instance,
  2380. a GMT call in the MATLAB or Octave application might look like
  2381. .. code-block:: none
  2382. table = gmt('blockmean -R30W/30E/10S/10N -I2m', [x y z]);
  2383. grid = gmt('surface -R -I2m -Lu', table, high_limit_grid);
  2384. grid2 = gmt('grdmath ? LOG10 ? MUL', grid, grid);
  2385. Most of the time our implicit rules will take care of the ordering. The
  2386. rule says that all required input data items must be listed before any
  2387. secondary input data items, and all primary output items must be listed
  2388. on the left hand side before any secondary output items.
  2389. There are three situations where the parsing will need further help;
  2390. (1) Specifying the positions of memory arguments given to :doc:`gmtmath`,
  2391. (2) specifying the positions of memory arguments given to :doc:`grdmath`,
  2392. and (3) using -R? when passing a memory grid to the -R option (since just -R
  2393. means use the previous region in the command history).
  2394. Thus, in the :doc:`gmtmath` call we we needed to specify where
  2395. the specific arguments should be placed among the operators.
  2396. API developers will rely on GMT_Open_VirtualFile_ to convert the
  2397. above syntax to correct options for GMT_Call_Module_.
  2398. The prototype is
  2399. .. _GMT_Encode_Options:
  2400. ::
  2401. struct GMT_RESOURCE *GMT_Encode_Options (void *API, const char *module, int n_in,
  2402. struct GMT_OPTION **head, int *n_items);
  2403. where ``module`` is the name of the module whose linked options are
  2404. pointed to by ``*head``, ``n_in`` contains the number of *input*
  2405. objects we have to connect (or -1 if not known) and we return an array
  2406. that contains specific information for those options that
  2407. (after processing) contain explicit memory references. The number of
  2408. items in the array is returned via the ``n_items`` variable. The function
  2409. returns NULL if there are errors and sets ``API->error`` to the corresponding
  2410. error number. The GMT_RESOURCE structure is defined below:
  2411. .. .. _struct-grid:
  2412. .. code-block:: c
  2413. struct GMT_RESOURCE { /* Information for passing external resources */
  2414. enum GMT_enum_family family; /* GMT data family */
  2415. enum GMT_enum_geometry geometry; /* One of the recognized GMT geometries */
  2416. enum GMT_enum_std direction; /* Either GMT_IN or GMT_OUT */
  2417. struct GMT_OPTION *option; /* Pointer to the corresponding module option */
  2418. int object_ID; /* Object ID returned by GMT_Register_IO */
  2419. int pos; /* Index into external object in|out arrays */
  2420. int mode; /* 0 means primary i/o object, 1 means secondary */
  2421. void *object; /* Pointer to the registered GMT object */
  2422. };
  2423. API developers will need to provide specific code to handle the registration of native
  2424. structures in their language or application and to translate between the GMT resources
  2425. and the corresponding native items. Developers should look at an existing and working
  2426. interface such as the GMT/MATLAB toolbox to see the required steps.
  2427. Expand an option with explicit memory references
  2428. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2429. When the external tool or application knows the name of the special file names
  2430. used for memory references the developer should replace the place-holder ``?`` character
  2431. in any option string with the actual reference name. This is accomplished by
  2432. calling GMT_Expand_Option_, with prototype
  2433. .. _GMT_Expand_Option:
  2434. ::
  2435. int GMT_Expand_Option (void *API, struct GMT_OPTION *option, const char *name);
  2436. where ``option`` is the current option and ``name``
  2437. is the special file name for the memory reference.
  2438. The GMT FFT Interface
  2439. =====================
  2440. While the i/o options presented so far lets you easily read in a data
  2441. table or grid and manipulate them, if you need to do the manipulation in the
  2442. wavenumber domain then this chapter is for you. Here, we outline how to
  2443. take the Fourier transform of such data, perform calculations in the
  2444. wavenumber domain, and take the inverse transform before writing the
  2445. results. To assist programmers we also distribute fully functioning
  2446. demonstration programs that takes you through the steps we are about to
  2447. discuss; these demo programs may be used as your starting point for
  2448. further development and can be found in the gmt-custom repository.
  2449. Presenting and parsing the FFT options
  2450. --------------------------------------
  2451. Several GMT programs that use the FFTs present the same unified option and
  2452. modifier sets to the user. The API makes these available as well. If
  2453. your program needs to present the FFT option usage you can call
  2454. .. _GMT_FFT_Option:
  2455. ::
  2456. unsigned int GMT_FFT_Option (void *API, char option, unsigned int dim,
  2457. const char *string);
  2458. Here, ``option`` is the unique character used for this particular
  2459. program option (most GMT programs have standardized on using 'N' but
  2460. you are free to choose whatever letter you want except existing GMT common
  2461. options). The ``dim`` sets the dimension of the transform; currently you
  2462. must choose 1 or 2, while ``string`` is a one-line message that
  2463. states what the option does; you should tailor this to your program. If
  2464. NULL then a generic message is placed instead.
  2465. To parse the user's selection you call
  2466. .. _GMT_FFT_Parse:
  2467. ::
  2468. void *GMT_FFT_Parse (void *API, char option, unsigned int dim, const char *arg);
  2469. which accepts the user's string option via ``arg``; the other arguments
  2470. are the same as those above. The function returns an opaque pointer to a
  2471. structure with the chosen parameters.
  2472. Initializing the FFT machinery
  2473. ------------------------------
  2474. Before your can take any transforms you must initialize the FFT
  2475. machinery. This process involves a series of preparatory steps that are
  2476. conveniently performed for you by
  2477. .. _GMT_FFT_Create:
  2478. ::
  2479. void *GMT_FFT_Create (void *API, void *X, unsigned int dim,
  2480. unsigned int mode, void *F);
  2481. Here, ``X`` is either your dataset or grid pointer, ``dim`` is the
  2482. dimension of the transform (1 or 2 only), ``mode`` passes various flags to the setup, such as whether
  2483. the data is real, imaginary, or complex, and ``F`` is the opaque pointer
  2484. previously returned by GMT_FFT_Parse_. Depending on the option string you passed to
  2485. GMT_FFT_Parse_, the data may have a constant level or a trend
  2486. removed, mirror reflected and extended by various symmetries, padded and
  2487. tapered to desired transform dimensions, and possibly
  2488. temporary files are written out before the transform takes place. See the :doc:`grdfft`
  2489. man page for a full explanation of the options presented by GMT_FFT_Option_.
  2490. Taking the FFT
  2491. --------------
  2492. Now that everything has been set up you can perform the transform with
  2493. .. _GMT_FFT:
  2494. ::
  2495. void *GMT_FFT (void *API, void *X, int direction, unsigned int mode, void *K);
  2496. which takes as ``direction`` either ``GMT_FFT_FWD`` or ``GMT_FFT_INV``. The
  2497. ``mode`` is used to specify if we pass a real (``GMT_FFT_REAL``) or complex
  2498. (``GMT_FFT_COMPLEX``) data set, and ``K`` is the opaque pointer returned
  2499. by GMT_FFT_Create_. The transform is performed in place and returned
  2500. via ``X``. When done with your manipulations (below) you can call it
  2501. again with the inverse direction to recover the corresponding space-domain
  2502. version of your data. The FFT is fully normalized so that calling
  2503. forward followed by inverse yields the original data set. The information
  2504. passed via ``K`` determines if a 1-D or 2-D transform takes place; the
  2505. key work is done via ``GMT_FFT_1D`` or ``GMT_FFT_2D``, as explained below.
  2506. Taking the 1-D FFT
  2507. ------------------
  2508. A lower-level 1-D FFT is also available via the API, i.e.,
  2509. .. _GMT_FFT_1D:
  2510. ::
  2511. int GMT_FFT_1D (void *API, float *data, uint64_t n, int direction,
  2512. unsigned int mode);
  2513. which takes as ``direction`` either ``GMT_FFT_FWD`` or ``GMT_FFT_INV``. The
  2514. ``mode`` is used to specify if we pass a real (``GMT_FFT_REAL``) or complex
  2515. (``GMT_FFT_COMPLEX``) data set, and ``data`` is the 1-D data array of length
  2516. ``n`` that we wish
  2517. to transform. The transform is performed in place and returned
  2518. via ``data``. When done with your manipulations (below) you can call it
  2519. again with the inverse direction to recover the corresponding space-domain
  2520. version of your data. The 1-D FFT is fully normalized so that calling
  2521. forward followed by inverse yields the original data set.
  2522. Taking the 2-D FFT
  2523. ------------------
  2524. A lower-level 2-D FFT is also available via
  2525. .. _GMT_FFT_2D:
  2526. ::
  2527. int GMT_FFT_2D (void *API, float *data, unsigned int n_columns,
  2528. unsigned int n_rows, int direction, unsigned int mode);
  2529. which takes as ``direction`` either ``GMT_FFT_FWD`` or ``GMT_FFT_INV``. The
  2530. ``mode`` is used to specify if we pass a real (``GMT_FFT_REAL``) or complex
  2531. (``GMT_FFT_COMPLEX``) data set, and ``data`` is the 2-D data array in
  2532. row-major format, with row length ``n_columns`` and column length ``n_rows``.
  2533. The transform is performed in place and returned
  2534. via ``data``. When done with your manipulations (below) you can call it
  2535. again with the inverse direction to recover the corresponding space-domain
  2536. version of your data. The 2-D FFT is fully normalized so that calling
  2537. forward followed by inverse yields the original data set.
  2538. Wavenumber calculations
  2539. -----------------------
  2540. As your data have been transformed to the wavenumber domain you may wish
  2541. to operate on the various values as a function of wavenumber. We will
  2542. show how this is done for datasets and grids separately. First, we
  2543. present the function that returns an individual wavenumber:
  2544. .. _GMT_FFT_Wavenumber:
  2545. ::
  2546. double GMT_FFT_Wavenumber (void *API, uint64_t k, unsigned int mode, void *K);
  2547. where ``k`` is the index into the array or grid, ``mode`` specifies
  2548. which wavenumber we want (it is not used for 1-D transform but for the
  2549. 2-D transform we can select either the x-wavenumber (0), the
  2550. y-wavenumber (1), or the radial wavenumber (2)), and finally the opaque
  2551. vector created by GMT_FFT_Create_.
  2552. 1-D FFT manipulation
  2553. ~~~~~~~~~~~~~~~~~~~~
  2554. [To be added after gmtfft has been added as new module, probably in 5.4.]
  2555. 2-D FFT manipulation
  2556. ~~~~~~~~~~~~~~~~~~~~
  2557. The number of complex pairs in the grid is given by the header's ``nm``
  2558. variable, while ``size`` will be twice that value as it holds the number
  2559. of components. To visit all the complex values and obtain the
  2560. corresponding wavenumber we simply need to loop over ``size`` and call
  2561. GMT_FFT_Wavenumber_. This code snippet multiples the complex grid by
  2562. the radial wavenumber:
  2563. ::
  2564. uint64_t k;
  2565. for (k = 0; k < Grid->header->size; k++) {
  2566. wave = GMT_FFT_Wavenumber (API, k, 2, K);
  2567. Grid->data[k] *= wave;
  2568. }
  2569. Alternatively, you may choose to be more specific about which components
  2570. are real and imaginary (especially if they are to be treated
  2571. differently), and set up the loop this way:
  2572. ::
  2573. uint64_t re, im;
  2574. for (re = 0, im = 1; re < Grid->header->size; re += 2, im += 2) {
  2575. wave = GMT_FFT_Wavenumber (API, re, 2, K);
  2576. Grid->data[re] *= wave;
  2577. Grid->data[im] *= 2.0 * wave;
  2578. }
  2579. Destroying the FFT machinery
  2580. ----------------------------
  2581. When done you terminate the FFT machinery with
  2582. .. _GMT_FFT_Destroy:
  2583. ::
  2584. double GMT_FFT_Destroy (void *API, void *K);
  2585. which simply frees up the memory allocated by the FFT machinery with GMT_FFT_Create_.
  2586. FORTRAN Support
  2587. ===============
  2588. FORTRAN 90 developers who wish to use the GMT API may use the same
  2589. API functions as discussed in Chapter 2. As we do not have much (i.e., any) experience
  2590. with modern Fortran we are not sure to what extent you are able to access
  2591. the members of the various structures, such as the :ref:`GMT_GRID <struct-grid>` structure. Thus,
  2592. this part will depend on feedback and for the time being is to be considered
  2593. preliminary and subject to change. We encourage you to take contact should you
  2594. wish to use the API with your Fortran 90 programs.
  2595. FORTRAN 77 Grid i/o
  2596. -------------------
  2597. Because of a lack of structure pointers we can only provide a low level of
  2598. support for Fortran 77. This API is limited to help you inquire, read and write
  2599. GMT grids directly from Fortran 77.
  2600. To inquire about the range of information in a grid, use
  2601. .. _gmt_f77_readgrdinfo:
  2602. ::
  2603. int gmt_f77_readgrdinfo (unsigned int dim[], double limits[], double inc[],
  2604. char *title, char *remark, const char *file)
  2605. where ``dim`` returns the grid width, height, and registration, ``limits`` returns the min and max values for x, y, and z
  2606. as three consecutive pairs, ``inc`` returns the x and y increments, while the ``title`` and ``remark``
  2607. return the values of these strings. The ``file``
  2608. argument is the name of the file we wish to inquire about. The function returns 0 unless there is an error.
  2609. Note that you must declare your variables so that ``limits`` has at least 6 elements, ``inc`` has at least 2, and ``dim`` has at least 4.
  2610. To actually read the grid, we use
  2611. .. _gmt_f77_readgrd:
  2612. ::
  2613. int gmt_f77_readgrd (float *array, unsigned int dim[], double wesn[],
  2614. double inc[], char *title, char *remark, const char *file)
  2615. where ``array`` is the 1-D grid data array, ``dim`` returns the grid width, height, and registration,
  2616. ``limits`` returns the min and max values for x, y, and z, ``inc`` returns the x and y increments, and
  2617. the ``title`` and ``remark`` return the values of the corresponding strings. The ``file``
  2618. argument is the name of the file we wish to read from. The function returns 0 unless there is an error.
  2619. Note on input, ``dim[2]`` can be set to 1, which means we will allocate the array for you; otherwise
  2620. we assume space has already been secured. Also, if ``dim[3]`` is set to 1 we will in-place transpose
  2621. the array from C-style row-major array order to Fortran column-major array order.
  2622. Finally, to write a grid to file you can use
  2623. .. _gmt_f77_writegrd:
  2624. ::
  2625. int gmt_f77_writegrd_(float *array, unsigned int dim[], double wesn[], double inc[],
  2626. const char *title, const char *remark, const char *file)
  2627. where ``array`` is the 1-D grid data array, ``dim`` specifies the grid width, height, and registration,
  2628. ``limits`` may be used to specify a subset (normally, just pass zeros), ``inc`` specifies the x and y increments,
  2629. while the ``title`` and ``remark`` supply the values of these strings. The ``file``
  2630. argument is the name of the file we wish to write to. The function returns 0 unless there is an error.
  2631. If ``dim[3]`` is set to 1 we will in-place transpose
  2632. the array from Fortran column-major array order to C-style row-major array order before writing. Note
  2633. this means ``array`` will have been transposed when the function returns.
  2634. External Interfaces
  2635. ===================
  2636. Developers may want to access GMT modules from external programming environments, such as MATLAB,
  2637. Octave, Julia, Python, R, IDL, etc., etc. These all face similar challenges and hence this section
  2638. will speak in somewhat abstract terms. Specific language addressing the challenges for some of
  2639. the above-mentioned environments will follow below.
  2640. The C/C++ API for GMT makes it possible to call any of the ~100 core modules, the 40 or so supplemental
  2641. modules, and any number of custom modules provided via shared libraries (e.g., the gsfml modules). Many
  2642. of the external interfaces come equipped with methods to call C functions directly.
  2643. The key challenges pertain to specifying the input to use in the module and to receive
  2644. what is produced by the module.
  2645. As we know from GMT command line usage, all GMT modules expect input to be given via input files (or stdin, except for sources like grids and images). Similarly, output will be written to a specified
  2646. output file (or stdout if the data type supports it). Clearly, external interfaces
  2647. could do the same thing. The problem is that most of the time we already will have the input data in
  2648. memory and would prefer the output to be returned back to memory, thus avoiding using temporary files.
  2649. Here, we will outline the general approach for using the GMT API. We will describe a relatively low-level approach
  2650. to calling GMT modules. Once such an interface exists it is simpler to build a more flexible and user-friendly
  2651. layer on top that can handle argument parsing in a form that makes the interface seem more of a natural
  2652. extension of your external environment than a forced fit to GMT's command-line heritage.
  2653. Before we describe the interface it is important to understand that the GMT modules, since the beginning
  2654. or time, have done the i/o inside the modules. While these steps are helped by i/o library functions, the
  2655. i/o activities all take place *inside* the modules. This means that external environments in which the desired
  2656. input data already reside in memory and the desired results should be returned back to memory pose a
  2657. trickier challenge. We will see the solution to this involves the concept of *virtual* files.
  2658. .. figure:: /_images/GMT_API_use.*
  2659. :width: 500 px
  2660. :align: center
  2661. GMT Modules can read and write information in may ways. The GMT command line modules
  2662. can only access the methods in white, while all methods are available via the C API.
  2663. External interfaces will preferentially want the methods in orange.
  2664. Plain interface
  2665. ---------------
  2666. While the syntax of your external environment's language will dictate the details of the implementation, we will in general
  2667. need to build a function (or class, or method) that allows you to issue a call like this:
  2668. [*results*] = **gmt** (*module*, *options*, *inputs*)
  2669. where *results* (i.e., objects returned back to memory) is optional and may be one or more items grouped
  2670. together, depending on language syntax. If no output is required then no left-hand side
  2671. assignment will be present. Likewise, *inputs* is optional and may be one or more comma-separated
  2672. objects present in memory. In most cases, *options* will be required and this is a string with
  2673. options very similar to the arguments given on the GMT command line. Finally, *module* is required since you
  2674. must specify which one you want to call. The coding of the **gmt** method, class, or function above may be written entirely in
  2675. C, partly in C and the external scripting language, or entirely in the scripting language, depending on
  2676. restrictions on what needs to be done and where this is most easily accomplished.
  2677. How this is accomplished may vary from environment to environment.
  2678. .. figure:: /_images/GMT_API_flow.*
  2679. :width: 500 px
  2680. :align: center
  2681. Data pass in and out of the **gmt** interface which may be written in the scripting language used
  2682. by the external interface. The native data will need to be encapsulated by GMT containers and this
  2683. step may be done by a C **parser** but could also be done by the **gmt** interface directly. Either
  2684. of these communicate directly with the C functions in the GMT API.
  2685. Data containers
  2686. ---------------
  2687. The external interface developer will need to create native data classes or structures that are capable of
  2688. containing the information associated with the 5 GMT objects: data tables, grids, images, color palette tables,
  2689. and PostScript documents. In other words, how your external environment will represent these
  2690. data in memory. Some of these "containers" may already exist, while others may need to be designed. Most likely, you will end up with
  2691. a set of five containers that can hold the various GMT data objects and related metadata. In addition, it may
  2692. be convenient to also consider the two GMT helper objects MATRIX and VECTOR, which may be closer to the native
  2693. representation of your data than, for instance, the native GMT_DATASET.
  2694. Input from memory
  2695. -----------------
  2696. Whether input comes from memory or from external files, the call to a GMT module is the same: we have to specify
  2697. *filenames* to provide the input data. Thus, the game is to provide *virtual* file names that represent our in-memory
  2698. data. The process is relatively simple and may need to be done in a snippet of C
  2699. code that can be called by a function written in your environments scripting language. The steps go like this:
  2700. #. Create a GMT C container marked for input and copy or reference your data provided by
  2701. your external environment into this container.
  2702. #. Open a virtual file using this container to represent the input source.
  2703. #. Insert this virtual file name in the appropriate location in the GMT option string. If the
  2704. module imports data from *stdin* then we can use the hidden option -<filename.
  2705. When the GMT module is run it will know how to make the connections between the virtual file names and
  2706. the actual data via information stored inside the C API. When the module completes you should close any
  2707. open virtual files that were used by the module.
  2708. Output to memory
  2709. ----------------
  2710. As the case for selecting input, GMT modules only know about writing results to a file (or stdout). Hence, we must follow the same paradigm as we did for input
  2711. and identify virtual files to represent the output destinations. The steps are:
  2712. #. Create an empty GMT C container of the right type marked for output.
  2713. #. Create a virtual file name to represent this output destination.
  2714. #. Place this file name in the appropriate location in the GMT option string. If the
  2715. module exports data to *stdout* then we can use the hidden option ->filename.
  2716. When the GMT module is run it will know how to make the connections between the memory allocated by the
  2717. module and the virtual file names stored inside the C API. Once the module call has completed you can access the
  2718. results in the external environment by using GMT_Read_VirtualFile_ with the virtual filename you created earlier. This will return a GMT C container with the results, and
  2719. you can now populate you external data containers with data produced by the GMT module.
  2720. The magic of knowing
  2721. --------------------
  2722. External developers have access to the two extra API functions GMT_Encode_Options_ and GMT_Expand_Option_.
  2723. Your **gmt** will need to call GMT_Encode_Options_ to obtain information about what the selected
  2724. module expects, what its options are, which were selected, and what data types are expected. It may
  2725. possibly modify the options, such as adding the filename "?" to options that set
  2726. *required* input and output files and returns an array of structures with specific information about
  2727. all inputs and outputs. If sources and destinations were missing from your *options* string it is taken
  2728. to mean that you want to associate these sources and destinations
  2729. with memory locations rather than actual files. The second function GMT_Expand_Option_ can then then
  2730. used to replace these place-holder names with the virtual filenames you created earlier.
  2731. The MATLAB interface
  2732. ~~~~~~~~~~~~~~~~~~~~
  2733. We have built a MATLAB/Octave interface to GMT called the toolbox. It was our first attempt to use the C API from an
  2734. external environment and its development influenced
  2735. how we designed the final GMT C API. MATLAB represents most data as matrices but there are also structures that
  2736. can hold many different items, including several matrices and text strings. Thus, we designed several native mex structures
  2737. that represent the five GMT objects. The main **gmt** function available in MATLAB derives from a small MATLAB script
  2738. (gmt.m) which handles basic argument testing and then passes the arguments to our C function gmtmex.c.
  2739. Most of the high-level parsing of options and arguments is done in this function, but we also rely on
  2740. a C library (gmtmex_parser.c) that hides the details of the implementation. It is this library that
  2741. does most of the work in translating between the GMT and MATLAB object layouts. Knowing what types are
  2742. represented by the different sources and destinations is provided by the array of structures returned
  2743. by GMT_Encode_Options_.
  2744. The Julia interface
  2745. ~~~~~~~~~~~~~~~~~~~
  2746. Unlike the MATLAB interface, the Julia interface is written entirely in the Julia language.
  2747. The Python interface
  2748. ~~~~~~~~~~~~~~~~~~~~
  2749. To be defined shortly.
  2750. Appendix A: GMT resources
  2751. -------------------------
  2752. We earlier introduced the five standard GMT resources (dataset, grid, image, color palette table, PostScript)
  2753. as well as the user vector and matrix. Here are the complete definitions of these structures, including
  2754. all variables accessible via the structures.
  2755. Data set
  2756. ~~~~~~~~
  2757. Each data set is represented by a :ref:`GMT_DATASET <struct-dataset>` that consists of one or more data
  2758. tables represented by a :ref:`GMT_DATATABLE <struct-datatable>`, and each table consists of one or more
  2759. segments represented by a :ref:`GMT_DATASEGMENT <struct-datasegment>`, and each segment contains one or
  2760. more rows of a fixed number of columns.
  2761. .. _struct-dataset:
  2762. .. code-block:: c
  2763. struct GMT_DATASET { /* Single container for an array of GMT tables (files) */
  2764. /* Variables we document for the API: */
  2765. uint64_t n_tables; /* Total number of tables (files) contained */
  2766. uint64_t n_columns; /* Number of data columns */
  2767. uint64_t n_segments; /* Total number of segments across all tables */
  2768. uint64_t n_records; /* Total number of data records across all tables */
  2769. double *min; /* Minimum coordinate for each column */
  2770. double *max; /* Maximum coordinate for each column */
  2771. struct GMT_DATATABLE **table; /* Pointer to array of tables */
  2772. unsigned int type; /* The data record type of this dataset */
  2773. unsigned int geometry; /* The geometry of this dataset */
  2774. const char *ProjRefPROJ4; /* To store a referencing system string in PROJ.4 format */
  2775. const char *ProjRefWKT; /* To store a referencing system string in WKT format */
  2776. int ProjRefEPSG; /* To store a referencing system EPSG code */
  2777. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2778. };
  2779. Here is the full definition of the ``GMT_DATATABLE`` structure:
  2780. .. _struct-datatable:
  2781. .. code-block:: c
  2782. struct GMT_DATATABLE { /* To hold an array of line segment structures and header information in one container */
  2783. /* Variables we document for the API: */
  2784. unsigned int n_headers; /* Number of file header records (0 if no header) */
  2785. uint64_t n_columns; /* Number of columns (fields) in each record */
  2786. uint64_t n_segments; /* Number of segments in the array */
  2787. uint64_t n_records; /* Total number of data records across all segments */
  2788. double *min; /* Minimum coordinate for each column */
  2789. double *max; /* Maximum coordinate for each column */
  2790. char **header; /* Array with all file header records, if any) */
  2791. struct GMT_DATASEGMENT **segment; /* Pointer to array of segments */
  2792. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2793. };
  2794. Here is the full definition of the ``GMT_DATASEGMENT`` structure:
  2795. .. _struct-datasegment:
  2796. .. code-block:: c
  2797. struct GMT_DATASEGMENT { /* For holding segment lines in memory */
  2798. /* Variables we document for the API: */
  2799. uint64_t n_rows; /* Number of points in this segment */
  2800. uint64_t n_columns; /* Number of fields in each record (>= 2) */
  2801. double *min; /* Minimum coordinate for each column */
  2802. double *max; /* Maximum coordinate for each column */
  2803. double **data; /* Data x,y, and possibly other columns */
  2804. char **text; /* trailing text strings beyond the data */
  2805. char *label; /* Label string (if applicable) */
  2806. char *header; /* Segment header (if applicable) */
  2807. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2808. };
  2809. GMT grid
  2810. ~~~~~~~~
  2811. A grid is represented by a :ref:`GMT_GRID <struct-grid>` that consists of a header structure
  2812. represented by a :ref:`GMT_GRID_HEADER <struct-gridheader>` and an float array ``data`` that
  2813. contains the grid values.
  2814. .. _struct-grid:
  2815. .. code-block:: c
  2816. struct GMT_GRID { /* To hold a GMT float grid and its header in one container */
  2817. struct GMT_GRID_HEADER *header; /* Pointer to full GMT header for the grid */
  2818. float *data; /* Pointer to the float grid */
  2819. double *x, *y; /* Vector of coordinates */
  2820. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2821. };
  2822. The full definition of the ``GMT_GRID_HEADER`` structure. Most of these members are only used internally:
  2823. .. _struct-gridheader:
  2824. .. code-block:: c
  2825. struct GMT_GRID_HEADER {
  2826. /* Variables we document for the API:
  2827. They are copied verbatim to the native grid header and must be 4-byte unsigned ints. */
  2828. uint32_t n_columns; /* Number of columns */
  2829. uint32_t n_rows; /* Number of rows */
  2830. uint32_t registration; /* GMT_GRID_NODE_REG (0) or GMT_GRID_PIXEL_REG (1) */
  2831. /* == The types of the following 12 elements must not be changed.
  2832. == They are also copied verbatim to the native grid header. */
  2833. double wesn[4]; /* Min/max x and y coordinates */
  2834. double z_min; /* Minimum z value */
  2835. double z_max; /* Maximum z value */
  2836. double inc[2]; /* x and y increment */
  2837. double z_scale_factor; /* grd values must be multiplied by this */
  2838. double z_add_offset; /* After scaling, add this */
  2839. char x_units[GMT_GRID_UNIT_LEN80]; /* units in x-direction */
  2840. char y_units[GMT_GRID_UNIT_LEN80]; /* units in y-direction */
  2841. char z_units[GMT_GRID_UNIT_LEN80]; /* grid value units */
  2842. char title[GMT_GRID_TITLE_LEN80]; /* name of data set */
  2843. char command[GMT_GRID_COMMAND_LEN320];/* name of generating command */
  2844. char remark[GMT_GRID_REMARK_LEN160]; /* comments re this data set */
  2845. /* == End of "untouchable" header. */
  2846. /* This section is flexible. It is not copied to any grid header
  2847. or stored in any file. It is considered private */
  2848. unsigned int type; /* Grid format */
  2849. unsigned int bits; /* Bits per value (e.g., 32 for ints/floats; 8 for bytes) */
  2850. unsigned int complex_mode; /* 0 = normal, GMT_GRID_IS_COMPLEX_REAL = real part of complex
  2851. grid, GMT_GRID_IS_COMPLEX_IMAG = imag part of complex grid */
  2852. unsigned int mx, my; /* Actual dimensions of the grid in memory, allowing for the padding */
  2853. size_t nm; /* Number of data items in this grid (n_columns * n_rows) [padding is excluded] */
  2854. size_t size; /* Actual number of items (not bytes) required to hold this grid (= mx * my), per band */
  2855. size_t n_alloc; /* Bytes allocated for this grid */
  2856. unsigned int n_bands; /* Number of bands [1]. Used with IMAGE containers and macros to get ij index from row,col, band */
  2857. unsigned int pad[4]; /* Padding on west, east, south, north sides [2,2,2,2] */
  2858. const char *ProjRefPROJ4; /* To store a referencing system string in PROJ.4 format */
  2859. const char *ProjRefWKT; /* To store a referencing system string in WKT format */
  2860. float nan_value; /* Missing value as stored in grid file */
  2861. double xy_off; /* 0.0 (registration == GMT_GRID_NODE_REG) or 0.5 ( == GMT_GRID_PIXEL_REG) */
  2862. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2863. };
  2864. GMT image
  2865. ~~~~~~~~~
  2866. An image is similar to a grid except it may have more than one layer (i.e., band).
  2867. It is represented by a :ref:`GMT_IMAGE <struct-image>` structure that consists of the
  2868. :ref:`GMT_GRID_HEADER <struct-gridheader>` structure and an char array ``data`` that
  2869. contains the image values. The type of the array is determined by the value of ``type``.
  2870. **Note**: The header *size* value reflects number of nodes per band, so the actual memory
  2871. allocated will be *size * n_bands*.
  2872. .. _struct-image:
  2873. .. code-block:: c
  2874. struct GMT_IMAGE {
  2875. enum GMT_enum_type type; /* Data type, e.g. GMT_FLOAT */
  2876. int *colormap; /* Array with color lookup values */
  2877. int n_indexed_colors; /* Number of colors in a color-mapped image */
  2878. struct GMT_GRID_HEADER *header; /* Pointer to full GMT header for the image */
  2879. unsigned char *data; /* Pointer to actual image */
  2880. unsigned char *alpha; /* Pointer to an optional transparency layer */
  2881. const char *color_interp; /* Color interpretation name */
  2882. double *x, *y; /* Vector of coordinates */
  2883. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2884. };
  2885. CPT palette table
  2886. ~~~~~~~~~~~~~~~~~
  2887. A CPT is represented by a :ref:`GMT_PALETTE <struct-palette>` structure that contains several
  2888. items, such as a :ref:`GMT_LUT <struct-lut>` structure ``data`` that
  2889. contains the color information per interval. The background, foreground and Nan-color values have
  2890. colors specified by the :ref:`GMT_BFN <struct-bnf>` array structure ``bfn``. As each actual
  2891. color may be specified in different ways, including as an image, each color slice is represented by
  2892. the :ref:`GMT_FILL <struct-fill>` structure.
  2893. .. _struct-palette:
  2894. .. code-block:: c
  2895. struct GMT_PALETTE { /* Holds all pen, color, and fill-related parameters */
  2896. /* Variables we document for the API: */
  2897. struct GMT_LUT *data; /* CPT lookup data read by GMT_read_cpt */
  2898. struct GMT_BFN bfn[3]; /* Structures with back/fore/nan fills */
  2899. unsigned int n_headers; /* Number of CPT header records (0 if no header) */
  2900. unsigned int n_colors; /* Number of colors in CPT lookup table */
  2901. unsigned int mode; /* Flags controlling use of BFN colors */
  2902. unsigned int model; /* RGB, HSV, CMYK */
  2903. unsigned int is_wrapping; /* true if a cyclic colortable */
  2904. unsigned int is_gray; /* true if only grayshades are needed */
  2905. unsigned int is_bw; /* true if only black and white are needed */
  2906. unsigned int is_continuous; /* true if continuous color tables have been given */
  2907. unsigned int has_pattern; /* true if CPT contains any patterns */
  2908. unsigned int has_hinge; /* true if CPT has a hinge */
  2909. unsigned int has_range; /* true if CPT has a natural range */
  2910. unsigned int categorical; /* true if CPT applies to categorical data */
  2911. double minmax[2]; /* The default range, if has_range is true */
  2912. double hinge; /* The default hinge, if is_wrapping is true */
  2913. double wrap_length; /* The default period, if has_hinge is true */
  2914. char **header; /* Array with all CPT header records, if any) */
  2915. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2916. };
  2917. The full definition of the ``GMT_LUT`` structure.
  2918. .. _struct-lut:
  2919. .. code-block:: c
  2920. struct GMT_LUT { /* For back-, fore-, and nan-colors */
  2921. double z_low, z_high, i_dz;
  2922. double rgb_low[4], rgb_high[4], rgb_diff[4];
  2923. double hsv_low[4], hsv_high[4], hsv_diff[4];
  2924. unsigned int annot; /* 1 for Lower, 2 for Upper, 3 for Both */
  2925. unsigned int skip; /* true means skip this slice */
  2926. struct GMT_FILL *fill; /* For patterns instead of color */
  2927. char *label; /* For non-number labels */
  2928. };
  2929. The full definition of the ``GMT_BFN`` structure:
  2930. .. _struct-bnf:
  2931. .. code-block:: c
  2932. struct GMT_BFN { /* For back-, fore-, and nan-colors */
  2933. double rgb[4]; /* Red, green, blue, and alpha */
  2934. double hsv[4]; /* Hue, saturation, value, alpha */
  2935. unsigned int skip; /* true means skip this slice */
  2936. struct GMT_FILL *fill; /* For patterns instead of color */
  2937. };
  2938. The full definition of the ``GMT_FILL`` structure. **Note**: Not part of the GMT API:
  2939. .. _struct-fill:
  2940. .. code-block:: c
  2941. struct GMT_FILL { /*! Holds fill attributes */
  2942. double rgb[4]; /* Chosen color if no pattern + Transparency 0-1 [0 = opaque] */
  2943. double f_rgb[4], b_rgb[4]; /* Colors applied to unset and set bits in 1-bit image */
  2944. bool use_pattern; /* true if pattern rather than rgb is set */
  2945. int pattern_no; /* Number of a predefined pattern, or -1 if not set */
  2946. unsigned int dpi; /* Desired dpi of image building-block if use_pattern is true */
  2947. char pattern[GMT_BUFSIZ];/* Full filename of user-defined raster pattern */
  2948. };
  2949. PostScript text
  2950. ~~~~~~~~~~~~~~~
  2951. Bulk PostScript is represented by a :ref:`GMT_POSTSCRIPT <struct-postscript>` structure that contains
  2952. ``data`` that points to the text array containing ``n_bytes`` characters of raw PostScript code. The
  2953. ``mode`` parameter reflects the status of the PostScript document.
  2954. .. _struct-postscript:
  2955. .. code-block:: c
  2956. struct GMT_POSTSCRIPT { /* Single container for a chunk of PostScript code */
  2957. /* Variables we document for the API: */
  2958. unsigned int n_headers; /* Number of PostScript header records (0 if no header) */
  2959. size_t n_bytes; /* Length of data array so far */
  2960. unsigned int mode; /* Bit-flag for header (1) and trailer (2) */
  2961. char *data; /* Pointer to PostScript code */
  2962. char **header; /* Array with all PostScript header records, if any) */
  2963. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2964. };
  2965. Matrix
  2966. ~~~~~~
  2967. User matrices are represented by a :ref:`GMT_MATRIX <struct-matrix>` structure that contains
  2968. ``data`` that points to an array of size ``n_columns`` by ``n_rows``. The
  2969. ``type`` indicates the memory type of the matrix, which is represented
  2970. by the :ref:`GMT_UNIVECTOR <struct-univector>` union.
  2971. .. _struct-matrix:
  2972. .. code-block:: c
  2973. struct GMT_MATRIX {
  2974. uint64_t n_rows; /* Number of rows in the matrix */
  2975. uint64_t n_columns; /* Number of columns in the matrix */
  2976. uint64_t n_layers; /* Number of layers in a 3-D matrix */
  2977. enum GMT_enum_fmt shape; /* 0 = C (rows) and 1 = Fortran (cols) */
  2978. enum GMT_enum_reg registration; /* 0 for gridline and 1 for pixel registration */
  2979. size_t dim; /* Allocated length of longest C or Fortran dim */
  2980. size_t size; /* Byte length of data */
  2981. enum GMT_enum_type type; /* Data type, e.g. GMT_FLOAT */
  2982. double range[6]; /* Contains xmin/xmax/ymin/ymax[/zmin/zmax] */
  2983. union GMT_UNIVECTOR data; /* Union with pointer to actual matrix of the chosen type */
  2984. char **text; /* Pointer to optional array of strings [NULL] */
  2985. char **header; /* Array with all Vector header records, if any) */
  2986. char command[GMT_GRID_COMMAND_LEN320]; /* name of generating command */
  2987. char remark[GMT_GRID_REMARK_LEN160]; /* comments re this data set */
  2988. const char *ProjRefPROJ4; /* To store a referencing system string in PROJ.4 format */
  2989. const char *ProjRefWKT; /* To store a referencing system string in WKT format */
  2990. int ProjRefEPSG; /* To store a referencing system EPSG code */
  2991. void *hidden; /* ---- Variables "hidden" from the API ---- */
  2992. };
  2993. Vectors
  2994. ~~~~~~~
  2995. User vectors are represented by a :ref:`GMT_VECTOR <struct-vector>` structure that contains
  2996. ``data`` that points to an array of ``n_columns`` individual vectors. The
  2997. ``type`` array indicates the memory type of each vector. Each vector is represented
  2998. by the :ref:`GMT_UNIVECTOR <struct-univector>` union which can accommodate any data type.
  2999. .. _struct-vector:
  3000. .. code-block:: c
  3001. struct GMT_VECTOR {
  3002. uint64_t n_columns; /* Number of vectors */
  3003. uint64_t n_rows; /* Number of rows in each vector */
  3004. enum GMT_enum_reg registration; /* 0 for gridline and 1 for pixel registration */
  3005. enum GMT_enum_type *type; /* Array with data type for each vector */
  3006. union GMT_UNIVECTOR *data; /* Array with unions for each column */
  3007. double range[2]; /* The min and max limits on t-range (or 0,0) */
  3008. char **text; /* Pointer to optional array of strings [NULL] */
  3009. char **header; /* Array with all Vector header records, if any) */
  3010. char command[GMT_GRID_COMMAND_LEN320]; /* name of generating command */
  3011. char remark[GMT_GRID_REMARK_LEN160]; /* comments re this data set */
  3012. const char *ProjRefPROJ4; /* To store a referencing system string in PROJ.4 format */
  3013. const char *ProjRefWKT; /* To store a referencing system string in WKT format */
  3014. int ProjRefEPSG; /* To store a referencing system EPSG code */
  3015. void *hidden; /* ---- Variables "hidden" from the API ---- */
  3016. };
  3017. The full definition of the ``GMT_UNIVECTOR`` union that holds a pointer to any array or matrix type:
  3018. .. _struct-univector:
  3019. .. code-block:: c
  3020. union GMT_UNIVECTOR {
  3021. uint8_t *uc1; /* Pointer for unsigned 1-byte array */
  3022. int8_t *sc1; /* Pointer for signed 1-byte array */
  3023. uint16_t *ui2; /* Pointer for unsigned 2-byte array */
  3024. int16_t *si2; /* Pointer for signed 2-byte array */
  3025. uint32_t *ui4; /* Pointer for unsigned 4-byte array */
  3026. int32_t *si4; /* Pointer for signed 4-byte array */
  3027. uint64_t *ui8; /* Pointer for unsigned 8-byte array */
  3028. int64_t *si8; /* Pointer for signed 8-byte array */
  3029. float *f4; /* Pointer for float array */
  3030. double *f8; /* Pointer for double array */
  3031. };
  3032. Appendix B: GMT constants
  3033. -------------------------
  3034. To increase readability we have encoded many simple integer constants as named
  3035. enum. These are listed in the tables below and used as flags to various API
  3036. functions.
  3037. .. _tbl-types:
  3038. +--------------+------------------------------------------+
  3039. | constant | description |
  3040. +==============+==========================================+
  3041. | GMT_CHAR | int8_t, 1-byte signed integer type |
  3042. +--------------+------------------------------------------+
  3043. | GMT_UCHAR | int8_t, 1-byte unsigned integer type |
  3044. +--------------+------------------------------------------+
  3045. | GMT_SHORT | int16_t, 2-byte signed integer type |
  3046. +--------------+------------------------------------------+
  3047. | GMT_USHORT | uint16_t, 2-byte unsigned integer type |
  3048. +--------------+------------------------------------------+
  3049. | GMT_INT | int32_t, 4-byte signed integer type |
  3050. +--------------+------------------------------------------+
  3051. | GMT_UINT | uint32_t, 4-byte unsigned integer type |
  3052. +--------------+------------------------------------------+
  3053. | GMT_LONG | int64_t, 8-byte signed integer type |
  3054. +--------------+------------------------------------------+
  3055. | GMT_ULONG | uint64_t, 8-byte unsigned integer type |
  3056. +--------------+------------------------------------------+
  3057. | GMT_FLOAT | 4-byte data float type |
  3058. +--------------+------------------------------------------+
  3059. | GMT_DOUBLE | 8-byte data float type |
  3060. +--------------+------------------------------------------+
  3061. The known data types in the GMT API.
  3062. When GMT_Open_VirtualFile_ is used with a NULL pointer to create a
  3063. virtual file for returning results from a GMT module *and* you are
  3064. using a :ref:`GMT_MATRIX <struct-matrix>` or :ref:`GMT_VECTOR <struct-vector>`
  3065. as your container, you may prescribe
  3066. the data type used for the underlying arrays. The constants below
  3067. can be added to the ``direction`` argument in order to change the
  3068. default data types [float for matrix and double for vector].
  3069. .. _tbl-viatypes:
  3070. +------------------+------------------------------------------+
  3071. | constant | description |
  3072. +==================+==========================================+
  3073. | GMT_VIA_CHAR | Select GMT_CHAR as array type |
  3074. +------------------+------------------------------------------+
  3075. | GMT_VIA_UCHAR | Select GMT_UCHAR as array type |
  3076. +------------------+------------------------------------------+
  3077. | GMT_VIA_SHORT | Select GMT_SHORT as array type |
  3078. +------------------+------------------------------------------+
  3079. | GMT_VIA_USHORT | Select GMT_USHORT as array type |
  3080. +------------------+------------------------------------------+
  3081. | GMT_VIA_INT | Select GMT_INT as array type |
  3082. +------------------+------------------------------------------+
  3083. | GMT_VIA_UINT | Select GMT_UINT as array type |
  3084. +------------------+------------------------------------------+
  3085. | GMT_VIA_LONG | Select GMT_LONG as array type |
  3086. +------------------+------------------------------------------+
  3087. | GMT_VIA_ULONG | Select GMT_ULONG as array type |
  3088. +------------------+------------------------------------------+
  3089. | GMT_VIA_FLOAT | Select GMT_FLOAT as array type |
  3090. +------------------+------------------------------------------+
  3091. | GMT_VIA_DOUBLE | Select GMT_DOUBLE as array type |
  3092. +------------------+------------------------------------------+
  3093. Flags to select the type of arrays used in output GMT_MATRIX or GMT_VECTOR.
  3094. Footnotes
  3095. ---------
  3096. .. [1]
  3097. or via a very confusing and ever-changing myriad of low-level library
  3098. functions for bold programmers.
  3099. .. [2]
  3100. Currently, C/C++, FORTRAN, MATLAB and Julia are being tested.
  3101. .. [3]
  3102. At the moment, GMT does not have native support for 3-D grids.
  3103. .. [4]
  3104. This may change in later releases.
  3105. .. [5]
  3106. However, there is no thread-support yet, so you will need to manage your
  3107. own threads.
  3108. .. ------------------------------------- Examples code -------------------
  3109. .. |ex_resource_init| raw:: html
  3110. <a href="#openModal">Example</a>
  3111. <div id="openModal" class="modalDialog">
  3112. <div>
  3113. <a href="#close" title="Close" class="close">X</a>
  3114. <h2>Resource initialization example</h2>
  3115. <p>
  3116. </p>
  3117. </div>
  3118. </div>
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...