Esempio n. 1
0
 /// <summary>Given a Header return an appropriate datum.</summary>
 internal static Data DataFactory(Header hdr)
 {
     if (ImageHDU.IsHeader(hdr))
     {
         return(ImageHDU.ManufactureData(hdr));
     }
     else if (RandomGroupsHDU.IsHeader(hdr))
     {
         return(RandomGroupsHDU.ManufactureData(hdr));
     }
     else if (useAsciiTables && AsciiTableHDU.IsHeader(hdr))
     {
         return(AsciiTableHDU.ManufactureData(hdr));
     }
     else if (BinaryTableHDU.IsHeader(hdr))
     {
         return(BinaryTableHDU.ManufactureData(hdr));
     }
     else if (UndefinedHDU.IsHeader(hdr))
     {
         return(UndefinedHDU.ManufactureData(hdr));
     }
     else
     {
         throw new FitsException("Unrecognizable header in dataFactory");
     }
 }
Esempio n. 2
0
        public void TestBadHeader()
        {
            Fits     f   = new Fits("testdocs\\ht1.fits");
            ImageHDU hdu = (ImageHDU)f.GetHDU(0);
            Header   hdr = hdu.Header;
            Cursor   c   = hdr.GetCursor();

            c = hdr.GetCursor();
            c.MoveNext();
            c.MoveNext();
            c.Remove();
            bool thrown = false;

            try
            {
                hdr.Rewrite();
            }
            catch (Exception)
            {
                thrown = true;
            }

            Assertion.AssertEquals("BITPIX delete", true, thrown);
            f.Close();
        }
Esempio n. 3
0
        public void TestRewrite()
        {
            Fits f = null;

            try
            {
                // Should be rewriteable until we add enough cards to
                // start a new block.
                f = new Fits(TestFileSetup.GetTargetFilename("ht1.fits"));
                ImageHDU hdu = (ImageHDU)f.GetHDU(0);
                Header   hdr = hdu.Header;
                Cursor   c   = hdr.GetCursor();
                c.MoveNext();

                int nc = hdr.NumberOfCards;
                int nb = (nc - 1) / 36;

                while (hdr.Rewriteable)
                {
                    int nbx = (hdr.NumberOfCards - 1) / 36;
                    Assert.AreEqual(nb == nbx, hdr.Rewriteable);
                    c.Add(new HeaderCard("DUMMY" + nbx, null, null));
                }
            }
            finally
            {
                if (f != null)
                {
                    f.Close();
                }
            }
        }
Esempio n. 4
0
        public void TestBadHeader()
        {
            Fits f = null;

            try
            {
                f = new Fits(TestFileSetup.GetTargetFilename("ht1.fits"));
                ImageHDU hdu = (ImageHDU)f.GetHDU(0);
                Header   hdr = hdu.Header;
                Cursor   c   = hdr.GetCursor();

                c = hdr.GetCursor();
                c.MoveNext();
                c.MoveNext();
                c.Remove();
                bool thrown = false;
                try
                {
                    hdr.Rewrite();
                }
                catch (Exception e)
                {
                    thrown = true;
                }

                Assert.AreEqual(true, thrown);
            }
            finally
            {
                f.Close();
            }
        }
Esempio n. 5
0
        public void TestSimpleImages()
        {
            float[][] img = new float[300][];
            for (int i = 0; i < 300; i++)
            {
                img[i] = new float[300];
            }

            Fits f = new Fits();

            ImageHDU hdu = (ImageHDU)Fits.MakeHDU(img);

            BufferedFile bf = new BufferedFile("ht1.fits", FileAccess.ReadWrite, FileShare.ReadWrite);

            f.AddHDU(hdu);
            f.Write(bf);
            bf.Close();

            f   = new Fits("ht1.fits");
            hdu = (ImageHDU)f.GetHDU(0);
            Header hdr = hdu.Header;

            Assertion.AssertEquals("NAXIS", 2, hdr.GetIntValue("NAXIS"));
            Assertion.AssertEquals("NAXIS1", 300, hdr.GetIntValue("NAXIS1"));
            Assertion.AssertEquals("NAXIS2", 300, hdr.GetIntValue("NAXIS2"));
            Assertion.AssertEquals("NAXIS2a", 300, hdr.GetIntValue("NAXIS2", -1));
            Assertion.AssertEquals("NAXIS3", -1, hdr.GetIntValue("NAXIS3", -1));

            Assertion.AssertEquals("BITPIX", -32, hdr.GetIntValue("BITPIX"));


            Cursor c = hdr.GetCursor();

            c.MoveNext();
            HeaderCard hc = (HeaderCard)((DictionaryEntry)c.Current).Value;

            Assertion.AssertEquals("SIMPLE_1", "SIMPLE", hc.Key);

            c.MoveNext();
            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            Assertion.AssertEquals("BITPIX_2", "BITPIX", hc.Key);

            c.MoveNext();
            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            Assertion.AssertEquals("NAXIS_3", "NAXIS", hc.Key);

            c.MoveNext();
            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            Assertion.AssertEquals("NAXIS1_4", "NAXIS1", hc.Key);

            c.MoveNext();
            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            Assertion.AssertEquals("NAXIS2_5", "NAXIS2", hc.Key);
            f.Close();
        }
Esempio n. 6
0
        public void test()
        {
            float[][] data = new float[300][];
            for (int i = 0; i < 300; i++)
            {
                data[i] = new float[300];
            }

            for (int i = 0; i < 300; i += 1)
            {
                for (int j = 0; j < 300; j += 1)
                {
                    data[i][j] = 1000 * i + j;
                }
            }

            Fits f = null;

            try
            {
                f = new Fits();

                BufferedFile bf = new BufferedFile(TestFileSetup.GetTargetFilename("tiler1.fits"), FileAccess.ReadWrite,
                                                   FileShare.ReadWrite);
                f.AddHDU(Fits.MakeHDU(data));
                f.Write(bf);
                bf.Close();
                bf.Dispose();
                f.Close();

                f = new Fits(TestFileSetup.GetTargetFilename("tiler1.fits"));
                ImageHDU h = (ImageHDU)f.ReadHDU();

                ImageTiler t = h.Tiler;
                doTile("t1", data, t, 200, 200, 50, 50);
                doTile("t2", data, t, 133, 133, 72, 26);

                Object o = h.Data.Kernel;
                doTile("t3", data, t, 200, 200, 50, 50);
                doTile("t4", data, t, 133, 133, 72, 26);
            }
            finally
            {
                if (f != null)
                {
                    f.Close();
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// writes the header to fits file
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="filename"></param>
        /// <param name="writeMode"></param>
        /// <param name="stringTruncationLength"></param>
        /// <param name="padStringsLeft"></param>
        /// <param name="stringPadChar"></param>
        public static void Write(IDataReader reader, String filename,
                                 StreamedBinaryTableHDU.StringWriteMode writeMode, int stringTruncationLength,
                                 bool padStringsLeft, char stringPadChar)
        {
            Header header = new Header();

            header.Simple = true;
            header.Bitpix = 8;
            header.Naxes  = 0;

            Cursor c = header.GetCursor();

            // move to the end of the header cards
            for (c.MoveNext(); c.MoveNext();)
            {
                ;
            }
            // we know EXTEND isn't there yet
            c.Add("EXTEND", new HeaderCard("EXTEND", true, null));

            ImageHDU hdu1 = new ImageHDU(header, null);

            StreamedBinaryTableHDU hdu2 =
                new StreamedBinaryTableHDU(new DataReaderAdapter(reader), 4096,
                                           writeMode, stringTruncationLength, padStringsLeft, stringPadChar);

            Fits fits = new Fits();

            fits.AddHDU(hdu1);
            fits.AddHDU(hdu2);

            Stream s = null;

            try
            {
                s = new FileStream(filename, FileMode.Create);
                fits.Write(s);
                s.Close();
            }
            catch (Exception e)
            {
                s.Close();
                throw (e);
            }
        }
Esempio n. 8
0
        // change suggested in .99.5 version: Method made public from protected.
        /// <summary>Given an object, create the appropriate FITS header to describe it.</summary>
        /// <param name="o">The object to be described.</param>
        public static BasicHDU HDUFactory(Object o)
        {
            Data   d;
            Header h;

            if (ImageHDU.IsData(o))
            {
                d = ImageHDU.Encapsulate(o);
                h = ImageHDU.ManufactureHeader(d);
            }
            else if (RandomGroupsHDU.IsData(o))
            {
                d = RandomGroupsHDU.Encapsulate(o);
                h = RandomGroupsHDU.ManufactureHeader(d);
            }
            else if (useAsciiTables && AsciiTableHDU.IsData(o))
            {
                d = AsciiTableHDU.Encapsulate(o);
                h = AsciiTableHDU.ManufactureHeader(d);
            }
            else if (BinaryTableHDU.IsData(o))
            {
                d = BinaryTableHDU.Encapsulate(o);
                h = BinaryTableHDU.ManufactureHeader(d);
            }
            else if (UndefinedHDU.IsData(o))
            {
                d = UndefinedHDU.Encapsulate(o);
                h = UndefinedHDU.ManufactureHeader(d);
            }
            else
            {
                Console.WriteLine();
                throw new FitsException("Invalid data presented to HDUFactory");
            }

            return(HDUFactory(h, d));
        }
Esempio n. 9
0
        public void TestRewrite()
        {
            // Should be rewriteable until we add enough cards to
            // start a new block.

            Fits     f   = new Fits("testdocs\\ht1.fits");
            ImageHDU hdu = (ImageHDU)f.GetHDU(0);
            Header   hdr = hdu.Header;
            Cursor   c   = hdr.GetCursor();

            c.MoveNext();

            int nc = hdr.NumberOfCards;
            int nb = (nc - 1) / 36;

            while (hdr.Rewriteable)
            {
                int nbx = (hdr.NumberOfCards - 1) / 36;
                Assertion.AssertEquals("Rewrite:" + nbx, nb == nbx, hdr.Rewriteable);
                c.Add(new HeaderCard("DUMMY" + nbx, null, null));
            }
            f.Close();
        }
Esempio n. 10
0
 public ImageHduHandler(ImageHDU hdu)
     : base(hdu)
 {
 }
Esempio n. 11
0
        /// <summary>
        /// writes the header to fits file
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="filename"></param>
        /// <param name="writeMode"></param>
        /// <param name="stringTruncationLength"></param>
        /// <param name="padStringsLeft"></param>
        /// <param name="stringPadChar"></param>
        public static void Write(IDataReader reader, String filename,
                                StreamedBinaryTableHDU.StringWriteMode writeMode, int stringTruncationLength,
                                bool padStringsLeft, char stringPadChar)
        {
            Header header = new Header();
              header.Simple = true;
              header.Bitpix = 8;
              header.Naxes = 0;

              Cursor c = header.GetCursor();
              // move to the end of the header cards
              for(c.MoveNext(); c.MoveNext(););
              // we know EXTEND isn't there yet
              c.Add("EXTEND", new HeaderCard("EXTEND", true, null));

              ImageHDU hdu1 = new ImageHDU(header, null);

              StreamedBinaryTableHDU hdu2 =
            new StreamedBinaryTableHDU(new DataReaderAdapter(reader), 4096,
            writeMode, stringTruncationLength, padStringsLeft, stringPadChar);

              Fits fits = new Fits();
              fits.AddHDU(hdu1);
              fits.AddHDU(hdu2);

              Stream s = null;
              try
              {
            s = new FileStream(filename, FileMode.Create);
            fits.Write(s);
            s.Close();
              }
              catch(Exception e)
              {
            s.Close();
            throw(e);
              }
        }
Esempio n. 12
0
        public void TestCursor()
        {
            Fits     f   = new Fits("testdocs\\ht1.fits");
            ImageHDU hdu = (ImageHDU)f.GetHDU(0);
            Header   hdr = hdu.Header;
            Cursor   c   = hdr.GetCursor();

            c.Key = "XXX";
            c.Add("CTYPE1", new HeaderCard("CTYPE1", "GLON-CAR", "Galactic Longitude"));
            c.Add("CTYPE2", new HeaderCard("CTYPE2", "GLAT-CAR", "Galactic Latitude"));

            c.Key = "CTYPE1";  // Move before CTYPE1
            c.Add("CRVAL1", new HeaderCard("CRVAL1", 0f, "Longitude at reference"));

            c.Key = "CTYPE2"; // Move before CTYPE2
            c.Add("CRVAL2", new HeaderCard("CRVAL2", -90f, "Latitude at reference"));

            c.Key = "CTYPE1";  // Just practicing moving around!!
            c.Add("CRPIX1", new HeaderCard("CRPIX1", 150.0, "Reference Pixel X"));

            c.Key = "CTYPE2";
            c.Add("CRPIX2", new HeaderCard("CRPIX2", 0f, "Reference pixel Y"));
            c.Add("INV2", new HeaderCard("INV2", true, "Invertible axis"));
            c.Add("SYM2", new HeaderCard("SYM2", "YZ SYMMETRIC", "Symmetries..."));

            Assertion.AssertEquals("CTYPE1", "GLON-CAR", hdr.GetStringValue("CTYPE1"));
            Assertion.AssertEquals("CRPIX2", 0f, hdr.GetDoubleValue("CRPIX2", -2f));


            c.Key = "CRVAL1";
            HeaderCard hc = (HeaderCard)((DictionaryEntry)c.Current).Value;

            c.MoveNext();
            Assertion.AssertEquals("CRVAL1_c", "CRVAL1", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("CRPIX1_c", "CRPIX1", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("CTYPE1_c", "CTYPE1", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("CRVAL2_c", "CRVAL2", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("CRPIX2_c", "CRPIX2", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("INV2_c", "INV2", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("SYM2_c", "SYM2", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("CTYPE2_c", "CTYPE2", hc.Key);


            hdr.FindCard("CRPIX1");
            hdr.AddValue("INTVAL1", 1, "An integer value");
            hdr.AddValue("LOG1", true, "A true value");
            hdr.AddValue("LOGB1", false, "A false value");
            hdr.AddValue("FLT1", 1.34, "A float value");
            hdr.AddValue("FLT2", -1.234567890e-134, "A very long float");
            hdr.AddValue("COMMENT", null, "Comment after flt2");


            c.Key = "INTVAL1";

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("INTVAL1", "INTVAL1", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("LOG1", "LOG1", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("LOGB1", "LOGB1", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("FLT1", "FLT1", hc.Key);

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("FLT2", "FLT2", hc.Key);

            c.MoveNext(); // Skip comment
            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            c.MoveNext();
            Assertion.AssertEquals("CRPIX1x", "CRPIX1", hc.Key);

            Assertion.AssertEquals("FLT1", 1.34, hdr.GetDoubleValue("FLT1", 0));


            c.Key = "FLT1";
            c.Remove();
            Assertion.AssertEquals("FLT1", 0f, hdr.GetDoubleValue("FLT1", 0));


            c.Key = "LOGB1";
            hc    = (HeaderCard)((DictionaryEntry)c.Current).Value;
            Assertion.AssertEquals("AftDel1", "LOGB1", hc.Key);
            c.MoveNext();

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            Assertion.AssertEquals("AftDel2", "FLT2", hc.Key);
            c.MoveNext();

            hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
            Assertion.AssertEquals("AftDel3", "Comment after flt2", hc.Comment);
            c.MoveNext();

            f.Close();
        }
Esempio n. 13
0
        public void TestSimpleImages()
        {
            float[][] img = new float[300][];
            for (int i = 0; i < 300; i++)
            {
                img[i] = new float[300];
            }

            Fits f = null;

            try
            {
                f = new Fits();

                ImageHDU     hdu = (ImageHDU)Fits.MakeHDU(img);
                BufferedFile bf  = new BufferedFile(
                    TestFileSetup.GetTargetFilename("ht1.fits"),
                    FileAccess.ReadWrite,
                    FileShare.ReadWrite);
                f.AddHDU(hdu);
                f.Write(bf);
                bf.Close();
                bf.Dispose();
                f.Close();

                f   = new Fits(TestFileSetup.GetTargetFilename("ht1.fits"));
                hdu = (ImageHDU)f.GetHDU(0);
                Header hdr = hdu.Header;

                Assert.AreEqual(2, hdr.GetIntValue("NAXIS"));
                Assert.AreEqual(300, hdr.GetIntValue("NAXIS1"));
                Assert.AreEqual(300, hdr.GetIntValue("NAXIS2"));
                Assert.AreEqual(300, hdr.GetIntValue("NAXIS2", -1));
                Assert.AreEqual(-1, hdr.GetIntValue("NAXIS3", -1));

                Assert.AreEqual(-32, hdr.GetIntValue("BITPIX"));

                Cursor c = hdr.GetCursor();
                c.MoveNext();
                HeaderCard hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
                Assert.AreEqual("SIMPLE", hc.Key);

                c.MoveNext();
                hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
                Assert.AreEqual("BITPIX", hc.Key);

                c.MoveNext();
                hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
                Assert.AreEqual("NAXIS", hc.Key);

                c.MoveNext();
                hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
                Assert.AreEqual("NAXIS1", hc.Key);

                c.MoveNext();
                hc = (HeaderCard)((DictionaryEntry)c.Current).Value;
                Assert.AreEqual("NAXIS2", hc.Key);
            }
            finally
            {
                if (f != null)
                {
                    f.Close();
                }
            }
        }