Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding Blockette 1000 to MiniSEED files without one.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Script adding Blockette 1000 to MiniSEED files that have been written without
one. It will discard all other blockettes and furthermore requires at least 8
bytes between the fixed header and the beginning of the data.
Requirements: Python >= 2.7
USAGE
=====
$ python add_blockette_1000.py
[--reclen {128,256,512,1024,2048,4096,8192,16384,32768,65536,131072}]
[--encoding {STEIM1,STEIM2,...}]
[--header-endian {<,>}]
[--data-endian {<,>}]
input_filename output_filename
The MIT License (MIT)
Copyright (c) 2016 Lion Krischer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import argparse
import io
import math
import os
import struct
ENCODINGS = {
"ASCII": 0,
"INT16": 1,
"INT24": 2,
"INT32": 3,
"FLOAT32": 4,
"FLOAT64": 5,
"STEIM1": 10,
"STEIM2": 11,
"GEOSCOPE24": 12,
"GEOSCOPE163": 13,
"GEOSCOPE164": 14,
"USNATIONAL": 15,
"CDSN": 16,
"GRAEFENBERG": 17,
"IPG": 18,
"STEIM3": 19,
"SRO": 30,
"HGLP": 31,
"DWWSSN": 32,
"RSTN": 33}
parser = argparse.ArgumentParser(
prog="python add_blockette_1000.py",
description="Add blockette 1000 to MiniSEED records without one. Will "
"discard all other blockettes. Requires at least 8 bytes to be "
"available between the fixed header and the beginning of the data.")
parser.add_argument('input', type=str, help='The input MiniSEED file.')
parser.add_argument('output', type=str, help='The output MiniSEED file.')
parser.add_argument('--reclen', type=int,
help='The record length of the input file.',
default=4096, choices=[2 ** _i for _i in range(7, 18)])
parser.add_argument('--encoding', type=str, help='The encoding of the data.',
default="STEIM1", choices=sorted(ENCODINGS.keys()))
parser.add_argument('--header-endian', type=str,
help='The byteorder of the header.',
default=">", choices=["<", ">"])
parser.add_argument('--data-endian', type=str,
help='The byteorder of the data.',
default=">", choices=["<", ">"])
args = parser.parse_args()
assert os.path.isfile(args.input), "Input file must exist"
assert not os.path.exists(args.output) and os.path.isdir(os.path.dirname(
os.path.abspath(args.output))), \
"Output file must not yet exist but its folder must."
with io.open(args.input, "rb") as fh_in, \
io.open(args.output, "wb") as fh_out:
while True:
# Copy first 38 bytes.
d_in = fh_in.read(39)
if not d_in:
break
fh_out.write(d_in)
# Write number of blockettes that follow.
fh_in.read(1)
fh_out.write(struct.pack("B", 1))
# Copy next 4 bytes.
fh_out.write(fh_in.read(4))
# Make sure there is enough space for blockette 1000.
beginning_of_data = struct.unpack("%sH" % args.header_endian,
fh_in.read(2))[0]
if beginning_of_data < 48 + 8:
raise ValueError("Requires at least 8 bytes between fixed header "
"and beginning of data")
fh_out.write(struct.pack("%sH" % args.header_endian,
beginning_of_data))
# Write first blockette field
fh_in.read(2)
fh_out.write(struct.pack("%sH" % args.header_endian, 48))
# Write Blockette 1000.
fh_in.read(8)
fh_out.write(struct.pack(
"%sHHBBBB" % args.header_endian, 1000, 0,
ENCODINGS[args.encoding],
1 if args.data_endian == ">" else 0,
int(math.log(args.reclen, 2)), 0))
# Copy the rest.
fh_out.write(fh_in.read(args.reclen - 56))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment