def extrap1d(Z,mask):
    """    
    EXTRAP1D : Extrapolate values from a 1D vector at its beginning and end using reversal of this array
    
    @note : gaps in vector Z should be filled first as values from the vector are replicated out of edges
    @note : if isinstance(Z,np.ma.masked_array) : mask = Z.mask
    
    @param Z: 1D vector to extrapolate
    @param mask: mask flag of the vector 
    
    @author: Renaud DUSSURGET, LER/PAC, Ifremer La Seyne
    """
    Zout=Z.copy()
    N=len(Zout)
    ind=np.arange(N)
    xout=ind[mask]
    hist=(~mask).astype(int)
    dhist=hist[1:]-hist[:-1]
    st=ind[dhist==1]+1
    if len(st) > 0 :
        st=st[0]
        Zout[:st]=Z[st] - (np.roll(Z,-st-1)[:st][::-1] - Z[st])
    en=ind[dhist==-1]
    if len(en) > 0 :
        en=en[-1]
        Zout[en+1:]=Z[en] - (np.roll(Z,-en)[::-1][:N-en-1] - Z[en])
    return Zout