# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
#read file 
import pandas as pd
import requests
import json
from config import gkey

filepath = "pollution_us_2000_2016.csv"
us_pollution_data = pd.read_csv(filepath)
us_pollution_data.head()

# -



# +
#Create Year column. which we will use to group the data by year.
Date_data = []
#run a loop on the 'Date Local' Column
for i in us_pollution_data['Date Local']:
#Split each row (which is date in string format) into a list
    date = i.split("-")
#The first element of the list will be the year.
    year= date[0]
#Append the year to a list of dates
    Date_data.append(year)
#Create a year column using Date data
us_pollution_data['Year'] = Date_data

#clean data by dropping rows with null values.
us_pollution_data_clean = us_pollution_data.dropna(how='any')
# -

#Create a collection of columns of interest 
Chem_Columns_data = us_pollution_data_clean[['State', 'Date Local', 'Year','NO2 Units', 'NO2 Mean', 'O3 Units', 'O3 Mean','SO2 Units', 'SO2 Mean', 'CO Units', 'CO Mean']]
#reindex data to set indices starting from 0 
Chem_Columns_data = Chem_Columns_data.reset_index()
#drop the index column 
Chem_Columns_data = Chem_Columns_data.drop(columns=['index'])
Chem_Columns_data.head()

#convert mean values to same units.
#In this case we convert all parts per billion to parts per million. 
#To do this we divide all parts per billion observations by 1000
Chem_Columns_data['NO2 Mean'] = Chem_Columns_data['NO2 Mean']/1000
Chem_Columns_data['SO2 Mean'] = Chem_Columns_data['SO2 Mean']/1000





Chem_by_ST_Year = Chem_Columns_data.groupby(['Year', 'State']).mean()
Chem_by_ST_Year



# +
#Create a column for the average of the mean measurements
#note still need to take conversions into account
Mean_us_pol = []
for index, row in Chem_by_ST_Year.iterrows():
    avg_pol = (row[0] + row[1] + row[2] + row[3])/4
    Mean_us_pol.append(avg_pol)
    
Chem_by_ST_Year['State Average'] = Mean_us_pol

Chem_by_ST_Year.head()
# -



Chem_by_Year = Chem_Columns_data.groupby(['Year']).mean()
Chem_by_Year.head()

# +
Year_Mean_us_pol = []
for index, row in Chem_by_Year.iterrows():
    avg_pol = (row[0] + row[1] + row[2] + row[3])/4
    Year_Mean_us_pol.append(avg_pol)
    
Chem_by_Year['Year Average'] = Year_Mean_us_pol
Chem_by_Year.head()
# -



#save DataFrame as Csv for easy access to Dataframe data 
Chem_by_ST_Year.to_csv("../DataFrames/Chem_mean_by_State.csv", header=True)

# +
#Are we sure we have the correct interpretation of a summation of the pullutant means 
# -

#save second DataFrame as Csv for easy access to Dataframe data 
Chem_by_Year.to_csv("../DataFrames/Chem_mean_by_Year.csv", header=True)



# +
#Create a new Groupby object which will be help for acquiring State pollutant means by year
Group_State_then_Year = Chem_Columns_data.groupby(['State', 'Year']).mean()
Year_Mean_us_pol2 = []
#Create an average column like we did before
for index, row in Group_State_then_Year.iterrows():
    avg_pol = (row[0] + row[1] + row[2] + row[3])/4
    Year_Mean_us_pol2.append(avg_pol)
    
Group_State_then_Year['Year Average'] = Year_Mean_us_pol2
Group_State_then_Year.head()
# -

Group_State_then_Year.to_csv("../DataFrames/Group_State_then_Year.csv", header=True)



AQI_data = us_pollution_data_clean[['State Code', 'County Code', 'State', 'County', 'City','Year','NO2 AQI', 'O3 AQI', 'SO2 AQI', 'CO AQI']]
AQI_data.head()





# +
#plot the average state values for a year versus number of cases in states for that year. or 
#plot the average us_val for a year versus number of case for that year 

#might have to merge on state or produce an overall average mean (of year) for the US based on an average of all states (for a year)

# +
target_city = "Phoenix, Arizona"

# Build the endpoint URL
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?'
    'address={0}&key={1}').format(target_city, gkey)

geo_data = requests.get(target_url).json()


lat = geo_data["results"][0]["geometry"]["location"]["lat"]
lng = geo_data["results"][0]["geometry"]["location"]["lng"]
# -













StarcoderdataJupyterScriptsDedupFiltered