Skip to content

FieldSet.from_netcdf when the NetCDF files have depth dimension #1958

@albertotb

Description

@albertotb

Parcels version

3.1.2

Description

I'm trying to update from OceanParcels v2.3.0 to v3.1.2. I was following the example to create a FieldSet from netCDF files: https://docs.oceanparcels.org/en/latest/examples/parcels_tutorial.html#Reading-in-data-from-arbritrary-NetCDF-files

However, my files do have a depth dimension:

Image

Otherwise, they have a similar structure to the example ones:

Image

However, when trying to create a FieldSet I'm getting this error:

fieldset = parcels.FieldSet.from_netcdf(
    {
        "U": ["./test/data/uv/2020-03-20.nc", "./test/data/uv/2020-03-21.nc"],
        "V": ["./test/data/uv/2020-03-20.nc", "./test/data/uv/2020-03-21.nc"],
    },
    variables={"U": "uo", "V": "vo"},
    dimensions={
        "time": "time",
        "depth": "depth",
        "lat": "latitude",
        "lon": "longitude",
    },
)
AssertionError: Field uo expecting a data shape of [tdim=2, zdim=26, ydim=241, xdim=241] but got shape (1, 26, 241, 241). Flag transpose=True could help to reorder the data.

I tried the suggestion of using transpose=True, but no luck. For some reason, it seems to expect 2 values in the time dimension, but each NetCDF file only has one, you need to combine them both. I went ahead and removed the depth dimension from the files:

Image

And that seems to work:

fieldset = parcels.FieldSet.from_netcdf(
    {
        "U": [
            "./test/data/uv/2020-03-20_nodepth.nc",
            "./test/data/uv/2020-03-21_nodepth.nc",
        ],
        "V": [
            "./test/data/uv/2020-03-20_nodepth.nc",
            "./test/data/uv/2020-03-21_nodepth.nc",
        ],
    },
    variables={"U": "uo", "V": "vo"},
    dimensions={
        "time": "time",
        "lat": "latitude",
        "lon": "longitude",
    },
)

I'm not sure if this is a bug, but this same code used to work in OceanParcels v2.3.0 with both depth and time dimension.

Code sample

I know that to run the example you need the data files. If required, I can try and upload them somewhere, because they are too big to be attached directly in Github.

fieldset = parcels.FieldSet.from_netcdf(
    {
        "U": ["./test/data/uv/2020-03-20.nc", "./test/data/uv/2020-03-21.nc"],
        "V": ["./test/data/uv/2020-03-20.nc", "./test/data/uv/2020-03-21.nc"],
    },
    variables={"U": "uo", "V": "vo"},
    dimensions={
        "time": "time",
        "depth": "depth",
        "lat": "latitude",
        "lon": "longitude",
    },
)
AssertionError: Field uo expecting a data shape of [tdim=2, zdim=26, ydim=241, xdim=241] but got shape (1, 26, 241, 241). Flag transpose=True could help to reorder the data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions