question1.py 6.7 KB
Newer Older
Tianyang's avatar
Tianyang committed
1
2
3
import numpy as np
import matplotlib.pyplot as plt
from functools import reduce
Tianyang's avatar
Tianyang committed
4
from database_pre1 import connection
Tianyang's avatar
Tianyang committed
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
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]]


Tianyang's avatar
Tianyang committed
72
#Map reduce fonction
Tianyang's avatar
Tianyang committed
73
74
75
76
77
78
79
80
81
82
83
84
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]
Tianyang's avatar
Tianyang committed
85
        if str(data_time) == 'null' or str(data_target) == 'null':
Tianyang's avatar
Tianyang committed
86
87
88
89
90
91
92
93
94
95
            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

Tianyang's avatar
Tianyang committed
96
#Zip the values in 3 list, one by one
Tianyang's avatar
Tianyang committed
97
98
99
100
101
102
103
def zipValues (values):
    result = [[],[],[]]
    for i in range(3):
        for each in values:
            result[i].append(each[i])
    return result

Tianyang's avatar
Tianyang committed
104
#Fonction 1: the history courbe graph
Tianyang's avatar
Tianyang committed
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
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.)
Tianyang's avatar
Tianyang committed
135
136
    plt.savefig("Projet-NF26/question1.png")
    print ("Generate successfully")
Tianyang's avatar
Tianyang committed
137
138


Tianyang's avatar
Tianyang committed
139
#Check which number of the indicateur
Tianyang's avatar
Tianyang committed
140
141
142
143
144
145
146
147
148
149
150
def checkNBvariable (x):
    i=0
    for each in table_variable:
        if x == each:
            return i
        i += 1
    print ('Doesn\'t exist!!')




Tianyang's avatar
Tianyang committed
151
152
153
154
155
156
157
158
159
#Caculate the mean of the values of each season
def caculateMean_Season(result):
    i = 0
    total = 0
    for each in result:
        total += each
        i += 1
    return total/i , i

Tianyang's avatar
Tianyang committed
160
#Draw the courbe of the fonction2
Tianyang's avatar
Tianyang committed
161
162
def drawCourbe_season(session,season,target,targetNB,espace):
    data = session.execute_async("select * from caitiany.database_espace where station = '%s'"%espace )
Tianyang's avatar
Tianyang committed
163
    #We do the same map reduce as fonction 1 by fixing the time as season
Tianyang's avatar
Tianyang committed
164
165
166
167
168
169
170
171
172
173
174
175
    results = mapReduce_mmm(data,2,targetNB)
    results = seprateSeason(results,season)
    keys = list(results.keys())
    values = list(results.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)
Tianyang's avatar
Tianyang committed
176
    mean_season_mean, longth = caculateMean_Season(zipped_result[0])
Tianyang's avatar
Tianyang committed
177
    plt.plot(keys,zipped_result[0],'x-',label="mean")
Tianyang's avatar
Tianyang committed
178
179
180
    plt.plot(keys,[mean_season_mean for i in range(longth)],'--')

    mean_season_mean, longth = caculateMean_Season(zipped_result[1])
Tianyang's avatar
Tianyang committed
181
    plt.plot(keys,zipped_result[1],'+-',label="max")
Tianyang's avatar
Tianyang committed
182
183
184
    plt.plot(keys,[mean_season_mean for i in range(longth)],'--')

    mean_season_mean, longth = caculateMean_Season(zipped_result[2])
Tianyang's avatar
Tianyang committed
185
    plt.plot(keys,zipped_result[2],'b--',label="min")
Tianyang's avatar
Tianyang committed
186
187
    plt.plot(keys,[mean_season_mean for i in range(longth)],'--')

Tianyang's avatar
Tianyang committed
188
189
190
191
    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.)
Tianyang's avatar
Tianyang committed
192
193
    plt.savefig("Projet-NF26/question1_season.png")
    print ("Generate successfully")
Tianyang's avatar
Tianyang committed
194
195


Tianyang's avatar
Tianyang committed
196
#Choose the data of the season we want
Tianyang's avatar
Tianyang committed
197
198
199
200
201
202
203
204
205
206
207
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()
Tianyang's avatar
Tianyang committed
208
209
210
211
212
    choice = int(input("Which kind of service do you want?\n1.Station history\n2.Check history by seasons\nYour choice: "))

    #if choice == 1, we will use the fonction 1
    if choice == 1:
        espace = input("Please enter which station you want to search [LEBZ,LETO,etc]:  ")
Tianyang's avatar
Tianyang committed
213
        time = input("By which kind of time [year,season,month]:  ")
Tianyang's avatar
Tianyang committed
214
215
216
217
218
219
220
221
222
223
224
        target = input("Which indicator do you want to check [tmpf,dwpf,etc]:  ")
        timeNB = checkNBvariable(time)
        targetNB = checkNBvariable(target)
        drawCourbe_history(session,time,target,timeNB,targetNB,espace)
    else:
        #Fonction 2, the check according to the seasons
        espace = input("Please enter what station you want to search [LEBZ,LETO,etc]:  ")
        target = input("Which indicator do you want to check [tmpf,dwpf,etc]:  ")
        targetNB = checkNBvariable(target)
        season = input("Please enter which season you want to search [Spring,Summer,Autumn,Winter]:  ")
        drawCourbe_season(session,season,target,targetNB,espace)