-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_data2.py
executable file
·144 lines (112 loc) · 4.6 KB
/
compile_data2.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
#!/usr/bin/python
import os
import sys
import math
import login
the_db = login.get_db()
import data_objects as DO
import compile_data as CD1
import queries
import write_table as WT
CONTROLLED_TRIALS = [4537, 5506]
FIELD_TRIALS = [5544, 5541, 5546, 5540, 5542, 5543, 5539, 5545]
DETHLINGEN_TRIALS = [5519]
def write_table_f(data, out=sys.stdout):
by_loc = {}
for k, v in data.items():
by_loc[k[0]] = by_loc.get(k[0], []) + [k[1:] + (v,)]
# print 5541, sorted(by_loc[5541])
# print 5542, sorted(by_loc[5542])
# return None
for k, rows in sorted(by_loc.items()):
for row in rows:
out.write('%i,%s,%i,%.5f\n' % ((k,) + row))
pass
def get_climate_data():
the_db.query(queries.climate_query)
climate_data = the_db.store_result().fetch_row(how=1, maxrows=0)
climate_data = [DO.ClimateData(d.keys(), d.values())
for d in climate_data]
heat_d, h2o_d = CD1.compute_climate_data(climate_data)
return heat_d, h2o_d
def get_irrigation_data():
the_db.query(queries.irrigation_query)
irri_data = the_db.store_result().fetch_row(how=1, maxrows=0)
irri_data = [DO.DataObject(d.keys(), d.values()) for d in irri_data]
irrigation_d = {}
for dobj in irri_data:
key = (dobj.limsid, dobj.treatment_id)
irrigation_d[key] = irrigation_d.get(key, 0) + dobj.irrigation_amount
return irrigation_d
def annotate_compiled_data(compiled, heat_d, h2o_d, irrigation_d):
for k, v in sorted(compiled.items()):
if isinstance(k, str): continue
try:
compiled[k].heat_sum, compiled[k].heat_nmeasures = heat_d[k[0]]
except:
compiled[k].heat_sum, compiled[k].heat_nmeasures = 0.0, -1
try:
compiled[k].precipitation, compiled[k].prec_nmeasures = h2o_d[k[0]]
except:
compiled[k].precipitation, compiled[k].prec_nmeasures = 0.0, -1
compiled[k].limsloc = k[0]
irr_key = (compiled[k].limsloc, compiled[k].treatment)
compiled[k].precipitation += irrigation_d.get(irr_key, 0)
return compiled
###
def main(argv):
heat_d, h2o_d = get_climate_data()
irrigation_d = get_irrigation_data()
the_db.query(queries.starch_query)
data = the_db.store_result().fetch_row(how=1, maxrows=0)
data = [DO.StarchData(d.keys(), d.values()) for d in data]
golm_data = CD1.group_by([d for d in data if d.location_id == 4537],
'sub_id')
dethl_data = CD1.group_by([d for d in data if d.location_id == 5519],
'sub_id')
field_data = [d for d in data if d.location_id in FIELD_TRIALS]
jki_gh_data = CD1.group_by([d for d in data if d.location_id == 6019],
'sub_id')
jki_sh_data = CD1.group_by([d for d in data if d.location_id == 6020],
'sub_id')
jki_gh_results = CD1.compute_starch_rel_ctrl(jki_gh_data, 6019,
[CD1.DROUGHT_ID])
jki_sh_results = CD1.compute_starch_rel_ctrl(jki_sh_data, 6020,
[CD1.DROUGHT_ID])
golm_results = CD1.compute_starch_rel_ctrl(golm_data, 4537,
[CD1.DROUGHT_ID])
dethl_results = CD1.compute_starch_rel_ctrl(dethl_data, 5519,
CD1.DETHLINGEN_DROUGHT_IDS)
field_results = CD1.compute_starch_rel_field(field_data,
FIELD_TRIALS, CD1.DROUGHT_ID)
all_results = [(jki_gh_results, 'JKI_CGW'),
(jki_sh_results, 'JKI_SHE'),
(golm_results, 'MPI_FIELD'),
(dethl_results, 'DET_FIELD'),
(field_results, 'ALL_FIELD')]
"""
compiled = {}
# compiled.update(golm_results)
compiled.update(dethl_results)
# compiled.update(field_results)
# compiled.update(jki_sh_results)
# compiled.update(jki_gh_results)
"""
for data, name in all_results:
print name
annotate_compiled_data(data, heat_d, h2o_d, irrigation_d)
WT.write_table(data, WT.FIELDS,
out=open('STARCH_REL_%s.csv' % name, 'w'))
return None
"""
results = CD1.compute_starch_rel_controlled(data, 4537)
write_table(results)
# return None
results = CD1.compute_starch_rel_dethlingen(data)
write_table(results)
# return None
"""
# results = CD1.compute_field_trials(data)
# write_table(results[0])
# return None
if __name__ == '__main__': main(sys.argv[1:])