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

Refactoring of FieldSet field store #1663

Open
VeckoTheGecko opened this issue Aug 20, 2024 · 2 comments
Open

Refactoring of FieldSet field store #1663

VeckoTheGecko opened this issue Aug 20, 2024 · 2 comments
Labels
coding/Python good first issue Good for new parcels developers needs investigation refactor code to refactor unstructured grids Development of unstructured grids support

Comments

@VeckoTheGecko
Copy link
Contributor

Parcels/parcels/fieldset.py

Lines 162 to 197 in 595b7a9

def add_field(self, field, name=None):
"""Add a :class:`parcels.field.Field` object to the FieldSet.
Parameters
----------
field : parcels.field.Field
Field object to be added
name : str
Name of the :class:`parcels.field.Field` object to be added
Examples
--------
For usage examples see the following tutorials:
* `Nested Fields <../examples/tutorial_NestedFields.ipynb>`__
* `Unit converters <../examples/tutorial_unitconverters.ipynb>`__ (Default value = None)
"""
if self.completed:
raise RuntimeError(
"FieldSet has already been completed. Are you trying to add a Field after you've created the ParticleSet?"
)
name = field.name if name is None else name
if hasattr(self, name): # check if Field with same name already exists when adding new Field
raise RuntimeError(f"FieldSet already has a Field with name '{name}'")
if isinstance(field, NestedField):
setattr(self, name, field)
for fld in field:
self.gridset.add_grid(fld)
fld.fieldset = self
else:
setattr(self, name, field)
self.gridset.add_grid(field)
field.fieldset = self

I don't like the current implementation of .add_field() calling set_attr(self, name, field). Programmatically storing these fields messes with static typecheckers, and it would be clearer if this was handled another way (either a list of fields, a dictionary, or something else). We can set __getattr__ so users can still do fieldset.U

@VeckoTheGecko VeckoTheGecko added good first issue Good for new parcels developers coding/Python refactor code to refactor needs investigation labels Aug 20, 2024
@erikvansebille
Copy link
Member

Agree, this can be made much cleaner. Note though that this could also be touched upon / impacted by the unstructured grids development?

@erikvansebille erikvansebille added the unstructured grids Development of unstructured grids support label Aug 21, 2024
@VeckoTheGecko
Copy link
Contributor Author

Ah yes, definitely would be impacted by unstructured grids. Might be worth refactoring ahead of time in prep for v4 (so that development is easier), but it's low on my to-do list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
coding/Python good first issue Good for new parcels developers needs investigation refactor code to refactor unstructured grids Development of unstructured grids support
Projects
Status: Backlog
Development

No branches or pull requests

2 participants