Skip to content

Improve the two-step grid reading when source is a matrix #3510

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

Merged
merged 11 commits into from
Jun 21, 2020
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
34 changes: 31 additions & 3 deletions src/gmt_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@ GMT_LOCAL int gmtapi_init_matrix (struct GMTAPI_CTRL *API, uint64_t dim[], doubl
M->type = (dim == NULL) ? GMT_DOUBLE : dim[3]; /* Use selected data type for export or default to double */
M->dim = (M->shape == GMT_IS_ROW_FORMAT) ? M->n_columns : M->n_rows;
M->registration = registration;
size = M->n_rows * M->n_columns * ((size_t)M->n_layers); /* Size in bytes of the initial matrix allocation */
size = M->n_rows * M->n_columns * ((size_t)M->n_layers); /* Size of the initial matrix allocation (number of elements) */
if ((mode & GMT_CONTAINER_ONLY) == 0) { /* Must allocate data memory */
struct GMT_MATRIX_HIDDEN *MH = gmt_get_M_hidden (M);
if (size) { /* Must allocate data matrix and possibly string array */
Expand Down Expand Up @@ -2814,6 +2814,7 @@ bool gmt_is_an_object (struct GMT_CTRL *GMT, void *ptr) {
}

/*! Determine if resource is a filename that has already been registered */
#if 0
GMT_LOCAL int gmtapi_memory_registered (struct GMTAPI_CTRL *API, enum GMT_enum_family family, unsigned int direction, void *resource) {
int object_ID = 0, item;
unsigned int module_input = (family & GMT_VIA_MODULE_INPUT); /* Are we dealing with a resource that is a module input? */
Expand All @@ -2825,6 +2826,31 @@ GMT_LOCAL int gmtapi_memory_registered (struct GMTAPI_CTRL *API, enum GMT_enum_f
if (module_input && direction == GMT_IN) API->object[item]->module_input = true; /* Flag this object as a module input resource */
return (object_ID); /* resource is a registered and valid item */
}
#endif

/*! Determine if resource is a filename that has already been registered */
GMT_LOCAL int gmtapi_memory_registered (struct GMTAPI_CTRL *API, enum GMT_enum_family family, unsigned int direction, char *filename) {
char SP, D, F, A, G, M;
int k, object_ID;
if (!gmt_M_file_is_memory (filename)) return GMT_NOTSET; /* If not a memory reference then there is no ID etc */
/* Name template: @GMTAPI@-S-D-F-A-G-M-###### where # is the 6-digit integer object code.
* S stands for P(rimary) or S(econdary) input or output object (command line is primary, files via options are secondary).
* D stands for Direction and is either I(n) or O(ut).
* F stands for Family and is one of D(ataset), G(rid), I(mage), C(PT), X(PostScript), M(atrix), V(ector), U(ndefined).
* A stands for Actual Family and is one of D, G, I, C, X, M, V, and U as well.
* Actual family may differ from family if a Dataset is actually passed as a Matrix, for instance.
* G stands for Geometry and is one of (poin)T, L(ine), P(olygon), C(Line|Polygon), A(POint|Line|Polygon), G(rid), N(one), X(text), or U(ndefined).
* M stands for Messenger and is either Y(es) or N(o).
* Limitation: object_ID must be <= GMTAPI_MAX_ID */

SP = D = F = A = G = M = 0; /* Initialize */
if ((k = sscanf (&filename[GMTAPI_PREFIX_LEN], "%c-%c-%c-%c-%c-%c-%d", &SP, &D, &F, &A, &G, &M, &object_ID)) != 7) {
GMT_Report (API, GMT_MSG_DEBUG, "gmtapi_memory_registered: Failed to decode memory name [%s], only %d conversions were successful.\n", filename, k);
return (GMT_NOTSET); /* Get the object ID unless we fail scanning */
}
if (direction == GMT_IN && D != 'I') return GMT_NOTSET; /* Not the right direction */
return object_ID;
}

/*! . */
GMT_LOCAL int gmtapi_is_registered (struct GMTAPI_CTRL *API, int family, int geometry, int direction, unsigned int mode, char *filename, void *resource) {
Expand Down Expand Up @@ -4846,6 +4872,7 @@ GMT_LOCAL struct GMT_IMAGE * gmtapi_import_image (struct GMTAPI_CTRL *API, int o
return_null (API, GMT_OBJECT_NOT_FOUND); /* Some internal error... */
API->object[new_item]->resource = I_obj;
API->object[new_item]->status = GMT_IS_USED; /* Mark as read */
API->object[new_item]->method = S_obj->method;
IH->alloc_level = API->object[new_item]->alloc_level; /* Since allocated here */
via = true;
if (S_obj->region) { /* Possibly adjust the pad so inner region matches wesn */
Expand Down Expand Up @@ -5249,7 +5276,7 @@ GMT_LOCAL struct GMT_GRID * gmtapi_import_grid (struct GMTAPI_CTRL *API, int obj
return_null (API, GMT_OBJECT_NOT_FOUND);
if ((new_item = gmtlib_validate_id (API, GMT_IS_GRID, new_ID, GMT_IN, GMT_NOTSET)) == GMT_NOTSET)
return_null (API, GMT_OBJECT_NOT_FOUND);
API->object[new_item]->method = GMT_IS_DUPLICATE;
API->object[new_item]->method = S_obj->method;
GH = gmt_get_G_hidden (G_obj);
HH = gmt_get_H_hidden (G_obj->header);
G_obj->header->complex_mode = mode; /* Set the complex mode */
Expand Down Expand Up @@ -5294,6 +5321,7 @@ GMT_LOCAL struct GMT_GRID * gmtapi_import_grid (struct GMTAPI_CTRL *API, int obj
if (gmt_M_err_pass (GMT, gmt_grd_BC_set (GMT, G_obj, GMT_IN), "Grid memory"))
return_null (API, GMT_GRID_BC_ERROR); /* Set boundary conditions */
API->object[new_item]->status = GMT_IS_USED; /* Mark as read */
API->object[new_item]->actual_family = GMT_IS_GRID; /* Done reading from matrix */
GH->alloc_level = API->object[new_item]->alloc_level; /* Since allocated here */
break;

Expand Down Expand Up @@ -6354,7 +6382,7 @@ int gmtlib_validate_id (struct GMTAPI_CTRL *API, int family, int object_ID, int
for (i = 0, item = GMT_NOTSET; item == GMT_NOTSET && i < API->n_objects; i++) {
S_obj = API->object[i]; /* Shorthand only */
if (!S_obj) continue; /* Empty object */
if (direction == GMT_IN && S_obj->status != GMT_IS_UNUSED) continue; /* Already used this input object once */
if (direction == GMT_IN && S_obj->status != GMT_IS_UNUSED && object_ID == GMT_NOTSET) continue; /* Already used this input object once */
if (!(family == GMT_NOTSET || (int)S_obj->family == family)) { /* Not the required data type; check for exceptions... */
if (family == GMT_IS_DATASET && (S_obj->actual_family == GMT_IS_VECTOR || S_obj->actual_family == GMT_IS_MATRIX))
S_obj->family = GMT_IS_DATASET; /* Vectors or Matrix masquerading as dataset are valid. Change their family here. */
Expand Down
26 changes: 12 additions & 14 deletions src/grdimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -1412,22 +1412,20 @@ EXTERN_MSC int GMT_grdimage (void *V_API, int mode, void *args) {
I->x = Ix; I->y = Iy;
}

if (!need_to_project && !API->external) {
for (k = 0; k < n_grids; k++) { /* If memory grids are passed in we must restore the headers */
if (mem_G[k]) {
gmt_copy_gridheader (GMT, Grid_orig[k]->header, header_G[k]);
gmt_free_header (API->GMT, &header_G[k]);
}
}
if (mem_I) {
gmt_copy_gridheader (GMT, Intens_orig->header, header_I);
gmt_free_header (API->GMT, &header_I);
}
if (mem_D) {
gmt_copy_gridheader (GMT, I->header, header_D);
gmt_free_header (API->GMT, &header_D);
for (k = 0; k < n_grids; k++) { /* If memory grids are passed in we must restore the headers */
if (mem_G[k]) {
gmt_copy_gridheader (GMT, Grid_orig[k]->header, header_G[k]);
gmt_free_header (API->GMT, &header_G[k]);
}
}
if (mem_I) {
gmt_copy_gridheader (GMT, Intens_orig->header, header_I);
gmt_free_header (API->GMT, &header_I);
}
if (mem_D) {
gmt_copy_gridheader (GMT, I->header, header_D);
gmt_free_header (API->GMT, &header_D);
}

if (Ctrl->D.active) { /* Free the color tables for indexed or gray images */
if (GMT_Destroy_Data (API, &Img_proj) != GMT_NOERROR) {
Expand Down
27 changes: 27 additions & 0 deletions src/testapi_imageshading.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "gmt.h"
#include <string.h>

int main (int argc, char **argv) {
void *API = NULL; /* The API control structure */
struct GMT_MATRIX *M = NULL; /* Structure to hold input matrix */
char input[GMT_VF_LEN] = {""}; /* String to hold virtual input filename */
char args[128] = {""}; /* String to hold module command arguments */
/* Initialize the GMT session */
API = GMT_Create_Session ("test", 2U, GMT_SESSION_EXTERNAL, NULL);
M = GMT_Read_Data (API, GMT_IS_MATRIX, GMT_IS_FILE, GMT_IS_SURFACE, GMT_READ_NORMAL, NULL, "2d-matrix.txt", NULL);
/* Associate our matrix with a virtual file */
GMT_Open_VirtualFile (API, GMT_IS_GRID|GMT_VIA_MATRIX, GMT_IS_SURFACE, GMT_IN, M, input);

/* Prepare the module arguments */
if (argc >= 2 && !strcmp (argv[1], "-I")) /* Want shading */
sprintf (args, "%s -JX6i -P -Baf -Cgeo -I+d", input);
else /* Just the image */
sprintf (args, "%s -JX6i -P -Baf -Cgeo", input);
/* Call the grdimage module */
GMT_Call_Module (API, "grdimage", GMT_MODULE_CMD, args);

/* Close the virtual file */
GMT_Close_VirtualFile (API, input);
/* Destroy session */
if (GMT_Destroy_Session (API)) return EXIT_FAILURE;
};
Loading