question1.py 5.25 KB
Newer Older
Tianyang's avatar
Tianyang committed
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
import numpy as np
import matplotlib.pyplot as plt
from functools import reduce
from database_pre import connection
import matplotlib.pyplot as plt


table_variable = ['station varchar',
        'year',
        'season',
        'month',
        'day',
        'hour',
        'minute',
        'lon',
        'lat',
		'tmpf',
		'dwpf',
		'relh',
		'drct',
		'sknt',
		'p01i',
		'alti',
		'mslp',
		'vsby',
		'gust',
		'skyc1',
		'skyc2',
		'skyc3',
		'skyc4',
		'skyl1',
		'skyl2',
		'skyl3',
		'skyl4',
		'wxcodes',
		'ice_accretion_1hr',
		'ice_accretion_3hr',
		'ice_accretion_6hr',
		'peak_wind_gust',
		'peak_wind_drct',
		'peak_wind_time',
		'feel',
		'metar']

def add (x,y):
    return x+y

def fmax(x,y):
    return max(x,y)

def fmin(x,y):
    return min(x,y)

#caculate mean, max, min reduce
#input [count,mean,max,min]
def reduceFonction (x,y):
    result = []
    fonctions = {0:'add',1:'add',2:'fmax',3:'fmin'}
    for i in range(4):
        result.append(reduce(eval(fonctions.get(i)),[x[i],y[i]]))
    return result

#input [valeur] -> [count,mean,max,min]
def mapFonction1 (x):
    return [1,x,x,x]

#input [count,mean,max,min] -> [mean,max,min]
def mapFonction2 (x):
    return [x[1]/x[0],x[2],x[3]]

def testNan (x):
    test = x != x
    return test

def mapReduce_mmm(data,timeNB,targetNB):
    results = dict()
    for row in data.result():
        if timeNB == 1:
            data_time = row[timeNB]
        elif timeNB == 2:
            data_time = (row[timeNB-1],row[timeNB])
        elif timeNB == 3:
            data_time = (row[timeNB-2],row[timeNB-1],row[timeNB])
        else:
            assert 1==2, "Doesn\'t exits!"
        data_target = row[targetNB]
        if testNan(data_time) or testNan(data_target):
            continue
        if results.get(data_time) is None:
            results[data_time] = mapFonction1(data_target)
        else:
            mapresult = mapFonction1(data_target)
            results[data_time] = reduceFonction(mapresult,results[data_time])
    for eachTime in results:
        results[eachTime] = mapFonction2(results[eachTime])
    return results


def zipValues (values):
    result = [[],[],[]]
    for i in range(3):
        for each in values:
            result[i].append(each[i])
    return result


def drawCourbe_history(session,time,target,timeNB,targetNB,espace):
    data = session.execute_async("select * from caitiany.database_espace where station = '%s'"%espace )
    results = mapReduce_mmm(data,timeNB,targetNB)
    keys = list(results.keys())
    values = list(results.values())
    zipped_result = zipValues(values)
    if isinstance(keys[0],tuple):
        for each in keys:
            index_each = keys.index(each)
            each = list(each)
            if timeNB == 2:
                each[0] = str(each[0])
                keys[index_each] = ".".join(each)
            elif timeNB == 3:
                each[0] = str(each[0])
                each[2] = str(each[2])
                keys[index_each] = each[0] +'.'+ each[2]
    fig, ax = plt.subplots(1, 1)
    plt.plot(keys,zipped_result[0],'x-',label="mean")
    plt.plot(keys,zipped_result[1],'+-',label="max")
    plt.plot(keys,zipped_result[2],'b--',label="min")
    plt.xticks(keys, keys, rotation=45, fontsize=5)
    plt.ylabel(target)
    plt.grid(True)
    if timeNB == 3:
        for label in ax.get_xticklabels():
            label.set_visible(False)
        for label in ax.get_xticklabels()[::6]:
            label.set_visible(True)
    plt.legend(bbox_to_anchor=(1.0, 1), loc=1, borderaxespad=0.)
    plt.savefig("Projet-NF26/test.png")


def checkNBvariable (x):
    i=0
    for each in table_variable:
        if x == each:
            return i
        i += 1
    print ('Doesn\'t exist!!')




def drawCourbe_season(session,season,target,targetNB,espace):
    data = session.execute_async("select * from caitiany.database_espace where station = '%s'"%espace )
    results = mapReduce_mmm(data,2,targetNB)
    results = seprateSeason(results,season)
    keys = list(results.keys())
    values = list(results.values())
    print(values)
    zipped_result = zipValues(values)
    for each in keys:
        index_each = keys.index(each)
        each = list(each)
        each[0] = str(each[0])
        keys[index_each] = ".".join(each)

    fig, ax = plt.subplots(1, 1)
    plt.plot(keys,zipped_result[0],'x-',label="mean")
    plt.plot(keys,zipped_result[1],'+-',label="max")
    plt.plot(keys,zipped_result[2],'b--',label="min")
    plt.xticks(keys, keys, rotation=45, fontsize=5)
    plt.ylabel(target)
    plt.grid(True)
    plt.legend(bbox_to_anchor=(1.0, 1), loc=1, borderaxespad=0.)
    plt.savefig("Projet-NF26/test.png")



def seprateSeason (results,season):
    output = dict()
    for each in results:
        if season in each:
            output[each] = results[each]
    return output



if __name__ == "__main__":
    session = connection()
    espace = input("Please enter what station you want to search:  ")
    #time = input("Per which kind of time:  ")
    target = input("Which value do you want to check:  ")
    #timeNB = checkNBvariable(time)
    targetNB = checkNBvariable(target)
    #drawCourbe_history(session,time,target,timeNB,targetNB,espace)

    #Season check
    season = input("Please enter which season you want to search:  ")
    drawCourbe_season(session,season,target,targetNB,espace)