source: SH_SHM/trunk/source/seed_io/sfdline.py @ 303

Revision 303, 2.6 KB checked in by marcus, 12 years ago (diff)

r145 | walther | 2011-01-11 23:18:02 +0100 (Di, 11 Jan 2011) | 12 lines

  • Extended sfdline for output of additional information about network and location code (on by default). Setting the new option "-nonetloc" will reveal the old behaviour. This change should be transparent for any Seismic Handler installations, since this extra information is ignored by default. To access a code using network and location code, simply change for station MOX (network GR and empty location code):

reads . 10-APR-2010_00:00:19 5 mox z bh

to

reads . 10-APR-2010_00:00:19 5 mox.gr.. z bh

Please note: SHM doesn't yet support these schema.

  • Added python version of sfdline (only subset of options supported).
  • Property svn:eol-style set to native
Line 
1#! /bin/env python
2
3import sys
4import os
5from obspy.core import read
6from obspy.sh.core import fromUTCDateTime
7
8def 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
68if __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))
Note: See TracBrowser for help on using the repository browser.