def aposteriori_spawning(fin, fout, pin, pout, save_canonical=False):
    """
    :param f: An ``IOManager`` instance providing the simulation data.
    :param datablock: The data block where the results are.
    """
    # Number of time steps we saved
    timesteps = fin.load_wavepacket_timegrid()
    nrtimesteps = timesteps.shape[0]

    params = fin.load_wavepacket_parameters()
    coeffs = fin.load_wavepacket_coefficients()

    # A data transformation needed by API specification
    coeffs = [ [ coeffs[i,j,:] for j in xrange(pin["ncomponents"]) ] for i in xrange(nrtimesteps) ]

    # The potential
    Potential = PotentialFactory().create_potential(pin)

    # Initialize a mother Hagedorn wavepacket with the data from another simulation
    HAWP = HagedornWavepacket(pin)
    HAWP.set_quadrature(None)

    # Initialize an empty wavepacket for spawning
    SWP = HagedornWavepacket(pout)
    SWP.set_quadrature(None)

    # Initialize a Spawner
    NAS = NonAdiabaticSpawnerKF(pout)

    # Try spawning for these components, if none is given, try it for all.
    if not "spawn_components" in parametersout:
        components = range(pin["ncomponents"])
    else:
        components = parametersout["spawn_components"]

    # Iterate over all timesteps and spawn
    for i, step in enumerate(timesteps):
        print(" Try spawning at timestep "+str(step))

        # Configure the wave packet and project to the eigenbasis.
        HAWP.set_parameters(params[i])
        HAWP.set_coefficients(coeffs[i])

        # Project to the eigenbasis as the parameter estimation
        # has to happen there because of coupling.
        T = HAWP.clone()
        T.project_to_eigen(Potential)

        # Try spawning a new packet for each component
        estps = [ NAS.estimate_parameters(T, component=acomp) for acomp in components ]

        # The quadrature
        quadrature = InhomogeneousQuadrature()

        # Quadrature, assume same quadrature order for both packets
        # Assure the "right" quadrature is choosen if mother and child have
        # different basis sizes
        if max(HAWP.get_basis_size()) > max(SWP.get_basis_size()):
            quadrature.set_qr(HAWP.get_quadrature().get_qr())
        else:
            quadrature.set_qr(SWP.get_quadrature().get_qr())

        for index, ps in enumerate(estps):
            if ps is not None:
                # One choice of the sign
                U = SWP.clone()
                U.set_parameters(ps)
                # Project the coefficients to the spawned packet
                tmp = T.clone()
                NAS.project_coefficients(tmp, U, component=components[index])

                # Other choice of the sign
                V = SWP.clone()
                # Transform parameters
                psm = list(ps)
                B = ps[0]
                Bm = -np.real(B)+1.0j*np.imag(B)
                psm[0] = Bm
                V.set_parameters(psm)
                # Project the coefficients to the spawned packet
                tmp = T.clone()
                NAS.project_coefficients(tmp, V, component=components[index])

                # Compute some inner products to finally determine which parameter set we use
                ou = abs(quadrature.quadrature(T,U, component=components[index]))
                ov = abs(quadrature.quadrature(T,V, component=components[index]))

                # Choose the packet which maximizes the inner product. This is the main point!
                if ou >= ov:
                    U = U
                else:
                    U = V

                # Finally do the spawning, this is essentially to get the remainder T right
                # The packet U is already ok by now.
                NAS.project_coefficients(T, U, component=components[index])

                # Transform back
                if save_canonical is True:
                    T.project_to_canonical(Potential)
                    U.project_to_canonical(Potential)

                # Save the mother packet rest
                fout.save_wavepacket_parameters(T.get_parameters(), timestep=step, blockid=2*index)
                fout.save_wavepacket_coefficients(T.get_coefficients(), timestep=step, blockid=2*index)

                # Save the spawned packet
                fout.save_wavepacket_parameters(U.get_parameters(), timestep=step, blockid=2*index+1)
                fout.save_wavepacket_coefficients(U.get_coefficients(), timestep=step, blockid=2*index+1)
quads1 = []
quads2 = []
quads12 = []

for index, pos in enumerate(positions):
    print(pos)
    # Moving Gaussian
    WP2.set_parameters((1.0j, 1.0, 0.0, 0.0, pos))

    # Transform the nodes
    nodes1 = squeeze(HQ1.transform_nodes(WP1.get_parameters(), WP1.eps))
    nodes2 = squeeze(HQ2.transform_nodes(WP2.get_parameters(), WP2.eps))
    nodes12 = squeeze(IHQ.transform_nodes(Pibra, WP2.get_parameters(), WP1.eps))

    # Compute inner products
    Q1 = IHQ.quadrature(WP1, WP1, summed=True)
    Q2 = IHQ.quadrature(WP2, WP2, summed=True)
    Q12 = IHQ.quadrature(WP1, WP2, summed=True)

    quads1.append(Q1)
    quads2.append(Q2)
    quads12.append(Q12)

    # Evaluate the packets
    y = WP1.evaluate_at(x, prefactor=True, component=0)
    z = WP2.evaluate_at(x, prefactor=True, component=0)

    figure()
    plot(x, conj(y) * y, "b")
    plot(x, conj(z) * z, "g")
    plot(x, conj(y) * z, "r")