Last active
December 4, 2018 03:33
-
-
Save variux/b45db62aa31736a3b736b51fcf3f5165 to your computer and use it in GitHub Desktop.
Proyecto de Valeria [03/12/18]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os | |
import numpy as np | |
import matplotlib.pyplot as plt | |
class CsvHandler(): | |
file_route = None | |
station = None | |
seca = None | |
lluviosa = None | |
output_file = None | |
promedio_dict = {} | |
def __init__(self, file_route, output_file): | |
self.set_fileroute(file_route) | |
self.set_output(output_file) | |
def set_fileroute(self, file_route): | |
self.file_route = file_route | |
def set_output(self, route): | |
self.output_file = route | |
def set_station(self, station): | |
self.station = station | |
def set_seca(self, seca): | |
self.seca = seca | |
def set_lluviosa(self, lluviosa): | |
self.lluviosa = lluviosa | |
def read_all_info(self): | |
with open(self.file_route, mode='r') as station_file: | |
read_stations = csv.DictReader(station_file, delimiter=',') | |
station_list = [] | |
for row in read_stations: | |
station_list.append(dict(row)) | |
return station_list | |
def read_output(self): | |
with open(self.output_file, mode='r') as station_file: | |
read_stations = csv.DictReader(station_file, delimiter=',') | |
station_list = [] | |
for row in read_stations: | |
station_list.append(dict(row)) | |
return station_list | |
def promedio(self): | |
stations = self.read_all_info() | |
sum_ml = 0.0 | |
station_counter = 0 | |
for station in stations: | |
sum_ml = sum_ml + float(station["ML"]) | |
station_counter = station_counter + 1 | |
return sum_ml / station_counter | |
def moda(self): | |
stations = self.read_all_info() | |
mls = [] | |
for station in stations: | |
mls.append(float(station["ML"])) | |
rpt = 0 | |
for i in mls: | |
n = mls.count(i) | |
if n > rpt: | |
rpt = n | |
moda_mls = [] | |
for i in mls: | |
n = mls.count(i) | |
if n == rpt and i not in moda_mls: | |
moda_mls.append(i) | |
if len(moda_mls) != len(mls): | |
return moda_mls | |
else: | |
return False | |
def mediana(self): | |
stations = self.read_all_info() | |
mls = [] | |
for station in stations: | |
mls.append(float(station["ML"])) | |
mls_ordered = sorted(mls) | |
mls_len = len(mls) | |
index = (mls_len - 1) // 2 | |
if mls_len % 2: | |
return mls_ordered[index] | |
else: | |
return (mls_ordered[index] + mls_ordered[index + 1])/2.0 | |
def promedio_precipitacion(self): | |
stations = self.read_all_info() | |
station_names = [] | |
for station in stations: | |
station_names.append(station["ESTACION"]) | |
clean_station_names = list(set(station_names)) | |
dict_stations = {} | |
for station_name in clean_station_names: | |
dict_stations[station_name] = list(filter(lambda estacion: estacion["ESTACION"] == station_name, stations)) | |
self.promedio_dict = {} | |
for station_name in clean_station_names: | |
sum_station_seca = 0.0 | |
sum_station_lluviosa = 0.0 | |
counter_seca = 0 | |
counter_lluviosa = 0 | |
self.promedio_dict[station_name] = {} | |
for i in range(len(dict_stations[station_name])): | |
mes = dict_stations[station_name][i]["MM"] | |
if int(mes) == 12 or (int(mes) >= 1 and int(mes) <= 4): | |
sum_station_seca = sum_station_seca + float(dict_stations[station_name][i]["ML"]) | |
counter_seca = counter_seca + 1 | |
elif int(mes) >= 5 and int(mes) <=11: | |
sum_station_lluviosa = sum_station_lluviosa + float(dict_stations[station_name][i]["ML"]) | |
counter_lluviosa = counter_lluviosa + 1 | |
if counter_seca != 0: | |
self.promedio_dict[station_name]["SECA"] = sum_station_seca / counter_seca | |
elif counter_seca == 0: | |
self.promedio_dict[station_name]["SECA"] = sum_station_seca | |
if counter_lluviosa != 0: | |
self.promedio_dict[station_name]["LLUVIOSA"] = sum_station_lluviosa / counter_lluviosa | |
elif counter_lluviosa == 0: | |
self.promedio_dict[station_name]["LLUVIOSA"] = sum_station_lluviosa | |
self.insert_data() | |
return True | |
def insert_header(self): | |
with open(self.output_file, mode='a') as station_file: | |
write_header = csv.writer(station_file, delimiter=',') | |
write_header.writerow(["ESTACION", "SECA", "LLUVIOSA"]) | |
def insert_data(self): | |
if(os.stat(self.output_file).st_size == 0): | |
self.insert_header() | |
with open(self.output_file, mode='a') as station_file: | |
write_station = csv.writer(station_file, delimiter=',') | |
for station in self.promedio_dict: | |
write_station.writerow([station, self.promedio_dict[station]["SECA"], self.promedio_dict[station]["LLUVIOSA"]]) | |
return True | |
class EstacionMeteorologica(): | |
dd = None | |
mm = None | |
aa = None | |
file_route = None | |
ml = None | |
estacion = None | |
def __init__(self, dia, mes, ano, ml, estacion, file_route): | |
self.set_dd(dia) | |
self.set_mm(mes) | |
self.set_aa(ano) | |
self.set_ml(ml) | |
self.set_route(file_route) | |
self.set_estacion(estacion) | |
def set_estacion(self, estacion): | |
self.estacion = estacion | |
return True | |
def set_route(self, file_route): | |
self.file_route = file_route | |
def set_dd(self,dia): | |
if dia > 0 and dia < 32: | |
self.dd = dia | |
return True | |
else: | |
return False | |
def set_mm(self, mes): | |
if mes > 0 and mes < 13: | |
self.mm = mes | |
return True | |
else: | |
return False | |
def set_aa(self, ano): | |
self.aa = int(ano) | |
return True | |
def set_ml(self, mili): | |
self.ml = float(mili) | |
return True | |
def get_dd(self): | |
return self.dd | |
def get_mm(self): | |
return self.mm | |
def get_aa(self): | |
return self.aa | |
def get_ml(self): | |
return self.ml | |
def get_station(self): | |
return self.estacion | |
def insert_info(self): | |
with open(self.file_route, mode='a') as station_file: | |
write_station = csv.writer(station_file, delimiter=',') | |
write_station.writerow([self.dd, self.mm, self.aa, self.estacion, self.ml]) | |
return True | |
class Graph(): | |
datos_grafico = None | |
N = None | |
seca = [] | |
lluviosa = [] | |
nombres = [] | |
def __init__(self, datos_grafico): | |
self.datos_grafico = csv_read.read_output() | |
self.N = len(self.datos_grafico) | |
for datos in datos_grafico: | |
self.seca.append(float(datos["SECA"])) | |
self.lluviosa.append(float(datos["LLUVIOSA"])) | |
self.nombres.append(datos["ESTACION"]) | |
seca_means = tuple(self.seca) | |
lluviosa_means = tuple(self.lluviosa) | |
nombres_tuple = tuple(self.nombres) | |
fig, ax = plt.subplots() | |
index = np.arange(self.N) | |
bar_width = 0.35 | |
opacity = 0.8 | |
rects1 = plt.bar(index, seca_means, bar_width, | |
alpha=opacity, | |
color='b', | |
label='Seca') | |
rects2 = plt.bar(index + bar_width, lluviosa_means, bar_width, | |
alpha=opacity, | |
color='g', | |
label='Lluviosa') | |
plt.xlabel('Estaciones') | |
plt.ylabel('Promedio de precipitación (mL)') | |
plt.title('Promedio de precipitación por estación') | |
plt.xticks(index + bar_width, nombres_tuple) | |
plt.legend() | |
plt.tight_layout() | |
plt.show() | |
menu_flag = True | |
while menu_flag: | |
print("Bievenido al sistema de registro de precipitaciones del INM:".center(os.get_terminal_size().columns)) | |
print("") | |
print("1 - Registrar Precipitación".center(os.get_terminal_size().columns)) | |
print("") | |
print("2 - Generar promedio de las precipitaciones por estación".center(os.get_terminal_size().columns)) | |
print("") | |
print("3 - Genera gráfico de precipitaciones promedio".center(os.get_terminal_size().columns)) | |
print("") | |
print("4 - Generar promedios de precipitación (mL) ".center(os.get_terminal_size().columns)) | |
print("") | |
print("5 - Generar mediana de precipitación (mL) ".center(os.get_terminal_size().columns)) | |
print("") | |
print("6 - Generar moda de precipitación (mL) ".center(os.get_terminal_size().columns)) | |
print("") | |
print("7 - Si desea salir escoja esta opción".center(os.get_terminal_size().columns)) | |
print("") | |
opt = int(input("Escoja una de las opciones anteriores: ")) | |
file_route = 'datosBasico.csv' | |
if opt == 1: | |
precipitacion_flag = True | |
while precipitacion_flag: | |
dia = int(input("Inserte el día: ")) | |
mes = int(input("Inserte el mes: ")) | |
ano = int(input('Inserte el ano: ')) | |
ml = float(input('Inserte la cantidad en mL de precipitación: ')) | |
estacion = input('Inserte la estación donde se registró: ') | |
station = EstacionMeteorologica(dia, mes, ano, ml, estacion, file_route) | |
opt_persona = input('Desea agregar otra precipitación?: escriba "s" para sí y "n" para no: ') | |
if opt_persona == "s": | |
precipitacion_flag = True | |
elif opt_persona == "n": | |
precipitacion_flag = False | |
elif opt == 2: | |
print("Generando...") | |
opt_2 = input('Desea realizar otra acción?: escriba "s" para sí y "n" para no: ') | |
csv_read = CsvHandler('datosBasico.csv', 'datosOutput.csv') | |
csv_read.promedio_precipitacion() | |
if opt_2 == "s": | |
menu_flag = True | |
elif opt_2 == "n": | |
menu_flag == False | |
elif opt == 3: | |
csv_read = CsvHandler('datosBasico.csv', 'datosOutput.csv') | |
grafico = Graph(csv_read.read_output()) | |
opt_3 = input('Desea realizar otra acción?: escriba "s" para sí y "n" para no: ') | |
if opt_3 == "s": | |
menu_flag = True | |
elif opt_3 == "n": | |
menu_flag == False | |
elif opt == 4: | |
csv_read = CsvHandler('datosBasico.csv', 'datosOutput.csv') | |
print("El promedio es :") | |
print(csv_read.promedio()) | |
elif opt == 5: | |
csv_read = CsvHandler('datosBasico.csv', 'datosOutput.csv') | |
print("La mediana es: ") | |
print(csv_read.mediana()) | |
elif opt == 6: | |
csv_read = CsvHandler('datosBasico.csv', 'datosOutput.csv') | |
if csv_read.moda() == False: | |
print("No presenta ninguna moda") | |
else: | |
print("La moda es: ") | |
print(csv_read.moda()) | |
elif opt == 7: | |
print("Muchas gracias por utilizar el sistema!") | |
menu_flag = False | |
else: | |
print("Debe escoger alguna de las opciones anteriores") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment