File: GMT_API.rst

package info (click to toggle)
gmt 5.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 150,460 kB
  • ctags: 18,539
  • sloc: ansic: 194,217; sh: 7,349; xml: 149; makefile: 72; fortran: 49; lisp: 41; csh: 5
file content (3805 lines) | stat: -rw-r--r-- 184,329 bytes parent folder | download | duplicates (2)
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
:tocdepth: 4

.. set default highlighting language for this document:

.. highlight:: c

**The Generic Mapping Tools**

**C/C++ Application Programming Interface**

**PÃ¥l (Paul) Wessel**

**SOEST, University of Hawai'i at Manoa**

**Walter H. F. Smith**

**Laboratory for Satellite Altimetry, NOAA/NESDIS/STAR**

**Remko Scharroo**

**EUMETSAT, Darmstadt, Germany**

**Joaquim F. Luis**

**Universidade do Algarve, Faro, Portugal**

**Florian Wobbe**

**Alfred Wegener Institute, Germany**

Introduction
============

.. index:: ! API

Preamble
--------

.. figure:: /_images/GMT4_mode.png
   :height: 554 px
   :width: 1122 px
   :align: center
   :scale: 50 %

   GMT 4 programs contain all the high-level functionality.


Prior to version 5, the bulk of GMT functionality was coded directly
in the standard GMT C program modules (e.g., ``surface.c``, ``psxy.c``, ``grdimage.c``, etc.). The
GMT library only offered access to low-level functions from which
those high-level GMT programs were built. The standard GMT programs
have been very successful, with tens of thousands of users world-wide.
However, the design of the main programs prevented developers from
leveraging GMT functionality from within other programming
environments since access to GMT tools could only be achieved via
system calls [1]_. Consequently, all data i/o had to be done via
temporary files. The design also prevented the GMT developers
themselves from taking advantage of these modules directly. For
instance, the tool :doc:`pslegend` needed to
make extensive use of system calls to :doc:`psxy` and
:doc:`pstext` in order to plot the lines,
symbols and text that make up a map legend, making it a very awkward
program to maintain.

.. figure:: /_images/GMT5_mode.png
   :height: 399 px
   :width: 1116 px
   :align: center
   :scale: 50 %

   GMT 5 programs contain all the high-level functionality.


Starting with GMT version 5, all standard GMT programs have been
rewritten into separate function "modules" invoked by a single
driver program called ``gmt.c``.
The :doc:`gmt` executable simply calls the corresponding
GMT modules; it is these modules that do all the work. These new
functions have been placed in a new GMT high-level API library and can
be called from a variety of environments (C/C++, Fortran, Julia, Python,
MATLAB, Visual Basic, R, etc.) [2]_. For example, the main
program ``blockmean.c`` has been reconfigured as a high-level function
``GMT_blockmean()``, which does the actual spatial averaging and can
pass the result back to the calling program (or write it to file). The
previous behavior of ``blockmean.c`` is achieved by calling ``gmt blockmean``,
i.e., the module is now just the first argument to the :doc:`gmt` executable.
For backwards compatibility with older GMT (4) scripts we optionally
install numerous symbolic links to the gmt executable with names such
as blockmean, psxy, surface, etc.  The gmt executable is smart enough to
understand when it is being invoked via one of these links and then knows
which module to call upon.
Consequently, ``blockmean.c`` and other files do in
fact no longer exist.

.. figure:: /_images/GMT5_external.png
   :height: 616 px
   :width: 1193 px
   :align: center
   :scale: 50 %

   GMT 5 API showing current and future external environments.


The i/o abstraction layer
-------------------------

In order for the API to be as flexible as possible we have
generalized the notions of input and output. Data that already reside in
an application's memory may serve as input to a GMT module and we refer
to such data as "Virtual Files". Other
sources of input may be file pointers and file descriptors (as well as
the standard mechanism for passing file names). For standard
data table i/o, the GMT API takes care of the task of assembling any
combination of files, pointers, and memory locations into *a single
virtual data set* from which the GMT module may read (a) all
records at once into memory, or (b) read one record at a time. Likewise,
GMT functions may write their output to a virtual destination, which
might be a memory location in the user's application (another Virtual File), a file pointer or
descriptor, or an output file. The GMT modules are unaware of these
details and simply read from a "source" and write to a "destination".
Thus, the standard concept of file-based input/output so familiar to
any GMT user carries over to the API, except for the generalization
that files can be virtual files already in memory.  Because of this
design we will see that we need to associate these virtual files
with special filenames that we may pass to modules, and the modules
will faithfully treat these as real files.  However, under the hood
the API layer will take care of the differences between real and
virtual files.

Users who wish to maintain their own data types and memory management
can also use the GMT modules, but some limitations and requirements do
apply: The user's data can either be provided as (1) a 2-D matrix (of
any data type, e.g., float, integer, etc.) and in any memory layout
configuration (e.g., row-major or column-major layout) or as (2) a
set of column vectors that each may be of any type.  These custom arrays
will need to be hooked onto the GMT containers :ref:`GMT_MATRIX <struct-matrix>`
and :ref:`GMT_VECTOR <struct-vector>`, respectively.
Such objects can then be treated as virtual files for either input of output.

Our audience
------------

Here, we document the new functions in the GMT API library for
application developers who wish to call these functions from their own
custom programs. At this point, only the new high-level GMT API is
fully documented and intended for public use. The structure and
documentation of the under-lying lower-level GMT library is not
finalized. Developers using these functions may risk disruption to their
programs due to changes we may make in the library in support of the
GMT API. However, developers who wish to make supplemental packages to
be distributed as part of GMT will (other than talk to us) probably
want to access the entire low-level GMT library as well. It is
unlikely that the low-level library will ever be fully documented.

There are two classes of development that users can pursue:

#. Building stand-alone custom executables that link with the shared GMT
   API.  Our examples in this documentation are of this kind.  There programs
   are likely to address a user's special data formats or processing needs
   by leveraging high-level GMT modules to do some of the heavy lifting.

#. Building shared library plugins to extend the breath of GMT.  Users who
   wish to build one or more new modules and distributed then via a plugin
   that is dynamically loaded at run-time can now do so.   At the present,
   all the modules in the official GMT supplement are compiled into a single
   plugin that can be accessed at run-time.  Similarly, developers may add
   additional plugin libraries with any number of GMT-like modules and these
   will then be available from the gmt command (as well as from derived
   interfaces such as the GMT/MATLAB toolbox and the Python module).  An
   example of plugin development is given by the
   `GSFML extension to GMT <http://www.soest.hawaii.edu/PT/GSFML/>`_.

Definitions
-----------

For the purpose of this documentation a few definitions are needed:

#. "Standard GMT program" refers to one of the traditional stand-alone
   command-line executables known to all GMT users, e.g.,
   :doc:`blockmean`, :doc:`psxy`,
   :doc:`grdimage`, etc. Prior to version 5,
   these were the only GMT executables available.  In GMT 5, these are
   accessed via the :doc:`gmt` executable.

#. "\ GMT module" refers to the function in the GMT API library that
   is responsible for all the action taken by the corresponding
   standard GMT program. All such modules are given the same names as the
   corresponding programs, e.g., "blockmean", but are invoked via the
   GMT_Call_Module_ function, which takes the name of the module as
   one of its arguments.

#. "\ GMT application" refers to a new application written by any
   developer.  It uses the API, perhaps for custom i/o, and may call one
   or more GMT functions to create a new GMT-compatible executable.

#. "\ GMT plugin library" refers to a collection of one or more new custom
   GMT-like modules that are presented as a plugin library.  It such libraries
   are placed in the official GMT plugin directory or their path is added to
   the GMT defaults parameter GMT_CUSTOM_LIBS then the :doc:`gmt` executable can find them.

#. "Family" refers to one of the many high-level GMT data types (e.g., grids, CPTs)
   and is typically a required argument to some API functions.

#. "Method" refers to one of several ways in which data can be read or written
   in the API, including from existing memory variables.

#. "Direction" is typically either GMT_IN (for reading) or GMT_OUT (for writing).

#. In the API description that follows we will use the type ``int`` to
   mean a 4-byte integer. All integers used in the API are 4-byte
   integers with the exception of one function where an 8-byte integer is
   used. Since different operating systems have their own way of
   defining 8-byte integers we use C99's ``int64_t`` for this purpose;
   it is guaranteed to yield the correct type that the GMT function
   expects.

In version 5, the standard GMT programs are themselves simple invocations
of the :doc:`gmt` application with the function name as argument.
However, some of these modules, such as
:doc:`pslegend`, :doc:`gmtconvert`,
:doc:`grdblend`,
:doc:`grdfilter` and others may call several additional modules.

GMT resources
-------------

The GMT API knows how to create, duplicate, read and write six types of data objects common to
GMT operations: Pure data tables (ASCII or binary), grids, images, color
palette tables (also known as CPT), PostScript documents, and text tables (ASCII,
usually a mix of data and free-form text).  In addition, we
provide two data objects to facilitate the passing of simple user arrays
(one or more equal-length data columns of any data type, e.g., double,
char) and 2-D or 3-D user matrices (of any data type and column/row
organization [3]_). We refer to these data types as GMT *resources*.
There are many attributes for each of these resources and therefore we
use a top-level structure for each object to keep them all within one
container. These containers are given or returned by GMT API
functions using opaque pointers (``void *``). Below we provide a brief
overview of these containers, listing only the most critical members.
For complete details, see Appendix A.  We will later present how they are used when
importing or exporting them to or from files, memory locations, or
streams. The first six are the standard GMT objects, while the latter
two are special data containers to facilitate the passing of user
data in and out of GMT modules. These resources are defined in the include
file ``gmt_resources.h``; please consult this file to ensure correctness
in case the documentation is not up-to-date.  Note than in all instances
the fundamental data variable is called "data".

Data tables
~~~~~~~~~~~

Much data processed in GMT come in the form of ASCII, netCDF, or
native binary data tables. These may have any number of header records
(ASCII files only) and perhaps segment headers that separate groups of points
or lines and polygons. GMT programs will read
one or more such tables when importing data. However, to avoid memory
duplication or data limitations some programs may prefer to read such records one
at the time. The GMT API has functions that let you read your data
record-by-record by presenting a *virtual* data set that combines all the
data tables specified as input. This simplifies record processing
considerably.  Programs reading an entire data set will encounter several
structures: A data set (``struct`` :ref:`GMT_DATASET <struct-dataset>`) may contain any number of
tables (``struct`` :ref:`GMT_DATATABLE <struct-datatable>`), each with any number of segments
(``struct`` :ref:`GMT_DATASEGMENT <struct-datasegment>`), each segment with any number of
records, and each record with any number of (fixed) columns. Thus, the arguments
to GMT API functions that handle such data sets expect a struct :ref:`GMT_DATASET <struct-dataset>`.
All segments are expected to have the same number of columns.

.. _struct-dataset2:

.. code-block:: c

   struct GMT_DATASET {	/* Single container for an array of GMT tables (files) */
       uint64_t  n_tables;     /* The total number of tables contained */
       uint64_t  n_columns;    /* The number of data columns */
       uint64_t  n_segments;   /* The total number of segments across all tables */
       uint64_t  n_records;    /* The total number of data records across all tables */
       double   *min;         /* Minimum coordinate for each column */
       double   *max;         /* Maximum coordinate for each column */
       struct GMT_DATATABLE **table;    /* Pointer to array of tables */
   };

The top-level dataset structure for pure data tables contains the table structure, as defined below:

.. _struct-datatable2:

.. code-block:: c

   struct GMT_DATATABLE {  /* Single container for an array of data segments */
       unsigned int n_headers;    /* Number of table header records (0 if no header) */
       uint64_t     n_columns;    /* Number of columns (fields) in each record */
       uint64_t     n_segments;   /* Number of segments in the array */
       uint64_t     n_records;    /* Total number of data records across all segments */
       double      *min;          /* Minimum coordinate for each column */
       double      *max;          /* Maximum coordinate for each column */
       char       **header;       /* Array with all table header records, if any) */
       struct GMT_DATASEGMENT **segment; /* Pointer to array of segments */
   };

Finally, the table structure depends on a structure for individual data segments:

.. _struct-datasegment2:

.. code-block:: c

   struct GMT_DATASEGMENT {       /* For holding segment lines in memory */
       uint64_t n_rows;           /* Number of points in this segment */
       uint64_t n_columns;        /* Number of fields in each record (>= 2) */
       double  *min;              /* Minimum coordinate for each column */
       double  *max;              /* Maximum coordinate for each column */
       double **data;             /* Data x,y, and possibly other columns */
       char    *label;            /* Label string (if applicable) */
       char    *header;           /* Segment header (if applicable) */
    ;

Data sets may have different geometries, such as representing a set of points,
one or more lines, or closed polygons.

GMT grids
~~~~~~~~~

GMT grids are used to represent equidistant and organized 2-D
surfaces. These can be processed or plotted as contour maps, color images, or
perspective surfaces. Because the native GMT grid is simply a 1-D
float array with metadata kept in a separate ``struct`` :ref:`GMT_GRID_HEADER <struct-gridheader>` header, we pass
this information via a ``struct`` :ref:`GMT_GRID <struct-grid>`, which is a container that
holds both items. Thus, the arguments to GMT API functions that handle
GMT grids expect this type of variable.

.. _struct-grid2:

.. code-block:: c

   struct GMT_GRID {                        /* A GMT float grid and header in one container */
       struct GMT_GRID_HEADER *header;      /* The full GMT header for the grid */
       float                  *data;        /* Pointer to the float grid array */
   };

The top-level grid structure, holding both header and data array, depends on the grid header structure:

.. code-block:: c

   struct GMT_GRID_HEADER {
       uint32_t n_columns;                     /* Number of columns */
       uint32_t n_rows;                        /* Number of rows */
       uint32_t registration;                  /* GMT_GRID_NODE_REG (0) for node grids,
						  GMT_GRID_PIXEL_REG (1) for pixel grids */
       double wesn[4];                         /* Min/max x and y coordinates */
       double z_min;                           /* Minimum z value */
       double z_max;                           /* Maximum z value */
       double inc[2];                          /* The x and y increments */
       double z_scale_factor;                  /* Grid values must be multiplied by this factor */
       double z_add_offset;                    /* After scaling, add this */
       char   x_units[GMT_GRID_UNIT_LEN80];    /* Units in x-direction */
       char   y_units[GMT_GRID_UNIT_LEN80];    /* Units in y-direction */
       char   z_units[GMT_GRID_UNIT_LEN80];    /* Grid value units */
       char   title[GMT_GRID_TITLE_LEN80];     /* Name of data set */
       char   command[GMT_GRID_COMMAND_LEN320];/* Name of generating command */
       char   remark[GMT_GRID_REMARK_LEN160];  /* Comments regarding this data set */
   };

   The basic grid header holds the metadata written to grid files.

GMT images
~~~~~~~~~~

GMT images are used to represent bit-mapped images typically obtained
via the GDAL bridge. These can be reprojected internally, such as when
used in :doc:`grdimage`. Since images and grids share the concept of a header,
we use the same header structure for grids as for images; however, some
additional metadata attributes are also needed. Finally, the image
itself may be of any data type and have more than one band (channel).
Both image and header information are passed via a ``struct`` :ref:`GMT_IMAGE <struct-image>`,
which is a container that holds both items. Thus, the arguments to
GMT API functions that handle GMT images expect this type of
variable. Unlike the other objects, writing images has only partial
support via :doc:`grdimage` [4]_.
For the full definition, see :ref:`GMT_IMAGE <struct-image>`.

.. _struct-image2:

.. code-block:: c

   struct GMT_IMAGE {     /* A GMT char image, header, and colormap in one container */
	enum GMT_enum_type      type;             /* Data type, e.g. GMT_FLOAT */
	int                    *colormap;         /* Array with color lookup values */
	int                     n_indexed_colors; /* Number of colors in a color-mapped image */
	struct GMT_GRID_HEADER *header;           /* Pointer to full GMT header for the image */
	unsigned char          *data;             /* Pointer to actual image */
   };

Color palette tables (CPT)
~~~~~~~~~~~~~~~~~~~~~~~~~~

The color palette table files, or just CPTs, contain colors and
patterns used for plotting data, such as surfaces (i.e., GMT grids) or
symbols, lines and polygons (i.e., GMT data sets). GMT programs will
generally read a color palette table, make it the current palette, do
the plotting, and destroy the table when done. The information is
accessed via a pointer to ``struct`` :ref:`GMT_PALETTE <struct-palette>`. Thus, the arguments
to GMT API functions that handle palettes expect this type of
variable. It is not expected that users will wish to manipulate the CPT
directly, but rather use this mechanism to hold them in memory and
pass as arguments to GMT modules.  Developers are unlikely to actually
manipulate the contents of CPT structures but if needed then
the full definition can be found in :ref:`GMT_PALETTE <struct-palette>`.

.. _struct-palette2:

.. code-block:: c

   struct GMT_PALETTE {	/* Holds color-related parameters for look-up */
       unsigned int          n_headers;     /* Number of CPT header records (0 if no header) */
       unsigned int          n_colors;      /* Number of colors in the data array */
       unsigned int          mode;          /* Flags controlling use of BFN colors */
       struct GMT_LUT       *data;          /* CPT lookup data with color information */
       struct GMT_BFN        bfn[3];        /* Structures with back/fore/nan fills */
       char                **header;        /* Array with all CPT header records, if any) */
   };

PostScript document
~~~~~~~~~~~~~~~~~~~

Normally, GMT modules producing PostScript will write to standard output
or a designated file.  Alternatively, you can tell the API to write to a
memory buffer instead and then receive a structure with the final
plot (or partial plot) represented as a long text string.
The full structure definition can be found in :ref:`GMT_POSTSCRIPT <struct-postscript>`.

.. _struct-postscript2:

.. code-block:: c

   struct GMT_POSTSCRIPT {	/* Single container for a chunk of PostScript text */
       unsigned int n_headers;          /* Number of PostScript header records (0 if no header) */
       size_t n_alloc;                  /* Size of array allocated so far */
       size_t n_bytes;                  /* Length of data array so far */
       unsigned int mode;               /* Bit-flag for header (1) and trailer (2) */
       char *data;                      /* Pointer to actual PostScript text */
       char **header;                   /* Array with all PostScript header records, if any) */
   };

Text tables
~~~~~~~~~~~

Some data needed by GMT are simply free-form ASCII text tables. In many respects these
are handled similarly to data tables. E.g., they may have any number of
header records and even segment headers, and GMT programs can read one
or more tables or get text records one at the time. A
``struct`` :ref:`GMT_TEXTSET <struct-textset>` may contain any number of ``struct`` :ref:`GMT_TEXTTABLE <struct-texttable>`, each with any
number of ``struct`` :ref:`GMT_TEXTSEGMENT <struct-textsegment>`, and each segment with any number of text records. Thus,
the arguments to GMT API functions that handle such data sets expect
this type of variable. The user's program may then parse and process
such text records as required. This resources is particularly useful
when your data consist of a mix or data coordinates and ordinary text
since regular data tables will be parsed for floating-point columns
only.  For the full definition, see :ref:`GMT_TEXTSET <struct-textset>`.

.. _struct-textset2:

.. code-block:: c

   struct GMT_TEXTSET {	/* Single container for an array of GMT text tables (files) */
       uint64_t               n_tables;     /* The total number of tables in the set */
       uint64_t               n_segments;   /* The total number of segments in the set */
       uint64_t               n_records;    /* The total number of data records in the set */
       struct GMT_TEXTTABLE **table;        /* Pointer to array of tables */
   };

The top-level structure for mixed data/text data sets depends on the text table structure:

.. _struct-texttable2:

.. code-block:: c

   struct GMT_TEXTTABLE {  /* Single container for an array of text segments */
       unsigned int n_headers;   /* Number of table header records (0 if no header) */
       uint64_t n_segments;      /* Number of segments in the table */
       uint64_t n_records;       /* Total number of data records in the table */
       char   **header;          /* Array with all table header records, if any) */
       struct GMT_TEXTSEGMENT **segment; /* Pointer to array of segments */
   };

Finally, the text table structure depends on the structure for text segments:

.. _struct-textsegment2:

.. code-block:: c

   struct GMT_TEXTSEGMENT {      /* For holding segment text records in memory */
       uint64_t n_rows;          /* Number of rows in this segment */
       char   **data;            /* Array of text records */
       char    *label;           /* Label string (if applicable) */
       char    *header;          /* Segment header (if applicable) */
   };


User data matrices
~~~~~~~~~~~~~~~~~~

Users may write programs that need to call GMT modules but may keep their data in separate
2-D arrays that the allocate and maintain independent of GMT.
For instance, a program may have built an integer 2-D matrix in memory and wish to
use that as the input grid to the ``grdfilter`` module, which
normally expects a ``struct`` :ref:`GMT_GRID <struct-grid>` with floating point data via an actual or virtual
file. To handle this case we create a ``struct`` :ref:`GMT_MATRIX <struct-matrix>` container (see :ref:`Create empty resources <sec-create>`),
assign the appropriate union pointer to your data matrix and provide information on dimensions
and data type. We then open this container as a virtual file and pass its filename to any module.
The full structure definition can be found in :ref:`GMT_MATRIX <struct-matrix>`.

.. _struct-matrix2:

.. code-block:: c

   struct GMT_MATRIX {	/* Single container to hold a user matrix */
      uint64_t             n_rows;        /* Number of rows in the matrix */
      uint64_t             n_columns;     /* Number of columns in the matrix */
      uint64_t             n_layers;      /* Number of layers in a 3-D matrix */
      enum GMT_enum_fmt    shape;         /* 0 = C (rows) and 1 = Fortran (cols) */
      enum GMT_enum_reg    registration;  /* 0 for gridline and 1 for pixel registration  */
      size_t               dim;           /* Allocated length of longest C or Fortran dim */
      size_t               size;          /* Byte length of data */
      enum GMT_enum_type   type;          /* Data type, e.g. GMT_FLOAT */
      double               range[6];      /* Contains xmin/xmax/ymin/ymax[/zmin/zmax] */
      union GMT_UNIVECTOR  data;          /* Pointer to actual matrix of the chosen type */
   };

The ``enum`` types referenced in :ref:`GMT_VECTOR <struct-vector>` and
Table :ref:`GMT_MATRIX <struct-matrix>` and summarized in Table :ref:`types <tbl-types>`.

User data columns
~~~~~~~~~~~~~~~~~

Likewise, programs may instead be manipulating a set of custom column vectors.
For instance, the user's program may have allocated and populated
three column arrays of type float and wishes to use these as the input
source to the ``surface`` module, which normally expects double
precision triplets via a ``struct`` :ref:`GMT_DATASET <struct-dataset>` read from an actual or virtual file 
Simply create a new :ref:`GMT_VECTOR <struct-vector>` container
(see section :ref:`Create empty resources <sec-create>`) and assign the union array pointers (see
:ref:`univector <struct-univector>`) to your data columns and provide the required
information on length, data types, and optionally range. Again, once we open this data
as a virtual file we can pass its filename to any module expecting such data.
The full structure definition can be found in :ref:`GMT_VECTOR <struct-vector>`.

.. _struct-vector2:

.. code-block:: c

  struct GMT_VECTOR {	/* Single container to hold user vectors */
      uint64_t             n_columns;     /* Number of vectors */
      uint64_t             n_rows;        /* Number of rows in each vector */
      enum GMT_enum_reg    registration;  /* 0 for gridline and 1 for pixel registration */
      enum GMT_enum_type  *type;          /* Array with data type for each vector */
      union GMT_UNIVECTOR *data;          /* Array with unions for each column */
      double               range[2];      /* The min and max limits on t-range (or 0,0) */
  };

.. _chapter-overview:

Overview of the GMT C Application Program Interface
===================================================

Users who wish to create their own GMT application based on the API
must make sure their program goes through the steps below. The details for
each step will be revealed in the following chapter. We have kept the
API simple: In addition to the GMT modules, there are only 57 public
functions to become familiar with, but most applications will only use a
very small subset of this selection. Functions either return an integer error
code (when things go wrong; otherwise it is set to ``GMT_NOERROR (0)``), or they
return a void pointer to a GMT resource (or NULL if things go wrong).
In either case, the API will report what the error is. The layout here
assumes you wish to use virtual files as input sources (i.e., data you already
have in memory); if the data must be
read from actual data files then things simplify considerably.

To keep things as simple as possible we will assume you are writing an
application that will read in table data, call a module using the data in
memory as input, and then save the output from the module back into
another memory location.  No actual processing of the data or further
calculation will be done here (so a bit of a boring program, but the
point is to develop something short we can test).  Also, to keep the code
short we completely ignore
the return codes of the modules for now.  We will call our program
:ref:`example1.c <example-code1>`.  Here are the steps:

#. Initialize a new GMT session with GMT_Create_Session_, which
   allocates a hidden GMT API control structure and returns an opaque
   pointer to it. This pointer is a *required* argument to all subsequent
   GMT API function calls within the session.

#. Read a data set (or grid, etc.) into memory with GMT_Read_Data_,
   which, depending on data type, returns one of the data structures
   discussed earlier.

#. Associate your data with a virtual file using GMT_Open_VirtualFile_.
   This steps returns a special filename that you can use to tell a module where
   to read its input.  No actual file is created.

#. Open a new virtual file to hold the output using GMT_Open_VirtualFile_.
   This step also returns a special filename for the module to send its output.

#. Prepare required arguments (including the two virtual file names) and
   call the GMT module you wish to use via GMT_Call_Module.

#. Obtain the desired output object via GMT_Read_VirtualFile_, which
   returns a data structure of requested type.

#. Close the virtual files you have been using with GMT_Close_VirtualFile_.

#. We terminate the GMT session by calling GMT_Destroy_Session_.

Example code
------------

For the example code to run, make sure the data table "table_5.11" from gallery
example 14 is placed in the current directory, then compile and run
this program:

.. _example-code1:

.. code-block:: c

  #include "gmt.h"
  int main (int argc, char *argv[]) {
      void *API;        		/* The API control structure */
      struct GMT_DATASET *D = NULL;     /* Structure to hold input dataset */
      struct GMT_GRID *G = NULL;        /* Structure to hold output grid */
      char input[GMT_STR16] = {""};     /* String to hold virtual input filename */
      char output[GMT_STR16] = {""};    /* String to hold virtual output filename */
      char args[128] = {""};    	/* String to hold module command arguments */
      
      /* Initialize the GMT session */
      API = GMT_Create_Session ("test", 2U, 0, NULL);
      /* Read in our data table to memory */
      D = GMT_Read_Data (API, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_PLP, GMT_READ_NORMAL, NULL,
          "table_5.11", NULL);
      /* Associate our data table with a virtual file */
      GMT_Open_VirtualFile (API, GMT_IS_DATASET, GMT_IS_PLP, GMT_IN, D, input);
      /* Create a virtual file to hold the resulting grid */
      GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_OUT, NULL, output);
      /* Prepare the module arguments */
      sprintf (args, "-R0/7/0/7 -I0.2 -D1 -St0.3 %s -G%s", input, output);
      /* Call the greenspline module */
      GMT_Call_Module (API, "greenspline", GMT_MODULE_CMD, args);
      /* Obtain the grid from the virtual file */
      G = GMT_Read_VirtualFile (API, output);
      /* Close the virtual files */
      GMT_Close_VirtualFile (API, 0, input);
      GMT_Close_VirtualFile (API, 0, output);
      /* Write the grid to file */
      GMT_Write_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_READ_NORMAL, NULL,
          "junk.nc", G);
      /* Destroy the GMT session */
      GMT_Destroy_Session (API);
  };

Compilation
-----------

To compile this program (we assume it is called example1.c), we use the
gmt-config script to determine the correct compile and link flags and then run
gcc:

.. _example-comp:

.. code-block:: bash

   inc=`gmt-config --cflags`
   lib=`gmt-config --libs`
   gcc example1.c $inc $lib -o example1
   ./example1

This obviously assumes you have already installed GMT and that it is in your path.
If you run example1 it will take a moment (this is mostly due to the gridding
performed by :doc:`greenspline`) and then it stops.  You should find the resulting
grid junk.nc in the current directory.  Plot it to see if it makes sense, e.g.

.. _example-view:

.. code-block:: bash

   gmt grdimage junk.nc > junk.ps

If you intend to write applications that take any number of data files
via the command line then there will be more book-keeping to deal with,
and we will discuss those steps later.
Likewise, if you need to process a file record-by-record then more lines
of code will be required.

Plugins
-------

Developers who wish to make custom plugin libraries that are compatible
with GMT should examine the fully functioning examples of more involved
code, available from the repository gmt-custom, obtainable via

.. code-block:: bash

   svn checkout svn://gmtserver.soest.hawaii.edu/gmt-custom/trunk gmt-custom


List of API functions
---------------------

The following is an alphabetical listing of all the public API functions in GMT. Click on
any of them to see the full syntax of each function.

.. _tbl-API:

    +--------------------------+-------------------------------------------------------+
    | constant                 | description                                           |
    +==========================+=======================================================+
    | GMT_Alloc_Segment_       | Allocate data and text segments                       |
    +--------------------------+-------------------------------------------------------+
    | GMT_Append_Option_       | Append new option structure to linked list            |
    +--------------------------+-------------------------------------------------------+
    | GMT_Begin_IO_            | Enable record-by-record i/o                           |
    +--------------------------+-------------------------------------------------------+
    | GMT_Call_Module_         | Call any of the GMT modules                           |
    +--------------------------+-------------------------------------------------------+
    | GMT_Convert_Data_        | Convert between compatible data types                 |
    +--------------------------+-------------------------------------------------------+
    | GMT_Close_VirtualFile_   | Close a virtual file                                  |
    +--------------------------+-------------------------------------------------------+
    | GMT_Create_Args_         | Convert linked list of options to text array          |
    +--------------------------+-------------------------------------------------------+
    | GMT_Create_Cmd_          | Convert linked list of options to command line        |
    +--------------------------+-------------------------------------------------------+
    | GMT_Create_Data_         | Create an empty data resource                         |
    +--------------------------+-------------------------------------------------------+
    | GMT_Create_Options_      | Convert command line options to linked list           |
    +--------------------------+-------------------------------------------------------+
    | GMT_Create_Session_      | Initialize a new GMT session                          |
    +--------------------------+-------------------------------------------------------+
    | GMT_Delete_Option_       | Delete an option structure from the linked list       |
    +--------------------------+-------------------------------------------------------+
    | GMT_Destroy_Args_        | Delete text array of arguments                        |
    +--------------------------+-------------------------------------------------------+
    | GMT_Destroy_Cmd_         | Delete text command of arguments                      |
    +--------------------------+-------------------------------------------------------+
    | GMT_Destroy_Data_        | Delete a data resource                                |
    +--------------------------+-------------------------------------------------------+
    | GMT_Destroy_Group_       | Delete a group of data resources                      |
    +--------------------------+-------------------------------------------------------+
    | GMT_Destroy_Options_     | Delete the linked list of option structures           |
    +--------------------------+-------------------------------------------------------+
    | GMT_Destroy_Session_     | Terminate a GMT session                               |
    +--------------------------+-------------------------------------------------------+
    | GMT_Duplicate_Data_      | Make an identical copy of a data resources            |
    +--------------------------+-------------------------------------------------------+
    | GMT_Encode_Options_      | Encode option arguments for external interfaces       |
    +--------------------------+-------------------------------------------------------+
    | GMT_Expand_Option_       | Expand option with explicit memory references         |
    +--------------------------+-------------------------------------------------------+
    | GMT_End_IO_              | Disable further record-by-record i/o                  |
    +--------------------------+-------------------------------------------------------+
    | GMT_FFT_                 | Take the Fast Fourier Transform of data object        |
    +--------------------------+-------------------------------------------------------+
    | GMT_FFT_1D_              | Take the Fast Fourier Transform of 1-D float data     |
    +--------------------------+-------------------------------------------------------+
    | GMT_FFT_2D_              | Take the Fast Fourier Transform of 2-D float data     |
    +--------------------------+-------------------------------------------------------+
    | GMT_FFT_Create_          | Initialize the FFT machinery                          |
    +--------------------------+-------------------------------------------------------+
    | GMT_FFT_Destroy_         | Terminate the FFT machinery                           |
    +--------------------------+-------------------------------------------------------+
    | GMT_FFT_Option_          | Explain the FFT options and modifiers                 |
    +--------------------------+-------------------------------------------------------+
    | GMT_FFT_Parse_           | Parse argument with FFT options and modifiers         |
    +--------------------------+-------------------------------------------------------+
    | GMT_FFT_Wavenumber_      | Return wavenumber given data index                    |
    +--------------------------+-------------------------------------------------------+
    | GMT_Find_Option_         | Find an option in the linked list                     |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Common_          | Determine if a GMT common option was set              |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Coord_           | Create a coordinate array                             |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Default_         | Obtain one of the API or GMT default settings         |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Index_           | Convert row, col into a grid or image index           |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Matrix_          | Obtain pointer to user matrix from container          |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Pixel_           | Get grid or image node                                |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Record_          | Import a single data record                           |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Row_             | Import a single grid row                              |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Values_          | Convert string into coordinates or dimensions         |
    +--------------------------+-------------------------------------------------------+
    | GMT_Get_Vector_          | Obtain pointer to user vector from container          |
    +--------------------------+-------------------------------------------------------+
    | GMT_Init_IO_             | Initialize i/o given registered resources             |
    +--------------------------+-------------------------------------------------------+
    | GMT_Init_VirtualFile_    | Reset a virtual file for reuse                        |
    +--------------------------+-------------------------------------------------------+
    | GMT_Make_Option_         | Create an option structure                            |
    +--------------------------+-------------------------------------------------------+
    | GMT_Message_             | Issue a message, optionally with time stamp           |
    +--------------------------+-------------------------------------------------------+
    | GMT_Open_VirtualFile_    | Select memory location as input or output for module  |
    +--------------------------+-------------------------------------------------------+
    | GMT_Option_              | Explain one or more GMT common options                |
    +--------------------------+-------------------------------------------------------+
    | GMT_Parse_Common_        | Parse the GMT common options                          |
    +--------------------------+-------------------------------------------------------+
    | GMT_Put_Matrix_          | Put user matrix into container                        |
    +--------------------------+-------------------------------------------------------+
    | GMT_Put_Record_          | Export a data record                                  |
    +--------------------------+-------------------------------------------------------+
    | GMT_Put_Row_             | Export a grid row                                     |
    +--------------------------+-------------------------------------------------------+
    | GMT_Put_Vector_          | Put user vector into container                        |
    +--------------------------+-------------------------------------------------------+
    | GMT_Read_Data_           | Import a data resource or file                        |
    +--------------------------+-------------------------------------------------------+
    | GMT_Read_Group_          | Import a group of data resources or files             |
    +--------------------------+-------------------------------------------------------+
    | GMT_Read_VirtualFile_    | Access the output from a module via memory            |
    +--------------------------+-------------------------------------------------------+
    | GMT_Register_IO_         | Register a resources for i/o                          |
    +--------------------------+-------------------------------------------------------+
    | GMT_Report_              | Issue a message contingent upon verbosity level       |
    +--------------------------+-------------------------------------------------------+
    | GMT_Set_Default_         | Set one of the API or GMT default settings            |
    +--------------------------+-------------------------------------------------------+
    | GMT_Set_Comment_         | Assign a comment to a data resource                   |
    +--------------------------+-------------------------------------------------------+
    | GMT_Set_Columns_         | Specify how many output columns to use for rec-by-rec |
    +--------------------------+-------------------------------------------------------+
    | GMT_Set_Index_           | Convert row, col into a grid or image index           |
    +--------------------------+-------------------------------------------------------+
    | GMT_Status_IO_           | Check status of record-by-record i/o                  |
    +--------------------------+-------------------------------------------------------+
    | GMT_Update_Option_       | Modify an option structure                            |
    +--------------------------+-------------------------------------------------------+
    | GMT_Write_Data_          | Export a data resource                                |
    +--------------------------+-------------------------------------------------------+

    One-line summaries of all the API functions and their purposes.
	
The GMT C Application Program Interface
=======================================

Initialize a new GMT session
----------------------------

Advanced programs may be calling more than one GMT session and thus
run several sessions, perhaps concurrently as different threads on
multi-core machines. We will now discuss these steps in more detail.
Throughout, we will introduce upper-case GMT C enum constants *in
lieu* of simple integer constants. These are considered part of the API
and are available for developers via the ``gmt_resources.h`` include file.

Most applications will need to initialize only a single GMT session.
This is true of all the standard GMT programs since they only call one
GMT module and then exit. Most user-developed GMT applications are
likely to only initialize one session even though they may call many
GMT modules. However, the GMT API supports any number of
simultaneous sessions should the programmer wish to take advantage of
it. This might be useful when you have access to several CPUs and want
to spread the computing load [5]_. In the following discussion we will
simplify our treatment to the use of a single session only.

To initiate the new GMT session we use

.. _GMT_Create_Session:

  ::

    void *GMT_Create_Session (const char *tag, unsigned int pad, unsigned int mode,
    	int (*print_func) (FILE *, const char *));

and you will typically call it like this:

  ::

    void *API = NULL;	/* Opaque pointer to GMT controls */
    API = GMT_Create_Session ("Session name", 2, 0, NULL);

where ``API`` is an opaque pointer to the hidden GMT API control
structure. You will need to pass this pointer to *all* subsequent
GMT API functions; this is how essential internal information is
passed around. The key task of this initialization is to
set up the GMT machinery and internal variables used for map
projections, plotting, i/o, etc. The initialization also allocates space
for internal structures used to keep track of data. The ``pad`` argument
specifies how many rows and columns should be used as padding for grids and
images so that boundary conditions can be applied. GMT uses 2 and we strongly
recommend that you use that value. In particular, if you choose 0 or 1 there may be certain
GMT modules that will be unable to do their work properly as they count on those
boundary rows and columns in the grids.  Note that this setting has no effect
on what is written to a grid file; the padding is an internal feature.
The ``mode`` argument is only used for external APIs that need
to communicate their special needs during the session creation.  This integer argument
is a sum of bit flags and the various bits control the following settings:

#. Bit 1 (1): If set, then GMT will not call the system exit function when a
   serious problem has been detected but instead will simply return control
   to the calling environment.  For instance, this is required by the GMT/MATLAB toolbox
   since calling exit would also exit MATLAB itself.  Unless your environment
   has this feature you should leave this bit alone.
#. Bit 2 (2): If set, then it means we are calling the GMT API from an external
   API, such as MATLAB, Octave, or Python.  Normal C/C++ programs should
   leave this bit alone.  Its effect is to enable two additional modules
   for reading and writing GMT resources from these environments (those modules
   would not make any sense in a Unix command-line environment).
#. Bit 3 (4): If set, then it means the external API uses a column-major format for
   matrices (e.g., MATLAB, Fortran).  If not set we default to row-major
   format (C/C++, Python, etc.).

Finally, the ``print_func`` argument is a pointer to a function that is used to print
messages from GMT via GMT_Message_ or GMT_Report_ from external environments that cannot use the
standard printf function (this is the case for the GMT/MATLAB toolbox, for instance).
For all other uses you should simply pass NULL for this argument.
Should something go wrong during the API initialization then ``API`` will be returned as ``NULL``.

Below is a bare-bones minimalistic GMT program hello.c that initializes and destroys
a GMT session:

.. _example-code2:

.. code-block:: c

  #include "gmt.h"
  int main (int argc, char *argv[]) {
  	void *API;	/* The API control structure */
  	/* Initialize the GMT session */
  	API = GMT_Create_Session ("test", 2U, 0, NULL);
	/* And now for something original: */
	GMT_Message (API, GMT_TIME_NONE, "hello, world\n");
  	/* Destroy the GMT session */
  	GMT_Destroy_Session (API);
  };

While not super-exiting, this code demonstrates the two essential API calls
required to initiate and later terminate a GMT session.  In between we do what
all basic programs are supposed to do: print "Hello, world".  The user is of course
allowed to do whatever custom processing before the GMT session is created
and can do all sorts of stuff after the GMT session is destroyed, as long as
no GMT functions or resources are accessed.  It may be convenient to isolate
the GMT-specific processing from the custom part of the program and only
maintain an active GMT session when needed.

Register input or output resources
----------------------------------

When using the standard GMT programs, it is common to specify input files on the
command line or via special program options (e.g.,
**-I**\ *intensity.nc*). The outputs of the programs are either written
to standard output (which you may redirect to files or pipes into other
programs) or to files specified by specific program options (e.g.,
**-G**\ *output.nc*). Alternatively, the GMT API allows you to specify
input (and output) to be associated with open file handles or virtual files.
We will examine this more closely below. Registering a
resource is a required step before attempting to import or export data
that *do not* come from files or standard input/output.

.. _sec-res_init:

Resource initialization
~~~~~~~~~~~~~~~~~~~~~~~

All GMT programs dealing with input or output files given on the
command line, and perhaps defaulting to the standard input or output
streams if no files are given, must call the i/o initializer function
GMT_Init_IO_ once for each direction required (i.e., input and output
separately). For input it determines how many input sources have already
been registered. If none has been registered then it scans the program
arguments for any filenames given on the command line and register these
input resources. Finally, if we still have found no input sources we
assign the standard input stream as the single input source. For output
it is similar: If no single destination has been registered we specify
the standard output stream as the output destination. Only one main
output destination is allowed to be active when a module writes data
(some modules also write additional output via program-specific
options). The prototype for this function is

.. _GMT_Init_IO:

  ::

    int GMT_Init_IO (void *API, unsigned int family, unsigned int geometry,
    	unsigned int direction, unsigned int mode, unsigned int n_args, void *args);

where :ref:`family <tbl-family>` specifies what kind of resource is to be registered,
:ref:`geometry <tbl-geometry>` specifies the geometry of the data, ``direction`` is either
``GMT_IN`` or ``GMT_OUT``, and ``mode`` is a bit flag that determines
what we do if no resources have been registered. The choices are

    **GMT_ADD_FILES_IF_NONE** (1) means "add command line (option)
    files if none have been registered already".

    **GMT_ADD_FILES_ALWAYS** (2) means "always add any command line files".

    **GMT_ADD_STDIO_IF_NONE** (4) means "add std\* if no other
    input/output have been specified".

    **GMT_ADD_DEFAULT** (6) means "always add any command line files first, and then
    add std\* if no other input/output were specified".

    **GMT_ADD_STDIO_ALWAYS** (8) means "always add std\* even if
    resources have been registered".

    **GMT_ADD_EXISTING** (16) means "only use already registered resources".

The standard behavior is ``GMT_ADD_DEFAULT`` (6). Next, ``n_args`` is 0
if ``args`` is the head of a linked list of options (further discussed
in :ref:`Prepare modules opts <sec-func>`); otherwise ``args`` is an array of ``n_args``
strings (i.e., the int argc, char \*argv[] model)

Many programs will register an export location where results of a GMT function (say, a filtered grid)
should be returned, but may then wish to use that variable as an *input* resource in a subsequent module
call. This is accomplished by re-registering the resource as an *input* source, thereby changing the
*direction* of the data set. The function returns 1 if there is an error; otherwise it returns 0. |ex_resource_init|

Resource registration
~~~~~~~~~~~~~~~~~~~~~

Should your program need to add additional sources (or a destination) to the list of items
to be considered you will need to register them manually.  This is considered a low-level
activity and most applications are unlikely to have to resort to this step.  We document
it here in case your situation calls for such action.
Registration involves a direct or indirect call to

.. _GMT_Register_IO:

  ::

    int GMT_Register_IO (void *API, unsigned int family, unsigned int method,
    	unsigned int geometry, unsigned int direction, double wesn[], void *ptr);

where :ref:`family <tbl-family>` specifies what kind of resource is to be registered,
:ref:`method <tbl-methods>` specifies
how we to access this resource (see Table :ref:`methods <tbl-methods>` for recognized
methods), :ref:`geometry <tbl-geometry>` specifies the geometry of the data, ``ptr`` is the address of the
pointer to the named resource. If ``direction`` is ``GMT_OUT`` and the
``method`` is not related to a file (filename, stream, or handle), then
``ptr`` must be NULL. Note there are some limitations on when you may pass a file pointer
as the method.  Many grid file formats cannot be read via a stream (e.g., netCDF files) so in
those situations you cannot pass a file pointer [and GMT_Register_IO would have no way of knowing
this].  For grid (and image)
resources you may request to obtain a subset via the :ref:`wesn <tbl-wesn>` array; otherwise, pass NULL
(or an array with at least 4 items all set to 0) to obtain the
entire grid (or image). The ``direction`` indicates input or output and
is either ``GMT_IN`` or ``GMT_OUT``. Finally, the function returns a
unique resource ID, or ``GMT_NOTSET`` if there was an error.


.. _tbl-family:

    +-------------------+-----------------------------+
    | family            | source points to            |
    +===================+=============================+
    | GMT_IS_DATASET    | A [multi-segment] data file |
    +-------------------+-----------------------------+
    | GMT_IS_GRID       | A grid file                 |
    +-------------------+-----------------------------+
    | GMT_IS_IMAGE      | An image                    |
    +-------------------+-----------------------------+
    | GMT_IS_PALETTE    | A color palette table [CPT] |
    +-------------------+-----------------------------+
    | GMT_IS_POSTSCRIPT | A GMT PostScript object     |
    +-------------------+-----------------------------+
    | GMT_IS_TEXTSET    | A [multi-segment] text file |
    +-------------------+-----------------------------+
    | GMT_IS_MATRIX     | A custom user data matrix   |
    +-------------------+-----------------------------+
    | GMT_IS_VECTOR     | A custom user data vector   |
    +-------------------+-----------------------------+

    GMT constants used to specify a data family.

.. _tbl-methods:

    +------------------+------------------------------------------------+
    | method           | how to read/write data                         |
    +==================+================================================+
    | GMT_IS_FILE      | Pointer to name of a file                      |
    +------------------+------------------------------------------------+
    | GMT_IS_STREAM    | Pointer to open stream (or process)            |
    +------------------+------------------------------------------------+
    | GMT_IS_FDESC     | Pointer to integer file descriptor             |
    +------------------+------------------------------------------------+
    | GMT_IS_DUPLICATE | Pointer to memory we may *duplicate* data from |
    +------------------+------------------------------------------------+
    | GMT_IS_REFERENCE | Pointer to memory we may *reference* data from |
    +------------------+------------------------------------------------+

    GMT constants used to specify how data will be read or written.

.. _tbl-geometry:

    +----------------+-----------------------------------------+
    | geometry       |  description                            |
    +================+=========================================+
    | GMT_IS_NONE    | Not a geographic feature                |
    +----------------+-----------------------------------------+
    | GMT_IS_POINT   | Multi-dimensional point data            |
    +----------------+-----------------------------------------+
    | GMT_IS_LINE    | Geographic or Cartesian line segments   |
    +----------------+-----------------------------------------+
    | GMT_IS_POLYGON | Geographic or Cartesian closed polygons |
    +----------------+-----------------------------------------+
    | GMT_IS_PLP     | Either points, lines, or polygons       |
    +----------------+-----------------------------------------+
    | GMT_IS_SURFACE | 2-D gridded surface                     |
    +----------------+-----------------------------------------+

    GMT constants used to specify the geometry of the data object.

.. _tbl-wesn:

    +---------+----------------------------------------------+
    | index   |  description                                 |
    +=========+==============================================+
    | GMT_XLO | x_min (west) boundary of grid subset         |
    +---------+----------------------------------------------+
    | GMT_XHI | x_max (east) boundary of grid subset         |
    +---------+----------------------------------------------+
    | GMT_YLO | y_min (south) boundary of grid subset        |
    +---------+----------------------------------------------+
    | GMT_YHI | y_max (north) boundary of grid subset        |
    +---------+----------------------------------------------+
    | GMT_ZLO | z_min (bottom) boundary of 3-D matrix subset |
    +---------+----------------------------------------------+
    | GMT_ZHI | z_max (top) boundary of 3-D matrix subset    |
    +---------+----------------------------------------------+

    GMT constants used for domain array indexing.

.. _sec-create:

Create empty resources
----------------------

If your application needs to build and populate GMT resources in ways
that do not depend on external resources (files, memory locations,
etc.), or you have data read in separately and you wish to build a
GMT resource from scratch, then you can obtain an empty object by calling

.. _GMT_Create_Data:

  ::

    void *GMT_Create_Data (void *API, unsigned int family, unsigned int geometry,
         unsigned int mode, uint64_t par[], double *wesn, double *inc,
         unsigned int registration, int pad, void *data)

which returns a pointer to the allocated resource. Pass a valid :ref:`family <tbl-family>` selection.
Also pass a compatible :ref:`geometry <tbl-geometry>`. Depending on the family and your particular way of
representing dimensions you may pass the additional parameters in one of
two ways:

#. Actual integer dimensions of items needed (which depends on the ``family``).

#. Physical distances and increments of each dimension.

For the first case you should pass both ``wesn`` and ``inc`` as NULL (or as arrays with elements all set to 0),
and pass the ``par`` array with contents as indicated below:

  **GMT_IS_GRID**.
    An empty :ref:`GMT_GRID <struct-grid>` structure with a header is allocated; the data
    array is NULL.  Use ``registration`` to choose either gridline (``GMT_GRID_PIXEL_REG``) or pixel
    (``GMT_GRID_NODE_REG``) registration.  The domain can be prescribed on one of two ways:
    (1) The ``par`` argument is NULL. Then ``wesn`` and ``inc`` can also be NULL but only if the common GMT options
    **-R** and **-I** have been set because they are required to get the necessary info. If they
    were not set, then ``wesn`` and ``inc`` must in fact be transmitted.  If ``wesn`` and ``inc``
    are set (directly or indirectly) then ``par`` is ignored, even if not NULL.
    (2) The ``par`` argument is not NULL but both ``wesn`` and ``inc`` are NULL.
    Now, ``par[0]`` must have the number of columns and ``par[1]`` must have the number of rows in the grid.  Internally,
    ``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``
    and we also allocate the grids's *x* and *y* coordinate vectors.

  **GMT_IS_IMAGE**.
    Same procedure as for **GMT_IS_GRID** but we return an empty :ref:`GMT_IMAGE <struct-image>` object.  In either
    way of specification you may use ``par[2]`` to pass the number of image bands [1].

  **GMT_IS_DATASET**.
    We allocate an empty :ref:`GMT_DATASET <struct-dataset>` structure consisting of ``par[0]`` tables,
    each with ``par[1]`` segments, each with ``par[2]`` rows, all with ``par[3]`` columns.
    The ``wesn``, ``inc``, and ``registration`` argument are ignored.  The ``data`` argument should be NULL.

  **GMT_IS_TEXTSET**.
    We allocate an empty :ref:`GMT_TEXTSET <struct-textset>` structure consisting of ``par[0]`` tables,
    each with ``par[1]`` segments, all with ``par[2]`` text records (rows).
    The ``wesn``, ``inc``, and ``registration`` arguments are ignored and should be NULL/0.  The ``data`` argument should be NULL.

  **GMT_IS_PALETTE**.
    We allocate an empty :ref:`GMT_PALETTE <struct-palette>` structure with ``par[0]`` palette entries.
    The ``wesn``, ``inc``, and ``registration`` arguments are ignored and should be NULL/0.  The ``data`` argument should be NULL.

  **GMT_IS_POSTSCRIPT**.
    We allocate an empty :ref:`GMT_POSTSCRIPT <struct-postscript>` structure with a text buffer of length ``par[0]``.
    Give ``par[0]`` = 0 if the PostScript string is allocated or obtained by other means.
    The ``wesn``, ``inc``, and ``registration`` arguments are ignored and should be NULL/0.  The ``data`` argument should be NULL.

  **GMT_IS_VECTOR**.
    We allocate an empty :ref:`GMT_VECTOR <struct-vector>` structure with ``par[0]`` column entries.
    The number of rows can be specified in one of two ways: (1) Set the number of rows via ``par[1]``. Then,
    ``wesn``, ``inc``, and ``registration`` arguments are ignored.
    (2) Specify ``wesn``, ``inc``, and ``registration`` and the number of rows will be computed form these
    parameters instead.  The ``data`` argument should be NULL.  If you have custom vectors you wish to use then pass ``par[1]`` = 0
    to avoid any allocation and use GMT_Put_Vector_ to hook up your vectors.

  **GMT_IS_MATRIX**.
    We allocate an empty :ref:`GMT_MATRIX <struct-matrix>` structure. The domain can be prescribed on one of two ways:
    (1) Here, ``par[0]`` is the number of columns while ``par[1]`` has the number of rows.  Also,
    ``par[2]`` indicates the number of layers for a 3-D matrix, or pass 0, 1, or NULL for a 2-D matrix.
    (2) Pass ``wesn``, ``inc``, ``registration`` and we compute the dimensions of the matrix.
    The ``data`` argument should be NULL.  As for vectors, give dimensions as 0 and hook your custom matrix in
    via a call to GMT_Put_Matrix_.

For grids and images you may pass ``pad`` to set the padding, or -1 to
accept the prevailing GMT default. The ``mode`` determines what is actually
allocated when you have chosen grids or images. As for GMT_Read_Data_
you can pass ``GMT_GRID_ALL`` to initialize the header *and* allocate
space for the array; here ``data`` must be NULL. Alternatively, you can pass
``GMT_GRID_HEADER_ONLY`` to just initialize the grid or image header,
and later call GMT_Create_Data a second time, now passing ``GMT_GRID_DATA_ONLY``, to allocate
space for the array. In that second call you pass the pointer returned
by the first call as ``data`` and specify the family; all other
arguments should be NULL or 0. Normally, resources created by this
function are considered to be input (i.e., have a direction that is ``GMT_IN``).
The exception to this is for vectors and matrices which will have a direction
set to ``GMT_OUT`` when the dimensions specified are not complete (i.e., the
row dimension of vectors is 0 and both dimensions are zero for matrices).
The function returns a pointer to the
data container. In case of an error we return a NULL pointer and pass an
error code via ``API->error``.

Hooking user arrays to objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you have custom column vector or matrices and you want them to be used as 
input to GMT modules, you will need to create a :ref:`GMT_VECTOR <struct-vector>` or :ref:`GMT_MATRIX <struct-matrix>` container
and hook your items to them.  Likewise, if you want to receive the output of GMT modules
into user arrays or matrices then you will need to access those data.
The following utility functions are used for these tasks:

.. _GMT_Put_Matrix:

  ::

    int GMT_Put_Matrix (struct GMT_MATRIX *M, unsigned int type, void *matrix);

where ``M`` is a :ref:`GMT_MATRIX <struct-matrix>` created by GMT_Create_Data_, the ``type`` is one of the
recognized data :ref:`types <tbl-types>`, and ``matrix`` is your custom matrix.
To extract a custom matrix from an output :ref:`GMT_MATRIX <struct-matrix>` you can use

.. _GMT_Get_Matrix:

  ::

    void *GMT_Get_Matrix (void *API, struct GMT_MATRIX *M);

which simply returns a pointer to the right union pointer.
For vectors the same principles apply:

.. _GMT_Put_Vector:

  ::

    int GMT_Put_Vector (void *API, struct GMT_VECTOR *V, unsigned int col,
	unsigned int type, void *vector);

where ``V`` is the :ref:`GMT_VECTOR <struct-vector>` created by GMT_Create_Data_, ``col`` is the vector
column in question, ``type`` is one of the
recognized data :ref:`types <tbl-types>` used for this vector, and ``vector`` is
a pointer to this custom vector.
To extract a custom vector from an output :ref:`GMT_VECTOR <struct-vector>` you can use

.. _GMT_Get_Vector:

  ::

    void *GMT_Get_Vector (void *API, struct GMT_VECTOR *V, unsigned int col);

where ``col`` is the vector number you wish to obtain a pointer to.

Manually add segments
~~~~~~~~~~~~~~~~~~~~~

If you do not know the number of rows in the segments or you expect different segments to have different
lengths then you should set the row dimension to zero in GMT_Create_Data and add the segments
manually with ``GMT_Alloc_Segment``, which allocates a new :ref:`GMT_DATASET <struct-dataset>` or
:ref:`GMT_TEXTSET <struct-textset>` segment for such multi-segment tables.

.. _GMT_Alloc_Segment:

  ::

    void *GMT_Alloc_Segment (void *API, unsigned int family,
    	uint64_t n_rows, uint64_t n_columns, char *header, void *S);

where ``header`` is the segment's desired header (or NULL) and `family` selects which
kind of resource is desired , which in this case should either be ``GMT_IS_DATASET``
or ``GMT_IS_TEXTSET``.  If ``S`` is not NULL then we simply reallocate the lengths
of the segment; otherwise a new segment is first allocated.

For a :ref:`GMT_DATASET <struct-dataset>` there is also the option of controlling the allocation of the segment
array by setting n_rows = 0.  This would allow external arrays (double-precision only) to connected to
the S->data[col] arrays and not be freed by GMT's garbage collector.

Duplicate resources
-------------------

Often you have read or created a data resource and then need an
identical copy, presumably to make modifications to. Or, you want a copy
with the same dimensions and allocated memory, except data values should
not be duplicated. Alternatively, perhaps you just want to duplicate the
header and skip the allocation and duplication of the data entirely. These tasks
are addressed by

.. _GMT_Duplicate_Data:

  ::

    void *GMT_Duplicate_Data (void *API, unsigned int family, unsigned int mode,
    	void *data);

which returns a pointer to the allocated resource. Specify which
:ref:`family <tbl-family>` and select ``mode`` from ``GMT_DUPLICATE_DATA``,
``GMT_DUPLICATE_ALLOC``, and ``GMT_DUPLICATE_NONE``, as discussed above
(also see ``mode`` discussion above). For :ref:`GMT_DATASET <struct-dataset>` and :ref:`GMT_TEXTSET <struct-textset>` you can
add modifiers ``GMT_ALLOC_VERTICAL`` or ``GMT_ALLOC_HORIZONTAL`` to the ``mode`` if you
wish to put all the data into a single long table or to paste all tables
side-by-side, respectively (thus getting one wide table instead).
Additional note for :ref:`GMT_DATASET <struct-dataset>`: Normally we allocate the output given the
corresponding input dimensions. You can override these by specifying your
alternative dimensions in the input dataset's variable ``dim[]``.
The ``data`` is a pointer to the resource you wish to duplicate. In case
of an error we return a NULL pointer and pass an error code via
``API->error``.

Convert between resource types
------------------------------

