All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
copyPostGresToSQLite.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #---------------------------------------------------------------
3 #
4 # Name: copyPostGresToSQLite.py
5 #
6 # Purpose: Accessing the channel mapping database
7 #
8 # Created: 23-Oct-2020 T. Usher
9 #
10 #---------------------------------------------------------------
11 
12 from __future__ import print_function
13 import sys, os, datetime
14 import psycopg2
15 import sqlite3
16 from DataLoader3 import DataLoader, DataQuery
17 
18 # Set up the columns for the three databases
19 readoutBoardsColumns = ['readout_board_id integer',
20  'flange_id integer',
21  'chimney_number integer',
22  'tpc_id text',
23  'create_time text',
24  'create_user text',
25  'update_time text',
26  'update_user text',
27  'fragement_id text']
28 
29 daqChannelsColumns = ['channel_id integer',
30  'wire_number integer',
31  'readout_board_id integer',
32  'chimney_number integer',
33  'readout_board_slot integer',
34  'channel_number integer',
35  'create_time text',
36  'create_user text',
37  'update_time text',
38  'update_user text',
39  'plane text',
40  'cable_label_number text',
41  'channel_type text']
42 
43 flangesColumns = ['flange_id integer',
44  'tpc_id text',
45  'chimney_number integer',
46  'flange_pos_at_chimney text',
47  'create_time text',
48  'update_user text',
49  'update_time text',
50  'create_user text',
51  'power_supply_ip_address text',
52  'power_supply_id integer']
53 
54 pmtPlacementColumns = ['pmt_id integer',
55  'pmt_sn text',
56  'sector_label text',
57  'ch_number integer',
58  'pmt_position_code integer',
59  'hv_cable_label text',
60  'signal_cable_label text',
61  'light_fiber_label text',
62  'digitizer_label text',
63  'digitizer_ch_number integer',
64  'hv_supply_label text',
65  'hv_supply_ch_number integer',
66  'create_time text',
67  'update_user text',
68  'update_time text',
69  'create_user text',
70  'pmt_in_tpc_plane text',
71  'channel_id integer',
72  'fragment_id integer']
73 
74 crtFEBChannelsColumns = ['feb_id text',
75  'feb_channel integer',
76  'pedestal float',
77  'threshold_adjust integer',
78  'bias integer',
79  'hg integer',
80  'create_time text',
81  'update_user text',
82  'update_time text',
83  'create_user text',
84  'channel_id integer',
85  'feb_index integer',
86  'mac_address integer']
87 
88 topcrtFEBColumns = ['feb_barcode text',
89  'serialnum integer',
90  'mac_add8b text',
91  'mac_add integer',
92  'voltage float',
93  'ch0 integer',
94  'ch1 integer',
95  'ch2 integer',
96  'ch3 integer',
97  'ch4 integer',
98  'ch5 integer',
99  'ch6 integer',
100  'ch7 integer',
101  'ch8 integer',
102  'ch9 integer',
103  'ch10 integer',
104  'ch11 integer',
105  'ch12 integer',
106  'ch13 integer',
107  'ch14 integer',
108  'ch15 integer',
109  'ch16 integer',
110  'ch17 integer',
111  'ch18 integer',
112  'ch19 integer',
113  'ch20 integer',
114  'ch21 integer',
115  'ch22 integer',
116  'ch23 integer',
117  'ch24 integer',
118  'ch25 integer',
119  'ch26 integer',
120  'ch27 integer',
121  'ch28 integer',
122  'ch29 integer',
123  'ch30 integer',
124  'ch31 integer',
125  'create_time text',
126  'update_user text',
127  'update_time text',
128  'create_user text',
129  'feb_index integer']
130 
131 # Define the function to create and fill each table
132 def copyTable(postGres, dbCurs, dbName, table, columns):
133  createString = "CREATE TABLE " + table + " ("
134 
135  for columnIdx in range(len(columns)):
136  if columnIdx > 0:
137  createString += ", "
138  createString += columns[columnIdx]
139 
140  createString += ")"
141 
142  print(createString)
143 
144  dbCurs.execute(createString)
145 
146  query = postGres.query(database=dbName, table=table, columns="*")
147 
148  for rowIdx in range(len(query)):
149  rowList = query[rowIdx].split(',')
150  if len(rowList) != len(columns):
151  print("Length mismatch! Will skip this row (",rowIdx,")")
152  continue
153  insertString = "INSERT INTO " + table + " VALUES ("
154  for idx in range(len(rowList)):
155  if idx > 0:
156  insertString += ", "
157  if "text" in columns[idx]:
158  fieldEntry = rowList[idx]
159  if table == "daq_channels" and "plane" in columns[idx]:
160  fieldEntry = rowList[idx].upper()
161  insertString += "\'" + fieldEntry + "\'"
162  else:
163  if "none" in rowList[idx]:
164  #print("Found none in column data, field:",columns[idx],", value: ",rowList[idx])
165  insertString += '0'
166  else:
167  insertString += "\'" +rowList[idx]+ "\'"
168  insertString += ")"
169  if table == "daq_channels":
170  print("idx:",rowIdx," -->",insertString)
171  dbCurs.execute(insertString)
172 
173 # Ok get down to extracting the info and copying to SQLite
174 queryurl = "https://dbdata0vm.fnal.gov:9443/QE/hw/app/SQ/query"
175 
176 dataQuery = DataQuery(queryurl)
177 
178 print(dataQuery)
179 
180 dbName = "icarus_hardware_prd"
181 readoutBoardsTable = "readout_boards"
182 daqChannelsTable = "daq_channels"
183 flangesTable = "flanges"
184 pmtPlacementTable = "pmt_placements"
185 crtfebchannelsTable= "feb_channels"
186 topcrtfebTable = "crtfeb"
187 ##################################################################################
188 
189 # Start by creating the new database and setting up the first table
190 sqliteDB = sqlite3.connect("ChannelMapICARUS.db")
191 dbCurs = sqliteDB.cursor()
192 
193 ###################################################################################
194 
195 copyTable(dataQuery, dbCurs, dbName, readoutBoardsTable, readoutBoardsColumns)
196 
197 copyTable(dataQuery, dbCurs, dbName, daqChannelsTable, daqChannelsColumns)
198 
199 copyTable(dataQuery, dbCurs, dbName, flangesTable, flangesColumns)
200 
201 copyTable(dataQuery, dbCurs, dbName, pmtPlacementTable, pmtPlacementColumns)
202 
203 copyTable(dataQuery, dbCurs, dbName, crtfebchannelsTable, crtFEBChannelsColumns)
204 
205 copyTable(dataQuery, dbCurs, dbName, topcrtfebTable, topcrtFEBColumns)
206 
207 ###################################################################################
208 
209 sqliteDB.commit()
210 
211 sqliteDB.close()
212 
213 
214 
do one_file $F done echo for F in find $TOP name CMakeLists txt print