Skip to content

Bug when passing matrix as a pixel-registered grid to GMT C API? #3502

Closed

Description

@seisman

Here is a bash script, which converts a sample XYZ data to gridline- and pixel-registered grids, and plots them, respectively.

# prepare sample data/matrix
cat > input.dat << EOF
0 0 0.1
0 1 0.2
0 2 0.3
1 0 0.4
1 1 0.5
1 2 0.6
2 0 0.7
2 1 0.8
2 2 0.9
EOF

# convert sample data to gridline- and pixel- grids.
gmt xyz2grd input.dat -R0/2/0/2 -I1/1 -Ggridline.grd -rg -V
gmt xyz2grd input.dat -R-0.5/2.5/-0.5/2.5 -I1/1 -Gpixel.grd -rp -V

# check grid information
gmt grdinfo gridline.grd
gmt grdinfo pixel.grd

# Plot gridline- and pixel- grids side by side
gmt grdimage gridline.grd -JX6c -R-1/3/-1/3 -Baf -BWSen+t"Gridline" -K > gridline_pixel.ps
gmt grdimage pixel.grd    -JX6c -R-1/3/-1/3 -Baf -BWSen+t"Pixel" -X8c -O >> gridline_pixel.ps
gmt psconvert -A -P -Tg gridline_pixel.ps

rm input.dat gridline.grd pixel.grd gmt.history gridline_pixel.ps

Although the two grids, gridline.grd and pixel.grd, have different grid registration, the plots are the same. See the output figure below:

image

I tried to plot the same figure using the GMT C API, by directly passing a matrix as gridline- or pixel grids. Here is the C code.

#include "gmt_dev.h"

int main () {
	unsigned int mode = GMT_SESSION_EXTERNAL;
	struct GMT_MATRIX *M_p = NULL, *M_g = NULL;
	char input_p[GMT_VF_LEN] = {""}, input_g[GMT_VF_LEN] = {""};
	char args_p[128] = {""}, args_g[128]= {""};
	struct GMTAPI_CTRL *API = NULL;

	double inc[2] = {1.0, 1.0};
	double coord[9] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};

	API = GMT_Create_Session ("test", 2U, mode, NULL);

	/* pass matrix as gridline-registered grid */
	double range_g[4] = {0.0, 2.0, 0.0, 2.0};
	if ((M_g = GMT_Create_Data (API, GMT_IS_GRID|GMT_VIA_MATRIX, GMT_IS_SURFACE, GMT_CONTAINER_ONLY, NULL, range_g, inc, GMT_GRID_NODE_REG, 0, NULL)) == NULL) return (EXIT_FAILURE);
	GMT_Put_Matrix (API, M_g, GMT_DOUBLE, 0, coord);
	GMT_Open_VirtualFile (API, GMT_IS_GRID|GMT_VIA_MATRIX, GMT_IS_SURFACE, GMT_IN|GMT_IS_REFERENCE, M_g, input_g);
	sprintf (args_g, "%s -R-1/3/-1/3 -JX6c -Baf -BWSen+tGridline -K > test_matrix.ps", input_g);
	GMT_Call_Module (API, "grdimage", GMT_MODULE_CMD, args_g);
	GMT_Close_VirtualFile (API, input_g);

	/* pass matrix as pixel-registered grid */
	double range_p[4] = {-0.5, 2.5, -0.5, 2.5};
	if ((M_p = GMT_Create_Data (API, GMT_IS_GRID|GMT_VIA_MATRIX, GMT_IS_SURFACE, GMT_CONTAINER_ONLY, NULL, range_p, inc, GMT_GRID_PIXEL_REG, 0, NULL)) == NULL) return (EXIT_FAILURE);
	GMT_Put_Matrix (API, M_p, GMT_DOUBLE, 0, coord);
	GMT_Open_VirtualFile (API, GMT_IS_GRID|GMT_VIA_MATRIX, GMT_IS_SURFACE, GMT_IN|GMT_IS_REFERENCE, M_p, input_p);
	sprintf (args_p, "%s -R-1/3/-1/3 -JX6c -Baf -BWSen+tPixel -O -X8c >> test_matrix.ps", input_p);
	GMT_Call_Module (API, "grdimage", GMT_MODULE_CMD, args_p);
	GMT_Close_VirtualFile (API, input_p);

	if (GMT_Destroy_Session (API)) return EXIT_FAILURE;
	exit (0);
}

The C code is compiled using:

gcc test_matrix.c -o test_matrix `gmt-config --cflags` `gmt-config --libs`

Running the executable test_matrix gives me the following result. The gridline image looks correct (although the order of the colored pixels is not the same as the bash version), but the pixel image is black.

image

Something wrong with my C code? Or a GMT API bug?

Activity

  1. joa-quim commented on Jun 20, 2020

    @joa-quim
    Member

    Although the two grids, gridline.grd and pixel.grd, have different grid registration, the plots are the same. See the output figure below:

    Yes, they are equal and that is correct. Both have 3x3 pixels and because the global limits are larger than the grid registered the border pixels are not trimmed.

  2. PaulWessel commented on Jun 20, 2020

    @PaulWessel
    Contributor

    Think you might as well commit this C code to the repo, no? I can have a look.

  3. seisman commented on Jun 20, 2020

    @seisman
    MemberAuthor

    I just commited the C code to the branch testapi-registration. You can work on the PR #3503 directly.

  4. added
    bug
    on Jun 20, 2020
  5. added this to the 6.1.0 milestone on Jun 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions