Skip to content
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

Support a start_index of 1 #300

Merged
merged 5 commits into from
Sep 9, 2024
Merged

Support a start_index of 1 #300

merged 5 commits into from
Sep 9, 2024

Conversation

Huite
Copy link
Collaborator

@Huite Huite commented Sep 6, 2024

No description provided.

Copy link
Collaborator

@veenstrajelmer veenstrajelmer left a comment

Choose a reason for hiding this comment

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

This works like a charm, writing a netfile with start_index=1 is now much simpler than before. I have tested:

  • writing a file from scratch (start_index can conveniently be set before writing)
  • reading an existing file and writing it again (properties are maintained)
  • reading an existing file, cropping the grid and writing it again (properties are maintained)

Thanks a lot for this useful improvement! The diff also shows quite some fill_value changes, I guess these are additions to #299 right? I did not specifically look into the behaviour of fill_value, but just let me know if I should.

@Huite
Copy link
Collaborator Author

Huite commented Sep 9, 2024

The diff also shows quite some fill_value changes, I guess these are additions to #299 right? I did not specifically look into the behaviour of fill_value, but just let me know if I should.

Some fill value logic is required to avoid increment or decrementing the fill values in the connectivity arrays.

This is one of the few cases where having masked arrays instead of ordinary numpy arrays have some merit:

import numpy as np
import numpy.ma as ma

x = np.array([1, 2, 3, -1, 5])
mx = ma.masked_array(x, mask=[0, 0, 0, 1, 0])
mx2 = mx + 1

print(mx.data)  # array([ 1,  2,  3, -1,  5])
print(mx2.data)  # array([ 2  3  4 -1  6])

When indexing, it'll use the original value:

a = np.arange(10)
assert np.array_equal(a[x], a[mx])

The -1 value is therefore still the best choice, since it will be used for indexing.

It would remove the need to generate is_fill = connectivity == FILL_VALUE.
A nice feature is that masked does propagate during indexing.

Anyway, just a side note.

@Huite Huite merged commit e37c792 into main Sep 9, 2024
14 checks passed
@Huite Huite deleted the start-index branch September 9, 2024 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support writing of netfile with start_index=1 and _FillValue!=-1
2 participants