Skip to content

FieldSet.time_interval breaks for more than 2 non-overlapping models #2783

Description

@erikvansebille

Parcels version

v4

Description

While working on a production run for the Sargassum project, I ran into a bug in the fieldset.time_interval when there are more than two models and some are non-overlapping

(Note that the code below works without the addition of ds3)

Code sample

def test_multi_model_nonoverlapping_time_interval():
    ds1 = datasets_structured["ds_2d_left"][["U_A_grid", "V_A_grid", "grid"]]
    fieldset = FieldSet.from_sgrid_conventions(ds1, mesh="flat")

    ds2 = ds1.copy().rename({"U_A_grid": "U2", "V_A_grid": "V2"})
    ds2["time"] = (ds2["time"].dims, ds2["time"].data + np.timedelta64(timedelta(days=1000)), ds2["time"].attrs)
    fieldset += FieldSet.from_sgrid_conventions(ds2, mesh="flat")

    ds3 = ds1.copy().rename({"U_A_grid": "U3", "V_A_grid": "V3"})
    ds3["time"] = (ds3["time"].dims, ds3["time"].data + np.timedelta64(timedelta(days=2000)), ds3["time"].attrs)
    fieldset += FieldSet.from_sgrid_conventions(ds3, mesh="flat")

    fieldset.add_constant_field("constant_field", 1.0, mesh="flat")

    assert fieldset.time_interval is None

============================================================= FAILURES ==============================================================
___________________________________________ test_multi_model_nonoverlapping_time_interval ___________________________________________

    def test_multi_model_nonoverlapping_time_interval():
        ds1 = datasets_structured["ds_2d_left"][["U_A_grid", "V_A_grid", "grid"]]
        fieldset = FieldSet.from_sgrid_conventions(ds1, mesh="flat")
    
        ds2 = ds1.copy().rename({"U_A_grid": "U2", "V_A_grid": "V2"})
        ds2["time"] = (ds2["time"].dims, ds2["time"].data + np.timedelta64(timedelta(days=1000)), ds2["time"].attrs)
        fieldset += FieldSet.from_sgrid_conventions(ds2, mesh="flat")
    
        ds3 = ds1.copy().rename({"U_A_grid": "U3", "V_A_grid": "V3"})
        ds3["time"] = (ds3["time"].dims, ds3["time"].data + np.timedelta64(timedelta(days=2000)), ds3["time"].attrs)
        fieldset += FieldSet.from_sgrid_conventions(ds3, mesh="flat")
    
        fieldset.add_constant_field("constant_field", 1.0, mesh="flat")
    
>       assert fieldset.time_interval is None
               ^^^^^^^^^^^^^^^^^^^^^^

tests/test_fieldset.py:254: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <parcels._core.fieldset.FieldSet object at 0x31e2df230>, name = 'time_interval'

    def __getattr__(self, name):
        """Get the field by name. If the field is not found, check if it's a context variable."""
        if name in self._fields:
            return self._fields[name]
        elif name in self.context:
            return self.context[name]
        else:
>           raise AttributeError(f"FieldSet has no attribute '{name}'")
E           AttributeError: FieldSet has no attribute 'time_interval'

src/parcels/_core/fieldset.py:109: AttributeError

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions