question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError when applying xgcm.Grid.cumsum on the LAYERS package output

See original GitHub issue

I have a xgcm.grid object that is recognizing the layers grid:

import xgcm.grid as Grid
grid = Grid(ds, periodic=['X'])
grid
--------
<xgcm.Grid>
Z Axis (not periodic):
  * center   Z (40) --> left
  * left     Zl (40) --> center
  * outer    Zp1 (41) --> center
  * right    Zu (40) --> center
Y Axis (not periodic):
  * center   YC (200) --> left
  * left     YG (200) --> center
X Axis (periodic):
  * center   XC (100) --> left
  * left     XG (100) --> center
1TH Axis (not periodic):
  * center   layer_1TH_center (42) --> outer
  * outer    layer_1TH_bounds (43) --> center
  * inner    layer_1TH_interface (41) --> center
T Axis (not periodic):
  * center   time (3600)

However, when I try to apply the grid.cumsum function over the 1TH Axis, I get the following KeyError:

vname = 'LaVH1TH' 
mean_thkns = ds[vname].mean(['time','XC'])
print(mean_thkns)
grid.cumsum(mean_thkns[::-1], 'layer_1TH_center')
--------
<xarray.DataArray 'LaVH1TH' (layer_1TH_center: 42, YG: 200)>
dask.array<shape=(42, 200), dtype=float32, chunksize=(42, 200)>
Coordinates:
  * YG                (YG) >f4 0.0 10000.0 20000.0 30000.0 40000.0 50000.0 ...
  * layer_1TH_center  (layer_1TH_center) float32 -0.1 0.1 0.3 0.5 0.7 0.9 ...
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-32-7d428bd4e21f> in <module>()
      2 mean_thkns = ds[vname].mean(['time','XC'])
      3 print(mean_thkns)
----> 4 grid.cumsum(mean_thkns, 'layer_1TH_center')

~/xgcm/xgcm/grid.py in cumsum(self, da, axis, **kwargs)
    552         """
    553 
--> 554         ax = self.axes[axis]
    555         return ax.cumsum(da, **kwargs)
    556 

KeyError: 'layer_1TH_center'

Is there a work around for this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
rabernatcommented, Mar 27, 2018

Do you understand how I debugged this? The important step is to understand the nature of this error

--> 554         ax = self.axes[axis]
    555         return ax.cumsum(da, **kwargs)
    556 

KeyError: 'layer_1TH_center'

You could have examined grid.axes yourself to verify that ‘layer_1TH_center’ was not a valid key.

1reaction
rabernatcommented, Mar 27, 2018

The name of the axis is 1TH, not layer_1TH_center. layer_1TH_center is the name of a coordinate, not an axis. So you should do

grid.cumsum(mean_thkns, '1TH')

(Also, do not reverse the data. that will not work with xgcm.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source code for xgcm.grid
"i.e. use `Grid.cumsum` instead of `Axis.cumsum`", FutureWarning, ) pos, ... except KeyError: raise KeyError("Position '%s' was not found in axis.coords.
Read more >
DataSet()方法的代码示例 - Python源码库
Grid attributes are assumed to have their internal aospy names. ... and/or unlabeled arrays to which to apply the function. y : Dataset,...
Read more >
The xgcm from xgcm - GithubHelp
KeyError when applying xgcm.Grid.cumsum on the LAYERS package output. I have a xgcm.grid object that is recognizing the layers grid: import xgcm.grid as ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found