[Python] How to interpolate a multidimensional xarray DataArray?

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am using the xarray library and I have some doubts/questions.

    I have this dataset::

    ds
    <xarray.Dataset> Size: 2GB
    Dimensions: (Latitude: 364, Longitude: 246, Lon_u: 247, Lat_v: 364,
    Time: 1087)
    Coordinates:
    Longitude (Latitude, Longitude)
    Latitude (Latitude, Longitude)
    Lon_u (Latitude, Lon_u)
    Lat_v (Lat_v, Longitude)
    * Time (Time) datetime64[ns]
    Data variables:
    Lat_u (Latitude, Lon_u)
    Lon_v (Lat_v, Longitude)
    uSurf (Time, Latitude, Lon_u)
    vSurf (Time, Lat_v, Longitude)


    I want to interpolate the data (Lat_v > Latitude and Lon_u > Longitude). The result must be like something like this:

    ds
    <xarray.Dataset> Size: 2GB
    Dimensions: (Latitude: 364, Longitude: 246, Time: 1087)
    Coordinates:
    Longitude (Longitude)
    Latitude (Latitude)
    * Time (Time) datetime64[ns]
    Data variables:
    uSurf (Time, Latitude, Longitude)
    vSurf (Time, Latitude, Longitude)


    Or actually I don't know if converting 2D coordinates to 1D makes sense. I read that if they are 2D coordinates, they are logical coordinates (Longitude, Latitude), working as a irregular matrix. But I think the example above, it's also a irregular matrix just because the dimensions are different: {Latitude: 364, Longitude: 246}.

    So I don't know if I should preserve the coordinates like this:

    ds
    <xarray.Dataset> Size: 2GB
    Dimensions: (Latitude: 364, Longitude: 246, Lon_u: 247, Lat_v: 364,
    Time: 1087)
    Coordinates:
    Longitude (Latitude, Longitude)
    Latitude (Latitude, Longitude)
    * Time (Time) datetime64[ns]
    Data variables:
    uSurf (Time, Latitude, Longitude)
    vSurf (Time, Latitude, Longitude)


    I don't understand this notation though. Because I find it recursive. Imagine I want to get all the possible Latitude values: I can do this:

    ds.Latitude.values[:, 0]


    But Latitude is also a coordinate of Latitude itself. So I could do something like this

    ds.Latitude.Latitude.Latitude.Latitude.values[:, 0]


    Is that a normal behaviour? Or it's something wrong with my DataSet and I need to flatten it?

    I have tried this code:

    interp_coords = {'Latitude': ds.Latitude.values[:, 0], 'Longitude': ds.Longitude.values[0, :]}

    uSurf_interp = ds.uSurf.interp(
    Latitude=interp_coords['Latitude'],
    Lon_u=interp_coords['Longitude'],
    method='linear'
    )

    vSurf_interp = ds.vSurf.interp(
    Lat_v=interp_coords['Latitude'],
    Longitude=interp_coords['Longitude'],
    method='linear'
    )

    ds_interp = xr.Dataset(
    {
    'uSurf': uSurf_interp,
    'vSurf': vSurf_interp
    },
    coords={
    'Latitude': interp_coords['Latitude'],
    'Longitude': interp_coords['Longitude'],
    'Time': ds.Time
    }
    )


    I get this error in the first interp instruction:

    ValueError: Input DataArray is not 1-D.


    As interp accepts only 1-D coordinates. So I need to flatten the coordinates. It's one thing I wanted to avoid.

    I have read the documentation. But I cannot find a solution:


    I found this diagram quite ilustrative. But for 1D coordinates

    [​IMG]

    UPDATE


    I'm trying now to do it manually with np.meshgrid and scipy.interpolate.griddata. Also, I'm trying to combine them with xarray.apply_ufunc

    Continue reading...

Compartilhe esta Página