You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
grdinfo -L doesn't work for external wrappers #8525
Shouldn't this be {8, 9} (or {9, 8}), the size of the grid?
You're right. It's a typo in the original test.
After changing to dim[2] = {9, 8}, the result is still incorrect:
0 0 0 0 0 0 0 0 1 1 0 0
Actually, {5, 4} might be the correct size without counting the padding.
For example, here is the original testapi_gmtgrid.c file in the src directory:
#include "gmt.h"
/*
* Testing the use of user grid memory allocated externally.
*/
int main () {
unsigned int mode = GMT_SESSION_EXTERNAL;
struct GMT_GRID *G = NULL;
struct GMTAPI_CTRL *API = NULL;
uint64_t dim[2] = {5, 4};
char args[1000] = {""};
char input[GMT_VF_LEN] = {""};
gmt_grdfloat *data = NULL;
API = GMT_Create_Session ("testapi_gmtgrid", 2U, mode, NULL);
/* Create a container for the grid */
G = GMT_Create_Data (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_CONTAINER_ONLY, dim, NULL, NULL, 0, 2, NULL);
/* Allocate memory of the data array in the external program (C or PyGMT) */
data = (gmt_grdfloat *)malloc(sizeof(gmt_grdfloat) * 9 * 8);
for (int i = 0; i < 9 * 8; i++) data[i] = (gmt_grdfloat)i;
/* Assign the user data to the GMT_GRID structure */
G->data = data;
/* Using the grid */
GMT_Open_VirtualFile (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_IN, G, input);
sprintf (args, "%s", input);
GMT_Call_Module (API, "grd2xyz", GMT_MODULE_CMD, args);
GMT_Close_VirtualFile (API, input);
/* Free the data array allocated by the external program */
free (data);
if (GMT_Destroy_Session (API)) return EXIT_FAILURE;
exit (0);
}
The example is correct without the -L0 because it only prints what's in the grid's header. But with it, it scans the grid and that's where the problem arises. With a pad = 0 it actually works well too, but with pad = 2 in gmt_api.c#L5383
if (!S_obj->region && gmt_grd_pad_status (GMT, G_obj->header, GMT->current.io.pad)) { /* Want an exact copy with no subset and same padding */
GMT->current.io.pad is {0,0,0,0} when it should be {2,2,2,2} and the code flow, flows along a deadly branch, the wesn is set to [0,0,0,0] and grid (from virtual file) is wrongly read.
Now, is this a flaw of this simple example or is it a more fundamental issue in the API? I almost don't use the xx_VirtualFile in Julia so cannot tell much about it.
Activity
joa-quim commentedon Jun 20, 2024
Hmm, it works for me in Julia
Shouldn't this be
{8, 9}(or{9, 8}), the size of the grid?seisman commentedon Jun 21, 2024
You're right. It's a typo in the original test.
After changing to
dim[2] = {9, 8}, the result is still incorrect:seisman commentedon Jun 21, 2024
If I change
GMT_INtoGMT_IN|GMT_IS_REFERENCE, the result is slightly better:seisman commentedon Jun 21, 2024
Actually,
{5, 4}might be the correct size without counting the padding.For example, here is the original testapi_gmtgrid.c file in the
srcdirectory:Its output is:
After changing
dim[2]to{9, 8}, the output is:joa-quim commentedon Jun 21, 2024
A yes, the pad.
joa-quim commentedon Jun 21, 2024
Once more: yes, the pad
The example is correct without the -L0 because it only prints what's in the grid's header. But with it, it scans the grid and that's where the problem arises. With a
pad = 0it actually works well too, but withpad = 2ingmt_api.c#L5383GMT->current.io.padis {0,0,0,0} when it should be {2,2,2,2} and the code flow, flows along a deadly branch, thewesnis set to [0,0,0,0] and grid (from virtual file) is wrongly read.Now, is this a flaw of this simple example or is it a more fundamental issue in the API? I almost don't use the
xx_VirtualFilein Julia so cannot tell much about it.joa-quim commentedon Jun 21, 2024
Or maybe the problem is in
grdinfoline 692 that always sets the session pad to 0seisman commentedon Jun 24, 2024
I tried to comment out this line, and the new result is:
joa-quim commentedon Jun 24, 2024
Which is the correct result. The NaNs originate in the test example that does not set the grid's z_min|max
seisman commentedon Jun 24, 2024
But
-L0meansReport range of data by actually reading them (not from header)., So min/max should be correctly reported, no?seisman commentedon Jun 24, 2024
Looking at the
grdinfosource code.grdinfoscans all the grid nodes to find the min/max values of the grid and the min/max values are stored in the variablesv_min/v_max:gmt/src/grdinfo.c
Lines 823 to 840 in 206db56
The min/max in the grid header is only updated when
-Moption is set:gmt/src/grdinfo.c
Line 855 in 206db56
When output the grid information,
grdinfoalways uses the information from the header:gmt/src/grdinfo.c
Line 967 in 206db56
I think this is a bug. When
-L0is given, we should do either of these:header->z_min/header->z_maxwithv_min/v_maxv_min/v_maxrather thanheader->z_min/header->z_maxjoa-quim commentedon Jun 24, 2024
I agrre. Line 855 should become
[-]grdinfo -L0 doesn't work for external wrappers[/-][+]grdinfo -L doesn't work for external wrappers[/+]