

def add_neighborhood_data(output):
    print('adding neighborhood data')
    bg_population = {
        
    }
    for intsect in json.load(open('data/neighborhoods-blockgroups.json')):
        bg = intsect['2012-blockgroups']['id']
        if (bg in bg_population):
            continue
        (state_fips, county_fips, tract, bg_num) = (bg[0:2], bg[2:5], bg[5:11], bg[11:12])
        if (state_fips != '11'):
            bg_population[bg] = 0
            continue
        url = ('http://api.census.gov/data/%s?key=%s&get=%s&in=state:%s+county:%s+tract:%s&for=block+group:%s' % ('2010/sf1', census_api_key, 'P0010001', state_fips, county_fips, tract, bg_num))
        census_data = json.load(urlopen(url))
        bg_population[bg] = int(census_data[1][0])
    hood_population = {
        
    }
    for intsect in json.load(open('data/neighborhoods-blockgroups.json')):
        h = intsect['dc-neighborhoods']['id']
        bg = intsect['2012-blockgroups']['id']
        intsect_pop = (bg_population[bg] * intsect['2012-blockgroups']['ratio'])
        hood_population[h] = (hood_population.get(h, 0) + intsect_pop)
    for ward in output.values():
        for anc in ward['ancs'].values():
            anc['neighborhoods'] = []
            for smd in anc['smds'].values():
                smd['neighborhoods'] = []
    for anc_smd in ('anc', 'smd'):
        dat = json.load(open(('data/%s-neighborhood.json' % anc_smd)))
        for ix in dat:
            ward = ix[anc_smd]['id'][0]
            anc = ix[anc_smd]['id'][1]
            if (anc_smd == 'anc'):
                feature = output[ward]['ancs'][anc]
            else:
                smd = ix[anc_smd]['id'][2:]
                feature = output[ward]['ancs'][anc]['smds'][smd]
            feature['neighborhoods'].append({
                'name': ix['neighborhood']['name'],
                ('part-of-' + anc_smd): ix[anc_smd]['ratio'],
                'part-of-neighborhood': ix['neighborhood']['ratio'],
                'population': int(round((hood_population[ix['neighborhood']['id']] * ix['neighborhood']['ratio']))),
            })
    for ward in output.values():
        for anc in ward['ancs'].values():
            anc['neighborhoods'].sort(key=(lambda h: (- h['part-of-anc'])))
            for smd in anc['smds'].values():
                smd['neighborhoods'].sort(key=(lambda h: (- h['part-of-smd'])))

dataset/ETHPy150Open codefordc/ancfinder/scripts/update_anc_database.py add_neighborhood_data/original