protected void TALPrereader(object data)
        {
            TALPrereadError.Clear();
            int  currentBlockIndex = 0;
            bool finished          = false;

            try
            {
                while (!finished && !TALPrereadError.Signaled)
                {
                    // Get all TAL blocks one-by-one; this will read data from file if not available
                    TAL.LastError = TalError.None;
                    TAL.Block(currentBlockIndex); // read the data, ignore the result
                    currentBlockIndex++;
                    finished = (currentBlockIndex >= FileInfo.NrDataRecords);
                    if ((TAL.LastError & TalError.BlockOrder) == TalError.BlockOrder)
                    {
                        TALPrereadError.Add(EdfConstants.DataRecordsNotOrdered, EdfConstants.ErrorIdEdfPlusBlockOrder);
                    }
                }
            }
            catch (ThreadAbortException)
            {
                // Thread was stopped
            }
            catch (Exception ex)
            {
                TALPrereadError.Add(ex.Message, ErrorMessage.EIdException);
            }
            DoHandlePrereadTALsFinished(this);
        }
        public bool WriteModifiedTALBlocks()
        {
            if (OpenReadOnly)
            {
                throw new ArgumentException(EdfConstants.FileIsReadOnly);
            }
            if (!Active)
            {
                return(false);
            }
            int  i       = 0;
            bool writeOk = true;

            if (AnnotationSignalNrs.Count > 0)
            {
                while (writeOk && (i < _tal.BlockCount))
                {
                    EdfPlusAnnotationDataBlock talBlock;
                    if (TAL.BlockInMemory(i))
                    {
                        talBlock = TAL.Block(i);
                        if (talBlock.Modified)
                        {
                            WriteTALs(this, talBlock.DataRecNr, ref writeOk);
                        }
                    }
                    i++;
                }
            }
            return(writeOk);
        }
        protected int ReadTALs(object sender, int blockNr, ref bool readOk)
        {
            int result = -1;

            if (Active)
            {
                if ((blockNr >= 0) && (blockNr < FileInfo.NrDataRecords))
                {
                    try
                    {
                        // Add a new block to TAL if necessary, set onset to NAN
                        TAL.AddBlock(blockNr, double.NaN); // To avoid recursive calls by ReadDataBlock
                        DoReadDataBlock(blockNr, ref _talDataBuffer);
                        DoReadTALs(blockNr, _talDataBuffer, ref readOk);
                        result = blockNr;
                    }
                    catch { }
                }
                if (result < 0)
                {
                    readOk = false;
                    result = FileInfo.NrDataRecords;
                }
            }
            return(result);
        }
 protected void DoReadTALs(int blockNr, short[] buffer, ref bool readOk)
 {
     readOk = true;
     for (int i = 0; i < AnnotationSignalNrs.Count; i++)
     {
         readOk = TAL.ReadFromBuffer(blockNr, ref buffer,
                                     SignalInfo[AnnotationSignalNrs[i]].BufferOffset,
                                     SignalInfo[AnnotationSignalNrs[i]].NrSamples * sizeof(short), i);
     }
 }
 public override void ReadDataBlock(int blockNr)
 {
     base.ReadDataBlock(blockNr);
     Debug.Assert(DataBuffer != null, "Object not initialised");
     if (!DoReadDataBlock(blockNr, ref _dataBuffer))
     {
         throw new DataFileReadDataException(DataFileConsts.DataFileReadData);
     }
     if (!TAL.BlockInMemory(blockNr))
     {
         bool b = true;
         DoReadTALs(blockNr, DataBuffer, ref b);
         if (!b)
         {
             throw new DataFileReadDataException(DataFileConsts.DataFileErrorReadingTAL);
         }
     }
 }
 public void WriteDataBlock(int blockNr, double onset)
 {
     base.WriteDataBlock(blockNr);
     Debug.Assert(DataBuffer == null, "Object is not initialized");
     if (blockNr == 0)
     {
         if ((onset < 0) || (onset >= 1))
         {
             throw new DataFileInvalidBlock0OnsetException(DataFileConsts.DataFileInvalidBlock0Onset);
         }
         FileInfo.FirstDataBlockOnset = onset;
     }
     if (FileInfo.SignalDataIsContinuous && (FileInfo.FirstDataBlockOnset + blockNr * FileInfo.SampleRecDuration != onset))
     {
         throw new DataFileContinuousException(DataFileConsts.DataFileContinuous);
     }
     TAL.AddBlock(blockNr, onset); // overwrites offset if block already exists
     // Write data to file
     if (!DoWriteDataBlock(blockNr, DataBuffer))
     {
         throw new DataFileWriteDataException(DataFileConsts.DataFileWriteData);
     }
 }
Beispiel #7
0
 public override void WriteDataBlock(int blockNr)
 {
     base.WriteDataBlock(blockNr);
     Debug.Assert(DataBuffer != null, "Object is not initialized");
     // Update TAL list
     if (!TAL.BlockInMemory(blockNr))
     {
         if (!FileInfo.SignalDataIsContinuous)
         {
             throw new DataFileNotContinuousException(DataFileConsts.DataFileNotContinuous);
         }
         TAL.AddBlock(blockNr, blockNr * FileInfo.SampleRecDuration);
         if (blockNr == 0)
         {
             FileInfo.FirstDataBlockOnset = 0;
         }
     }
     // Write data to file
     if (!DoWriteDataBlock(blockNr, DataBuffer))
     {
         throw new DataFileWriteDataException(DataFileConsts.DataFileWriteData);
     }
 }
 protected void WriteTALs(object sender, int blockNr, ref bool writeOk)
 {
     if ((blockNr >= 0) && (blockNr <= FileInfo.NrDataRecords))
     {
         try
         {
             writeOk = true;
             if (blockNr < FileInfo.NrDataRecords)
             {
                 DoReadDataBlock(blockNr, ref _talDataBuffer);
             }
             else
             {
                 _talDataBuffer = CreateDataBuffer();
             }
             for (int i = 0; i < AnnotationSignalNrs.Count; i++)
             {
                 writeOk = TAL.WriteToBuffer(
                     blockNr, ref _talDataBuffer, SignalInfo[AnnotationSignalNrs[i]].BufferOffset,
                     SignalInfo[AnnotationSignalNrs[i]].NrSamples * sizeof(short), i);
             }
             if (writeOk)
             {
                 DoWriteDataBlock(blockNr, _talDataBuffer);
                 TAL.Block(blockNr).Modified = false;
             }
         }
         catch
         {
             writeOk = false;
         }
     }
     else
     {
         writeOk = false;
     }
 }
        protected override void DoOpenFile()
        {
            _tal.Clear();
            base.DoOpenFile();
            // Extended DataRecordDuration valid check
            // DataRecDuration can be '0' if either:
            //   1) Each ordinary signal only 1 sample/block and file is discontinuous
            //   2) Only time stamped event signals
            if (ValidFormat && (FileInfo.SampleRecDuration == 0))
            {
                int count = 0;
                int i     = 0;
                while (ValidFormat && (i < FileInfo.NrSignals))
                {
                    if (!((EdfPlusSignalInfo)SignalInfo[i]).IsAnnotationSignal)
                    {
                        count++;
                        if (SignalInfo[i].NrSamples > 1)
                        {
                            ValidFormat = false;
                        }
                    }
                    i++;
                }
                if (ValidFormat && (count > 0) && FileInfo.SignalDataIsContinuous)
                {
                    ValidFormat = false;
                }
                if (!ValidFormat)
                {
                    FileInfo.FieldValid[(int)EdfFileInfoBase.Field.SampleRecDuration] = false;
                }
            }
            if (ValidFormat)
            {
                TAL.Clear();
                TAL.BlockDuration = FileInfo.SampleRecDuration;
                TAL.SetBlocksListCapacity(FileInfo.NrDataRecords);
                GetAnnotationsSignalNrs();
                bool readOk = true;
                // Always preread first and last block (for getting file time)
                ReadTALs(this, 0, ref readOk);
                if (readOk && (FileInfo.NrDataRecords > 1))
                {
                    ReadTALs(this, FileInfo.NrDataRecords - 1, ref readOk);
                }
                if (readOk)
                {
                    if ((TAL.Block(0).DataRecOnset < 0) || (TAL.Block(0).DataRecOnset >= 1))
                    {
                        HeaderError |= EdfHeaderErrorType.EdfPlusAnnotationFirstOffsetError;
                    }
                    try
                    {
                        FileInfo.FirstDataBlockOnset = TAL.Block(0).DataRecOnset;
                    }
                    catch
                    {
                    }
                    // Use the fileheader startdate/time, not the startdate/time of the first datablock!
                    TAL.FileStartDateTime = FileInfo.StartDateTime.HasValue ? new HPDateTime(FileInfo.StartDateTime.Value) : new HPDateTime();
                    switch (PrereadTALs)
                    {
                    case PrereadTALs.None:
                        break;

                    case PrereadTALs.OnOpenFile:
                        TALPrereader(null);
                        DoHandlePrereadTALsFinished(this);
                        break;

                    case PrereadTALs.UseThread:
                        StartTALsPrereadThread();
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    ValidFormat  = false;
                    HeaderError |= EdfHeaderErrorType.EdfPlusAnnotationSignalError;
                }
            }
        }