File size: 7,953 Bytes
cbdae8d
 
 
 
 
db198b2
cbdae8d
db198b2
 
cbdae8d
db198b2
 
cbdae8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db198b2
 
cbdae8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db198b2
cbdae8d
db198b2
 
cbdae8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db198b2
 
cbdae8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db198b2
 
cbdae8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db198b2
cbdae8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db198b2
cbdae8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db198b2
cbdae8d
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import fnmatch
import json
import re
import unicodedata
import zipfile
from pathlib import Path

import pandas as pd

from data.schema import Col


ROOT_DIR = Path(__file__).resolve().parent.parent
DEFAULT_CONFIG = ROOT_DIR / "config.json"


# ============================================================
# CONFIG
# ============================================================

def load_config(
    path: str | Path | None = None,
) -> dict:

    with open(path or DEFAULT_CONFIG, encoding="utf-8") as f:
        return json.load(f)


def resolve_source_path(
    config: dict,
    path: str | Path | None = None,
) -> Path:
    """
    Explicit path if given, otherwise the configured one
    (relative paths resolve against the project root).
    """
    path = Path(path or config["source"]["path"])

    if not path.is_absolute():
        path = ROOT_DIR / path

    if not path.exists():
        raise FileNotFoundError(
            f"Source file not found: {path}"
        )

    return path


# ============================================================
# ZIP ACCESS
# ============================================================

def slugify(text: str) -> str:
    """
    'Parque Eólico de Coll del Moro' -> 'parque_eolico_de_coll_del_moro'
    """
    ascii_text = (
        unicodedata.normalize("NFKD", str(text))
        .encode("ascii", "ignore")
        .decode("ascii")
    )

    return re.sub(
        r"[^a-z0-9]+", "_", ascii_text.lower()
    ).strip("_")


def read_parquets(
    zip_path: str | Path,
    pattern: str,
) -> dict[str, pd.DataFrame]:
    """
    Read every parquet in the zip whose file name matches
    `pattern` (shell-style). Returns {file name: DataFrame}.
    """
    frames = {}

    with zipfile.ZipFile(zip_path) as z:
        for member in z.namelist():
            name = Path(member).name

            if fnmatch.fnmatch(name, pattern):
                with z.open(member) as f:
                    frames[name] = pd.read_parquet(f)

    if not frames:
        raise ValueError(
            f"No file matching '{pattern}' in {Path(zip_path).name}."
        )

    return frames


def _require_columns(df, columns, source):

    missing = set(columns.values()) - set(df.columns)

    if missing:
        raise ValueError(
            f"{source} is missing columns {sorted(missing)}. "
            "Check the column mapping in config.json."
        )


def _to_local_date(series, timezone):
    """
    Timestamps -> tz-naive calendar dates in the market timezone.
    """
    series = pd.to_datetime(series)

    if series.dt.tz is None:
        series = series.dt.tz_localize("UTC")

    return (
        series.dt.tz_convert(timezone)
        .dt.tz_localize(None)
        .dt.normalize()
    )


# ============================================================
# PRICES
# ============================================================

def load_prices(
    zip_path: str | Path,
    config: dict,
) -> tuple[pd.DataFrame, pd.DataFrame]:
    """
    Load forward-curve prices from the zip.

    Returns
    -------
    prices : pd.DataFrame
        Index = pricing date, columns = factor_id.

    delivery : pd.DataFrame
        Index = factor_id. Delivery period of every factor
        (tz-aware), taken from its most recent quote.
    """
    source = config["source"]
    cfg = source["prices"]
    cols = cfg["columns"]

    frames = read_parquets(zip_path, cfg["file_pattern"])
    raw = pd.concat(frames.values(), ignore_index=True)

    _require_columns(raw, cols, "Price file")

    raw = raw.copy()
    raw[Col.DATE] = _to_local_date(
        raw[cols["date"]], source["timezone"]
    )
    raw[Col.FACTOR] = raw[cols["tenor"]].map(
        lambda tenor: cfg["factor_id_template"].format(tenor=tenor)
    )

    if raw.duplicated([Col.DATE, Col.FACTOR]).any():
        raise ValueError(
            "Price file contains duplicated (date, tenor) quotes."
        )

    prices = (
        raw.pivot(
            index=Col.DATE,
            columns=Col.FACTOR,
            values=cols["price"],
        )
        .sort_index()
        .rename_axis(columns=None)
    )

    latest = (
        raw.sort_values(Col.DATE)
        .groupby(Col.FACTOR)
        .last()
    )

    delivery = pd.DataFrame(
        {
            Col.DELIVERY_START: pd.to_datetime(
                latest[cols["delivery_start"]], utc=True
            ),
            Col.DELIVERY_END: pd.to_datetime(
                latest[cols["delivery_end"]], utc=True
            ),
        }
    ).loc[prices.columns]

    return prices, delivery


# ============================================================
# GENERATION
# ============================================================

def load_generation(
    zip_path: str | Path,
    config: dict,
) -> dict[str, pd.DataFrame]:
    """
    Load metered generation per plant.

    Returns {plant slug: DataFrame} with columns
    delivery_start, delivery_end (tz-aware UTC) and the
    average power in MW over each interval.
    """
    cfg = config["source"]["generation"]
    cols = cfg["columns"]
    unit_regex = re.compile(cfg["unit_from_filename_regex"])

    generation = {}

    for name, raw in read_parquets(
        zip_path, cfg["file_pattern"]
    ).items():

        _require_columns(raw, cols, name)

        match = unit_regex.search(name)

        if match is None:
            raise ValueError(
                f"Cannot extract the plant name from '{name}'. "
                "Check 'unit_from_filename_regex' in config.json."
            )

        slug = slugify(match.group("unit"))

        if slug in generation:
            raise ValueError(
                f"More than one generation file for plant '{slug}'."
            )

        if cfg.get("status_filter"):
            raw = raw[raw[cols["status"]].isin(cfg["status_filter"])]

        df = pd.DataFrame(
            {
                Col.DELIVERY_START: pd.to_datetime(
                    raw[cols["start"]], utc=True
                ),
                Col.DELIVERY_END: pd.to_datetime(
                    raw[cols["end"]], utc=True
                ),
                Col.POWER_MW: (
                    raw[cols["quantity"]].astype(float)
                    * cfg["quantity_to_mw"]
                ),
            }
        ).sort_values(Col.DELIVERY_START)

        if df[Col.DELIVERY_START].duplicated().any():
            raise ValueError(
                f"{name} contains duplicated delivery intervals."
            )

        if (df[Col.POWER_MW] < 0).any():
            raise ValueError(
                f"{name} contains negative generation."
            )

        generation[slug] = df.reset_index(drop=True)

    return generation


def load_units(
    zip_path: str | Path,
    config: dict,
) -> pd.DataFrame:
    """
    Load plant master data. Index = plant slug.
    """
    cfg = config["source"]["units"]
    cols = cfg["columns"]

    raw = pd.concat(
        read_parquets(zip_path, cfg["file_pattern"]).values(),
        ignore_index=True,
    )

    _require_columns(raw, cols, "Generation units file")

    unknown_units = (
        set(raw[cols["capacity_unit"]].dropna())
        - set(cfg["capacity_to_mw"])
    )

    if unknown_units:
        raise ValueError(
            f"Unknown capacity units {sorted(unknown_units)}. "
            "Add them to 'capacity_to_mw' in config.json."
        )

    units = pd.DataFrame(
        {
            Col.PLANT: raw[cols["name"]],
            Col.TECHNOLOGY: raw[cols["technology"]],
            Col.COUNTRY: raw[cols["country"]],
            Col.CAPACITY_MW: (
                raw[cols["capacity"]].astype(float)
                * raw[cols["capacity_unit"]].map(cfg["capacity_to_mw"])
            ),
        }
    )

    units.index = units[Col.PLANT].map(slugify)

    if units.index.duplicated().any():
        raise ValueError(
            "Generation units file contains duplicated plants."
        )

    return units