Exemple #1
0
                public void double1d()
                {
                    using (var container = new TempH5FileContainer())
                    {
                        H5Group ROOT = container.Content().Root;

                        int rank = 1;
                        var dims = new long[] { 3 };
                        using (dset1d <double> DSET = ROOT.CreateDataset("dataset", rank, dims, typeof(double)) as dset1d <double>)
                        {
                            Assert.NotNull(DSET);
                            Assert.Equal(dims, DSET.Dims);

                            DSET[0] = 3.14;
                            DSET[1] = 3.14;
                            DSET[2] = 3.14;

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3] = 3.14);

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[-1]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[-1] = 3.14);
                        }
                    }
                }
Exemple #2
0
                public void string1d()
                {
                    using (var container = new TempH5FileContainer())
                    {
                        H5Group ROOT = container.Content().Root;

                        int rank = 1;
                        var dims = new long[] { 3 };

                        using (string1d DSET = ROOT.CreateDataset("dataset", rank, dims, typeof(string)) as string1d)
                        {
                            Assert.NotNull(DSET);
                            Assert.Equal(dims, DSET.Dims);

                            DSET[0] = "foo";
                            DSET[1] = "bar";
                            DSET[2] = "yom";

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3] = "grok");

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[-1]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[-1] = "grok");
                        }
                    }
                }
Exemple #3
0
            public void CanAccessAbsolutePaths()
            {
                using (var container = new TempH5FileContainer())
                {
                    H5File hf = container.Content();

                    using (H5Group GRP = hf.Root.CreateGroup("foo"))
                    {
                        H5Group SUBGRP = GRP.SubGroup("bar", create: true);
                        SUBGRP.Dispose();

                        Assert.True(H5Link.Exists(hf.ID, "/foo/"));
                        Assert.True(H5Link.Exists(hf.ID, "/foo/bar/"));

                        SUBGRP = hf.Root.SubGroup("/foo");
                        Assert.NotNull(GRP);
                        SUBGRP.Dispose();

                        SUBGRP = hf.Root.SubGroup("/foo/bar");
                        Assert.NotNull(GRP);
                        SUBGRP.Dispose();

                        SUBGRP = hf.Root.SubGroup("foo/bar");
                        Assert.NotNull(SUBGRP);
                        SUBGRP.Dispose();
                    }
                }
            }
Exemple #4
0
            public void AttemptToWriteClosedFileFails()
            {
                string path = Path.GetTempFileName();

                H5File FILE = H5File.Open(path, mode: "w");

                // create some stuff..
                H5Group GROUP = FILE.Root.SubGroup("foo", create: true);

                GROUP.CreateDataset("bar", 1, new long[] { 7L }, typeof(byte));
                dset1d <byte> DSET = GROUP["bar"] as dset1d <byte>;

                Assert.NotNull(DSET);

                DSET[3] = 5;

                // close all H5Objects manually in this test-scenario:
                GROUP.Dispose();
                DSET.Dispose();
                FILE.Dispose();

                // the file.ID becomes the H5F.close() return value, which is often
                // (but not guaranteed to be) zero / non-negative:
                Assert.Equal((H5Ohm.hid_t) 0, FILE.ID);
                Assert.Equal((H5Ohm.hid_t) 0, FILE.Root.ID);
                Assert.Equal((H5Ohm.hid_t) 0, DSET.ID);
                Assert.Equal((H5Ohm.hid_t) 0, GROUP.ID);

                Assert.Throws <InvalidOperationException>(() => FILE.Root["foo/bar"]);
                Assert.Throws <InvalidOperationException>(() => DSET[5] = 3);
                Assert.Throws <InvalidOperationException>(() => GROUP["bar"]);
            }
Exemple #5
0
            public void CreateChunkedDataset()
            {
                using (var container = new TempH5FileContainer())
                {
                    H5Group ROOT = container.Content().Root;

                    int rank    = 2;
                    var dims    = new long[] { 2, 3 };
                    var maxdims = new long[] { 5, 6 };

                    var trace = new StringWriter();
                    //TextWriterTraceListener listener = new TextWriterTraceListener(trace);
                    //Trace.Listeners.Add(listener);

                    using (var DSET = ROOT.CreateDataset("chunked", rank, dims, typeof(int), maxdims))
                    {
                        // chunking is not available in hdf5 v1.8.12 and we want to know about it:
                        //if (H5Library.LibVersion == "1.8.12")
                        //    Assert.Contains("WARNING", trace.ToString());
                        //Trace.Listeners.Remove(listener);

                        Assert.NotNull(DSET);
                        Assert.Equal(dims, DSET.Dims);
                    }
                }
            }
