NGED Data API
NGED JSON Data
This package handles the ingestion and processing of NGED's new JSON data format.
Key Components
load_nged_json: Loads and parses NGED JSON files.clean_power_time_series: Cleans power data, including filtering out "stuck" sensors based on daily variance and insane values.append_to_delta: Appends cleaned data to a Delta table, ensuring no duplicates.upsert_metadata: Upserts metadata to a Parquet file.
Data Quality
NGED provides data every 6 hours via JSON files on S3. The data has several known quality issues that are handled during ingestion:
- Stuck values: Detected by computing the standard deviation over a 24-hour rolling window; periods where std is below a threshold are removed.
- Outliers / invalid values: Isolated zeros in non-zero time series and values beyond a threshold number of standard deviations from the mean are removed.
- Early ramp-up period: The first ~two months of each time series are typically low quality (meter calibration period) and are discarded.
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.
Classes
nged_data.storage
Attributes
log = logging.getLogger(__name__)
module-attribute
Classes
NoNewData
Bases: Exception
Source code in packages/nged_data/src/nged_data/storage.py
126 127 | |
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
194 195 196 197 198 199 200 201 202 203 204 205 | |
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
Source code in packages/nged_data/src/nged_data/storage.py
330 331 332 333 | |
Attributes
metadata_n_new_TimeSeriesIDs
instance-attribute
metadata_n_updated_TimeSeriesIDs
instance-attribute
metadata_updated_TimeSeriesIDs
instance-attribute
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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
remove_small_files_from_listing(file_listing, size_threshold_bytes=1000)
Remove files that are too small. This is used to remove NGED JSON files that have no data
field.
Typical files sizes for NGED JSON files
- 486 bytes: no
dataand noWKT. - 2011 bytes: 12 timesteps in
dataand noWKT. - 4328 bytes: no
databut a largeWKTstring.
Source code in packages/nged_data/src/nged_data/storage.py
111 112 113 114 115 116 117 118 119 120 121 122 123 | |
download_and_parse_files(store, paths_df)
Load data end_time by end_time, in order, so more recent data overwrites older duplicates, if there are any duplicates.
Raises NoNewData if there is no new data.
Source code in packages/nged_data/src/nged_data/storage.py
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 | |
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 CLAUDE.md).
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
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 | |
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 that are more recent than the most recent
data already in our Delta table, 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
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 318 319 320 321 322 323 324 325 326 327 | |
upsert_metadata(new_metadata, metadata_path, storage_options=None)
Upserts metadata to a Parquet file.
This function assumes it is called by one thread at a time so no explicit locking is required.
If the local Parquet file does not exist, it saves the new_metadata. If it exists, it merges the new_metadata with the existing metadata, keeping the latest version for each time_series_id, and updates the Parquet file if there are differences.
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
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 361 362 363 364 365 366 367 368 369 370 371 372 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 | |