I have an xarray.Dataset that looks like this, which is available here: https://psl.noaa.gov/thredds/dodsC/Datasets/NARR/Dailies/monolevel/acpcp.1979.nc:
<xarray.Dataset>
Dimensions: (time: 365, y: 277, x: 349, nbnds: 2)
Coordinates:
* time (time) datetime64[ns] 1979年01月01日 ... 1979年12月31日
* y (y) float32 0.0 3.246e+04 ... 8.927e+06 8.96e+06
* x (x) float32 0.0 3.246e+04 ... 1.126e+07 1.13e+07
lat (y, x) float32 1.0 1.104 1.208 ... 46.93 46.64 46.35
lon (y, x) float32 -145.5 -145.3 -145.1 ... -2.644 -2.57
Dimensions without coordinates: nbnds
Data variables:
Lambert_Conformal int32 ...
time_bnds (time, nbnds) float64 ...
acpcp (time, y, x) float32 ...
Attributes: (12/17)
Conventions: CF-1.2
centerlat: 50.0
centerlon: -107.0
comments:
institution: National Centers for Environmental Predi...
latcorners: [ 1.000001 0.897945 46.3544 46.63433 ]
... ...
history: created Mon Mar 21 17:56:47 MDT 2016 by ...
dataset_title: NCEP North American Regional Reanalysis ...
references: https://www.esrl.noaa.gov/psd/data/gridd...
source: http://www.emc.ncep.noaa.gov/mmb/rreanl/...
_NCProperties: version=2,netcdf=4.6.3,hdf5=1.10.5
DODS_EXTRA.Unlimited_Dimension: time
And I want to be able to select a given point using .sel based on the lat and lon coordinates like:
ds.sel(lat=point_lat, lon=point_lon, method='nearest')
But I get KeyError: 'no index found for coordinate lat'. I think this makes sense because lat is a coordinate but not a dimension.
So, what would be the best way to be able to use .sel in this case - is there a way to simply add lat and lon to the set of dimensions?
-
I am pretty sure there must be a global attribute by the name of "featureType" with a value of "timeSeries". Can you please confirm?Patrick– Patrick2025年05月30日 17:42:04 +00:00Commented May 30, 2025 at 17:42
-
Do you have a link to the file?Patrick– Patrick2025年05月30日 17:47:26 +00:00Commented May 30, 2025 at 17:47
-
@Patrick Updated example for a case that isn't a single point with a link to the filepbreach– pbreach2025年06月02日 12:40:56 +00:00Commented Jun 2, 2025 at 12:40
1 Answer 1
This netCDF file has only a single latitude-longitude point. lat and lon coordinates use the station dimension which has a length of 1. lat and lon are thus also not coordinate variables (in the sense of the CF Metadata Conventions) and I am a bit surprised that xarray would list them as coordinates but then not allow you to select on them (which indeed does not make sense). I see a lat value of 45.16, which would place this in Lake Michigan or Lake Huron, most likely.
What you have here is most likely a netCDF file with discrete sampling geometries (more CF-speak), in this case a timeSeries of 1-minute interval (time, 1 million+ length over nearly 2 years) of temperature measured at 18 depths (z). You can select on either of these two dimensions as usual.
In short, this is data for a single location. If you want to select data for another location, you should get a gridded data product.