Intelligent segmentation of data.
    
    :keyword remove_edges: discard data at track edges.
    :keyword truncate_if_continents: Force truncating data if a continent is found within a segment of data.
    
    :keyword last: Get segments of data sticked to the last point in track
    :keyword first: Get segments of data sticked to the first point in track
    :keyword mid: Get segments of data sticked to the middle point in track

def get_segment(sla,N,last=True,mid=None,first=None,remove_edges=True,truncate_if_continents=True):
    '''
    Intelligent segmentation of data.
    
    :keyword remove_edges: discard data at track edges.
    :keyword truncate_if_continents: Force truncating data if a continent is found within a segment of data.
    
    :keyword last: Get segments of data sticked to the last point in track
    :keyword first: Get segments of data sticked to the first point in track
    :keyword mid: Get segments of data sticked to the middle point in track

    '''
    #Set defaults
    if first is not None :
        last=None
        mid=None
    elif mid is not None :
        last=None
        first=None
    
    dumsla=sla.copy()
    nx=sla.shape[1]
    nt=sla.shape[0]
    
    #Save input mask
    dumsla.data[dumsla.mask]=dumsla.fill_value
    
    if len(dumsla.mask.shape) > 0:
        mask=np.ma.array(dumsla.mask.copy(),mask=np.zeros(sla.shape,dtype=bool))
    else:
        mask=np.array([dumsla.mask]*sla.size).reshape(sla.shape)
        mask=np.ma.array(mask,mask=np.zeros(sla.shape,dtype=bool))
        dumsla.mask=mask#np.array([dumsla.mask]*sla.size).reshape(sla.shape)
    
    #~ dumsla.mask[:]=False
    
    #Get edges
    if remove_edges : xid=np.ma.array(np.repeat(np.arange(nx),nt).reshape(nx,nt).transpose(),mask=mask.data)
    else : xid=np.ma.array(np.repeat(np.arange(nx),nt).reshape(nx,nt).transpose(),mask=np.zeros(sla.shape,dtype=bool))
    left=xid.min(axis=1)
    right=xid.max(axis=1)
    
    #Shift towards end
    if last :
        st=(right-N).astype(int) +1
        en=(right).astype(int) + 1
    elif mid :
        midpt=nx/2
        rlag=right-midpt
        llag=midpt-left
        odd = np.int(N)&1 and True or False
        if not odd : nr=nl=np.int(N)/2
        else :
            nr=np.int(N)/2 + 1
            nl=np.int(N)/2
#         for i,jk in enumerate(zip(*(llag,rlag))):
#             j,k=jk
#             st=0
        st=np.repeat(midpt-nl,nt)
        en=np.repeat(midpt+nr,nt)
    elif first :
        st=(left).astype(int)
        en=(left+N).astype(int)
    
    if not remove_edges :
        st[st < 0] = 0
        en[en > nx] = nx
    
    for i in np.arange(nt) :
        dumsla.mask[i,:st[i]]=True
        dumsla.mask[i,en[i]:]=True
        mask.mask[i,:st[i]]=True
        mask.mask[i,en[i]:]=True
    
    #Update nt
    cycempty=dumsla.mask.sum(axis=1) == N
    ind=np.arange(nt)[~cycempty]
    nt=(~cycempty).sum()
    
    #Reform stuff
    dumsla=dumsla.compressed().reshape(nt,N)
    mask=mask.compressed().reshape(nt,N)
    
    if truncate_if_continents :
        empty=mask.sum(axis=0) == nt
        if empty.sum() > 0 :
            dumsla=dumsla[:,~empty]
            mask=mask[:,~empty]
            print '[WARNING] Points over land mass - removed {} pts'.format(empty.sum())
    
    return np.ma.array(dumsla,mask=mask), ind