roocs / clisops

Climate Simulation Operations
https://clisops.readthedocs.io/en/latest/
Other
21 stars 9 forks source link

Problem when lon_bnds set to None in subset_bbox #256

Open DamienIrving opened 1 year ago

DamienIrving commented 1 year ago

Description

I've written a command line program where argparse defines start_date, end_date, lat_bnds and lon_bnds options that are None by default (i.e. unless the user passes their desired values for those options at the command line). I then pass those values to subset_bbox as follows:

ds = subset_bbox(ds, start_date=start_date, end_date=end_date, lat_bnds=lat_bnds, lon_bnds=lon_bnds)

I figured this would work fine because in the clisops source code the default value for each of those keyword arguments is None in the subset_bbox function definition, but it fails if you pass lon_bnds=None. I think the issue might be related to how the check_lons decorator function handles the input?

What I Did

import xarray as xr
from clisops.core.subset import subset_bbox
ds = xr.tutorial.load_dataset("air_temperature")
ds
<xarray.Dataset>
Dimensions:  (lat: 25, time: 2920, lon: 53)
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  * lon      (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
  * time     (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
Data variables:
    air      (time, lat, lon) float32 241.2 242.5 243.5 ... 296.5 296.2 295.7
Attributes:
    Conventions:  COARDS
    title:        4x daily NMC reanalysis (1948)
    description:  Data is from NMC initialized reanalysis\n(4x/day).  These a...
    platform:     Model
    references:   http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly...
subset_bbox(ds, lon_bnds=None)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [5], line 1
----> 1 subset_bbox(ds, lon_bnds=None)

File /g/data/xv83/dbi599/miniconda3/envs/icclim/lib/python3.10/site-packages/clisops/core/subset.py:270, in check_lons.<locals>.func_checker(*args, **kwargs)
    267         else:
    268             kwargs[lon][kwargs[lon] <= 180] -= 360
--> 270 return func(*args, **kwargs)

File /g/data/xv83/dbi599/miniconda3/envs/icclim/lib/python3.10/site-packages/clisops/core/subset.py:1211, in subset_bbox(da, lon_bnds, lat_bnds, start_date, end_date, first_level, last_level, time_values, level_values)
   1209     if lon in da.dims and lon_bnds is not None:
   1210         lon_bnds = _check_desc_coords(coord=da[lon], bounds=lon_bnds, dim=lon)
-> 1211         da = da.sel({lon: slice(*lon_bnds)})
   1213 # Curvilinear case (lat and lon are coordinates, not dimensions)
   1214 elif ((lat in da.coords) and (lon in da.coords)) or (
   1215     (lat in da.data_vars) and (lon in da.data_vars)
   1216 ):
   1217     # Define a bounding box along the dimensions
   1218     # This is an optimization, a simple `where` would work but take longer for large hi-res grids.

TypeError: iteration over a 0-d array