-
Notifications
You must be signed in to change notification settings - Fork 0
/
addcols.py
executable file
·260 lines (218 loc) · 7.8 KB
/
addcols.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
import datetime, logging
from sqlalchemy import create_engine, exc
import sqlalchemy.exc
from sqlalchemy.orm import sessionmaker
from flask import current_app
from app.models import OldPlant, OldSpecies
import unicodecsv
import sys
reload(sys) # Reload does the trick!
sys.setdefaultencoding('UTF8')
engine = create_engine('mysql://lifecycle:sitl3015@localhost/lifecycle', echo=False)
logging.basicConfig(level=logging.DEBUG, filename='dberrors.log')
# create a Session
Session = sessionmaker(bind=engine)
session = Session()
session._model_changes = {}
error = sqlalchemy.exc.ProgrammingError
logf = open("error.log", "w")
def degtodec(deg, mins, sec):
if deg == 'NA' or mins == 'NA' or sec == 'NA':
return
else:
dec = (float(deg) + (float(mins) * 1/60) + (float(sec) * 1/60 * 1/60))
return dec
def submitSpecies(species):
speciesname = species['speciesaccepted']
print speciesname
if session.query(OldSpecies).filter_by(name=speciesname).first() is None:
print "Entering new data", speciesname
n_species = OldSpecies()
n_species.name = species['speciesaccepted']
n_species.speciesauthor = species['speciesauthor']
n_species.authority = str(species['authority'])
n_species.taxonomy = species['taxonomy']
n_species.tplversion = species['tplversion']
n_species.intraspecificaccepted = species['intraspecificaccepted']
n_species.speciesepithetaccepted = species['speciesepithetaccepted']
n_species.genusaccepted = species['genusaccepted']
n_species.kingdom = species['kingdom']
n_species.phylum = species['phylum']
n_species.angiogymno = species['angiogymno']
n_species.dicotmonoc = species['dicotmonoc']
n_species._class = species['t_class']
n_species._order = species['order']
n_species.family = species['family']
n_species.genus = species['genus']
try:
session.add(n_species)
session.commit()
except:
logf.write("Failed to download {0}\n".format(n_species.name))
pass
else:
print "Data already exists"
def submitPlant(plant):
plantname = plant['speciesaccepted']
speciesname = session.query(OldSpecies).filter_by(name=plantname).first()
print plantname
if speciesname is None:
print "No Species"
pass
else:
mat = plant['matrix_a']
comp = plant['matrixcomposite']
cn = session.query(OldPlant).filter_by(matrixcomposite=comp, matrix_a=mat).first()
if cn is None :
newplant = [OldPlant()]
newplant[0].name = plant['speciesaccepted']
newplant[0].matrix_a = plant['matrix_a']
newplant[0].matrix_u = plant['matrix_u']
newplant[0].matrix_f = plant['matrix_f']
newplant[0].matrix_c = plant['matrix_c']
newplant[0].dimension = plant['matrixdimension']
newplant[0].matrixsplit = plant['matrixsplit']
newplant[0].classnames = str(plant['classnames'])
newplant[0].observation = str(plant['observation'])
newplant[0].matrixcomposite = plant['matrixcomposite']
newplant[0].matrixtreatment = str(plant['matrixtreatment'])
newplant[0].matrixcaptivity = plant['matrixcaptivity']
newplant[0].matrixstartyear = plant['matrixstartyear']
newplant[0].matrixstartseason = plant['matrixstartseason']
newplant[0].matrixstartmonth = plant['matrixstartmonth']
newplant[0].matrixendyear = plant['matrixendyear']
newplant[0].matrixendseason = plant['matrixendseason']
newplant[0].matrixendmonth = plant['matrixendmonth']
newplant[0].matrixfec = plant['matrixfec']
newplant[0].studiedsex = plant['studiedsex']
newplant[0].population = str(plant['matrixpopulation'])
newplant[0].latdeg = plant['latdeg']
newplant[0].latmin = plant['latmin']
newplant[0].latsec = plant['latsec']
newplant[0].latns = plant['latns']
newplant[0].londeg = plant['londeg']
newplant[0].lonmin = plant['lonmin']
newplant[0].lonsec = plant['lonsec']
newplant[0].lonwe = plant['lonwe']
newplant[0].latitudedec = degtodec(plant['latdeg'], plant['latmin'], plant['latsec'])
newplant[0].longitudedec = degtodec(plant['londeg'], plant['lonmin'], plant['lonsec'])
newplant[0].annualperiodicity = plant['annualperiodicity']
newplant[0].altitude = plant['altitude']
newplant[0].country = plant['country']
newplant[0].continent = plant['continent']
newplant[0].criteriasize = plant['criteriasize']
newplant[0].criteriaontogeny = plant['criteriaontogeny']
newplant[0].authors = str(plant['authors'])
newplant[0].journal = plant['journal']
newplant[0].yearpublication = plant['yearpublished']
newplant[0].doiisbn = plant['doiisbn']
newplant[0].additionalsource = str(plant['additionalsource'])
# newplant.enteredby = db.Column(db.String(64))
# newplant.entereddate = db.Column(db.String(64))
# newplant.source = db.Column(db.String(100))
# newplant.statusstudy = db.Column(db.String(64))
# newplant.statusstudyref = db.Column(db.String(64))
# newplant.statuselsewhere = db.Column(db.String(64))
# newplant.statuselsewhereref = db.Column(db.String(64))
print vars(newplant[0])
print "Adding", plant['speciesaccepted'], "to Session"
if len(speciesname.plants) is 0:
speciesname.plants.append(newplant[0])
else:
speciesname.plants.extend(newplant)
session.add(speciesname)
try:
"Committing to database"
session.commit()
session.flush()
except sqlalchemy.exc.ProgrammingError, exc:
session.rollback()
logging.exception("Fail: ")
pass
def importCSV():
import csv
def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs):
csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs)
for row in csv_reader:
yield [unicode(cell, 'utf-8') for cell in row]
print 'Unpacked CSV'
with open('compadreFlat3.csv', 'rU') as csvfile:
fileread = unicodecsv.reader(csvfile, delimiter=',', quotechar='"', encoding='utf-8')
allMatrices = []
allSpecies = []
allPlants = []
for i, row in enumerate(fileread):
if i > 0:
allSpecies.append ({
'speciesauthor' : row[0],
'speciesaccepted': row[1],
'authority' : row[2],
'taxonomy' : row[3],
'tplversion' : row[4],
'intraspecificaccepted' : row[5],
'speciesepithetaccepted' : row[6],
'genusaccepted' : row[7],
'genus' : row[8],
'family' : row[9],
'order' : row[10],
't_class' : row[11],
'dicotmonoc' : row[12],
'angiogymno' : row[13],
'phylum' : row[14],
'kingdom' : row[15],
'growthtype' : row[16]
})
allPlants.append({
'speciesaccepted' : row[1],
'authors' : row[17],
'journal' : row[18],
'yearpublished' : row[19],
'doiisbn' : row[20],
'additionalsource' : row[21],
'studyduration' : row[22],
'studystart' : row[23],
'studyend' : row[24],
'annualperiodicity' : row[25],
'numberpopulations' : row[26],
'criteriasize' : row[27],
'criteriaontogeny' : row[28],
'criteriaage' : row[29],
'matrixpopulation' : row[30],
'latdeg' : row[31],
'latmin' : row[32],
'latsec' : row[33],
'latns' : row[34],
'londeg' : row[35],
'lonmin' : row[36],
'lonsec' : row[37],
'lonwe' : row[38],
'altitude' : row[39],
'country' : row[40],
'continent' : row[41],
'ecoregion' : row[42],
'studiedsex' : row[43],
'matrixcomposite' : row[44],
'matrixtreatment' : row[45],
'matrixcaptivity' : row[46],
'matrixstartyear' : row[47],
'matrixstartseason' : row[48],
'matrixstartmonth' : row[49],
'matrixendyear' : row[50],
'matrixendseason' : row[51],
'matrixendmonth' : row[52],
'matrixsplit' : row[53],
'matrixfec' : row[54],
'observation' : row[55],
'matrixdimension' : row[56],
'survivalissue' : row[57],
'classnames' : row[58],
'matrix_a' : row[59],
'matrix_u' : row[60],
'matrix_f' : row[61],
'matrix_c' : row[62]
})
for species in allSpecies:
submitSpecies(species)
for plant in allPlants:
submitPlant(plant)
importCSV()