Esempio n. 1
0
        public void TestRandomGroup()
        {
            //float[,] fa = new float[20,20];
            float[][] fa = new float[20][];
            for (int i = 0; i < fa.Length; i++)
            {
                fa[i] = new float[20];
            }
            float[] pa = new float[3];

            BufferedFile bf = new BufferedFile(
                TestFileSetup.GetTargetFilename("rg1.fits"),
                FileAccess.ReadWrite,
                FileShare.ReadWrite);

            Object[][] data = new Object[1][];
            data[0]    = new Object[2];
            data[0][0] = pa;
            data[0][1] = fa;

            Console.Out.WriteLine("***** Write header ******");
            BasicHDU hdu = Fits.MakeHDU(data);
            Header   hdr = hdu.Header;

            // Change the number of groups
            hdr.AddValue("GCOUNT", 20, "Number of groups");
            hdr.Write(bf);

            Console.Out.WriteLine("***** Write data group by group ******");
            for (int i = 0; i < 20; i += 1)
            {
                for (int j = 0; j < pa.Length; j += 1)
                {
                    pa[j] = i + j;
                }
                for (int j = 0; j < fa.GetLength(0); j += 1)
                {
                    try
                    {
                        //  fa[j, j] = i * j;
                        fa[j][j] = i * j;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine("i ,j value:" + i + "   " + j);
                    }
                }
                // Write a group
                bf.WriteArray(data);
            }

            byte[] padding = new byte[FitsUtil.Padding(20 * ArrayFuncs.ComputeSize(data))];
            Console.Out.WriteLine("****** Write padding ******");
            bf.Write(padding);

            bf.Flush();
            bf.Close();
            bf.Dispose();

            Fits f = null;

            try
            {
                Console.Out.WriteLine("****** Read data back in ******");
                f = new Fits(TestFileSetup.GetTargetFilename("rg1.fits"));
                BasicHDU[] hdus = f.Read();

                data = (Object[][])hdus[0].Kernel;
                // data = hdus[0].Kernel;
                Console.Out.WriteLine("**** Check parameter and data info *****");
                for (int i = 0; i < data.Length; i += 1)
                {
                    pa = (float[])data[i][0];
                    // fa = (float[,]) data[i,1];
                    Array[] tfa = (Array[])data[i][1];
                    for (int j = 0; j < pa.Length; j += 1)
                    {
                        Assert.AreEqual((float)(i + j), pa[j]);
                    }
                    for (int j = 0; j < fa.Length; j += 1)
                    {
                        // Assert.AreEqual("dataTest:" + i + " " + j, (float)(i * j), fa[j,j]);
                        Assert.AreEqual((float)(i * j), ((Array)tfa.GetValue(j)).GetValue(j));
                    }
                }

                f.Close();

                Console.Out.WriteLine("**** Create HDU from kernel *****");
                f = new Fits();

                // Generate a FITS HDU from the kernel.
                f.AddHDU(Fits.MakeHDU(data));
                bf = new BufferedFile(
                    TestFileSetup.GetTargetFilename("rg2.fits"),
                    FileAccess.ReadWrite,
                    FileShare.ReadWrite);

                Console.Out.WriteLine("**** Write new file *****");
                f.Write(bf);
                bf.Flush();
                bf.Close();
                bf.Dispose();
                f.Close();

                Console.Out.WriteLine("**** Read and check *****");
                f    = new Fits(TestFileSetup.GetTargetFilename("rg2.fits"));
                data = (Object[][])f.Read()[0].Kernel;

                for (int i = 0; i < data.Length; i += 1)
                {
                    pa = (float[])data[i][0];
                    //   fa = (float[,]) data[i,1];
                    Array[] tfa = (Array[])data[i][1];

                    for (int j = 0; j < pa.Length; j += 1)
                    {
                        Assert.AreEqual((float)(i + j), pa[j]);
                    }

                    for (int j = 0; j < fa.Length; j += 1)
                    {
                        //Assert.AreEqual("dataTest:" + i + " " + j, (float)(i * j), fa[j,j]);
                        Assert.AreEqual((float)(i * j), ((Array)tfa.GetValue(j)).GetValue(j));
                    }
                }
            }
            finally
            {
                if (f != null)
                {
                    f.Close();
                }
            }
        }
