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

added TELEMAC support and tests #91

Merged
merged 1 commit into from
May 29, 2024
Merged

added TELEMAC support and tests #91

merged 1 commit into from
May 29, 2024

Conversation

tomsail
Copy link
Collaborator

@tomsail tomsail commented May 19, 2024

added the following components for normalisation:

  • is_telemac()
  • normalize_telemac
  • THALASSA_FORMATS.TELEMAC
  • infer_format()
  • NORMALIZE_DISPATCHER

Which should address the normalisation for TELEMAC format discussed in #90

I might have added too many elements:

  • xarray-selafin to support directly .slf file formats
  • Global variable names in order to prepare for a more mainstream formating convention (like UGRID):
EDGE_DIM = "edge"
FACE_DIM = "face"
NODE_DIM = "node"
VERTICAL_DIM = "layer"
CONNECTIVITY = "face_nodes"
VERTICE_DIM = "max_no_vertices"
X_DIM = "lon"
Y_DIM = "lat"

If it too much for one PR, let me know, I'll drop these last changes

@tomsail
Copy link
Collaborator Author

tomsail commented May 19, 2024

I forgot the .slf file, the PR is not complete

@tomsail
Copy link
Collaborator Author

tomsail commented May 19, 2024

ok ready for review

Copy link
Collaborator

@pmav99 pmav99 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, kudos. Just some minor stuff are needed

pyproject.toml Outdated Show resolved Hide resolved
@@ -74,7 +74,7 @@ def open_dataset(
import xarray as xr

default_kwargs: dict[str, T.Any] = dict(
mask_and_scale=True,
# mask_and_scale=True,
Copy link
Collaborator

Choose a reason for hiding this comment

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

why comment out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good question, I forgot to mention this:
mask_and_scale is an argument that is not available in the xarray-selafin backend but that seems to exists for all other formats (zarr, grib, netcdf). I don't know how to solve this

Copy link
Collaborator

Choose a reason for hiding this comment

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

mask_and_scale is about applying the add_offset and the scale_factor. It's a netcdf thing, but xarray also applies it to zarr. I don't know if it makes sense in selafin, but even if it doesn't, I would add it as a do-nothing argument. Either that or I would add a **kwargs so that it can be consumed without throwing an exception

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you mean directly in xarray-selafin, correct?

Copy link
Collaborator

Choose a reason for hiding this comment

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

According to the docs a **kwargs is not allowed:
https://docs.xarray.dev/en/stable/internals/how-to-add-new-backend.html#open-dataset

Furthermore, mask_and_scale may not be supported by all backends: https://docs.xarray.dev/en/stable/generated/xarray.open_dataset.html

Therefore, I guess it's OK to remove it. That being said, if the selafin files can be quantized it might make sense to add support for mask_and_scale in xarray-selafin but that's a different issue.

thalassa/normalization.py Show resolved Hide resolved
thalassa/normalization.py Outdated Show resolved Hide resolved

# TELEMAC output uses one-based indices for `face_nodes`
# Let's ensure that we use zero-based indices everywhere.
ds[CONNECTIVITY] = ((FACE_DIM, VERTICE_DIM), ds.attrs['ikle2'] - 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is most probably xarray-selafin related, but why is ikle2 an attribute and not a variable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Luc actually shifted ikle2 and ikle3 from coordinates to attributes because it removed useless dimensions.
Here's the commit.
We might want to move them back in the variables at some point I agree.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I wouldn't call the connectivity useless. Furthermore, it might be that serializing attributes needs more space compared to data variables (lack of compression etc). Haven't tested it though.

thalassa/normalization.py Outdated Show resolved Hide resolved
@pmav99
Copy link
Collaborator

pmav99 commented May 19, 2024

Can you please test test_create_trimesh with selafin, too?

@pmav99
Copy link
Collaborator

pmav99 commented May 20, 2024

Please update the README, too.

@tomsail
Copy link
Collaborator Author

tomsail commented May 22, 2024

I might have forgotten something important to remove the overlapping triangles:

this is what

mesh = api.open_dataset('meshes/global-v0.2.slf')
map = thalassa.plot(mesh.isel(time=0), variable = 'B', show_mesh = True)

gives me :
image

@tomsail
Copy link
Collaborator Author

tomsail commented May 22, 2024

sorry my bad. I though dropping elements crossing IDL was integrated in the create_trimesh() function.

mesh = api.open_dataset('meshes/global-v0.2.slf')
mesh_ = thalassa.utils.drop_elements_crossing_idl(mesh)
map = thalassa.plot(mesh_.isel(time=0), variable = 'B', show_mesh = True)

did it

@pmav99
Copy link
Collaborator

pmav99 commented May 22, 2024

No it isn't. it has to be done manually. we could add a boolean param for it though. E.g.

thalassa.open_dataset("/path/to/netcdf", drop_idl=True)

but that should be discussed in a different issue.

@pmav99
Copy link
Collaborator

pmav99 commented May 22, 2024

I had a second look BTW. I am starting to think that we should remove the xarray-selafin package from the main dependencies.

xarray-selafin is pretty much the same as cfgrib or any other xarray engine. If it is installed, then you can use it. But there is no need to pull it when you install thalassa.

So if a user wants to open a selafin file he should just make sure that both thalassa and xarray-selafin are available in their environment:

pip install thalassa
pip install xarray-selafin

and then use:

import thalassa

ds = thalassa.open_dataset("/path/to/foo.slf", engine="selafin")

and things should just work.

That being said, we should still keep it as a dev dependency because we want to use it for our tests.

@pmav99
Copy link
Collaborator

pmav99 commented May 22, 2024

These need to be updated, too:

And if there is a publicly available selafin file, we could add it here, too: https://thalassa.readthedocs.io/en/latest/installation.html

@pmav99 pmav99 merged commit d264508 into ec-jrc:master May 29, 2024
5 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