如何使用xarray提取最接近坐标的变量值?

时间:2019-03-29 17:07:52

标签: python pandas python-xarray

问题

我有这个数据集,我想要针对特定​​时间,纬度和经度提取swnet的值。如果没有确切的组合,则取最接近的值。

<xarray.Dataset>
Dimensions:           (lat: 92, lon: 172, time: 183)
Coordinates:
  * lat               (lat) float32 4.125001 4.375 4.625 ... 26.624994 26.874996
  * lon               (lon) float32 nan nan nan ... 24.374996 24.624998 24.875
  * time              (time) datetime64[ns] 2003-09-01 2003-09-02 ... 2004-03-01
Data variables:
    swnet        (time, lat, lon) float32 dask.array<shape=(183, 92, 172), chunksize=(1, 92, 172)>

尝试

df.sel(time='2003-09-01', lon=6.374997, lat=16.375006, method='nearest')

错误摘录

   2738 
   2739         if not self.is_unique:
-> 2740             raise InvalidIndexError('Reindexing only valid with uniquely'
   2741                                     ' valued Index objects')
   2742 

InvalidIndexError: Reindexing only valid with uniquely valued Index objects

诊断数据

df.lat

<xarray.DataArray 'lat' (lat: 92)>
array([ 4.125001,  4.375   ,  4.625   ,  4.875   ,  5.125   ,  5.375   ,
        5.625   ,  5.875   ,  6.125001,  6.374997,  6.625001,  6.875002,
        7.124997,  7.375001,  7.625003,  7.874998,  8.125002,  8.375004,
        8.624998,  8.875002,  9.125005,  9.375001,  9.625   ,  9.874996,
       10.124999, 10.375003, 10.625   , 10.875004, 11.124999, 11.374996,
       11.625   , 11.875002, 12.125002, 12.375006, 12.624997, 12.874997,
       13.125   , 13.375004, 13.625004, 13.875008, 14.124997, 14.374997,
       14.625   , 14.875005, 15.125004, 15.374993, 15.624997, 15.874997,
       16.125   , 16.375006, 16.625006, 16.874994, 17.124996, 17.375   ,
       17.624998, 17.875004, 18.125004, 18.374994, 18.624994, 18.875   ,
       19.125002, 19.375004, 19.625008, 19.875   , 20.125002, 20.375008,
       20.624996, 20.874998, 21.12499 , 21.374994, 21.624996, 21.875   ,
       22.125004, 22.375006, 22.625011, 22.875004, 23.125006, 23.375011,
       23.624994, 23.874996, 24.12499 , 24.374996, 24.624998, 24.875   ,
       25.125006, 25.375008, 25.625004, 25.875006, 26.125011, 26.374989,
       26.624994, 26.874996], dtype=float32)
Coordinates:
  * lat      (lat) float32 4.125001 4.375 4.625 ... 26.624994 26.874996

df.lon

<xarray.DataArray 'lon' (lon: 172)>
array([       nan,        nan,        nan,        nan,        nan,        nan,
              nan,        nan,        nan,        nan,        nan,        nan,
              nan,        nan,        nan,        nan,        nan,        nan,
       -13.375004, -13.125   , -12.874997, -12.624997, -12.375006, -12.125002,
       -11.875002, -11.625   , -11.374996, -11.124999, -10.875004, -10.625   ,
       -10.375003, -10.124999,  -9.874996,  -9.625   ,  -9.375001,  -9.125005,
        -8.875002,  -8.624998,  -8.375004,  -8.125002,  -7.874998,  -7.625003,
        -7.375001,  -7.124997,  -6.875002,  -6.625001,  -6.374997,  -6.125001,
        -5.875   ,  -5.625   ,  -5.375   ,  -5.125   ,  -4.875   ,  -4.625   ,
        -4.375   ,  -4.125001,  -3.874999,  -3.625   ,  -3.375001,  -3.124999,
        -2.874999,  -2.625001,  -2.375   ,  -2.125   ,  -1.875   ,  -1.625   ,
        -1.375   ,  -1.125   ,  -0.875   ,  -0.625   ,  -0.375   ,  -0.125   ,
         0.125   ,   0.375   ,   0.625   ,   0.875   ,   1.125   ,   1.375   ,
         1.625   ,   1.875   ,   2.125   ,   2.375   ,   2.625001,   2.874999,
         3.124999,   3.375001,   3.625   ,   3.874999,   4.125001,   4.375   ,
         4.625   ,   4.875   ,   5.125   ,   5.375   ,   5.625   ,   5.875   ,
         6.125001,   6.374997,   6.625001,   6.875002,   7.124997,   7.375001,
         7.625003,   7.874998,   8.125002,   8.375004,   8.624998,   8.875002,
         9.125005,   9.375001,   9.625   ,   9.874996,  10.124999,  10.375003,
        10.625   ,  10.875004,  11.124999,  11.374996,  11.625   ,  11.875002,
        12.125002,  12.375006,  12.624997,  12.874997,  13.125   ,  13.375004,
        13.625004,  13.875008,  14.124997,  14.374997,  14.625   ,  14.875005,
        15.125004,  15.374993,  15.624997,  15.874997,  16.125   ,  16.375006,
        16.625006,  16.874994,  17.124996,  17.375   ,  17.624998,  17.875004,
        18.125004,  18.374994,  18.624994,  18.875   ,  19.125002,  19.375004,
        19.625008,  19.875   ,  20.125002,  20.375008,  20.624996,  20.874998,
        21.12499 ,  21.374994,  21.624996,  21.875   ,  22.125004,  22.375006,
        22.625011,  22.875004,  23.125006,  23.375011,  23.624994,  23.874996,
        24.12499 ,  24.374996,  24.624998,  24.875   ], dtype=float32)
Coordinates:
  * lon      (lon) float32 nan nan nan nan ... 24.374996 24.624998 24.875

0 个答案:

没有答案