1

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

enter image description here

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.