write
Write functions and Frame class for GWF files.
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
write ¶
write(filename: str | PathLike[str], 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)
Write channel data to a GWF file.
This is a convenience function for simple write operations. For more control, use the Frame class directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or path - like
|
Output file path |
required |
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'
|
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 single channel
>>> data = np.sin(np.linspace(0, 2*np.pi, 16384))
>>> gwframe.write('output.gwf', data, start=1234567890.0, sample_rate=16384,
... name='L1:TEST', unit='strain')
>>> # Write multiple channels
>>> gwframe.write('output.gwf',
... channels={'L1:CHAN1': data1, 'L1:CHAN2': data2},
... start=1234567890.0, sample_rate=16384, name='L1')
>>> # Write with different sample rates
>>> gwframe.write('output.gwf',
... channels={'L1:FAST': data1, 'L1:SLOW': data2},
... start=1234567890.0,
... sample_rate={'L1:FAST': 16384, 'L1:SLOW': 256},
... name='L1')
See Also
Frame : For more control over frame creation and metadata
Source code in gwframe/write.py
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 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 | |
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 | |