Esempio n. 2
0
        public static void  BufferedFileTest(System.String filename, int numIts, double[] db, double[] db2, float[] fl, float[] fl2, long[] ln, long[] ln2, int[] in_Renamed, int[] in2, short[] sh, short[] sh2, char[] ch, char[] ch2, byte[] by, byte[] by2, bool[] bl, bool[] bl2, int[][][][] multi, int[][][][] multi2)
        {
            int dim = db.Length;

            double ds = SupportClass.Random.NextDouble() - 0.5;
            double ds2;
            float  fs = (float)(SupportClass.Random.NextDouble() - 0.5);
            float  fs2;
            int    is_Renamed = (int)(1000000 * (SupportClass.Random.NextDouble() - 500000));
            int    is2;
            long   ls = (long)(100000000000L * (SupportClass.Random.NextDouble() - 50000000000L));
            long   ls2;
            short  ss = (short)(60000 * (SupportClass.Random.NextDouble() - 30000));
            short  ss2;
//			char cs = (char) (60000 * SupportClass.Random.NextDouble());
            char cs = SupportClass.NextChar();
            char cs2;
            byte bs = (byte)(256 * SupportClass.Random.NextDouble() - 128);
            byte bs2;
            bool bls = (SupportClass.Random.NextDouble() > 0.5);
            bool bls2;

            System.Console.Out.WriteLine("New libraries: nom.tam.util.BufferedFile");
            System.Console.Out.WriteLine("               Using array I/O methods.");

            BufferedFile f = new BufferedFile(filename, FileAccess.ReadWrite);

            ResetTime();
            for (int i = 0; i < numIts; i += 1)
            {
                f.WriteArray(db);
            }
            System.Console.Out.WriteLine("  BF  Dbl write: " + (8 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.WriteArray(fl);
            }
            System.Console.Out.WriteLine("  BF  Flt write: " + (4 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.WriteArray(in_Renamed);
            }
            System.Console.Out.WriteLine("  BF  Int write: " + (4 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.WriteArray(ln);
            }
            System.Console.Out.WriteLine("  BF  Lng write: " + (8 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.WriteArray(sh);
            }
            System.Console.Out.WriteLine("  BF  Sht write: " + (2 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.WriteArray(ch);
            }
            System.Console.Out.WriteLine("  BF  Chr write: " + (2 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.WriteArray(by);
            }
            System.Console.Out.WriteLine("  BF  Byt write: " + (1 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.WriteArray(bl);
            }
            System.Console.Out.WriteLine("  BF  Boo write: " + (1 * dim * numIts) / (1000 * DeltaTime()));

            f.Write((byte)bs);
            f.Write((System.Char)cs);
            f.Write((System.Int16)ss);
            f.Write(is_Renamed);
            f.Write(ls);
            f.Write(fs);
            f.Write(ds);
            f.Write(bls);

            f.WriteArray(multi);
            f.Flush();
            f.Seek(0, SeekOrigin.Begin);

            ResetTime();
            for (int i = 0; i < numIts; i += 1)
            {
                f.ReadArray(db2);
            }
            System.Console.Out.WriteLine("  BF  Dbl read:  " + (8 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.ReadArray(fl2);
            }
            System.Console.Out.WriteLine("  BF  Flt read:  " + (4 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.ReadArray(in2);
            }
            System.Console.Out.WriteLine("  BF  Int read:  " + (4 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.ReadArray(ln2);
            }
            System.Console.Out.WriteLine("  BF  Lng read:  " + (8 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.ReadArray(sh2);
            }
            System.Console.Out.WriteLine("  BF  Sht read:  " + (2 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.ReadArray(ch2);
            }
            System.Console.Out.WriteLine("  BF  Chr read:  " + (2 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.ReadArray(by2);
            }
            System.Console.Out.WriteLine("  BF  Byt read:  " + (1 * dim * numIts) / (1000 * DeltaTime()));
            for (int i = 0; i < numIts; i += 1)
            {
                f.ReadArray(bl2);
            }
            System.Console.Out.WriteLine("  BF  Boo read:  " + (1 * dim * numIts) / (1000 * DeltaTime()));

            bs2  = (byte)f.ReadByte();
            cs2  = f.ReadChar();
            ss2  = f.ReadInt16();
            is2  = f.ReadInt32();
            ls2  = f.ReadInt64();
            fs2  = f.ReadSingle();
            ds2  = f.ReadDouble();
            bls2 = f.ReadBoolean();

            // Now read only pieces of the multidimensional array.
            for (int i = 0; i < 5; i += 1)
            {
                // Skip the odd initial indices and read the evens.
                System.Int64 temp_Int64;
                temp_Int64 = f.Position;
                temp_Int64 = f.Seek(4000) - temp_Int64;
                int generatedAux27 = (int)temp_Int64;
                f.ReadArray(multi2[2 * i + 1]);
            }

            f.Close();

            System.Console.Out.WriteLine("BufferedFile Verification:");
            System.Console.Out.WriteLine("  An error should be reported for double and float NaN's");
            System.Console.Out.WriteLine("  Arrays:");

            for (int i = 0; i < dim; i += 1)
            {
                if (db[i] != db2[i] && !Double.IsNaN(db[i]) && !Double.IsNaN(db2[i]))
                {
                    System.Console.Out.WriteLine("     Double error at " + i + " " + db[i] + " " + db2[i]);
                }
                if (fl[i] != fl2[i] && !Single.IsNaN(fl[i]) && !Single.IsNaN(fl2[i]))
                {
                    System.Console.Out.WriteLine("     Float error at " + i + " " + fl[i] + " " + fl2[i]);
                }
                if (in_Renamed[i] != in2[i])
                {
                    System.Console.Out.WriteLine("     Int error at " + i + " " + in_Renamed[i] + " " + in2[i]);
                }
                if (ln[i] != ln2[i])
                {
                    System.Console.Out.WriteLine("     Long error at " + i + " " + ln[i] + " " + ln2[i]);
                }
                if (sh[i] != sh2[i])
                {
                    System.Console.Out.WriteLine("     Short error at " + i + " " + sh[i] + " " + sh2[i]);
                }
                if (ch[i] != ch2[i])
                {
                    System.Console.Out.WriteLine("     Char error at " + i + " " + (int)ch[i] + " " + (int)ch2[i]);
                }
                if (by[i] != by2[i])
                {
                    System.Console.Out.WriteLine("     Byte error at " + i + " " + by[i] + " " + by2[i]);
                }
                if (bl[i] != bl2[i])
                {
                    System.Console.Out.WriteLine("     Bool error at " + i + " " + bl[i] + " " + bl2[i]);
                }
            }

            System.Console.Out.WriteLine("  Scalars:");
            // Check the scalars.
            if (bls != bls2)
            {
                System.Console.Out.WriteLine("     Bool Scalar mismatch:" + bls + " " + bls2);
            }
            if (bs != bs2)
            {
                System.Console.Out.WriteLine("     Byte Scalar mismatch:" + bs + " " + bs2);
            }
            if (cs != cs2)
            {
                System.Console.Out.WriteLine("     Char Scalar mismatch:" + (int)cs + " " + (int)cs2);
            }
            if (ss != ss2)
            {
                System.Console.Out.WriteLine("     Short Scalar mismatch:" + ss + " " + ss2);
            }
            if (is_Renamed != is2)
            {
                System.Console.Out.WriteLine("     Int Scalar mismatch:" + is_Renamed + " " + is2);
            }
            if (ls != ls2)
            {
                System.Console.Out.WriteLine("     Long Scalar mismatch:" + ls + " " + ls2);
            }
            if (fs != fs2)
            {
                System.Console.Out.WriteLine("     Float Scalar mismatch:" + fs + " " + fs2);
            }
            if (ds != ds2)
            {
                System.Console.Out.WriteLine("     Double Scalar mismatch:" + ds + " " + ds2);
            }

            System.Console.Out.WriteLine("  Multi: odd rows should match");
            for (int i = 0; i < 10; i += 1)
            {
                System.Console.Out.WriteLine("      " + i + " " + multi[i][i][i][i] + " " + multi2[i][i][i][i]);
            }
            System.Console.Out.WriteLine("Done BufferedFile Tests");
        }