Skip to content

Error with indexing 2D lat/lon coordinates #4090

Closed

Description

@shoyer
filslp = "ChonghuaYinData/prmsl.mon.mean.nc"
filtmp = "ChonghuaYinData/air.sig995.mon.mean.nc"
filprc = "ChonghuaYinData/precip.mon.mean.nc"

ds_slp = xr.open_dataset(filslp).sel(time=slice(str(yrStrt)+'-01-01', str(yrLast)+'-12-31'))

ds_slp

outputs:

<xarray.Dataset>
Dimensions:            (nbnds: 2, time: 480, x: 349, y: 277)
Coordinates:
  * time               (time) datetime64[ns] 1979-01-01 ... 2018-12-01
    lat                (y, x) float32 ...
    lon                (y, x) float32 ...
  * y                  (y) float32 0.0 32463.0 64926.0 ... 8927325.0 8959788.0
  * x                  (x) float32 0.0 32463.0 64926.0 ... 11264660.0 11297120.0
Dimensions without coordinates: nbnds
Data variables:
    Lambert_Conformal  int32 ...
    prmsl              (time, y, x) float32 ...
    time_bnds          (time, nbnds) float64 ...
Attributes:
    Conventions:    CF-1.2
    centerlat:      50.0
    centerlon:      -107.0
    comments:       
    institution:    National Centers for Environmental Prediction
    latcorners:     [ 1.000001  0.897945 46.3544   46.63433 ]
    loncorners:     [-145.5       -68.32005    -2.569891  148.6418  ]
    platform:       Model
    standardpar1:   50.0
    standardpar2:   50.000001
    title:          NARR Monthly Means
    dataset_title:  NCEP North American Regional Reanalysis (NARR)
    history:        created 2016/04/12 by NOAA/ESRL/PSD
    references:     https://www.esrl.noaa.gov/psd/data/gridded/data.narr.html
    source:         http://www.emc.ncep.noaa.gov/mmb/rreanl/index.html
    References:     
yrStrt  = 1950          # manually specify for convenience
yrLast  = 2018          # 20th century ends 2018  

clStrt  = 1950          # reference climatology for SOI
clLast  = 1979          

yrStrtP = 1979          # 1st year GPCP
yrLastP = yrLast        # match 20th century

latT = -17.6         # Tahiti
lonT = 210.75  
latD = -12.5         # Darwin 
lonD = 130.83  

# select grids of T and D
T = ds_slp.sel(lat=latT, lon=lonT, method='nearest')
D = ds_slp.sel(lat=latD, lon=lonD, method='nearest')

outputs:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-6702b30f473f> in <module>
      1 # select grids of T and D
----> 2 T = ds_slp.sel(lat=latT, lon=lonT, method='nearest')
      3 D = ds_slp.sel(lat=latD, lon=lonD, method='nearest')

~\Anaconda3\lib\site-packages\xarray\core\dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2004         indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
   2005         pos_indexers, new_indexes = remap_label_indexers(
-> 2006             self, indexers=indexers, method=method, tolerance=tolerance
   2007         )
   2008         result = self.isel(indexers=pos_indexers, drop=drop)

~\Anaconda3\lib\site-packages\xarray\core\coordinates.py in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    378 
    379     pos_indexers, new_indexes = indexing.remap_label_indexers(
--> 380         obj, v_indexers, method=method, tolerance=tolerance
    381     )
    382     # attach indexer's coordinate to pos_indexers

~\Anaconda3\lib\site-packages\xarray\core\indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
    257     new_indexes = {}
    258 
--> 259     dim_indexers = get_dim_indexers(data_obj, indexers)
    260     for dim, label in dim_indexers.items():
    261         try:

~\Anaconda3\lib\site-packages\xarray\core\indexing.py in get_dim_indexers(data_obj, indexers)
    223     ]
    224     if invalid:
--> 225         raise ValueError("dimensions or multi-index levels %r do not exist" % invalid)
    226 
    227     level_indexers = defaultdict(dict)

ValueError: dimensions or multi-index levels ['lat', 'lon'] do not exist

Does any know how fix to this problem?Thank you very much.

Originally posted by @JimmyGao0204 in #475 (comment)

Activity

  1. stale commented on Apr 28, 2022

    @stale

    In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

    If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

  2. benbovy commented on Sep 28, 2022

    @benbovy
    Member

    @JimmyGao0204 this is not supported by Xarray itself but the xoak has been developed for that purpose.

    I'm going to close this issue as Xarray now provides everything needed for selecting data using 2D lat/lon coordinates (i.e., advanced indexing, flexible indexes), and it is likely that this specific case will be further maintained in a 3rd party library like xoak. Feel free to comment / re-open if you think this should be built-in Xarray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions