Skip to content

Commit dc13136

Browse files
committed
CSV converted improvements and small improvements
1 parent 804d2b9 commit dc13136

File tree

6 files changed

+200
-27
lines changed

6 files changed

+200
-27
lines changed

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,49 @@ The project provides also a Graphical user Interface that allows for easing the
88
For further information, please refer to the istSOS website: <http://istsos.org/>
99

1010
istSOS is released under the GPL License, and runs on all major platforms (Windows, Linux, Mac OS X), even though tests were conducted under a Linux environment.
11+
12+
13+
## Start istSOS with docker-compose
14+
15+
The faste way to use istSOS is with docker.
16+
17+
Create a docker-compose.yml file:
18+
19+
```bash
20+
version: '3.7'
21+
22+
services:
23+
istsos-db:
24+
image: postgis/postgis:12-2.5-alpine
25+
restart: always
26+
container_name: istsos-db
27+
environment:
28+
POSTGRES_USER: postgres
29+
POSTGRES_PASSWORD: postgres
30+
POSTGRES_DB: istsos
31+
TZ: Europe/Zurich
32+
volumes:
33+
- istsos-db-data:/var/lib/postgresql/data
34+
35+
istsos:
36+
image: istsos/istsos:2.4.1
37+
container_name: istsos
38+
restart: always
39+
ports:
40+
- 80:80
41+
42+
volumes:
43+
istsos-db-data:
44+
name: istsos-db-data
45+
46+
```
47+
48+
And start the containers by running the following command:
49+
50+
51+
```bash
52+
docker-compose up -d
53+
```
54+
55+
Open your browser and go to http://localhost/istsos/admin you should be able to see the web admin page.
56+
Before creating a new istsos service instance go to the "Database" page and set "istsos-db" as Host.

scripts/Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
FROM python:2.7.18-slim-buster
3+
COPY ./ /app/scripts
4+
RUN pip install -r /app/scripts/requirements.txt

scripts/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Create the virtualenv with docker
2+
3+
Build the images
4+
5+
```bash
6+
docker build --no-cache -t docker push docker.pkg.github.com/istsos/istsos2/istsos-scripts:2.4.1 .
7+
```
8+
9+
Publish
10+
11+
```bash
12+
docker push docker.pkg.github.com/istsos/istsos2/istsos-scripts:2.4.1
13+
```
14+
15+
Run the script
16+
```bash
17+
docker run --rm -it \
18+
--network="host" \
19+
-v $PWD/acquisition:/app/acquisition \
20+
-v /home/milan/workspace/istsos/istsos2/scripts:/app/scripts \
21+
-v /mnt/acq/:/mnt/acq/ \
22+
-w /app \
23+
istsos/istsos-scripts:2.4.1 \
24+
python ./acquisition/acquisition.py
25+
```

scripts/converter/csv.py

+122-27
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,61 @@
9898
)
9999
csv.execute()
100100
101+
102+
# File example: BR7_GW_____20210325000036.MIS
103+
# =====================================
104+
<STATION>BR7_GW____</STATION><SENSOR>0001</SENSOR><DATEFORMAT>YYYYMMDD</DATEFORMAT>
105+
20201024;181000;9.997
106+
20201024;182000;9.996
107+
20201024;183000;9.996
108+
<STATION>BR7_GW____</STATION><SENSOR>0002</SENSOR><DATEFORMAT>YYYYMMDD</DATEFORMAT>
109+
20201024;181000;8.68
110+
20201024;182000;8.70
111+
20201024;183000;8.68
112+
# =====================================
113+
114+
# Block interval enable / disable
115+
importer = csv.CsvImporter('T_TRE', {
116+
"enabled_from": {
117+
"include": "<SENSOR>0001</SENSOR>"
118+
},
119+
"disabled_from": {
120+
"include": "<SENSOR>",
121+
"exclude": "<SENSOR>0001</SENSOR>"
122+
},
123+
skiplines: [
124+
'NO DATA AVAILABLE'
125+
],
126+
"headrows": 0,
127+
"separator": ";",
128+
"filenamedate": {
129+
"format": '%Y%m%d%H%M%S',
130+
"remove": ['BR7_GW_____','.MIS']
131+
},
132+
"datetime": {
133+
"tz": '+01:00',
134+
"time": {
135+
"column": 1,
136+
"format": '%H%M%S'
137+
},
138+
"date": {
139+
"column": 2,
140+
"format": '%Y%m%d'
141+
}
142+
},
143+
"observations": [{
144+
"observedProperty": "urn:ogc:def:parameter:x-istsos:1.0:meteo:air:temperature",
145+
"column": 2
146+
}]
147+
},
148+
'http://localhost/istsos', 'pippo',
149+
"istsos/test/scripts/data/in/csv", 'BR7_GW_____20210325000036.MIS',
150+
"istsos/test/scripts/data/out",
151+
True
152+
)
153+
csv.execute()
154+
155+
101156
"""
102157

103158
from scripts import raw2csv
@@ -116,7 +171,19 @@ def __init__(self, procedureName, config, url, service, inputDir,
116171
csvlength=5000, filenamecheck=None, archivefolder = None, extra={}):
117172

118173
self.config = config
119-
174+
175+
self.max_cols = 0
176+
177+
if 'observations' in config:
178+
for obs in config['observations']:
179+
if 'column' in obs:
180+
self.max_cols = max(self.max_cols, obs['column'])
181+
182+
if 'enabled_from' in config:
183+
self.enabled = False
184+
else:
185+
self.enabled = None
186+
120187
raw2csv.Converter.__init__(self,
121188
procedureName, url, service, inputDir, fileNamePattern, outputDir,
122189
qualityIndex, exceptionBehaviour, user, password, debug, csvlength,
@@ -187,8 +254,6 @@ def setEndPositionFromFilename(self, fileName):
187254
188255
self.setEndPosition(dt)
189256
"""
190-
191-
192257

