Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble with sortby() #56

Closed
kingkong-cmd opened this issue Sep 1, 2024 · 4 comments
Closed

Trouble with sortby() #56

kingkong-cmd opened this issue Sep 1, 2024 · 4 comments

Comments

@kingkong-cmd
Copy link

I'm getting an error when trying to reorder a grib file from NOAA to make it work with openlayers. Since the raw file comes with 0 to 360 lons, I just minus 180 from the lons above 180. This works fine, but I want to reorder so that the data now goes from -180 to 180.

However, when I try ds.sortby('logintude') I get AttributeError: 'GribberishBackendArray' object has no attribute 'oindex'. Same if i try to sort by latitude. Am I doing something wrong?

Python code to reproduce:

import xarray as xr
input_grib_filename = 'tmp/grib/2024082912/12/1p00/2024082912_1p00_f000.grb'
ds = xr.open_dataset(input_grib_filename, engine='gribberish')
ds.coords['longitude'] = (ds.coords['longitude'] + 180) % 360 - 180
ds.sortby('longitude')

Content of ds after -180 operation:

<xarray.Dataset> Size: 1MB
Dimensions:    (time: 1, latitude: 181, longitude: 360)
Coordinates:
  * time       (time) datetime64[ns] 8B 2024-08-29T12:00:00
  * latitude   (latitude) float64 1kB 90.0 89.0 88.0 87.0 ... -88.0 -89.0 -90.0
  * longitude  (longitude) float64 3kB 0.0 1.0 2.0 3.0 ... -4.0 -3.0 -2.0 -1.0
Data variables:
    ugrd       (time, latitude, longitude) float64 521kB ...
    vgrd       (time, latitude, longitude) float64 521kB ...
Attributes:
    meta:     Generated with gribberishpy

Error trace

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/venv/lib/python3.10/site-packages/xarray/core/dataset.py", line 8023, in sortby
    return aligned_self.isel(indices)
  File "/venv/lib/python3.10/site-packages/xarray/core/dataset.py", line 2963, in isel
    var = var.isel(var_indexers)
  File "/venv/lib/python3.10/site-packages/xarray/core/variable.py", line 1018, in isel
    return self[key]
  File "/venv/lib/python3.10/site-packages/xarray/core/variable.py", line 785, in __getitem__
    data = indexing.apply_indexer(indexable, indexer)
  File "/venv/lib/python3.10/site-packages/xarray/core/indexing.py", line 1000, in apply_indexer
    return indexable.oindex[indexer]
  File "/venv/lib/python3.10/site-packages/xarray/core/indexing.py", line 342, in __getitem__
    return self.getter(key)
  File "/venv/lib/python3.10/site-packages/xarray/core/indexing.py", line 813, in _oindex_get
    return type(self)(_wrap_numpy_scalars(self.array.oindex[indexer]))
  File "/venv/lib/python3.10/site-packages/xarray/core/indexing.py", line 342, in __getitem__
    return self.getter(key)
  File "/venv/lib/python3.10/site-packages/xarray/core/indexing.py", line 763, in _oindex_get
    return type(self)(_wrap_numpy_scalars(self.array.oindex[indexer]))
AttributeError: 'GribberishBackendArray' object has no attribute 'oindex'
@mpiannucci
Copy link
Owner

I think an index is missing from the gribberish python code. Can you send me a link to the grib file you're working with?

@kingkong-cmd
Copy link
Author

I think an index is missing from the gribberish python code. Can you send me a link to the grib file you're working with?

Sure! See below:

https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_1p00.pl?&dir=/gfs.20240901/12/atmos&file=gfs.t12z.pgrb2.1p00.f000&var_UGRD=on&var_VGRD=on&lev_10_m_above_ground=on

I also tried the reindex(). It suddenly worked, but then it suddenly didn't again, with the same error as sortby().

@mpiannucci
Copy link
Owner

mpiannucci commented Sep 5, 2024

Should be fixed with main branch or with version 0.20.2 which is building for release now :) .. Lemme know if you still run into issues!

@kingkong-cmd
Copy link
Author

Should be fixed with main branch or with version 0.20.2 which is building for release now :) .. Lemme know if you still run into issues!

Wow, works perfectly! Thank you! And thanks for making this lib.

<xarray.Dataset> Size: 17MB
Dimensions:    (time: 1, latitude: 721, longitude: 1440)
Coordinates:
  * time       (time) datetime64[ns] 8B 2024-09-03T16:00:00
  * latitude   (latitude) float64 6kB 90.0 89.75 89.5 ... -89.5 -89.75 -90.0
  * longitude  (longitude) float64 12kB -179.8 -179.5 -179.2 ... 179.8 180.0
Data variables:
    vgrd       (time, latitude, longitude) float64 8MB -5.648 -5.638 ... 3.722
    ugrd       (time, latitude, longitude) float64 8MB -2.235 -2.265 ... 4.795
Attributes:
    meta:     Generated with gribberishpy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants