Esempio n. 1
0
        /// <summary>Create an ASCII table given a header</summary>
        public AsciiTable(Header hdr)
        {
            nRows = hdr.GetIntValue("NAXIS2");
            nFields = hdr.GetIntValue("TFIELDS");
            rowLen = hdr.GetIntValue("NAXIS1");

            types = new Type[nFields];
            offsets = new int[nFields];
            lengths = new int[nFields];
            nulls = new String[nFields];

            for (int i = 0; i < nFields; i += 1)
            {
                offsets[i] = hdr.GetIntValue("TBCOL" + (i + 1)) - 1;
                String s = hdr.GetStringValue("TFORM" + (i + 1));
                if (offsets[i] < 0 || s == null)
                {
                    throw new FitsException("Invalid Specification for column:" + (i + 1));
                }
                s = s.Trim();
                char c = s[0];
                s = s.Substring(1);
                if (s.IndexOf('.') > 0)
                {
                    s = s.Substring(0, (s.IndexOf('.')) - (0));
                }
                lengths[i] = Int32.Parse(s);

                switch (c)
                {
                    case 'A':
                        types[i] = typeof(String);
                        break;
                    case 'I':
                        if (lengths[i] > 10)
                        {
                            types[i] = typeof(long);
                        }
                        else
                        {
                            types[i] = typeof(int);
                        }
                        break;
                    case 'F':
                    case 'E':
                        types[i] = typeof(float);
                        break;
                    case 'D':
                        types[i] = typeof(double);
                        break;
                    }

                nulls[i] = hdr.GetStringValue("TNULL" + (i + 1));
                if (nulls[i] != null)
                {
                    nulls[i] = nulls[i].Trim();
                }
            }
        }
Esempio n. 2
0
 /// <summary>Check if we can find the length of the data for this header.</summary>
 /// <returns><CODE>true</CODE> if this HDU has a valid header.</returns>
 public static new bool IsHeader(Header hdr)
 {
     if(hdr.GetStringValue("XTENSION") != null && hdr.GetIntValue("NAXIS", - 1) >= 0)
     {
         return true;
     }
     return false;
 }
Esempio n. 3
0
        /// <summary>
        /// Create an UndefinedData object using the specified header.
        /// </summary>
        /// <param name="h"></param>
        public UndefinedData(Header h)
        {
            int size = 1;
            for (int i = 0; i < h.GetIntValue("NAXIS"); i += 1)
            {
                size *= h.GetIntValue("NAXIS" + (i + 1));
            }
            size += h.GetIntValue("PCOUNT");
            if (h.GetIntValue("GCOUNT") > 1)
            {
                size *= h.GetIntValue("GCOUNT");
            }
            size *= System.Math.Abs(h.GetIntValue("BITPIX") / 8);

            data = new byte[size];
            byteSize = size;
        }
Esempio n. 4
0
        protected internal virtual ArrayDesc ParseHeader(Header h)
        {
            int bitpix;
            //			int type;
            int ndim;
            int[] dims;
            int i;

            //Object dataArray;
            Type baseClass;

            int gCount = h.GetIntValue("GCOUNT", 1);
            int pCount = h.GetIntValue("PCOUNT", 0);
            if (gCount > 1 || pCount != 0)
            {
                throw new FitsException("Group data treated as images");
            }

            bitpix = (int) h.GetIntValue("BITPIX", 0);

            if (bitpix == 8)
            {
                baseClass = Type.GetType("System.Byte");
            }
            else if (bitpix == 16)
            {
                baseClass = Type.GetType("System.Int16");
            }
            else if (bitpix == 32)
            {
                baseClass = Type.GetType("System.Int32");
            }
            else if (bitpix == 64)
            {
                /* This isn't a standard for FITS yet...*/
                baseClass = Type.GetType("System.Int64");
            }
            else if (bitpix == - 32)
            {
                baseClass = Type.GetType("System.Single");
            }
            else if (bitpix == - 64)
            {
                baseClass = Type.GetType("System.Double");
            }
            else
            {
                throw new FitsException("Invalid BITPIX:" + bitpix);
            }

            ndim = h.GetIntValue("NAXIS", 0);
            dims = new int[ndim];

            // Note that we have to invert the order of the axes
            // for the FITS file to get the order in the array we
            // are generating.

            byteSize = 1;
            for (i = 0; i < ndim; i += 1)
            {
                int cdim = h.GetIntValue("NAXIS" + (i + 1), 0);
                if (cdim < 0)
                {
                    throw new FitsException("Invalid array dimension:" + cdim);
                }
                byteSize *= cdim;
                dims[ndim - i - 1] = (int) cdim;
            }
            byteSize *= Math.Abs(bitpix) / 8;
            if (ndim == 0)
            {
                byteSize = 0;
            }
            return new ArrayDesc(this, dims, baseClass);
        }
