Example #1
0
        /// <summary>Delete an HDU from the HDU list.</summary>
        /// <param name="n"> The index of the HDU to be deleted.
        /// If n is 0 and there is more than one HDU present, then
        /// the next HDU will be converted from an image to
        /// primary HDU if possible.  If not a dummy header HDU
        /// will then be inserted.</param>
        public virtual void DeleteHDU(int n)
        {
            int size = NumberOfHDUs;

            if (n < 0 || n >= size)
            {
                throw new FitsException("Attempt to delete non-existent HDU:" + n);
            }
            try
            {
                hduList.RemoveAt(n);
                if (n == 0 && size > 1)
                {
                    BasicHDU newFirst = (BasicHDU)hduList[0];
                    if (newFirst.CanBePrimary)
                    {
                        newFirst.PrimaryHDU = true;
                    }
                    else
                    {
                        InsertHDU(BasicHDU.DummyHDU, 0);
                    }
                }
            }
            catch (Exception)
            {
                throw new FitsException("Internal Error: hduList Vector Inconsitency");
            }
        }
        public BaseHduHandler(BasicHDU hdu)
        {
            if (hdu == null)
                 throw new ArgumentNullException("Hdu cannot be null");

             _hdu = hdu;
        }
Example #3
0
        private BaseHduHandler GetHandler(BasicHDU curHdu)
        {
            if (curHdu is ImageHDU)
                return new ImageHduHandler(curHdu as ImageHDU);
            if (curHdu is TableHDU)
                return new TableHduHandler(curHdu as TableHDU);

            return null;
        }
Example #4
0
        public void TestObj()
        {
            FitsFactory.UseAsciiTables = false;

            /*** Create a binary table from an Object[][] array */
            Object[][] x = new Object[5][];
            for (int i = 0; i < 5; i += 1)
            {
                x[i] = new Object[3];

                x[i][0] = new float[] { i };

                string temp = string.Concat("AString", i);
                x[i][1] = new string[] { temp };

                int[][] t = new int[2][];
                for (int j = 0; j < 2; j++)
                {
                    t[j]    = new int[2];
                    t[j][0] = j * i;
                    t[j][1] = (j + 2) * i;
                }
                x[i][2] = t;
            }

            Fits     f   = new Fits();
            BasicHDU hdu = Fits.MakeHDU(x);

            f.AddHDU(hdu);
            BufferedFile bf = new BufferedFile("bt5.fits", FileAccess.ReadWrite, FileShare.ReadWrite);

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


            /* Now get rid of some columns */
            BinaryTableHDU xhdu = (BinaryTableHDU)hdu;

            // First column
            Assertion.AssertEquals("delcol1", 3, xhdu.NCols);
            xhdu.DeleteColumnsIndexOne(1, 1);
            Assertion.AssertEquals("delcol2", 2, xhdu.NCols);

            xhdu.DeleteColumnsIndexZero(1, 1);
            Assertion.AssertEquals("delcol3", 1, xhdu.NCols);

            bf = new BufferedFile("bt6.fits", FileAccess.ReadWrite, FileShare.ReadWrite);
            f.Write(bf);
            bf.Close();

            f = new Fits("bt6.fits");

            xhdu = (BinaryTableHDU)f.GetHDU(1);
            Assertion.AssertEquals("delcol4", 1, xhdu.NCols);
            f.Close();
        }
Example #5
0
        /// <summary>Insert a FITS object into the list of HDUs.</summary>
        /// <param name="myHDU">The HDU to be inserted into the list of HDUs.</param>
        /// <param name="n">The location at which the HDU is to be inserted.</param>
        public virtual void InsertHDU(BasicHDU myHDU, int n)
        {
            if (myHDU == null)
            {
                return;
            }

            if (n < 0 || n > NumberOfHDUs)
            {
                throw new FitsException("Attempt to insert HDU at invalid location: " + n);
            }

            try
            {
                if (n == 0)
                {
                    // Note that the previous initial HDU is no longer the first.
                    // If we were to insert tables backwards from last to first,
                    // we could get a lot of extraneous DummyHDUs but we currently
                    // do not worry about that.

                    if (NumberOfHDUs > 0)
                    {
                        ((BasicHDU)hduList[0]).PrimaryHDU = false;
                    }

                    if (myHDU.CanBePrimary)
                    {
                        myHDU.PrimaryHDU = true;
                        hduList.Insert(0, myHDU);
                    }
                    else
                    {
                        InsertHDU(BasicHDU.DummyHDU, 0);
                        myHDU.PrimaryHDU = false;
                        hduList.Insert(1, (Object)myHDU);
                    }
                }
                else
                {
                    myHDU.PrimaryHDU = false;
                    hduList.Insert(n, myHDU);
                }
            }
            catch (Exception e)
            {
                throw new FitsException("hduList inconsistency in insertHDU: " + e);
            }
        }
Example #6
0
        /// <summary>Return all HDUs for the Fits object.   If the
        /// FITS file is associated with an external stream make
        /// sure that we have exhausted the stream.</summary>
        /// <returns> an array of all HDUs in the Fits object.  Returns
        /// null if there are no HDUs associated with this object.
        /// </returns>
        public virtual BasicHDU[] Read()
        {
            ReadToEnd();

            int size = NumberOfHDUs;

            if (size == 0)
            {
                return(null);
            }

            BasicHDU[] hdus = new BasicHDU[size];
            hduList.CopyTo(hdus);
            return(hdus);
        }
Example #7
0
        // change suggested in .99 version: all the methods added in .99 version.

        #region CHECKSUM Methods

        /// <summary>Add or update the CHECKSUM keyword.</summary>
        /// <param name="hdu">The primary or other header to get the current DATE.</param>
        public static void SetChecksum(BasicHDU hdu)
        {
            /* the next line with the delete is needed to avoid some unexpected
             *  problems with non.tam.fits.Header.checkCard() which otherwise says
             *  it expected PCOUNT and found DATE.
             */
            Header hdr = hdu.Header;

            hdr.DeleteKey("CHECKSUM");

            /* This would need org.nevec.utils.DateUtils compiled before org.nevec.prima.fits ....
             * final String doneAt = DateUtils.dateToISOstring(0) ;
             * We need to save the value of the comment string because this is becoming part
             * of the checksum calculated and needs to be re-inserted again - with the same string -
             * when the second/final call to addVallue() is made below.
             */
            String doneAt = "as of " + FitsDate.FitsDateString;

            hdr.AddValue("CHECKSUM", "0000000000000000", doneAt);

            /* Convert the entire sequence of 2880 byte header cards into a byte array.
             * The main benefit compared to the C implementations is that we do not need to worry
             * about the particular byte order on machines (Linux/VAX/MIPS vs Hp-UX, Sparc...) supposed that
             * the correct implementation is in the write() interface.
             */
            MemoryStream hduByteImage = new MemoryStream();

            //System.err.flush();
            hdu.Write(new BufferedDataStream(hduByteImage));
            byte[] data = hduByteImage.ToArray();
            long   csu  = Checksum(data);

            /* This time we do not use a deleteKey() to ensure that the keyword is replaced "in place".
             * Note that the value of the checksum is actually independent to a permutation of the
             * 80-byte records within the header.
             */
            hdr.AddValue("CHECKSUM", ChecksumEnc(csu, true), doneAt);
        }
Example #8
0
        /// <summary>Read the next HDU on the default input stream.</summary>
        /// <returns>The HDU read, or null if an EOF was detected.
        /// Note that null is only returned when the EOF is detected immediately
        /// at the beginning of reading the HDU.</returns>
        public virtual BasicHDU ReadHDU()
        {
            if (dataStr == null || atEOF)
            {
                return(null);
            }

            Header hdr = Header.ReadHeader(dataStr);

            if (hdr == null)
            {
                atEOF = true;
                return(null);
            }

            Data datum = hdr.MakeData();

            datum.Read(dataStr);
            BasicHDU nextHDU = FitsFactory.HDUFactory(hdr, datum);

            hduList.Add(nextHDU);
            return(nextHDU);
        }
