#This is a raw, inline version of the loess filtering function. Runs much more slowlier.

def loess_inline(h,x,cut,nan=True):
    """
    #This is a raw, inline version of the loess filtering function. Runs much more slowlier.
    """
    
    n=np.size(h)

    #Start filtering if there are more than 1 point
    if n == 1 : lf = h[0]
    else : pass
    
    #Get the half-width of filter window
    l_c=cut/2.0          
    
    #Initiate weights and outputs
    w=np.zeros(n)
    lf = np.repeat(np.NaN,n)
    
    #Get valid points in serie
    flag = ~np.isnan(h)
    fcnt = flag.sum()
    fnt = np.arange(n).compress(flag)
#        WHERE(FINITE(h),fcnt)

    #Loop from 1st to last valid point
    for i in fnt :

        icur=i
      
        #Get Q (distance from central point divided by half-window width)
        q=(np.abs((x-x[icur])/l_c))
            
        #Compute S from Q�
        s = (1.0 - q*q*q)
        
     
        #Set all values out of filter window to 0
        outOfFilter_flag = q > 1.0
        outCnt = outOfFilter_flag.sum() 
        outOfFilter = np.arange(n).compress(outOfFilter_flag)
        if (outCnt > 0) : s[outOfFilter]=0.0
      
        #Compute weights (S� - Tricube)
        w=s*s*s  
        sumvar=np.nansum(w)

        #Compute current height (heights times weigths, divided by sum of weights)
        lf[icur]=np.nansum(w*h)/sumvar

    return lf