Esempio n. 5
0
        internal static Object[] GenerateSampleRow(Header h)
        {
            int ndim = h.GetIntValue("NAXIS", 0) - 1;
            int[] dims = new int[ndim];

            int bitpix = h.GetIntValue("BITPIX", 0);

            Type baseClass;

            switch (bitpix)
            {
                case 8:
                    baseClass = typeof(byte);
                      break;
                case 16:
                    baseClass = typeof(short);
                      break;
                case 32:
                    baseClass = typeof(int);
                      break;
                case 64:
                    baseClass = typeof(long);
                      break;
                case - 32:
                    baseClass = typeof(float);
                      break;
                case - 64:
                    baseClass = typeof(double);
                      break;
                default:
                    throw new FitsException("Invalid BITPIX:" + bitpix);
            }

            // Note that we have to invert the order of the axes
            // for the FITS file to get the order in the array we
            // are generating.  Also recall that NAXIS1=0, so that
            // we have an 'extra' dimension.
            for (int i = 0; i < ndim; i += 1)
            {
                long cdim = h.GetIntValue("NAXIS" + (i + 2), 0);
                if (cdim < 0)
                {
                    throw new FitsException("Invalid array dimension:" + cdim);
                }
                dims[ndim - i - 1] = (int) cdim;
            }

            Object[] sample = new Object[2];
            sample[0] = ArrayFuncs.NewInstance(baseClass, h.GetIntValue("PCOUNT"));
            sample[1] = ArrayFuncs.NewInstance(baseClass, dims);

            return sample;
        }
Esempio n. 6
0
        /// <summary>Create FITS data object corresponding to a given header.</summary>
        public static Data ManufactureData(Header hdr)
        {
            int gcount = hdr.GetIntValue("GCOUNT", - 1);
            int pcount = hdr.GetIntValue("PCOUNT", - 1);

            if (!hdr.GetBooleanValue("GROUPS") || hdr.GetIntValue("NAXIS1", - 1) != 0 || gcount < 0 || pcount < 0 || hdr.GetIntValue("NAXIS") < 2)
            {
                throw new FitsException("Invalid Random Groups Parameters");
            }

            // Allocate the object.
            Object[][] dataArray;

            if (gcount > 0)
            {
                dataArray = new Object[gcount][];
                for (int i = 0; i < gcount; i++)
                {
                    dataArray[i] = new Object[2];
                }
            }
            else
            {
                dataArray = new Object[0][];
            }

            Object[] sampleRow = GenerateSampleRow(hdr);
            for (int i = 0; i < gcount; i += 1)
            {
                ((Object[][]) dataArray)[i][0] = ((Object[]) ArrayFuncs.DeepClone(sampleRow))[0];
                ((Object[][]) dataArray)[i][1] = ((Object[]) ArrayFuncs.DeepClone(sampleRow))[1];
            }
            return new RandomGroupsData(dataArray);
        }
Esempio n. 7
0
        /// <summary>Create a binary table from given header information.</summary>
        /// <param name="myHeader">header describing what the binary table should look like.</param>
        public BinaryTable(Header myHeader)
        {
            int heapSize = myHeader.GetIntValue("PCOUNT");
            heapOffset = myHeader.GetIntValue("THEAP");

            // .99.1 changes: changed the sequence of code lines.
            int rwsz = myHeader.GetIntValue("NAXIS1");
            nRow = myHeader.GetIntValue("NAXIS2");

            // Subtract out the size of the regular table from
            // the heap offset.

            if (heapOffset > 0)
            {
                heapOffset -= nRow * rwsz;
            }

            if (heapOffset < 0 || heapOffset > heapSize)
            {
                throw new FitsException("Inconsistent THEAP and PCOUNT");
            }

            heap = new FitsHeap(heapSize - heapOffset);
            nCol = myHeader.GetIntValue("TFIELDS");
            rowLen = 0;

            ExtendArrays(nCol);
            for (int col = 0; col < nCol; col += 1)
            {
                rowLen += ProcessCol(myHeader, col);
            }

            // .99.1 changes: Added to replace new value of NAXIS1 keyword.
            HeaderCard card = myHeader.FindCard("NAXIS1");
            card.Value = rowLen.ToString();
            myHeader.UpdateLine("NAXIS1", card);
        }