Exemple #6
0
            public void ResizeDataset(long[] max_dims, long[] new_dims)
            {
                using (var container = new TempH5FileContainer())
                {
                    H5Group ROOT = container.Content().Root;

                    int rank = max_dims.Length;
                    var dims = new long[rank];
                    dims.Fill(1L);

                    using (H5DataSet DSET = ROOT.CreateDataset("resizable", rank, dims, typeof(double), max_dims))
                    {
                        Assert.NotNull(DSET);
                        Assert.Equal(dims, DSET.Dims);

                        if (H5Library.LibVersion == "1.8.12")
                        {
                            Assert.NotEqual(max_dims, DSET.MaxDims);
                            Assert.Equal(dims, DSET.MaxDims);
                            Assert.Throws <NotImplementedException>(() => DSET.Resize(max_dims));
                        }
                        else
                        {
                            DSET.Resize(new_dims);

                            Assert.Equal(new_dims, DSET.Dims);
                        }
                    }
                }
            }
Exemple #7
0
            public void ResizeUnlimited(long[] new_dims)
            {
                using (var container = new TempH5FileContainer())
                {
                    H5Group ROOT = container.Content().Root;

                    int rank    = 2;
                    var dims    = new long[] { 2, 3 };
                    var maxdims = new long[] { -1, -1 };

                    using (var DSET = ROOT.CreateDataset("resizable", rank, dims, typeof(double), maxdims) as dset2d <double>)
                    {
                        Assert.NotNull(DSET);

                        Assert.Equal(dims, DSET.Dims);

                        if (H5Library.LibVersion == "1.8.12")
                        {
                            Assert.Throws <NotImplementedException>(() => DSET.Resize(new long[] { 6, 9 }));
                        }
                        else
                        {
                            DSET.Resize(new_dims);
                            Assert.Equal(new_dims, DSET.Dims);
                        }
                    }
                }
            }
Exemple #8
0
            public void ResizeDatasetChecksBounds(long[] new_dims)
            {
                using (var container = new TempH5FileContainer())
                {
                    H5Group ROOT = container.Content().Root;

                    int rank    = 2;
                    var dims    = new long[] { 2, 3 };
                    var maxdims = new long[] { 5, 6 };

                    using (H5DataSet DSET = ROOT.CreateDataset("resizable", rank, dims, typeof(double), maxdims))
                    {
                        Assert.NotNull(DSET);
                        Assert.Equal(dims, DSET.Dims);

                        if (H5Library.LibVersion == "1.8.12")
                        {
                            Assert.NotEqual(maxdims, DSET.MaxDims);
                            Assert.Equal(dims, DSET.MaxDims);
                            Assert.Throws <NotImplementedException>(() => DSET.Resize(maxdims));
                        }
                        else
                        {
                            Assert.Throws <IndexOutOfRangeException>(() => DSET.Resize(new_dims));
                        }
                    }
                }
            }
Exemple #9
0
 public MyObject(H5Group location)
     : base(location)
 {
     cityNames.Values = new string[] {
         "Chicago", "New York", "San Francisco",
         "Springfield, Nebraska", "New Berlin",
     };
 }
Exemple #10
0
            public void FindsAllSubgroups()
            {
                using (var container = new TempH5FileContainer())
                {
                    H5File hf = container.Content();

#if DEBUG
                    int nObjectsInitially = H5Base.nObjects;
#endif

                    H5Group SUT = hf.Root;

                    var testnames = new List <string>
                    {
                        "foo",
                        "bar",
                        "zoom",
                        "grok",
                    };
                    // create a group hierarchy..
                    foreach (string name in testnames)
                    {
                        using (var subgroup = SUT.CreateGroup(name))
                        {
                            using (subgroup.CreateGroup("tarnkappenzwerg"))
                            {
                                // just create and dispose..
                            }
                        }
                    }
                    // ..as well as some confusion..
                    using (SUT.CreateDataset("tarnkappenbomber", 1, new long[] { 7 }, typeof(float)))
                    {
                    }

                    var actual = SUT.SubGroups().Select(g => g.Name);

                    // a) check for set equality..
                    var testnames_sorted = new SortedSet <string>(testnames);

                    Assert.True(testnames_sorted.SetEquals(actual));

                    // b) check the default behaviour of sorting the SubGroups() alphabetically..
                    Assert.Equal(testnames_sorted.ToList(), actual);

                    // c) check that all allocated SubGroups() have been disposed of properly..
#if DEBUG
                    Assert.Equal(nObjectsInitially, H5Base.nObjects);
#endif
                }
            }
