read
Read functions for GWF files.
read ¶
read(source: str | PathLike[str] | BinaryIO, channel: str, frame_index: int = 0, *, validate_checksum: bool = False, start: float | None = None, end: float | None = None) -> TimeSeries
read(source: str | PathLike[str] | BinaryIO, channel: str | None | list[str] = None, frame_index: int = 0, *, validate_checksum: bool = False, start: float | None = None, end: float | None = None) -> TimeSeries | dict[str, TimeSeries]
Read channel data from a GWF file or file-like object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str, path-like, or file-like object
|
Either a path to the GWF file (str or PathLike), or a file-like object with a .read() method (e.g., open('file.gwf', 'rb'), BytesIO) |
required |
channel
|
str, None, or list[str]
|
Channel(s) to read: - str: Read single channel (e.g., 'L1:GWOSC-16KHZ_R1_STRAIN') - None: Read all channels from the frame (default) - list[str]: Read specific list of channels |
None
|
frame_index
|
int
|
Index of the frame to read from (default: 0). Mutually exclusive with start/end parameters. |
0
|
validate_checksum
|
bool
|
Validate frame file checksums before reading (default: False). When enabled, performs file-level checksum validation which requires reading the entire frame file. Disabled by default for performance. |
False
|
start
|
float
|
GPS start time for time-based slicing. Must be used with end parameter. When specified, reads and stitches all frames overlapping [start, end). Mutually exclusive with frame_index parameter. |
None
|
end
|
float
|
GPS end time for time-based slicing. Must be used with start parameter. When specified, reads and stitches all frames overlapping [start, end). Mutually exclusive with frame_index parameter. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
data |
TimeSeries or dict[str, TimeSeries]
|
|
Examples:
>>> # Read single channel from file path
>>> data = gwframe.read('data.gwf', 'L1:GWOSC-16KHZ_R1_STRAIN')
>>> print(f"Read {len(data.array)} samples at {data.sample_rate} Hz")
>>> print(f"Time range: {data.t0} to {data.t0 + data.duration}")
>>> # Read all channels
>>> all_data = gwframe.read('data.gwf', channel=None)
>>> print(f"Found {len(all_data)} channels: {list(all_data.keys())}")
>>> # Read specific list of channels
>>> channels = ['L1:STRAIN', 'L1:LSC-DARM_IN1']
>>> data_dict = gwframe.read('data.gwf', channels)
>>> for ch, ts in data_dict.items():
... print(f"{ch}: {len(ts.array)} samples")
>>> # Read from file object
>>> with open('data.gwf', 'rb') as f:
... data = gwframe.read(f, 'L1:GWOSC-16KHZ_R1_STRAIN')
>>> # Read from BytesIO
>>> from io import BytesIO
>>> data = gwframe.read(BytesIO(gwf_bytes), 'L1:STRAIN')
>>> # Time-based slicing (reads and stitches multiple frames)
>>> data = gwframe.read('multi_frame.gwf', 'L1:STRAIN',
... start=1234567890.0, end=1234567900.0)
>>> print(
... f"Read {data.duration} seconds from {data.t0} to "
... f"{data.t0 + data.duration}"
... )
Notes
When using time-based slicing (start/end parameters), this function automatically finds, reads, and stitches together all frames that overlap with the requested time range. The returned data is sliced to the exact [start, end) interval.
When reading from file-like objects, the entire file is loaded into memory.
Source code in gwframe/read.py
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 | |
read_bytes ¶
read_bytes(data: bytes, channel: str, frame_index: int = 0, *, validate_checksum: bool = False, start: float | None = None, end: float | None = None) -> TimeSeries
read_bytes(data: bytes, channel: str | None | list[str] = None, frame_index: int = 0, *, validate_checksum: bool = False, start: float | None = None, end: float | None = None) -> TimeSeries | dict[str, TimeSeries]
Read channel data from GWF data in memory (bytes).
This allows reading GWF data without writing to disk first, which is useful when working with data from network streams, compressed archives, or in-memory buffers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Raw GWF file data as bytes |
required |
channel
|
str, None, or list[str]
|
Channel(s) to read: - str: Read single channel (e.g., 'L1:GWOSC-16KHZ_R1_STRAIN') - None: Read all channels from the frame (default) - list[str]: Read specific list of channels |
None
|
frame_index
|
int
|
Index of the frame to read from (default: 0) |
0
|
validate_checksum
|
bool
|
Validate frame file checksums before reading (default: False). When enabled, performs file-level checksum validation which requires reading the entire frame file. Disabled by default for performance. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
data |
TimeSeries or dict[str, TimeSeries]
|
|
Examples:
>>> with open('data.gwf', 'rb') as f:
... gwf_bytes = f.read()
>>> data = gwframe.read_bytes(gwf_bytes, 'L1:GWOSC-16KHZ_R1_STRAIN')
>>> print(f"Read {len(data.array)} samples at {data.sample_rate} Hz")
>>> # Read all channels
>>> all_data = gwframe.read_bytes(gwf_bytes, channel=None)
>>> print(f"Found {len(all_data)} channels")
>>> import io
>>> from io import BytesIO
>>> data = gwframe.read_bytes(BytesIO(gwf_bytes).read(), 'L1:STRAIN')
Notes
This function uses frameCPP's MemoryBuffer internally to read from memory without writing to disk.
Source code in gwframe/read.py
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 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
read_frames ¶
Read frames from a GWF file, preserving complete metadata.
Yields Frame objects that can be written directly to disk with identical metadata (frame name, run number, frame number, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or path - like
|
Path to the GWF file |
required |
Yields:
| Name | Type | Description |
|---|---|---|
frame |
Frame
|
Frame object containing all channel data with correct sample rates, units, types, and original frame metadata |
Examples:
>>> # Iterate over frames
>>> for frame in gwframe.read_frames('data.gwf'):
... print(f"Frame {frame.name} at GPS {frame.t0}")
>>> # Process and write frames
>>> with gwframe.FrameWriter('output.gwf') as writer:
... for frame in gwframe.read_frames('input.gwf'):
... writer.write_frame(frame)
>>> # Collect all frames into a list
>>> frames = list(gwframe.read_frames('data.gwf'))
>>> print(f"Read {len(frames)} frames")
See Also
read : Read channel data from frames Frame : Frame object for creating and manipulating frames FrameWriter : Context manager for writing frames to files