source: SHX/trunk/SeismicHandler/basics/tools.py @ 531

Revision 531, 1.5 KB checked in by marcus, 11 years ago (diff)
  • ...
  • method for expanding trace lists
  • Property svn:eol-style set to native
Line 
1# -*- coding: utf-8 -*-
2
3"""
4Collection of globally used helper functions.
5"""
6__all__ = ["Property", "Singleton"]
7
8# property wrapper
9def Property(func):
10    return property(**func())
11
12
13class Singleton(type):
14    """
15    Meta-class for singletons.
16    """
17
18    def __init__(cls, name, bases, dic):
19        super(Singleton, cls).__init__(name, bases, dic)
20        cls.instance = None
21
22    def __call__(cls, *args, **kwargs):
23        if cls.instance is None:
24            cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
25
26        return cls.instance
27
28
29def expandTraceList(trcs, selection):
30    """
31    Expand comma separated list of traces into sorted internal trace numbers
32    (reverse order).
33    """
34
35    if not len(trcs):
36        return None
37
38    selection = [i.lower() for i in selection.split(",")]
39
40    if "all" in selection:
41        selected = range(0, len(trcs))
42        selected.reverse()
43        return selected
44
45    selected = []
46    for s in selection:
47        # range
48        if "-" in s:
49            try:
50                start, stop = map(int, s.split("-"))
51            except:
52                print "  skipping '%s'" % s
53                continue
54
55            if stop < start:
56                start, stop = stop, start
57
58            selected.extend(range(start - 1, stop))
59
60        else:
61            if s.isdigit():
62                selected.append(int(s) - 1)
63            else:
64                print "  skipping '%s'" % s
65
66    # unique
67    selected = list(set(selected))
68    selected.sort()
69    selected.reverse()
70
71    return selected
Note: See TracBrowser for help on using the repository browser.