import os
import requests
import pandas as pd
import csv
import urllib2
import openpyxl
import csv

"""
Explanation: (c) Karen Belita  <br /> 
Team Neighborhood Change <br /> 
Last Updated 9/7/2016 <br /> 
NOTEBOOK: INGESTION for CBSA-CITY-STATE
To create a table that has the CBSA code for City and State abbreviation that belongs to that MSA that.
MSA = Metropolitan Statistical Area
OUTPUT
MSA_principal.xls - contains cbsa code and corresponding msa, and the cities that belong to it (fips state code only) <br /> 
MSA_STATE.xls - contains state abbreviation and its fips code
DEPENDENCIES
End of explanation
"""


def xls_state():

    path_year = os.path.join(os.getcwd())
    file_name = path_year + "/" + "MSA_STATE"+ ".xls" 
    url= "https://www.census.gov/2010census/xls/fips_codes_website.xls"

    f = urllib2.urlopen(url)
    data = f.read()
    with open(file_name, "wb") as code:
        code.write(data)
    



"""
Explanation: INGESTION
DEFINE FUNCTIONS
Define function that downloads census xls file that contains state abbreviation and state fips code
End of explanation
"""


def xls_principal():

    path_year = os.path.join(os.getcwd())
    file_name = path_year + "/" + "MSA_principal"+ ".xls" 
    url= "http://www.census.gov/population/metro/files/lists/2015/List2.xls"

    f = urllib2.urlopen(url)
    data = f.read()
    with open(file_name, "wb") as code:
        code.write(data)
    
    


"""
Explanation: Define function taht downloads census xls file that contains cbsa and the corresponding msa name  and principal cities that belong to that msa
End of explanation
"""



def main():
    """
    Main execution
    """
    xls_state()       
    xls_principal() 


#######################
### Execution ########
#######################

if __name__ == '__main__':
    main()

"""
Explanation: MAIN EXECUTION
End of explanation
"""


