导入自定义着色表文件时出现问题

Problem importing a custom colortable file

所以,我正在尝试通过 metpy 函数 metpy.plots.ctables.read_colortable 导入自定义着色表 .tbl 文件,以使用 Py-ART 绘制一些雷达场。

这些是我正在使用的代码:

from metpy.plots import ctables

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

问题是,当我尝试这样做时,我收到以下错误消息:

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.

我尝试使用十六进制 HTML 和 RGB 算术格式得到相同的结果。

有什么想法吗?

add_colortable的第一个参数不是文件名,而是类文件对象,所以需要自己调用open():

from metpy.plots import ctables

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