#Setup
import time
import pickle
import pandas as pd
import numpy as np
import csv
from pandas import DataFrame
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="myGeocoder")

#Raw file
rawFile = pd.read_csv("pueblaClean.csv")
df = DataFrame(rawFile, columns= ['Year','Clave_Ent','Entidad','Cve. Municipio','Municipio','Bien juridico afectado','Tipo de delito','Subtipo de delito','Modalidad','Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'])

def create_numerical(name):
    #Parse to numpy array
    tmp = df.loc[df.Municipio == name]
    tmp = tmp[tmp.columns[9:20]]
    tmp = tmp.get_values()
    #Process data, two dimensional numpy array
    sum = 0
    count = 0
    for x in range(0, tmp.shape[0]): #1st dim
        for y in range(0, tmp.shape[1]): #2nd dim
            try:
                if (type(tmp[x, y]) is str):
                    tmp[x, y] = float(tmp[x, y])
                    sum += tmp[x, y]
            except:
                tmp[x, y] = 0.0
    return sum

def create_municipios(data_frame):
    dfMunicipios = data_frame.set_index("Municipio", drop = False)
    municipios = []
    for index, row in df.iterrows():
        if (row['Municipio'] not in municipios) and ("?" not in row['Municipio']):
            municipios.append(row['Municipio'])
    return municipios

def create_lat(municipios, state, country):
    lats = []
    for municipio in municipios:
        time.sleep(0.5) #don't remove! API will block if removed
        try:
            location = geolocator.geocode(municipio+", "+state+", "+country)
            lat = location.latitude
        except:
            lat = 0.0
        lats.append(lat)
    return lats

def create_lon(municipios, state, country):
    lons = []
    for municipio in municipios:
        time.sleep(0.5) #don't remove! API will block if removed
        try:
            location = geolocator.geocode(municipio+", "+state+", "+country)
            lon = location.longitude
        except:
            lon = 0.0
        lons.append(lon)
    return lons

def pickling(l, name_of_file):
    with open(name_of_file, "wb") as f:
        pickle.dump(l, f)

##################################################################################
#creates municipalities
state = "Puebla"
country = "Mexico"
municipios = create_municipios(df)

#creates numerical data
sums = []
for item in municipios:
    sums.append(create_numerical(item))

#creates latitudes and longitudes
latitudes = create_lat(municipios, state, country)
longitudes = create_lon(municipios, state, country)

#pickling
pickling(municipios, "municipiosList")
pickling(sums, "numbersList")
pickling(latitudes, "latitudesList")
pickling(longitudes, "longitudesList")

print("done")
########## O P T I O N A L #########
#CREATES CSV FILE OF MUNICIPALITIES#
#cities to csv
#with open("municipios.csv", 'w') as f:
    #wr = csv.writer(f, quoting=csv.QUOTE_ALL)
    #wr.writerow(municipios)