dapper.dpr_config

Load default and user configurations into the rc dict.

The rc dict can be updated (after startup) as any normal dict. See the source for the default configuration.

 1"""Load default and user configurations into the `rc` dict.
 2
 3The `rc` dict can be updated (after startup) as any normal dict. See the
 4[source](https://github.com/nansencenter/DAPPER/blob/master/dapper/dpr_config.yaml)
 5for the default configuration.
 6"""
 7
 8import os
 9from pathlib import Path
10
11import matplotlib as mpl
12import yaml
13from mpl_tools import is_using_interactive_backend
14from struct_tools import DotDict
15
16##################################
17# Load configurations
18##################################
19dapper_dir = Path(__file__).absolute().parent
20rc = DotDict()
21rc.loaded_from = []
22for d in [dapper_dir, "~", "~/.config", "."]:
23    d = Path(d).expanduser().absolute()
24    for prefix in [".", ""]:
25        f = d / (prefix+"dpr_config.yaml")
26        if f.is_file():
27            dct = yaml.load(open(f), Loader=yaml.SafeLoader)
28            rc.loaded_from.append(str(f))
29            if dct:
30                if d == dapper_dir:
31                    rc.update(dct)
32                else:
33                    for k in dct:
34                        if k in rc:
35                            rc[k] = dct[k]
36                        else:
37                            print(f"Warning: invalid key '{k}' in '{f}'")
38
39
40##################################
41# Setup dir paths
42##################################
43rc.dirs = DotDict()
44rc.dirs.dapper = dapper_dir
45rc.dirs.DAPPER = rc.dirs.dapper.parent
46# Data path
47x = rc.pop("data_root")
48if x.lower() in ["$cwd", "$pwd"]:
49    x = Path.cwd()
50elif x.lower() == "$dapper":
51    x = rc.dirs.DAPPER
52else:
53    x = Path(x)
54rc.dirs.data = x / "dpr_data"
55rc.dirs.samples = rc.dirs.data / "samples"
56
57# Expanduser, create dir
58for d in rc.dirs:
59    rc.dirs[d] = rc.dirs[d].expanduser()
60    os.makedirs(rc.dirs[d], exist_ok=True)
61
62
63##################################
64# Disable rc.liveplotting in case of non-interactive backends
65##################################
66# Otherwise, warnings are thrown on every occurence of plt.pause
67# (though not plot_pause), and (who knows) maybe errors too.
68# Also, the assimilation slows down, even though nothing is shown.
69if rc.liveplotting and not is_using_interactive_backend():
70    # Note: plot_pause could maybe be adapted to also work for
71    # "inline" backend (which is not striclty interactive), but I think
72    # this would be buggy, and is incompatible with a "stop" button.
73    print("\nWarning: You have not disableed interactive/live plotting",
74          "in your dpr_config.yaml,",
75          "but this is not supported by the current matplotlib backend:",
76          f"{mpl.get_backend()}. To enable it, try using another backend.\n")
77    rc.liveplotting = False
dapper_dir = PosixPath('/home/runner/work/DAPPER/DAPPER/dapper')
rc = { 'loaded_from': ['/home/runner/work/DAPPER/DAPPER/dapper/dpr_config.yaml'], 'field_summaries': ['m', 'rms', 'ma'], 'comps': {'error_only': False, 'max_spectral': 51}, 'sigfig': 4, 'store_u': False, 'liveplotting': False, 'place_figs': False, 'dirs': { 'dapper': PosixPath('/home/runner/work/DAPPER/DAPPER/dap per'), 'DAPPER': PosixPath('/home/runner/work/DAPPER/DAPPER'), 'data': PosixPath('/home/runner/dpr_data'), 'samples': PosixPath('/home/runner/dpr_data/samples') } }
x = PosixPath('~')