Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

# -*- coding: utf-8 -*- 

""" 

SQLAlchemy ORM definitions (database layout) for obspy.db. 

 

:copyright: 

    The ObsPy Development Team (devs@obspy.org) 

:license: 

    GNU Lesser General Public License, Version 3 

    (http://www.gnu.org/copyleft/lesser.html) 

""" 

 

from sqlalchemy import ForeignKey, Column, Integer, DateTime, Float, String, \ 

    PickleType, Boolean 

from sqlalchemy.ext.declarative import declarative_base 

from sqlalchemy.orm import relation 

from obspy import Trace, UTCDateTime 

import numpy as np 

from sqlalchemy.schema import UniqueConstraint 

import pickle 

 

 

Base = declarative_base() 

 

 

class WaveformPath(Base): 

    """ 

    DB table containing file directories. 

    """ 

    __tablename__ = 'default_waveform_paths' 

    __table_args__ = (UniqueConstraint('path'), {}) 

 

    id = Column(Integer, primary_key=True) 

    path = Column(String, nullable=False, index=True) 

    archived = Column(Boolean, default=False) 

 

    files = relation("WaveformFile", order_by="WaveformFile.id", 

                     backref="path", cascade="all, delete, delete-orphan") 

 

    def __init__(self, data={}): 

        self.path = data.get('path') 

 

    def __repr__(self): 

        return "<WaveformPath('%s')>" % self.path 

 

 

class WaveformFile(Base): 

    """ 

    DB table containing waveform files. 

    """ 

    __tablename__ = 'default_waveform_files' 

    __table_args__ = (UniqueConstraint('file', 'path_id'), {}) 

 

    id = Column(Integer, primary_key=True) 

    file = Column(String, nullable=False, index=True) 

    size = Column(Integer, nullable=False) 

    mtime = Column(Integer, nullable=False, index=True) 

    format = Column(String, nullable=False, index=True) 

    path_id = Column(Integer, ForeignKey('default_waveform_paths.id')) 

 

    channels = relation("WaveformChannel", order_by="WaveformChannel.id", 

                        backref="file", cascade="all, delete, delete-orphan") 

 

    def __init__(self, data={}): 

        self.file = data.get('file') 

        self.size = data.get('size') 

        self.mtime = int(data.get('mtime')) 

        self.format = data.get('format') 

 

    def __repr__(self): 

        return "<WaveformFile('%s')>" % (self.id) 

 

 

class WaveformChannel(Base): 

    """ 

    DB table containing channels. 

    """ 

    __tablename__ = 'default_waveform_channels' 

    __table_args__ = (UniqueConstraint('network', 'station', 'location', 

                                       'channel', 'file_id'), {}) 

 

    id = Column(Integer, primary_key=True) 

    file_id = Column(Integer, ForeignKey('default_waveform_files.id'), 

                     index=True) 

    network = Column(String(2), nullable=False, index=True) 

    station = Column(String(5), nullable=False, index=True) 

    location = Column(String(2), nullable=False, index=True) 

    channel = Column(String(3), nullable=False, index=True) 

    starttime = Column(DateTime, nullable=False, index=True) 

    endtime = Column(DateTime, nullable=False, index=True) 

    calib = Column(Float, nullable=False) 

    sampling_rate = Column(Float, nullable=False) 

    npts = Column(Integer, nullable=False) 

    preview = Column(PickleType(protocol=0), nullable=True) 

 

    gaps = relation("WaveformGaps", order_by="WaveformGaps.id", 

                    backref="channel", cascade="all, delete, delete-orphan") 

 

    features = relation("WaveformFeatures", order_by="WaveformFeatures.id", 

                        backref="channel", 

                        cascade="all, delete, delete-orphan") 

 

    def __init__(self, data={}): 

        self.update(data) 

 

    def update(self, data): 

        self.network = data.get('network', '') 

        self.station = data.get('station', '') 

        self.location = data.get('location', '') 

        self.channel = data.get('channel', '') 

        self.starttime = data.get('starttime') 

        self.endtime = data.get('endtime') 

        self.calib = data.get('calib', 1.0) 

        self.npts = data.get('npts', 0) 

        self.sampling_rate = data.get('sampling_rate', 1.0) 

        self.preview = data.get('preview', None) 

 

    def __repr__(self): 

        return "<WaveformChannel('%s')>" % (self.id) 

 

    def getPreview(self, apply_calibration=False): 

        try: 

            data = np.loads(self.preview) 

        except: 

            data = np.array([]) 

        if apply_calibration: 

            data = data * self.calib 

        tr = Trace(data=data) 

        tr.stats.starttime = UTCDateTime(self.starttime) 

        tr.stats.delta = 30.0 

        tr.stats.network = self.network 

        tr.stats.station = self.station 

        tr.stats.location = self.location 

        tr.stats.channel = self.channel 

        tr.stats.calib = self.calib 

        tr.stats.preview = True 

        return tr 

 

 

class WaveformGaps(Base): 

    """ 

    DB table containing gaps. 

    """ 

    __tablename__ = 'default_waveform_gaps' 

 

    id = Column(Integer, primary_key=True) 

    channel_id = Column(Integer, ForeignKey('default_waveform_channels.id'), 

                        index=True) 

    gap = Column(Boolean, nullable=False, index=True) 

    starttime = Column(DateTime, nullable=False, index=True) 

    endtime = Column(DateTime, nullable=False, index=True) 

    samples = Column(Integer, nullable=False) 

 

    def __init__(self, data={}): 

        self.gap = data.get('gap', True) 

        self.starttime = data.get('starttime') 

        self.endtime = data.get('endtime') 

        self.samples = data.get('samples', 0) 

 

    def __repr__(self): 

        return "<WaveformGaps('%s')>" % (self.id) 

 

 

class WaveformFeatures(Base): 

    """ 

    DB table containing optional features created during indexing. 

    """ 

    __tablename__ = 'default_waveform_features' 

    __table_args__ = (UniqueConstraint('channel_id', 'key'), {}) 

 

    id = Column(Integer, primary_key=True) 

    channel_id = Column(Integer, ForeignKey('default_waveform_channels.id'), 

                        index=True) 

    key = Column(String, nullable=False, index=True) 

    value = Column(PickleType, nullable=True) 

 

    def __init__(self, data={}): 

        self.key = data.get('key') 

        self.value = pickle.dumps(data.get('value', None)) 

 

    def __repr__(self): 

        return "<WaveformFeatures('%s')>" % (self.id)