Skip to content

GMT_GRID_HEADER: Parse grid header and add grid properties #3134

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 21 commits into from
Apr 1, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented Mar 22, 2024

Description of proposed changes

Part of PR #2398.

This PR add codes to parse the grid header and get the dimension names, attributes, gtype and more. They're stored as properties of the header.

The codes can be reused in wrapping GMT_GRID/GMT_IMAGE/GMT_CUBE data structures.

Tests are added to the _parse_nameunits function but not for other functions. The function will be tested in the GMT_GRID.to_dataarray method.

@seisman seisman added enhancement Improving an existing feature needs review This PR has higher priority and needs review. labels Mar 22, 2024
@seisman seisman added this to the 0.12.0 milestone Mar 22, 2024
@seisman seisman requested a review from weiji14 March 22, 2024 11:41
seisman and others added 2 commits March 22, 2024 21:22
@seisman seisman changed the title GMT_GRID: Add private function _parse_nameunits for parsing long_name and units from x/y/z_units datatypes: Add private functions _parse_nameunits and _parse_header for parsing grid header Mar 22, 2024
@seisman seisman changed the title datatypes: Add private functions _parse_nameunits and _parse_header for parsing grid header datatypes: Add functions _parse_nameunits and _parse_header for parsing grid header Mar 22, 2024
Co-authored-by: Yvonne Fröhlich <[email protected]>
@seisman seisman marked this pull request as draft March 25, 2024 23:37
@seisman seisman removed the needs review This PR has higher priority and needs review. label Mar 25, 2024
Comment on lines +58 to +63
# Name of data set
("title", ctp.c_char * GMT_GRID_TITLE_LEN80),
# Name of generating command
("command", ctp.c_char * GMT_GRID_COMMAND_LEN320),
# Comments for this data set
("remark", ctp.c_char * GMT_GRID_REMARK_LEN160),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Store these metadata in the attrs dict too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in dfbc2e5.

@seisman
Copy link
Member Author

seisman commented Mar 29, 2024

I'm wondering if we should move the _parse_nameunits/_parse_header functions as the GMT_GRID_HEADER class methods, so that we don't have to import these private functions when wrapping GMT_GRID/GMT_IMAGE.

@weiji14
Copy link
Member

weiji14 commented Mar 29, 2024

I'm wondering if we should move the _parse_nameunits/_parse_header functions as the GMT_GRID_HEADER class methods, so that we don't have to import these private functions when wrapping GMT_GRID/GMT_IMAGE.

That's not a bad idea. But maybe split the implementation of _parse_header into getter methods like get_attrs(), get_gtype() and so on?

@seisman
Copy link
Member Author

seisman commented Mar 29, 2024

But maybe split the implementation of _parse_header into getter methods like get_attrs(), get_gtype() and so on?

Sounds good, but the gtype is determined from the x/y axis attributes. We may need to parse the headers (e.g., the current _parse_header function), then store the returned attrs, dims, gtype and registration into a class attributes (e.g., GMT_GRID_HEADER.nc) and then provide the getter methods which return the requested attributes.

@seisman seisman changed the title datatypes: Add functions _parse_nameunits and _parse_header for parsing grid header GMT_GRID_HEADER: Add getter methods for getting attributes from grid headers Mar 31, 2024
@seisman
Copy link
Member Author

seisman commented Mar 31, 2024

I'm wondering if we should move the _parse_nameunits/_parse_header functions as the GMT_GRID_HEADER class methods, so that we don't have to import these private functions when wrapping GMT_GRID/GMT_IMAGE.

That's not a bad idea. But maybe split the implementation of _parse_header into getter methods like get_attrs(), get_gtype() and so on?

Done in 2bd353a.

Comment on lines 269 to 278
def get_registration(self) -> int:
"""
Get the grid registration from the grid header.

Returns
-------
registration
The grid registration. 0 for gridline and 1 for pixel.
"""
return self.registration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just remove this and call self.registration no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 79f4c87.

Comment on lines 193 to 202
def get_name(self) -> str:
"""
Get the name of the grid from the grid header.

Returns
-------
name
The name of the grid.
"""
return "z"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably remove this if it is only returning the letter z?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name defaults to z for 2-D grids and cube for 3-D cubes, but it can be overridden by the varname variable stored in the hidden GMT_GRID_HEADER_HIDDEN data structure. I guess we won't wrap the GMT_GRID_HEADER_HIDDEN data structure now because it is designed to be hidden and may change anytime, but maybe we have to wrap it in the future?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I see that varname is marked as private at https://github.com/GenericMappingTools/gmt/blob/9b93e07982233a31e97b3f8a4ed8df3a3dfa56f9/src/gmt_hidden.h#L161, but it is supposedly holding the name of a NetCDF variable? Maybe we should try to parse it instead of always returning z (or cube)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, varname is only assigned when a netCDF name like input.nc?varname is specified. I guess it's just rare cases.

As mentioned above, to parse varname, we have to wrap the GMT_GRID_HEADER_HIDDEN data structure, which contains more than 50 fields, and the last time it was changed is just a few months ago (GenericMappingTools/gmt@b9d3f2b).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Let's just leave it unwrapped for now then.

Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. There's lots of code coverage missing, but that will be covered in #2398 and/or #3128. Might make small changes to the header parsing as we continue on wrapping GMT_GRID, GMT_IMAGE and GMT_CUBE.

@seisman seisman changed the title GMT_GRID_HEADER: Add getter methods for getting attributes from grid headers GMT_GRID_HEADER: Parse grid header and add grid properties Apr 1, 2024
@seisman seisman marked this pull request as ready for review April 1, 2024 05:50
@seisman seisman merged commit 65cc190 into main Apr 1, 2024
10 of 12 checks passed
@seisman seisman deleted the gmtgrid/parser branch April 1, 2024 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants