Nihilum
Nihilum

Reputation: 701

Xarray select value based on variable

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

Its dimensions are:

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

Upvotes: 2

Views: 1738

Answers (1)

Michael Delgado
Michael Delgado

Reputation: 15442

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

Upvotes: 1

Related Questions