/
orm_timeseries.py
175 lines (155 loc) · 8.2 KB
/
orm_timeseries.py
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
"""
Declaration via ORM mapping of KINM data, including FEWS time series
compatibla datamodel, requires:
Pyhton packages
- sqlalchemy
- geoalchemy2
PostgreSQL/PostGIS
- schema timeseries
"""
# Copyright notice
# --------------------------------------------------------------------
# Copyright (C) 2022 Deltares for Projects with a FEWS datamodel in
# PostgreSQL/PostGIS database used in Water Information Systems
# Gerrit Hendriksen@deltares.nl
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------
#
# This tool is part of <a href="http://www.OpenEarth.eu">OpenEarthTools</a>.
# OpenEarthTools is an online collaboration to share and manage data and
# programming tools in an open source, version controlled environment.
# Sign up to recieve regular updates of this function, and to contribute
# your own tools.
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Sequence, ForeignKey, Column, UniqueConstraint,PrimaryKeyConstraint
from sqlalchemy import Integer, Float, DateTime, String, Text
from geoalchemy2 import Geometry
from sqlalchemy.dialects import postgresql
Base = declarative_base()
"""
FEWS data model has been partially adopted. Due to completion of the datamodel with borehole datamodel the timeseries datamodel is split in
several parts. A common part of the datamodel (location, filesource, parameter and unit tables) are now considered as shared tables over various schemas
"""
class TimeStep(Base):
__tablename__ = 'timesteps'
__table_args__ = {'schema': 'timeseries'}
timestepkey = Column(Integer,Sequence('timeseries.timesteps_timestepkey_seq'),primary_key=True)
id = Column(String, unique=True,nullable=False)
label = Column(String)
class TimeSeries(Base):
__tablename__ = 'timeseries'
__table_args__ = {'schema': 'timeseries'}
timeserieskey = Column(Integer, Sequence('timeseries.timeserieskeys_timeserieskey_seq'),primary_key=True)
locationkey = Column(Integer, ForeignKey('timeseries.location.locationkey', ondelete='CASCADE'))
parameterkey = Column(Integer, ForeignKey('timeseries.parameter.parameterkey', ondelete='CASCADE'))
timestepkey = Column(Integer, ForeignKey('timeseries.timesteps.timestepkey', ondelete='CASCADE'))
filesourcekey = Column(Integer, ForeignKey('timeseries.filesource.filesourcekey', ondelete='CASCADE'))
valuetype = Column(Integer, nullable=False,default=0)
modificationtime = Column(DateTime, nullable=False)
class TimeSeriesComments(Base):
__tablename__ = 'timeseriescomments'
__table_args__ = {'schema': 'timeseries'}
timeserieskey = Column(Integer,ForeignKey('timeseries.timeseries.timeserieskey', ondelete='CASCADE'),primary_key=True)
datetime = Column(DateTime, primary_key = True)
commenttext = Column(String)
class Users(Base):
__tablename__ = 'users'
__table_args__ = {'schema': 'timeseries'}
userkey = Column(Integer,Sequence('timeseries.users_userkey_seq'),primary_key=True)
id = Column(String, unique=True,nullable=False)
name = Column(String)
class Flags(Base):
__tablename__ = 'flags'
__table_args__ = {'schema': 'timeseries'}
flagkey = Column(Integer,Sequence('timeseries.users_flagkey_seq'),primary_key=True)
id = Column(String, unique=True,nullable=False)
name = Column(String)
class TimeSeriesManualEditsHistory(Base):
__tablename__ = 'timeseriesmanualeditshistory'
__table_args__ = {'schema': 'timeseries'}
timeserieskey = Column(Integer,ForeignKey('timeseries.timeseries.timeserieskey', ondelete='CASCADE'),primary_key=True)
editdatetime = Column(DateTime, primary_key = True)
datetime = Column(DateTime, primary_key = True)
userkey = Column(Integer,ForeignKey('timeseries.users.userkey', ondelete='CASCADE'))
scalarvalue = Column(Float)
flags = Column(Integer,nullable=False)
commenttext = Column(String)
class TimeSeriesValuesAndFlags(Base):
__tablename__ = 'timeseriesvaluesandflags'
__table_args__ = (PrimaryKeyConstraint('timeserieskey', 'datetime','scalarvalue'),{'schema': 'timeseries'})
timeserieskey = Column(Integer, ForeignKey('timeseries.timeseries.timeserieskey', ondelete='CASCADE'), nullable = False)
datetime = Column(DateTime, nullable=False)
scalarvalue = Column(Float,nullable=False)
flags = Column(Integer,ForeignKey('timeseries.flags.flagkey', ondelete='CASCADE'), nullable=False)
"""records pump history"""
class Parameter(Base):
__tablename__ = 'parameter'
__table_args__ = {'schema': 'timeseries'}
parameterkey = Column(Integer,Sequence('timeseries.parameter_parameterkey_seq'),primary_key=True)
id = Column(String)
name = Column(String)
unitkey = Column(Integer, ForeignKey('timeseries.unit.unitkey', ondelete='CASCADE'))
compartment = Column(String)
shortname = Column(String)
description = Column(String)
valueresolution = Column(Float)
waarnemingssoort = Column(String)
class Unit(Base):
__tablename__ = 'unit'
__table_args__ = {'schema': 'timeseries'}
unitkey = Column(Integer,Sequence('timeseries.unitdesc_seq'),primary_key=True)
unit = Column(String,nullable=False)
unitdescription = Column(String)
class FileSource(Base):
__tablename__ ='filesource'
__table_args__ = {'schema': 'timeseries'}
filesourcekey = Column(Integer,Sequence('timeseries.filesource_seq'),primary_key=True)
deviceid = Column(String)
filesource = Column(String,nullable=False)
remark = Column(String)
lasttransactionid = Column(Integer)
def __repr__(self):
return "<FileSource: filesourcekey=%s, lasttransactionid=%s) >" % (self.filesourcekey, self.lasttransactionid)
class Transaction(Base):
__tablename__ ='transaction'
__table_args__ = {'schema': 'timeseries'}
transactionkey = Column(Integer,Sequence('timeseries.transaction_seq'),primary_key=True)
timeserieskey = Column(Integer, ForeignKey('timeseries.timeseries.timeserieskey', ondelete='CASCADE'), nullable = False)
transactiontime = Column(DateTime, nullable=False)
periodstart = Column(DateTime, nullable=False)
periodend = Column(DateTime, nullable=False)
transactionid = Column(Integer,nullable=False)
class Location(Base):
__tablename__ = 'location'
__table_args__ = {'schema': 'timeseries'}
locationkey = Column(Integer,Sequence('timeseries.location_locationkey_seq1'),primary_key=True) #key
filesourcekey = Column(Integer,ForeignKey('timeseries.filesource.filesourcekey', ondelete='CASCADE')) #fskey
diverid = Column(String)
filterid = Column(Integer)
filterdepth = Column(Float)
name = Column(String)
shortname = Column(String)
description = Column(String)
x = Column(Float)
y = Column(Float)
z = Column(Float)
epsgcode = Column(Integer)
geom = Column(Geometry('POINT', srid=28992))
altitude_msl = Column(Float) #hoogte maaiveld
tubetop = Column(Float)
tubebot = Column(Float)
cablelength = Column(Float) #kabellengte
def __repr__(self):
return "<Location: locationkey=%s)>" % (self.locationkey)