source: SHX/trunk/SeismicHandler/basics/analysispar.py @ 1101

Revision 1101, 5.8 KB checked in by klaus, 4 years ago (diff)

handle None-depths in events; backslash use in filenames of reado; phase clear_all command

Line 
1# -*- coding: utf-8 -*-
2
3#    This file is part of Seismic Handler eXtended (SHX). For terms of use and
4#    license information please see license.txt and visit
5#    http://www.seismic-handler.org/portal/wiki/Shx/LicenseTerms
6
7"""
8Manage anlysis parameters.
9"""
10
11
12from SeismicHandler.basics import Singleton
13
14
15PICKTYPE_MANUAL = 1
16PICKTYPE_THEO   = 2
17PICKTYPE_AUTO   = 3
18PICKTYPE_MARK   = 4
19DEFAULT_PICKTYPE = PICKTYPE_MANUAL
20
21typenames = {
22    PICKTYPE_MANUAL : 'manual',
23    PICKTYPE_THEO   : 'theo',
24    PICKTYPE_AUTO   : 'auto',
25    PICKTYPE_MARK   : 'mark',
26}
27
28typecolors = {
29    PICKTYPE_MANUAL : 'red',
30    PICKTYPE_THEO   : 'green',
31    PICKTYPE_AUTO   : 'orange',
32    PICKTYPE_MARK   : 'violet',
33}
34
35
36
37class Phase:
38
39    def __init__( self, name=None, picktime=None, picktype=None,
40        comp=None, weight=None ):
41        # name of phase
42        self.name = name
43        # absolute time of phase (UTDCDateTime)
44        self.picktime = picktime
45        # type of phase
46        self.picktype = picktype
47        # picked on which component
48        self.comp = comp
49        # phase weight
50        self.weight = weight
51
52
53class PhaseList(object):
54    """
55    Store phases for all stations.
56    """
57
58    __metaclass__ = Singleton
59
60    def __init__( self ):
61        self.phasedict = {}
62        self.default_phase = 'P'
63        self.default_weight = 9
64   
65    def translatePicktype( self, picktypename ):
66        "Returns picktype ID or None."
67        lpickname = picktypename.lower()
68        for pt in typenames.keys():
69            if lpickname == typenames[pt]:
70                return pt
71        return None
72   
73    def picktypeName( self, picktype ):
74        try:
75            return typenames[picktype]
76        except:
77            return "unknown"
78       
79    def picktypeColor( self, picktype ):
80        try:
81            return typecolors[picktype]
82        except:
83            return 'gray'
84   
85    def phaseAlreadyPicked( self, station, name=None, picktype=None ):
86        "Returns True if phase of given type is already picked at station."
87        if picktype == PICKTYPE_MARK:
88            return False
89        upstat = station.upper()
90        if upstat not in self.phasedict.keys():
91            return False
92        if picktype == None:
93            picktype = DEFAULT_PICKTYPE
94        if name == None:
95            name = self.default_phase
96        for phase in self.phasedict[upstat]:
97            if phase.name == name and phase.picktype == picktype:
98                return True
99        return False
100   
101    def addPhasePick( self, station, picktime, name=None, picktype=None,
102        comp=None, weight=None ):
103        "Add phase pick to list."
104        upstat = station.upper()
105        if picktype == None:
106            picktype = DEFAULT_PICKTYPE
107        if name == None:
108            name = self.default_phase
109        if weight == None:
110            weight = self.default_weight
111        # comp can be None
112        if self.phaseAlreadyPicked( upstat, name, picktype ):
113            return False
114        newphase = Phase( name, picktime, picktype, comp, weight )
115        if upstat in self.phasedict.keys():
116            self.phasedict[upstat].append( newphase )
117        else:
118            self.phasedict[upstat] = [newphase]
119        return True
120   
121    def clearPhasename( self, phasename, picktype=None ):
122        for station in self.phasedict.keys():
123            self.clearPhasenameStation( phasename, station )
124   
125    def clearPhasenameStation( self, phasename, station, picktype=None ):
126        def keepElem(phase,phasename,picktype):
127            if picktype and picktype != phase.picktype:
128                return True
129            if phase.name == phasename:
130                return False
131            return True
132        if station not in self.phasedict.keys():
133            return
134        #for phase in self.phasedict[station]:
135        #    if picktype and picktype != phase.picktype:
136        #        continue
137        #    if phase.name == phasename:
138        #        self.phasedict[station].remove( phase )
139        self.phasedict[station][:] = [p for p in self.phasedict[station] \
140            if keepElem(p,phasename,picktype)] 
141        if len(self.phasedict[station]) == 0:
142            del( self.phasedict[station] )
143
144    def clearStation( self, station ):
145        if station not in self.phasedict.keys():
146            return
147        del( self.phasedict[station] )
148   
149    def clearPicktype( self, picktype ):
150        for station in self.phasedict.keys():
151            self.phasedict[station][:] = [p for p in self.phasedict[station] \
152                if (p.picktype != picktype)]
153            if len(self.phasedict[station]) == 0:
154                del( self.phasedict[station] )
155   
156    def clearAllPhases( self ):
157        self.phasedict = {}
158   
159    def getStations( self ):
160        "Returns all stations which have phases defined."
161        return self.phasedict.keys()
162   
163    def getPhaseList( self, station, picktype=None, comp=None, minweight=None,
164        weight=None ):
165        "Returns list of phases."
166        if station not in self.phasedict.keys():
167            return []
168        ret = []
169        for phase in self.phasedict[station]:
170            if picktype and picktype != phase.picktype:
171                continue
172            if comp and comp != phase.comp:
173                continue
174            if minweight and phase.weight < minweight:
175                continue
176            if weight and phase.weight != weight:
177                continue
178            # append copy of phase
179            ret.append( Phase(
180                name=phase.name,
181                picktime=phase.picktime,
182                picktype=phase.picktype,
183                comp=phase.comp,
184                weight=phase.weight,
185            ) )
186        return ret
187   
188    def setDefaultPhase( self, phase ):
189        self.default_phase = phase
190
191    def setDefaultWeight( self, weight ):
192        self.default_weight = weight
193
194
195if __name__ == "__main__":
196    phaselist = PhaseList()
Note: See TracBrowser for help on using the repository browser.