-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingest_permian_source_csv_to_sql.py
276 lines (223 loc) · 8.03 KB
/
ingest_permian_source_csv_to_sql.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import requests
import json
import argparse
import sys
import traceback
import openpyxl
import types
import os
import uuid
import psycopg2
import csv
DB_ENDPOINT = "localhost"
DB_PORT = 5432
DB_USER = ""
DB_PASSWD = ""
DB_NAME = ""
VISTA_NA_FILL_VALUE = "NA"
class ColumnHeaders:
SOURCE_ID = "source_id"
SOURCE_LATITUDE = "source_lat"
SOURCE_LONGITUDE = "source_lon"
SOURCE_TYPE = "source_type"
IPCC = "ipcc"
NUMBER_OVERFLIGHTS = "number_overflights"
SOURCE_PERSISTENCE = "source_persistence"
CONFIDENCE_IN_PERSISTENCE = "confidence_in_persistence"
Q_SOURCE = "qsource"
SIGMA_QSOURCE = "sigma_qsource"
# ColumnLettersSources = {
# ColumnHeaders.SOURCE_IDENTIFIER : "A",
# ColumnHeaders.SOURCE_LATITUDE : "B",
# ColumnHeaders.SOURCE_LONGITUDE : "C",
# ColumnHeaders.NUMBER_OVERFLIGHTS: "G",
# ColumnHeaders.SOURCE_PERSISTANCE : "H",
# ColumnHeaders.Q_SOURCE_KGHR: "J",
# ColumnHeaders.SIGMA_Q_SOURCE_KGHR: "K",
# ColumnHeaders.VISTA_ID: "D",
# ColumnHeaders.CONFIDENCE_IN_PERSISTENCE: "I"
# }
def process_row(headers, row):
datamap = {}
for i in range(0, len(headers)):
header = headers[i].strip()
value = row[i]
datamap[header] = value
return datamap
def parse_csv(input_file, verbose=False):
datamap = []
with open(input_file) as csvfile:
data = csv.reader(csvfile, delimiter=',', quotechar='\"')
row_num = 0
headers = []
for row in data:
if row_num == 0:
headers = row
else:
row_data = process_row(headers, row)
datamap.append(row_data)
row_num += 1
return datamap
def str_to_float(s):
if s == "TBD":
return -9999.0
elif s != "TBD" and s is not None and len(s) > 0:
return float(s)
else:
return None
# Sources:
def read_source_record(ws, row_num):
record_map = {}
if ws["A%d"%row_num].value == None:
return None
# First pass for values
for key in ColumnLettersSources:
cell_coord = "%s%d" % (ColumnLettersSources[key], row_num)
value = ws[cell_coord].value
record_map[key] = value
return record_map
def read_sources_sheet(ws):
sources = []
for n in range(2, 1000):
record = read_source_record(ws, n)
if record is None:
break
sources.append(record)
return sources
def __check_if_divbyzero(value):
if value == "#DIV/0!" or value == '':
return None
else:
return value
def update_source(source, cur, verbose=False):
sql = """
update sources set
source_latitude_deg = %s,
source_longitude_deg = %s,
total_overflights = %s,
Source_persistence = %s,
q_source_final = %s,
q_source_final_sigma = %s,
vista_id = %s,
source_location = ST_GeomFromText('POINT(%s %s)', 4326),
confidence_in_persistence = %s,
is_active = true
where
source_id = %s;
"""
cur.execute(sql,
(
source[ColumnHeaders.SOURCE_LATITUDE],
source[ColumnHeaders.SOURCE_LONGITUDE],
source[ColumnHeaders.NUMBER_OVERFLIGHTS],
source[ColumnHeaders.SOURCE_PERSISTENCE],
source[ColumnHeaders.Q_SOURCE],
source[ColumnHeaders.SIGMA_QSOURCE],
None, # source[ColumnHeaders.VISTA_ID] if source[ColumnHeaders.VISTA_ID] != VISTA_NA_FILL_VALUE else None,
float(source[ColumnHeaders.SOURCE_LONGITUDE]),
float(source[ColumnHeaders.SOURCE_LATITUDE]),
source[ColumnHeaders.CONFIDENCE_IN_PERSISTENCE],
source[ColumnHeaders.SOURCE_ID]
))
def insert_source(source, cur, verbose=False):
sql = """
INSERT INTO sources
(
source_id,
source_latitude_deg,
source_longitude_deg,
total_overflights,
Source_persistence,
q_source_final,
q_source_final_sigma,
vista_id,
source_location,
confidence_in_persistence,
is_active
) VALUES (
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
ST_GeomFromText('POINT(%s %s)', 4326),
%s,
true
)
"""
cur.execute(sql,
(
source[ColumnHeaders.SOURCE_ID],
source[ColumnHeaders.SOURCE_LATITUDE],
source[ColumnHeaders.SOURCE_LONGITUDE],
source[ColumnHeaders.NUMBER_OVERFLIGHTS],
source[ColumnHeaders.SOURCE_PERSISTENCE],
source[ColumnHeaders.Q_SOURCE],
source[ColumnHeaders.SIGMA_QSOURCE],
None, # source[ColumnHeaders.VISTA_ID] if source[ColumnHeaders.VISTA_ID] != VISTA_NA_FILL_VALUE else None,
float(source[ColumnHeaders.SOURCE_LONGITUDE]),
float(source[ColumnHeaders.SOURCE_LATITUDE]),
source[ColumnHeaders.CONFIDENCE_IN_PERSISTENCE]
))
def upload_source(source, cur, verbose=False, test_only=False):
if verbose:
print (source)
if does_source_exist(source[ColumnHeaders.SOURCE_ID], cur) is True:
if verbose:
print("Source '%s' already in database. Updating"%source[ColumnHeaders.SOURCE_ID])
update_source(source, cur, verbose)
else:
if verbose:
print("Source '%s' not yet in database. Inserting"%source[ColumnHeaders.SOURCE_ID])
insert_source(source, cur, verbose)
def does_source_exist(source_id, cur):
cur.execute("select count(source_id) as source_count from sources where source_id=%s",
(source_id,))
row = cur.fetchone()
return row[0] >= 1
def set_all_in_db_inactive(cur):
sql = "update sources set is_active=false;"
cur.execute(sql)
def upload_sources(sources, verbose=False, test_only=False):
conn = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASSWD, host=DB_ENDPOINT, port=DB_PORT)
cur = conn.cursor()
set_all_in_db_inactive(cur)
for source in sources:
upload_source(source, cur, verbose, test_only)
if test_only:
conn.rollback()
else:
conn.commit()
cur.close()
conn.close()
# def process_workbook(input_file, worksheet_name, test_only=False, verbose=False):
# print "Processing", input_file
# wb2 = openpyxl.load_workbook(input_file)
# ws_sources = wb2[worksheet_name]
# sources = read_sources_sheet(ws_sources)
# upload_sources(sources, test_only=test_only, verbose=verbose)
def process_csv(input_file, verbose=False, test_only=False):
data = parse_csv(input_file, verbose)
upload_sources(data, verbose, test_only)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--data", help="Input Sources CSV (csv)", required=True, type=str, nargs='+')
parser.add_argument("-e", "--endpoint", help="Target PostgreSQL endpoint", type=str, default="localhost", required=False)
parser.add_argument("-p", "--port", help="Target PostgreSQL port", type=str, default="8983", required=False)
#parser.add_argument("-s", "--sheet", help="Sources worksheet name", type=str, default="Sheet1", required=False)
parser.add_argument("-v", "--verbose",
help="Extra output",
required=False, action="store_true")
parser.add_argument("-t", "--test",
help="Test. Don't actually upload to PostgreSQL.",
required=False, action="store_true")
args = parser.parse_args()
input_files = args.data
verbose = args.verbose
test_only = args.test
# worksheet_name = args.sheet
for input_file in input_files:
process_csv(input_file, verbose=verbose, test_only=test_only)