Example #9
0
 /// <summary>Add an HDU to the Fits object.  Users may intermix
 /// calls to functions which read HDUs from an associated
 /// input stream with the addHDU and insertHDU calls,
 /// but should be careful to understand the consequences.
 /// </summary>
 /// <param name="myHDU"> The HDU to be added to the end of the FITS object.</param>
 public virtual void AddHDU(BasicHDU myHDU)
 {
     InsertHDU(myHDU, NumberOfHDUs);
 }
Example #10
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();
                }
            }
        }
Example #11
0
        /// <summary>Return all HDUs for the Fits object.   If the
        /// FITS file is associated with an external stream make
        /// sure that we have exhausted the stream.</summary>
        /// <returns> an array of all HDUs in the Fits object.  Returns
        /// null if there are no HDUs associated with this object.
        /// </returns>
        public virtual BasicHDU[] Read()
        {
            ReadToEnd();

            int size = NumberOfHDUs;

            if (size == 0)
            {
                return null;
            }

            BasicHDU[] hdus = new BasicHDU[size];
            hduList.CopyTo(hdus);
            return hdus;
        }
Example #12
0
        /// <summary>Insert a FITS object into the list of HDUs.</summary>
        /// <param name="myHDU">The HDU to be inserted into the list of HDUs.</param>
        /// <param name="n">The location at which the HDU is to be inserted.</param>
        public virtual void InsertHDU(BasicHDU myHDU, int n)
        {
            if (myHDU == null)
            {
                return ;
            }

            if (n < 0 || n > NumberOfHDUs)
            {
                throw new FitsException("Attempt to insert HDU at invalid location: " + n);
            }

            try
            {

                if (n == 0)
                {

                    // Note that the previous initial HDU is no longer the first.
                    // If we were to insert tables backwards from last to first,
                    // we could get a lot of extraneous DummyHDUs but we currently
                    // do not worry about that.

                    if (NumberOfHDUs > 0)
                    {
                        ((BasicHDU) hduList[0]).PrimaryHDU = false;
                    }

                    if (myHDU.CanBePrimary)
                    {
                        myHDU.PrimaryHDU = true;
                        hduList.Insert(0, myHDU);
                    }
                    else
                    {
                        InsertHDU(BasicHDU.DummyHDU, 0);
                        myHDU.PrimaryHDU = false;
                        hduList.Insert(1, (Object) myHDU);
                    }
                }
                else
                {
                    myHDU.PrimaryHDU = false;
                    hduList.Insert(n, myHDU);
                }
            }
            catch(Exception e)
            {
                throw new FitsException("hduList inconsistency in insertHDU: " + e);
            }
        }
Example #13
0
 /// <summary>Add an HDU to the Fits object.  Users may intermix
 /// calls to functions which read HDUs from an associated
 /// input stream with the addHDU and insertHDU calls,
 /// but should be careful to understand the consequences.
 /// </summary>
 /// <param name="myHDU"> The HDU to be added to the end of the FITS object.</param>
 public virtual void AddHDU(BasicHDU myHDU)
 {
     InsertHDU(myHDU, NumberOfHDUs);
 }
Example #14
0
        /// <summary>Add or update the CHECKSUM keyword.</summary>
        /// <param name="hdu">The primary or other header to get the current DATE.</param>
        public static void SetChecksum(BasicHDU hdu)
        {
            /* the next line with the delete is needed to avoid some unexpected
             *  problems with non.tam.fits.Header.checkCard() which otherwise says
             *  it expected PCOUNT and found DATE.
             */
            Header hdr = hdu.Header;
            hdr.DeleteKey("CHECKSUM") ;

            /* This would need org.nevec.utils.DateUtils compiled before org.nevec.prima.fits ....
            * final String doneAt = DateUtils.dateToISOstring(0) ;
            * We need to save the value of the comment string because this is becoming part
            * of the checksum calculated and needs to be re-inserted again - with the same string -
            * when the second/final call to addVallue() is made below.
            */
            String doneAt = "as of " + FitsDate.FitsDateString;
            hdr.AddValue("CHECKSUM", "0000000000000000", doneAt);

            /* Convert the entire sequence of 2880 byte header cards into a byte array.
             * The main benefit compared to the C implementations is that we do not need to worry
             * about the particular byte order on machines (Linux/VAX/MIPS vs Hp-UX, Sparc...) supposed that
             * the correct implementation is in the write() interface.
             */
            MemoryStream hduByteImage = new MemoryStream() ;
            //System.err.flush();
            hdu.Write(new BufferedDataStream(hduByteImage)) ;
            byte[] data = hduByteImage.ToArray() ;
            long csu = Checksum(data) ;

            /* This time we do not use a deleteKey() to ensure that the keyword is replaced "in place".
            * Note that the value of the checksum is actually independent to a permutation of the
            * 80-byte records within the header.
            */
            hdr.AddValue("CHECKSUM", ChecksumEnc(csu,true), doneAt ) ;
        }
Example #15
0
            private void AddMetadataToFrame(RawFrameInfo info, BasicHDU hdu)
            {
                if (info == null)
                    info = new RawFrameInfo() {ColourSpace = _colourSpaceId};

                if (info.ExposureMs.HasValue)
                    hdu.AddValue("EXPTIME", info.ExposureMs.Value/1000, "");

                if (info.PixelSize.HasValue)
                {
                    hdu.AddValue("XPIXSZ", info.PixelSize.Value, "");
                    hdu.AddValue("YPIXSZ", info.PixelSize.Value, "");
                }

                if (info.Binning > 0)
                {
                    hdu.AddValue("XBINNING", info.Binning, "");
                    hdu.AddValue("YBINNING", info.Binning, "");
                }

                if (info.SensorTemp.HasValue)
                {
                    hdu.AddValue("CCD-TEMP", info.SensorTemp.Value, "");
                }

                switch (info.ColourSpace)
                {
                    case ColourSpaceId.Mono:
                        break;
                    case ColourSpaceId.RGB:
                        hdu.AddValue("COLORTYP", "RGB", "");
                        break;
                    case ColourSpaceId.Bayer_RGGB:
                        hdu.AddValue("COLORTYP", "RGGB", "");
                        break;
                    case ColourSpaceId.Bayer_GRBG:
                        hdu.AddValue("COLORTYP", "GRBG", "");
                        break;
                    case ColourSpaceId.Bayer_GBRG:
                        hdu.AddValue("COLORTYP", "GBRG", "");
                        break;
                    case ColourSpaceId.Bayer_BGGR:
                        hdu.AddValue("COLORTYP", "BGGR", "");
                        break;
                    case ColourSpaceId.Bayer_CYYM:
                        hdu.AddValue("COLORTYP", "CYYM", "");
                        break;
                    case ColourSpaceId.Bayer_YMCY:
                        hdu.AddValue("COLORTYP", "YMCY", "");
                        break;
                    case ColourSpaceId.Bayer_MYYC:
                        hdu.AddValue("COLORTYP", "MYYC", "");
                        break;
                    case ColourSpaceId.Bayer_Unknown:
                        break;
                }

                hdu.AddValue("SWCREATE", "SharpCap", "");
                hdu.AddValue("DATE-OBS", DateTime.UtcNow.ToString("s"), "");

                if (!string.IsNullOrEmpty(info.CameraName))
                    hdu.AddValue("INSTRUME", info.CameraName, "");
            }