Skip to content

Conversation

@dopplershift
Copy link
Member

@dopplershift dopplershift commented Aug 25, 2022

Description Of Changes

This adds xarray direct reading of GINI files by implementing v2 of xarray's backends API. This registers a new entrypoint that basically allows open_dataset() to just work™️ for any file with a name that ends with ".gini". It also works to specify engine='gini'--and it all works without needing to manually import metpy thanks to the entrypoints mechanism.

The v2 backend API was added in xarray 0.18.0, released 6 May 2021. I've elected at this point not to bother making the imports and classes conditional. At least right now I didn't feel like the effort was worth it to cover 8 more months of xarray releases.

Refactored a bit, but it could be cleaned up in the future when we can drop the previously supported mechanisms. This tweaks the xarray reading a bit to include lat/lon in coordinates (should have already been done) and eliminates using a masked array (xarray is supposed to use nans).

As part of this I also added support for the xarray .dt accessor to add_timestamp--it already worked with it in calling .strftime, this just makes it try .item() if the result of that call to strftime isn't a string.

Also updated the existing GINI example to use all of this stuff.

Checklist

@dopplershift dopplershift added Type: Enhancement Enhancement to existing functionality Area: IO Pertains to reading data Area: Xarray Pertains to xarray integration labels Aug 25, 2022
@dopplershift dopplershift added this to the August 2022 milestone Aug 25, 2022
@dopplershift dopplershift requested a review from a team as a code owner August 25, 2022 22:35
@dopplershift dopplershift requested review from dcamron and removed request for a team August 25, 2022 22:35
@dopplershift dopplershift mentioned this pull request Aug 25, 2022
@dopplershift
Copy link
Member Author

@kgoebber You may be interested in the add_timestamp() update here.

@dopplershift dopplershift force-pushed the gini-backend branch 2 times, most recently from b5ddf08 to 5d4ba44 Compare August 25, 2022 23:04
def test_gini_xarray_entrypoint(filename, bounds, data_var, proj_attrs, image, dt,
specify_engine):
"""Test that GINI files can be opened directly by xarray using the v2 API."""
ds = xr.open_dataset(get_test_data(filename, as_file_obj=specify_engine),

Check warning

Code scanning / CodeQL

File is not always closed

File is opened but is not closed.
"""Test that add_timestamp can work with xarray datetime accessor."""
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ds = xr.open_dataset(get_test_data('AK-REGIONAL_8km_3.9_20160408_1445.gini'),

Check warning

Code scanning / CodeQL

File is not always closed

File is opened but is not closed.
dcamron
dcamron previously approved these changes Oct 4, 2022
Copy link
Member

@dcamron dcamron left a comment

Choose a reason for hiding this comment

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

Tests fine to me, and I'm alright with the decisions here re old xarray API and directly using GiniFile. Nothing immediately jumps out as missed here. If we need I can leave this up until the morning in case anyone else has last-minute considerations, but I think it's good to go!

This refactors a bit to make this reasonably direct, rather than having
it re-parse all the variables created by the previous support.

It also makes some minor tweaks to the general xarray decoding by
no-longer using masked arrays (Xarray uses nans) and adds lat/lon to
coordinates--as they should have been in the first place.
All that's needed is a conditional call to .item(), and everything "just
works".
@dopplershift
Copy link
Member Author

Resolved the conflict, this should be good to go.

@dopplershift dopplershift requested a review from dcamron October 4, 2022 17:49
@dcamron dcamron merged commit c02b8e2 into Unidata:main Oct 7, 2022
@dopplershift dopplershift deleted the gini-backend branch October 7, 2022 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: IO Pertains to reading data Area: Xarray Pertains to xarray integration Type: Enhancement Enhancement to existing functionality

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Update GINI for new XArray backends API

2 participants