'Xarray select value based on variable

I have a .nc file that I open with xarray as a dataset. This dataset has 3 variables:

  • Band (5000x300x250)
  • latitude (300x250)
  • longitude (300x250)

Its dimensions are:

  • time (5000)
  • y (300)
  • x (250)

I created the dataset myself and made a mistake, because I would like to "grab" the timeseries of a specific point of "Band" based on its coordinates value:

dataset.Band.sel(longitude=6.696e+06,latitude=4.999e+05,method='nearest')

(I based the values to grab on the first values of both variables).

The issue is that when I created the .nc file, I did not enter the latitude and longitude as dimensions but as variables. Is there a way to use my code but modify a few things so I can grab the point based on the nearest values of variables latitude and longitude ? Or should I redefine completely the dimensions of my .nc to replace x and y by longitude and latitude?

Dataset Description



Solution 1:[1]

there isn't a great way to select data using the lat/lon values - as your data is structured you essentially have mutlidimensional coordinates.

That said, if your lat/lon are actually only indexed by x OR y; that is, latitude has the same value repeated over and over for all levels of x and same for longitude with y, you could reorganize your data pretty easily:

lats = dataset.latitude.mean(dim='x')
lons = dataset.longitude.mean(dim='y')
dataset = dataset.drop(['latitude', 'longitude'])
dataset.coords['latitude'] = latitude
dataset.coords['longitude'] = longitude
dataset = dataset.swap_dims({'x': 'longitude', 'y': 'latitude'})

At this point, your data is indexed by time, latitude, longitude and you can select the data how you'd like

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Delgado