I/O -- Parsers & Writers¶
Functions and classes for reading and writing EnergyPlus models in IDF (text) and epJSON (JSON) formats.
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
IDFParser
¶
Streaming parser for IDF files.
Uses memory mapping for large files and regex for tokenization.
Source code in src/idfkit/idf_parser.py
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 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 | |
parse(version=None)
¶
Parse the IDF file into an IDFDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
tuple[int, int, int] | None
|
Optional version override |
None
|
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.
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')
¶
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'
|
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))
```
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
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 | |
parse(version=None)
¶
Parse the epJSON file into an IDFDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
tuple[int, int, int] | None
|
Optional version override |
None
|
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)
¶
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
|
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
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 | |
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
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 | |
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)
¶
Write document to epJSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
IDFDocument
|
The document to write |
required |
filepath
|
Path | str | None
|
Output path (if None, returns string) |
None
|
indent
|
int
|
JSON indentation |
2
|
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')
¶
Write document to IDF format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
IDFDocument
|
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'
|
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