Search code examples
metpy

Problem importing a custom colortable file


So, I'm trying to import a custom colortable .tbl file via the metpy function metpy.plots.ctables.read_colortable, to plot some radar fields with Py-ART.

These are the codes I'm using:

from metpy.plots import ctables

ctables.registry.add_colortable('zdr_table.tbl','nexrad_zdr')
zdr_cmap = ctables.registry.get_colortable('nexrad_zdr')

The problem is that, when I try doing this I get the following error message:

Traceback (most recent call last):
  File "/home/viper/miniconda3/envs/pyart_env/lib/python3.8/site-packages/metpy/plots/ctables.py", line 95, in read_colortable
    literal = _parse(line)
  File "/home/viper/miniconda3/envs/pyart_env/lib/python3.8/site-packages/metpy/plots/ctables.py", line 67, in _parse
    return ast.literal_eval(s)
  File "/home/viper/miniconda3/envs/pyart_env/lib/python3.8/ast.py", line 96, in literal_eval
    return _convert(node_or_string)
  File "/home/viper/miniconda3/envs/pyart_env/lib/python3.8/ast.py", line 95, in _convert
    return _convert_signed_num(node)
  File "/home/viper/miniconda3/envs/pyart_env/lib/python3.8/ast.py", line 74, in _convert_signed_num
    return _convert_num(node)
  File "/home/viper/miniconda3/envs/pyart_env/lib/python3.8/ast.py", line 66, in _convert_num
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x7fcf76e35c40>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "plot_zdr_ppi2.py", line 31, in <module>
    ctables.registry.add_colortable('zdr_table.tbl','nexrad_zdr')
  File "/home/viper/miniconda3/envs/pyart_env/lib/python3.8/site-packages/metpy/plots/ctables.py", line 187, in add_colortable
    self[name] = read_colortable(fobj)
  File "/home/viper/miniconda3/envs/pyart_env/lib/python3.8/site-packages/metpy/plots/ctables.py", line 100, in read_colortable
    raise RuntimeError('Malformed colortable.')
RuntimeError: Malformed colortable.

I've tried to use both Hex HTML and RGB arithmetic formats with the same results.

Any ideas?


Solution

  • The first argument to add_colortable is not a filename, but a file-like object, so you need to call open() yourself:

    from metpy.plots import ctables
    
    ctables.registry.add_colortable(open('zdr_table.tbl', 'rt'),'nexrad_zdr')
    zdr_cmap = ctables.registry.get_colortable('nexrad_zdr')