<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def storage_distribution(network, scaling=1, filename=None):
 """ Plot storage distribution as circles on grid nodes Displays storage size and distribution in network. Parameters network : PyPSA network container Holds topology of grid including results from powerflow analysis filename : str Specify filename If not given, figure will be show directly """


    stores = network.storage_units
    storage_distribution = network.storage_units.p_nom_opt[stores.index]\
            .groupby(network.storage_units.bus)\
            .sum().reindex(network.buses.index, fill_value=0.)

    fig, ax = plt.subplots(1, 1)
    fig.set_size_inches(6, 6)

    msd_max = storage_distribution.max()
    msd_median = storage_distribution[storage_distribution != 0].median()
    msd_min = storage_distribution[storage_distribution > 1].min()

    if msd_max != 0:
        LabelVal = int(log10(msd_max))
    else:
        LabelVal = 0
    if LabelVal < 0:
        LabelUnit = 'kW'
        msd_max, msd_median, msd_min = msd_max * \
            1000, msd_median * 1000, msd_min * 1000
        storage_distribution = storage_distribution * 1000
    elif LabelVal < 3:
        LabelUnit = 'MW'
    else:
        LabelUnit = 'GW'
        msd_max, msd_median, msd_min = msd_max / \
            1000, msd_median / 1000, msd_min / 1000
        storage_distribution = storage_distribution / 1000

    if sum(storage_distribution) == 0:
        network.plot(bus_sizes=0, ax=ax, title="No storages")
    else:
        network.plot(
            bus_sizes=storage_distribution * scaling,
            ax=ax,
            line_widths=0.3,
            title="Storage distribution")

    # Here we create a legend:
    # we'll plot empty lists with the desired size and label
    for area in [msd_max, msd_median, msd_min]:
        plt.scatter([], [], c='white', s=area * scaling,
                    label='= ' + str(round(area, 0)) + LabelUnit + ' ')
    plt.legend(scatterpoints=1, labelspacing=1, title='Storage size')

    if filename is None:
        plt.show()
    else:
        plt.savefig(filename)
        plt.close()