Example #1
0
        public DataHeader getHeader()
        {
            // get the header information
            if (header == null)
            {
                header = readHeader(filename);
            }

            // return the header information
            return(header);
        }
Example #2
0
        public void close()
        {
            // threadsafety
            lock (lockReader) {
                // if a datastream is open, close it
                if (dataStream != null)
                {
                    dataStream.Close();
                    dataStream = null;
                }

                // clear the header
                header = null;

                // reset the row index
                currentRowIndex = 0;
            }
        }
Example #3
0
        public bool open()
        {
            // threadsafety
            lock (lockReader) {
                // check if file does not exists
                if (!File.Exists(filename))
                {
                    // message
                    logger.Error("Could not open data file '" + filename + "' for reading");

                    // return failure
                    return(false);
                }
                DataHeader a = getHeader();

                // make sure the header is read
                // return false if the header could not be read
                if (getHeader() == null || string.IsNullOrEmpty(getHeader().code))
                {
                    return(false);
                }

                try {
                    // open file stream
                    dataStream = new FileStream(filename, FileMode.Open);
                } catch (Exception) {
                    // message
                    logger.Error("Could not create filestream to data file '" + filename + "' for reading");

                    // return failure
                    return(false);
                }

                // set the data pointer to the beginning
                resetDataPointer();

                // return success
                return(true);
            }   // end lock
        }
Example #4
0
        public static DataHeader readHeader(String fileName)
        {
            // create a new data header object
            DataHeader header = new DataHeader();

            FileStream fileStream = null;

            try {
                // open file stream
                fileStream = new FileStream(fileName, FileMode.Open);

                // retrieve version number
                byte[] bVersion = new byte[sizeof(int)];
                fileStream.Read(bVersion, 0, sizeof(int));
                header.version = BitConverter.ToInt32(bVersion, 0);

                // check version
                if (header.version != 1 && header.version != 2)
                {
                    throw new Exception("Unknown data version");
                }

                // retrieve the code from the header
                byte[] bCode = new byte[3];
                fileStream.Read(bCode, 0, bCode.Length);
                header.code = Encoding.ASCII.GetString(bCode);

                // retrieve the epochs (V2)
                if (header.version == 2)
                {
                    byte[] bRunStartEpoch = new byte[8];
                    fileStream.Read(bRunStartEpoch, 0, bRunStartEpoch.Length);
                    header.runStartEpoch = BitConverter.ToInt64(bRunStartEpoch, 0);

                    byte[] bFileStartEpoch = new byte[8];
                    fileStream.Read(bFileStartEpoch, 0, bFileStartEpoch.Length);
                    header.fileStartEpoch = BitConverter.ToInt64(bFileStartEpoch, 0);
                }

                // retrieve the sample rate
                byte[] bSampleRate = new byte[8];
                fileStream.Read(bSampleRate, 0, bSampleRate.Length);
                header.sampleRate = BitConverter.ToDouble(bSampleRate, 0);

                // retrieve the number of playback input streams
                byte[] bNumPlaybackStreams = new byte[4];
                fileStream.Read(bNumPlaybackStreams, 0, bNumPlaybackStreams.Length);
                header.numPlaybackStreams = BitConverter.ToInt32(bNumPlaybackStreams, 0);

                // # streams + streams details (V2)
                if (header.version == 2)
                {
                    byte[] bNumStreams = new byte[4];
                    fileStream.Read(bNumStreams, 0, bNumStreams.Length);
                    header.numStreams = BitConverter.ToInt32(bNumStreams, 0);
                    for (int i = 0; i < header.numStreams; i++)
                    {
                        byte[] bStreamDataType = new byte[1];
                        fileStream.Read(bStreamDataType, 0, bStreamDataType.Length);
                        header.streamDataTypes.Add(bStreamDataType[0]);

                        byte[] bStreamSamplesPer = new byte[2];
                        fileStream.Read(bStreamSamplesPer, 0, bStreamSamplesPer.Length);
                        header.streamDataSamplesPerPackage.Add(BitConverter.ToUInt16(bStreamSamplesPer, 0));
                    }
                }

                // retrieve the number of columns
                byte[] bNumColumns = new byte[4];
                fileStream.Read(bNumColumns, 0, bNumColumns.Length);
                header.numColumns = BitConverter.ToInt32(bNumColumns, 0);

                // retrieve the size of the column names
                byte[] bColumnNamesSize = new byte[4];
                fileStream.Read(bColumnNamesSize, 0, bColumnNamesSize.Length);
                header.columnNamesSize = BitConverter.ToInt32(bColumnNamesSize, 0);

                // retrieve the column names from the header
                byte[] bColumnNames = new byte[header.columnNamesSize];
                fileStream.Read(bColumnNames, 0, header.columnNamesSize);
                header.columnNames = Encoding.ASCII.GetString(bColumnNames).Split('\t');



                //
                // calculate worker variables
                //

                // store the position where the data starts (= the current position of the pointer in the stream after reading the header)
                header.posDataStart = fileStream.Position;

                if (header.version == 1)
                {
                    // determine the rowsize based on the header code (src/dat or plugin file)
                    if (header.code == "src" || header.code == "dat")
                    {
                        header.rowSize  = sizeof(uint);                                 // sample id
                        header.rowSize += (header.numColumns - 1) * sizeof(double);     // data
                    }
                    else
                    {
                        header.rowSize = header.numColumns * sizeof(double);            // sampleid <double> + stream values
                    }

                    // determine the number of rows
                    // casting to integer will make numrows round down in case of incomplete rows
                    header.numRows = (fileStream.Length - header.posDataStart) / header.rowSize;
                }
                else if (header.version == 2)
                {
                    // determine the highest number of samples that any data stream in the pipeline would want to log
                    header.maxSamplesStream = 0;
                    for (int i = 0; i < header.numStreams; i++)
                    {
                        if (header.streamDataSamplesPerPackage[i] > header.maxSamplesStream)
                        {
                            header.maxSamplesStream = header.streamDataSamplesPerPackage[i];
                        }
                    }
                }
            } catch (Exception) {
                // close the data stream
                if (fileStream != null)
                {
                    fileStream.Close();
                }

                // return failure
                return(null);
            } finally {
                // close the data stream
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }

            return(header);
        }
