1
1
import logging
2
2
3
- from platformdirs import user_cache_path
4
-
5
3
from message_ix_models .tools .exo_data import ExoDataSource , register_source
6
4
from message_ix_models .tools .iamc import iamc_like_data_for_query
7
- from message_ix_models .util import package_data_path , private_data_path
5
+ from message_ix_models .util import path_fallback
8
6
9
7
__all__ = [
10
8
"SSPOriginal" ,
13
11
14
12
log = logging .getLogger (__name__ )
15
13
14
+ #: :py:`where` argument to :func:`path_fallback`, used by both :class:`.SSPOriginal` and
15
+ #: :class:`.SSPUpdate`. In order:
16
+ #:
17
+ #: 1. Currently data is stored in message-static-data, cloned and linked from within the
18
+ #: user's 'local' data directory.
19
+ #: 2. Previously some files were stored directly within message_ix_models (available in
20
+ #: an editable install from a clone of the git repository, 'package') or in
21
+ #: :mod:`message_data` ('private'). These settings are only provided for backward
22
+ #: compatibility.
23
+ #: 3. If the above are not available, use the fuzzed/random test data ('test').
24
+ WHERE = "local package private test"
25
+
16
26
17
27
@register_source
18
28
class SSPOriginal (ExoDataSource ):
@@ -93,25 +103,17 @@ def __init__(self, source, source_kw):
93
103
94
104
self .raise_on_extra_kw (source_kw )
95
105
106
+ # Identify input data path
107
+ self .path = path_fallback ("ssp" , self .filename , where = WHERE )
108
+ if "test" in self .path .parts :
109
+ log .warning (f"Read random data from { self .path } " )
110
+
96
111
# Assemble a query string
97
112
extra = "d" if ssp_id == "4" and model == "IIASA-WiC POP" else ""
98
113
self .query = (
99
114
f"SCENARIO == 'SSP{ ssp_id } { extra } _v9_{ date } ' and VARIABLE == '{ measure } '"
100
115
+ (f" and MODEL == '{ model } '" if model else "" )
101
116
)
102
- # log.debug(query)
103
-
104
- # Iterate over possible locations for the data file
105
- dirs = [private_data_path ("ssp" ), package_data_path ("test" , "ssp" )]
106
- for path in [d .joinpath (self .filename ) for d in dirs ]:
107
- if not path .exists ():
108
- log .info (f"Not found: { path } " )
109
- continue
110
- if "test" in path .parts :
111
- log .warning (f"Reading random data from { path } " )
112
- break
113
-
114
- self .path = path
115
117
116
118
def __call__ (self ):
117
119
# Use prepared path, query, and replacements
@@ -185,12 +187,6 @@ def __init__(self, source, source_kw):
185
187
scenarios = []
186
188
187
189
if release in ("3.1" , "3.0.1" , "3.0" ):
188
- # Directories in which to locate `self.filename`:
189
- # - User's local cache (retrieved with "mix-models fetch" or equivalent).
190
- # - Stored directly within message_ix_models (editable install from a clone
191
- # of the git repository).
192
- dirs = [user_cache_path ("message-ix-models" ), package_data_path ("ssp" )]
193
-
194
190
scenarios .append (f"SSP{ ssp_id } " )
195
191
196
192
if measure == "GDP|PPP" :
@@ -203,9 +199,6 @@ def __init__(self, source, source_kw):
203
199
Scenario = {"Historical Reference" : scenarios [0 ]},
204
200
)
205
201
elif release == "preview" :
206
- # Look first in message_data, then in message_ix_models test data
207
- dirs = [private_data_path ("ssp" ), package_data_path ("test" , "ssp" )]
208
-
209
202
models .extend ([model ] if model is not None else [])
210
203
scenarios .append (f"SSP{ ssp_id } - Review Phase 1" )
211
204
else :
@@ -215,22 +208,15 @@ def __init__(self, source, source_kw):
215
208
)
216
209
raise ValueError (release )
217
210
211
+ # Identify input data path
212
+ self .path = path_fallback ("ssp" , self .filename [release ], where = WHERE )
213
+ if "test" in self .path .parts :
214
+ log .warning (f"Read random data from { self .path } " )
215
+
218
216
# Assemble and store a query string
219
217
self .query = f"Scenario in { scenarios !r} and Variable == '{ measure } '" + (
220
218
f"and Model in { models !r} " if models else ""
221
219
)
222
- # log.info(f"{self.query = }")
223
-
224
- # Iterate over possible locations for the data file
225
- for path in [d .joinpath (self .filename [release ]) for d in dirs ]:
226
- if not path .exists ():
227
- log .info (f"Not found: { path } " )
228
- continue
229
- if "test" in path .parts :
230
- log .warning (f"Reading random data from { path } " )
231
- break
232
-
233
- self .path = path
234
220
235
221
def __call__ (self ):
236
222
# Use prepared path, query, and replacements
0 commit comments