euroargodev / euroargodev/argopy
is_indexbox can fails even with valid box
- Dominant language
- Python
- Stars
- 229
- Forks
- 52
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 8
Description
The ``argopy.utils.is_indexbox`` utility raises an error with valid longitudes.
#### MCVE Code Sample
```python
import argopy
b = [-80, 15., 15, 78, '19000101', '20991231']
argopy.ArgoIndex().query.box(b)
```
Raises a ``ValueError: [-80, 15.0, 15, 78, '19000101', '20991231']: lon_max must be larger than lon_min``
Error stack
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[8], line 2
1 b = [-80, 15., 15, 78, '19000101', '20991231']
----> 2 f = argopy.ArgoIndex().query.box(b)
File [~/git/github/euroargodev/argopy/argopy/stores/index/implementations/pyarrow/search_engine.py:306](http://localhost:8888/lab/workspaces/auto-U/tree/~/git/github/euroargodev/argopy/argopy/stores/index/implementations/pyarrow/search_engine.py#line=305), in SearchEngine.box(self, BOX, nrows, composed)
298 filt.append(
299 pa.compute.less_equal(
300 pa.compute.cast(self._obj.index[key], pa.timestamp("ms")),
301 pa.array([pd.to_datetime(BOX[5])], pa.timestamp("ms"))[0],
302 )
303 )
304 return self._obj._reduce_a_filter_list(filt, op="and")
--> 306 key = checker(BOX)
307 self._obj.load(nrows=self._obj._nrows_index)
308 search_filter = composer(BOX, key)
File [~/git/github/euroargodev/argopy/argopy/stores/index/implementations/pyarrow/search_engine.py:279](http://localhost:8888/lab/workspaces/auto-U/tree/~/git/github/euroargodev/argopy/argopy/stores/index/implementations/pyarrow/search_engine.py#line=278), in SearchEngine.box..checker(BOX)
277 if "longitude" not in self._obj.convention_columns:
278 raise InvalidDatasetStructure("Cannot search for coordinates in this index")
--> 279 is_indexbox(BOX)
280 log.debug("Argo index searching for lat[/lon/date](http://localhost:8888/lon/date) in BOX=%s ..." % BOX)
281 return "date"
File [~/git/github/euroargodev/argopy/argopy/utils/checkers.py:115](http://localhost:8888/lab/workspaces/auto-U/tree/~/git/github/euroargodev/argopy/argopy/utils/checkers.py#line=114), in is_indexbox(box, errors)
112 break
114 if error and errors == "raise":
--> 115 raise ValueError("%s: %s" % (box, error))
116 elif error:
117 return False
ValueError: [-80, 15.0, 15, 78, '19000101', '20991231']: lon_max must be larger than lon_min
#### The issue
The failed box validation test is from this line:
```python
tests["lon_max must be larger than lon_min"] = lambda b: conv_lon(b[0], '360') < conv_lon(b[1], '360')
```
so this bug could be fixed for this box using a `180` convention on longitude.
But then, a box with longitude going through the dateline would fail !
#### Solution
A real fix would be to check if the first longitude is to the West of the 2nd longitude, and testing that the 2 longitudes would make an angle smaller than if they would be flipped.
This seems to be a fairly standard issue when dealing with longitudes, some bibliography is required here.
In the mean time, we implement a temporary solution by raising a log warning instead of an error for this test.
Contributor guide
Research direction
Start in argopy/utils/checkers.py at is_indexbox and reproduce the MCVE through SearchEngine.box in argopy/stores/index/implementations/pyarrow/search_engine.py. Review the longitude validation and its existing tests, then research the stated dateline and longitude-convention cases. Done means valid boxes no longer raise incorrectly while dateline-crossing boxes remain correctly handled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, data
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100