I/O -- Parsers & Writers¶
Functions and classes for reading and writing EnergyPlus models in IDF (text) and epJSON (JSON) formats.
Convenience Loaders¶
Top-level convenience functions:
load_idf(path, version=None, *, strict_parsing=True, strict=False, preserve_formatting=False)for IDF files. Strict parsing is on by default.load_epjson(path, version=None, *, strict=False, preserve_formatting=False)for epJSON files.
Load an IDF file and return an IDFDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the IDF file |
required |
version
|
tuple[int, int, int] | None
|
Optional version override (major, minor, patch) |
None
|
strict_parsing
|
bool
|
If True, fail fast on malformed IDF objects (default: True) |
True
|
strict
|
bool
|
When |
True
|
preserve_formatting
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
IDFDocument[bool]
|
Parsed IDFDocument |
Examples:
Load a DOE reference building and list its zones:
```python
model = load_idf("RefBldgSmallOfficeNew2004.idf")
print(f"Loaded {len(model)} objects")
for zone in model["Zone"]:
print(zone.name)
```
Override the version for a legacy model:
```python
model = load_idf("pre_v9_building.idf", version=(9, 6, 0))
```
Lossless round-trip:
```python
model = load_idf("building.idf", preserve_formatting=True)
write_idf(model, "building_copy.idf") # byte-identical
```
Source code in src/idfkit/__init__.py
Load an epJSON file and return an IDFDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the epJSON file |
required |
version
|
tuple[int, int, int] | None
|
Optional version override (major, minor, patch) |
None
|
strict
|
bool
|
When |
True
|
preserve_formatting
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
IDFDocument[bool]
|
Parsed IDFDocument |
Examples:
Load an epJSON model and iterate over zones:
```python
model = load_epjson("SmallOffice.epJSON")
for zone in model["Zone"]:
print(zone.name, zone.x_origin)
```
Specify an explicit EnergyPlus version:
```python
model = load_epjson("SmallOffice.epJSON", version=(24, 1, 0))
```
Source code in src/idfkit/__init__.py
IDF Parser¶
Streaming IDF parser - parses EnergyPlus IDF files into IDFDocument.
Features: - Memory-efficient streaming for large files - Regex-based tokenization - Direct parsing into IDFDocument (no intermediate structures) - Type coercion based on schema - Optional CST (Concrete Syntax Tree) for lossless round-tripping
IDFParser
¶
Streaming parser for IDF files.
Uses memory mapping for large files and regex for tokenization.
Source code in src/idfkit/idf_parser.py
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 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 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 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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | |
parse(version=None, *, strict=True, preserve_formatting=False)
¶
Parse the IDF file into an IDFDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
tuple[int, int, int] | None
|
Optional version override |
None
|
strict
|
bool
|
Enforce strict field access on the resulting document. |
True
|
preserve_formatting
|
bool
|
Build a CST for lossless round-tripping. |
False
|
Returns:
| Type | Description |
|---|---|
IDFDocument
|
Parsed IDFDocument |
Source code in src/idfkit/idf_parser.py
get_idf_version(filepath)
¶
Quick version detection without full parsing.
Only reads the first 10 KB of the file, making it very fast even for large models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path | str
|
Path to IDF file |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
Version tuple (major, minor, patch) |
Raises:
| Type | Description |
|---|---|
VersionNotFoundError
|
If version cannot be detected |
Examples:
Check which EnergyPlus version a model was created for (reads only the first 10 KB for speed):
```python
from idfkit import get_idf_version
version = get_idf_version("5ZoneAirCooled.idf")
print(f"EnergyPlus v{version[0]}.{version[1]}.{version[2]}")
```
Source code in src/idfkit/idf_parser.py
iter_idf_objects(filepath, encoding='latin-1')
¶
Iterate over objects in an IDF file without loading into document.
Yields:
| Type | Description |
|---|---|
tuple[str, str, list[str]]
|
Tuples of (object_type, name, [field_values]) |
This is useful for quick scanning or filtering without full parsing.
Note
Type names are returned exactly as they appear in the IDF file
(no schema-based case normalization). Use case-insensitive
comparisons when filtering: obj_type.upper() == "ZONE".
Examples:
Count thermal zones without loading the full document (useful for quickly sizing batch runs):
```python
from idfkit import iter_idf_objects
zone_count = sum(
1 for obj_type, name, _
in iter_idf_objects("5ZoneAirCooled.idf")
if obj_type == "Zone"
)
```
Collect all material names for an audit report:
```python
materials = [
name for obj_type, name, _
in iter_idf_objects("LargeOffice.idf")
if obj_type == "Material"
]
```
Source code in src/idfkit/idf_parser.py
parse_idf(filepath, schema=None, version=None, encoding='latin-1', strict_parsing=True, strict=True, *, preserve_formatting=False)
¶
Parse an IDF file into an IDFDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path | str
|
Path to the IDF file |
required |
schema
|
EpJSONSchema | None
|
Optional EpJSONSchema for field ordering and type coercion |
None
|
version
|
tuple[int, int, int] | None
|
Optional version override (auto-detected if not provided) |
None
|
encoding
|
str
|
File encoding (default: latin-1 for compatibility) |
'latin-1'
|
strict_parsing
|
bool
|
If True, fail fast on malformed objects (default: True) |
True
|
strict
|
bool
|
When |
True
|
preserve_formatting
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
IDFDocument
|
Parsed IDFDocument |
Raises:
| Type | Description |
|---|---|
VersionNotFoundError
|
If version cannot be detected |
IdfKitError
|
If parsing fails |
Examples:
Load and inspect a DOE reference building:
```python
from idfkit import parse_idf
model = parse_idf("RefBldgSmallOfficeNew2004.idf")
for zone in model["Zone"]:
print(zone.name, zone.x_origin)
```
Force a specific EnergyPlus version when auto-detection fails (e.g., a pre-v8.9 file that was manually upgraded):
```python
model = parse_idf("legacy_building.idf", version=(9, 6, 0))
```
Round-trip an IDF file losslessly:
```python
model = parse_idf("building.idf", preserve_formatting=True)
# write_idf(model) reproduces the original byte-for-byte
```
Source code in src/idfkit/idf_parser.py
epJSON Parser¶
epJSON parser - parses EnergyPlus epJSON files into IDFDocument.
The epJSON format is the native JSON representation of EnergyPlus models. Parsing is straightforward since it's already structured JSON.
EpJSONParser
¶
Parser for epJSON files.
epJSON is the native JSON format for EnergyPlus models, making parsing straightforward - just json.load() and transform.
Source code in src/idfkit/epjson_parser.py
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 | |
parse(version=None, *, strict=True, preserve_formatting=False)
¶
Parse the epJSON file into an IDFDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
tuple[int, int, int] | None
|
Optional version override |
None
|
strict
|
bool
|
Enforce strict field access on the resulting document. |
True
|
preserve_formatting
|
bool
|
Store raw JSON text for lossless round-tripping. |
False
|
Returns:
| Type | Description |
|---|---|
IDFDocument
|
Parsed IDFDocument |
Source code in src/idfkit/epjson_parser.py
get_epjson_version(filepath)
¶
Quick version detection from epJSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path | str
|
Path to epJSON file |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
Version tuple (major, minor, patch) |
Raises:
| Type | Description |
|---|---|
VersionNotFoundError
|
If version cannot be detected |
Examples:
Detect the EnergyPlus version of an epJSON file:
```python
from idfkit.epjson_parser import get_epjson_version
version = get_epjson_version("SmallOffice.epJSON")
print(f"EnergyPlus v{version[0]}.{version[1]}")
```
Source code in src/idfkit/epjson_parser.py
load_epjson(filepath)
¶
Load raw epJSON data without parsing into document.
Useful for quick inspection or manipulation when you need the raw JSON dict rather than an IDFDocument.
Examples:
Grab the raw JSON dict for custom post-processing:
```python
from idfkit.epjson_parser import load_epjson
data = load_epjson("SmallOffice.epJSON")
zone_names = list(data.get("Zone", {}).keys())
```
Source code in src/idfkit/epjson_parser.py
parse_epjson(filepath, schema=None, version=None, strict=True, *, preserve_formatting=False)
¶
Parse an epJSON file into an IDFDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path | str
|
Path to the epJSON file |
required |
schema
|
EpJSONSchema | None
|
Optional EpJSONSchema for validation |
None
|
version
|
tuple[int, int, int] | None
|
Optional version override (auto-detected if not provided) |
None
|
strict
|
bool
|
When |
True
|
preserve_formatting
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
IDFDocument
|
Parsed IDFDocument |
Raises:
| Type | Description |
|---|---|
VersionNotFoundError
|
If version cannot be detected |
IdfKitError
|
If parsing fails |
Examples:
Load an epJSON model and list its thermal zones:
```python
from idfkit import parse_epjson
model = parse_epjson("SmallOffice.epJSON")
for zone in model["Zone"]:
print(zone.name)
```
Source code in src/idfkit/epjson_parser.py
Writers¶
Writers for IDF and epJSON formats.
Provides serialization of IDFDocument to both formats.
The write_idf function accepts an output_type parameter that
mirrors eppy's idf.outputtype options:
"standard"(default): field comments included (!- Field Name)."nocomment": no field comments, one field per line."compressed": entire object on a single line (minimal whitespace).
EpJSONWriter
¶
Writes IDFDocument to epJSON format.
The epJSON format is:
{
"Version": {
"Version 1": {
"version_identifier": "23.2"
}
},
"Zone": {
"Zone 1": {
"direction_of_relative_north": 0.0,
...
}
}
}
Source code in src/idfkit/writers.py
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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
to_dict()
¶
Convert document to epJSON dict.
Source code in src/idfkit/writers.py
write_to_file(filepath, indent=2)
¶
IDFWriter
¶
Writes IDFDocument to IDF text format.
The IDF format is:
Supports three output_type modes mirroring eppy's
idf.outputtype:
"standard"— full comments (default)."nocomment"— no field comments, one field per line."compressed"— each object on a single line.
Source code in src/idfkit/writers.py
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 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 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 | |
format_object(obj)
¶
Convert a single object to IDF string.
Source code in src/idfkit/writers.py
to_string()
¶
Convert document to IDF string.
Source code in src/idfkit/writers.py
write_to_file(filepath, encoding='latin-1')
¶
convert_epjson_to_idf(epjson_path, idf_path=None)
¶
Convert an epJSON file to IDF format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epjson_path
|
Path | str
|
Input epJSON file path |
required |
idf_path
|
Path | str | None
|
Output IDF path (default: same name with .idf extension) |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the output file |
Examples:
Convert an epJSON model back to classic IDF format:
```python
output = convert_epjson_to_idf("5ZoneAirCooled.epJSON")
# Creates 5ZoneAirCooled.idf
convert_epjson_to_idf("modern_model.epJSON", "classic_model.idf")
```
Source code in src/idfkit/writers.py
convert_idf_to_epjson(idf_path, epjson_path=None)
¶
Convert an IDF file to epJSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idf_path
|
Path | str
|
Input IDF file path |
required |
epjson_path
|
Path | str | None
|
Output epJSON path (default: same name with .epJSON extension) |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the output file |
Examples:
Convert an IDF model to native JSON format:
```python
output = convert_idf_to_epjson("5ZoneAirCooled.idf")
# Creates 5ZoneAirCooled.epJSON
convert_idf_to_epjson("legacy_model.idf", "modern_model.epJSON")
```
Source code in src/idfkit/writers.py
write_epjson(doc, filepath=None, indent=2, *, preserve_formatting=None)
¶
Write document to epJSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
IDFDocument[bool]
|
The document to write |
required |
filepath
|
Path | str | None
|
Output path (if None, returns string) |
None
|
indent
|
int
|
JSON indentation |
2
|
preserve_formatting
|
bool | None
|
If |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
JSON string if filepath is None, otherwise None |
Examples:
Serialize the model to epJSON for use with EnergyPlus v9.3+:
>>> from idfkit import new_document, write_epjson
>>> model = new_document()
>>> model.add("Zone", "Perimeter_ZN_1")
Zone('Perimeter_ZN_1')
>>> json_str = write_epjson(model)
>>> '"Zone"' in json_str
True
Write to disk:
```python
write_epjson(model, "in.epJSON")
```
Source code in src/idfkit/writers.py
write_idf(doc, filepath=None, encoding='latin-1', output_type='standard', *, preserve_formatting=None)
¶
Write document to IDF format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
IDFDocument[bool]
|
The document to write. |
required |
filepath
|
Path | str | None
|
Output path (if |
None
|
encoding
|
str
|
Output encoding. |
'latin-1'
|
output_type
|
OutputType
|
Output formatting mode — |
'standard'
|
preserve_formatting
|
bool | None
|
If |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
IDF string if filepath is |
Examples:
Serialize the model to an IDF string for inspection:
>>> from idfkit import new_document, write_idf
>>> model = new_document()
>>> model.add("Zone", "Perimeter_ZN_1")
Zone('Perimeter_ZN_1')
>>> idf_str = write_idf(model)
>>> "Zone," in idf_str
True
Write to disk for EnergyPlus simulation:
```python
write_idf(model, "in.idf")
```
Use compressed format for batch parametric runs:
>>> compressed = write_idf(model, output_type="compressed")
>>> "\n" not in compressed.split("Zone")[1].split(";")[0]
True
Lossless round-trip:
```python
from idfkit import load_idf, write_idf
model = load_idf("building.idf", preserve_formatting=True)
write_idf(model, "building_copy.idf") # byte-identical
```