forked from NCAR/wrf-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
239 lines (160 loc) · 4.87 KB
/
config.py
File metadata and controls
239 lines (160 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from __future__ import (absolute_import, division, print_function)
from threading import local
import wrapt
from ._wrffortran import (fomp_enabled, fomp_set_num_threads,
fomp_set_schedule, fomp_set_dynamic,
omp_constants)
_local_config = local()
def _try_enable_xarray():
global _local_config
_local_config.xarray_enabled = True
try:
from xarray import DataArray
except ImportError:
_local_config.xarray_enabled = False
def _try_enable_cartopy():
global _local_config
_local_config.cartopy_enabled = True
try:
from cartopy import crs
except ImportError:
_local_config.cartopy_enabled = False
def _try_enable_basemap():
global _local_config
_local_config.basemap_enabled = True
try:
from mpl_toolkits.basemap import Basemap
except ImportError:
_local_config.basemap_enabled = False
def _try_enable_pyngl():
global _local_config
_local_config.pyngl_enabled = True
try:
from Ngl import Resources
except ImportError:
_local_config.pyngl_enabled = False
def _init_local():
global _local_config
_try_enable_xarray()
_try_enable_cartopy()
_try_enable_basemap()
_try_enable_pyngl()
_local_config.cache_size = 20
_local_config.initialized = True
# Initialize the main thread's configuration
_init_local()
def init_local():
"""A decorator that initializes thread local data if necessary."""
@wrapt.decorator
def func_wrapper(wrapped, instance, args, kwargs):
global _local_config
try:
initialized = _local_config.initialized
except AttributeError:
_init_local()
else:
if not initialized:
_init_local()
return wrapped(*args, **kwargs)
return func_wrapper
@init_local()
def xarray_enabled():
"""Return True if xarray is installed and enabled.
Returns:
:obj:`bool`: True if xarray is installed and enabled.
"""
global _local_config
return _local_config.xarray_enabled
@init_local()
def enable_xarray():
"""Enable xarray if it is installed."""
_try_enable_xarray()
@init_local()
def disable_xarray():
"""Disable xarray."""
global _local_config
_local_config.xarray_enabled = False
@init_local()
def cartopy_enabled():
"""Return True if cartopy is installed and enabled.
Returns:
:obj:`bool`: True if cartopy is installed and enabled.
"""
global _local_config
return _local_config.cartopy_enabled
@init_local()
def enable_cartopy():
"""Enable cartopy if it is installed."""
_try_enable_cartopy()
@init_local()
def disable_cartopy():
"""Disable cartopy."""
global _local_config
_local_config.cartopy_enabled = False
@init_local()
def basemap_enabled():
"""Return True if basemap is installed and enabled.
Returns:
:obj:`bool`: True if basemap is installed and enabled.
"""
global _local_config
return _local_config.basemap_enabled
@init_local()
def enable_basemap():
"""Enable basemap if it is installed."""
_try_enable_basemap()
@init_local()
def disable_basemap():
"""Disable basemap."""
global _local_config
_local_config.basemap_enabled = False
@init_local()
def pyngl_enabled():
"""Return True if pyngl is installed and enabled.
Returns:
:obj:`bool`: True if pyngl is installed and enabled.
"""
global _local_config
return _local_config.pyngl_enabled
@init_local()
def enable_pyngl():
"""Enable pyngl if it is installed."""
_try_enable_pyngl()
@init_local()
def disable_pyngl():
"""Disable pyngl."""
global _local_config
_local_config.pyngl_enabled = False
@init_local()
def set_cache_size(size):
"""Set the maximum number of items that the threadlocal cache can retain.
This cache is primarily used for coordinate variables.
Args:
size (:obj:`int`): The number of items to retain in the cache.
Returns:
None
"""
global _local_config
_local_config.cache_size = size
@init_local()
def get_cache_size():
"""Return the maximum number of items that the threadlocal cache can
retain.
Returns:
:obj:`int`: The maximum number of items the cache can retain.
"""
global _local_config
return int(_local_config.cache_size)
def omp_enabled():
"""Return True if OpenMP is enabled.
OpenMP is only enabled if compiled with OpenMP features.
Returns:
:obj:`bool`: True if OpenMP is enabled, otherwise False.
"""
return True if fomp_enabled() else False
# Set OpenMP to use 1 thread, static scheduler, and no dynamic
# Note: Using the raw extension functions here to prevent possible
# circular import problems in the future.
fomp_set_num_threads(1)
fomp_set_schedule(omp_constants.fomp_sched_static, 0)
fomp_set_dynamic(False)