193258
def parse(self, fileObj, fileName):
194259
# print "Filename: %s" % fileName
@@ -197,37 +262,67 @@ def parse(self, fileObj, fileName):
197262
cnt = cnt+1
198263
# Skipping header rows if present in configuration
199264
if "headrows" in self.config and cnt <= self.config['headrows']:
265+
# print("skipping headrows (%s/%s)" % (cnt, self.config['headrows']))
200266
continue
201267

202268
elif "stopat" in self.config:
203269
if isinstance(self.config["stopat"], str) and (
204270
line.find(self.config["stopat"])>=0):
271+
# print("Breaking at line %s: %s" % (cnt, line))
205272
break
206273

207-
elif isinstance(self.config["stopat"], int):
208-
pass
209-
210-
else:
211-
pass
212-
213-
# Line splitting
214-
columns = line.split(self.config['separator'])
215-
try:
216-
date = self.parseDate(columns)
217-
values = {}
218-
for obs in self.config["observations"]:
219-
if obs["column"] is None:
220-
values[obs["observedProperty"]] = -999.9
221-
else:
222-
values[obs["observedProperty"]] = columns[obs["column"]]
223-
self.addObservation(
224-
raw2csv.Observation(date, values)
225-
)
226-
self.setEndPosition(date)
227-
except Exception as e:
228-
print("%s [%s]:%s" % (fileName,cnt,line))
229-
print(traceback.print_exc())
230-
raise e
274+
if "skiplines" in self.config:
275+
skipline = False
276+
for sl in self.config['skiplines']:
277+
if sl in line:
278+
skipline = True
279+
break
280+
281+
if skipline is True:
282+
# print("Skipping at line %s, found '%s': %s" % (cnt, sl, line))
283+
continue
284+
285+
if self.enabled is not None and self.enabled is not True:
286+
287+
if self.config['enabled_from']['include'] in line:
288+
# print("Enabled at line %s, found '%s': %s" % (cnt, self.config['enabled_from']['include'], line))
289+
self.enabled = True
290+
continue
291+
292+
elif self.enabled is not None and self.enabled is True:
293+
294+
if (
295+
self.config['disabled_from']['include'] in line
296+
and self.config['disabled_from']['exclude'] not in line
297+
):
298+
self.enabled = False
299+
continue
300+
301+
if self.enabled is None or self.enabled is True:
302+
303+
# Line splitting
304+
columns = line.split(self.config['separator'])
305+
306+
if self.max_cols > 0 and len(columns) < (self.max_cols+1):
307+
continue
308+
309+
try:
310+
date = self.parseDate(columns)
311+
values = {}
312+
for obs in self.config["observations"]:
313+
if obs["column"] is None:
314+
values[obs["observedProperty"]] = -999.9
315+
else:
316+
values[obs["observedProperty"]] = columns[obs["column"]]
317+
self.addObservation(
318+
raw2csv.Observation(date, values)
319+
)
320+
self.setEndPosition(date)
321+
322+
except Exception as e:
323+
print("%s [%s]:%s" % (fileName,cnt,line))
324+
print(traceback.print_exc())
325+
raise e
231326

232327
self.setEndPositionFromFilename(fileName)
233328

scripts/raw2csv.py

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import os
3333
from os import path
3434
import glob
35+
import fnmatch
3536
from datetime import datetime
3637
from datetime import timedelta
3738
import decimal
@@ -591,6 +592,7 @@ def prepareFiles(self):
591592
self.addException(msg)
592593
raise FileReaderError(msg)
593594

595+
print(" > Searching: %s" % os.path.join(self.folderIn, "%s" % (self.pattern)))
594596
files = list(filter(
595597
path.isfile,
596598
glob.glob(os.path.join(self.folderIn, "%s" % (self.pattern)))))

scripts/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pytz==2019.3
66
requests==2.23.0
77
six==1.14.0
88
urllib3[secure]==1.25.7
9+
html==1.16

0 commit comments

Comments
 (0)