Skip to content

Fundamental change in sel and seltime methods - now not masking NaN-v…#187

Merged
knutfrode merged 5 commits intoOpenDrift:mainfrom
knutfrode:dev
Feb 3, 2026
Merged

Fundamental change in sel and seltime methods - now not masking NaN-v…#187
knutfrode merged 5 commits intoOpenDrift:mainfrom
knutfrode:dev

Conversation

@knutfrode
Copy link
Contributor

…alues, as these from now on will have significant meaning as dividing trajectories into segments. to_1d() is now also not removing NaN-values.

…alues, as these from now on will have significant meaning as dividing trajectories into segments. to_1d() is now also not removing NaN-values.
@knutfrode
Copy link
Contributor Author

knutfrode commented Feb 2, 2026

@gauteh It would be good to have your check of this before proceeding.

example_concat works locally, so not sure why it fails here.
The failing line 54 is dc = xr.concat((d1, d2), dim='trajectory', join='outer'),
but neither d1 nor d2 contain NaN (holes) or duplicates, although they are created with to_1d that does now allow NaN

@knutfrode
Copy link
Contributor Author

Inserting these two lines before 54 (concat) solves the Doc-build problem. But this is quite strange, since no duplicates are found locally.

d1 = d1.drop_duplicates('time')
d2 = d2.drop_duplicates('time')

trajan/traj1d.py Outdated
ds_dropped_selected = ds_dropped.sel(*args, **kwargs)

# Find indices of original trajectory corresponding to selected range
ind_start = np.where(self.ds[self.obs_dim] == ds_dropped_selected[self.obs_dim][0])[0][0]
Copy link
Member

Choose a reason for hiding this comment

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

What if obs_dim is unordered? I think this has to work on indices, not the values of the obs_dim. I guess obs_dim is time since this is 1d, but comparing floats is not so safe. To find the first/last value matching argmax is better.

@gauteh
Copy link
Member

gauteh commented Feb 3, 2026

Feels like we have to try this one out for a bit, I guess we could merge and potentially revert... It's nice to be able to use standard sel etc in 1d, so it's a bit of a pity if that cannot be done anymore. Maybe sel should check if there's any NaN's before doing the special version.

Now you're slicing on first and last match, but sel doesn't need to always be a slice. It can be an arbitrary list. So the new sel is more of a slice than a sel :)

@knutfrode
Copy link
Contributor Author

You are of course right, I simply forgot other options than selecting a slice...
Will have another look.

…om traj.sel to traj.seltime, and traj.sel again corresponds to xarray.sel. Also adding missing obs-coordinates after e.g. padding of arrays in obs-dimension
@knutfrode
Copy link
Contributor Author

knutfrode commented Feb 3, 2026

@gauteh Now traj.sel is simply using self.ds.sel as before (NaN not accepted), whereas logic to preserve NaN is moved to traj.seltime

One note: for several methods that e.g. padded/added items along the obs-dimension I have now re-written the obs-dim coordinate as integers from 0 to N before returning.
Earlier there were duplicates (e.g. [1, 2, 3, 4, 4]) or NaN-values.
However, this then also has to be consistent, otherwise it is not possible to compare arrays with differing indices. Thus I had to insert this also at methods where it was (apparently) not necessary before, e.g. here: f212ee9

@knutfrode knutfrode merged commit e54e045 into OpenDrift:main Feb 3, 2026
12 checks passed
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.

2 participants