<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _attach_aggregated(network, grid, aggregated, ding0_grid):
 """Add Generators and Loads to MV station representing aggregated generation capacity and load Parameters grid: MVGrid MV grid object aggregated: dict Information about aggregated load and generation capacity. For ding0_grid: ding0.Network Ding0 network container Returns ------- MVGrid Altered instance of MV grid including aggregated load and generation """


    aggr_line_type = ding0_grid.network._static_data['MV_cables'].iloc[
        ding0_grid.network._static_data['MV_cables']['I_max_th'].idxmax()]

    for la_id, la in aggregated.items():
        # add aggregated generators
        for v_level, val in la['generation'].items():
            for type, val2 in val.items():
                for subtype, val3 in val2.items():
                    if type in ['solar', 'wind']:
                        gen = GeneratorFluctuating(
                            id='agg-' + str(la_id) + '-' + '_'.join(
                                [str(_) for _ in val3['ids']]),
                            nominal_capacity=val3['capacity'],
                            weather_cell_id=val3['weather_cell_id'],
                            type=type,
                            subtype=subtype,
                            geom=grid.station.geom,
                            grid=grid,
                            v_level=4)
                    else:
                        gen = Generator(
                            id='agg-' + str(la_id) + '-' + '_'.join(
                                [str(_) for _ in val3['ids']]),
                            nominal_capacity=val3['capacity'],
                            type=type,
                            subtype=subtype,
                            geom=grid.station.geom,
                            grid=grid,
                            v_level=4)
                    grid.graph.add_node(gen, type='generator_aggr')

                    # backup reference of geno to LV geno list (save geno
                    # where the former LV genos are aggregated in)
                    network.dingo_import_data.set_value(network.dingo_import_data['id'].isin(val3['ids']),
                                                        'agg_geno',
                                                        gen)

                    # connect generator to MV station
                    line = Line(id='line_aggr_generator_la_' + str(la_id) + '_vlevel_{v_level}_'
                                '{subtype}'.format(
                                 v_level=v_level,
                                 subtype=subtype),
                                 type=aggr_line_type,
                                 kind='cable',
                                 length=1e-3,
                                 grid=grid)
                    grid.graph.add_edge(grid.station,
                                        gen,
                                        line=line,
                                        type='line_aggr')

        for sector, sectoral_load in la['load'].items():
            load = Load(
                geom=grid.station.geom,
                consumption={sector: sectoral_load},
                grid=grid,
                id='_'.join(['Load_aggregated', sector, repr(grid), str(la_id)]))

            grid.graph.add_node(load, type='load')

            # connect aggregated load to MV station
            line = Line(id='_'.join(['line_aggr_load_la_' + str(la_id), sector, str(la_id)]),
                        type=aggr_line_type,
                        kind='cable',
                        length=1e-3,
                        grid=grid)

            grid.graph.add_edge(grid.station,
                                load,
                                line=line,
                                type='line_aggr')