xCDAT / xcdat

An extension of xarray for climate data analysis on structured grids.
https://xcdat.readthedocs.io/en/latest/
Apache License 2.0
115 stars 12 forks source link

[Bug]: `create_grid` breaks if an arg is a tuple with the second element being `None` #536

Closed tomvothecoder closed 1 year ago

tomvothecoder commented 1 year ago

What happened?

I ran into this issue during e3sm_diags development while testing a function to reconstruct pressure levels. I passed a single pressure level (e.g., plevs=[800]) to create_axis("lev", plevs, generate_bounds=True) and it threw an error about generating bounds requiring > 1 data points.

To workaround this, I tried setting generate_bounds=False. However, I ran into the issue of create_grid breaking because of a bug in the conditional for copying bounds as described below.

Steps to reproduce (refer to MVCE):

  1. Use z_axis = xc.create_axis(...generate_bounds=False), which will return a tuple consisting of (xr.DataArray, None).
  2. Pass this tuple as an argument to xc.create_grid(z=z_axis)
  3. xc.create_grid breaks on line 544 -- AttributeError: 'NoneType' object has no attribute 'copy'

What did you expect to happen? Are there are possible answers you came across?

The conditional to check the argument type for create_grid should also check if the second element is None and execute the appropriate logic

https://github.com/xCDAT/xcdat/blob/08cfea37cf47454abea83de66cf7677ab157708b/xcdat/regridder/grid.py#L532-L553

Minimal Complete Verifiable Example (MVCE)

import xcdat as xc

plevs = [800, 200]

# Create the output pressure grid to regrid to using the `plevs` array.
z_axis = xc.create_axis("lev", plevs, generate_bounds=False)
pressure_grid = xc.create_grid(z=z_axis)

Relevant log output

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/global/homes/v/vo13/mambaforge/envs/e3sm_diags_dev_658/lib/python3.10/site-packages/xcdat/regridder/grid.py", line 544, in create_grid
    axis, bnds = item[0].copy(deep=True), item[1].copy(deep=True)  # type: ignore[union-attr]
AttributeError: 'NoneType' object has no attribute 'copy'

Anything else we need to know?

No response

Environment

xcdat=0.6.0rc1 and main branch

tomvothecoder commented 1 year ago

@jasonb5 I can open up a PR for this issue since you have plenty of other GH issues you're working on.

jasonb5 commented 1 year ago

@tomvothecoder Thanks!