Having a resource in memory you may want to convert it to an alternative
representation.  For instance, you may have a :ref:`GMT_DATASET <struct-dataset>` in memory but
for an application you need the equivalent information in :ref:`GMT_TEXTSET <struct-textset>` format.
Or, you have read a :ref:`GMT_DATASET <struct-dataset>` but need to strip the information from the
data into a VECTOR format, dropping all the segment header information, so
that your custom algorithm or other non-GMT functions can be used on the data.
In this case you will use

.. _GMT_Convert_Data:

  ::

    void *GMT_Convert_Data (void *API, void *In, unsigned int family_in,
		void *Out, unsigned int family_out, unsigned int flag[]);

which returns a pointer to the converted resource. Specify the neeeded
:ref:`family <tbl-family>` for both the input and output resources and set the
(up to) three flags passed via the ``flag`` array.  The first ``flag[0]``
determines how table headers and segment headers should be handled.
By default (``flag[0]`` = 0) they are preserved (to the extent possible).
E.g., converting a :ref:`GMT_DATASET <struct-dataset>` to MATRIX always means table headers are
skipped whereas segment headers are converted to NaN-records. Other
values for this flag is 1 (Table headers are not copied, segment headers are preserved),
2 (Headers are preserved, segment headers are reset to blank), or
3 (All headers headers are eliminated).  Note that this flag only
affects duplication of headers.  If the new object is written to file at
a later stage then it is up to the GMT default setting if headers are written
to file or not. The second ``flag[1]`` controls
how many columns to expect when converting :ref:`GMT_TEXTSET <struct-textset>` only.  If 0 then
we try to determine the number of columns from the first text record.
If ``family_in`` is not ``GMT_IS_TEXTSET`` then flag[1] is ignored.
The third ``flag[2]`` controls restructuring of tables and segments within
a set.  For ``flag[2]`` = 0 we retain the original layout.  Other selections
are ``GMT_WRITE_TABLE_SEGMENT`` (combine all segments into a *single* segment in a *single* table),
``GMT_WRITE_TABLE`` (collect all segments into a *single* table), and ``GMT_WRITE_SEGMENT``
(combine segments into *one* segment per table).
Many family combinations are simply not allowed, such as grid to color palette, dataset to image,
etc.

Import Data Sets
----------------

If your program needs to import any of the six recognized data types
(data table, grid, image, CPT, PostScript, or text table) you will use
the GMT_Read_Data_ or GMT_Read_VirtualFile_ functions. The former
is typically used when reading from files, streams (e.g., ``stdin``), or
an open file handle, while the latter is only used to read from memory.
Because of the similarities of these six
import functions we use an generic form that covers all of them.

All input functions takes a parameter called ``mode``. The ``mode``
parameter generally has different meanings for the different data types
and will be discussed below. However, one bit setting is common to all
types: By default, you are only allowed to read a data source once; the
source is then flagged as having been read and subsequent attempts to
read from the same source will result in a warning and no reading takes
place. In the unlikely event you need to re-read a source you can
override this default behavior by adding ``GMT_IO_RESET`` to your ``mode``
parameter. Note that this override does not apply to sources that are
streams or file handles, as it may not be possible to re-read their
contents.


Import from a file, stream, or handle
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To read an entire resource from a file, stream, or file handle, use

.. _GMT_Read_Data:

  ::

    void *GMT_Read_Data (void *API, unsigned int family, unsigned int method,
    	unsigned int geometry, unsigned int mode, double wesn[], const char *input, void *ptr);

* :ref:`API <GMT_Create_Session>`
* :ref:`family <tbl-family>`
* :ref:`method <tbl-methods>`
* :ref:`geometry <tbl-geometry>`
* mode -- *see below*
* :ref:`wesn <tbl-wesn>`
* input -- a pointer to char holding the file name to read, or NULL if ``stdin``
* ptr -- NULL or the pointer returned by this function after a first call (when reading grids in two steps)
* Return: Pointer to data container, or NULL if there were errors (passed back via API->error)


where ``ptr`` is NULL except when reading grids in two steps (i.e.,
first get a grid structure with a header, then read the data). Most of
these arguments have been discussed earlier. This function can be called
in three different situations:

#. If you have a single source (filename, stream pointer, etc.) you can
   call GMT_Read_Data_ directly; there is no need to first register
   the source with GMT_Register_IO_ or gather the sources with
   GMT_Init_IO_. Furthermore, for :ref:`GMT_DATASET <struct-dataset>` and :ref:`GMT_TEXTSET <struct-textset>` you can also
   specify a filename that contains UNIX wildcards (e.g., "all_*_[ab]?.txt")
   and these will all be read to produce a single multi-table :ref:`GMT_DATASET <struct-dataset>` or
   :ref:`GMT_TEXTSET <struct-textset>` (for other datatypes, see GMT_Read_Group_ instead).

#. If you want to specify ``stdin`` as source then pass ``input`` as NULL.

#. If you already registered all desired sources with GMT_Init_IO_
   then you indicate this choice by passing the invalid ``geometry`` = 0.

Space will be allocated to hold the results, as needed, and a pointer to
the object is returned. If there are errors we simply return NULL and
report the error. The ``mode`` parameter has different meanings for
different data types.

**Color palette table**.
    ``mode`` contains bit-flags that control how the CPT's back-,
    fore-, and NaN-colors should be initialized. Select 0 to use the
    CPT resource's back-, fore-, and NaN-colors, 2 to replace these with the current
    GMT default values, or 4 to replace them with the color table's
    entries for highest and lowest value.

**Data table**.
    ``mode`` is currently not used.

**Text table**.
    ``mode`` is currently not used.

**GMT grid** or **image**.
    Here, ``mode`` determines how we read the grid: To read the entire
    grid and its header, pass ``GMT_GRID_ALL``. However, if you may need to
    extract a sub-region you must first read the header by passing
    ``GMT_GRID_HEADER_ONLY``, then examine the header structure range
    attributes, specify a subset via the array ``wesn``, and
    finally call GMT_Read_Data_ a second time, now with ``mode`` =
    ``GMT_GRID_DATA_ONLY``, passing your ``wesn`` array and the grid
    structure returned from the first call as ``ptr``. In the event your
    data array should be allocated to hold both the real and imaginary
    parts of a complex data set you must add either
    ``GMT_GRID_IS_COMPLEX_REAL`` or ``GMT_GRID_IS_COMPLEX_IMAG`` to
    ``mode`` so as to allow for the extra memory needed and to stride
    the complex value-pairs correctly. If your grid is huge and you must read
    it row-by-row, set ``mode`` to ``GMT_GRID_HEADER_ONLY`` \|
    ``GMT_GRID_ROW_BY_ROW``. You can then access the grid row-by-row
    using GMT_Get_Row_. By default, the rows will be automatically
    processed in sequential order. To completely specify which row to be read, pass
    ``GMT_GRID_ROW_BY_ROW_MANUAL`` instead.
    Finally, as an option you may add ``GMT_GRID_XY`` to the mode and we also
    allocate the *x* and *y* coordinate vectors for the grid or image.

**PostScript**.
    ``mode`` is currently not used.

If you need to read the same resource more than once you should add the
bit flag ``GMT_IO_RESET`` to the given ``mode``.

Import a group of data sets
~~~~~~~~~~~~~~~~~~~~~~~~~~~

To read a group of resources, you may instead use

.. _GMT_Read_Group:

  ::

    void *GMT_Read_Group (void *API, unsigned int family, unsigned int method,
    	unsigned int geometry, unsigned int mode, double wesn[],
    	void *input, unsigned int *n_items, void *ptr);

* :ref:`API <GMT_Create_Session>`
* :ref:`family <tbl-family>`
* :ref:`method <tbl-methods>`
* :ref:`geometry <tbl-geometry>`
* mode -- *see below*
* :ref:`wesn <tbl-wesn>`
* input -- Contents depends on the value of *n_items*.  If it is zero then we expect
  a pointer to char holding UNIX wildcard file name(s) to read, otherwise we expect
  a pointer to an array of character strings (*n_items* in total) with names of all
  the files to read.  If *n_items* is NULL then we assume 0 but cannot return the number
  found.
* ptr -- NULL or the pointer returned by this function after a first call (applies when reading grids or images in two steps)
* Return: Pointer to array of data container, or NULL if there were errors (passed back via API->error)


where ``ptr`` is NULL except when reading grids in two steps (i.e.,
first get a grid structures with a header, then read the data arrays). Most of
these arguments have been discussed earlier. It is useful when you need to read
a series of files (e.g., from a list with filenames) or want to specify the items
to read using a UNIX wildcard specification.  Note: If used with :ref:`GMT_DATASET <struct-dataset>`
or :ref:`GMT_TEXTSET <struct-textset>`
then you will receive an array of structures as well.  Typically, many data/text files
are read into separate tables that all form part of a single SET (this is what GMT_Read_Data_ does),
but if GMT_Read_Group_ is used on the same arguments then an array of one-table sets will
be returned instead.  The purpose of your application will dictate which form is more convenient.

Open a virtual file (memory location)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you have read in or otherwise obtained a data object in memory and you
now wish for it to serve as input to a GMT module, you will have to associate
that object with a "Virtual File".  This step assigns a special filename to the
memory location and you can then pass this filename to any module that
needs to read that data.  It is similar for writing, and you can pass
NULL as the object to have GMT automatically allocate the output resource.
The full syntax is

.. _GMT_Open_VirtualFile:

  ::

    void *GMT_Open_VirtualFile (void *API, unsigned int family, unsigned int geometry,
		unsigned int direction, void *data, char *filename);

Here, ``data`` is the pointer to your memory object.  The function returns the
desired filename via ``filename``.  This string must be at least ``GMT_STR16`` bytes (16).
The other arguments have been discussed earlier.  Simply pass this filename in
the calling sequence to the module you want to use to indicate which file should
be used for reading or writing.

Import from a virtual file
~~~~~~~~~~~~~~~~~~~~~~~~~~

Once the module completes it will have written its output to the virtual file
you initialized with GMT_Open_VirtualFile_.  To use the actual
data you will need to "read" it into your program.  Of course, the data are already
in memory but to access it you need to use GMT_Read_VirtualFile_, which expects
the output filename you obtained from GMT_Open_VirtualFile_.  The syntax is

.. _GMT_Read_VirtualFile:

  ::

    void *GMT_Read_VirtualFile (void *API, char *filename);

The function requires the output filename via ``filename`` and then returns
the data object, similar to what GMT_Read_Data_ does.

Reset a virtual file for reuse
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Should you need to read a virtual file again then you must first reset
it to its original state with GMT_Init_VirtualFile_.  The syntax is

.. _GMT_Init_VirtualFile:

  ::

    int GMT_Init_VirtualFile (void *API, unsigned int mode, const char *filename);

The function requires the virtual file's ``filename`` and then resets the
internal counters (e.g., record numbers and other book-keeping parameters).
The ``mode`` is presently not used.

Close a virtual file
~~~~~~~~~~~~~~~~~~~~

Once you have finished using a virtual file you need to close it.
This will reset its internal settings back to what it was before you
used it as a virtual file.  The syntax is


.. _GMT_Close_VirtualFile:

  ::

    int GMT_Close_VirtualFile (void *API, char *filename);

where ``filename`` is the name of the virtual file.


Record-by-record input
----------------------

In the case of data and text tables you have the option of selecting
record-by-record reading or writing.  As a general rule, your program
development simplifies if you can read entire resources into memory with 
GMT_Read_Data_ or GMT_Read_VirtualFile_.  However, if this leads to
unacceptable memory usage or if the program logic is particularly simple,
you may obtain one data record at the time via GMT_Get_Record_ and write
one at the time with GMT_Put_Record_.  For row-by-row i/o for grids there
is the corresponding function GMT_Get_Row_. There are additional overhead involved
in setting up record-by-record processing, which is the topic of this section.

Enable Data Import
~~~~~~~~~~~~~~~~~~

Once all input resources have been registered, we signal the API that we
are done with the registration phase and are ready to start the actual
data import. This step is only required when reading one record at the
time. We initialize record-by-record reading by calling
GMT_Begin_IO_. This function enables data and text
record-by-record reading and prepares the registered sources for the
upcoming import. The prototype is

.. _GMT_Begin_IO:

  ::

    int GMT_Begin_IO (void *API, unsigned int family, unsigned int direction,
    	unsigned int header);

where :ref:`family <tbl-family>` specifies the resource type to be read or written
(only ``GMT_IS_DATASET`` and ``GMT_IS_TEXTSET`` are
available for record-by-record handling). The ``direction`` is either
``GMT_IN`` or ``GMT_OUT``, so for import we obviously use ``GMT_IN``. The
function determines the first input source and sets up procedures for
skipping to the next input source in a virtual data set. The
GMT_Get_Record_ function will not be able to read any data before
GMT_Begin_IO_ has been called. As you might guess, there is a
companion GMT_End_IO_ function that completes, then disables
record-by-record data access. You can use these several times to switch
modes between registering data resources, doing the importing/exporting,
and disabling further data access, perhaps to do more registration. We
will discuss GMT_End_IO_ once we are done with the data import. The final
``header`` argument determines if the common header-block should be
written during initialization; choose between ``GMT_HEADER_ON`` and
``GMT_HEADER_OFF``. The function returns 1 if there is an
error; otherwise it returns 0.

Importing a data record
~~~~~~~~~~~~~~~~~~~~~~~

If your program will read data table records one-by-one you must first
enable this input mechanism with GMT_Begin_IO_ and then read the
records within a loop, repeatedly using

.. _GMT_Get_Record:

  ::

    void *GMT_Get_Record (void *API, unsigned int mode, int *nfields);

where the returned value is either a pointer to a double array with the
current row values or to a character string with the current row,
depending on ``mode``. In either case these pointers point to ephemeral memory
internal to GMT and should be considered read-only. When we reach
end-of-file, encounter conversion problems, read header comments, or
identify segment headers we instead return a NULL pointer. The ``nfields``
integer pointer will return the number of fields returned; pass NULL if your
program should ignore this information.

Normally (i.e., ``mode`` = ``GMT_READ_DOUBLE``), we return a pointer to
a double array. To read text records, supply instead ``mode`` =
``GMT_READ_TEXT`` and we will return a pointer to the text
record. However, if you have input records that mixes organized
floating-point columns with text items you could pass ``mode`` =
``GMT_READ_MIXED``. Then, GMT will attempt to extract the
floating-point values from as many columns as needed; you can still access the original record string, as
discussed below. Finally, if your application needs to be notified when
GMT closes one file and opens the next, add ``GMT_FILE_BREAK`` to
``mode`` and check for the status code ``GMT_IO_NEXT_FILE`` (by default,
we treat the concatenation of many input files as a single virtual
file). Using GMT_Get_Record_ requires you to first initialize the
source(s) with GMT_Init_IO_. For certain records, GMT_Get_Record_
will return NULL and sets status codes that your program will need to
examine to take appropriate response. Table :ref:`IO-status <tbl-iostatus>` lists the
various status codes you can check for, using the ``GMT_Status_IO`` function (see
next section).

Examining record status
~~~~~~~~~~~~~~~~~~~~~~~

Programs that read record-by-record must be aware of what the current
record represents. Given the presence of headers, data gaps, NaN-record,
etc., the developer may want to check the status after reading the current
record. The internal i/o status mode can be interrogated with the function

.. _GMT_Status_IO:

  ::

    int GMT_Status_IO (void *API, unsigned int mode);

which returns 0 (false) or 1 (true) if the current status is reflected
by the specified ``mode``. There are 11 different modes available to
programmers; for a list see Table :ref:`IO-status <tbl-iostatus>` For an example of how
these may be used, see the test program ``testgmtio.c``. Developers who plan to import
data on a record-by-record basis may also consult the source code of,
say, :doc:`blockmean` or :doc:`pstext`, to see examples of working code.

.. _tbl-iostatus:

    +------------------------+--------------------------------------------------------+
    | mode                   | description and return value                           |
    +========================+========================================================+
    | GMT_IO_DATA_RECORD     | 1 if we read a data record                             |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_TABLE_HEADER    | 1 if we read a table header                            |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_SEGMENT_HEADER  | 1 if we read a segment header                          |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_ANY_HEADER      | 1 if we read either header record                      |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_MISMATCH        | 1 if we read incorrect number of columns               |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_EOF             | 1 if we reached the end of the file (EOF)              |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_NAN             | 1 if we only read NaNs                                 |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_GAP             | 1 if this record implies a data gap                    |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_NEW_SEGMENT     | 1 if we enter a new segment                            |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_LINE_BREAK      | 1 if we encountered a segment header, EOF, NaNs or gap |
    +------------------------+--------------------------------------------------------+
    | GMT_IO_NEXT_FILE       | 1 if we finished one file but not the last             |
    +------------------------+--------------------------------------------------------+

    The various modes used to test the status of the record-by-record machinery. 

Importing a grid row
~~~~~~~~~~~~~~~~~~~~

If your program must read a grid file row-by-row you must first enable
row-by-row reading with GMT_Read_Data_ and then use the
GMT_Get_Row_ function in a loop; the prototype is

.. _GMT_Get_Row:

  ::

    int GMT_Get_Row (void *API, int row_no, struct GMT_GRID *G, float *row);

where ``row`` is a pointer to a pre-allocated single-precision array to receive the
current row, ``G`` is the grid in question, and ``row_no`` is the number
of the current row to be read. Note this value is only considered if the
row-by-row mode was initialized with ``GMT_GRID_ROW_BY_ROW_MANUAL``.
The user must allocate enough space to hold the entire row in memory.

Disable Data Import
~~~~~~~~~~~~~~~~~~~

Once the record-by-record input processing has completed we disable
further input to prevent accidental reading from occurring (due to poor
program structure, bugs, etc.). We do so by calling GMT_End_IO_. This
function disables further record-by-record data import; its prototype is

.. _GMT_End_IO:

  ::

    int GMT_End_IO (void *API, unsigned int direction, unsigned int mode);

and we specify ``direction`` = ``GMT_IN``. At the moment, ``mode`` is not
used. This call will also reallocate any arrays obtained into their
proper lengths. The function returns 1 if there is an error
(whose code is passed back with ``API->error``), otherwise it returns 0 (``GMT_NOERROR``).

.. _sec-manipulate:

Manipulate data
---------------

Once you have created and allocated empty resources, or read in
resources from the outside, you may wish to manipulate their contents.
This section discusses how to set up loops and access the important
variables for each of the supported families. For grids and images it may in addition
be required to determine what the coordinates are at each node point. This information
can be obtained via arrays of coordinates for each dimension, obtained by

.. _GMT_Get_Coord:

  ::

    double *GMT_Get_Coord (void *API, unsigned int family, unsigned int dim,
    	void *data);

where :ref:`family <tbl-family>` must be ``GMT_IS_GRID`` or ``GMT_IS_DATASET``, ``dim`` is either
``GMT_IS_X`` or ``GMT_IS_Y``, and ``data`` is the grid or image pointer.  This
function will be used below in our example on grid manipulation.

Another aspect of dealing with grids and images is to convert a row and column
2-D reference to our 1-D array index.  Because of grid and image boundary padding
the relationship is not straightforward, hence we supply

.. _GMT_Get_Index:

  ::

    int64_t GMT_Get_Index (struct GMT_GRID_HEADER *header, int row, int col);

where the ``header`` is the header of either a grid or image, and ``row`` and
``col`` is the 2-D position in the grid or image.  We return the 1-D array
position; again this function is used below in our example.  Likewise, for images
with many layers we also define

.. _GMT_Get_Pixel:

  ::

    int64_t GMT_Get_Pixel (struct GMT_GRID_HEADER *header, int row,
    	int col, int layer);

where the ``header`` is the header of an image, and ``row``, ``col`` and
``layer`` (= 1 for grids) is the position in the grid or image.

Manipulate grids
~~~~~~~~~~~~~~~~

Most applications wishing to manipulate grids will want to loop over all
the nodes, typically in a manner organized by rows and columns. In doing
so, the coordinates at each node may also be required for a calculation.
Below is a snippet of code that shows how to do visit all nodes in a
grid and assign each node the product x \* y:

  ::

    int row, col, node;
    double *x_coord = NULL, *y_coord = NULL;
    /*... create a grid G or read one ... */
    x_coord = GMT_Get_Coord (API, GMT_IS_GRID, GMT_X, G);
    y_coord = GMT_Get_Coord (API, GMT_IS_GRID, GMT_Y, G);
    for (row = 0; row < G->header->n_rows) {
        for (col = 0; col < G->header->n_columns; col++) {
            node = GMT_Get_Index (G->header, row, col);
            G->data[node] = x_coord[col] * y_coord[row];
        }
    }

Note the use of :GMT_Get_Index_ to get the grid node number associated
with the ``row`` and ``col`` we are visiting. Because GMT grids have
padding (for boundary conditions) the relationship between rows,
columns, and node indices is more complicated and hence we hide that
complexity in GMT_Get_Index_. Note that for trivial procedures such
setting all grid nodes to a constant (e.g., -9999.0) where the row and
column does not enter you can instead do a single loop:

  ::

    int node;
    /*... create a grid G or read one ... */
    for (node = 0; node < G->header->size) G->data[node] = -9999.0;

Note we must use ``G->header->size`` (size of allocated array) and not
``G->header->nm`` (number of nodes in grid) since the latter is smaller
due to the padding and a single loop like the above treats the pad as
part of the "inside" grid. Replacing ``size`` by ``nm`` would be a bug.

Manipulate data tables
~~~~~~~~~~~~~~~~~~~~~~

Another common application is to process the records in a data table.
Because GMT considers the :ref:`GMT_DATASET <struct-dataset>` resources to contain one or more
tables, each of which may contain one or more segments, all of which may
contain one or more columns, you will need to have multiple nested loops to
visit all entries. The following code snippet will visit all data
records and add 1 to all columns beyond the first two (x and y):

  ::

    uint64_t tbl, seg, row, col;
    struct GMT_DATATABLE *T = NULL;
    struct GMT_DATASEGMENT *S = NULL;

    /* ... create a dataset D or read one ... */
    for (tbl = 0; tbl < D->n_tables; tbl++) {       /* For each table */
      T = D->table[tbl];       /* Convenient shorthand for current table */
      for (seg = 0; seg < T->n_segments; seg++) {   /* For all segments */
        S = T->segment[seg];   /* Convenient shorthand for current segment */
        for (row = 0; row < S->n_rows; row++) {	/* For all rows in segment */
          for (col = 2; col < T->n_columns; col++) {	/* For all cols > 1 */
            S->data[col][row] += 1.0;	/* Just add one */
          }
        }
      }
    }

Manipulate text tables
~~~~~~~~~~~~~~~~~~~~~~

When the data files contain text mixed in with numbers you must open the file
as a :ref:`GMT_TEXTSET <struct-textset>` and do your own parsing of the data records. The
following code snippet will visit all text records and print them out with some counters:

  ::

    uint64_t tbl, seg, row, col;
    struct GMT_TEXTTABLE *T = NULL;
    struct GMT_TEXTSEGMENT *S = NULL;

    /* ... create a textset D or read one ... */
    for (tbl = 0; tbl < D->n_tables; tbl++) {   /* For each table */
      T = D->table[tbl];        /* Convenient shorthand for current table */
      for (seg = 0; seg < T->n_segments; seg++) {   /* For all segments */
        S = T->segment[seg];    /* Convenient shorthand for current segment */
        for (row = 0; row < S->n_rows; row++) {	/* For each text record */
          printf ("T=%d S=%d R=%d : %s\n", tbl, seg, row, S->data[row]);
        }
      }
    }

Message and Verbose Reporting
-----------------------------

The API provides two functions for your program to present information
to the user during the run of the program. One is used for messages that
are always written (optionally with a time stamp) while the other is used
for reports whose verbosity level must exceed the verbosity settings specified via **-V**.

Verbose reporting
~~~~~~~~~~~~~~~~~

.. _GMT_Report:

  ::

    int GMT_Report (void *API, unsigned int level, const char *message, ...);

This function takes a verbosity level and a multi-part message (e.g., a
format statement and zero or more variables as required by the format string). The verbosity ``level`` is
an integer in the 0–5 range; these levels are listed in Table :ref:`timemodes <tbl-verbosity>`
You assign an appropriate verbosity level to your message, and depending
on the chosen run-time verbosity level set via **-V** your message may
or may not be reported. Only messages whose stated verbosity level is
lower or equal to the **-V**\ *level* will be printed.  These messages are typically
progress reports, etc., and are sent to standard error.


.. _tbl-verbosity:

    +----------------------+------------------------------------------------+
    | constant             | description                                    |
    +======================+================================================+
    | GMT_MSG_QUIET        | No messages whatsoever                         |
    +----------------------+------------------------------------------------+
    | GMT_MSG_NORMAL       | Default output, e.g., warnings and errors only |
    +----------------------+------------------------------------------------+
    | GMT_MSG_COMPAT       | Compatibility warnings                         |
    +----------------------+------------------------------------------------+
    | GMT_MSG_VERBOSE      | Verbose level                                  |
    +----------------------+------------------------------------------------+
    | GMT_MSG_LONG_VERBOSE | Longer verbose                                 |
    +----------------------+------------------------------------------------+
    | GMT_MSG_DEBUG        | Debug messages for developers mostly           |
    +----------------------+------------------------------------------------+

    The different levels of verbosity that can be selected.

User messages
~~~~~~~~~~~~~

For custom messages to the user that should always be printed, we use 

.. _GMT_Message:

  ::

    int GMT_Message (void *API, unsigned int mode, const char *format, ...);

This function always prints its message to the standard output. Use the
``mode`` value to control if a time stamp should preface the message,
and if selected how the time information should be formatted. See
Table :ref:`timemodes <tbl-timemodes>` for the various modes.

.. _tbl-timemodes:

    +------------------+---------------------------------------+
    | constant         | description                           |
    +==================+=======================================+
    | GMT_TIME_NONE    | Display no time information           |
    +------------------+---------------------------------------+
    | GMT_TIME_CLOCK   | Display current local time            |
    +------------------+---------------------------------------+
    | GMT_TIME_ELAPSED | Display elapsed time since last reset |
    +------------------+---------------------------------------+
    | GMT_TIME_RESET   | Reset the elapsed time to 0           |
    +------------------+---------------------------------------+

    The different types of message modes.

The elapsed times refer to time since the start of the session, but this can
be reset by adding the time flag ``GMT_TIME_RESET``.

Special GMT modules
-------------------

There are some differences between calling
modules on the command line and using them via the API.  These are discussed here.

API-only modules
~~~~~~~~~~~~~~~~

There are two general-purpose modules that are not part of the command-line version of
GMT.  These are the read and write modules.  Both take an option to specify what GMT
resource is being read of written: **-Tc**\ \|\ **d**\ \|\ **g**\ \|\ **i**\ \|\ **p**\ \|\ **t**,
which selects CPT, dataset, grid, image, PostScript, or textset, respectively.  In addition
both modules accept the *infile* and *outfile* argument for source and destination.  These
may be actual files of memory locations, of course.

PostScript Access
~~~~~~~~~~~~~~~~~

The GMT module :doc:`psconvert` is normally given one or more PostScript files that may be
converted to other formats.  When accessed by the API it may also be given the special
file name "=", which means we are to use the internal PostScript string produced by
the latest GMT plotting instead of any actual file name.  The module can access this
string which must be a complete plot (i.e., it must have header, middle, and trailer
and thus be a valid PostScript file).  This allows the API to convert plots to a
suitable image format without any duplication and manipulation of the PostScript
itself.

Adjusting headers and comments
------------------------------

All header records in incoming datasets are stored in memory. You may
wish to replace these records with new information, or append new
information to the existing headers. This is achieved with

.. _GMT_Set_Comment:

  ::

    int GMT_Set_Comment (void *API, unsigned int family, unsigned int mode,
    	void *arg, void *data)

