Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API case for external data passed as vectors #6521

Merged
merged 5 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/gmt_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -3980,9 +3980,11 @@ GMT_LOCAL struct GMT_DATASET * gmtapi_import_dataset (struct GMTAPI_CTRL *API, i
if ((V_obj = S_obj->resource) == NULL) {
gmt_M_free (GMT, D_obj); return_null (API, GMT_PTR_IS_NULL);
}
if (V_obj->type[0] != GMT_DOUBLE) {
GMT_Report (API, GMT_MSG_ERROR, "Only double-precision vectors can be passed via reference to datasets\n");
gmt_M_free (GMT, D_obj); return_null (API, GMT_NOT_A_VALID_TYPE);
for (col = 0; col < V_obj->n_columns; col++) {
if (V_obj->type[col] != GMT_DOUBLE) {
GMT_Report (API, GMT_MSG_ERROR, "Only double-precision vectors can be passed via reference to datasets\n");
gmt_M_free (GMT, D_obj); return_null (API, GMT_NOT_A_VALID_TYPE);
}
}
VH = gmt_get_V_hidden (V_obj);
if (GMT->common.q.mode == GMT_RANGE_ROW_IN || GMT->common.q.mode == GMT_RANGE_DATA_IN)
Expand All @@ -4009,7 +4011,7 @@ GMT_LOCAL struct GMT_DATASET * gmtapi_import_dataset (struct GMTAPI_CTRL *API, i
else
col_pos = col_pos_out = col; /* Just goto that column */
S->data[col_pos_out] = V_obj->data[col_pos].f8;
SH->alloc_mode[col_pos_out] = VH->alloc_mode[col]; /* Inherit from what we got */
SH->alloc_mode[col_pos_out] = GMT_ALLOC_EXTERNALLY; /* Not this objects job to free what was passed in by reference */
}
DH = gmt_get_DD_hidden (D_obj);
if (smode) S->text = V_obj->text;
Expand Down Expand Up @@ -7646,7 +7648,7 @@ GMT_LOCAL int gmtapi_init_import (struct GMTAPI_CTRL *API, enum GMT_enum_family
return_value (API, API->error, GMT_NOTSET); /* Failure to register */
}
n_reg++; /* Count of new items registered */
gmt_M_free (API->GMT, wesn);
if (API->GMT->common.R.active[RSET]) gmt_M_free (API->GMT, wesn);
if (first_ID == GMT_NOTSET) first_ID = object_ID; /* Found our first ID */
if ((item = gmtlib_validate_id (API, family, object_ID, GMT_IN, GMTAPI_MODULE_INPUT)) == GMT_NOTSET)
return_value (API, API->error, GMT_NOTSET); /* Some internal error... */
Expand Down
28 changes: 28 additions & 0 deletions src/testapi_vector_times.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "gmt.h"
int main () {
void *API = NULL; /* The API control structure */
struct GMT_VECTOR *V = NULL; /* Structure to hold input dataset as vectors */
char input[GMT_VF_LEN] = {""}; /* String to hold virtual input filename */
char args[128] = {""}; /* String to hold module command arguments */

uint64_t dim[4] = {2, 4, 1, 0};
char *x[4] = {"2021-03-01", "2021-03-02", "2021-03-03", "2021-03-04"};
double y[4] = {0.0, 1.0, 2.0, 3.0};

/* Initialize the GMT session */
API = GMT_Create_Session ("test", 2U, GMT_SESSION_EXTERNAL, NULL);
/* Create a dataset */
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);
/**/
GMT_Put_Vector(API, V, 0, GMT_TEXT, x);
GMT_Put_Vector(API, V, 1, GMT_DOUBLE, y);
/* Associate our data table with a virtual file */
GMT_Open_VirtualFile (API, GMT_IS_DATASET|GMT_VIA_VECTOR, GMT_IS_POINT, GMT_IN|GMT_IS_REFERENCE, V, input);
/* Prepare the module arguments */
sprintf (args, "%s -JX10c/5c -R2021-03-01/2021-03-04/-0.1/30 -Baf -BWSen -P", input);
/* Call the psxy module */
GMT_Call_Module (API, "psxy", GMT_MODULE_CMD, args);
GMT_Close_VirtualFile (API, input);
/* Destroy the GMT session */
if (GMT_Destroy_Session (API)) return EXIT_FAILURE;
};
6 changes: 6 additions & 0 deletions test/api/apivectortimes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
#
# Test the C API for plotting datetimes
# See https://github.com/GenericMappingTools/gmt/pull/6521
ps=apivectortimes.ps
testapi_vector_times > $ps
6 changes: 3 additions & 3 deletions test/baseline/api.dvc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
outs:
- md5: 5bd99a34dd32a31bbfeb4336a9e445b7.dir
size: 3017982
nfiles: 15
- md5: b985dda50abc7f6dcceb4a8848e0a83a.dir
size: 3041741
nfiles: 16
path: api
1 change: 1 addition & 0 deletions test/gmtest.in
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ for apiprog in \
testapi_matrix_as_grid \
testapi_vector_strings \
testapi_vector_plot \
testapi_vector_times \
testapi_vector_io \
testapi_matrix_io \
testapi_matrix_360 \
Expand Down