View Raw SPL
/*****************************************************************************
*                                                                            *
*   NANRESHAPE.SPL Copyright (C) 2009 DSP Development Corporation            *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:        Randy Race                                                *
*                                                                            *
*   Synopsis:      Reshapes a series into a table based on nan locations     *
*                                                                            *
*   Revisions:     11 Jun 2009  RRR  Creation                                *
*                                                                            *
*****************************************************************************/

#if @HELP_NANRESHAPE

    NANRESHAPE

    Purpose: Converts a series into a table based on NAN values

    Syntax:  NANRESHAPE(t)

              t - A series, the single column input to convert.


    Returns: A multi-column series if nans exist in the input.

    Example:
             W1: {1, 2, 3, nan, nan, 4, 5, 6, nan, nan, nan, 6, 7, 8}
             W2: nanreshape(W1)

             W1 contains the table:

               {{1, 4, 6},
                {2, 5, 7},
                {3, 6, 8}}

    Remarks:
             NANRESHAPE uses the locations of NaN values of a single
             column series to determine column delimiters.  Consecutive
             NaN values are treated as a single column delimiter.

             The delimiters determine the lengths of the resulting
             output columns.

             If the series does not contain any NaNs, the original series 
             is returned.

             If the series contains all NaNs, an empty series is returned.

    See Also:
             Ravel
             Reshape
#endif



/* form a table based on the locations of NaN values */
nanreshape(t)
{
        local nsnidx, clen;

        if (argc < 1) error("nanreshape - input series required");

        /* get location of nan values */
        nanidx = find(isnan(t));

        /* use indices of nans to form columns */
        if (length(nanidx) > 0)
        {
                /* find differences between subsequent indices */
                clen = lderiv(nanidx);

                /* unique column lengths are where the differences > 1 */
                clen = delete(clen, clen == 1);

                /* use nan locations to reshape the series into a table */
                if (length(clen) > 0) 
                {
                        /* remove nans */
                        t = removena(t);

                        /* form table */
                        t = reshape(t, clen - 1);
                }
                else
                {
                        /* series was all nan - return empty series */
                        t = {};
                }
        }
        
        return(t);
}