read
Read functions for GWF files.
read ¶
read(source: str | PathLike[str] | BinaryIO, channels: str, frame_index: int = 0, *, validate_checksum: bool = False, start: float | None = None, end: float | None = None, allow_invalid: bool = False) -> TimeSeries
read(source: str | PathLike[str] | BinaryIO, channels: str | None | list[str] = None, frame_index: int = 0, *, validate_checksum: bool = False, start: float | None = None, end: float | None = None, allow_invalid: bool = False) -> 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 |
channels
|
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.start} to {data.start + data.duration}")
>>> # Read all channels
>>> all_data = gwframe.read('data.gwf', channels=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.start} to "
... f"{data.start + 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
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 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 | |
read_bytes ¶
read_bytes(data: bytes, channels: str, frame_index: int = 0, *, validate_checksum: bool = False, start: float | None = None, end: float | None = None, allow_invalid: bool = False) -> TimeSeries
read_bytes(data: bytes, channels: str | None | list[str] = None, frame_index: int = 0, *, validate_checksum: bool = False, start: float | None = None, end: float | None = None, allow_invalid: bool = False) -> 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 |
channels
|
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, channels=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
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 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
read_frames ¶
read_frames(filename: str | PathLike[str], *, allow_invalid: bool = False) -> Generator[Frame, None, None]
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.start}")
>>> # 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
Source code in gwframe/read.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 | |