parcels icon indicating copy to clipboard operation
parcels copied to clipboard

Codebase wide `Path` object support

Open VeckoTheGecko opened this issue 1 year ago • 1 comments

Parcels version

v3.0.5

Description

Field.from_netcdf() doesn't work for filenames provided as Path object. It would be good to ensure the whole codebase is Path object compatible.

Code sample

from pathlib import Path

@pytest.mark.parametrize("with_timestamps", [False])
def test(with_timestamps):
    TEST_DATA = Path(__file__).resolve().parent / "test_data"
    filenames = {
        "lon": TEST_DATA / "mask_nemo_cross_180lon.nc",
        "lat": TEST_DATA / "mask_nemo_cross_180lon.nc",
        "data": TEST_DATA / "Uu_eastward_nemo_cross_180lon.nc",
    }
    variable = "U"
    dimensions = {"lon": "glamf", "lat": "gphif"}
    
    Field.from_netcdf(filenames, variable, dimensions, interp_method="cgrid_velocity")

        lonlat_filename = cls.get_dim_filenames(filenames, "lon")
        if isinstance(filenames, dict):
>           assert len(lonlat_filename) == 1
E           TypeError: object of type 'PosixPath' has no len()

parcels/field.py:477: TypeError

VeckoTheGecko avatar Sep 16 '24 12:09 VeckoTheGecko

As @michaeldenes mentioned this is an enhancement not a bug :))

VeckoTheGecko avatar Sep 16 '24 13:09 VeckoTheGecko

from pathlib import Path

@pytest.mark.parametrize("with_timestamps", [False])
def test(with_timestamps):
    TEST_DATA = Path(__file__).resolve().parent / "test_data"
    filenames = {
        "lon": TEST_DATA / "mask_nemo_cross_180lon.nc",
        "lat": TEST_DATA / "mask_nemo_cross_180lon.nc",
        "data": TEST_DATA / "Uu_eastward_nemo_cross_180lon.nc",
    }
    variable = "U"
    dimensions = {"lon": "glamf", "lat": "gphif"}
    
    Field.from_netcdf(filenames, variable, dimensions, interp_method="cgrid_velocity")

01efbb8 implements this test case. I wouldn't go so far to say we now have full Path support, but its a step in the right direction

VeckoTheGecko avatar Dec 04 '24 18:12 VeckoTheGecko

Only relevant to v3 (will be free in v4). Closing

VeckoTheGecko avatar Feb 06 '25 15:02 VeckoTheGecko