def METHOD_NAME(vec, Xh):
    """ converts a petsc Vec object to a StencilVector or a BlockVector format.
        We gather the petsc global vector in all the processes and extract the chunk owned by the Psydac Vector.
        .. warning: This function will not work if the global vector does not fit in the process memory.
    """
    if isinstance(Xh, BlockVectorSpace):
        u = BlockVector(Xh)
        if isinstance(Xh.spaces[0], BlockVectorSpace):
            comm       = u[0][0].space.cart.global_comm
            dtype      = u[0][0].space.dtype
            sendcounts = np.array(comm.allgather(len(vec.array)))
            recvbuf    = np.empty(sum(sendcounts), dtype=dtype)
            # gather the global array in all the procs
            comm.Allgatherv(sendbuf=vec.array, recvbuf=(recvbuf, sendcounts))
            inds = 0
            for d in range(len(Xh.spaces)):
                starts = [np.array(V.starts) for V in Xh.spaces[d].spaces]
                ends   = [np.array(V.ends)   for V in Xh.spaces[d].spaces]
                for i in range(len(starts)):
                    idx = tuple( slice(m*p,-m*p) for m,p in zip(u.space.spaces[d].spaces[i].pads, u.space.spaces[d].spaces[i].shifts) )
                    shape = tuple(ends[i]-starts[i]+1)
                    npts  = Xh.spaces[d].spaces[i].npts
                    # compute the global indices of the coefficents owned by the process using starts and ends
                    indices = np.array([np.ravel_multi_index( [s+x for s,x in zip(starts[i], xx)], dims=npts,  order='C' ) for xx in np.ndindex(*shape)] )
                    vals = recvbuf[indices+inds]
                    u[d][i]._data[idx] = vals.reshape(shape)
                    inds += np.product(npts)
        else:
            comm       = u[0].space.cart.global_comm
            dtype      = u[0].space.dtype
            sendcounts = np.array(comm.allgather(len(vec.array)))
            recvbuf    = np.empty(sum(sendcounts), dtype=dtype)
            # gather the global array in all the procs
            comm.Allgatherv(sendbuf=vec.array, recvbuf=(recvbuf, sendcounts))
            inds = 0
            starts = [np.array(V.starts) for V in Xh.spaces]
            ends   = [np.array(V.ends)   for V in Xh.spaces]
            for i in range(len(starts)):
                idx = tuple( slice(m*p,-m*p) for m,p in zip(u.space.spaces[i].pads, u.space.spaces[i].shifts) )
                shape = tuple(ends[i]-starts[i]+1)
                npts  = Xh.spaces[i].npts
                # compute the global indices of the coefficents owned by the process using starts and ends
                indices = np.array([np.ravel_multi_index( [s+x for s,x in zip(starts[i], xx)], dims=npts,  order='C' ) for xx in np.ndindex(*shape)] )
                vals = recvbuf[indices+inds]
                u[i]._data[idx] = vals.reshape(shape)
                inds += np.product(npts)
    elif isinstance(Xh, StencilVectorSpace):
        u          = StencilVector(Xh)
        comm       = u.space.cart.global_comm
        dtype      = u.space.dtype
        sendcounts = np.array(comm.allgather(len(vec.array)))
        recvbuf    = np.empty(sum(sendcounts), dtype=dtype)
        # gather the global array in all the procs
        comm.Allgatherv(sendbuf=vec.array, recvbuf=(recvbuf, sendcounts))
        # compute the global indices of the coefficents owned by the process using starts and ends
        starts = np.array(Xh.starts)
        ends   = np.array(Xh.ends)
        shape  = tuple(ends-starts+1)
        npts   = Xh.npts
        indices = np.array([np.ravel_multi_index( [s+x for s,x in zip(starts, xx)], dims=npts,  order='C' ) for xx in np.ndindex(*shape)] )
        idx = tuple( slice(m*p,-m*p) for m,p in zip(u.space.pads, u.space.shifts) )
        vals = recvbuf[indices]
        u._data[idx] = vals.reshape(shape)
    else:
        raise ValueError('Xh must be a StencilVectorSpace or a BlockVectorSpace')
    u.update_ghost_regions()
    return u