# FILL_GAPS
    # @summary: This function allow interpolating data in gaps, depending on gap size. Data must be regularly gridded
    # @param lat {type:numeric} : latitude
    # @param lon {type:numeric} : longitude
    # @param sla {type:numeric} : data
    # @return:
    #    outdst : resampled distance
    #    outlon : resampled longitude
    #    outlat : resampled latitude
    #    outsla : resampled data
    #    gaplen : length of the longest gap in data
    #    ngaps : number of detected gaps in data
    #    dx : average spatial sampling
    #    interpolated : True when data was interpolated (empty bin)
    #
    # @author: Renaud DUSSURGET (RD) - LER/PAC, Ifremer
    # @change: Created by RD, July 2012
    #    29/08/2012 : Major change -> number of output variables changes (added INTERPOLATED), and rebinning modified
    #    06/11/2012 : Included in alti_tools lib

def fill_gaps(lat,lon,sla,mask,remove_edges=False):
    """
    # FILL_GAPS
    # @summary: This function allow interpolating data in gaps, depending on gap size. Data must be regularly gridded
    # @param lat {type:numeric} : latitude
    # @param lon {type:numeric} : longitude
    # @param sla {type:numeric} : data
    # @return:
    #    outdst : resampled distance
    #    outlon : resampled longitude
    #    outlat : resampled latitude
    #    outsla : resampled data
    #    gaplen : length of the longest gap in data
    #    ngaps : number of detected gaps in data
    #    dx : average spatial sampling
    #    interpolated : True when data was interpolated (empty bin)
    #
    # @author: Renaud DUSSURGET (RD) - LER/PAC, Ifremer
    # @change: Created by RD, July 2012
    #    29/08/2012 : Major change -> number of output variables changes (added INTERPOLATED), and rebinning modified
    #    06/11/2012 : Included in alti_tools lib
    """
    
    dst=calcul_distance(lat,lon)
    
    #Find gaps in data
    dx = dst[1:] - dst[:-1]
    mn_dx = np.median(dx)
    nx=len(sla)

    flag=~mask
    
    #Get filled bins indices
    outsla = sla.copy()
    outlon = lon.copy()
    outlat = lat.copy()
    outind = np.arange(nx)
    
    #Replace missing data on edges by the latest valid point
    first=np.where((flag))[0].min()
    last=np.where((flag))[0].max()
    if remove_edges :
        outsla=outsla[first:last+1]
        outlon=outlon[first:last+1]
        outlat=outlat[first:last+1]
        outind=outind[first:last+1]
        mask=mask[first:last+1]
        flag=flag[first:last+1]
    else:
        outsla[0:first] = outsla[first]
        outsla[last:] = outsla[last]
    
    #Get gap properties
    hist=np.ones(nx,dtype=int)
    hist[outsla.mask]=0
    while hist[0] == 0 : 
        hist=np.delete(hist,[0])
    while hist[-1] == 0 :
        hist=np.delete(hist,[len(hist)-1])
    
    ind=np.arange(len(hist))
    dhist=(hist[1:] - hist[:-1])
    st=ind.compress(dhist==-1)+1
    en=ind.compress(dhist==1)
    gaplen=(en-st) + 1
    ngaps=len(st)
    gapedges=np.array([st,en])
    
    
    ok = np.where(flag)[0]
    empty = np.where(mask)[0] 
    
    #Fill the gaps if there are some
    if len(empty) > 0 : 
        #Interpolate lon,lat @ empty positions
        outsla[empty] = interp1d(ok, outsla[ok], empty)
    
    #Get empty bin flag
    interpolated=~hist.astype('bool')
    
    return outsla, outlon, outlat, outind, ngaps, gapedges, gaplen, interpolated