Exemple #11
0
        public void Read()
        {
            using (H5File hf = H5File.Open(demodata + "test_generic_read.h5", mode: "r"))
            {
                H5Group grp = hf.Root.SubGroup("my_group");

                H5Attribute attr = grp.GetAttribute("group_attr");
                Assert.NotNull(attr);
                Assert.Equal(42, attr.Read <int>());

                attr = grp["float_1D"].GetAttribute("dataset_attr");
                Assert.NotNull(attr);
                Assert.Equal("a foo that bars", attr.Reads());
            }
        }
Exemple #12
0
        public void ReadFails()
        {
            using (H5File hf = H5File.Open(demodata + "test_generic_read.h5", mode: "r"))
            {
                H5Group grp = hf.Root.SubGroup("my_group");

                Assert.Throws <KeyNotFoundException>(() => grp.GetAttribute("asdfawieryan"));
                Assert.Throws <KeyNotFoundException>(() => grp["float_1D"].GetAttribute("awxryan"));

                H5Attribute attr = grp.GetAttribute("group_attr");

                Assert.Throws <InvalidCastException>(() => attr.Read <float>());
                Assert.Throws <InvalidCastException>(() => attr.Read <string>());
                Assert.Throws <InvalidCastException>(() => attr.Read <long>());
            }
        }
Exemple #13
0
        public void ReadFromMapping()
        {
            using (H5File hf = H5File.Open(demodata + "test_generic_read.h5", mode: "r"))
            {
                MyObject myo;
                using (H5Group grp = hf.Root.SubGroup("my_group"))
                {
                    myo = new MyObject(grp);
                }

                Assert.Equal(42, myo.Attr["group_attr"].Read());
                Assert.Equal("a foo that bars", myo.Attr["dataset_attr"].Read());

                Assert.Throws <KeyNotFoundException>(() => myo.Attr["not_existing"]);

                myo.Dispose();
            }
        }
Exemple #14
0
                public void string2d()
                {
                    using (var container = new TempH5FileContainer())
                    {
                        H5Group ROOT = container.Content().Root;

                        int rank = 2;
                        var dims = new long[] { 3, 2 };

                        using (string2d DSET = ROOT.CreateDataset("dataset", rank, dims, typeof(string)) as string2d)
                        {
                            Assert.NotNull(DSET);
                            Assert.Equal(dims, DSET.Dims);

                            DSET[0, 0] = "foo";
                            DSET[2, 1] = "bar";
                            DSET[1]    = new string[] { "zoom", "grok" };

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3, 0]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3, 0] = "foo");

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3, 2]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3, 2] = "foo");

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[1, 7]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[1, 7] = "foo");

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[1, -3]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[1, -3] = "foo");

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3] = new string[] { "foo", "bar" });

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[-1]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[-1] = new string[] { "foo", "bar" });

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[0] = new string[] { "foo", "bar", "zoom" });
                        }
                    }
                }
Exemple #15
0
                public void double2d()
                {
                    using (var container = new TempH5FileContainer())
                    {
                        H5Group ROOT = container.Content().Root;

                        int rank = 2;
                        var dims = new long[] { 3, 2 };

                        using (dset2d <double> DSET = ROOT.CreateDataset("dataset", rank, dims, typeof(double)) as dset2d <double>)
                        {
                            Assert.NotNull(DSET);
                            Assert.Equal(dims, DSET.Dims);

                            DSET[0, 0] = 3.14;
                            DSET[2, 1] = 3.14;
                            DSET[1]    = new double[] { 1.2, 3.4 };

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3, 0]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3, 0] = 3.14);

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3, 2]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3, 2] = 3.14);

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[1, 7]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[1, 7] = 3.14);

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[1, -3]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[1, -3] = 3.14);

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[3] = new double[] { 1.0, 2.0 });

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[-1]);
                            Assert.Throws <IndexOutOfRangeException>(() => DSET[-1] = new double[] { 1.0, 2.0 });

                            Assert.Throws <IndexOutOfRangeException>(() => DSET[0] = new double[] { 1.0, 2.0, 3.0 });
                        }
                    }
                }
