Skip to content

Reading/writing a matrix from/to a grid file#3848

Merged
PaulWessel merged 9 commits intomasterfrom
grid2matrix
Aug 6, 2020
Merged

Reading/writing a matrix from/to a grid file#3848
PaulWessel merged 9 commits intomasterfrom
grid2matrix

Conversation

@PaulWessel
Copy link
Member

Description of proposed changes

This PR started out with trying to generalize GMT_Read_Data for GMT_IS_MATRIX. Now, if you pass the geometry GMT_IS_SURFACE then we read the file as a grid but return a matrix. The conversion between grid to matrix is simple (we do this in the general import_grid anyway) but it pointed out some issues when testing via a short C code starting a session: Since no modules were involved, the remote files were unknown. So now we added a check to refresh the server if the list is NULL. The test codes now pass remote file names and GMT_IS_SURFACE and this works fine.
Then the same was applied to GMT_Write_Data, i.e., ability to write a MATRIX (camouflaging as a grid) to a grid file. This exposed another problem in gmt_nc.c in that the file-specific chunking settings were not reset, causing a second GMT_Write_Data to fail.
I have updated a few of the testapi*.c codes to indicate the geometry (GMT_IS_POINT if just a dumb text table and GMT_IS_SURFACE if a real grid), and updated some of the PS originals that changed due to the roundoff involved in the text version.
A new test program testapi_grid2matrix.c was added to test the reading of a grid into a matrix and then writing the matrix out as a grid, and the new test apimat2grd.sh runs this and makes a plot.

Because there are several bug fixes involved in this PR I have labeled this as a bug fix which requires backport.

@PaulWessel PaulWessel added bug Something isn't working backport 6.1 Backport this PR to 6.1 branch labels Aug 5, 2020
@PaulWessel PaulWessel requested review from joa-quim and seisman August 5, 2020 20:30
@PaulWessel PaulWessel self-assigned this Aug 5, 2020
@seisman
Copy link
Member

seisman commented Aug 6, 2020

This PR breaks a PyGMT test, but maybe we need to update PyGMT.

The PyGMT test does following things:

  1. Create a grid by calling GMT_Create_Data with following parameters:
family="GMT_IS_GRID|GMT_VIA_MATRIX", 
geometry="GMT_IS_SURFACE", 
mode="GMT_CONTAINER_ONLY"
  1. Attach a matrix to the dataset by calling GMT_Put_Matrix
  2. Call GMT_Write_Data with parameters
family="GMT_IS_MATRIX",
geometry="GMT_IS_SURFACE",
mode="GMT_CONTAINER_AND_DATA",

Before this PR, it writes a table/matrix; after this PR, it writes a netCDF grid. Is this what we want?

With this PR, to write a table data, PyGMT should use GMT_IS_POINT, right?

@PaulWessel
Copy link
Member Author

Yes, passing geometry as GMT_IS_POINT will case it to read or write an ascii table, while GMT_IS_SURFACE ends up calling import|export_grid. So this is backwards incompatible to some extent.

@seisman
Copy link
Member

seisman commented Aug 6, 2020

This is a good backwards incompatible to PyGMT, but I'm not sure if GMT.jl likes it.

@PaulWessel
Copy link
Member Author

We need to check with @joa-quim to see if GMT.jl uses GMT_Read_Data directly on a text file to read in a GMT_IS_MATRIX. I don't think he is, but we will wait for confirmation. Depending on how long he is banned from checking email, this could be 10 days. Meanwhile, I will update the API docs.

@joa-quim
Copy link
Member

joa-quim commented Aug 6, 2020

Leave only tomorrow.

I use

M = GMT_Create_Data(API, GMT_IS_MATRIX|GMT_VIA_MATRIX, GMT_IS_PLP, 0, dim, C_NULL, C_NULL, 0, 0, C_NULL)
actual_family[1] = actual_family[1] | GMT_VIA_MATRIX

but not GMT_Read_Data

