ecmwf / magics-python

Python interface to Magics meteorological plotting package
Apache License 2.0
49 stars 12 forks source link

cannot create magics.mxarray object #35

Closed jlequeux closed 3 years ago

jlequeux commented 3 years ago

Hi,

I'm trying to plot images based on a xarray object. But the magics.mxarray function returns an error.

Here is the code:

import xarray as xr
import Magics.macro as magics
# open dataset from https://github.com/ecmwf/notebook-examples/tree/master/data/total_precipitation.grib
df = xr.open_dataset('total_precipitation.grib', engine='cfgrib')

magics.mxarray(df, 'tp')

Complete traceback:

Exception ignored on calling ctypes callback function: <function error at 0x7f44ac0c90d0>
Traceback (most recent call last):
  File "/opt/conda/lib/python3.8/site-packages/Magics/macro.py", line 59, in error
    print(msg.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb8 in position 1394: invalid start byte
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-9517270a2356> in <module>
----> 1 magics.mxarray(df, 'tp')

/opt/conda/lib/python3.8/site-packages/Magics/macro.py in mxarray(xarray_dataset, xarray_variable_name, xarray_dimension_settings)
    565     # lat/lon data in xarray_dataset.data_vars instead.
    566     for ds_attributes in [xarray_dataset.coords, xarray_dataset.data_vars]:
--> 567         ret = _mxarray(
    568             xarray_dataset,
    569             xarray_variable_name,

/opt/conda/lib/python3.8/site-packages/Magics/macro.py in _mxarray(xarray_dataset, xarray_variable_name, ds_attributes, xarray_dimension_settings)
    580     xarray_dataset, xarray_variable_name, ds_attributes, xarray_dimension_settings
    581 ):
--> 582     lat_name, lon_name = detect_lat_lon(xarray_dataset, ds_attributes)
    583 
    584     if lat_name and lon_name:

/opt/conda/lib/python3.8/site-packages/Magics/macro.py in detect_lat_lon(xarray_dataset, ds_attributes)
    552         for ds_attribute in ds_attributes
    553     }
--> 554     lat_name = detect(attrs, "latitude")
    555     lon_name = detect(attrs, "longitude")
    556     return lat_name, lon_name

/opt/conda/lib/python3.8/site-packages/Magics/macro.py in detect(attributes, dimension)
    544 
    545 def detect(attributes, dimension):
--> 546     return Magics.detect(json.dumps(attributes, default=encode_numpy), dimension)
    547 
    548 

/opt/conda/lib/python3.8/site-packages/Magics/Magics.py in wrapped(*args)
    138         r = fn(*new_args)
    139         if fn.restype is c_char_p:
--> 140             r = char_to_string(r)
    141         return r
    142 

/opt/conda/lib/python3.8/site-packages/Magics/Magics.py in _char_to_string(x)
    112 
    113 def _char_to_string(x):
--> 114     return x.decode()
    115 
    116 

AttributeError: 'NoneType' object has no attribute 'decode'

Platform and versions: Ubuntu 20.04.1 LTS python 3.8 Magics==1.5.5 xarray==0.15.1 numpy==1.19.5 ecmwflibs==0.0.94

jlequeux commented 3 years ago

The issue seems to come from the Magics.detect function. I tried several input types (str, bytes, dict) but all of them returns errors. I'm unsure of the expected behaviour of that function, it seems to map the .detect method of the lib object In my case lib is /opt/conda/lib/python3.8/site-packages/ecmwflibs.libs/libMagPlus-409fefe9.so

b8raoult commented 3 years ago

Can you update ecmwflibs?

jlequeux commented 3 years ago

The last version seems to works now. Thanks.