Again, :ref:`family <tbl-family>` selects which kind of resource is passed via ``data``.
The ``mode`` determines what kind of comment is being considered, how it
should be included, and in what form the comment passed via ``arg`` is provided.
Table :ref:`comments <tbl-comments>` lists the available options, which may be combined
by adding (bitwise "or"). The GMT_Set_Comment_ function does not actually
output anything but sets the relevant comment and header records in the
relevant structure. When a file is written out the information will be
output as well (Note: Users can always decide if they wish to turn
header output on or off via the common GMT option ``-h``. For
record-by-record writing you must enable the header block output when
you call GMT_Begin_IO_.

.. _tbl-comments:

    +-------------------------+---------------------------------------------------+
    | constant                | description                                       |
    +=========================+===================================================+
    | GMT_COMMENT_IS_TEXT     | Comment is a text string                          |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_OPTION   | Comment is a linked list of GMT_OPTION structures |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_COMMAND  | Comment is the command                            |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_REMARK   | Comment is the remark                             |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_TITLE    | Comment is the title                              |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_NAME_X   | Comment is the x variable name (grids only)       |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_NAME_Y   | Comment is the y variable name (grids only)       |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_NAME_Z   | Comment is the z variable name (grids only)       |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_COLNAMES | Comment is the column names header                |
    +-------------------------+---------------------------------------------------+
    | GMT_COMMENT_IS_RESET    | Comment replaces existing information             |
    +-------------------------+---------------------------------------------------+

    The modes for setting various comment types.

The named modes (*command*, *remark*, *title*, *name_x,y,z* and
*colnames* are used to distinguish regular text comments from specific
fields in the header structures of the data resources, such as
:ref:`GMT_GRID <struct-grid>`. For the various table resources (e.g., :ref:`GMT_DATASET <struct-dataset>`)
these modifiers result in a specially formatted comments beginning with
"Command: " or "Remark: ", reflecting how this type of information is
encoded in the headers.

Export Data Sets
----------------

If your program needs to write any of the six recognized data types
(CPTs, data tables, text tables, grids, images, or PostScript) you can use the
GMT_Write_Data_ function. 

Both of these output functions takes a parameter called ``mode``. The
``mode`` parameter generally takes on different meanings for the
different data types and will be discussed below. However, one bit
setting is common to all types: By default, you are only allowed to
write a data resource once; the resource is then flagged to have been
written and subsequent attempts to write to the same resource will
quietly be ignored. In the unlikely event you need to re-write a
resource you can override this default behavior by adding ``GMT_IO_RESET``
to your ``mode`` parameter.

Exporting a data set
~~~~~~~~~~~~~~~~~~~~

To have your program accept results from GMT modules and write them
separately requires you to use the GMT_Write_Data_ function. It is very similar to the
GMT_Read_Data_ function encountered earlier.

Exporting a data set to a file, stream, or handle
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The prototype for writing to a file (via name, stream, or file handle) is

.. _GMT_Write_Data:

  ::

    int GMT_Write_Data (void *API, unsigned int family, unsigned int method,
    	unsigned int geometry, unsigned int mode, double wesn[], void *output, void *data);

* :ref:`API <GMT_Create_Session>`
* :ref:`family <tbl-family>`
* :ref:`method <tbl-methods>`
* :ref:`geometry <tbl-geometry>`
* mode -- specific to each data type (\ *see below*)
* :ref:`wesn <tbl-wesn>`
* output --
* data -- A pointer to any of the six families.
* Return: 0 on success, otherwise return -1 and set API->error to reflect to cause.

where ``data`` is a pointer to any of the four structures discussed previously.

**Color palette table**
    ``mode`` controls if the CPT's back-, fore-, and NaN-colors
    should be written (1) or not (0).

**Data table**
    If ``method`` is ``GMT_IS_FILE``, then the value of ``mode`` affects
    how the data set is written:

    **GMT_WRITE_SET**
        The entire data set will be written to the single file [0].

    **GMT_WRITE_TABLE**
        Each table in the data set is written to individual files [1].
        You can either specify an output file name that *must* contain
        one C-style format specifier for a int variable (e.g.,
        "New_Table_%06d.txt"), which will be replaced with the table
        number (a running number from 0) *or* you must assign to each
        table *i* a unique output file name via the
        ``D->table[i]->file[GMT_OUT]`` variables prior to calling the
        function.

    **GMT_WRITE_SEGMENT**
        Each segment in the data set is written to an individual file
        [2]. Same setup as for ``GMT_WRITE_TABLE`` except we use
        sequential segment numbers to build the file names.

    **GMT_WRITE_TABLE_SEGMENT**
        Each segment in the data set is written to an individual file
        [3]. You can either specify an output file name that *must*
        contain two C-style format specifiers for two int variables
        (e.g., "New_Table_%06d_Segment_%03d.txt"), which will be
        replaced with the table and segment numbers, *or* you must
        assign to each segment *j* in each table *i* a unique output
        file name via the ``D->table[i]->segment[j]->file[GMT_OUT]``
        variables prior to calling the function.

    **GMT_WRITE_OGR**
        Writes the dataset in OGR/GMT format in conjunction with the
        ``-a`` setting [4].

**Text table**
    The ``mode`` is used the same way as for data tables.

**GMT grid**
    Here, ``mode`` may be ``GMT_GRID_HEADER_ONLY`` to only update a
    file's header structure, but normally it is simply ``GMT_GRID_ALL``
    so the entire grid and its header will be exported (a subset is
    not allowed during export). However, in the event your data array
    holds both the real and imaginary parts of a complex data set you
    must add either ``GMT_GRID_IS_COMPLEX_REAL`` or
    ``GMT_GRID_IS_COMPLEX_IMAG`` to ``mode`` so as to export the
    corresponding grid values correctly. Finally, for native binary
    grids you may skip writing the grid header by adding
    ``GMT_GRID_NO_HEADER``; this setting is ignored for all other grid
    formats. If your output grid is huge and you are building it
    row-by-row, set ``mode`` to ``GMT_GRID_HEADER_ONLY`` \|
    ``GMT_GRID_ROW_BY_ROW``. You can then write the grid row-by-row
    using GMT_Put_Row_. By default the rows will be automatically
    processed in order. To completely specify which row to be written,
    use ``GMT_GRID_ROW_BY_ROW_MANUAL`` instead; this requires a file format
    that supports direct writes, such as netCDF.

Note: If ``method`` is GMT_IS_FILE, :ref:`family <tbl-family>` is ``GMT_IS_GRID``,
and the filename implies a change from NaN to another value then the grid is
modified accordingly. If you continue to use that grid after writing please be
aware that the changes you specified were applied to the grid.

Record-by-record output
-----------------------

In the case of data and text tables, you may also
consider the GMT_Put_Record_ function for record-by-record writing. As a general rule, your
program organization may simplify if you can write the entire
resource with GMT_Write_Data_. However, if the program logic is simple
or already involves using GMT_Get_Record_, it may be better to export
one data record at the time via GMT_Put_Record_.  For grids there is the
corresponding GMT_Put_Row_ function.

Enable Data Export
~~~~~~~~~~~~~~~~~~

Similar to the data import procedures, once all output destinations have
been registered, we signal the API that we are done with the
registration phase and are ready to start the actual data export. As for
input, this step is only needed when dealing with record-by-record
writing. Again, we enable record-by-record writing by calling
GMT_Begin_IO_, this time with ``direction`` = ``GMT_OUT``. This function
enables data export and prepares the registered destinations for the
upcoming writing.


Specifying the number of output columns
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For record-based input/output you will need to specify the number of output
columns, unless it equals the number of input columns.  This is done with
the GMT_Set_Columns_ function:

.. _GMT_Set_Columns:

  ::

    void *GMT_Set_Columns (void *API, unsigned int n_columns, unsigned int mode);

The ``n_columns`` is a number related to the number of output columns you plan to write, while
``mode`` controls what that number means.  Here, ``mode`` = ``GMT_COL_FIX`` means it is the actual
number of output columns,  ``mode`` = ``GMT_COL_ADD`` means it should be added to the known number
of input columns to arrive at the number of final output columns, while ``mode`` = ``GMT_COL_SUB``
means this value should be subtracted from the number of input columns to find the number of
output columns.


Exporting a data record
~~~~~~~~~~~~~~~~~~~~~~~

If your program must write data table records one-by-one you must first
enable record-by-record writing with GMT_Begin_IO_ and then use the
``GMT_Put_Record`` function in a loop; the prototype is

.. _GMT_Put_Record:

  ::

    int GMT_Put_Record (void *API, unsigned int mode, void *rec);

where ``rec`` is a pointer to either (a) a double-precision array with
the current row. Then, ``rec`` is expected to hold at least as many
items as the current output column setting, which represents the
number of columns in the output destination. Alternatively (b), ``rec``
points to a text string. The ``mode`` parameter must be set to reflect
what is passed. Using GMT_Put_Record_ requires you to first
initialize the destination with GMT_Init_IO_. Note that for families
``GMT_IS_DATASET`` and ``GMT_IS_TEXTSET`` the methods ``GMT_IS_DUPLICATE`` and
``GMT_IS_REFERENCE`` are not supported since you can simply populate the
:ref:`GMT_DATASET <struct-dataset>` structure directly. As mentioned, ``mode`` affects what is
actually written:

**GMT_WRITE_DOUBLE**.
    Normal operation that builds the current output record from the values in ``rec``.

**GMT_WRITE_TEXT**.
    For ASCII output mode we write the text string ``rec``. If ``rec``
    is NULL then we use the current (last imported) text record. If
    binary output mode we quietly skip writing this record.

**GMT_WRITE_TABLE_HEADER**.
    For ASCII output mode we write the text string ``rec``. If ``rec``
    is NULL then we write the last read header record (and ensures it
    starts with #). If binary output mode we quietly skip writing this record.

**GMT_WRITE_SEGMENT_HEADER**.
    For ASCII output mode we use the text string ``rec`` as the
    segment header. If ``rec`` is NULL then we use the current (last
    read) segment header record. If binary output mode instead we write
    a record composed of NaNs.

The function returns 1 if there was an error associated with the
writing (which is passed back with ``API->error``), otherwise it returns
0 (``GMT_NOERROR``).

Exporting a grid row
~~~~~~~~~~~~~~~~~~~~

If your program must write a grid file row-by-row you must first enable
row-by-row writing with GMT_Read_Data_ and then use the
GMT_Put_Row_ function in a loop; the prototype is

.. _GMT_Put_Row:

  ::

    int GMT_Put_Row (void *API, int row_no, struct GMT_GRID *G, float *row);

where ``row`` is a pointer to a single-precision array with the current
row, ``G`` is the grid in question, and ``row_no`` is the number of the
current row to be written. Note this value is only considered if the
row-by-row mode was initialized with ``GMT_GRID_ROW_BY_ROW_MANUAL``.

Disable Data Export
~~~~~~~~~~~~~~~~~~~

Once the record-by-record output has completed we disable further output
to prevent accidental writing from occurring (due to poor program
structure, bugs, etc.). We do so by calling GMT_End_IO_. This
function disables further record-by-record data export; here, we
obviously pass ``direction`` as ``GMT_OUT``.

Destroy allocated resources
---------------------------

If your session imported any data sets into memory then you may
explicitly free this memory once it is no longer needed and before
terminating the session. This is done with the GMT_Destroy_Data_
function, whose prototype is

.. _GMT_Destroy_Data:

  ::

    int GMT_Destroy_Data (void *API, void *data);

where ``data`` is the address of the pointer to a data container, i.e., not
the pointer to the container but the *address* of that pointer (e.g. &pointer).  Note that
when each module completes it will automatically free memory created by
the API; similarly, when the session is destroyed we also automatically
free up memory. Thus, ``GMT_Destroy_Data`` is therefore generally only
needed when you wish to directly free up memory to avoid running out of
it. The function returns 1 if there is an error when trying to
free the memory (the error code is passed back with ``API->error``),
otherwise it returns 0 (``GMT_NOERROR``).

Destroy groups of allocated resources
-------------------------------------

If you obtained an array of resources via GMT_Read_Group_ then
you will need to destroy these resources with GMT_Destroy_Group_ instead,
whose prototype is

.. _GMT_Destroy_Group:

  ::

    int GMT_Destroy_Group (void *API, void *data, unsigned int n);

where ``data`` is the address of the array with data containers, i.e., not
the array to the containers but the *address* of that array (e.g. &array),
and ``n`` is the number of containers.

Terminate a GMT session
-----------------------

Before your program exits it should properly terminate the
GMT session, which involves a call to

.. _GMT_Destroy_Session:

  ::

    int GMT_Destroy_Session (void *API);

which simply takes the pointer to the GMT API control structure as its
only arguments. It terminates the GMT machinery and deallocates all
memory used by the GMT API book-keeping. It also unregisters any
remaining resources previously registered with the session. The
GMT API will only close files that it was responsible for opening in
the first place. Finally, the API structure itself is freed so your main
program does not need to do so. The function returns 1 if there
is an error when trying to free the memory (the error code is passed
back with ``API->error``), otherwise it returns 0 (``GMT_NOERROR``).

.. _sec-parsopt:

Presenting and accessing GMT options
------------------------------------

As you develop a program you may wish to rely on some of
the GMT common options. For instance, you may wish to have your
program present the ``-R`` option to the user, let GMT handle the
parsing, and examine the values. You may also wish to encode your own
custom options that may require you to parse user text into the
corresponding floating point dimensions, constants, coordinates, absolute time, etc.
The API provides several functions to simplify these tedious parsing
tasks. This section is intended to show how the programmer will obtain
information from the user that is necessary to do the task at hand
(e.g., special options to provide values and settings for the program).
In the following section we will concern ourselves with preparing
arguments for calling any of the GMT modules.

Display usage syntax for GMT common options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can have your program menu display the standard usage message for a
GMT common option by calling the function

.. _GMT_Option:

  ::

    int GMT_Option (void *API, const char *options);

where ``options`` is a comma-separated list of GMT common options
(e.g., "R,J,O,X"). You can repeat this function with different sets of
options in order to intersperse your own custom options within an
overall alphabetical order; see any GMT module for examples of typical
layouts.

Parsing the GMT common options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The parsing of all GMT common option is done by on call to

.. _GMT_Parse_Common:

  ::

    int GMT_Parse_Common (void *API, const char *args, struct GMT_OPTION *list);

where ``args`` is a string of the common GMT options your program is allowed to use.
An error will be reported if any of the common GMT options fail
to parse, and if so we return 1; if no errors we return 0. All
other options, including file names, will be silently ignored. The
parsing will update the internal GMT information structure that
affects module operations.

Inquiring about the GMT common options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The API provide only a limited window into the full GMT machinery
accessible to the modules. You can determine if a particular common
option has been parsed and in some cases determine the values that were set with

.. _GMT_Get_Common:

  ::

    int GMT_Get_Common (void *API, unsigned int option, double *par);

where ``option`` is a single option character (e.g., 'R') and ``par`` is
a double array with at least a length of 6. If the particular option has
been parsed then the function returns the number of parameters passed
back via ``par``; otherwise we return -1. For instance, to determine if
the ``-R`` was set and to obtain the specified region you may call

  ::

    if (GMT_Get_Common (API, 'R', wesn)) != -1) {
        /* wesn now contains the boundary information */
    }

The ``wesn`` array could now be passed to the various read and create
functions for GMT resources.

Parsing text values
~~~~~~~~~~~~~~~~~~~

Your program may need to request values from the user, such as
distances, plot dimensions, coordinates, date/time strings and other data. The conversion
from such text to actual distances, taking units into account, is
tedious to program. You can simplify this by using

.. _GMT_Get_Values:

  ::

    int GMT_Get_Values (void *API, const char *arg, double par[], int maxpar);

where ``arg`` is the text item with one or more values that are
separated by commas, spaces, tabs, semi-colons, or slashes, and ``par`` is an array of length ``maxpar`` long
enough to hold all the items you are parsing. The function returns the
number of items parsed with a maximum of ``maxpar``, or -1 if there is an error. For instance, assume
the character string ``origin`` was given by the user as two geographic
coordinates separated by a slash (e.g., ``"35:45W/19:30:55.3S"``). We
obtain the two coordinates in decimal degrees by calling

  ::

    n = GMT_Get_Values (API, origin, pair, 2);

Your program can now check that ``n`` equals 2 and then use the values
in ``pairs`` separately. Note: Dimensions given with units of inches, cm, or points
are converted to the current default unit set via :ref:`PROJ_LENGTH_UNIT <PROJ_LENGTH_UNIT>`,
while distances given in km, nautical miles, miles, feet, or
survey feet are returned in meters. Arc lengths in minutes and seconds
are returned in decimal degrees, and date/time values are returned in
seconds since the current epoch [1970].

Get or set an API or GMT default parameter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If your program needs to determine one or more of the current
API or GMT default settings you can do so via

.. _GMT_Get_Default:

  ::

    int GMT_Get_Default (void *API, const char *keyword, char *value);

where ``keyword`` is one such keyword (e.g., :ref:`PROJ_LENGTH_UNIT <PROJ_LENGTH_UNIT>`) and
``value`` must be a character string long enough to hold the answer.  In
addition to the long list of GMT defaults you can also inquire about the
few API parameters ``API_PAD`` (the current pad setting), ``API_IMAGE_LAYOUT`` (the
order and structure of image memory storage), and ``API_GRID_LAYOUT`` (order of
grid memory storage).
Depending on what parameter you selected you could further convert it to
a numerical value with GMT_Get_Values_ or just use it in a text comparison.

To change any of the API or
GMT default settings programmatically you would use

.. _GMT_Set_Default:

  ::

    int GMT_Set_Default (void *API, const char *keyword, const char *value);

where as before ``keyword`` is one such keyword (e.g., :ref:`PROJ_LENGTH_UNIT <PROJ_LENGTH_UNIT>`) and
``value`` must be a character string with the new setting.
Note that all settings must be passed as text strings even if many are
inherently integers or floats.

For indexed access to custom grids and images we may need to know the internal matrix layout.
You can change this information via

.. _GMT_Set_Index:

  ::

    int64_t GMT_Set_Index (struct GMT_GRID_HEADER *header, char *code);

where the ``header`` is the header of either a grid or image, and ``code`` is a three-character
code indication ...

.. _sec-func:

Call a module
-------------

One of the advantages of programming with the API is that you
have access to the high-level GMT modules. For example, if your
program must compute the distance from a node to all other nodes in the grid
then you can simply set up options and call :doc:`grdmath` to do it
for you and accept the result back as an input grid. All the module
interfaces are identical and are called via

.. _GMT_Call_Module:

  ::

    int GMT_Call_Module (void *API, const char *module, int mode, void *args);

Here, ``module`` is the name of any of the GMT modules, such as
:doc:`psxy` or :doc:`grdvolume`.  All GMT modules may be called with one of
three sets of ``args`` depending on ``mode``. The three modes differ in
how the options are passed to the module:

    *mode* = ``GMT_MODULE_EXIST``.
        Return GMT_NOERROR (0) if the module exists, nonzero otherwise.

    *mode* = ``GMT_MODULE_PURPOSE``.
        Just print the one-line purpose of the module; args must be NULL.

    *mode* = ``GMT_MODULE_LIST``.
        Just prints a list of all modules (including those given as plugins); args must be NULL.

    *mode* = ``GMT_MODULE_OPT``.
        Expects ``args`` to be a pointer to a doubly-linked list of objects with individual
        options for the current program. We will see
        how API functions can help prepare and maintain such lists.

    *mode* = ``GMT_MODULE_CMD``.
        Expects ``args`` to be a single text string with all required options.

    *mode > 0*.
        Expects ``args`` to be an array of text strings and ``mode`` to be a count of how many
        options are passed (i.e., the ``argc, argv[]`` model used by the GMT programs themselves).


Set program options via text array arguments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When ``mode > 0`` we expect an array ``args`` of character
strings that each holds a single command line option (e.g.,
"-R120:30/134:45/8S/3N") and interpret ``mode`` to be the count of how
many options are passed. This, of course, is almost exactly how the
stand-alone GMT programs are called (and reflects how they themselves
are activated internally). We call this the "argc-argv" mode. Depending
on how your program obtains the necessary options you may find that this
interface offers all you need.

Set program options via text command
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If ``mode =`` 0 then ``args`` will be examined to see if it contains
several options within a single command string. If so we will break
these into separate options. This is useful if you wish to pass a single
string such as "-R120:30/134:45/8S/3N -JM6i mydata.txt -Sc0.2c". We call
this the "command" mode and it is extensively used by the modules themselves.

Set program options via linked structures
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The third, linked-list interface allows developers using higher-level
programming languages to pass all command options via a pointer to a
NULL-terminated, doubly-linked list of option structures, each
containing information about a single option. Here, instead of text
arguments we pass the pointer to the linked list of options mentioned
above, and ``mode`` must be passed as ``GMT_MODULE_OPT``. Using
this interface can be more involved since you need to generate the
linked list of program options; however, utility functions exist to
simplify its use. This interface is intended for programs whose internal
workings are better suited to generate such arguments -- we call this the
"options" mode. The order in the list is not important as GMT will
sort it internally according to need. The option structure is defined below.

.. _options:

  ::

    struct GMT_OPTION {
        char               option;  /* Single option character (e.g., 'G' for -G) */
        char              *arg;     /* String with arguments (NULL if not used) */
        struct GMT_OPTION *next;    /* Next option pointer (NULL for last option) */
        struct GMT_OPTION *prev;    /* Previous option (NULL for first option) */
    };

Convert between text and linked structures
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To assist programmers there are also two convenience functions that
allow you to convert between the two argument formats. They are

.. _GMT_Create_Options:

  ::

    struct GMT_OPTION *GMT_Create_Options (void *API, int argc, void *args);

This function accepts your array of text arguments (cast via a void
pointer), allocates the necessary space, performs the conversion, and
returns a pointer to the head of the linked list of program options.
However, in case of an error we return a NULL pointer and set
``API->error`` to indicate the nature of the problem. Otherwise, the
pointer may now be passed to the relevant GMT module. Note that if
your list of text arguments were obtained from a C ``main()`` function
then ``argv[0]`` will contain the name of the calling program. To avoid
passing this as a bad file name option, call GMT_Create_Options_ with
``argc-1`` and ``argv+1`` instead. If you wish to pass a single text string with
multiple options (in lieu of an array of text strings), then pass
``argc`` = 0. When no longer needed you can remove the entire list by calling

.. _GMT_Destroy_Options:

  ::

    int GMT_Destroy_Options (void *API, struct GMT_OPTION **list);

The function returns 1 if there is an error (which is passed back
with ``API->error``), otherwise it returns 0 (``GMT_NOERROR``).

The inverse function prototype is

.. _GMT_Create_Args:

  ::

    char **GMT_Create_Args (void *API, int *argc, struct GMT_OPTION *list);

which allocates space for the text strings and performs the conversion;
it passes back the count of the arguments via ``argc`` and returns a
pointer to the text array. In the case of an error we return a NULL
pointer and set ``API->error`` to reflect the error type. Note that
``argv[0]`` will not contain the name of the program as is the case the
arguments presented by a C ``main()`` function. When you no longer have
any use for the text array, call

.. _GMT_Destroy_Args:

  ::

    int GMT_Destroy_Args (void *API, int argc, char **argv[]);

to deallocate the space used. This function returns 1 if there is
an error (which is passed back with ``API->error``), otherwise it returns 0 (``GMT_NOERROR``).

Finally, to convert the linked list of option structures to a single
text string command, use

.. _GMT_Create_Cmd:

  ::

    char *GMT_Create_Cmd (void *API, struct GMT_OPTION *list);

Developers who plan to import and export GMT shell scripts might find
it convenient to use these functions. In case of an error we return a
NULL pointer and set ``API->error``, otherwise a pointer to an allocated
string is returned.  When you no longer have
any use for the text string, call

.. _GMT_Destroy_Cmd:

  ::

    int GMT_Destroy_Cmd (void *API, char **string);

to deallocate the space used. This function returns 1 if there is
an error (which is passed back with ``API->error``), otherwise it
returns 0  (``GMT_NOERROR``).

Manage the linked list of options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Several additional utility functions are available for programmers who
wish to manipulate program option structures within their own programs.
These allow you to create new option structures, append them to the
linked list, replace existing options with new values, find a particular
option, and remove options from the list. Note: The order in which the
options appear in the linked list is of no consequence to GMT.
Internally, GMT will sort and process the options in the manner
required. Externally, you are free to maintain your own order.

Make a new option structure
^^^^^^^^^^^^^^^^^^^^^^^^^^^

GMT_Make_Option_ will allocate a new option structure, assign
values given the ``option`` and ``arg`` parameters (pass NULL if there is
no argument for this option), and return a pointer to the allocated
structure. The prototype is

.. _GMT_Make_Option:

  ::

    struct GMT_OPTION *GMT_Make_Option (void *API, char option, const char *arg);

Should memory allocation fail the function will print an error message
pass an error code via ``API->error``, and return NULL.

Append an option to the linked list
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

GMT_Append_Option_ will append the specified ``option`` to the end of
the doubly-linked ``list``. The prototype is

.. _GMT_Append_Option:

  ::

    struct GMT_OPTION *GMT_Append_Option (void *API, struct GMT_OPTION *option,
    	struct GMT_OPTION *list);

We return the list back, and if ``list`` is given as NULL we return
``option`` as the start of the new list. Any errors result in a NULL
pointer with ``API->error`` holding the error type.

Find an option in the linked list
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

GMT_Find_Option_ will return a pointer ``ptr`` to the first option in
the linked list starting at ``list`` whose option character equals
``option``. If not found we return NULL. While this is not necessarily
an error we still set ``API->error`` accordingly. The prototype is

.. _GMT_Find_Option:

  ::

    struct GMT_OPTION *GMT_Find_Option (void *API, char option,
    	struct GMT_OPTION *list);

If you need to look for multiple occurrences of a certain option you
will need to call GMT_Find_Option_ again, passing the option
following the previously found option as the ``list`` entry, i.e.,

  ::

    list = *ptr->next;

Update an existing option in the list
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

GMT_Update_Option_ will replace the argument of ``current`` with the
new argument ``arg`` and otherwise leave the option at its place in the
list. The prototype is

.. _GMT_Update_Option:

  ::

    int GMT_Update_Option (void *API, struct GMT_OPTION *current, const char *arg);

An error will be reported if (a) ``current`` is NULL or (b) ``arg`` is
NULL. The function returns 1 if there is an error, otherwise it returns 0 (``GMT_NOERROR``).

Delete an existing option in the linked list
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You may use GMT_Delete_Option_ to remove the ``current`` option from the linked
``list``. The prototype is

.. _GMT_Delete_Option:

  ::

    int GMT_Delete_Option (void *API, struct GMT_OPTION *current);

We return 1 if the option is not found in the list and set
``API->error`` accordingly. Note: Only the first occurrence of the
specified option will be deleted. If you need to delete all such options
you will need to call this function in a loop until it returns a
non-zero status.

Specify a file via a linked option
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To specify an input file name via an option, simply use < as the
option (this is what GMT_Create_Options_ does when it finds filenames
on the command line). Likewise, > can be used to explicitly
indicate an output file. In order to append to an existing file, use
). For example the following command would read from file.A and
append to file.B:

  ::

    gmt convert -<file.A -)file.B

These options also work on the command line but usually one would have
to escape the special characters <, > and ) as they are normally
used for file redirection.

Encode option arguments for external interfaces
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Developers writing interfaces between GMT and external platforms such
as other languages (Python, Java, Julia, etc.) or tools (MATLAB, Octave,
etc.) need to manipulate linked options in a special way.  For instance,
a GMT call in the MATLAB or Octave application might look like

  ::

    table = gmt ('blockmean -R30W/30E/10S/10N -I2m', [x y z]);
    grid  = gmt ('surface -R -I2m -Lu', table, high_limit_grid);
    grid2 = gmt ('grdmath ? LOG10 ? MUL', grid, grid);

Most of the time our implicit rules will take care of the ordering.  The
rule says that all required input data items must be listed before any
secondary input data items, and all primary output items must be listed
on the left hand side before any secondary output items.
There are three situations where the parsing will need further help;
(1) Specifying the positions of memory arguments given to :doc:`gmtmath`,
(2) specifying the positions of memory arguments given to :doc:`grdmath`,
and (3) using -R? when passing a memory grid to the -R option (since just -R
means use the previous region in the command history).
Thus, in the :doc:`gmtmath` call we we needed to specify where
the specific arguments should be placed among the operators.
API developers will rely on GMT_Open_VirtualFile_ to convert the
above syntax to correct options for GMT_Call_Module_.
The prototype is

.. _GMT_Encode_Options:

  ::

    struct GMT_RESOURCE *GMT_Encode_Options (void *API, const char *module, int n_in,
    	struct GMT_OPTION **head, int *n_items);

where ``module`` is the name of the module whose linked options are
pointed to by ``*head``, ``n_in`` contains the number of *input*
objects we have to connect (or -1 if not known) and we return an array
that contains specific information for those options that
(after processing) contain explicit memory references.  The number of
items in the array is returned via the ``n_items`` variable.  The function
returns NULL if there are errors and sets ``API->error`` to the corresponding
error number.  The GMT_RESOURCE structure is defined below:

.. .. _struct-grid:

.. code-block:: c

   struct GMT_RESOURCE {	/* Information for passing external resources */
       enum GMT_enum_family family;     /* GMT data family */
       enum GMT_enum_geometry geometry; /* One of the recognized GMT geometries */
       enum GMT_enum_std direction;     /* Either GMT_IN or GMT_OUT */
       struct GMT_OPTION *option;       /* Pointer to the corresponding module option */
       int object_ID;                   /* Object ID returned by GMT_Register_IO */
       int pos;                         /* Index into external object in|out arrays */
       int mode;                        /* 0 means primary i/o object, 1 means secondary */
       void *object;                    /* Pointer to the registered GMT object */
   };

API developers will need to provide specific code to handle the registration of native
structures in their language or application and to translate between the GMT resources
and the corresponding native items.  Developers should look at an existing and working
interface such as the GMT/MATLAB toolbox to see the required steps.

Expand an option with explicit memory references
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

When the external tool or application knows the name of the special file names
used for memory references the developer should replace the place-holder ``?`` character
in any option string with the actual reference name.  This is accomplished by
calling GMT_Expand_Option_, with prototype

.. _GMT_Expand_Option:

  ::

    int GMT_Expand_Option (void *API, struct GMT_OPTION *option, const char *name);

where ``option`` is the current option and ``name``
is the special file name for the memory reference.

The GMT FFT Interface
=====================

While the i/o options presented so far lets you easily read in a data
table or grid and manipulate them, if you need to do the manipulation in the
wavenumber domain then this chapter is for you. Here, we outline how to
take the Fourier transform of such data, perform calculations in the
wavenumber domain, and take the inverse transform before writing the
results. To assist programmers we also distribute fully functioning
demonstration programs that takes you through the steps we are about to
discuss; these demo programs may be used as your starting point for
further development and can be found in the gmt-custom repository.

Presenting and parsing the FFT options
--------------------------------------

Several GMT programs that use the FFTs present the same unified option and
modifier sets to the user. The API makes these available as well. If
your program needs to present the FFT option usage you can call

