%config InlineBackend.figure_format = 'retina'
%matplotlib inline

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import pandas as pd

import seaborn as sns
sns.set_style("white")

"""
Explanation: San Diego Burrito Analytics: Coordinates
Determine the longitude and latitude for each restaurant based on its address
Default imports
End of explanation
"""


import util2
df, dfRestaurants, dfIngredients = util2.load_burritos()
N = df.shape[0]

"""
Explanation: Load data
End of explanation
"""


dfRestaurants=dfRestaurants.reset_index().drop('index',axis=1)

dfRestaurants

"""
Explanation: Process
End of explanation
"""


dfAvg = df.groupby('Location').agg({'Cost': np.mean,'Volume': np.mean,'Hunger': np.mean,
                                'Tortilla': np.mean,'Temp': np.mean,'Meat': np.mean,
                                'Fillings': np.mean,'Meat:filling': np.mean,'Uniformity': np.mean,
                                'Salsa': np.mean,'Synergy': np.mean,'Wrap': np.mean,
                                'overall': np.mean, 'Location':np.size})
dfAvg.rename(columns={'Location': 'N'}, inplace=True)
dfAvg['Location'] = list(dfAvg.index)

# Calculate latitutude and longitude for each city
import geocoder
addresses = dfRestaurants['Address'] + ', San Diego, CA'
lats = np.zeros(len(addresses))
longs = np.zeros(len(addresses))
for i, address in enumerate(addresses):
    g = geocoder.google(address)
    Ntries = 1
    while g.latlng ==[]:
        g = geocoder.google(address)
        Ntries +=1
        print 'try again: ' + address
        if Ntries >= 5:
            if 'Marshall College' in address:
                address = '9500 Gilman Drive, La Jolla, CA'
                g = geocoder.google(address)
                Ntries = 1
                while g.latlng ==[]:
                    g = geocoder.google(address)
                    Ntries +=1
                    print 'try again: ' + address
                    if Ntries >= 5:
                        raise ValueError('Address not found: ' + address)
            else:
                raise ValueError('Address not found: ' + address)
    lats[i], longs[i] = g.latlng
    
# # Check for nonsense lats and longs
if sum(np.logical_or(lats>34,lats<32)):
    raise ValueError('Address not in san diego')
if sum(np.logical_or(longs<-118,longs>-117)):
    raise ValueError('Address not in san diego')

# Incorporate lats and longs into restaurants data
dfRestaurants['Latitude'] = lats
dfRestaurants['Longitude'] = longs
# Merge restaurant data with burrito data
dfTableau = pd.merge(dfRestaurants,dfAvg,on='Location')

dfTableau.head()

dfTableau.to_csv('df_burrito_tableau.csv')

"""
Explanation: Process Cali burrito data: Averages for each restaurant
End of explanation
"""


