NGED Data API
NGED JSON Data
This package reads NGED's telemetry JSON files from S3, parses them into the PowerTimeSeries
and TimeSeriesMetadata schemas (see contracts), and writes them to Delta Lake and Parquet.
Public surface
Only upsert_metadata is re-exported from the package root (from nged_data import
upsert_metadata); the other five live in nged_data.storage (from nged_data.storage import
list_timeseries_json_files, etc.).
nged_data.storage.list_timeseries_json_files(store)— lists the timeseries JSON files on NGED's S3 bucket, parsingtime_series_id,start_timeandend_timeout of each file's path.nged_data.storage.remove_small_files_from_listing(file_listing, size_threshold_bytes=520)— drops files too small to carry any readings, sodownload_and_parse_filesnever fetches and parses one only to discard the result.nged_data.storage.download_and_parse_files(store, paths_df)— downloads and parses each listed file, returning aDownloadAndParseResultofmetadata(TimeSeriesMetadata),power_time_series(PowerTimeSeries) andn_implausible_power_rows_dropped. RaisesNoNewDataif none of the listed files yielded any metadata or power rows.nged_data.storage.select_new_rows(time_series, delta_path, storage_options=None)— filterstime_seriesdown to rows newer than what thepower_time_seriesDelta table atdelta_pathalready holds, pertime_series_id.nged_data.storage.time_series_coverage(delta_path, storage_options=None)— the earliest and latest observationtimeon disk for eachtime_series_idin thepower_time_seriesDelta table.nged_data.upsert_metadata(new_metadata, metadata_path, storage_options=None)— merges aTimeSeriesMetadatasnapshot into the stored metadata Parquet file, keeping the newest values pertime_series_idand rewriting the file only if something changed.
Data quality
download_and_parse_files drops rows whose time is malformed — outside the plausible
datetime range, null, or not aligned to the top or bottom of the hour — via
PowerTimeSeries.drop_implausible_rows, and reports how many as
n_implausible_power_rows_dropped. No other cleaning happens during ingestion.
Usage
This package is used by the power_time_series_and_metadata Dagster asset in
src/nged_substation_forecast/defs/assets.py.
nged_data.read_nged_json
Extracts metadata and time series from NGED JSON data.
The JSON is expected to have a structure where metadata fields are at the top level, and a 'data' field contains an array of time series data points.
Attributes
log = logging.getLogger(__name__)
module-attribute
Classes
ExtractedPowerTimeSeries
Bases: NamedTuple
Result of parsing PowerTimeSeries rows out of one NGED JSON file.
n_dropped counts rows dropped by PowerTimeSeries.drop_implausible_rows for a malformed
time — see that method's docstring for why ingestion degrades rather than raising.
Source code in packages/nged_data/src/nged_data/read_nged_json.py
28 29 30 31 32 33 34 35 36 | |
Attributes
dataframe
instance-attribute
n_dropped
instance-attribute
nged_data.storage
Reading NGED's telemetry JSON from S3 and writing power observations and metadata to storage.
Attributes
log = logging.getLogger(__name__)
module-attribute
Classes
NoNewData
Bases: Exception
Raised when a listing of NGED files yields no rows we have not already stored.
Source code in packages/nged_data/src/nged_data/storage.py
144 145 | |
DownloadAndParseResult
Bases: NamedTuple
Result of download_and_parse_files.
n_implausible_power_rows_dropped sums ExtractedPowerTimeSeries.n_dropped across every
file in the batch — see PowerTimeSeries.drop_implausible_rows for what gets dropped and
why.
Source code in packages/nged_data/src/nged_data/storage.py
148 149 150 151 152 153 154 155 156 157 158 | |
Attributes
metadata
instance-attribute
power_time_series
instance-attribute
n_implausible_power_rows_dropped
instance-attribute
TimeSeriesCoverage
Bases: Model
Per-series observation-time span of the power_time_series Delta table.
first_time/last_time are the earliest/latest observation time for each
time_series_id. A transient intermediate (never persisted): the freshness asset check
reads last_time to detect staleness, select_new_rows reads last_time to find
genuinely-new rows, and CV fold-eligibility (eligible_time_series_ids) reads both.
Source code in packages/nged_data/src/nged_data/storage.py
227 228 229 230 231 232 233 234 235 236 237 238 | |
Attributes
time_series_id = _get_time_series_id_dtype(unique=True)
class-attribute
instance-attribute
first_time = pt.Field(dtype=(PowerTimeSeries.dtypes['time']))
class-attribute
instance-attribute
last_time = pt.Field(dtype=(PowerTimeSeries.dtypes['time']))
class-attribute
instance-attribute
UpsertMetadataStats
Bases: TypedDict
What the TimeSeriesMetadata upsert did, published as Dagster output metadata.
Source code in packages/nged_data/src/nged_data/storage.py
363 364 365 366 367 368 369 370 | |
Attributes
metadata_n_new_TimeSeriesIDs
instance-attribute
metadata_n_updated_TimeSeriesIDs
instance-attribute
metadata_updated_TimeSeriesIDs
instance-attribute
metadata_upsert_failed
instance-attribute
Set by the asset when the whole upsert raised, so the power write went ahead without it.
Functions:
list_timeseries_json_files(store)
List all the timeseries JSON files in NGED's S3 bucket.
The paths are assumed to be of the form: timeseries/1774512000000_1774533600000/TimeSeries_23_20260326T080000Z_20260326T140000Z.json
Source code in packages/nged_data/src/nged_data/storage.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
remove_small_files_from_listing(file_listing, size_threshold_bytes=520)
Remove files too small to carry any readings.
This is used to skip NGED JSON files that have no data field, so download_and_parse_files
never has to fetch and parse them only to discard the result. It is an optimisation, not a
correctness requirement: download_and_parse_files already tolerates a null data field.
size_threshold_bytes defaults to 520, derived from the real files on NGED's S3: a WKT-less
file with zero readings tops out at 488 bytes, and one with a single reading starts at 556
bytes, so 520 sits in the gap between them. WKT-bearing (Primary substation) files run far
larger — zero-reading examples measured between 4,405 and 20,148 bytes — so the WKT-less
floor is the binding constraint on the threshold.
That 68-byte gap comes from V1's 33 series, and it is narrow enough that V2's ~2,500 series
want re-measuring before this default is trusted there. Two things would close it: a populated
information field, which TimeSeriesMetadata records as always null in the V1 trial area,
would push a zero-reading file above 520; and a substation name shorter than any in V1 would
pull a one-reading file below it. Re-run the measurement rather than assume the gap survives.
Source code in packages/nged_data/src/nged_data/storage.py
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 | |
download_and_parse_files(store, paths_df)
Load data end_time by end_time, in order.
Loading in order means more recent data overwrites older duplicates, if there are any.
Raises NoNewData if there is no new data.
Source code in packages/nged_data/src/nged_data/storage.py
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 | |
time_series_coverage(delta_path, storage_options=None)
Return the earliest/latest observation time on disk per time_series_id.
Returns an empty (but correctly typed) frame if the Delta table does not exist yet.
min/max grouped by time_series_id are value aggregations, so they are safe from
the Polars 32-bit row-count wraparound even on a very large table (see
https://openclimatefix.github.io/nged-substation-forecast/architecture/code-style/#data-handling).
Cost: a full two-column scan-and-aggregate, O(rows in the table). Projection pushdown drops
the power column, but a group-wise min/max cannot be answered from Parquet
row-group statistics (no engine on our stack does aggregate-from-statistics), so every
time/time_series_id value is read; computing both bounds instead of one is ~20%
more wall-clock and no extra memory (the shared scan dominates). The collect uses the
streaming engine to keep peak memory bounded — the freshness check runs hourly on a small
control-plane VM. Measured on a synthetic V2 table (2,500 series, half-hourly, partitioned
by time_series_id) for a year of history (43.8M rows): streaming ~0.21 s / ~190 MB peak,
versus ~1.3 GB peak for the in-memory engine — same result, ~7x less memory. Cost scales
linearly with accumulated history. If the scan ever becomes a problem, both bounds can
instead be read from the Delta add-action min.time/max.time file statistics —
metadata-only, O(files): ~0.02 s / <100 MB at the same scale — the same Delta-log-metadata
trick used to count whole-table rows without scanning.
delta_path is a local path or remote URI for the power_time_series Delta table;
storage_options carries the object-store credentials/endpoint for a remote delta_path.
Source code in packages/nged_data/src/nged_data/storage.py
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 | |
select_new_rows(time_series, delta_path, storage_options=None)
select_new_rows(
time_series: pt.DataFrame[PowerTimeSeries],
delta_path: str,
storage_options: ObjectStoreOptions | None = None,
) -> pt.DataFrame[PowerTimeSeries]
select_new_rows(
time_series: pt.DataFrame[_ProcessedFileListing],
delta_path: str,
storage_options: ObjectStoreOptions | None = None,
) -> pt.DataFrame[_ProcessedFileListing]
Return rows in time_series newer than what our Delta table already holds.
The comparison is made on a time_series_id by time_series_id basis.
delta_path is a local path or remote URI for the power_time_series Delta table;
storage_options carries the object-store credentials/endpoint for a remote delta_path.
Source code in packages/nged_data/src/nged_data/storage.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
upsert_metadata(new_metadata, metadata_path, storage_options=None)
Upserts metadata to a Parquet file, keeping the newest version of each time series.
This function assumes it is called by one thread at a time so no explicit locking is required.
If the Parquet file does not exist, it saves the new_metadata. If it exists, it merges the
new_metadata into it and rewrites the file only if something changed. The snapshot need not
carry the same columns, or the same column order, as the stored roster, and rows are matched
on time_series_id. A series that new_metadata covers is replaced wholesale, so a field
the snapshot has stopped carrying is cleared for that series. A series that
new_metadata omits keeps its last stored values indefinitely. The roster therefore holds
every time series we have ever seen, not only the ones in the latest snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_metadata
|
DataFrame[TimeSeriesMetadata]
|
The new metadata DataFrame. |
required |
metadata_path
|
str
|
Local path or remote URI of the Parquet file where we store our version of the metadata. |
required |
storage_options
|
ObjectStoreOptions | None
|
Object-store credentials/endpoint for a remote |
None
|
Returns stats about new metadata
Source code in packages/nged_data/src/nged_data/storage.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |