-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathconstrain2db.py
executable file
·204 lines (170 loc) · 7.44 KB
/
constrain2db.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# -*- coding: utf-8 -*-
# ===============================================================================
#
# Authors: Massimiliano Cannata, Milan Antonovic
#
# Copyright (c) 2015 IST-SUPSI (www.supsi.ch/ist)
#
# This program 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 2 of the License, or (at your option)
# any later version.
#
# This program 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# ===============================================================================
"""
description:
usage example:
python scripts/constrain2db.py
-f /home/maxi/Desktop/const_csv.txt
-s http://localhost/istsos/wa/istsos/services/demo
-r urn:ogc:def:parameter:x-istsos:1.0:qualityIndex:check:lev1
-v
"""
import sys
import os
from os import path
import traceback
import json
import pprint
import glob
sys.path.insert(0, path.abspath("."))
try:
import argparse as argparse
import requests as requests
except ImportError as e:
print("\nError loading internal libs:\n >> did you run the script from the istSOS root folder?\n\n")
raise e
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def execute(args):
pp = pprint.PrettyPrinter(indent=2)
try:
# verbose
verbose = args['v']
# veryverbose
veryverbose = args['vv']
if veryverbose:
verbose = True
# istSOS service
service = args['s']
# const constraint role
role = "urn:ogc:def:classifiers:x-istsos:1.0:qualityIndex:check:reasonable"
# filename
csvfile = args['f']
req = requests.session()
# Open CSV file
fo = open(csvfile, "rw+")
#check file validity
rlines = [ row.strip().split(",") for row in fo.readlines() if row.strip() is not ""]
lines = []
for line in rlines:
lines.append([c.strip() for c in line ])
# load sensor description
res = req.get("%s/procedures/operations/getlist" % (service),
prefetch=True, verify=False)
jj = json.loads(res.content)
if veryverbose:
print("RETRIVING PRECEDURES...")
pp.pprint(res.json)
print("---------------------")
elif verbose:
if jj['success'] is False:
pp.pprint(res.json)
print("---------------------")
procedures = dict( ( i["name"], [ j["name"] for j in i["observedproperties"] ] ) for i in jj["data"] )
for nr,line in enumerate(lines):
line = [ l.strip() for l in line ]
if len(line)==4:
if not line[0] in list(procedures.keys()):
raise Exception("[line %s]: procedure '%s' not observed by the istsos service!" %(nr,line[0]) )
if not "-".join(line[1].split(":")[-2:]) in procedures[line[0]]:
raise Exception("[line %s]: procedure '%s' does not observe property '%s'!" %(nr,line[0],line[1]) )
if not (is_number(line[2]) or line[2] is ""):
raise Exception("[line %s]: value '%s' at column 3 should represent min values if present, check it is a number!" %(nr,line[2]) )
if not (is_number(line[3]) or line[3] is ""):
raise Exception("[line %s]: value '%s' at column 4 should represent min values if present, check it is a number!" %(nr,line[3]) )
else:
raise Exception("[line %s]: %s input file must contain 4 row: station name, observed property URI, min, max" %(nr,line))
for nr,line in enumerate(lines):
if line:
# load sensor description
res = req.get("%s/procedures/%s" % (service,line[0]),
prefetch=True, verify=False)
ds = json.loads(res.content)
if veryverbose:
print("RETRIVING PRECEDURES...")
pp.pprint(res.json)
print("---------------------")
elif verbose:
if ds['success'] is False:
pp.pprint(res.json)
print("---------------------")
#update constraints in Json
for opr in ds["data"]["outputs"]:
if opr["definition"] == line[1]:
opr["constraint"] = {}
opr["constraint"]["role"]=role
if line[2] and line[3]:
opr["constraint"]["interval"]=[float(line[2]),float(line[3])]
elif not line[2] and line[3]:
opr["constraint"]["max"]=float(line[3])
elif line[2] and not line[3]:
opr["constraint"]["min"]=float(line[2])
# send Json request to update constrain on service
res = req.put("%s/procedures/%s" % (service,line[0]),
prefetch=True,
verify=False,
data=json.dumps(ds["data"])
)
# read response
jj = json.loads(res.content)
if veryverbose:
print("SAVING PRECEDURE %s..." % line[0])
pp.pprint(json.dumps(ds["data"]))
print("---------------------")
print("---------------------")
print(" > Updated %s procedure success: %s" %(line[0],res.json['success']))
if verbose:
if jj['success'] is False:
pp.pprint(res.json)
print("---------------------")
except Exception as e:
print("ERROR: %s\n\n" % e)
traceback.print_exc()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Update PROCEDUREs constraints from CSV file with line format: PROCEDURE_NAME,OBSERVED_PROPERTY_URN,MIN,MAX,ROLE')
parser.add_argument('-v','--verbose',
action = 'store_true',
dest = 'v',
help = 'Activate verbose debug')
parser.add_argument('-vv','--veryverbose',
action = 'store_true',
dest = 'vv',
help = 'Activate very verbose debug')
parser.add_argument('-f','--file',
action = 'store',
required=True,
dest = 'f',
help = 'CSV file path')
parser.add_argument('-s', '--istsos',
action='store',
required=True,
dest='s',
help='istSOS WA service address (e.g.: http://localhost/istsos/wa/istsos/services/demo).')
args = parser.parse_args()
#print args.__dict__
execute(args.__dict__)