@PaulWessel
Copy link
Member Author

OK, so it will not affect GMT.jl since GMT modules (other than a few testapi_*.c programs) never called GMT_Read_Data with intent of returning a matrix or sorts. So if @seisman is OK with this being merged then it might as well be approved now?

@PaulWessel PaulWessel merged commit 02af19c into master Aug 6, 2020
@PaulWessel PaulWessel deleted the grid2matrix branch August 6, 2020 20:04
@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2020

The backport to 6.1 failed:

The process 'git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-6.1 6.1
# Navigate to the new working tree
cd .worktrees/backport-6.1
# Create a new branch
git switch --create backport-3848-to-6.1
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick 02af19c043977c454d4fc1a29b947a0771c52974
# Push it to GitHub
git push --set-upstream origin backport-3848-to-6.1
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-6.1

Then, create a pull request where the base branch is 6.1 and the compare/head branch is backport-3848-to-6.1.

seisman pushed a commit that referenced this pull request Aug 6, 2020
* Allow GMT_Read_Data to return a matrix from a grid file

Experimental.  INitially it just read an ascii table but it would be convenient to recognize file extensions *.grd and *.nc plus the remote grids @earth*.

* Complete plan for read/write matrix via grids

* Update testapi_grid2matrix.c

* Fix the chunk issue

* Create apimat2grd.ps

* Update some test codes to read OPINTS not SURFACE if ascii, and update PS

* Update api.rst
seisman added a commit that referenced this pull request Aug 6, 2020
* Reading/writing a matrix from/to a grid file (#3848)

* Allow GMT_Read_Data to return a matrix from a grid file

Experimental.  INitially it just read an ascii table but it would be convenient to recognize file extensions *.grd and *.nc plus the remote grids @earth*.

* Complete plan for read/write matrix via grids

* Update testapi_grid2matrix.c

* Fix the chunk issue

* Create apimat2grd.ps

* Update some test codes to read OPINTS not SURFACE if ascii, and update PS

* Update api.rst

* Fix two conflicts

Co-authored-by: Paul Wessel <pwessel@hawaii.edu>
seisman added a commit to GenericMappingTools/pygmt that referenced this pull request Aug 6, 2020
The test `test_put_matrix_grid` fails due to recent changes of GMT API
in GenericMappingTools/gmt#3848.

Before this PR, for a matrix grid, calling GMT_Write_Data always writes
the matrix grid as a matrix, no matter the geometry is `GMT_IS_POINT` or
`GMT_IS_SURFACE`.

This PR declares it as a bug (or an improvement). With that PR merged,
`geometry=GMT_IS_POINT` writes a matrix, `geometry=GMT_IS_SURFACE`
writes a the matrix grid as a netCDF file.

This test expects a matrix, but the new API writes a netCDF grid.
That's why it fails.

This PR fixes the issue by simply changing `GMT_IS_SURFACE` to
`GMT_IS_POINT`, so that the test can pass for GMT 6.1.0, 6.1 and master
branches.
seisman added a commit to GenericMappingTools/pygmt that referenced this pull request Aug 7, 2020
The test `test_put_matrix_grid` fails due to recent changes of GMT API
in GenericMappingTools/gmt#3848.

Before this PR, for a matrix grid, calling GMT_Write_Data always writes
the matrix grid as a matrix, no matter the geometry is `GMT_IS_POINT` or
`GMT_IS_SURFACE`.

This PR declares it as a bug (or an improvement). With that PR merged,
`geometry=GMT_IS_POINT` writes a matrix, `geometry=GMT_IS_SURFACE`
writes a the matrix grid as a netCDF file.

This test expects a matrix, but the new API writes a netCDF grid.
That's why it fails.

This PR fixes the issue by simply changing `GMT_IS_SURFACE` to
`GMT_IS_POINT`, so that the test can pass for GMT 6.1.0, 6.1 and master
branches.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 6.1 Backport this PR to 6.1 branch bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants