Skip to content

Commit 10042f9

Browse files
github-actions[bot]seismanPaulWessel
authored
Update GMT_IS_REFERENCE and GMT_IS_DUPLICATE to allow duplicating strings (#3718) (#3777)
* Debug GMT_Put_Strings * SHift GMT_IS_DUPLICATE,REFERENCE and allow for string duplication GMT_Put_Strings only worked by reference. Now, we can accept a family argument such as GMT_IS_VECTOR|GMT_IS_DUPLICATE (or GMT_IS_REFERENCE which is the default) and it will duplicate the array string if requested. * Adjust these enums to allow future use in GMT_Put_Vector|Matrix * Update gmt_enum_dict.h * Possible clash with GMT_VIA_MODULE_INPUT * Update gmt_enum_dict.h * Update gmt_enum_dict.h and testapi_vector_strings2.c * Update api.rst * Update gmt_api.c * Update gmt_api.c * Add debug messages * check for directory * Update gmt_io.c Co-authored-by: Paul Wessel <[email protected]> Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Paul Wessel <[email protected]>
1 parent fe17ffb commit 10042f9

File tree

7 files changed

+146
-42
lines changed

7 files changed

+146
-42
lines changed

doc/rst/source/api.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,9 +1352,11 @@ array of text strings, one per row. This is done via
13521352

13531353
where ``family`` is either GMT_IS_VECTOR or GMT_IS_MATRIX, ``X`` is either a
13541354
:ref:`GMT_VECTOR <struct-vector>` or :ref:`GMT_MATRIX <struct-matrix>`, and
1355-
``array`` is the a pointer to your string array.
1355+
``array`` is the a pointer to your string array. You may add ``GMT_IS_DUPLICATE`` to
1356+
``family`` to indicate you want the array of strings to be duplicated; the default
1357+
is to just set a pointer to ``array``.
13561358

1357-
To extract the string array from an output vector or matrix container you will use
1359+
To access the string array from an output vector or matrix container you will use
13581360

13591361
.. _GMT_Get_Strings:
13601362

src/gmt_api.c

Lines changed: 77 additions & 35 deletions
Large diffs are not rendered by default.

src/gmt_enum_dict.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static struct GMT_API_DICT gmt_api_enums[GMT_N_API_ENUMS] = {
138138
{"GMT_IS_COL_FORMAT", 2},
139139
{"GMT_IS_COORD", 7},
140140
{"GMT_IS_DATASET", 0},
141-
{"GMT_IS_DUPLICATE", 3},
141+
{"GMT_IS_DUPLICATE", 16},
142142
{"GMT_IS_FDESC", 2},
143143
{"GMT_IS_FILE", 0},
144144
{"GMT_IS_GRID", 1},
@@ -155,7 +155,7 @@ static struct GMT_API_DICT gmt_api_enums[GMT_N_API_ENUMS] = {
155155
{"GMT_IS_POINT", 1},
156156
{"GMT_IS_POLY", 4},
157157
{"GMT_IS_POSTSCRIPT", 4},
158-
{"GMT_IS_REFERENCE", 4},
158+
{"GMT_IS_REFERENCE", 32},
159159
{"GMT_IS_ROW_FORMAT", 1},
160160
{"GMT_IS_STREAM", 1},
161161
{"GMT_IS_SURFACE", 8},

src/gmt_resources.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ enum GMT_enum_method {
120120
GMT_IS_FILE = 0, /* Entity is a filename */
121121
GMT_IS_STREAM = 1, /* Entity is an open stream */
122122
GMT_IS_FDESC = 2, /* Entity is an open file descriptor */
123-
GMT_IS_DUPLICATE = 3, /* Entity is a memory location that should be duplicated */
124-
GMT_IS_REFERENCE = 4, /* Entity is a memory location that should be referenced */
123+
GMT_IS_DUPLICATE = 16, /* Entity is a memory location that should be duplicated */
124+
GMT_IS_REFERENCE = 32, /* Entity is a memory location that should be referenced */
125125
GMT_IS_OUTPUT = 1024 /* When creating a resource as a container for output */
126126
};
127127

src/gmt_support.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8088,6 +8088,7 @@ struct GMT_PALETTE *gmt_get_palette (struct GMT_CTRL *GMT, char *file, enum GMT_
80888088
double noise;
80898089
struct GMT_PALETTE_HIDDEN *PH = NULL;
80908090

8091+
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "CPT argument %s understood to be a master table\n", file);
80918092
if (gmt_M_is_dnan (zmin) || gmt_M_is_dnan (zmax)) { /* Safety valve 1 */
80928093
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Passing zmax or zmin == NaN prevents automatic CPT generation!\n");
80938094
return (NULL);
@@ -8124,6 +8125,7 @@ struct GMT_PALETTE *gmt_get_palette (struct GMT_CTRL *GMT, char *file, enum GMT_
81248125
gmt_save_current_cpt (GMT, P, 0); /* Save for use by session, if modern */
81258126
}
81268127
else if (file) { /* Gave a CPT file */
8128+
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "CPT argument %s understood to be a regular CPT table\n", file);
81278129
P = GMT_Read_Data (GMT->parent, GMT_IS_PALETTE, GMT_IS_FILE, GMT_IS_NONE, GMT_READ_NORMAL, NULL, &file[first], NULL);
81288130
}
81298131
else

src/testapi_vector_strings2.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "gmt.h"
2+
#include <string.h>
3+
#include <stdlib.h>
4+
/*
5+
* Testing the use of user data provided via a GMT_VECTOR to pstext,
6+
* passing both numerical vectors and a string array.
7+
*/
8+
9+
/* Dimensions of the test dataset */
10+
#define NCOLS 3
11+
#define NROWS 2
12+
13+
int main () {
14+
void *API = NULL; /* The API control structure */
15+
struct GMT_VECTOR *V = NULL; /* Structure to hold input dataset as vectors */
16+
char input[GMT_VF_LEN] = {""}; /* String to hold virtual input filename */
17+
char args[128] = {""}; /* String to hold module command arguments */
18+
19+
uint64_t dim[4] = {NCOLS, NROWS, 1, 0}; /* ncols, nrows, nlayers, type */
20+
/* two data points */
21+
double x[2] = {5.0, 5.0};
22+
double y[2] = {3.0, 8.0};
23+
double angle[2] = {30.0, 60.0};
24+
char *strings[NROWS];
25+
26+
int i;
27+
for (i=0; i<NROWS; i++)
28+
strings[i] = (char *) malloc(sizeof(char)*128);
29+
30+
strcpy(strings[0], "ML 18p,1,blue First label");
31+
strcpy(strings[1], "MR 32p,2,red Second label");
32+
33+
/* Initialize the GMT session */
34+
API = GMT_Create_Session ("test", 2U, GMT_SESSION_EXTERNAL, NULL);
35+
/* Create a dataset */
36+
if ((V = GMT_Create_Data (API, GMT_IS_DATASET|GMT_VIA_VECTOR, GMT_IS_POINT, GMT_CONTAINER_ONLY, dim, NULL, NULL, 0, 0, NULL)) == NULL) return (EXIT_FAILURE);
37+
/* Hook the three vectors up to this container */
38+
GMT_Put_Vector(API, V, 0, GMT_DOUBLE, x);
39+
GMT_Put_Vector(API, V, 1, GMT_DOUBLE, y);
40+
GMT_Put_Vector(API, V, 2, GMT_DOUBLE, angle);
41+
/* Hook the user text array up to this container */
42+
GMT_Put_Strings(API, GMT_IS_VECTOR|GMT_IS_DUPLICATE, V, strings);
43+
44+
for (i=0; i<NROWS; i++) {
45+
free(strings[i]);
46+
}
47+
48+
/* Associate our data table with a virtual file */
49+
GMT_Open_VirtualFile (API, GMT_IS_DATASET|GMT_VIA_VECTOR, GMT_IS_POINT, GMT_IN, V, input);
50+
51+
/* Prepare the module arguments */
52+
sprintf (args, "%s -JX10c -R0/10/0/10 -Baf -F+a+j+f", input);
53+
/* Call the pstext module */
54+
GMT_Call_Module (API, "pstext", GMT_MODULE_CMD, args);
55+
GMT_Close_VirtualFile (API, input);
56+
/* Destroy the GMT session */
57+
if (GMT_Destroy_Session (API)) return EXIT_FAILURE;
58+
};

test/modern/imagepluscpt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
# Test automatic CPT scaling
33
gmt begin imagepluscpt ps
4-
gmt grdimage @earth_relief_05m -RMG+r2 -Cgeo -I+
4+
gmt grdimage @earth_relief_05m -RMG+r2 -Cgeo -I+d
55
gmt coast -Wthin -N1/thick,red -BWSne -B
66
gmt colorbar -DJTC -B
77
gmt end show

0 commit comments

Comments
 (0)