Example #1
0
        public static void FlushFile(hid_t file, bool global = true)
        {
            var scope = global ? H5F.scope_t.GLOBAL : H5F.scope_t.LOCAL;

            if (H5F.flush(file, scope) < 0)
            {
                throw new HDF5Exception("Failed to flush file.");
            }
        }
Example #2
0
 public void H5FflushTest1()
 {
     Assert.IsTrue(
         H5F.flush(m_v0_class_file, H5F.scope_t.GLOBAL) >= 0);
     Assert.IsTrue(
         H5F.flush(m_v2_class_file, H5F.scope_t.GLOBAL) >= 0);
     Assert.IsTrue(
         H5F.flush(m_v0_class_file, H5F.scope_t.LOCAL) >= 0);
     Assert.IsTrue(
         H5F.flush(m_v2_class_file, H5F.scope_t.LOCAL) >= 0);
 }
Example #3
0
        public override void Serialize(Epoch e, string fileTag)
        {
            if (!e.StartTime)
            {
                throw new PersistanceException("Epoch must have a start time.");
            }

            if (CurrentEpochGroupID == null)
            {
                throw new InvalidOperationException("Cannot serialize an Epoch without an open EpochGroup. Call BeginEpochGroup before attempting to serialize an Epoch.");
            }

            var       groupName = "epoch-" + e.ProtocolID + "-" + GuidGenerator();
            H5GroupId epochID   = H5G.create(CurrentEpochGroupID.EpochsId, groupName); //epochGroupMetadata

            log.DebugFormat("Serializing Epoch to HDF5 {0}", groupName);

            WriteAttribute(epochID, "protocolID", e.ProtocolID);

            // WriteEpochStart and duration
            WriteAttribute(epochID, startTimeOffsetName, ((DateTimeOffset)e.StartTime).Offset.TotalHours); //TimeZone.CurrentTimeZone.GetUtcOffset(((DateTimeOffset)e.StartTime).DateTime).TotalHours);
            WriteAttribute(epochID, startTimeUtcName, ((DateTimeOffset)e.StartTime).Ticks);
            WriteAttribute(epochID, "durationSeconds", ((TimeSpan)e.Duration).TotalSeconds);
            if (fileTag != null)
            {
                WriteAttribute(epochID, "fileTag", fileTag);
            }

            //Write keywords
            WriteKeywords(epochID, e.Keywords);

            // WriteBackground
            Write(epochID, "background", e.Background);

            // WriteProtocolParams
            WriteDictionary(epochID, "protocolParameters", e.ProtocolParameters);

            // WriteStimulus
            WriteStimuli(epochID, "stimuli", e.Stimuli);

            // WriteResponse
            WriteResponses(epochID, "responses", e.Responses);


            H5G.close(epochID);

            H5F.flush(fileId);
        }
Example #4
0
        protected override void WriteEpochGroupEnd(DateTimeOffset endTime)
        {
            if (CurrentEpochGroupID == null)
            {
                throw new InvalidOperationException("There is no open EpochGroup.");
            }

            var epochGroup = EpochGroupsIDs.Pop();

            WriteAttribute(epochGroup.GroupId, endTimeUtcName, endTime.UtcTicks);
            WriteAttribute(epochGroup.GroupId, endTimeOffsetName, endTime.Offset.TotalHours);//TimeZone.CurrentTimeZone.GetUtcOffset(endTime.DateTime).TotalHours);

            H5G.close(epochGroup.SubGroupsId);
            H5G.close(epochGroup.EpochsId);
            H5G.close(epochGroup.GroupId);

            H5F.flush(fileId);
        }
Example #5
0
        protected override void OnWrite(ChannelContextGroup contextGroup, ulong fileOffset, ulong bufferOffset, ulong length)
        {
            long groupId = -1;

            try
            {
                var firstChunk = (long)this.ToChunkIndex(fileOffset, contextGroup.SampleRate);
                var lastChunk  = (long)this.ToChunkIndex(fileOffset + length, contextGroup.SampleRate) - 1;

                groupId = H5G.open(_fileId, $"/info");

                _lastCompletedChunk = IOHelper.ReadDataset <double>(groupId, "last_completed_chunk").FirstOrDefault();

                // this does not work in conjunction with multiple context groups
                if (firstChunk <= _lastCompletedChunk)
                {
                    // throw new Exception(ErrorMessage.Mat73Writer_ChunkAlreadyWritten);
                }

                // write data
                for (int i = 0; i < contextGroup.ChannelContextSet.Count(); i++)
                {
                    this.WriteData(fileOffset, bufferOffset, length, contextGroup.ChannelContextSet[i]);
                }

                // write last_completed_chunk
                IOHelper.WriteDataset(groupId, "last_completed_chunk", new double[] { lastChunk });
            }
            finally
            {
                if (H5I.is_valid(groupId) > 0)
                {
                    H5G.close(groupId);
                }

                H5F.flush(_fileId, H5F.scope_t.GLOBAL);
            }
        }
Example #6
0
        protected override bool OnWaitForUserInput()
        {
            if (this.SelectedIndex < 0)
            {
                this.SelectedIndex = _vdsMetaAggregateFunctionSet.Count() > 0 ? 0 : -1;
            }
            else if (this.SelectedIndex >= _vdsMetaAggregateFunctionSet.Count())
            {
                this.SelectedIndex = _vdsMetaAggregateFunctionSet.Count() - 1;
            }

            if (this.LastIndex >= 0)
            {
                Console.SetCursorPosition(1, this.LastIndex);
                Console.Write(" ");
            }

            if (this.SelectedIndex >= 0)
            {
                Console.SetCursorPosition(1, this.SelectedIndex + 2);
                Console.Write("x");
            }

            _consoleKeyInfo = Console.ReadKey(true);

            switch (_consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:

                if (this.SelectedIndex > 0)
                {
                    this.LastIndex      = Console.CursorTop;
                    this.SelectedIndex -= 1;
                }

                break;

            case ConsoleKey.DownArrow:

                if (this.SelectedIndex < _vdsMetaAggregateFunctionSet.Count() - 1)
                {
                    this.LastIndex      = Console.CursorTop;
                    this.SelectedIndex += 1;
                }

                break;

            case ConsoleKey.Enter:
            case ConsoleKey.RightArrow:

                if (this.SelectedIndex >= 0)
                {
                    _vdsMetaAggregateFunctionSet[this.SelectedIndex] = Program.PromptAggregateFunctionData(_vdsMetaAggregateFunctionSet[this.SelectedIndex]);
                    _updateAttribute = true;
                }

                break;

            case ConsoleKey.N:

                _vdsMetaAggregateFunctionSet.Add(Program.PromptAggregateFunctionData(new hdf_aggregate_function_t()));

                this.SelectedIndex = _vdsMetaAggregateFunctionSet.Count();
                _updateAttribute   = true;

                break;

            case ConsoleKey.C:

                if (this.SelectedIndex >= 0)
                {
                    _vdsMetaAggregateFunctionSet.Add(_vdsMetaAggregateFunctionSet[this.SelectedIndex]);
                    this.SelectedIndex = _vdsMetaAggregateFunctionSet.Count();
                    this.OnRedraw();
                }

                break;

            case ConsoleKey.D:

                _vdsMetaAggregateFunctionSet.Add(new hdf_aggregate_function_t("mean", "none"));
                _vdsMetaAggregateFunctionSet.Add(new hdf_aggregate_function_t("min", "none"));
                _vdsMetaAggregateFunctionSet.Add(new hdf_aggregate_function_t("max", "none"));
                _vdsMetaAggregateFunctionSet.Add(new hdf_aggregate_function_t("std", "none"));
                _updateAttribute = true;
                this.OnRedraw();

                break;

            case ConsoleKey.Delete:

                if (this.SelectedIndex >= 0)
                {
                    Console.Clear();
                    Console.Write("The selected item will be deleted. Proceed (Y/N)? ");

                    if (Console.ReadKey().Key == ConsoleKey.Y)
                    {
                        _vdsMetaAggregateFunctionSet.RemoveAt(this.SelectedIndex);
                        _updateAttribute = true;
                    }

                    this.OnRedraw();
                }

                break;

            case ConsoleKey.PageUp:

                if (this.SelectedIndex >= 1 && this.SelectedIndex < _vdsMetaAggregateFunctionSet.Count())
                {
                    hdf_aggregate_function_t hdf_aggregate_function = _vdsMetaAggregateFunctionSet[this.SelectedIndex];
                    _vdsMetaAggregateFunctionSet.RemoveAt(this.SelectedIndex);
                    _vdsMetaAggregateFunctionSet.Insert(this.SelectedIndex - 1, hdf_aggregate_function);
                    this.SelectedIndex -= 1;
                    _updateAttribute    = true;
                }

                break;

            case ConsoleKey.PageDown:

                if (this.SelectedIndex >= 0 && this.SelectedIndex < _vdsMetaAggregateFunctionSet.Count() - 1)
                {
                    hdf_aggregate_function_t hdf_aggregate_function = _vdsMetaAggregateFunctionSet[this.SelectedIndex];
                    _vdsMetaAggregateFunctionSet.RemoveAt(this.SelectedIndex);
                    _vdsMetaAggregateFunctionSet.Insert(this.SelectedIndex + 1, hdf_aggregate_function);
                    this.SelectedIndex += 1;
                    _updateAttribute    = true;
                }

                break;

            case ConsoleKey.Escape:
            case ConsoleKey.LeftArrow:
                return(true);
            }

            // update attribute
            if (_updateAttribute)
            {
                _groupId = IOHelper.OpenOrCreateGroup(_vdsMetaFileId, _currentPath).GroupId;
                IOHelper.PrepareAttribute(_groupId, "aggregate_function_set", _vdsMetaAggregateFunctionSet.ToArray(), new ulong[] { (ulong)_vdsMetaAggregateFunctionSet.Count() }, true);
                _updateAttribute = false;
                this.OnRedraw();
            }

            // clean up
            if (H5I.is_valid(_groupId) > 0)
            {
                H5F.flush(_groupId, H5F.scope_t.GLOBAL);
                H5G.close(_groupId);
            }

            return(false);
        }
