Skip to content

Commit

Permalink
Some small improvements (#30)
Browse files Browse the repository at this point in the history
* Enhanced fast insert handling some common transmission errors

* Viewer modified to support for 2 observed properties on the same chart

* correcting bug when one of the obsprop are not present in a procedure

* changed significant figures (now 4) for water discharge vp

* Added utility script to apply retroactively the quality index

* improved register procedure script

* small correction

* add check on describeSensor request
  • Loading branch information
mantonovic authored Apr 17, 2018
1 parent 94a5b92 commit 0628698
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 10 deletions.
4 changes: 4 additions & 0 deletions istsoslib/filters/DS_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def __init__(self, sosRequest, method, requestObject, sosConfig):

if self.version == '2.0.0':
# OUTPUTFORMAT
if requestObject.has_key("procedure"):
if requestObject["procedure"] == '':
raise sosException.SOSException("MissingParameterValue", "procedure", "Missing 'procedure' parameter")

if requestObject.has_key("proceduredescriptionformat"):
if requestObject["proceduredescriptionformat"] == '':
raise sosException.SOSException("MissingParameterValue", "proceduredescriptionformat", "Missing 'proceduredescriptionformat' parameter")
Expand Down
247 changes: 247 additions & 0 deletions scripts/applyqi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
# -*- coding: utf-8 -*-
# =============================================================================
#
# Authors: Massimiliano Cannata, Milan Antonovic
#
# Copyright (c) 2016 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
#
# =============================================================================

import psycopg2
import sys
import json
from os import path

sys.path.insert(0, path.abspath("."))
try:
import lib.argparse as argparse
except ImportError as e:
print("""
Error loading internal libs:
>> did you run the script from the istSOS root folder?\n\n""")
raise e

conn = psycopg2.connect(
"dbname=istsos "
"user=postgres "
"password=postgres "
"host=localhost "
"port=9432"
)


def execute(args, conf=None):
"""
Example:
python scripts/applyqi.py
-s vedeggio
-p 598_33
-e 2018-02-09T00:00:00+02
-sqi 210
"""
cur = conn.cursor()

# Activate and print verbose information
# debug = args['v'] if 'v' in args else False

# dqi = args['dqi'] # default qi
cqi = args['cqi'] # correct qi
sqi = args['sqi'] # statistical qi

# Procedure name
procedure = args['procedure']
# Service name
service = args['service']

# Begin date
begin = args['begin'] if 'begin' in args else "*"
# End date
end = args['end'] if 'end' in args else "*"

try:
cur.execute("BEGIN;")
cur.execute("""
SELECT id_prc, stime_prc, etime_prc
FROM %s.procedures
WHERE
upper(name_prc) = upper(%s);
""" % (service, '%s'), (procedure,))
res = cur.fetchone()

if res is None:
raise Exception("Procedure %s not found in serivce %s" % (
procedure, service
))

print(res)
id_prc = res[0]
# b = res[1]
# e = res[2]
# print('b', b.isoformat(), 'e', e.isoformat())
# Load Procedure QI
cur.execute("""
SELECT
id_pro, constr_pro, constr_opr
name_opr, def_opr
FROM
%s.proc_obs,
%s.observed_properties
WHERE
id_opr = id_opr_fk
AND
id_prc_fk = %s;
""" % (service, service, '%s'), (id_prc,))
recs = cur.fetchall()

for rec in recs:
print(rec)
id_pro = rec[0]
constr_opr = rec[2] # level 0
constr_pro = rec[1] # level 1

conditions = []
if begin != '*':
conditions.append("""
time_eti >= '%s'::TIMESTAMP WITH TIME ZONE
""" % begin)
if end != '*':
conditions.append("""
time_eti <= '%s'::TIMESTAMP WITH TIME ZONE
""" % end)
conditions.append('id_prc_fk = %s' % id_prc)
conditions.append('id_pro_fk = %s' % id_pro)

for conf in [[constr_opr, cqi], [constr_pro, sqi]]:
constraint = conf[0]
qi = conf[1]
if constraint is not None and constraint != '':
constraint = json.loads(constraint)

if 'interval' in constraint:
conditions.append("""
(
val_msr >= %s
AND val_msr <= %s
)
""" % (
constraint['interval'][0],
constraint['interval'][1]
))
sql = """
SELECT id_msr
FROM
%s.measures,
%s.event_time
WHERE
id_eti = id_eti_fk
AND
%s
""" % (
service,
service,
" AND ".join(conditions)
)

# print("""
cur.execute("""
UPDATE %s.measures
SET id_qi_fk = %s
WHERE id_msr in (%s)
""" % (
service,
qi,
sql
))

cur.execute("COMMIT;")

except Exception as e:
print(str(e))
cur.execute("ROLLBACK;")


if __name__ == "__main__":

parser = argparse.ArgumentParser(
description='Apply the qi'
)

parser.add_argument(
'-v', '--verbose',
action='store_true',
dest='v',
help='Activate verbose debug'
)

parser.add_argument(
'-p', '--procedure',
action='store',
dest='procedure',
help='Procedure name'
)

parser.add_argument(
'-s', '--service',
action='store',
dest='service',
help='Service name'
)

parser.add_argument(
'-b', '--begin',
action='store',
dest='begin',
default='*',
metavar='1978-10-08T03:56:00+01:00',
help='Begin position date of the processing in ISO 8601.'
)

parser.add_argument(
'-e', '--end',
action='store',
dest='end',
default='*',
metavar='2014-01-27T11:27:00+01:00',
help='End position date of the processing in ISO 8601.'
)

parser.add_argument(
'-dqi',
action='store',
dest='dqi',
default=100,
help='Default QI'
)

parser.add_argument(
'-cqi',
action='store',
dest='cqi',
default=110,
help='Correct QI'
)

parser.add_argument(
'-sqi',
action='store',
dest='sqi',
default=200,
help='Correct QI'
)

args = parser.parse_args()
execute(args.__dict__)
2 changes: 1 addition & 1 deletion scripts/istsosutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def getProcedure(self, name):
jsonRes = res.json()
if not jsonRes['success']:
raise Exception("Error loading %s description: %s" % (
self.name, jsonRes['message']))
name, jsonRes['message']))
ret.description = jsonRes['data']

return ret
Expand Down
29 changes: 20 additions & 9 deletions scripts/registercsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,22 @@ def execute(args, logger=None):
proc.setDescription(line[1])
proc.setKeywords(line[2])
proc.setLongName(line[3])
proc.setModelNumber(line[4])
proc.setManufacturer(line[5])
proc.setSensorType(line[6])
if line[4] != '':
proc.setModelNumber(line[4])
if line[5] != '':
proc.setManufacturer(line[5])
if line[6] != '':
proc.setSensorType(line[6])
else:
proc.setSensorType('unknown')
coords = line[8].split(',')
proc.setFoi(line[9], line[7], coords[0], coords[1], coords[2])

foiName = line[9].strip()
foiName = foiName.replace('(', '')
foiName = foiName.replace(')', '')
foiName = foiName.replace(' ', '_')

proc.setFoi(foiName, line[7], coords[0], coords[1], coords[2])
if line[16] != '':
proc.setResolution(line[16])
if line[17] != '':
Expand All @@ -141,7 +152,7 @@ def execute(args, logger=None):

for idx in range(0, len(o1)):
proc.addObservedProperty(
o3[idx],
o3[idx] if o3[idx].find(':') == -1 else o3[idx].replace(':', '-'),
'urn:ogc:def:parameter:x-istsos:1.0:%s:%s%s' % (
o1[idx],
o2[idx],
Expand All @@ -157,7 +168,7 @@ def execute(args, logger=None):

for idx in range(0, len(o1)):
proc.addObservedProperty(
o3[idx],
o3[idx] if o3[idx].find(':') == -1 else o3[idx].replace(':', '-'),
'urn:ogc:def:parameter:x-istsos:1.0:%s:%s%s' % (
o1[idx],
o2[idx],
Expand All @@ -166,14 +177,14 @@ def execute(args, logger=None):
uom[idx]
)

# print proc.toJson()
service.registerProcedure(proc)

# print proc.toJson()
# Setting the begin position
if line[14]:
# Getting the Sensor id
aid = service.getProcedure(
proc.name).description['assignedSensorId']
proc.name
).description['assignedSensorId']

# Getting observation template
tmpl = service.getSOSProcedure(proc.name)
Expand Down

0 comments on commit 0628698

Please sign in to comment.