GenericMappingTools / pygmt

A Python interface for the Generic Mapping Tools.
https://www.pygmt.org
BSD 3-Clause "New" or "Revised" License
758 stars 220 forks source link

pygmt.conf() can't find default for FONT #409

Closed liamtoney closed 4 years ago

liamtoney commented 4 years ago

Description of the problem

The code

with pygmt.clib.Session() as session:
    session.call_module('gmtset', 'FONT 8p')

runs fine. But the equivalent pygmt.config() has trouble.

Full code that generated the error

pygmt.config(FONT='8p')

Full error message

pygmt-session [ERROR]: Syntax error: Unrecognized keyword FONT
Traceback (most recent call last):
  File "/opt/miniconda3/envs/liam/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-19-8e431d18430a>", line 1, in <module>
    pygmt.config(FONT='8p')
  File "/opt/miniconda3/envs/liam/lib/python3.7/site-packages/pygmt/modules.py", line 168, in __init__
    self.old_defaults[key] = lib.get_default(key)
  File "/opt/miniconda3/envs/liam/lib/python3.7/site-packages/pygmt/clib/session.py", line 458, in get_default
    name, status
pygmt.exceptions.GMTCLibError: Error getting default value for 'FONT' (error code 67).

System information

output of conda list
# packages in environment at /opt/miniconda3/envs/liam:
#
# Name                    Version                   Build  Channel
affine                    2.3.0                      py_0    conda-forge
alabaster                 0.7.12                     py_0    conda-forge
appdirs                   1.4.3            py37h28b3542_0  
appnope                   0.1.0                 py37_1000    conda-forge
array-processing          0.0.0                     dev_0    
attrs                     19.3.0                     py_0    conda-forge
babel                     2.8.0                      py_0    conda-forge
backcall                  0.1.0                      py_0    conda-forge
black                     19.10b0                    py_0  
boost-cpp                 1.70.0               h75728bb_2    conda-forge
bzip2                     1.0.8                h01d97ff_1    conda-forge
ca-certificates           2020.1.1                      0  
cairo                     1.16.0            he1c11cd_1002    conda-forge
cartopy                   0.17.0          py37h3c2a19d_1008    conda-forge
certifi                   2019.11.28       py37hc8dfbb8_1    conda-forge
cffi                      1.13.2           py37h33e799b_0    conda-forge
cfitsio                   3.470                h84d2f63_2    conda-forge
cftime                    1.0.4.2                  pypi_0    pypi
chardet                   3.0.4                 py37_1003    conda-forge
click                     7.0                        py_0    conda-forge
click-plugins             1.1.1                      py_0    conda-forge
cligj                     0.5.0                      py_0    conda-forge
colorcet                  2.0.1                      py_0    conda-forge
commonmark                0.9.1                      py_0    conda-forge
cryptography              2.8              py37hafa8578_0    conda-forge
curl                      7.65.3               h22ea746_0    conda-forge
cycler                    0.10.0                     py_2    conda-forge
decorator                 4.4.1                      py_0    conda-forge
docutils                  0.16                     py37_0    conda-forge
expat                     2.2.5             h4a8c4bd_1004    conda-forge
fastkml                   0.11                     pypi_0    pypi
fontconfig                2.13.1            h6b1039f_1001    conda-forge
freetype                  2.10.0               h24853df_1    conda-forge
freexl                    1.0.5             h1de35cc_1002    conda-forge
future                    0.18.2                   py37_0    conda-forge
gdal                      3.0.2            py37h97c3584_3    conda-forge
geos                      3.7.2                h6de7cb9_2    conda-forge
geotiff                   1.5.1                ha113b03_7    conda-forge
gettext                   0.19.8.1          h46ab8bc_1002    conda-forge
ghostscript               9.22              h0a44026_1001    conda-forge
giflib                    5.1.7                h01d97ff_1    conda-forge
glib                      2.58.3          py37h577aef8_1002    conda-forge
hdf4                      4.2.13            h84186c3_1003    conda-forge
hdf5                      1.10.5          nompi_h3e39495_1104    conda-forge
icu                       64.2                 h6de7cb9_1    conda-forge
idna                      2.8                   py37_1000    conda-forge
imagesize                 1.2.0                      py_0    conda-forge
ipython                   7.9.0            py37h5ca1d4c_0    conda-forge
ipython_genutils          0.2.0                      py_1    conda-forge
jedi                      0.15.1                   py37_0    conda-forge
jinja2                    2.11.1                     py_0    conda-forge
joblib                    0.14.0                   pypi_0    pypi
jpeg                      9c                h1de35cc_1001    conda-forge
json-c                    0.13.1            h1de35cc_1001    conda-forge
kealib                    1.4.10            h6659575_1005    conda-forge
kiwisolver                1.1.0            py37ha1b3eb9_0    conda-forge
krb5                      1.16.3            hcfa6398_1001    conda-forge
libblas                   3.8.0               14_openblas    conda-forge
libcblas                  3.8.0               14_openblas    conda-forge
libcurl                   7.65.3               h16faf7d_0    conda-forge
libcxx                    9.0.1                         1    conda-forge
libdap4                   3.20.4               habf5908_0    conda-forge
libedit                   3.1.20170329      hcfe32e1_1001    conda-forge
libffi                    3.2.1             h6de7cb9_1006    conda-forge
libgdal                   3.0.2                h6a635d1_3    conda-forge
libgfortran               4.0.0                         2    conda-forge
libiconv                  1.15              h01d97ff_1005    conda-forge
libkml                    1.3.0             hed7d534_1010    conda-forge
liblapack                 3.8.0               14_openblas    conda-forge
libnetcdf                 4.7.1           nompi_hec86efb_102    conda-forge
libopenblas               0.3.7                h4bb4525_3    conda-forge
libpng                    1.6.37               h2573ce8_0    conda-forge
libpq                     11.5                 h31a01ba_2    conda-forge
libspatialite             4.3.0a            h7a659be_1032    conda-forge
libssh2                   1.8.2                hcdc9a53_2    conda-forge
libtiff                   4.1.0                ha78913b_1    conda-forge
libwebp                   1.0.2                h20df551_3    conda-forge
libxml2                   2.9.10               h53d96d6_0    conda-forge
libxslt                   1.1.33               h320ff13_0    conda-forge
llvm-openmp               9.0.0                h40edb58_0    conda-forge
lxml                      4.4.1            py37he54a443_0    conda-forge
lz4-c                     1.8.3             h6de7cb9_1001    conda-forge
markupsafe                1.1.1            py37h0b31af3_0    conda-forge
matplotlib                3.2.1                         0    conda-forge
matplotlib-base           3.2.1            py37hddda452_0    conda-forge
mypy_extensions           0.4.3                    py37_0  
ncurses                   6.1               h0a44026_1002    conda-forge
netcdf4                   1.5.3                    pypi_0    pypi
numpy                     1.17.3           py37hde6bac1_0    conda-forge
obspy                     1.2.1            py37h10e2902_1    conda-forge
olefile                   0.46                       py_0    conda-forge
openjpeg                  2.3.1                hdc36067_1    conda-forge
openssl                   1.1.1e               h0b31af3_0    conda-forge
owslib                    0.19.0                     py_0    conda-forge
packaging                 19.2                       py_0    conda-forge
pandas                    0.25.3           py37h4f17bb1_0    conda-forge
param                     1.9.2                      py_0    conda-forge
parso                     0.5.1                      py_0    conda-forge
pathspec                  0.7.0                      py_0  
patsy                     0.5.1                    pypi_0    pypi
pbr                       5.4.4                      py_0  
pcre                      8.43                 h4a8c4bd_0    conda-forge
pexpect                   4.7.0                    py37_0    conda-forge
pickleshare               0.7.5                 py37_1000    conda-forge
pillow                    6.2.1            py37h43b43f3_1    conda-forge
pip                       19.3.1                   py37_0    conda-forge
pixman                    0.38.0            h01d97ff_1003    conda-forge
poppler                   0.67.0               h16886b5_8    conda-forge
poppler-data              0.4.9                         1    conda-forge
postgresql                11.5                 h26bc10f_2    conda-forge
proj                      6.2.1                h773a61f_0    conda-forge
prompt_toolkit            2.0.10                     py_0    conda-forge
ptyprocess                0.6.0                   py_1001    conda-forge
pycparser                 2.19                     py37_1    conda-forge
pyct                      0.4.6                      py_0    conda-forge
pyct-core                 0.4.6                      py_0    conda-forge
pyepsg                    0.4.0                      py_0    conda-forge
pygeoif                   0.7                      pypi_0    pypi
pygments                  2.4.2                      py_0    conda-forge
pygmt                     0+unknown                pypi_0    pypi
pykdtree                  1.3.1           py37h3b54f70_1002    conda-forge
pyopenssl                 19.0.0                   py37_0    conda-forge
pyparsing                 2.4.5                      py_0    conda-forge
pyproj                    2.4.1            py37h03a428a_1    conda-forge
pyqt5                     5.13.2                   pypi_0    pypi
pyqt5-sip                 12.7.0                   pypi_0    pypi
pyshp                     2.1.0                      py_0    conda-forge
pysocks                   1.7.1                    py37_0    conda-forge
python                    3.7.3                h93065d6_1    conda-forge
python-dateutil           2.8.1                      py_0    conda-forge
python_abi                3.7                     1_cp37m    conda-forge
pytz                      2019.3                     py_0    conda-forge
pyyaml                    5.1.2            py37h0b31af3_0    conda-forge
rasterio                  1.1.1            py37he71f6a4_0    conda-forge
readline                  8.0                  hcfe32e1_0    conda-forge
recommonmark              0.6.0                      py_0    conda-forge
regex                     2020.1.8         py37h1de35cc_0  
requests                  2.22.0                   py37_1    conda-forge
rioxarray                 0.0.17                     py_0    conda-forge
rtm                       0.0.0                     dev_0    
scikit-learn              0.21.3                   pypi_0    pypi
scipy                     1.3.2            py37h82752d6_0    conda-forge
seisk                     0.1.dev0                  dev_0    
setuptools                42.0.0                   py37_0    conda-forge
shapely                   1.6.4           py37h5c88e11_1006    conda-forge
simplekml                 1.3.3                    pypi_0    pypi
six                       1.13.0                   py37_0    conda-forge
snowballstemmer           2.0.0                      py_0    conda-forge
snuggs                    1.4.7                      py_0    conda-forge
sphinx                    2.4.3                      py_0    conda-forge
sphinx_rtd_theme          0.4.3                      py_0    conda-forge
sphinxcontrib-apidoc      0.3.0                      py_1    conda-forge
sphinxcontrib-applehelp   1.0.1                      py_0    conda-forge
sphinxcontrib-devhelp     1.0.1                      py_0    conda-forge
sphinxcontrib-htmlhelp    1.0.3                      py_0    conda-forge
sphinxcontrib-jsmath      1.0.1                      py_0    conda-forge
sphinxcontrib-qthelp      1.0.2                      py_0    conda-forge
sphinxcontrib-serializinghtml 1.1.3                      py_0    conda-forge
sqlalchemy                1.3.11           py37h0b31af3_0    conda-forge
sqlite                    3.30.1               h93121df_0    conda-forge
statsmodels               0.10.2                   pypi_0    pypi
tbb                       2018.0.5             h2d50403_0    conda-forge
tiledb                    1.7.0                hd5e958f_2    conda-forge
tk                        8.6.9             ha441bb4_1000    conda-forge
toml                      0.10.0           py37h28b3542_0  
tornado                   6.0.3            py37h0b31af3_0    conda-forge
traitlets                 4.3.3                    py37_0    conda-forge
typed-ast                 1.4.1            py37h1de35cc_0  
typing_extensions         3.7.4.1                  py37_0  
tzcode                    2019a             h01d97ff_1002    conda-forge
urllib3                   1.25.7                   py37_0    conda-forge
utm                       0.5.0                      py_0    conda-forge
waveform-collection       0.0.0                     dev_0    
wcwidth                   0.1.7                      py_1    conda-forge
wheel                     0.33.6                   py37_0    conda-forge
xarray                    0.14.1                     py_0    conda-forge
xerces-c                  3.2.2             hbda6038_1004    conda-forge
xz                        5.2.4             h1de35cc_1001    conda-forge
yaml                      0.1.7             h1de35cc_1001    conda-forge
zlib                      1.2.11            h0b31af3_1006    conda-forge
zstd                      1.4.4                he7fca8b_1    conda-forge
liamtoney commented 4 years ago

However — the command

pygmt.config(MAP_FRAME_TYPE='fancy')

works as expected. Does FONT not have a default, usually?

weiji14 commented 4 years ago

I'm not very familiar with GMT's settings, but looking at https://docs.generic-mapping-tools.org/latest/gmt.conf.html#font-parameters, it says:

FONT Sets the default for all fonts, except FONT_LOGO. This setting is not included in the gmt.conf file.

so I guess that means there's no default? I think we'll need to wrap the below code around a try...except clause to handle these cases:

https://github.com/GenericMappingTools/pygmt/blob/5a5091e1d8e6cfb8cb3b4ab25eb5de0fd7049b2f/pygmt/modules.py#L166-L168

weiji14 commented 4 years ago

Ok, so there seems to be 7 settings without default settings. Besides FONT which influences all FONT_ related settings (except FONT_LOGO), the other 6 seems to only control both the resulting _PRIMARY and _SECONDARY settings (e.g. FONT_ANNOT_PRIMARY and FONT_ANNOT_SECONDARY).

* FORMAT_TIME_PRIMARY_MAP, FORMAT_TIME_SECONDARY_MAP

I could do an 'allowlist' to map the 6 types above to their corresponding _PRIMARY and _SECONDARY settings, so that their default settings are reverted outside of the with block, but FONT itself will still be a bit tricky to handle. Any ideas?

liamtoney commented 4 years ago

Hmm, that's actually a tricky problem since we can't access FONT programmatically (what even is the default?). I guess for now we could do a try/except or if key == 'FONT' workaround to just not perform the error-raising lookup for the time-being?

I'm not familiar with context managers, but is there a way to raise an error if someone tries to set FONT using the with block? E.g. Error: Setting parameter "FONT" within a with block is not supported. That would be better in my view since it wouldn't just silently not revert the FONT value...

seisman commented 4 years ago

I could do an 'allowlist' to map the 6 types above to their corresponding _PRIMARY and _SECONDARY settings, so that their default settings are reverted outside of the with block, but FONT itself will still be a bit tricky to handle. Any ideas?

It's a good solution for me. For FONT, we probably have to maintain a list of FONT_* parameters.