write
Write functions and Frame class for GWF files.
Frame ¶
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 |
|---|---|---|---|
t0
|
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(t0=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, sample_rate: float, unit: str = '', comment: str = '', channel_type: str = 'proc')
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
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 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 | |
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(t0=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)
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):
... t0 = 1234567890.0 + i
... data = np.random.randn(16384)
... writer.write(data, t0=t0, 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, t0=1234567890.0 + i,
... sample_rate=16384, name='L1:TEST')
>>> gwf_bytes = buffer.getvalue()
Source code in gwframe/write.py
write ¶
write(channels: dict[str, NDArray] | NDArray, t0: float, sample_rate: float | dict[str, float], *, name: str = '', run: int = 0, unit: str | dict[str, str] = '', channel_type: str = 'proc')
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 |
t0
|
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, t0=1234567890.0 + i, sample_rate=16384, name='L1:TEST'
... )
Source code in gwframe/write.py
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 | |
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(t0=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, t0: 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)
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 |
t0
|
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, t0=1234567890.0, sample_rate=16384,
... name='L1:TEST', unit='strain')
>>> # Write multiple channels
>>> gwframe.write('output.gwf',
... channels={'L1:CHAN1': data1, 'L1:CHAN2': data2},
... t0=1234567890.0, sample_rate=16384, name='L1')
>>> # Write with different sample rates
>>> gwframe.write('output.gwf',
... channels={'L1:FAST': data1, 'L1:SLOW': data2},
... t0=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
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 814 815 816 817 818 819 820 821 822 823 824 825 826 | |
write_bytes ¶
write_bytes(channels: dict[str, NDArray] | NDArray, t0: 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) -> 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, t0=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