Exemple #16
0
            public void CreateNestedSubGroups()
            {
                using (var container = new TempH5FileContainer())
                {
                    H5File hf = container.Content();

                    Assert.False(H5Link.Exists(hf.ID, "zoom"));
                    Assert.False(H5Link.Exists(hf.ID, "zoom/zoom"));
                    Assert.False(H5Link.Exists(hf.ID, "zoom/zoom/zoom"));

                    H5Group GRP = hf.Root;

                    for (int i = 0; i < 3; i++)
                    {
                        GRP = GRP.CreateGroup("zoom");
                    }

                    GRP.Dispose();

                    Assert.True(H5Link.Exists(hf.ID, "zoom/zoom/zoom"));
                }
            }
        private int depth_first(HObject parentObject, int nTotal)
        {
            Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): start");

            int    nelems;
            string fullPath = null;
            string ppath    = null;
            long   gid      = -1;

            H5Group pgroup = (H5Group)parentObject;

            ppath = pgroup.getPath();

            if (ppath == null)
            {
                fullPath = HObject.SEPARATOR;
            }
            else
            {
                fullPath = ppath + pgroup.getName() + HObject.SEPARATOR;
            }

            nelems = 0;
            try
            {
                gid = pgroup.open();
                var info = new H5G.info_t();
                H5G.get_info(gid, ref info);
                nelems = (int)info.nlinks;
            }
            catch (Exception ex)
            {
                nelems = -1;
                Hdf5Utils.LogError?.Invoke($"depth_first({parentObject}): H5Gget_info(gid {gid}) failure: {ex}");
            }

            if (nelems <= 0)
            {
                pgroup.close(gid);
                Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): nelems <= 0");
                Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): finish");
                return(nTotal);
            }

            // since each call of H5.H5Gget_objname_by_idx() takes about one second.
            // 1,000,000 calls take 12 days. Instead of calling it in a loop,
            // we use only one call to get all the information, which takes about
            // two seconds
            int[] objTypes = new int[nelems];
            //long[] fNos = new long[nelems];
            //long[] objRefs = new long[nelems];
            string[]     objNames = new string[nelems];
            H5L.info_t[] infos    = new H5L.info_t[nelems];
            try
            {
                int i = 0;
                int callback(long group, IntPtr name, ref H5L.info_t info, IntPtr op_data)
                {
                    string realName = Marshal.PtrToStringAuto(name);

                    objTypes[i] = (int)info.type;
                    objNames[i] = realName;
                    infos[i]    = info;

                    return(i++);
                }

                ulong pos = 0;
                H5L.iterate(gid, indexType, indexOrder, ref pos, callback, IntPtr.Zero);

                //for (ulong i = 0; i < (ulong)nelems; i++)
                //{


                //    H5G.info_t info = new H5G.info_t();
                //    H5G.get_info_by_idx(fid, fullPath, indexType, indexOrder, i, ref info);
                //    infos[i] = info;


                //}

                // H5.H5Gget_obj_info_full(fid, fullPath, objNames, objTypes, null, fNos, objRefs, indexType, indexOrder);
            }
            catch (Exception ex)
            {
                Hdf5Utils.LogError?.Invoke($"depth_first({parentObject}): failure: {ex}");
                Hdf5Utils.LogError?.Invoke($"depth_first({parentObject}): finish");
                return(nTotal);
            }

            int nStart = getStartMembers();
            int nMax   = getMaxMembers();

            string obj_name;
            int    obj_type;

            // Iterate through the file to see members of the group
            for (int i = 0; i < nelems; i++)
            {
                obj_name = objNames[i];
                obj_type = objTypes[i];
                Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): obj_name={obj_name}, obj_type={obj_type}");

                if (obj_name == null)
                {
                    Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): continue after null obj_name");
                    continue;
                }

                nTotal++;

                if (nMax > 0)
                {
                    if ((nTotal - nStart) >= nMax)
                    {
                        break; // loaded enough objects
                    }
                }

                bool skipLoad = (nTotal > 0) && (nTotal < nStart);

                // create a new group
                if (obj_type == HDF5Constants.H5O_TYPE_GROUP)
                {
                    H5Group g = new H5Group(this, obj_name, fullPath, pgroup);

                    pgroup.addToMemberList(g);

                    // detect and stop loops
                    // a loop is detected if there exists object with the same
                    // object ID by tracing path back up to the root.
                    bool    hasLoop = false;
                    H5Group tmpObj  = (H5Group)parentObject;

                    while (tmpObj != null)
                    {
                        if (tmpObj.equalsOID(new IntPtr((int)infos[i].u.address)) && (tmpObj.getPath() != null))
                        {
                            hasLoop = true;
                            break;
                        }
                        else
                        {
                            tmpObj = (H5Group)tmpObj.getParent();
                        }
                    }

                    // recursively go through the next group
                    // stops if it has loop.
                    if (!hasLoop)
                    {
                        nTotal = depth_first(g, nTotal);
                    }
                }
                else if (skipLoad)
                {
                    continue;
                }
                else if (obj_type == HDF5Constants.H5O_TYPE_DATASET)
                {
                    long        did    = -1;
                    long        tid    = -1;
                    H5T.class_t tclass = H5T.class_t.NO_CLASS;
                    try
                    {
                        did = H5D.open(fid, fullPath + obj_name, HDF5Constants.H5P_DEFAULT);
                        if (did >= 0)
                        {
                            tid = H5D.get_type(did);

                            tclass = H5T.get_class(tid);
                            if ((tclass == HDF5Constants.H5T_ARRAY) || (tclass == HDF5Constants.H5T_VLEN))
                            {
                                // for ARRAY, the type is determined by the base type
                                long btid = H5T.get_super(tid);

                                tclass = H5T.get_class(btid);

                                try
                                {
                                    H5T.close(btid);
                                }
                                catch (Exception ex)
                                {
                                    Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject})[{i}] dataset {obj_name} H5Tclose(btid {btid}) failure: {ex}");
                                }
                            }
                        }
                        else
                        {
                            Hdf5Utils.LogError?.Invoke($"depth_first({parentObject})[{i}] {obj_name} dataset open failure");
                        }
                    }
                    catch (Exception ex)
                    {
                        Hdf5Utils.LogError?.Invoke($"depth_first({parentObject})[{i}] {obj_name} dataset access failure: {ex}");
                    }
                    finally
                    {
                        try
                        {
                            H5T.close(tid);
                        }
                        catch (Exception ex)
                        {
                            Hdf5Utils.LogError?.Invoke($"depth_first({parentObject})[{i}] dataset {obj_name} H5Tclose(tid {tid}) failure: {ex}");
                        }
                        try
                        {
                            H5D.close(did);
                        }
                        catch (Exception ex)
                        {
                            Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject})[{i}] dataset {obj_name} H5Dclose(did {did}) failure: {ex}");
                        }
                    }
                    //todo:
                    //Dataset d = null;
                    //if (tclass == HDF5Constants.H5T_COMPOUND)
                    //{
                    //    // create a new compound dataset
                    //    d = new H5CompoundDS(this, obj_name, fullPath, oid); // deprecated!
                    //}
                    //else
                    //{
                    //    // create a new scalar dataset
                    //    d = new H5ScalarDS(this, obj_name, fullPath, oid); // deprecated!
                    //}

                    // pgroup.addToMemberList(d);
                }
                else if (obj_type == HDF5Constants.H5O_TYPE_NAMED_DATATYPE)
                {
                    //Datatype t = new H5Datatype(this, obj_name, fullPath, oid); // deprecated!

                    //pgroup.addToMemberList(t);
                }
                else if (obj_type == HDF5Constants.H5O_TYPE_UNKNOWN)
                {
                    //H5Link link = new H5Link(this, obj_name, fullPath, oid);

                    // pgroup.addToMemberList(link);
                    continue; // do the next one, if the object is not identified.
                }
            } // ( i = 0; i < nelems; i++)

            pgroup.close(gid);

            Hdf5Utils.LogInfo?.Invoke($"depth_first({parentObject}): finish");
            return(nTotal);
        }
Exemple #18
0
 public ReadOnlyObject(H5Group grp) : base(grp)
 {
 }
Exemple #19
0
#pragma warning restore 0649

                public GuineaPig(H5Group grp) : base(grp)
                {
                }
Exemple #20
0
 public MyNewObject(H5Group grp) : base(grp)
 {
 }