-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertFunctions.py
More file actions
164 lines (147 loc) · 5.28 KB
/
insertFunctions.py
File metadata and controls
164 lines (147 loc) · 5.28 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
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
import os, os.path
import sys
import xarray as xr
sys.path.append('../login')
sys.path.append('../../config')
import pandas as pd
# import config_vault as cfgv
import credentials as cr
sys.path.append('../')
import dbCore as dc
def toSQLbcp(export_path, tableName, server):
if server == 'Rainier':
usr=cr.usr_rainier
psw=cr.psw_rainier
ip=cr.ip_rainier
port = cr.port_rainier
else:
usr=cr.usr_beast
psw=cr.psw_beast
ip=cr.ip_beast
port = cr.port_beast
print('Inserting Bulk %s into %s.' % (tableName[3:], tableName))
str = """bcp Opedia.dbo.""" + tableName + """ in """ + export_path + """ -e error -c -t, -U """ + usr + """ -P """ + psw + """ -S """ + ip + """,""" + port
os.system(str)
print('BCP insert finished')
def lineInsert(server, tableName, columnList ,query):
conn = dc.dbConnect(server)
cursor = conn.cursor()
insertQuery = """INSERT INTO %s %s VALUES %s """ % (tableName, columnList, query)
print(insertQuery)
cursor.execute(insertQuery)
conn.commit()
def netcdf_2_dataframe(netcdf_file,usecols = None):
xdf = xr.open_dataset(netcdf_file)
if usecols != None:
xdf = xdf[usecols]
df = xdf.to_dataframe()
df.reset_index(inplace=True)
return df, xdf
def findID_CRUISE(cruiseName):
""" this function pulls the ID value from the [tblCruises]"""
server = 'Rainier'
conn = dc.dbConnect(server)
cursor = conn.cursor()
cur_str = """select [ID] FROM [Opedia].[dbo].[tblCruise] WHERE [Name] like '%""" + cruiseName + """%'"""
cursor.execute(cur_str)
IDvar = (cursor.fetchone()[0])
return IDvar
def cruise_ID_list():
server = 'Rainier'
conn = dc.dbConnect(server)
cursor = conn.cursor()
cur_str = """select [ID], [Nickname] FROM [Opedia].[dbo].[tblCruise]"""
cursor.execute(cur_str)
IDlist = (cursor.fetchall())
newID_list = [x[0] for x in IDlist]
newNickname_list = [x[1] for x in IDlist]
return {'ID_list':newID_list,'Nickname_list':newNickname_list}
def findMinMaxDate_cruiseID(ID):
server = 'Rainier'
conn = dc.dbConnect(server)
cursor = conn.cursor()
cur_str_min = """select min(time) FROM [Opedia].[dbo].[tblCruise_Trajectory] where Cruise_ID = '""" + str(ID) + """'"""
cur_str_max = """select max(time) FROM [Opedia].[dbo].[tblCruise_Trajectory] where Cruise_ID = '""" + str(ID) + """'"""
cursor.execute(cur_str_min)
minDate = (cursor.fetchone()[0])
cursor.execute(cur_str_max)
maxDate = (cursor.fetchone()[0])
return {'minDate':minDate,'maxDate':maxDate}
def findMinMaxSpatial_cruiseID(ID):
server = 'Rainier'
conn = dc.dbConnect(server)
cursor = conn.cursor()
cur_str_minlat = """select min(lat) FROM [Opedia].[dbo].[tblCruise_Trajectory] where Cruise_ID = '""" + str(ID) + """'"""
cur_str_maxlat = """select max(lat) FROM [Opedia].[dbo].[tblCruise_Trajectory] where Cruise_ID = '""" + str(ID) + """'"""
cur_str_minlon = """select min(lon) FROM [Opedia].[dbo].[tblCruise_Trajectory] where Cruise_ID = '""" + str(ID) + """'"""
cur_str_maxlon = """select max(lon) FROM [Opedia].[dbo].[tblCruise_Trajectory] where Cruise_ID = '""" + str(ID) + """'"""
cursor.execute(cur_str_minlat)
minlat = (cursor.fetchone()[0])
cursor.execute(cur_str_maxlat)
maxlat = (cursor.fetchone()[0])
cursor.execute(cur_str_minlon)
minlon = (cursor.fetchone()[0])
cursor.execute(cur_str_maxlon)
maxlon = (cursor.fetchone()[0])
return {'minlat':minlat,'maxlat':maxlat,'minlon':minlon,'maxlon':maxlon}
def findMinMaxDate(tableName):
cur_str = 'select min(time), max(time) FROM [Opedia].[dbo].[' + tableName + ']'
df = dc.dbRead(cur_str)
dates = df.iloc[0].values
minDate = pd.to_datetime(str(dates[0])).strftime('%Y-%m-%d')
maxDate = pd.to_datetime(str(dates[1])).strftime('%Y-%m-%d')
return {'minDate':minDate,
'maxDate':maxDate}
def findSpatialBounds(tableName):
cur_str = 'select min(lat), max(lat), min(lon), max(lon) FROM [Opedia].[dbo].[' + tableName + ']'
df = dc.dbRead(cur_str)
dates = df.iloc[0].values
return {'minLat':dates[0],
'maxLat':dates[1], 'minLon':dates[2], 'maxLon':dates[3]}
# HTML beautifying funciton grabbed from: https://stackoverflow.com/questions/47704441/applying-styling-to-pandas-dataframe-saved-to-html-file
def write_to_html_file(df, title, filename,header=True, title_opt=False):
'''
Write an entire dataframe to an HTML file with nice formatting.
'''
result = '''
<html>
<head>
<style>
h2 {
text-align: center;
font-family: Helvetica, Arial, sans-serif;
}
table {
margin-left: auto;
margin-right: auto;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: center;
font-family: Helvetica, Arial, sans-serif;
font-size: 90%;
}
table tbody tr:hover {
background-color: #dddddd;
}
.wide {
width: 90%;
}
</style>
</head>
<body>
'''
if title_opt == True:
result += '<h2> %s </h2>\n' % title
else:
result += df.to_html(classes='wide', escape=False, index=False,header=header)
result += '''
</body>
</html>
'''
with open(filename, 'w') as f:
f.write(result)