Skip to content

GMT_GRID_HEADER: Parse grid header and add grid properties - #3134

Merged
seisman merged 21 commits into
mainfrom
gmtgrid/parser
Apr 1, 2024
Merged

GMT_GRID_HEADER: Parse grid header and add grid properties#3134
seisman merged 21 commits into
mainfrom
gmtgrid/parser

Conversation

@seisman

@seismanseisman commented Mar 22, 2024

Copy link
Copy Markdown
Member

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.

@seismanseisman added enhancement Improving an existing feature needs review This PR has higher priority and needs review. labels Mar 22, 2024
@seismanseisman added this to the 0.12.0 milestone Mar 22, 2024
@seisman
seisman requested a review from weiji14March 22, 2024 11:41
Comment threadpygmt/datatypes/grid.py Outdated
Comment threadpygmt/datatypes/grid.py Outdated
seismanand others added 2 commits March 22, 2024 21:22
Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
@seismanseisman changed the title GMT_GRID: Add private function _parse_nameunits for parsing long_name and units from x/y/z_unitsdatatypes: Add private functions _parse_nameunits and _parse_header for parsing grid headerMar 22, 2024
@seismanseisman changed the title datatypes: Add private functions _parse_nameunits and _parse_header for parsing grid headerdatatypes: Add functions _parse_nameunits and _parse_header for parsing grid headerMar 22, 2024
Comment threadpygmt/datatypes/grid.py Outdated
Comment threadpygmt/datatypes/grid.py Outdated
Comment threadpygmt/datatypes/grid.py Outdated
Comment threadpygmt/datatypes/grid.py Outdated
Comment threadpygmt/datatypes/grid.py Outdated
Comment threadpygmt/datatypes/grid.py Outdated
Comment threadpygmt/datatypes/grid.py Outdated
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
Comment threadpygmt/datatypes/grid.py Outdated
@seisman
seisman marked this pull request as draft March 25, 2024 23:37
@seismanseisman 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
Copy Markdown
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
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Done in dfbc2e5.

Comment threadpygmt/datatypes/header.py Outdated
Comment threadpygmt/datatypes/header.py Outdated
Comment threadpygmt/datatypes/header.py Outdated
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seisman

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
Member

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
Copy Markdown
MemberAuthor

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.

@seismanseisman changed the title datatypes: Add functions _parse_nameunits and _parse_header for parsing grid headerGMT_GRID_HEADER: Add getter methods for getting attributes from grid headersMar 31, 2024
@seisman

Copy link
Copy Markdown
MemberAuthor

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 threadpygmt/datatypes/header.py Outdated
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
Copy Markdown
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
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Removed in 79f4c87.

Comment threadpygmt/datatypes/header.py Outdated
Comment threadpygmt/datatypes/header.py Outdated
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
Copy Markdown
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
Copy Markdown
MemberAuthor

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
Copy Markdown
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
Copy Markdown
MemberAuthor

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
Copy Markdown
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.

@weiji14weiji14 left a comment

Copy link
Copy Markdown
Member

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.

Comment threadpygmt/datatypes/header.py Outdated
@seismanseisman changed the title GMT_GRID_HEADER: Add getter methods for getting attributes from grid headersGMT_GRID_HEADER: Parse grid header and add grid propertiesApr 1, 2024
@seisman
seisman marked this pull request as ready for review April 1, 2024 05:50
@seisman
seisman merged commit 65cc190 into mainApr 1, 2024
@seisman
seisman deleted the gmtgrid/parser branch April 1, 2024 05:56
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementImproving an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@seisman@weiji14@michaelgrund@yvonnefroehlich