-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
136 lines (111 loc) · 4.45 KB
/
generator.py
File metadata and controls
136 lines (111 loc) · 4.45 KB
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
# ==========================================================================
#
# Copyright (C) 2018 INAF - OAS Bologna
# Author: Leonardo Baroncelli <leonardo.baroncelli@inaf.it>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import sys
from time import sleep
from random import randint
import redis
from ast import literal_eval
import configparser
import csv
def sendMockData(config, redisConn, mockDetectionData, channel):
for event in reversed(mockDetectionData): # SORTING...
sleep(randint(config.getint('General', 'sleepmin'),config.getint('General', 'sleepmax')))
event['dataType'] = 'evt3'
sendEvent(redisConn, event, channel)
sendEvent(redisConn, None, channel, lastEvent = True)
def sendEvent(redisConn, event, channel, lastEvent = False):
if not lastEvent:
print("\nPublishing.. ",event," on DTR channel ",channel)
redisConn.publish(channel, event)
else:
redisConn.publish(channel, 'STOP')
def getMockData(mockDataPath, fileFormat='csv'):
print("Parsing data...")
if fileFormat == 'json':
mockdatafile = open(mockDataPath,'r')
mockData = mockdatafile.read()
return literal_eval(mockData);
elif fileFormat == 'csv':
data = []
with open(mockDataPath, newline='') as csvfile:
reader = list(csv.reader(csvfile, delimiter=','))
keys = reader[0]
print(keys)
print("Data length: {}".format(len(reader)))
for row in reader[2:]:
dict = {}
l = len(row)
for i in range(l):
dict[keys[i]] = row[i]
data.append(dict)
print("Data length: {}".format(len(data)))
return data
else:
print("Format not found.")
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Please enter: \n - the path to the configuration file \n - mock file path \n - the generator output channel")
exit()
a = [1, 3, 5]
b = a
a[:] = [x + 2 for x in a]
print(b)
configFilePath = sys.argv[1]
mockDataPath = sys.argv[2]
generatoroutputchannel = sys.argv[3]
config = configparser.ConfigParser()
config.read(configFilePath)
redisConn = redis.Redis(
host = config.get('Redis','host'),
port = config.getint('Redis','port'),
db = config.getint('Redis','dbname'),
password = config.get('Redis','password')
)
mockData = getMockData(mockDataPath, 'csv')
print("Example of data: ", mockData[0])
sendMockData(config, redisConn, mockData, generatoroutputchannel )
"""
Examples:
CSV CTA:
{'import_time': '1539884499.1339893',
'rootname': '/data01/ANALYSIS3.local/CTA-SOUTH/DREVT3_CTA_O1/ctools-lc_LC1000-shift10/T442803540_442804540/T442803540_442804540_E0.1_100_P184.557449_-5.78436',
'fluxul': 'NULL',
'detectionid': '40636',
'label': 'Crab-1',
'l': '184.557',
'b': '-5.7843',
'ella': '-1',
'ellb': '-1',
'fluxerr': '0.317889041855604',
'flux': '13.6674',
'sqrtts': '110.566',
'spectralindex': '2.43214',
'spectralindexerr': '0.0203501',
'tstart': '442803540',
'tstop': '442804540',
'emin': '0.1',
'emax': '100',
'run_l': '184.557449',
'run_b': '-5.78436',
'dataType': 'detection',
'instrument_id': '1',
'observation_id': '2',
'analysis_session_type_id': '3'}
"""