0

I am trying to extract two variables values of a netCDF file at specific coordinates. From what I have seen from various posts on StackOverflow, the best way to achieve that is to work with arrays.

I have tried to use this method but it does not work and I am struggling finding out why.

The code I used is the following:

import xarray as xr
file = "input.nc"
lon=24.2
lat=17.8

da = xr.open_dataset(file)
ts = da.sel(lon=lon, lat=lat, method="nearest")   #I also tried da["variable1"].sel(lon=lon, lat=lat, method="nearest") but same outcome.

But it raised this error:

KeyError: 'lon is not a valid dimension or coordinate'

I then tried to set lon and lat as coordinates (da.set_coords(("lat", "lon"))) but it still raised an error:

KeyError: 'no index found for coordinate lon'

I have very rarely worked with arrays so I don't understand what is wrong here. Here are more details of my netCDF:

xarray.Dataset
Dimensions: south_north: 63, west_east: 69, vcoord_dim: 8, Time: 25, bottom_top: 8
Coordinates: (0)  # For some reasons lon and lat are Data variables and not Coordinates in this netCDF.
Data variables: (216)

I also tried a more mannual extraction method (validated answer here) but it gave an index value way out of bound for the j that I can also not explain.


So, to summarize, what I want is extract the values of some Data variables at specific lon/lat values for all the timesteps of the netCDF.

I looked everywhere for answers but nothing helped me understand what was wrong. Any tips and advices would be greatly appreciated.

jhamman
  • 5,867
  • 19
  • 39
I.M.
  • 344
  • 3
  • 14
  • Xarray did not find any coordinates (e.g. lat, lon) for your dataset using the standard CF conventions. You may need to manually set up the coordinates. For that, you can use https://docs.xarray.dev/en/stable/generated/xarray.Dataset.set_coords.html after opening the dataset. – jhamman Jan 08 '23 at 03:08
  • your coordinates seem to be named `south_north` and `east_west`, not `lat`, `lon`. Just use the name of your coordinates instead of `lat/lon` and it should work. – Michael Delgado Jan 08 '23 at 03:48
  • @MichaelDelgado I tried but the following error was raised: ```ValueError: cannot supply ``method`` or ``tolerance`` when the indexed dimension does not have an associated coordinate.``` Is it maybe possible to change the coordinates name from ```south_north``` and ```east_west``` to ```lon``` and ```lat```? – I.M. Jan 08 '23 at 08:38
  • @jhamman This is the first thing I tried to do as showed in my question but setting coordinates gave me the following error: ```KeyError: 'no index found for coordinate lon'``` – I.M. Jan 08 '23 at 08:40
  • Lat and Lon do not seem to be variables in your dataset. Is there another variable name (of your 216) that you can promote to a coordinate? – jhamman Jan 08 '23 at 14:54
  • @jhamman Lon and Lat are datavariables (of the 216), I did try to set them as coordinates but it still wasn't working. Other than that, no other variables can be used as coordinates for this file in particular. – I.M. Jan 08 '23 at 14:59
  • Can you update your post to include the repr of the lat/lon variables? – jhamman Jan 08 '23 at 15:23

0 Answers0