Skip to content

Commit f886d2d

Browse files
committed
Simplify path handling in SSP{Original,Update}
Use path_fallback() in both methods with common arguments.
1 parent c343086 commit f886d2d

File tree

1 file changed

+23
-37
lines changed
  • message_ix_models/project/ssp

1 file changed

+23
-37
lines changed

message_ix_models/project/ssp/data.py

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import logging
22

3-
from platformdirs import user_cache_path
4-
53
from message_ix_models.tools.exo_data import ExoDataSource, register_source
64
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
86

97
__all__ = [
108
"SSPOriginal",
@@ -13,6 +11,18 @@
1311

1412
log = logging.getLogger(__name__)
1513

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+
1626

1727
@register_source
1828
class SSPOriginal(ExoDataSource):
@@ -93,25 +103,17 @@ def __init__(self, source, source_kw):
93103

94104
self.raise_on_extra_kw(source_kw)
95105

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+
96111
# Assemble a query string
97112
extra = "d" if ssp_id == "4" and model == "IIASA-WiC POP" else ""
98113
self.query = (
99114
f"SCENARIO == 'SSP{ssp_id}{extra}_v9_{date}' and VARIABLE == '{measure}'"
100115
+ (f" and MODEL == '{model}'" if model else "")
101116
)
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
115117

116118
def __call__(self):
117119
# Use prepared path, query, and replacements
@@ -185,12 +187,6 @@ def __init__(self, source, source_kw):
185187
scenarios = []
186188

187189
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-
194190
scenarios.append(f"SSP{ssp_id}")
195191

196192
if measure == "GDP|PPP":
@@ -203,9 +199,6 @@ def __init__(self, source, source_kw):
203199
Scenario={"Historical Reference": scenarios[0]},
204200
)
205201
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-
209202
models.extend([model] if model is not None else [])
210203
scenarios.append(f"SSP{ssp_id} - Review Phase 1")
211204
else:
@@ -215,22 +208,15 @@ def __init__(self, source, source_kw):
215208
)
216209
raise ValueError(release)
217210

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+
218216
# Assemble and store a query string
219217
self.query = f"Scenario in {scenarios!r} and Variable == '{measure}'" + (
220218
f"and Model in {models!r}" if models else ""
221219
)
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
234220

235221
def __call__(self):
236222
# Use prepared path, query, and replacements

0 commit comments

Comments
 (0)