Example #5
0
        /**
         * Create header of .dat (plugin or filter) or .src file
         *
         * boolean timing either includes (true) or excludes (false) extra column in header file for storing elapsed time
         **/
        public static void writeBinaryHeader(FileStream writer, DataHeader header)
        {
            // retrieve code into local variable
            string headerCode = header.code;

            // make sure the header code is always 3 characters long
            if (headerCode.Length != 3)
            {
                headerCode = "xxx";
            }

            // determine whether the header is a plugin
            bool isPlugin = !(string.Compare(headerCode, "src") == 0 || string.Compare(headerCode, "dat") == 0);


            //
            // prepare column names
            //

            // variable holding the column names and columncount
            string columnNames = "";
            int    numColumns  = 0;

            // add sample id column
            if (header.version == 1)
            {
                columnNames += "Sample\t";
            }
            else if (header.version == 2)
            {
                columnNames += "SamplePackage\t";
            }
            numColumns++;

            // add elapsed column (when not plugin)
            if (!isPlugin)
            {
                columnNames += "Elapsed_ms\t";
                numColumns++;
            }

            // add stream names to the local variable of that holds all the column names
            columnNames += string.Join("\t", header.columnNames);
            numColumns  += header.columnNames.Length;

            // convert column names to binary
            byte[] columnNamesBinary = Encoding.ASCII.GetBytes(columnNames);


            //
            // write header
            //

            // version
            byte[] versionBinary = BitConverter.GetBytes(header.version);
            writer.Write(versionBinary, 0, versionBinary.Length);

            // code
            byte[] headerCodeBinary = Encoding.ASCII.GetBytes(headerCode);
            writer.Write(headerCodeBinary, 0, headerCodeBinary.Length);

            // epochs
            if (header.version == 2)
            {
                byte[] headerRunStartEpoch = BitConverter.GetBytes(header.runStartEpoch);
                writer.Write(headerRunStartEpoch, 0, headerRunStartEpoch.Length);

                byte[] headerFileStartEpoch = BitConverter.GetBytes(header.fileStartEpoch);
                writer.Write(headerFileStartEpoch, 0, headerFileStartEpoch.Length);
            }

            // sample rate
            byte[] sampleRateBinary = BitConverter.GetBytes(header.sampleRate);
            writer.Write(sampleRateBinary, 0, sampleRateBinary.Length);

            // playback streams
            byte[] numPlaybackInputStreamsBinary = BitConverter.GetBytes(header.numPlaybackStreams);
            writer.Write(numPlaybackInputStreamsBinary, 0, numPlaybackInputStreamsBinary.Length);

            // # streams + streams details (V2)
            if (header.version == 2)
            {
                byte[] numStreamsBinary = BitConverter.GetBytes(header.numStreams);
                writer.Write(numStreamsBinary, 0, numStreamsBinary.Length);
                for (int i = 0; i < header.numStreams; i++)
                {
                    writer.Write(new byte[] { header.streamDataTypes[i] }, 0, 1);
                    byte[] streamDataSamplesBinary = BitConverter.GetBytes(header.streamDataSamplesPerPackage[i]);
                    writer.Write(streamDataSamplesBinary, 0, streamDataSamplesBinary.Length);
                }
            }

            // # columns, column-names byte-length and names
            byte[] numColumnsBinary = BitConverter.GetBytes(numColumns);
            writer.Write(numColumnsBinary, 0, numColumnsBinary.Length);
            byte[] columnNamesLengthBinary = BitConverter.GetBytes(columnNamesBinary.Length);
            writer.Write(columnNamesLengthBinary, 0, columnNamesLengthBinary.Length);
            writer.Write(columnNamesBinary, 0, columnNamesBinary.Length);

            // Note: don't close writer. Data could/will be written after this
        }