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

matlab-wrapper.rst 11 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  1. :tocdepth: 4
  2. :orphan:
  3. .. set default highlighting language for this document:
  4. .. highlight:: c
  5. **The Generic Mapping Tools**
  6. **The MATLAB Interface**
  7. **Pål (Paul) Wessel**
  8. **SOEST, University of Hawai'i at Manoa**
  9. **Joaquim F. Luis**
  10. **Universidade do Algarve, Faro, Portugal**
  11. Introduction
  12. ============
  13. The GMT MATLAB interface makes it possible to access all GMT modules from MATLAB.
  14. Users of MATLAB can write MATLAB scripts that call upon GMT modules to do any of the
  15. things GMT normally can do, and return the results (grids, data-tables, CPTs, text-files,
  16. and even final images via :doc:`psconvert`) to MATLAB variables. MATLAB matrices can be given
  17. as input to GMT modules. Examples below will give you the general idea.
  18. Installing
  19. ==========
  20. Windows
  21. -------
  22. The Windows installers come already with the gmtmex.mexw64|32 and gmt.m files necessary run the MEX.
  23. Only make sure that the GMT binary dir is either in the Windows path (the installer does that for you)
  24. and in the MATLAB path (you have to do it yourself).
  25. If you want to (re)build the MEX file yourself, see the *compile_mex.bat* in the source GitHub repository.
  26. OS X
  27. ----
  28. We have successfully built the MATLAB interface under OS X. However, due to the way MATLAB handles shared libraries
  29. it is a delicate process, with several caveats. This may change over time as we work with MathWorks to straighten out the
  30. kinks. The following works:
  31. #. Install the GMT OS X Bundle
  32. #. Run the gmt_prepmex.sh script in the bundle's share/tools directory. This will duplicate
  33. the GMT 5.3 installation into /opt/gmt and re-baptize all the shared libraries.
  34. #. Use gmtswitch to make /opt/gmt the current active GMT version
  35. #. Checkout the gmt-mex project via subversion into some directory, i.e.,
  36. git clone https://github.com/GenericMappingTools/gmtmex.git
  37. #. In gmt-mex/trunk, run autoconf then configure --enable-matlab (and maybe --enable-debug) is you
  38. can help debug things.
  39. #. Run make which builds the gmtmex.mexmaci64. This executable is accessed by the gmt.m script.
  40. #. Set your MATLAB path so these two can be found (or copy them to a suitable directory).
  41. #. Make sure your gmt.conf file has the entry GMT_CUSTOM_LIBS=/opt/gmt/lib/gmt/plugins/supplements.so.
  42. You can also build your own bundle (see CMakeLists.txt in main GMT directory). The above works
  43. with UNIX installations from fink or HomeBrew but fails for us if under MacPorts (then, MATLAB
  44. will complain about wrong shared HDF5 library and we crash).
  45. If you wish to help debug in XCode then see the gmt-mex wiki for more details. While the latest
  46. 2016a MATLAB version works with XCode 7, earlier versions may require 6.4 and you will have
  47. to install the older Xcode version.
  48. We used the 2016b MATLAB version to build the interface but 2015a,b should also work. Older
  49. versions may also work but we have not attempted this since we only have access to these three.
  50. Unix/Linux)
  51. -----------
  52. Preliminary experiments indicate we will have to fight the shared library dilemma here as well.
  53. Volunteers on Linux wishing to run the GMT MATLAB interface are needed to make progress.
  54. Using
  55. =====
  56. The MATLAB wrapper was designed to work in a way the closest as possible to the command line version
  57. and yet to provide all the facilities of the MATLAB IDE (the ML command line desktop). In this sense,
  58. all **GMT** options are put in a single text string that is passed, plus the data itself when it applies,
  59. to the ``gmt()`` command. For example to reproduce the CookBook example of an Hemisphere map using a
  60. Azimuthal projection
  61. .. code-block:: none
  62. gmt('coast -Rg -JA280/30/3.5i -Bg -Dc -A1000 -Gnavy -P > GMT_lambert_az_hemi.ps')
  63. but that is not particularly interesting as after all we could do the exact same thing on the a shell
  64. command line. Things start to get interesting when we can send data *in* and *out* from MATLAB to
  65. **GMT**. So, consider the following example
  66. .. code-block:: none
  67. t = rand(100,3) * 150;
  68. G = gmt('surface -R0/150/0/150 -I1', t);
  69. Here we just created a random data *100x3* matrix and told **GMT** to grid it using it's program
  70. *surface*. Note how the syntax follows closely the standard usage but we sent the data to be
  71. interpolated (the *t* matrix) as the second argument to the ``gmt()`` function. And on return we
  72. got the *G* variable that is a structure holding the grid and it's metadata. See the
  73. :ref:`grid struct <grid-struct>` for the details of its members.
  74. Imagining that we want to plot that random data art, we can do it with a call to *grdimage*\ , like
  75. .. code-block:: none
  76. gmt('grdimage -JX8c -Ba -P -Cblue,red > crap_img.ps', G)
  77. Note that we now sent the *G grid* as argument instead of the **-G**\ *gridname* that we would have
  78. used in the command line. But for readability we could well had left the **-G** option in command string. E.g:
  79. .. code-block:: none
  80. gmt('grdimage -JX8c -Ba -P -Cblue,red -G > crap_img.ps', G)
  81. While for this particular case it makes no difference to use or not the **-G**, because there is **only**
  82. one input, the same does not hold true when we have more than one. For example, we can run the same example
  83. but compute the CPT separately.
  84. .. code-block:: none
  85. cpt = gmt('grd2cpt -Cblue,red', G);
  86. gmt('grdimage -JX8c -Ba -P -C -G > crap_img.ps', G, cpt)
  87. Now we had to explicitly write the **-C** & **-G** (well, actually we could have omitted the **-G** because
  88. it's a mandatory input but that would make the things more confusing). Note also the order of the input data variables.
  89. It is crucial that any *required* (primary) input data objects (for grdimage that is the grid) are given before
  90. any *optional* (secondary) input data objects (here, that is the CPT object). The same is true for modules that
  91. return more than one item: List the required output object first followed by optional ones.
  92. To illustrate another aspect on the importance of the order of input data let us see how to plot a sine curve
  93. made of colored filled circles.
  94. .. code-block:: none
  95. x = linspace(-pi, pi)'; % The *xx* var
  96. seno = sin(x); % *yy*
  97. xyz = [x seno seno]; % Duplicate *yy* so that it can be colored
  98. cpt = gmt('makecpt -T-1/1/0.1'); % Create a CPT
  99. gmt('plot -R-3.2/3.2/-1.1/1.1 -JX12c -Sc0.1c -C -P -Ba > seno.ps', xyz, cpt)
  100. The point here is that we had to give *xyz, cpt* and not *cpt, xyz* (which would error) because optional input data
  101. associated with an option letter **always comes after the required input**.
  102. To plot text strings we send in the input data wrapped in a cell array. Example:
  103. .. code-block:: none
  104. lines = {'5 6 Some label', '6 7 Another label'};
  105. gmt('text -R0/10/0/10 -JM6i -Bafg -F+f18p -P > text.ps', lines)
  106. and we get back text info in cell arrays as well. Using the *G* grid computed above we can run *gmtinfo* on it
  107. .. code-block:: none
  108. info = gmt('info', G)
  109. At the end of an **GMT** session work we call the internal functions that will do the house keeping of
  110. freeing no longer needed memory. We do that with this command:
  111. .. code-block:: none
  112. gmt('destroy')
  113. So that's basically how it works. When numeric data have to be sent *in* to **GMT** we use
  114. MATLAB variables holding the data in matrices or structures or cell arrays, depending on data type. On
  115. return we get the computed result stored in variables that we gave as output arguments.
  116. Things only complicate a little more for the cases where we can have more than one *input* or
  117. *output* arguments, since the order or the arguments matter (Remember the rule: primary first, secondary second).
  118. The file *gallery.m*, that reproduces the examples in the Gallery section of the GMT
  119. documentation, has many (not so trivial) examples on usage of the MEX GMT API.
  120. .. _grid-struct:
  121. .. code-block:: none
  122. proj4 % Projection string in PROJ4 syntax (Optional)
  123. wkt % Projection string in WKT syntax (Optional)
  124. range % 1x6 vector with [x_min x_max y_min y_max z_min z_max]
  125. inc % 1x2 vector with [x_inc y_inc]
  126. registration % Registration type: 0 -> Grid registration; 1 -> Pixel registration
  127. nodata % The value of nodata
  128. pad % A scalar pad. Optional and only when direction is to GMT. (new in 1.1)
  129. title % Title (Optional)
  130. comment % Remark (Optional)
  131. command % Command used to create the grid (Optional)
  132. datatype % 'float' or 'double'
  133. x % [1 x n_columns] vector with XX coordinates
  134. y % [1 x n_rows] vector with YY coordinates
  135. z % [n_rows x n_columns] grid array
  136. x_unit % Units of XX axis (Optional)
  137. y_unit % Units of YY axis (Optional)
  138. z_unit % Units of ZZ axis (Optional)
  139. layout % A three character string describing the image memory layout
  140. Definition of the *grid structure* that holds a grid and its metadata.
  141. .. _img-struct:
  142. .. code-block:: none
  143. proj4 % Projection string in PROJ4 syntax (Optional)
  144. wkt % Projection string in WKT syntax (Optional)
  145. range % 1x6 vector with [x_min x_max y_min y_max z_min z_max]
  146. inc % 1x2 vector with [x_inc y_inc]
  147. registration % Registration type: 0 -> Grid registration; 1 -> Pixel registration [Default]
  148. nodata % The value of nodata
  149. pad % A scalar pad (optional). Use only when direction is to GMT and Image will be projected ([2]) (new in 1.1)
  150. title % Title (Optional)
  151. comment % Remark (Optional)
  152. command % Command used to create the image (Optional)
  153. datatype % 'uint8' or 'int8' (needs checking)
  154. x % [1 x n_columns] vector with XX coordinates
  155. y % [1 x n_rows] vector with YY coordinates
  156. image % [n_rows x n_columns] image array
  157. x_unit % Units of XX axis (Optional)
  158. y_unit % Units of YY axis (Optional)
  159. z_unit % Units of ZZ axis (Optional)
  160. colormap % A color palette structure
  161. alpha % A [n_rows x n_columns] alpha array
  162. layout % A four character string describing the image memory layout
  163. Definition of the *image structure* that holds an image and its metadata.
  164. .. _cpt-struct:
  165. .. code-block:: none
  166. colormap % A [ncolors x 3] matrix with colorvalues in [0-1] range
  167. alpha % A [ncolors x 1] vector with transparency (alpha) values in [0-1] range (optional)
  168. range % A [ncolors x 2] matrix with z_low z_high for each 'color' interval
  169. minmax % A 2 elements vector with [z_min z_max]
  170. bnf % A [3 x 3] matrix with color values in [0-1] range for background, foreground, and NaN-nodes
  171. depth % Depth of the CPT (1, 8, 24)
  172. hinge % The z-value for split colormaps [NaN means no hinge]
  173. cpt %
  174. model % Either RGB oy CMYK
  175. comment % Remark (Optional)
  176. Definition of the *CPT structure* that holds the color palette.
  177. .. _PS-struct:
  178. .. code-block:: c
  179. postscript % A string with all the PostScript code as text
  180. length % Number of bytes in the string
  181. mode % 1 means has header only, 2 means has trailer only, 3 means complete
  182. comment % Remark (Optional)
  183. Definition of the *PS structure* that holds the PostScript plot.
Tip!

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

Comments

Loading...