forked from TheAcademyofNaturalSciences/WikiSRATMicroService
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseAdapter.py
More file actions
95 lines (83 loc) · 3.86 KB
/
Copy pathDatabaseAdapter.py
File metadata and controls
95 lines (83 loc) · 3.86 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
import psycopg2
from constants import huc12_column_numbers
from constants import huc12_returned_column_numbers
from constants import comid_column_numbers
from constants import comid_returned_column_numbers
class DatabaseAdapter:
def __init__(self, db_in, user_in, host_in, port_in, password_in, flag_in):
try:
self.conn = psycopg2.connect(database=db_in, user=user_in, host=host_in, port=port_in, password=password_in)
except psycopg2.OperationalError as e:
raise AttributeError("unable to connect to datbase")
try:
if flag_in is None:
self.flag_in = 'base'
else:
self.flag_in = flag_in
except Exception as e:
raise print('No flag for base vs BMPs added in given, defaulting to base routine.')
@classmethod
def python_to_array(self, python_object):
result = []
for _ in range(0, len(huc12_column_numbers)): # create an array of arrays without numpy
result.append([])
for huc12 in python_object:
for attribute, value in huc12.items():
result[huc12_column_numbers[attribute]].append(value)
return result
@classmethod
def huc12_array_to_python(self, array):
result = []
for _ in range(0, len(array)):
result.append({}) # prep the right number of empty huc12 objects
for i, huc12 in enumerate(array):
for j, attribute in enumerate(huc12):
result[i][huc12_returned_column_numbers[j]] = attribute
return result
@classmethod
def comid_array_to_python(self, array):
result = []
for _ in range(0, len(array)):
result.append({}) # prep the right number of empty huc12 objects
for i, comid in enumerate(array):
for j, attribute in enumerate(comid):
result[i][comid_returned_column_numbers[j]] = attribute
return result
@classmethod
def huc12_array_to_dictionary(self, huc12_array):
return {str(x["huc12"]): x for x in huc12_array}
@classmethod
def comid_array_to_dictionary(self, comid_array):
return {str(x["comid"]): x for x in comid_array}
@classmethod
def join_huc12s__and_comids_(self, huc12s, comids):
comids_dictionary = self.comid_array_to_dictionary(comids)
for huc12 in huc12s:
comid_array = []
if("catchments" in huc12 and huc12["catchments"] != None):#if this huc12 has catchments, join the data from the comid array
for catchment in huc12["catchments"]:
if(catchment in comids_dictionary):
comid_array.append(comids_dictionary[catchment])#append to array too create array of comid data
else:
raise KeyError('comid not in table')
huc12["catchments"] = self.comid_array_to_dictionary(comid_array)
else:
huc12["catchments"] = {}#otherwise catchments is just an empty dictionary
return huc12s
def srat_nhd(self, input_array):
cur = self.conn.cursor()
if self.flag_in == 'base':
cur.callproc('wikiwtershed.srat_nhd', input_array)
elif self.flag_in == 'restoration':
cur.callproc('wikiwtershed.srat_nhd_restoration', input_array)
return self.comid_array_to_python(cur.fetchall())
def srat_huc12(self, input_array):
cur = self.conn.cursor()
cur.callproc('wikiwtershed.srat_huc12', input_array)
return self.huc12_array_to_python(cur.fetchall())
def run_model(self, input_array):
comids = self.srat_nhd(input_array)
huc12s = self.srat_huc12(input_array)
combined = self.join_huc12s__and_comids_(huc12s, comids)
huc12s_dictionary = self.huc12_array_to_dictionary(combined)
return {"huc12s": huc12s_dictionary}