Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the problem that inside my nc dataset:
Dimensions are x and y
Coordinates are longitude and latitude:

<xarray.Dataset>
Dimensions:                 (x: 521, xfit: 6, y: 420)
Coordinates:
    longitude               (y, x) float32 ...
    latitude                (y, x) float32 ...
Dimensions without coordinates: x, xfit, y


As you know it is easy to call and value based on index by x and y. for example
if I want to call variable sar_wind which is available, you can use this code

wind_speed_at_x_y_index = ds.sar_wind.isel(x=14,y=45).values.
You will get the speed wind at this index.

Nevertheless, It is important to find the values at given longitude and latitude. I still have no idea how can I call based on longitude and latitude which are the coordinates here.

Is there any way to reshape/reorganize the data to call the values directly using longitude and latitude coordinates?

What I have tried:

I have tried many different options:

option one:
Python
print(ds.sar_wind.where(longitude=14,latitude=15).values)


I got the following error:
TypeError: where() got an unexpected keyword argument 'longitude'


I tried with other functions like sel, I got the same error

The longitude and latitude values are float numbers and not integer.
Like
[4.8334117 4.841021  4.848631  ... 8.818756  8.826535  8.834313 ]
 [4.831756  4.8393664 4.8469768 ... 8.817567  8.825347  8.833126 ]
 [4.8301    4.8377113 4.8453226 ... 8.816378  8.824158  8.831939 ]
Posted
Updated 24-Jan-21 3:01am
Comments
Christian Graus 23-Jan-21 20:22pm    
What are X and Y? How do they translate to Lat/Long?
EngAb1989 23-Jan-21 21:42pm    
x and y are the dimensions of the array
x and y together are like numpy array with size 521*420..each single value refers to stored longitude and latitude value
https://geohackweek.github.io/nDarrays/02-xarray-architecture/

1 solution

When working with floating point values you usually search for the 'nearest value' match instead of 'equal value' match (there are good reasons for that).
In order to search for the 'nearest value' match, a good strategy could be a binary search on ordered values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900