-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
123 lines (100 loc) · 3.83 KB
/
filter.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
import argparse
import csv
import os
parser = argparse.ArgumentParser(description = "Script para busca de valores de simulação")
a = v = None
parser.add_argument('-a', action='store', dest=a, required=True, help='Ano inicial das simulações')
parser.add_argument('-v', action='store', dest=v, required=True, help='valor a ser buscado')
arg = parser.parse_args()
val = int(arg.v)
simulationYear = int(arg.a)
# Verifica se existe as rodadas
if os.path.exists("resultados"):
resultsDir = [d for d in os.listdir("resultados") if os.path.isdir(os.path.join("resultados", d))]
os.makedirs("busca", exist_ok=True)
for directory in resultsDir:
# Leitura do arquivo
simulationYear = int(arg.a)
years = []
with open("resultados/"+ directory +"/files/filtered.txt") as filtered:
for row in csv.reader(filtered, dialect="excel-tab"):
years.append(row)
results = []
for year in years[2:]:
# Separa simulacao e dano
daysAux = []
for day in year:
aux = day.split(';')
if aux[0].isdigit():
aux[0] = int(aux[0])
daysAux.append(aux)
# Adiciona o dia juliano
i = 1
for day in daysAux:
day.insert(0, i)
i += 1
# Remove as simulacoes que resultaram "NONE"
days = []
for day in daysAux:
if day[1] != "NONE":
days.append(day)
# Ordena os valores
days.sort(key=lambda x: x[1])
i = 0
found = []
# Laco que busca se existe o valor desejado
previous = None
while days[i][1] <= val:
if days[i][1] < val:
previous = days[i][1]
i += 1
continue
if days[i][1] == val:
if len(days[i]) > 2:
found.append(str(days[i][0]) + ";" + str(days[i][2]))
else:
found.append(str(days[i][0]))
i += 1
# Se o valor buscado nao foi encontrado utiliza-se o imediatamente menor
if not found:
i = 0
while days[i][1] <= previous:
if days[i][1] == previous:
if len(days[i]) > 2:
found.append(str(days[i][0]) + ";" + str(days[i][2]))
else:
found.append(str(days[i][0]))
i += 1
found.insert(0, simulationYear)
results.append(found)
simulationYear += 1
# Calculo das medias
averageResults = []
for result in results:
daysAux = []
average = None
for day in result[1:]:
aux = day.split(';')
daysAux.append(int(aux[0]))
sum = 0
count = len(daysAux)
for day in daysAux:
sum += day
average = sum / count
avg = []
avg.append(result[0])
avg.append(format(average, '.3f'))
averageResults.append(avg)
# Escrita dos resultados nos arquivos de saida
os.makedirs("busca/"+ directory, exist_ok=True)
with open("busca/"+ directory + "/dia_" + str(val) + ".txt", 'w') as f:
writer = csv.writer(f, dialect="excel-tab")
for r in results:
writer.writerow(r)
with open("busca/"+ directory + "/dia_" + str(val) + "_medias.txt", 'w') as f:
writer = csv.writer(f, dialect="excel-tab")
for ar in averageResults:
writer.writerow(ar)
else:
print("Pasta de resultados não encontrada!")
print("Processo concluído!")