1 | #! /bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | from obspy.core import read |
---|
6 | from obspy.sh.core import fromUTCDateTime |
---|
7 | |
---|
8 | def process_mseed(*args, **kwargs): |
---|
9 | """ |
---|
10 | Output information about mseed file for Seismic Handler's seed reading |
---|
11 | routine. |
---|
12 | |
---|
13 | Output may be redirected to file (second argument). |
---|
14 | """ |
---|
15 | |
---|
16 | try: |
---|
17 | stream = read(args[0], format="MSEED") |
---|
18 | except: |
---|
19 | quit("Cannot read input file: '%s'" % args[0]) |
---|
20 | |
---|
21 | if len(args) == 2: |
---|
22 | mode = kwargs["overwrite"] and "w" or "a" |
---|
23 | try: |
---|
24 | out = open(args[1], mode) |
---|
25 | except: |
---|
26 | quit("Cannot write to output file '%s'" % args[1]) |
---|
27 | else: |
---|
28 | out = sys.stdout |
---|
29 | |
---|
30 | # shortcut |
---|
31 | sts = stream[0].stats |
---|
32 | |
---|
33 | info = { |
---|
34 | # station + channel + component |
---|
35 | "s": ("-".join([sts.station, sts.channel[:2], sts.channel[2]])).lower(), |
---|
36 | # file |
---|
37 | "n": args[0], |
---|
38 | # starttime |
---|
39 | "b": fromUTCDateTime(sts.starttime), |
---|
40 | # endtime |
---|
41 | "e": fromUTCDateTime(sts.endtime), |
---|
42 | # number of records (file size / reclen) |
---|
43 | "r": str(os.stat(args[0])[6] / sts.mseed.record_length), |
---|
44 | # header swap |
---|
45 | "h": sts.mseed.byteorder == ">" and "0" or "1", |
---|
46 | # record length |
---|
47 | "l": sts.mseed.record_length, |
---|
48 | # byte offset (always zero) |
---|
49 | "o": "0", |
---|
50 | # network and location code |
---|
51 | "a": ((not sts.network and " " or sts.network) + \ |
---|
52 | (not sts.location and " " or sts.location)).replace(" ", "."), |
---|
53 | } |
---|
54 | |
---|
55 | # "standard" order of fields (SH doesn't care about it, but maybe others) |
---|
56 | order = "snberhlo" |
---|
57 | if kwargs["netloc"]: |
---|
58 | order += "a" |
---|
59 | |
---|
60 | for i in order: |
---|
61 | print >> out, "%s>%s" % (i, info[i]), |
---|
62 | print >> out, "" |
---|
63 | |
---|
64 | # close output file if not stdout |
---|
65 | if out != sys.stdout: |
---|
66 | out.close() |
---|
67 | |
---|
68 | if __name__ == "__main__": |
---|
69 | from optparse import OptionParser |
---|
70 | |
---|
71 | parser = OptionParser(usage="%prog [options] seedfile [outfile]") |
---|
72 | |
---|
73 | parser.add_option("--no-netloc", |
---|
74 | action="store_false", dest="netloc", default=True, |
---|
75 | help="Do not list network and location code.") |
---|
76 | |
---|
77 | parser.add_option("--overwrite", |
---|
78 | action="store_true", dest="overwrite", default=False, |
---|
79 | help="Overwrite outfile file (if given). Default will append " |
---|
80 | "information to exiting file.") |
---|
81 | |
---|
82 | (opts, args) = parser.parse_args() |
---|
83 | |
---|
84 | if not len(args): |
---|
85 | parser.error("Missing MSEED file for inspection.") |
---|
86 | |
---|
87 | if len(args) == 1 and opts.overwrite: |
---|
88 | parser.error("Option 'overwrite' set, but no output file given!") |
---|
89 | |
---|
90 | process_mseed(*args, **vars(opts)) |
---|