geoschem / gcpy

Python toolkit for GEOS-Chem. Contains basic plotting scripts, plus the suite of GEOS-Chem benchmarking utilities.
https://gcpy.readthedocs.io
Other
51 stars 24 forks source link

[BUG/ISSUE] "Cannot load backend 'TkAgg'" error when trying to regrid restart from lat-lon to cube sphere #269

Closed msulprizio closed 1 year ago

msulprizio commented 1 year ago

Describe the bug

When attempting to regrid restart files using the latest dev branch of GCPy (at commit 30a13f6) and the regridding fixes from PR #266, I'm getting the following error:

Traceback (most recent call last):
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/n/home05/msulprizio/python/gcpy/gcpy/__init__.py", line 6, in <module>
    from .examples import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/__init__.py", line 8, in <module>
    from .plotting import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/__init__.py", line 4, in <module>
    from .plot_single_panel import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/plot_single_panel.py", line 24, in <module>
    mpl_use("tkagg")
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
    plt.switch_backend(name)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/pyplot.py", line 350, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
Traceback (most recent call last):
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/n/home05/msulprizio/python/gcpy/gcpy/__init__.py", line 6, in <module>
    from .examples import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/__init__.py", line 8, in <module>
    from .plotting import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/__init__.py", line 4, in <module>
    from .plot_single_panel import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/plot_single_panel.py", line 24, in <module>
    mpl_use("tkagg")
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
    plt.switch_backend(name)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/pyplot.py", line 350, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
Traceback (most recent call last):
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/n/home05/msulprizio/python/gcpy/gcpy/__init__.py", line 6, in <module>
    from .examples import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/__init__.py", line 8, in <module>
    from .plotting import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/__init__.py", line 4, in <module>
    from .plot_single_panel import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/plot_single_panel.py", line 24, in <module>
    mpl_use("tkagg")
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
    plt.switch_backend(name)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/pyplot.py", line 350, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
Traceback (most recent call last):
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/n/home05/msulprizio/python/gcpy/gcpy/__init__.py", line 6, in <module>
    from .examples import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/__init__.py", line 8, in <module>
    from .plotting import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/__init__.py", line 4, in <module>
    from .plot_single_panel import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/plot_single_panel.py", line 24, in <module>
    mpl_use("tkagg")
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
    plt.switch_backend(name)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/pyplot.py", line 350, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

I updated to @yantosca's gcpy environment which had tk installed, but still get the same error.

To Reproduce

I call the file_regrid routine from a bash script:

$!/bin/bash

prefix="GEOSChem.Restart.fullchem.20190101_0000z"

cs_resolutions=( '24' '48' '90' '180') # '360' )

# Loop over output resolutions
for cs_res in "${cs_resolutions[@]}"; do

    python -m gcpy.file_regrid --filein ${prefix}.nc4 \
       --dim_format_in classic \
       --fileout ${prefix}.c${cs_res}.nc4 \
       --cs_res_out ${cs_res} \
       --dim_format_out checkpoint

    # Remove regridding files
    rm -rf conservative_*.nc

done
yantosca commented 1 year ago

Thanks @msulprizio. According to StackOverflow, this should be the proper import order:

from matplotlib import use as mpl_use
mpl_use("TkAgg")                          # X11 backend for plt.show()
import matplotlib.pyplot as plt

Apparently you need to apply the X11 backend immediately after importing matplotlib and before importing matplotlib.pyplot.

yantosca commented 1 year ago

A hotfix has been pushed in commit 8f7528459f3cb751438dea13e0d42064aff84a1c to the dev branch.

yantosca commented 1 year ago

We can close this issue as #275 has since been merged into version 1.4.0.