.. _GMT_FFT_Option:

  ::

    unsigned int GMT_FFT_Option (void *API, char option, unsigned int dim,
    	const char *string);

Here, ``option`` is the unique character used for this particular
program option (most GMT programs have standardized on using 'N' but
you are free to choose whatever letter you want except existing GMT common
options). The ``dim`` sets the dimension of the transform; currently you
must choose 1 or 2, while ``string`` is a one-line message that
states what the option does; you should tailor this to your program. If
NULL then a generic message is placed instead.

To parse the user's selection you call

.. _GMT_FFT_Parse:

  ::

    void *GMT_FFT_Parse (void *API, char option, unsigned int dim, const char *arg);

which accepts the user's string option via ``arg``; the other arguments
are the same as those above. The function returns an opaque pointer to a
structure with the chosen parameters.

Initializing the FFT machinery
------------------------------

Before your can take any transforms you must initialize the FFT
machinery. This process involves a series of preparatory steps that are
conveniently performed for you by

.. _GMT_FFT_Create:

  ::

    void *GMT_FFT_Create (void *API, void *X, unsigned int dim,
    	unsigned int mode, void *F);

Here, ``X`` is either your dataset or grid pointer, ``dim`` is the
dimension of the transform (1 or 2 only), ``mode`` passes various flags to the setup, such as whether
the data is real, imaginary, or complex, and ``F`` is the opaque pointer
previously returned by GMT_FFT_Parse_. Depending on the option string you passed to
GMT_FFT_Parse_, the data may have a constant level or a trend
removed, mirror reflected and extended by various symmetries, padded and
tapered to desired transform dimensions, and possibly
temporary files are written out before the transform takes place. See the :doc:`grdfft`
man page for a full explanation of the options presented by GMT_FFT_Option_.

Taking the FFT
--------------

Now that everything has been set up you can perform the transform with

.. _GMT_FFT:

  ::

    void *GMT_FFT (void *API, void *X, int direction, unsigned int mode, void *K);

which takes as ``direction`` either ``GMT_FFT_FWD`` or ``GMT_FFT_INV``. The
``mode`` is used to specify if we pass a real (``GMT_FFT_REAL``) or complex
(``GMT_FFT_COMPLEX``) data set, and ``K`` is the opaque pointer returned
by GMT_FFT_Create_. The transform is performed in place and returned
via ``X``. When done with your manipulations (below) you can call it
again with the inverse direction to recover the corresponding space-domain
version of your data. The FFT is fully normalized so that calling
forward followed by inverse yields the original data set. The information
passed via ``K`` determines if a 1-D or 2-D transform takes place; the
key work is done via ``GMT_FFT_1D`` or ``GMT_FFT_2D``, as explained below.

Taking the 1-D FFT
------------------

A lower-level 1-D FFT is also available via the API, i.e.,

.. _GMT_FFT_1D:

  ::

    int GMT_FFT_1D (void *API, float *data, uint64_t n, int direction,
    	unsigned int mode);

which takes as ``direction`` either ``GMT_FFT_FWD`` or ``GMT_FFT_INV``. The
``mode`` is used to specify if we pass a real (``GMT_FFT_REAL``) or complex
(``GMT_FFT_COMPLEX``) data set, and ``data`` is the 1-D data array of length
``n`` that we wish
to transform. The transform is performed in place and returned
via ``data``. When done with your manipulations (below) you can call it
again with the inverse direction to recover the corresponding space-domain
version of your data. The 1-D FFT is fully normalized so that calling
forward followed by inverse yields the original data set.

Taking the 2-D FFT
------------------

A lower-level 2-D FFT is also available via

.. _GMT_FFT_2D:

  ::

    int GMT_FFT_2D (void *API, float *data, unsigned int n_columns,
    	unsigned int n_rows, int direction, unsigned int mode);

which takes as ``direction`` either ``GMT_FFT_FWD`` or ``GMT_FFT_INV``. The
``mode`` is used to specify if we pass a real (``GMT_FFT_REAL``) or complex
(``GMT_FFT_COMPLEX``) data set, and ``data`` is the 2-D data array in
row-major format, with row length ``n_columns`` and column length ``n_rows``.
The transform is performed in place and returned
via ``data``. When done with your manipulations (below) you can call it
again with the inverse direction to recover the corresponding space-domain
version of your data. The 2-D FFT is fully normalized so that calling
forward followed by inverse yields the original data set.

Wavenumber calculations
-----------------------

As your data have been transformed to the wavenumber domain you may wish
to operate on the various values as a function of wavenumber. We will
show how this is done for datasets and grids separately. First, we
present the function that returns an individual wavenumber:

.. _GMT_FFT_Wavenumber:

  ::

    double GMT_FFT_Wavenumber (void *API, uint64_t k, unsigned int mode, void *K);

where ``k`` is the index into the array or grid, ``mode`` specifies
which wavenumber we want (it is not used for 1-D transform but for the
2-D transform we can select either the x-wavenumber (0), the
y-wavenumber (1), or the radial wavenumber (2)), and finally the opaque
vector created by GMT_FFT_Create_.

1-D FFT manipulation
~~~~~~~~~~~~~~~~~~~~

[To be added after gmtfft has been added as new module, probably in 5.4.]

2-D FFT manipulation
~~~~~~~~~~~~~~~~~~~~

The number of complex pairs in the grid is given by the header's ``nm``
variable, while ``size`` will be twice that value as it holds the number
of components. To visit all the complex values and obtain the
corresponding wavenumber we simply need to loop over ``size`` and call
GMT_FFT_Wavenumber_. This code snippet multiples the complex grid by
the radial wavenumber:

  ::

    uint64_t k;
    for (k = 0; k < Grid->header->size; k++) {
        wave = GMT_FFT_Wavenumber (API, k, 2, K);
        Grid->data[k] *= wave;
    }

Alternatively, you may choose to be more specific about which components
are real and imaginary (especially if they are to be treated
differently), and set up the loop this way:

  ::

    uint64_t re, im;
    for (re = 0, im = 1; re < Grid->header->size; re += 2, im += 2) {
        wave = GMT_FFT_Wavenumber (API, re, 2, K);
        Grid->data[re] *= wave;
        Grid->data[im] *= 2.0 * wave;
    }

Destroying the FFT machinery
----------------------------

When done you terminate the FFT machinery with

.. _GMT_FFT_Destroy:

  ::

    double GMT_FFT_Destroy (void *API, void *K);

which simply frees up the memory allocated by the FFT machinery with GMT_FFT_Create_.

FORTRAN interfaces
==================

FORTRAN 90 developers who wish to use the GMT API may use the same
API functions as discussed in Chapter 2. As we do not have much (i.e., any) experience
with modern Fortran we are not sure to what extent you are able to access
the members of the various structures, such as the :ref:`GMT_GRID <struct-grid>` structure. Thus,
this part will depend on feedback and for the time being is to be considered
preliminary and subject to change.  We encourage you to take contact should you
wish to use the API with your Fortran 90 programs.

FORTRAN 77 Grid i/o
-------------------

Because of a lack of structure pointers we can only provide a low level of
support for Fortran 77. This API is limited to help you inquire, read and write
GMT grids directly from Fortran 77.
To inquire about the range of information in a grid, use

.. _GMT_F77_readgrdinfo:

  ::

    int GMT_F77_readgrdinfo (unsigned int dim[], double limits[], double inc[],
    	char *title, char *remark, const char *file)

where ``dim`` returns the grid width, height, and registration, ``limits`` returns the min and max values for x, y, and z
as three consecutive pairs, ``inc`` returns the x and y increments, while the ``title`` and ``remark``
return the values of these strings. The ``file``
argument is the name of the file we wish to inquire about. The function returns 0 unless there is an error.
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.

To actually read the grid, we use

.. _GMT_F77_readgrd:

  ::

    int GMT_F77_readgrd (float *array, unsigned int dim[], double wesn[],
    	double inc[], char *title, char *remark, const char *file)

where ``array`` is the 1-D grid data array, ``dim`` returns the grid width, height, and registration,
``limits`` returns the min and max values for x, y, and z, ``inc`` returns the x and y increments, and
the ``title`` and ``remark`` return the values of the corresponding strings.  The ``file``
argument is the name of the file we wish to read from.  The function returns 0 unless there is an error.
Note on input, ``dim[2]`` can be set to 1, which means we will allocate the array for you; otherwise
we assume space has already been secured.  Also, if ``dim[3]`` is set to 1 we will in-place transpose
the array from C-style row-major array order to Fortran column-major array order.

Finally, to write a grid to file you can use

.. _GMT_F77_writegrd:

  ::

    int GMT_F77_writegrd_(float *array, unsigned int dim[], double wesn[],
    	double inc[], const char *title, const char *remark, const char *file)

where ``array`` is the 1-D grid data array, ``dim`` specifies the grid width, height, and registration,
``limits`` may be used to specify a subset (normally, just pass zeros), ``inc`` specifies the x and y increments,
while the ``title`` and ``remark`` supply the values of these strings.  The ``file``
argument is the name of the file we wish to write to.  The function returns 0 unless there is an error.
If ``dim[3]`` is set to 1 we will in-place transpose
the array from Fortran column-major array order to C-style row-major array order before writing. Note
this means ``array`` will have been transposed when the function returns.

Appendix A: GMT resources
-------------------------

We earlier introduced the six standard GMT resources (dataset, grid, image, color palette table, PostScript, textset)
as well as the user vector and matrix.  Here are the complete definitions of these structures, including
all variables accessible via the structures.

Data set
~~~~~~~~

Each data set is represented by a :ref:`GMT_DATASET <struct-dataset>` that consists of one or more data
tables represented by a :ref:`GMT_DATATABLE <struct-datatable>`, and each table consists of one or more
segments represented by a :ref:`GMT_DATASEGMENT <struct-datasegment>`, and each segment contains one or
more rows of a fixed number of columns.  If the dataset originated from an OGR/GMT file then there
will be items such as an :ref:`GMT_OGR <struct-ogr>` structure per table and a :ref:`GMT_OGR_SEG <struct-ogr_seg>`
structure per segment.

.. _struct-dataset:

.. code-block:: c

   struct GMT_DATASET {	/* Single container for an array of GMT tables (files) */
       /* Variables we document for the API: */
       uint64_t               n_tables;     /* Total number of tables (files) contained */
       uint64_t               n_columns;    /* Number of data columns */
       uint64_t               n_segments;   /* Total number of segments across all tables */
       uint64_t               n_records;    /* Total number of data records across all tables */
       double                *min;          /* Minimum coordinate for each column */
       double                *max;          /* Maximum coordinate for each column */
       struct GMT_DATATABLE **table;        /* Pointer to array of tables */
       /* ---- Variables "hidden" from the API ---- */
       uint64_t               id;           /* The internal number of the data set */
       size_t                 n_alloc;      /* The current allocation length of tables */
       uint64_t               dim[4];       /* Used by GMT_Duplicate_Data to override dimensions */
       unsigned int           geometry;     /* The geometry of this dataset */
       unsigned int           alloc_level;  /* The level it was allocated at */
       enum GMT_enum_dest     io_mode;      /* -1: write OGR format (requires proper -a),
                                               0: write everything to one destination [Default],
                                               1: use table->file[GMT_OUT] to write separate table,
                                               2: use segment->file[GMT_OUT] to write separate segments.
                                               3: as 2 but with no filenames we create filenames from
                                                 tbl and seg numbers */
       enum GMT_enum_alloc    alloc_mode;   /* Allocation mode [GMT_ALLOCATED_BY_GMT] */
       char                  *file[2];      /* Name of file or source [0 = in, 1 = out] */
   };

Here is the full definition of the ``GMT_DATATABLE`` structure:

.. _struct-datatable:

.. code-block:: c

   struct GMT_DATATABLE {  /* To hold an array of line segment structures and header information in one container */
       /* Variables we document for the API: */
       unsigned int n_headers;    /* Number of file header records (0 if no header) */
       uint64_t     n_columns;    /* Number of columns (fields) in each record */
       uint64_t     n_segments;   /* Number of segments in the array */
       uint64_t     n_records;    /* Total number of data records across all segments */
       double      *min;          /* Minimum coordinate for each column */
       double      *max;          /* Maximum coordinate for each column */
       char       **header;       /* Array with all file header records, if any) */
       struct GMT_DATASEGMENT **segment; /* Pointer to array of segments */
       /* ---- Variables "hidden" from the API ---- */
       uint64_t     id;           /* The internal number of the table */
       size_t       n_alloc;      /* The current allocation length of segments */
       double       dist;         /* Distance from a point to this feature */
       enum GMT_enum_write mode;  /* 0 = output table, 1 = output header only, 2 = skip table */
       struct GMT_OGR *ogr;       /* Pointer to struct with all things GMT/OGR
								     (if MULTI-geometry and not MULTIPOINT)  */
       char        *file[2];      /* Name of file or source [0 = in, 1 = out] */
   };

Here is the full definition of the ``GMT_DATASEGMENT`` structure:

.. _struct-datasegment:

.. code-block:: c

   struct GMT_DATASEGMENT {       /* For holding segment lines in memory */
       /* Variables we document for the API: */
       uint64_t n_rows;           /* Number of points in this segment */
       uint64_t n_columns;        /* Number of fields in each record (>= 2) */
       double  *min;              /* Minimum coordinate for each column */
       double  *max;              /* Maximum coordinate for each column */
       double **data;             /* Data x,y, and possibly other columns */
       char    *label;            /* Label string (if applicable) */
       char    *header;           /* Segment header (if applicable) */
       /* ---- Variables "hidden" from the API ---- */
       enum GMT_enum_write mode;  /* 0 = output segment, 1 = output header only, 2 = skip segment */
       enum GMT_enum_pol pol_mode;/* GMT_IS_PERIMETER [-Pp] or GMT_IS_HOLE [-Ph] (for polygons only) */
       uint64_t id;               /* The internal number of the segment */
       size_t   n_alloc;          /* The current allocation length of each data column */
       unsigned int range;        /* Longitude reporting scheme, e.g. GMT_IS_GIVEN_RANGE [0] */
       int      pole;             /* Spherical polygons only: If it encloses the S (-1)
									 or N (+1) pole, or none (0) */
       double   dist;             /* Distance from a point to this feature */
       double   lat_limit;        /* For polar caps: the latitude of the point closest to the pole */
       struct GMT_OGR_SEG *ogr;   /* NULL unless OGR/GMT metadata exist for this segment */
       struct GMT_DATASEGMENT *next; /* NULL unless polygon has holes and pointing to next hole */
       char    *file[2];          /* Name of file or source [0 = in, 1 = out] */
    };

The definition of the ``GMT_OGR`` structure (not part of the API):

.. _struct-ogr:

.. code-block:: c

    struct GMT_OGR {  /* Struct with all things GMT/OGR for a table */
        /* The first parameters are usually set once per data set and do not change */
        unsigned int  geometry;    /* @G: Geometry of this data set, if known [0 otherwise] */
        unsigned int  n_aspatial;  /* @T: Number of aspatial fields */
        char         *region;      /* @R: Region textstring [NULL if not set] */
        char         *proj[4];     /* @J: The 1-4 projection strings [NULL if not set] */
        unsigned int *type;        /* @T: Data types of the aspatial fields [NULL if not set]  */
        char        **name;        /* @N: Names of the aspatial fields [NULL if not set]  */
        /* The following are for OGR data only. It is filled during parsing (current segment)
		   but is then copied to the segment header so it can be accessed later */
        enum GMT_enum_pol pol_mode;/* @P: GMT_IS_PERIMETER or GMT_IS_HOLE (for polygons only) */
        char        **tvalue;      /* @D: The text values of the current aspatial fields */
        double       *dvalue;      /* @D: Same but converted to double (assumed possible) */
    };

The definition of the ``GMT_OGR_SEG`` structure (not part of the API):

.. _struct-ogr_seg:

.. code-block:: c

    struct GMT_OGR_SEG {           /* Struct with GMT/OGR aspatial data for a segment */
        enum GMT_enum_pol pol_mode;/* @P: GMT_IS_PERIMETER or GMT_IS_HOLE (for polygons only) */
        unsigned int   n_aspatial; /* @T: Number of aspatial fields */
        char         **tvalue;     /* @D: Values of current aspatial fields */
        double *dvalue;            /* @D: Same but converted to double (assumed possible) */
    };

GMT grid
~~~~~~~~

A grid is represented by a :ref:`GMT_GRID <struct-grid>` that consists of a header structure
represented by a :ref:`GMT_GRID_HEADER <struct-gridheader>` and an float array ``data`` that
contains the grid values.

.. _struct-grid:

.. code-block:: c

   struct GMT_GRID {                        /* To hold a GMT float grid and its header in one container */
       struct GMT_GRID_HEADER *header;      /* Pointer to full GMT header for the grid */
       float                  *data;        /* Pointer to the float grid */
       /* ---- Variables "hidden" from the API ---- */
       unsigned int            id;          /* Internal number of the grid */
       unsigned int            alloc_level; /* Level it was allocated at */
       enum GMT_enum_alloc     alloc_mode;  /* Allocation mode [GMT_ALLOCATED_BY_GMT] */
       double                 *x, *y;       /* Vector of coordinates */
       void                   *extra;       /* Row-by-row machinery information [NULL] */
   };

The full definition of the ``GMT_GRID_HEADER`` structure.  Most of these members are only used internally:

.. _struct-gridheader:

.. code-block:: c

   struct GMT_GRID_HEADER {
       /* Variables we document for the API:
        * They are copied verbatim to the native grid header and must be 4-byte unsigned ints. */
       uint32_t n_columns;                   /* Number of columns */
       uint32_t n_rows;                      /* Number of rows */
       uint32_t registration;                /* GMT_GRID_NODE_REG (0) or GMT_GRID_PIXEL_REG (1) */

       /* == The types of the following 12 elements must not be changed.
        * == They are also copied verbatim to the native grid header. */
       double wesn[4];                         /* Min/max x and y coordinates */
       double z_min;                           /* Minimum z value */
       double z_max;                           /* Maximum z value */
       double inc[2];                          /* x and y increment */
       double z_scale_factor;                  /* grd values must be multiplied by this */
       double z_add_offset;                    /* After scaling, add this */
       char   x_units[GMT_GRID_UNIT_LEN80];    /* units in x-direction */
       char   y_units[GMT_GRID_UNIT_LEN80];    /* units in y-direction */
       char   z_units[GMT_GRID_UNIT_LEN80];    /* grid value units */
       char   title[GMT_GRID_TITLE_LEN80];     /* name of data set */
       char   command[GMT_GRID_COMMAND_LEN320];/* name of generating command */
       char   remark[GMT_GRID_REMARK_LEN160];  /* comments re this data set */
       /* == End of "untouchable" header.       */

       /* ---- Variables "hidden" from the API ----
        * This section is flexible.  It is not copied to any grid header
        * or stored in any file. It is considered private */
       unsigned int type;               /* Grid format */
       unsigned int bits;               /* Bits per value (e.g., 32 for ints/floats; 8 for bytes) */
       unsigned int complex_mode;       /* 0 = normal, GMT_GRID_IS_COMPLEX_REAL = real part of complex
										   grid, GMT_GRID_IS_COMPLEX_IMAG = imag part of complex grid */
       unsigned int mx, my;             /* Actual dimensions of the grid in memory, allowing for the padding */
       size_t       nm;                 /* Number of data items in this grid (n_columns * n_rows) [padding is excluded] */
       size_t       size;               /* Actual number of items (not bytes) required to hold this grid (= mx * my) */
       size_t       n_alloc;            /* Bytes allocated for this grid */
       unsigned int trendmode;          /* Holds status for detrending of grids. 0 if not detrended, 1 if mean, 2 if mid-value, and 3 if LS plane removed */
       unsigned int arrangement;        /* Holds status for complex grid as how the read/imag is placed in the grid (interleaved, R only, etc.) */
       unsigned int n_bands;            /* Number of bands [1]. Used with IMAGE containers and macros to get ij index from row,col, band */
       unsigned int pad[4];             /* Padding on west, east, south, north sides [2,2,2,2] */
       unsigned int BC[4];              /* Boundary condition applied on each side via pad [0 = not set, 1 = natural, 2 = periodic, 3 = data] */
       unsigned int grdtype;            /* 0 for Cartesian, > 0 for geographic and depends on 360 periodicity [see GMT_enum_grdtype above] */
       char name[GMT_GRID_NAME_LEN256]; /* Actual name of the file after any ?<varname> and =<stuff> has been removed */
       char varname[GMT_GRID_VARNAME_LEN80];/* NetCDF: variable name */
       const char  *ProjRefPROJ4;       /* To store a referencing system string in PROJ.4 format */
       const char  *ProjRefWKT;         /* To store a referencing system string in WKT format */
       int          row_order;          /* NetCDF: k_nc_start_south if S->N, k_nc_start_north if N->S */
       int          z_id;               /* NetCDF: id of z field */
       int          ncid;               /* NetCDF: file ID */
       int          xy_dim[2];          /* NetCDF: dimension order of x and y; normally {1, 0} */
       size_t       t_index[3];         /* NetCDF: index of higher coordinates */
       size_t       data_offset;        /* NetCDF: distance from the beginning of the in-memory grid */
       unsigned int stride;             /* NetCDF: distance between two rows in the in-memory grid */
       float        nan_value;          /* Missing value as stored in grid file */
       double       xy_off;             /* 0.0 (registration == GMT_GRID_NODE_REG) or 0.5 ( == GMT_GRID_PIXEL_REG) */
       double       r_inc[2];           /* Reciprocal incs, i.e. 1/inc */
       char         flags[4];           /* Flags used for ESRI grids */
       char        *pocket;             /* GDAL: A working variable handy to transmit info between funcs e.g. +b<band_info> to gdalread */
       char         mem_layout[4];      /* 3 or 4 char code T|B R|C S|R|S (grd) or B|L|P + A|a (img) describing mem layout and interleaving */
       double       bcr_threshold;      /* sum of cardinals must >= threshold in bilinear; else NaN */
       unsigned int bcr_interpolant;    /* Interpolation function used (0, 1, 2, 3) */
       unsigned int bcr_n;              /* Width of the interpolation function */
       unsigned int nxp;                /* if X periodic, nxp > 0 is the period in pixels  */
       unsigned int nyp;                /* if Y periodic, nxp > 0 is the period in pixels  */
       unsigned int no_BC;              /* If true we skip BC stuff entirely */
       unsigned int gn;                 /* true if top    edge will be set as N pole  */
       unsigned int gs;                 /* true if bottom edge will be set as S pole  */
       unsigned int is_netcdf4;         /* true if netCDF-4/HDF5 format */
       size_t       z_chunksize[2];     /* chunk size (lat,lon) */
       unsigned int z_shuffle;          /* if shuffle filter is turned on */
       unsigned int z_deflate_level;    /* if deflate filter is in use */
       unsigned int z_scale_autoadust;  /* if z_scale_factor should be auto-detected */
       unsigned int z_offset_autoadust; /* if z_add_offset should be auto-detected */
                                        /* xy_*[] is separate settings for GMT_IN and GMT_OUT */
       unsigned int xy_adjust[2];       /* 1 if +u<unit> was parsed and scale set, 3 if xy has been adjusted, 0 otherwise */
       unsigned int xy_mode[2];         /* 1 if +U<unit> was parsed, 0 otherwise */
       unsigned int xy_unit[2];         /* Unit enum specified via +u<unit> */
       double       xy_unit_to_meter[2];/* Scale, given xy_unit, to convert xy from <unit> to meters */
   };

GMT image
~~~~~~~~~

An image is similar to a grid except it may have more than one layer (i.e., band).
It is represented by a :ref:`GMT_IMAGE <struct-image>` structure that consists of the
:ref:`GMT_GRID_HEADER <struct-gridheader>` structure and an char array ``data`` that
contains the image values.  The type of the array is determined by the value of ``type``.

.. _struct-image:

.. code-block:: c

  struct GMT_IMAGE {
      enum GMT_enum_type      type;             /* Data type, e.g. GMT_FLOAT */
      int                    *colormap;         /* Array with color lookup values */
      int                     n_indexed_colors; /* Number of colors in a color-mapped image */
      struct GMT_GRID_HEADER *header;           /* Pointer to full GMT header for the image */
      unsigned char          *data;             /* Pointer to actual image */
      /* ---- Variables "hidden" from the API ---- */
      uint64_t                id;               /* The internal number of the data set */
      unsigned int            alloc_level;      /* Level of initial allocation */
      enum GMT_enum_alloc     alloc_mode;       /* Allocation info [0] */
      const char             *color_interp;	/* Color interpretation name */
      double                 *x, *y;            /* Vector of coordinates */
  };

CPT palette table
~~~~~~~~~~~~~~~~~

A CPT is represented by a :ref:`GMT_PALETTE <struct-palette>` structure that contains several
items, such as a :ref:`GMT_LUT <struct-lut>` structure ``data`` that
contains the color information per interval.  The background, foreground and Nan-color values have
colors specified by the :ref:`GMT_BFN <struct-bnf>` array structure ``bfn``.  As each actual
color may be specified in different ways, including as an image, each color slice is represented by
the :ref:`GMT_FILL <struct-fill>` structure.

.. _struct-palette:

.. code-block:: c

   struct GMT_PALETTE {		/* Holds all pen, color, and fill-related parameters */
       /* Variables we document for the API: */
       unsigned int          n_headers;          /* Number of CPT header records (0 if no header) */
       unsigned int          n_colors;           /* Number of colors in CPT lookup table */
       unsigned int          mode;               /* Flags controlling use of BFN colors */
       struct GMT_LUT       *data;               /* CPT lookup data read by GMT_read_cpt */
       struct GMT_BFN        bfn[3];             /* Structures with back/fore/nan fills */
       char                **header;             /* Array with all CPT header records, if any) */
       /* ---- Variables "hidden" from the API ---- */
       uint64_t              id;                 /* The internal number of the data set */
       enum GMT_enum_alloc   alloc_mode;         /* Allocation mode [GMT_ALLOCATED_BY_GMT] */
       unsigned int          alloc_level;        /* The level it was allocated at */
       unsigned int          model;              /* RGB, HSV, CMYK */
       unsigned int          is_gray;            /* true if only grayshades are needed */
       unsigned int          is_bw;              /* true if only black and white are needed */
       unsigned int          is_continuous;      /* true if continuous color tables have been given */
       unsigned int          has_pattern;        /* true if CPT contains any patterns */
       unsigned int          skip;               /* true if current z-slice is to be skipped */
       unsigned int          categorical;        /* true if CPT applies to categorical data */
       unsigned int          z_adjust[2];        /* 1 if +u<unit> was parsed and scale set, 3 if z has been adjusted, 0 otherwise */
       unsigned int          z_mode[2];          /* 1 if +U<unit> was parsed, 0 otherwise */
       unsigned int          z_unit[2];          /* Unit enum specified via +u<unit> */
       double                z_unit_to_meter[2]; /* Scale, given z_unit, to convert z from <unit> to meters */
   };

The full definition of the ``GMT_LUT`` structure.

.. _struct-lut:

.. code-block:: c

   struct GMT_LUT {         /* For back-, fore-, and nan-colors */
       double                z_low, z_high, i_dz;
       double                rgb_low[4], rgb_high[4], rgb_diff[4];
       double                hsv_low[4], hsv_high[4], hsv_diff[4];
       unsigned int          annot;              /* 1 for Lower, 2 for Upper, 3 for Both */
       unsigned int          skip;               /* true means skip this slice */
       struct GMT_FILL      *fill;               /* For patterns instead of color */
       char                 *label;              /* For non-number labels */
   };

The full definition of the ``GMT_BFN`` structure:

.. _struct-bnf:

.. code-block:: c

   struct GMT_BFN {   /* For back-, fore-, and nan-colors */
       double                rgb[4];             /* Red, green, blue, and alpha */
       double                hsv[4];             /* Hue, saturation, value, alpha */
       unsigned int          skip;               /* true means skip this slice */
       struct GMT_FILL      *fill;               /* For patterns instead of color */
   };

The full definition of the ``GMT_FILL`` structure.  Note: Not part of the GMT API:

.. _struct-fill:

.. code-block:: c

   struct GMT_FILL {        /*! Holds fill attributes */
       double                rgb[4];             /* Chosen color if no pattern + Transparency 0-1 [0 = opaque] */
       double                f_rgb[4], b_rgb[4]; /* Colors applied to unset and set bits in 1-bit image */
       bool                  use_pattern;        /* true if pattern rather than rgb is set */
       int                   pattern_no;         /* Number of a predefined pattern, or -1 if not set */
       unsigned int          dpi;                /* Desired dpi of image building-block if use_pattern is true */
       char                  pattern[GMT_BUFSIZ];/* Full filename of user-defined raster pattern */
   };


PostScript text
~~~~~~~~~~~~~~~

Bulk PostScript is represented by a :ref:`GMT_POSTSCRIPT <struct-postscript>` structure that contains
``data`` that points to the text array containing ``n_bytes`` characters of raw PostScript code.  The
``mode`` parameter reflects the status of the PostScript document.

.. _struct-postscript:

.. code-block:: c

   struct GMT_POSTSCRIPT {	/* Single container for a chunk of PostScript code */
       /* Variables we document for the API: */
       unsigned int n_headers;          /* Number of PostScript header records (0 if no header) */
       size_t n_alloc;                  /* Length of array allocated so far */
       size_t n_bytes;                  /* Length of data array so far */
       unsigned int mode;               /* Bit-flag for header (1) and trailer (2) */
       char *data;                      /* Pointer to PostScript code */
       char **header;                   /* Array with all PostScript header records, if any) */
       /* ---- Variables "hidden" from the API ---- */
       uint64_t id;                     /* The internal number of the data set */
       unsigned int alloc_level;        /* The level it was allocated at */
       enum GMT_enum_alloc alloc_mode;  /* Allocation mode [GMT_ALLOC_INTERNALLY] */
   };

Text set
~~~~~~~~

Each text set is represented by a :ref:`GMT_TEXTSET <struct-textset>` that consists of one or more text
tables represented by a :ref:`GMT_TEXTTABLE <struct-texttable>`, and each table consists of one or more
segments represented by a :ref:`GMT_TEXTSEGMENT <struct-textsegment>`, and each segment contains one or
more rows of strings.

.. _struct-textset:

.. code-block:: c

   struct GMT_TEXTSET {	/* Single container for an array of GMT text tables (files) */
       /* Variables we document for the API: */
       uint64_t               n_tables;     /* The total number of tables (files) contained */
       uint64_t               n_segments;   /* The total number of segments across all tables */
       uint64_t               n_records;    /* The total number of data records across all tables */
       struct GMT_TEXTTABLE **table;        /* Pointer to array of tables */
       /* ---- Variables "hidden" from the API ---- */
       uint64_t               id;           /* The internal number of the data set */
       size_t                 n_alloc;      /* The current allocation length of tables */
       unsigned int           geometry;     /* The geometry of this dataset */
       unsigned int           alloc_level;  /* The level it was allocated at */
       enum GMT_enum_dest     io_mode;      /*-1 means write OGR format (requires proper -a),
                                               0 means write everything to one destination [Default],
                                               1 means use table->file[GMT_OUT] to write separate table,
                                               2 means use segment->file[GMT_OUT] to write separate segments.
                                               3 is same as 2 but with no filenames we create filenames
                                                 from tbl and seg numbers */
       enum GMT_enum_alloc    alloc_mode;   /* Allocation mode [GMT_ALLOCATED_BY_GMT] */
       char                  *file[2];      /* Name of file or source [0 = in, 1 = out] */
   };

The full definition of the ``GMT_TEXTTABLE`` structure:

.. _struct-texttable:

.. code-block:: c

   struct GMT_TEXTTABLE {  /* To hold an array of text segment structures and header information in one container */
       /* Variables we document for the API: */
       unsigned int n_headers;   /* Number of file header records (0 if no header) */
       uint64_t n_segments;      /* Number of segments in the array */
       uint64_t n_records;       /* Total number of data records across all segments */
       char   **header;          /* Array with all file header records, if any) */
       struct GMT_TEXTSEGMENT **segment; /* Pointer to array of segments */
       /* ---- Variables "hidden" from the API ---- */
       uint64_t id;              /* The internal number of the table */
       size_t   n_alloc;         /* The current allocation length of segments */
       enum GMT_enum_write mode; /* 0 = output table, 1 = output header only, 2 = skip table */
       char    *file[2];         /* Name of file or source [0 = in, 1 = out] */
   };

The full definition of the ``GMT_TEXTSEGMENT`` structure:

.. _struct-textsegment:

.. code-block:: c

   struct GMT_TEXTSEGMENT {      /* For holding segment text records in memory */
       /* Variables we document for the API: */
       uint64_t n_rows;          /* Number of rows in this segment */
       char   **data;            /* Array of text records */
       char    *label;           /* Label string (if applicable) */
       char    *header;          /* Segment header (if applicable) */
       /* ---- Variables "hidden" from the API ---- */
       uint64_t id;              /* The internal number of the table */
       enum GMT_enum_write mode; /* 0 = output segment, 1 = output header only, 2 = skip segment */
       size_t   n_alloc;         /* Number of rows allocated for this segment */
       char    *file[2];         /* Name of file or source [0 = in, 1 = out] */
       char   **tvalue;          /* The values of the OGR/GMT aspatial fields */
   };

Matrix
~~~~~~

User matrices are represented by a :ref:`GMT_MATRIX <struct-matrix>` structure that contains
``data`` that points to an array of size ``n_columns`` by ``n_rows``.  The
``type`` indicates the memory type of the matrix, which is represented
by the :ref:`GMT_UNIVECTOR <struct-univector>` union.

.. _struct-matrix:

.. code-block:: c

  struct GMT_MATRIX {
      uint64_t             n_rows;        /* Number of rows in the matrix */
      uint64_t             n_columns;     /* Number of columns in the matrix */
      uint64_t             n_layers;      /* Number of layers in a 3-D matrix */
      enum GMT_enum_fmt    shape;         /* 0 = C (rows) and 1 = Fortran (cols) */
      enum GMT_enum_reg    registration;  /* 0 for gridline and 1 for pixel registration  */
      size_t               dim;           /* Allocated length of longest C or Fortran dim */
      size_t               size;          /* Byte length of data */
      enum GMT_enum_type   type;          /* Data type, e.g. GMT_FLOAT */
      double               range[6];      /* Contains xmin/xmax/ymin/ymax[/zmin/zmax] */
      union GMT_UNIVECTOR  data;          /* Union with pointer to actual matrix of the chosen type */
      char command[GMT_GRID_COMMAND_LEN320]; /* name of generating command */
      char remark[GMT_GRID_REMARK_LEN160];   /* comments re this data set */
      /* ---- Variables "hidden" from the API ---- */
      uint64_t             id;            /* The internal number of the data set */
      unsigned int         alloc_level;   /* The level it was allocated at */
      enum GMT_enum_alloc  alloc_mode;    /* Allocation mode [GMT_ALLOCATED_BY_GMT] */
  };

Vectors
~~~~~~~

User vectors are represented by a :ref:`GMT_VECTOR <struct-vector>` structure that contains
``data`` that points to an array of ``n_columns`` individual vectors.  The
``type`` array indicates the memory type of each vector.  Each vector is represented
by the :ref:`GMT_UNIVECTOR <struct-univector>` union which can accommodate any data type.

.. _struct-vector:

.. code-block:: c

  struct GMT_VECTOR {
      uint64_t             n_columns;     /* Number of vectors */
      uint64_t             n_rows;        /* Number of rows in each vector */
      enum GMT_enum_reg    registration;  /* 0 for gridline and 1 for pixel registration */
      enum GMT_enum_type  *type;          /* Array with data type for each vector */
      union GMT_UNIVECTOR *data;          /* Array with unions for each column */
      double               range[2];      /* The min and max limits on t-range (or 0,0) */
      char command[GMT_GRID_COMMAND_LEN320]; /* name of generating command */
      char remark[GMT_GRID_REMARK_LEN160];   /* comments re this data set */
      /* ---- Variables "hidden" from the API ---- */
      uint64_t             id;            /* An identification number */
      unsigned int         alloc_level;   /* Level of initial allocation */
      enum GMT_enum_alloc  alloc_mode;    /* Determines if we may free the vectors or not */
  };

The full definition of the ``GMT_UNIVECTOR`` union that holds a pointer to any array or matrix type:

.. _struct-univector:

.. code-block:: c

  union GMT_UNIVECTOR {
      uint8_t  *uc1;       /* Pointer for unsigned 1-byte array */
      int8_t   *sc1;       /* Pointer for signed 1-byte array */
      uint16_t *ui2;       /* Pointer for unsigned 2-byte array */
      int16_t  *si2;       /* Pointer for signed 2-byte array */
      uint32_t *ui4;       /* Pointer for unsigned 4-byte array */
      int32_t  *si4;       /* Pointer for signed 4-byte array */
      uint64_t *ui8;       /* Pointer for unsigned 8-byte array */
      int64_t  *si8;       /* Pointer for signed 8-byte array */
      float    *f4;        /* Pointer for float array */
      double   *f8;        /* Pointer for double array */
  };


Appendix B: GMT constants
-------------------------

To increase readability we have encoded many simple integer constants as named
enum.  These are listed in the tables below and used as flags to various API
functions.

.. _tbl-types:

    +--------------+------------------------------------------+
    | constant     | description                              |
    +==============+==========================================+
    | GMT_CHAR     | int8_t, 1-byte signed integer type       |
    +--------------+------------------------------------------+
    | GMT_UCHAR    | int8_t, 1-byte unsigned integer type     |
    +--------------+------------------------------------------+
    | GMT_SHORT    | int16_t, 2-byte signed integer type      |
    +--------------+------------------------------------------+
    | GMT_USHORT   | uint16_t, 2-byte unsigned integer type   |
    +--------------+------------------------------------------+
    | GMT_INT      | int32_t, 4-byte signed integer type      |
    +--------------+------------------------------------------+
    | GMT_UINT     | uint32_t, 4-byte unsigned integer type   |
    +--------------+------------------------------------------+
    | GMT_LONG     | int64_t, 8-byte signed integer type      |
    +--------------+------------------------------------------+
    | GMT_ULONG    | uint64_t, 8-byte unsigned integer type   |
    +--------------+------------------------------------------+
    | GMT_FLOAT    | 4-byte data float type                   |
    +--------------+------------------------------------------+
    | GMT_DOUBLE   | 8-byte data float type                   |
    +--------------+------------------------------------------+

    The known data types in the GMT API.


.. [1]
   or via a very confusing and ever-changing myriad of low-level library
   functions for bold programmers.

.. [2]
   Currently, C/C++, FORTRAN, MATLAB and Julia are being tested.

.. [3]
   At the moment, GMT does not have native support for 3-D grids.

.. [4]
   This may change in later releases.

.. [5]
   However, there is no thread-support yet, so you will need to manage your
   own threads.

.. ------------------------------------- Examples code -------------------

.. |ex_resource_init| raw:: html

   <a href="#openModal">Example</a>
   <div id="openModal" class="modalDialog">
    <div>
        <a href="#close" title="Close" class="close">X</a>
        <h2>Resource initialization example</h2>
        <p>
        </p>
    </div>
   </div>