View Raw SPL
/*****************************************************************************
*                                                                            *
*   READTB.SPL   Copyright (C) 1997 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Read a binary "table" from a file                           *
*                                                                            *
*   Revisions:    7 Jun 1997  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#include 

#define REAL_TABLE    -101
#define COMPLEX_TABLE -202

#if @HELP_READTB

    READTB

    Purpose: Reads a binary table

    Syntax:  Readt("filename")

              filename - a string, name of file in quotes

    Returns: A series or array

    Example:
             Writetb("bin.dat", SINT, {{1, 2, 3}, {4, 5, 6}});
             mydata = Readtb("bin.dat");

             writes the 2x3 array

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

             to the file bin.dat as signed integers and reads the
             array into the variable mydata.


    Remarks:
             Readbt does not currently handle DELTAX, XOFFSET and units.


    See Also
             Readb
             Reada
             Writea
             Writeb
             Writetb

#endif



/* read a binary table */
readtb(fname)
{
        local nc, nr, table, tag;

        if (argc < 1)
        {
                error("readtb - filename required");
        }

        if (fopen(fname, "rb") != TRUE)
        {
                error(sprintf("readtb - cannot open %s", fname));
        }

        /* get tag value */
        tag = castint(freadb(fname, SINT, 1));

        if (tag != COMPLEX_TABLE && tag != REAL_TABLE)
        {
                fclose(fname);
                error(sprintf("readtb - '%s' not a binary table file", fname));
        }

        /* get total number of columns */
        nc = castint(freadb(fname, SINT, 1));

        /* read all column lengths */
        nr = freadb(fname, SINT, nc);

        /* get data type */
        type = castint(freadb(fname, SINT, 1));

        /* now read the data and close the file */
        table = freadb(fname, type, -1);
        fclose(fname);

        /* format if complex - de-multiplex */
        if (tag == COMPLEX_TABLE)
        {
                table = decimate(table, 2) + i * decimate(table, 2, 2);
                setdeltax(table, 1.0);
        }

        /* reshape table to original shape */
        table = reshape(table, nr);

        return(table, tag);
}