Example #7
0
        protected override bool OnWaitForUserInput()
        {
            _totalCount = _vdsTransferFunctionSet.Count() + _vdsMetaTransferFunctionSet.Count();

            if (this.SelectedIndex < 0)
            {
                this.SelectedIndex = _totalCount > 0 ? 0 : -1;
            }
            else if (this.SelectedIndex >= _totalCount)
            {
                this.SelectedIndex = _totalCount - 1;
            }

            if (this.SelectedIndex >= _vdsTransferFunctionSet.Count())
            {
                _offset = 3;
            }
            else
            {
                _offset = 0;
            }

            if (this.LastIndex >= 0)
            {
                Console.SetCursorPosition(1, this.LastIndex);
                Console.Write(" ");
            }

            if (this.SelectedIndex >= 0)
            {
                Console.SetCursorPosition(1, this.SelectedIndex + _offset + 4);
                Console.Write("x");
            }

            _consoleKeyInfo = Console.ReadKey(true);

            switch (_consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:

                if (this.SelectedIndex > 0)
                {
                    this.LastIndex      = Console.CursorTop;
                    this.SelectedIndex -= 1;
                }

                break;

            case ConsoleKey.DownArrow:

                if (this.SelectedIndex < _totalCount - 1)
                {
                    this.LastIndex      = Console.CursorTop;
                    this.SelectedIndex += 1;
                }

                break;

            case ConsoleKey.Enter:
            case ConsoleKey.RightArrow:

                if (this.SelectedIndex >= _vdsTransferFunctionSet.Count())
                {
                    int index = this.SelectedIndex - _vdsTransferFunctionSet.Count();
                    _vdsMetaTransferFunctionSet[index] = Program.PromptTransferFunctionData(_vdsMetaTransferFunctionSet[index]);
                    _updateAttribute = true;
                }

                break;

            case ConsoleKey.N:

                if ((_consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0)
                {
                    _vdsMetaTransferFunctionSet.Add(Program.PromptTransferFunctionData(new hdf_transfer_function_t(DateTime.MinValue.ToString("yyyy-MM-dd"), "polynomial", "permanent", string.Empty)));
                }
                else
                {
                    _vdsMetaTransferFunctionSet.Add(Program.PromptTransferFunctionData(new hdf_transfer_function_t()));
                }

                this.SelectedIndex = _totalCount;
                _updateAttribute   = true;

                break;

            case ConsoleKey.C:

                if (this.SelectedIndex >= 0)
                {
                    if (this.SelectedIndex >= 0 && this.SelectedIndex < _vdsTransferFunctionSet.Count())
                    {
                        _vdsMetaTransferFunctionSet.Add(_vdsTransferFunctionSet[this.SelectedIndex]);
                    }
                    else
                    {
                        _vdsMetaTransferFunctionSet.Add(_vdsMetaTransferFunctionSet[this.SelectedIndex - _vdsTransferFunctionSet.Count()]);
                    }

                    _updateAttribute   = true;
                    this.SelectedIndex = _totalCount;
                    this.OnRedraw();
                }

                break;

            case ConsoleKey.U:

                Console.CursorVisible = true;
                Console.Clear();
                Console.Write($"Enter value for unit ({ _unit }): ");

                (_unit, _isEscaped) = VdsToolUtilities.ReadLine(_vdsUnitSet);

                if (!_isEscaped)
                {
                    _updateUnit = true;
                }

                Console.CursorVisible = false;

                break;

            case ConsoleKey.Delete:

                if (this.SelectedIndex >= _vdsTransferFunctionSet.Count())
                {
                    Console.Clear();
                    Console.Write("The selected item will be deleted. Proceed (Y/N)? ");

                    if (Console.ReadKey().Key == ConsoleKey.Y)
                    {
                        _vdsMetaTransferFunctionSet.RemoveAt(this.SelectedIndex - _vdsTransferFunctionSet.Count());
                        _updateAttribute = true;
                    }
                    else
                    {
                        this.OnRedraw();
                    }
                }

                break;

            case ConsoleKey.PageUp:

                if (this.SelectedIndex >= _vdsTransferFunctionSet.Count() + 1 && this.SelectedIndex < _totalCount)
                {
                    int index = this.SelectedIndex - _vdsTransferFunctionSet.Count();
                    hdf_transfer_function_t hdf_transfer_function = _vdsMetaTransferFunctionSet[index];
                    _vdsMetaTransferFunctionSet.RemoveAt(index);
                    _vdsMetaTransferFunctionSet.Insert(index - 1, hdf_transfer_function);
                    this.SelectedIndex -= 1;
                    _updateAttribute    = true;
                }

                break;

            case ConsoleKey.PageDown:

                if (this.SelectedIndex >= _vdsTransferFunctionSet.Count() && this.SelectedIndex < _totalCount - 1)
                {
                    int index = this.SelectedIndex - _vdsTransferFunctionSet.Count();
                    hdf_transfer_function_t hdf_transfer_function = _vdsMetaTransferFunctionSet[index];
                    _vdsMetaTransferFunctionSet.RemoveAt(index);
                    _vdsMetaTransferFunctionSet.Insert(index + 1, hdf_transfer_function);
                    this.SelectedIndex += 1;
                    _updateAttribute    = true;
                }

                break;

            case ConsoleKey.Escape:
            case ConsoleKey.LeftArrow:
                return(true);
            }

            // update unit
            if (_updateUnit)
            {
                _groupId = IOHelper.OpenOrCreateGroup(_vdsMetaFileId, _currentPath).GroupId;
                IOHelper.PrepareAttribute(_groupId, "unit", new string[] { _unit }, new ulong[] { 1 }, true);
                _updateUnit = false;
                this.OnRedraw();
            }

            // update attribute
            if (_updateAttribute)
            {
                _groupId = IOHelper.OpenOrCreateGroup(_vdsMetaFileId, _currentPath).GroupId;
                IOHelper.PrepareAttribute(_groupId, "transfer_function_set", _vdsMetaTransferFunctionSet.ToArray(), new ulong[] { (ulong)_vdsMetaTransferFunctionSet.Count() }, true);
                _updateAttribute = false;
                this.OnRedraw();
            }

            // clean up
            if (H5I.is_valid(_groupId) > 0)
            {
                H5F.flush(_groupId, H5F.scope_t.GLOBAL);
                H5G.close(_groupId);
            }

            return(false);
        }
Example #8
0
 public static long Flush(long objectId, H5F.scope_t scope)
 {
     return(H5F.flush(objectId, scope));
 }
Example #9
0
        private void OpenFile(string filePath, DateTime startDateTime, IList <ChannelContext> channelContextSet)
        {
            long propertyId = -1;
            long datasetId  = -1;
            long groupId    = -1;

            bool isNew;

            try
            {
                _fileId = -1;

                // open file
                if (File.Exists(filePath))
                {
                    _fileId = H5F.open(filePath, H5F.ACC_RDWR, H5P.DEFAULT);

                    if (_fileId < 0)
                    {
                        try
                        {
                            File.Copy(filePath, filePath + ".backup");
                        }
                        catch
                        {
                            //
                        }
                    }
                }

                // create file
                if (_fileId < 0)
                {
                    propertyId = H5P.create(H5P.FILE_CREATE);
                    H5P.set_userblock(propertyId, USERBLOCK_SIZE);
                    _fileId = H5F.create(filePath, H5F.ACC_TRUNC, propertyId);
                }

                if (_fileId < 0)
                {
                    throw new Exception($"{ ErrorMessage.Mat73Writer_CouldNotOpenOrCreateFile } File: { filePath }.");
                }

                // prepare channels
                foreach (ChannelContext channelContext in channelContextSet)
                {
                    var periodInSeconds  = (ulong)Math.Round(_settings.FilePeriod.TotalSeconds, MidpointRounding.AwayFromZero);
                    var samplesPerSecond = channelContext.ChannelDescription.SampleRate.SamplesPerSecond;
                    (var chunkLength, var chunkCount) = GeneralHelper.CalculateChunkParameters(periodInSeconds, samplesPerSecond);

                    this.PrepareChannel(_fileId, channelContext.ChannelDescription, chunkLength, chunkCount);
                }

                // info
                groupId = this.OpenOrCreateStruct(_fileId, "/info").GroupId;

                (datasetId, isNew) = this.OpenOrCreateChannel(groupId, "last_completed_chunk", 1, 1);

                if (isNew)
                {
                    IOHelper.Write(datasetId, new double[] { -1 }, DataContainerType.Dataset);
                }

                // text entries
                _textEntrySet[2].Content = startDateTime.ToString("yyyy-MM-ddTHH-mm-ss");

                this.PrepareAllTextEntries(_textEntrySet);
            }
            finally
            {
                if (H5I.is_valid(datasetId) > 0)
                {
                    H5D.close(datasetId);
                }
                if (H5I.is_valid(propertyId) > 0)
                {
                    H5P.close(propertyId);
                }
                if (H5I.is_valid(groupId) > 0)
                {
                    H5G.close(groupId);
                }

                H5F.flush(_fileId, H5F.scope_t.GLOBAL);

                // write preamble
                if (H5I.is_valid(_fileId) > 0)
                {
                    H5F.close(_fileId);

                    this.WritePreamble(filePath);
                }
            }

            _fileId = H5F.open(filePath, H5F.ACC_RDWR);
        }
Example #10
0
 public void HDF5Flush()
 {
     H5F.flush(dataSet_, H5F.scope_t.LOCAL);
 }
Example #11
0
 public void H5FflushTest2()
 {
     Assert.IsFalse(
         H5F.flush(Utilities.RandomInvalidHandle(),
                   H5F.scope_t.GLOBAL) >= 0);
 }
Example #12
0
 public static void FlushToFile(Hdf5Identifier _fileId)
 {
     H5F.flush(_fileId.Value, H5F.scope_t.GLOBAL);
 }
Example #13
0
        protected override bool OnWaitForUserInput()
        {
            _totalCount = _vdsTagSet.Count() + _vdsMetaTagSet.Count();

            if (this.SelectedIndex < 0)
            {
                this.SelectedIndex = _totalCount > 0 ? 0 : -1;
            }
            else if (this.SelectedIndex >= _totalCount)
            {
                this.SelectedIndex = _totalCount - 1;
            }

            if (this.SelectedIndex >= _vdsTagSet.Count())
            {
                _offset = _baseOffset + 3;
            }
            else
            {
                _offset = _baseOffset + 0;
            }

            if (this.LastIndex >= 0)
            {
                Console.SetCursorPosition(1, this.LastIndex);
                Console.Write(" ");
            }

            if (this.SelectedIndex >= 0)
            {
                Console.SetCursorPosition(1, this.SelectedIndex + _offset + 2);
                Console.Write("x");
            }

            _consoleKeyInfo = Console.ReadKey(true);

            switch (_consoleKeyInfo.Key)
            {
            case ConsoleKey.UpArrow:

                if (this.SelectedIndex > 0)
                {
                    this.LastIndex      = Console.CursorTop;
                    this.SelectedIndex -= 1;
                }

                break;

            case ConsoleKey.DownArrow:

                if (this.SelectedIndex < _totalCount - 1)
                {
                    this.LastIndex      = Console.CursorTop;
                    this.SelectedIndex += 1;
                }

                break;

            case ConsoleKey.Enter:
            case ConsoleKey.RightArrow:

                if (this.SelectedIndex >= _vdsTagSet.Count())
                {
                    int index = this.SelectedIndex - _vdsTagSet.Count();
                    _vdsMetaTagSet[index] = Program.PromptTagData(_vdsMetaTagSet[index]);
                    _updateAttribute      = true;
                }

                break;

            case ConsoleKey.N:

                _vdsMetaTagSet.Add(Program.PromptTagData(new hdf_tag_t()));
                this.SelectedIndex = _totalCount;
                _updateAttribute   = true;

                break;

            case ConsoleKey.C:

                if (this.SelectedIndex >= 0)
                {
                    _vdsMetaTagSet.Add(_vdsMetaTagSet[this.SelectedIndex]);
                    this.SelectedIndex = _totalCount;
                    this.OnRedraw();
                }

                break;

            case ConsoleKey.D:

                Console.CursorVisible = true;
                Console.Clear();
                Console.Write($"\"{_description}\"\n");
                Console.WriteLine();
                Console.Write($"New description:\n");
                _description          = Console.ReadLine();
                Console.CursorVisible = false;

                _updateDescription = true;

                break;

            case ConsoleKey.Delete:

                if (this.SelectedIndex >= _vdsTagSet.Count())
                {
                    Console.Clear();
                    Console.Write("The selected item will be deleted. Proceed (Y/N)? ");

                    if (Console.ReadKey().Key == ConsoleKey.Y)
                    {
                        _vdsMetaTagSet.RemoveAt(this.SelectedIndex - _vdsTagSet.Count());
                        _updateAttribute = true;
                    }

                    this.OnRedraw();

                    break;
                }

                break;

            case ConsoleKey.PageUp:

                if (this.SelectedIndex >= _vdsTagSet.Count() + 1 && this.SelectedIndex < _totalCount)
                {
                    int       index   = this.SelectedIndex - _vdsTagSet.Count();
                    hdf_tag_t hdf_tag = _vdsMetaTagSet[index];
                    _vdsMetaTagSet.RemoveAt(index);
                    _vdsMetaTagSet.Insert(index - 1, hdf_tag);
                    this.SelectedIndex -= 1;
                    _updateAttribute    = true;
                }

                break;

            case ConsoleKey.PageDown:

                if (this.SelectedIndex >= _vdsTagSet.Count() && this.SelectedIndex < _totalCount - 1)
                {
                    int       index   = this.SelectedIndex - _vdsTagSet.Count();
                    hdf_tag_t hdf_tag = _vdsMetaTagSet[index];
                    _vdsMetaTagSet.RemoveAt(index);
                    _vdsMetaTagSet.Insert(index + 1, hdf_tag);
                    this.SelectedIndex += 1;
                    _updateAttribute    = true;
                }

                break;

            case ConsoleKey.Escape:
            case ConsoleKey.LeftArrow:
                return(true);
            }

            // update description
            if (_updateDescription)
            {
                _groupId = IOHelper.OpenOrCreateGroup(_vdsMetaFileId, _currentPath).GroupId;
                IOHelper.PrepareAttribute(_groupId, "description", new string[] { _description }, new ulong[] { 1 }, true);
                _updateDescription = false;
                this.OnRedraw();
            }

            // update attribute
            if (_updateAttribute)
            {
                _groupId = IOHelper.OpenOrCreateGroup(_vdsMetaFileId, _currentPath).GroupId;
                IOHelper.PrepareAttribute(_groupId, "tag_set", _vdsMetaTagSet.ToArray(), new ulong[] { (ulong)_vdsMetaTagSet.Count() }, true);
                _updateAttribute = false;
                this.OnRedraw();
            }

            // clean up
            if (H5I.is_valid(_groupId) > 0)
            {
                H5F.flush(_groupId, H5F.scope_t.GLOBAL);
                H5G.close(_groupId);
            }

            return(false);
        }
Example #14
0
        /**
         * Opens access to this file.
         *
         * @param loadFullHierarchy
         *            if true, load the full hierarchy into memory; otherwise just opens the file identifier.
         *
         * @return the file identifier if successful; otherwise returns negative value.
         *
         * @throws Exception
         *            If there is a failure.
         */
        private long open(bool loadFullHierarchy, long plist)
        {
            Hdf5Utils.LogInfo?.Invoke($"open(loadFullHierarchy = {loadFullHierarchy}, plist = {plist}): start");
            if (fid > 0)
            {
                Hdf5Utils.LogInfo?.Invoke("open(): FID already opened");
                Hdf5Utils.LogInfo?.Invoke("open(): finish");
                return(fid); // file is opened already
            }

            // The cwd may be changed at Dataset.read() by System.setProperty("user.dir", newdir)
            // to make it work for external datasets. We need to set it back
            // before the file is closed/opened.
            string rootPath = Environment.CurrentDirectory;

            if (!File.Exists(fullFileName))
            {
                Hdf5Utils.LogInfo?.Invoke($"open(): File {fullFileName} does not exist");
                Hdf5Utils.LogInfo?.Invoke("open(): finish");
                throw new Exception("File does not exist -- " + fullFileName);
            }
            else if ((flag == HDF5Constants.H5F_ACC_RDONLY) && new FileInfo(fullFileName).IsReadOnly)
            {
                Hdf5Utils.LogInfo?.Invoke($"open(): Cannot read file {fullFileName}");
                Hdf5Utils.LogInfo?.Invoke("open(): finish");
                throw new Exception("Cannot read file -- " + fullFileName);
            }
            // check for valid file access permission
            else if (flag < 0)
            {
                Hdf5Utils.LogInfo?.Invoke("open(): Invalid access identifier -- " + flag);
                Hdf5Utils.LogInfo?.Invoke("open(): finish");
                throw new Exception("Invalid access identifer -- " + flag);
            }
            else if (HDF5Constants.H5F_ACC_CREAT == flag)
            {
                // create a new file
                Hdf5Utils.LogInfo?.Invoke("open(): create file");
                fid = H5F.create(fullFileName, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
                                 HDF5Constants.H5P_DEFAULT);
                H5F.flush(fid, HDF5Constants.H5F_SCOPE_LOCAL);
                H5F.close(fid);
                flag = HDF5Constants.H5F_ACC_RDWR;
            }

            else if (((flag == HDF5Constants.H5F_ACC_RDWR) || (flag == HDF5Constants.H5F_ACC_CREAT)) && new FileInfo(fullFileName).IsReadOnly)
            {
                Hdf5Utils.LogInfo?.Invoke($"open(): Cannot write file {fullFileName}");
                Hdf5Utils.LogInfo?.Invoke("open(): finish");
                throw new Exception("Cannot write file, try opening as read-only -- " + fullFileName);
            }


            try
            {
                Hdf5Utils.LogInfo?.Invoke("open(): open file");
                fid = H5F.open(fullFileName, flag, plist);
            }
            catch (Exception ex)
            {
                try
                {
                    Hdf5Utils.LogInfo?.Invoke("open(): open failed, attempting to open file read-only");
                    fid        = H5F.open(fullFileName, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
                    isReadOnly = true;
                }
                catch (Exception ex2)
                {
                    Hdf5Utils.LogError?.Invoke("open(): open failed:" + ex2);
                }
            }

            if ((fid >= 0) && loadFullHierarchy)
            {
                // load the hierarchy of the file
                Hdf5Utils.LogInfo?.Invoke("open(loadFullHeirarchy): load the hierarchy");
                loadIntoMemory();
            }

            Hdf5Utils.LogInfo?.Invoke($"open(loadFullHeirarchy = {loadFullHierarchy}, plist = {plist}): finish");
            return(fid);
        }