gwframe
Frame ¶
Frame(start: float, duration: float, name: str = '', run: int = 0, frame_number: int = 0, frame_spec: int | None = None)
Bases: MutableMapping
High-level interface for creating and manipulating GWF frames.
This class provides a Pythonic interface to the underlying frameCPP FrameH class, with simplified methods for adding data and metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
float
|
GPS start time of the frame |
required |
duration
|
float
|
Duration of the frame in seconds |
required |
name
|
str
|
Frame name (e.g., 'L1' for LIGO Livingston) |
''
|
run
|
int
|
Run number (default: 0, negative for simulated data) |
0
|
Notes
Detector information is automatically added to the frame based on channel names. When you add a channel with a name like 'L1:TEST', the detector information for L1 will be automatically included.
Examples:
>>> frame = gwframe.Frame(start=1234567890.0, duration=1.0, name='L1', run=1)
>>> frame.add_channel('L1:TEST', data=np.random.randn(16384),
... dt=1.0/16384, unit='counts')
>>> frame.write('output.gwf')
Source code in gwframe/write.py
__getitem__ ¶
__getitem__(key: str) -> TimeSeries
__iter__ ¶
__setitem__ ¶
__setitem__(key: str, value: TimeSeries) -> None
add_channel ¶
add_channel(channel: str, data: NDArray | MaskedArray, sample_rate: float, unit: str = '', comment: str = '', channel_type: str = 'proc', on_mask_loss: str | OnMaskLoss = WARN)
Add a data channel to this frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
str
|
Channel name (e.g., 'L1:TEST-CHANNEL') |
required |
data
|
ndarray
|
1D NumPy array containing the channel data |
required |
sample_rate
|
float
|
Sample rate in Hz (samples per second) |
required |
unit
|
str
|
Physical unit of the data (e.g., 'strain', 'counts') |
''
|
comment
|
str
|
Comment or description for this channel |
''
|
channel_type
|
str
|
Type of channel: 'proc' (processed, default) or 'sim' (simulated) |
'proc'
|
Examples:
Notes
The data type (float64, float32, int32, etc.) is automatically determined from the NumPy array dtype.
Source code in gwframe/write.py
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 | |
add_history ¶
Add a history/metadata entry to the frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name/key for this metadata entry |
required |
comment
|
str
|
The metadata value/comment |
required |
time
|
int
|
GPS time for this entry (default: frame start time) |
None
|
Source code in gwframe/write.py
write ¶
write(filename: str | PathLike[str], compression: int = ZERO_SUPPRESS_OTHERWISE_GZIP, compression_level: int = 6)
Write this frame to a GWF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or path - like
|
Output file path |
required |
compression
|
int
|
Compression scheme (default: Compression.ZERO_SUPPRESS_OTHERWISE_GZIP) Use Compression.RAW for no compression |
ZERO_SUPPRESS_OTHERWISE_GZIP
|
compression_level
|
int
|
Compression level 0-9 (default: 6, higher = more compression) |
6
|
Examples:
>>> frame.write('output.gwf')
>>> frame.write('output_raw.gwf', compression=gwframe.Compression.RAW)
Source code in gwframe/write.py
write_bytes ¶
Write this frame to bytes (in-memory GWF format).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compression
|
int
|
Compression scheme (default: Compression.ZERO_SUPPRESS_OTHERWISE_GZIP) |
ZERO_SUPPRESS_OTHERWISE_GZIP
|
compression_level
|
int
|
Compression level 0-9 (default: 6) |
6
|
Returns:
| Type | Description |
|---|---|
bytes
|
GWF-formatted data as bytes |
Examples:
>>> frame = gwframe.Frame(start=1234567890.0, duration=1.0, name='L1')
>>> frame.add_channel('L1:TEST', data, dt=1.0/16384)
>>> gwf_bytes = frame.write_bytes()
>>> # Verify round-trip
>>> read_data = gwframe.read_bytes(gwf_bytes, 'L1:TEST')
Source code in gwframe/write.py
write_to_stream ¶
write_to_stream(stream, compression: int = ZERO_SUPPRESS_OTHERWISE_GZIP, compression_level: int = 6)
Write this frame to an output stream.
This is used internally by FrameWriter for writing multiple frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
OFrameFStream
|
Output stream to write to |
required |
compression
|
int
|
Compression scheme (default: Compression.ZERO_SUPPRESS_OTHERWISE_GZIP) |
ZERO_SUPPRESS_OTHERWISE_GZIP
|
compression_level
|
int
|
Compression level 0-9 (default: 6) |
6
|
Source code in gwframe/write.py
FrameWriter ¶
FrameWriter(destination: str | PathLike[str] | BytesIO, compression: int = ZERO_SUPPRESS_OTHERWISE_GZIP, compression_level: int = 6, frame_number: int = 0, frame_spec: int | None = None)
Context manager for writing multiple frames to a GWF file or BytesIO buffer.
This is the recommended way to write multiple frames, as it keeps the output stream open and efficiently writes frames sequentially.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
destination
|
str, path-like, or BytesIO
|
Output destination - either a file path or BytesIO object |
required |
compression
|
int
|
Compression scheme (default: Compression.ZERO_SUPPRESS_OTHERWISE_GZIP) |
ZERO_SUPPRESS_OTHERWISE_GZIP
|
compression_level
|
int
|
Compression level 0-9 (default: 6) |
6
|
Examples:
>>> # Write multiple 1-second frames to file
>>> with gwframe.FrameWriter('output.gwf') as writer:
... for i in range(10):
... start = 1234567890.0 + i
... data = np.random.randn(16384)
... writer.write(data, start=start, sample_rate=16384, name='L1:TEST')
>>> # Write to BytesIO
>>> from io import BytesIO
>>> buffer = BytesIO()
>>> with gwframe.FrameWriter(buffer) as writer:
... for i in range(10):
... data = np.random.randn(16384)
... writer.write(data, start=1234567890.0 + i,
... sample_rate=16384, name='L1:TEST')
>>> gwf_bytes = buffer.getvalue()
Source code in gwframe/write.py
close ¶
Finalize and close the writer.
For file destinations, this flushes the stream and atomically moves the temporary file to the final path. For BytesIO destinations, this extracts the bytes and writes them to the buffer.
This method is idempotent — calling it on an already-closed writer is a no-op.
Source code in gwframe/write.py
open ¶
Open the writer for writing frames.
This sets up the output stream. For file destinations, writes go to a
temporary file that is atomically moved to the final path on
:meth:close.
Can also be used via the context manager protocol (with statement),
which calls :meth:open and :meth:close automatically.
Source code in gwframe/write.py
write ¶
write(channels: dict[str, NDArray] | NDArray, start: float, sample_rate: float | dict[str, float], *, name: str = '', run: int = 0, unit: str | dict[str, str] = '', channel_type: str = 'proc', on_mask_loss: str | OnMaskLoss = WARN)
Convenience method to write data directly without creating Frame object.
This creates a Frame internally and writes it immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
dict or ndarray
|
Channel data. Either: - dict mapping channel names to 1D NumPy arrays - Single 1D NumPy array (requires channel name in name parameter) |
required |
start
|
float
|
GPS start time of the frame |
required |
sample_rate
|
float or dict
|
Sample rate in Hz. Either: - Single float value used for all channels - dict mapping channel names to sample rates |
required |
name
|
str
|
Frame name (e.g., 'L1') or single channel name if channels is an array |
''
|
run
|
int
|
Run number (default: 0, negative for simulated data) |
0
|
unit
|
str or dict
|
Physical unit. Either: - Single string used for all channels (default: '') - dict mapping channel names to units |
''
|
channel_type
|
str
|
Type of channels: 'proc' (processed, default) or 'sim' (simulated) |
'proc'
|
Examples:
>>> with gwframe.FrameWriter('output.gwf') as writer:
... for i in range(10):
... data = np.random.randn(16384)
... writer.write(
... data, start=1234567890.0 + i, sample_rate=16384, name='L1:TEST'
... )
Source code in gwframe/write.py
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 | |
write_frame ¶
write_frame(frame: Frame)
Write a Frame object to the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
The frame to write |
required |
Examples:
>>> with gwframe.FrameWriter('output.gwf') as writer:
... frame = gwframe.Frame(start=1234567890.0, duration=1.0, name='L1')
... frame.add_channel('L1:TEST', data, dt=1.0/16384)
... writer.write_frame(frame)
Notes
If the frame was created with frame_number=0 (default), the writer will use its tracked frame_number. Otherwise, the frame's frame_number is used. Frame numbers auto-increment with each write.
Source code in gwframe/write.py
TimeSeries
dataclass
¶
TimeSeries(array: NDArray[floating], name: str, dtype: dtype, start: float, dt: float, duration: float, sample_rate: float, unit: str, type: str, mask: NDArray[bool_] | None = None)
Time series data from a GWF channel.
This dataclass holds the array data and metadata for a channel read from a GWF file.
Attributes:
| Name | Type | Description |
|---|---|---|
array |
ndarray
|
NumPy array containing the time series data |
name |
str
|
Channel name (e.g., 'H1:LOSC-STRAIN') |
dtype |
dtype
|
NumPy dtype of the samples. Always mirrors |
start |
float
|
Start time in GPS seconds |
dt |
float
|
Sample spacing in seconds |
duration |
float
|
Total duration in seconds |
sample_rate |
float
|
Sampling rate in Hz (1/dt) |
unit |
str
|
Physical unit of the data (e.g., 'strain') |
type |
str
|
Channel type: 'proc' (processed), 'adc' (raw ADC), or 'sim' (simulated) |
mask |
ndarray or None
|
Boolean mask where |
Examples:
>>> data = gwframe.read('data.gwf', 'H1:LOSC-STRAIN')
>>> print(f"Channel: {data.name}")
>>> print(f"Duration: {data.duration} s at {data.sample_rate} Hz")
>>> print(f"Data shape: {data.array.shape}")
>>> # Check for invalid samples
>>> if data.mask is not None:
... print(f"Invalid samples: {data.mask.sum()}")
to_masked ¶
to_masked() -> NDArray[floating] | MaskedArray
Return data as a masked array if a mask is present, otherwise plain array.
This is useful when passing data to APIs that understand masked arrays, or when you want numpy operations to automatically skip invalid samples.
Returns:
| Type | Description |
|---|---|
MaskedArray or ndarray
|
Masked array if |
Source code in gwframe/types.py
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 | |
write_bytes ¶
write_bytes(channels: dict[str, NDArray] | NDArray, start: float, sample_rate: float | dict[str, float], *, name: str = '', run: int = 0, unit: str | dict[str, str] = '', channel_type: str = 'proc', compression: int = ZERO_SUPPRESS_OTHERWISE_GZIP, compression_level: int = 6, on_mask_loss: str | OnMaskLoss = WARN, frame_spec: int | None = None) -> bytes
Write channel data to bytes (in-memory GWF format).
Parameters are identical to write() function.
Returns:
| Type | Description |
|---|---|
bytes
|
GWF-formatted data as bytes |
Examples:
>>> data = np.sin(np.linspace(0, 2*np.pi, 16384))
>>> gwf_bytes = gwframe.write_bytes(
... data, start=1234567890.0, sample_rate=16384, name='L1:TEST'
... )
>>> # Verify round-trip
>>> read_data = gwframe.read_bytes(gwf_bytes, 'L1:TEST')
See Also
write : Write channel data to a file Frame.write_bytes : Write a Frame object to bytes
Source code in gwframe/write.py
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 | |