Пример #1
0
        public bool TestSByteVar()
        {
            NcFile file = null;
            NcDim  dim1 = null;
            NcVar  var1 = null;

            sbyte[] buffer     = new sbyte[4];
            sbyte[] readBuffer = new sbyte[4];
            for (int i = 0; i < 4; i++)
            {
                buffer[i] = (sbyte)(i - 1);
            }
            try {
                FileSetup(ref file, ref dim1, ref var1, NcByte.Instance, 4);
                // Test the array get/put
                var1.PutVar(buffer);
                var1.GetVar(readBuffer);
                for (int i = 0; i < 4; i++)
                {
                    Assert.Equals(readBuffer[i], buffer[i]);
                }

                // Test get and put for scalars
                var1.PutVar(new Int32[] { 1 }, new sbyte[] { -2 });
                var1.GetVar(new Int32[] { 1 }, readBuffer);
                Assert.Equals(readBuffer[0], (sbyte)-2);

                // Test get and put subsets
                var1.PutVar(new Int32[] { 2 }, new Int32[] { 2 }, new sbyte[] { -2, -2 });
                var1.GetVar(new Int32[] { 2 }, new Int32[] { 2 }, readBuffer);
                Assert.Equals(readBuffer[0], (sbyte)-2);
                Assert.Equals(readBuffer[1], (sbyte)-2);
                // Test ranges
                var1.PutVar(new Int32[] { 0 }, new int[] { 127 });
                try {
                    var1.PutVar(new Int32[] { 0 }, new int[] { 128 });
                    throw new AssertFailedException("Failed to raise NcRange exception.");
                } catch (NcRange) {
                }
                var1.PutVar(new Int32[] { 0 }, new int[] { -128 });
                try {
                    var1.PutVar(new Int32[] { 0 }, new int[] { -129 });
                    throw new AssertFailedException("Failed to raise NcRange exception.");
                } catch (NcRange) {
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #2
0
        public bool TestStrictChecking()
        {
            NcFile file = null;
            NcDim  dim1 = null;
            NcVar  var1 = null;

            Int32[] buffer = new Int32[20];
            for (int i = 0; i < 20; i++)
            {
                buffer[i] = i;
            }
            try {
                FileSetup(ref file, ref dim1, ref var1);
                var1.PutVar(buffer);
                buffer = new Int32[15]; // Squeeze it
                try {
                    var1.GetVar(buffer);
                    // If it gets to here the program will crash hard anyway
                    throw new AssertFailedException("BufferOverflow exception not raised");
                } catch (NcBufferOverflow) {
                    // yay it worked
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #3
0
        public bool TestUInt64Var()
        {
            NcFile file = null;
            NcDim  dim1 = null;
            NcVar  var1 = null;

            UInt64[] buffer     = new UInt64[20];
            UInt64[] readBuffer = new UInt64[20];
            for (int i = 0; i < 20; i++)
            {
                buffer[i] = (UInt64)i;
            }
            try {
                FileSetup(ref file, ref dim1, ref var1, NcUint64.Instance);
                // Test the basic get/put
                var1.PutVar(buffer);
                var1.GetVar(readBuffer);
                for (int i = 0; i < 20; i++)
                {
                    Assert.Equals(readBuffer[i], buffer[i]);
                }
                // test get and put scalars
                var1.PutVar(new Int32[] { 5 }, new UInt64[] { 30 });
                var1.GetVar(new Int32[] { 5 }, readBuffer);
                Assert.Equals(readBuffer[0], (UInt64)30);
                // test get and put 1d arrays
                var1.PutVar(new Int32[] { 2 }, new Int32[] { 4 }, new UInt64[] { 20, 20, 20, 20 });
                var1.GetVar(new Int32[] { 2 }, new Int32[] { 4 }, readBuffer);
                Assert.Equals(readBuffer[0], (UInt64)20);
                Assert.Equals(readBuffer[3], (UInt64)20);
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[0], (UInt64)0);
                Assert.Equals(readBuffer[2], (UInt64)20);
                Assert.Equals(readBuffer[5], (UInt64)20);
                Assert.Equals(readBuffer[6], (UInt64)6);

                // test striding
                var1.PutVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, new UInt64[] { 40, 40, 40 });
                var1.GetVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, readBuffer);
                Assert.Equals(readBuffer[0], (UInt64)40);
                Assert.Equals(readBuffer[2], (UInt64)40);
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[10], (UInt64)40);
                Assert.Equals(readBuffer[12], (UInt64)40);
                Assert.Equals(readBuffer[14], (UInt64)40);
                Assert.Equals(readBuffer[15], (UInt64)15);
                // Test ranges
                var1.PutVar(new Int32[] { 0 }, new ulong[] { 18446744073709551615L });
                try {
                    var1.PutVar(new Int32[] { 0 }, new long[] { -1 });
                    throw new AssertFailedException("Failed to raise NcRange exception.");
                } catch (NcRange) {
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #4
0
        public bool TestDoubleVar()
        {
            NcFile file = null;
            NcDim  dim1 = null;
            NcVar  var1 = null;

            double[] buffer     = new double[20];
            double[] readBuffer = new double[20];
            for (int i = 0; i < 20; i++)
            {
                buffer[i] = (double)i;
            }
            try {
                FileSetup(ref file, ref dim1, ref var1, NcDouble.Instance);
                // Test the basic get/put
                var1.PutVar(buffer);
                var1.GetVar(readBuffer);
                for (int i = 0; i < 20; i++)
                {
                    Assert.Equals(readBuffer[i], buffer[i]);
                }
                // test get and put scalars
                var1.PutVar(new Int32[] { 5 }, new double[] { 30.0 });
                var1.GetVar(new Int32[] { 5 }, readBuffer);
                Assert.Equals(readBuffer[0], 30.0);
                // test get and put 1d arrays
                var1.PutVar(new Int32[] { 2 }, new Int32[] { 4 }, new double[] { 20.0, 20.0, 20.0, 20.0 });
                var1.GetVar(new Int32[] { 2 }, new Int32[] { 4 }, readBuffer);
                Assert.Equals(readBuffer[0], (double)20.0);
                Assert.Equals(readBuffer[3], (double)20.0);
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[0], (double)0.0);
                Assert.Equals(readBuffer[2], (double)20.0);
                Assert.Equals(readBuffer[5], (double)20.0);
                Assert.Equals(readBuffer[6], (double)6.0);
                // test striding
                var1.PutVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, new double[] { 40, 40, 40 });
                var1.GetVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, readBuffer);
                Assert.Equals(readBuffer[0], (double)40);
                Assert.Equals(readBuffer[2], (double)40);
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[10], (double)40);
                Assert.Equals(readBuffer[12], (double)40);
                Assert.Equals(readBuffer[14], (double)40);
                Assert.Equals(readBuffer[15], (double)15);
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #5
0
        public bool TestScalar()
        {
            NcFile file = null;

            float[] buffer = new float[] { 1.0f };
            try {
                file = TestHelper.NewFile(filePath);
                NcVar var = file.AddVar("scalar", NcFloat.Instance);

                var.PutAtt("long_name", "A scalar variable");
                var.PutVar(new float[] { 1.0f });
                buffer[0] = 90.0f;
                Assert.Equals(buffer[0], 90.0f);
                var.GetVar(buffer);
                Assert.Equals(buffer[0], 1.0f);
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #6
0
        public bool TestStringVar()
        {
            NcFile        file    = null;
            NcDim         dim1    = null;
            NcVar         var1    = null;
            ASCIIEncoding encoder = new ASCIIEncoding();

            string buffer = String.Format("{0,20}", "hi there"); // need exactly 20 chars

            byte[] readBuffer = new byte[20];
            try {
                FileSetup(ref file, ref dim1, ref var1, NcChar.Instance);
                var1.PutVar(encoder.GetBytes(buffer));
                var1.GetVar(readBuffer);
                Assert.Equals(encoder.GetString(readBuffer), buffer);
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #7
0
        public bool TestPut()
        {
            NcFile file = null;

            try {
                file = TestHelper.NewFile(filePath);
                NcDim time = file.AddDim("time", 2);
                NcDim x    = file.AddDim("x", 2);
                NcDim y    = file.AddDim("y", 2);
                NcDim z    = file.AddDim("z", 4);
                NcVar u    = file.AddVar("u", NcFloat.Instance, new List <NcDim>()
                {
                    time, x, y, z
                });
                NcVar v = file.AddVar("v", NcFloat.Instance, new List <NcDim>()
                {
                    time, x, y, z
                });

                NcArray uArray = new NcArray(NcFloat.Instance, u.Shape);
                uArray.Fill(1);
                uArray.FillSlice(20, new int[] { 0, 0, 0, 3 }, new int[] { 2, 2, 2, 4 });
                NcArray vArray = new NcArray(NcFloat.Instance, v.Shape);
                vArray.Fill(100);

                u.PutVar(uArray);

                v.PutVar(vArray);

                NcArray outArray = u.GetVar();
                Assert.True(uArray.Equals(outArray));

                outArray = v.GetVar();
                Assert.True(vArray.Equals(outArray));
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #8
0
        public bool TestGet()
        {
            NcFile file = null;

            try {
                file = TestHelper.NewFile(filePath);
                NcDim time = file.AddDim("time", 10);
                NcDim x    = file.AddDim("x", 20);
                NcDim y    = file.AddDim("y", 20);
                NcVar u    = file.AddVar("u", NcFloat.Instance, new List <NcDim>()
                {
                    time, x, y
                });
                NcVar v = file.AddVar("v", NcFloat.Instance, new List <NcDim>()
                {
                    time, x, y
                });

                float[] uBuf = new float[10 * 20 * 20];
                for (int i = 0; i < uBuf.Length; i++)
                {
                    uBuf[i] = (float)i;
                }
                float[] vBuf = new float[10 * 20 * 20];
                for (int i = 0; i < vBuf.Length; i++)
                {
                    vBuf[i] = (float)i;
                }

                u.PutVar(uBuf);
                v.PutVar(vBuf);

                NcArray uArray = u.GetVar();
                Assert.Equals(uArray.Array, uBuf);
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #9
0
        public bool TestOpaque()
        {
            NcFile       file       = null;
            NcVar        var        = null;
            NcDim        dim        = null;
            NcType       type       = null;
            NcOpaqueType opaqueType = null;

            byte[] opaqueBuffer = new byte[32];
            byte[] readBuffer   = new byte[32];
            for (int i = 0; i < 32; i++)
            {
                opaqueBuffer[i] = (byte)i;
            }

            try {
                file       = TestHelper.NewFile(filePath);
                type       = file.AddOpaqueType("opaque", 32);
                opaqueType = new NcOpaqueType(type);

                Assert.Equals(type.GetTypeClass(), NcTypeEnum.NC_OPAQUE);
                Assert.Equals(opaqueType.GetTypeSize(), 32);

                dim = file.AddDim("time", 1);
                var = file.AddVar("opaqueVar", opaqueType, dim);
                int iLen = 0;
                var.PutVar(new Int32[] { 0 }, opaqueBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, readBuffer);
                Assert.Equals(iLen, 32);
                for (int i = 0; i < 32; i++)
                {
                    Assert.Equals(readBuffer[i], opaqueBuffer[i]);
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #10
0
        public bool TestUnlimitedDim()
        {
            NcFile file    = null;
            NcDim  time    = null;
            NcVar  timeVar = null;

            Int32[] readBuffer  = new Int32[11];
            Int32[] writeBuffer = new Int32[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            try {
                file    = TestHelper.NewFile(filePath);
                time    = file.AddDim("time");
                timeVar = file.AddVar("t", NcInt64.Instance, time);
                timeVar.PutVar(new Int32[] { 0 }, new Int32[] { 11 }, writeBuffer);
                timeVar.GetVar(readBuffer);
                for (int i = 0; i < 11; i++)
                {
                    Assert.Equals(readBuffer[i], (Int32)i);
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #11
0
        public bool TestEnum()
        {
            NcFile     file     = null;
            NcType     type     = null;
            NcEnumType enumType = null;
            NcDim      dim      = null;
            NcVar      var      = null;

            sbyte[]  sbyteBuffer  = new sbyte[1];
            byte[]   byteBuffer   = new byte[1];
            Int16[]  Int16Buffer  = new Int16[1];
            UInt16[] UInt16Buffer = new UInt16[1];
            Int32[]  Int32Buffer  = new Int32[1];
            UInt32[] UInt32Buffer = new UInt32[1];
            Int64[]  Int64Buffer  = new Int64[1];
            UInt64[] UInt64Buffer = new UInt64[1];

            try {
                file = TestHelper.NewFile(filePath);
                dim  = file.AddDim("time", 1);

                type     = file.AddEnumType("sbyteenum", NcEnumType.Types.NC_BYTE);
                enumType = new NcEnumType(type);
                Assert.Equals(enumType.GetName(), "sbyteenum");
                Assert.Equals(enumType.GetMemberCount(), 0);
                Assert.True(NcByte.Instance.Equals(enumType.GetBaseType()));
                enumType.AddMember("BASE", 0);
                enumType.AddMember("VOR", 1);
                enumType.AddMember("DME", 2);
                enumType.AddMember("TAC", 3);

                var = file.AddVar("enumsbyteVar", enumType, dim);
                var.PutVar(new sbyte[] { 3 });
                var.GetVar(sbyteBuffer);
                Assert.Equals(sbyteBuffer[0], (sbyte)3);

                type     = file.AddEnumType("byteenum", NcEnumType.Types.NC_UBYTE);
                enumType = new NcEnumType(type);
                Assert.Equals(enumType.GetName(), "byteenum");
                Assert.Equals(enumType.GetMemberCount(), 0);
                Assert.True(NcUbyte.Instance.Equals(enumType.GetBaseType()));
                enumType.AddMember("BASE", 0);
                enumType.AddMember("VOR", 1);
                enumType.AddMember("DME", 2);
                enumType.AddMember("TAC", 3);

                var = file.AddVar("enumbyteVar", enumType, dim);
                var.PutVar(new byte[] { 3 });
                var.GetVar(byteBuffer);
                Assert.Equals(byteBuffer[0], (byte)3);

                type     = file.AddEnumType("Int16enum", NcEnumType.Types.NC_SHORT);
                enumType = new NcEnumType(type);
                Assert.Equals(enumType.GetName(), "Int16enum");
                Assert.Equals(enumType.GetMemberCount(), 0);
                Assert.True(NcShort.Instance.Equals(enumType.GetBaseType()));
                enumType.AddMember("BASE", 0);
                enumType.AddMember("VOR", 1);
                enumType.AddMember("DME", 2);
                enumType.AddMember("TAC", 3);

                var = file.AddVar("enumInt16Var", enumType, dim);
                var.PutVar(new Int16[] { 3 });
                var.GetVar(Int16Buffer);
                Assert.Equals(Int16Buffer[0], (Int16)3);

                type     = file.AddEnumType("UInt16enum", NcEnumType.Types.NC_USHORT);
                enumType = new NcEnumType(type);
                Assert.Equals(enumType.GetName(), "UInt16enum");
                Assert.Equals(enumType.GetMemberCount(), 0);
                Assert.True(NcUshort.Instance.Equals(enumType.GetBaseType()));
                enumType.AddMember("BASE", 0);
                enumType.AddMember("VOR", 1);
                enumType.AddMember("DME", 2);
                enumType.AddMember("TAC", 3);

                var = file.AddVar("enumUInt16Var", enumType, dim);
                var.PutVar(new UInt16[] { 3 });
                var.GetVar(UInt16Buffer);
                Assert.Equals(UInt16Buffer[0], (UInt16)3);

                type     = file.AddEnumType("Int32enum", NcEnumType.Types.NC_INT);
                enumType = new NcEnumType(type);
                Assert.Equals(enumType.GetName(), "Int32enum");
                Assert.Equals(enumType.GetMemberCount(), 0);
                Assert.True(NcInt.Instance.Equals(enumType.GetBaseType()));
                enumType.AddMember("BASE", 0);
                enumType.AddMember("VOR", 1);
                enumType.AddMember("DME", 2);
                enumType.AddMember("TAC", 3);

                var = file.AddVar("enumInt32Var", enumType, dim);
                var.PutVar(new Int32[] { 3 });
                var.GetVar(Int32Buffer);
                Assert.Equals(Int32Buffer[0], (Int32)3);

                type     = file.AddEnumType("UInt32enum", NcEnumType.Types.NC_UINT);
                enumType = new NcEnumType(type);
                Assert.Equals(enumType.GetName(), "UInt32enum");
                Assert.Equals(enumType.GetMemberCount(), 0);
                Assert.True(NcUint.Instance.Equals(enumType.GetBaseType()));
                enumType.AddMember("BASE", 0);
                enumType.AddMember("VOR", 1);
                enumType.AddMember("DME", 2);
                enumType.AddMember("TAC", 3);

                var = file.AddVar("enumUInt32Var", enumType, dim);
                var.PutVar(new UInt32[] { 3 });
                var.GetVar(UInt32Buffer);
                Assert.Equals(UInt32Buffer[0], (UInt32)3);

                type     = file.AddEnumType("Int64enum", NcEnumType.Types.NC_INT64);
                enumType = new NcEnumType(type);
                Assert.Equals(enumType.GetName(), "Int64enum");
                Assert.Equals(enumType.GetMemberCount(), 0);
                Assert.True(NcInt64.Instance.Equals(enumType.GetBaseType()));
                enumType.AddMember("BASE", (Int64)0);
                enumType.AddMember("VOR", (Int64)1);
                enumType.AddMember("DME", (Int64)2);
                enumType.AddMember("TAC", (Int64)3);
                Assert.Equals(enumType.GetMemberCount(), 4);
                Assert.Equals(enumType.GetMemberNameFromValue((sbyte)1), "VOR");
                Assert.Equals(enumType.GetMemberNameFromValue((byte)1), "VOR");
                Assert.Equals(enumType.GetMemberNameFromValue((Int16)1), "VOR");
                Assert.Equals(enumType.GetMemberNameFromValue((UInt16)1), "VOR");
                Assert.Equals(enumType.GetMemberNameFromValue((Int32)1), "VOR");
                Assert.Equals(enumType.GetMemberNameFromValue((UInt32)1), "VOR");
                Assert.Equals(enumType.GetMemberNameFromValue((Int64)1), "VOR");
                Assert.Equals(enumType.GetMemberNameFromValue((UInt64)1), "VOR");

                var = file.AddVar("enumInt64Var", enumType, dim);
                var.PutVar(new Int64[] { 3 });
                var.GetVar(Int64Buffer);
                Assert.Equals(Int64Buffer[0], (Int64)3);

                type     = file.AddEnumType("UInt64enum", NcEnumType.Types.NC_UINT64);
                enumType = new NcEnumType(type);
                Assert.Equals(enumType.GetName(), "UInt64enum");
                Assert.Equals(enumType.GetMemberCount(), 0);
                Assert.True(NcUint64.Instance.Equals(enumType.GetBaseType()));
                enumType.AddMember("BASE", 0);
                enumType.AddMember("VOR", 1);
                enumType.AddMember("DME", 2);
                enumType.AddMember("TAC", 3);

                var = file.AddVar("enumUInt64Var", enumType, dim);
                var.PutVar(new UInt64[] { 3 });
                var.GetVar(UInt64Buffer);
                Assert.Equals(UInt64Buffer[0], (UInt64)3);
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #12
0
        public bool TestVlen()
        {
            NcFile     file             = null;
            NcVlenType vlen             = null;
            NcDim      dim              = null;
            NcVar      var              = null;
            string     stringVlenBuffer = "hi there";
            string     stringReadBuffer;

            int iLen = 0;

            sbyte[] sbyteVlenBuffer = new sbyte[] { 0, -12, -4 };
            sbyte[] sbyteReadBuffer = new sbyte[8];

            byte[] byteVlenBuffer = new byte[] { 0, 12, 4 };
            byte[] byteReadBuffer = new byte[8];

            Int16[] Int16VlenBuffer = new Int16[] { 0, -12, -4 };
            Int16[] Int16ReadBuffer = new Int16[8];

            UInt16[] UInt16VlenBuffer = new UInt16[] { 0, 12, 4 };
            UInt16[] UInt16ReadBuffer = new UInt16[8];

            Int32[] Int32VlenBuffer = new Int32[] { 0, -12, -4 };
            Int32[] Int32ReadBuffer = new Int32[8];

            UInt32[] UInt32VlenBuffer = new UInt32[] { 0, 12, 4 };
            UInt32[] UInt32ReadBuffer = new UInt32[8];

            Int64[] Int64VlenBuffer = new Int64[] { 0, -12, -4 };
            Int64[] Int64ReadBuffer = new Int64[8];

            UInt64[] UInt64VlenBuffer = new UInt64[] { 0, 12, 4 };
            UInt64[] UInt64ReadBuffer = new UInt64[8];

            float[] floatVlenBuffer = new float[] { 0, 12, 4 };
            float[] floatReadBuffer = new float[8];

            double[] doubleVlenBuffer = new double[] { 0, 12, 4 };
            double[] doubleReadBuffer = new double[8];

            try {
                file = TestHelper.NewFile(filePath);
                dim  = file.AddDim("time", 1);

                // string
                vlen = file.AddVlenType("vlenstring", NcChar.Instance);
                var  = file.AddVar("string", vlen, dim);
                var.PutVar(new Int32[] { 0 }, stringVlenBuffer);
                var.GetVar(new Int32[] { 0 }, out stringReadBuffer);
                Assert.Equals(stringVlenBuffer, stringReadBuffer);

                // sbyte
                vlen = file.AddVlenType("vlensbyte", NcByte.Instance);
                var  = file.AddVar("sbyte", vlen, dim);
                var.PutVar(new Int32[] { 0 }, sbyteVlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, sbyteReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(sbyteVlenBuffer[i], sbyteReadBuffer[i]);
                }

                // byte
                vlen = file.AddVlenType("vlenbyte", NcByte.Instance);
                var  = file.AddVar("byte", vlen, dim);
                var.PutVar(new Int32[] { 0 }, byteVlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, byteReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(byteVlenBuffer[i], byteReadBuffer[i]);
                }

                // Int16
                vlen = file.AddVlenType("vlenInt16", NcShort.Instance);
                var  = file.AddVar("Int16", vlen, dim);
                var.PutVar(new Int32[] { 0 }, Int16VlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, Int16ReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(Int16VlenBuffer[i], Int16ReadBuffer[i]);
                }

                // UInt16
                vlen = file.AddVlenType("vlenUInt16", NcUshort.Instance);
                var  = file.AddVar("UInt16", vlen, dim);
                var.PutVar(new Int32[] { 0 }, UInt16VlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, UInt16ReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(UInt16VlenBuffer[i], UInt16ReadBuffer[i]);
                }

                // Int32
                vlen = file.AddVlenType("vlenInt32", NcInt.Instance);
                var  = file.AddVar("Int32", vlen, dim);
                var.PutVar(new Int32[] { 0 }, Int32VlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, Int32ReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(Int32VlenBuffer[i], Int32ReadBuffer[i]);
                }

                // UInt32
                vlen = file.AddVlenType("vlenUInt32", NcUint.Instance);
                var  = file.AddVar("UInt32", vlen, dim);
                var.PutVar(new Int32[] { 0 }, UInt32VlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, UInt32ReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(UInt32VlenBuffer[i], UInt32ReadBuffer[i]);
                }

                // Int64
                vlen = file.AddVlenType("vlenInt64", NcInt64.Instance);
                var  = file.AddVar("Int64", vlen, dim);
                var.PutVar(new Int32[] { 0 }, Int64VlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, Int64ReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(Int64VlenBuffer[i], Int64ReadBuffer[i]);
                }

                // UInt64
                vlen = file.AddVlenType("vlenUInt64", NcUint64.Instance);
                var  = file.AddVar("UInt64", vlen, dim);
                var.PutVar(new Int32[] { 0 }, UInt64VlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, UInt64ReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(UInt64VlenBuffer[i], UInt64ReadBuffer[i]);
                }

                // float
                vlen = file.AddVlenType("vlenfloat", NcFloat.Instance);
                var  = file.AddVar("float", vlen, dim);
                var.PutVar(new Int32[] { 0 }, floatVlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, floatReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(floatVlenBuffer[i], floatReadBuffer[i]);
                }

                // double
                vlen = file.AddVlenType("vlendouble", NcDouble.Instance);
                var  = file.AddVar("double", vlen, dim);
                var.PutVar(new Int32[] { 0 }, doubleVlenBuffer);
                iLen = var.GetVar(new Int32[] { 0 }, doubleReadBuffer);
                for (int i = 0; i < iLen; i++)
                {
                    Assert.Equals(doubleVlenBuffer[i], doubleReadBuffer[i]);
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #13
0
        public bool TestExtensive()
        {
            NcFile  file    = null;
            NcGroup east    = null;
            NcGroup west    = null;
            NcDim   timeDim = null;
            NcDim   latDim  = null;
            NcDim   lonDim  = null;
            NcVar   timeVar = null;
            NcVar   latVar  = null;
            NcVar   lonVar  = null;
            NcVar   temp    = null;

            float[] buffer;

            double[] timeArray = new double[] { 0, 1, 2, 3, 4, 5, 6 };
            double[] latArray  = new double[] { 40.1, 40.2, 40.3, 40.4, 40.5 };
            double[] lonArray  = new double[] { -73.5, -73.4, -73.3, -73.2 };
            float[]  sstArray  = new float[140];

            double[] readBuffer = new double[140];
            for (int i = 0; i < 140; i++)
            {
                sstArray[i] = (float)i;
            }

            try {
                file = TestHelper.NewFile(filePath);
                east = file.AddGroup("east");
                west = file.AddGroup("west");

                // Fill out east first
                timeDim = east.AddDim("time");   // UNLIMITED
                latDim  = east.AddDim("lat", 5); // 5 lats
                lonDim  = east.AddDim("lon", 4); // 4 lons
                timeVar = east.AddVar("time", NcDouble.Instance, timeDim);
                latVar  = east.AddVar("lat", NcDouble.Instance, latDim);
                lonVar  = east.AddVar("lon", NcDouble.Instance, lonDim);
                temp    = east.AddVar("sst", NcFloat.Instance, (List <NcDim>) new[] { timeDim, latDim, lonDim }.ToList());

                timeVar.PutVar(new Int32[] { 0 }, new Int32[] { timeArray.Length }, timeArray);
                latVar.PutVar(latArray);
                lonVar.PutVar(lonArray);
                temp.PutVar(new Int32[] { 0, 0, 0 }, new Int32[] { 7, 5, 4 }, sstArray);

                // Now check that the writes were successful and that
                // the read buffer matches the writes

                timeVar.GetVar(readBuffer);
                for (int i = 0; i < 7; i++)
                {
                    Assert.Equals(readBuffer[i], timeArray[i]);
                }
                latVar.GetVar(readBuffer);
                for (int i = 0; i < 5; i++)
                {
                    Assert.Equals(readBuffer[i], latArray[i]);
                }
                lonVar.GetVar(readBuffer);
                for (int i = 0; i < 4; i++)
                {
                    Assert.Equals(readBuffer[i], lonArray[i]);
                }
                temp.GetVar(readBuffer);
                for (int i = 0; i < 140; i++)
                {
                    Assert.Equals((float)readBuffer[i], sstArray[i]);
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #14
0
        public bool TestInt64Var()
        {
            NcFile file = null;
            NcDim  dim1 = null;
            NcVar  var1 = null;

            Int64[] buffer     = new Int64[20];
            Int64[] readBuffer = new Int64[20];
            for (int i = 0; i < 20; i++)
            {
                buffer[i] = (Int64)(i - 10);
            }
            try {
                FileSetup(ref file, ref dim1, ref var1, NcInt64.Instance);
                // Test the basic get/put
                var1.PutVar(buffer);
                var1.GetVar(readBuffer);
                for (int i = 0; i < 20; i++)
                {
                    Assert.Equals(readBuffer[i], buffer[i]);
                }
                // test get and put scalars
                var1.PutVar(new Int32[] { 5 }, new Int64[] { -30 });
                var1.GetVar(new Int32[] { 5 }, readBuffer);
                Assert.Equals(readBuffer[0], (Int64)(-30));
                // test get and put 1d arrays
                var1.PutVar(new Int32[] { 2 }, new Int32[] { 4 }, new Int64[] { -20, -20, -20, -20 });
                var1.GetVar(new Int32[] { 2 }, new Int32[] { 4 }, readBuffer);
                Assert.Equals(readBuffer[0], (Int64)(-20));
                Assert.Equals(readBuffer[3], (Int64)(-20));
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[0], (Int64)(-10));
                Assert.Equals(readBuffer[2], (Int64)(-20));
                Assert.Equals(readBuffer[5], (Int64)(-20));
                Assert.Equals(readBuffer[6], (Int64)(-4));

                // test striding
                var1.PutVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, new Int64[] { -40, -40, -40 });
                var1.GetVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, readBuffer);
                Assert.Equals(readBuffer[0], (Int64)(-40));
                Assert.Equals(readBuffer[2], (Int64)(-40));
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[10], (Int64)(-40));
                Assert.Equals(readBuffer[12], (Int64)(-40));
                Assert.Equals(readBuffer[14], (Int64)(-40));
                Assert.Equals(readBuffer[15], (Int64)5);
                // Test ranges
                var1.PutVar(new Int32[] { 0 }, new ulong[] { 9223372036854775807L });
                try {
                    var1.PutVar(new Int32[] { 0 }, new ulong[] { 9223372036854775808L });
                    throw new AssertFailedException("Failed to raise NcRange exception.");
                } catch (NcRange) {
                }
                var1.PutVar(new Int32[] { 0 }, new long[] { -9223372036854775808L });
                // Can't actually show the negative... how would you represent the overflow?
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #15
0
        public bool TestInt32Var()
        {
            NcFile file = null;
            NcDim  dim1 = null;
            NcVar  var1 = null;

            Int32[] buffer     = new Int32[20];
            Int32[] readBuffer = new Int32[20];
            for (int i = 0; i < 20; i++)
            {
                buffer[i] = (Int32)(i - 10);
            }
            try {
                FileSetup(ref file, ref dim1, ref var1, NcInt.Instance);
                // Test the basic get/put
                var1.PutVar(buffer);
                var1.GetVar(readBuffer);
                for (int i = 0; i < 20; i++)
                {
                    Assert.Equals(readBuffer[i], buffer[i]);
                }
                // test get and put scalars
                var1.PutVar(new Int32[] { 5 }, new Int32[] { -30 });
                var1.GetVar(new Int32[] { 5 }, readBuffer);
                Assert.Equals(readBuffer[0], (Int32)(-30));
                // test get and put 1d arrays
                var1.PutVar(new Int32[] { 2 }, new Int32[] { 4 }, new Int32[] { -20, -20, -20, -20 });
                var1.GetVar(new Int32[] { 2 }, new Int32[] { 4 }, readBuffer);
                Assert.Equals(readBuffer[0], (Int32)(-20));
                Assert.Equals(readBuffer[3], (Int32)(-20));
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[0], (Int32)(-10));
                Assert.Equals(readBuffer[2], (Int32)(-20));
                Assert.Equals(readBuffer[5], (Int32)(-20));
                Assert.Equals(readBuffer[6], (Int32)(-4));

                // test striding
                var1.PutVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, new Int32[] { -40, -40, -40 });
                var1.GetVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, readBuffer);
                Assert.Equals(readBuffer[0], (Int32)(-40));
                Assert.Equals(readBuffer[2], (Int32)(-40));
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[10], (Int32)(-40));
                Assert.Equals(readBuffer[12], (Int32)(-40));
                Assert.Equals(readBuffer[14], (Int32)(-40));
                Assert.Equals(readBuffer[15], (Int32)5);
                // Test ranges
                var1.PutVar(new Int32[] { 0 }, new long[] { 2147483647 });
                try {
                    var1.PutVar(new Int32[] { 0 }, new long[] { 2147483648 });
                    throw new AssertFailedException("Failed to raise NcRange exception.");
                } catch (NcRange) {
                }
                var1.PutVar(new Int32[] { 0 }, new long[] { -2147483648 });
                try {
                    var1.PutVar(new Int32[] { 0 }, new long[] { -2147483649 });
                    throw new AssertFailedException("Failed to raise NcRange exception.");
                } catch (NcRange) {
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }
Пример #16
0
        public bool TestByteVar()
        {
            NcFile file = null;
            NcDim  dim1 = null;
            NcVar  var1 = null;

            byte[] buffer     = new byte[20];
            byte[] readBuffer = new byte[20];
            for (int i = 0; i < 20; i++)
            {
                buffer[i] = (byte)i;
            }
            try {
                FileSetup(ref file, ref dim1, ref var1, NcByte.Instance);
                // Test the basic get/put
                var1.PutVar(buffer);
                var1.GetVar(readBuffer);
                for (int i = 0; i < 20; i++)
                {
                    Assert.Equals(readBuffer[i], buffer[i]);
                }
                // test get and put scalars
                var1.PutVar(new Int32[] { 5 }, new byte[] { 30 });
                var1.GetVar(new Int32[] { 5 }, readBuffer);
                Assert.Equals(readBuffer[0], (byte)30);

                // test get and put 1d arrays
                var1.PutVar(new Int32[] { 2 }, new Int32[] { 4 }, new byte[] { 20, 20, 20, 20 });
                var1.GetVar(new Int32[] { 2 }, new Int32[] { 4 }, readBuffer);
                Assert.Equals(readBuffer[0], (byte)20);
                Assert.Equals(readBuffer[3], (byte)20);
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[0], (byte)0);
                Assert.Equals(readBuffer[2], (byte)20);
                Assert.Equals(readBuffer[5], (byte)20);
                Assert.Equals(readBuffer[6], (byte)6);

                // test striding
                var1.PutVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, new byte[] { 40, 40, 40, 40 });
                var1.GetVar(new Int32[] { 10 }, new Int32[] { 5 }, new Int32[] { 2 }, readBuffer);
                Assert.Equals(readBuffer[0], (byte)40);
                Assert.Equals(readBuffer[2], (byte)40);
                var1.GetVar(readBuffer);
                Assert.Equals(readBuffer[10], (byte)40);
                Assert.Equals(readBuffer[12], (byte)40);
                Assert.Equals(readBuffer[14], (byte)40);
                Assert.Equals(readBuffer[15], (byte)15);
                // Test ranges
                // Note: NC_BYTE is a SIGNED 8-bit integer, no getting around that.
                var1.PutVar(new Int32[] { 0 }, new int[] { 127 });
                try {
                    var1.PutVar(new Int32[] { 0 }, new int[] { 128 });
                    throw new AssertFailedException("Failed to raise NcRange exception.");
                } catch (NcRange) {
                }
                var1.PutVar(new Int32[] { 0 }, new int[] { -128 });
                try {
                    var1.PutVar(new Int32[] { 0 }, new int[] { -129 });
                    throw new AssertFailedException("Failed to raise NcRange exception.");
                } catch (NcRange) {
                }
            } finally {
                file.Close();
            }
            CheckDelete(filePath);
            return(true);
        }