Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        /// <summary>Read a single element from the table.  This returns an array of dimension 1.</summary>
        private Array ParseSingleElement(int row, int col)
        {
            Array res = new Array[1];

            try
            {
                GetBuffer(lengths[col], fileOffset + row * rowLen + offsets[col]);
            }
            catch (IOException)
            {
                buffer = null;
                throw new FitsException("Unable to read element");
            }
            res.SetValue(ArrayFuncs.NewInstance(types[col], 1), 0);

            if (ExtractElement(0, lengths[col], res, 0, 0, nulls[col]))
            {
                buffer = null;
                return((Array)res.GetValue(0));
            }
            else
            {
                buffer = null;
                return(null);
            }
        }
Ejemplo n.º 3
0
        /// <summary>Read a single row from the table.  This returns a set of arrays of dimension 1.</summary>
        private Array ParseSingleRow(int row)
        {
            int   offset = row * rowLen;
            Array res    = new Array[nFields];

            try
            {
                GetBuffer(rowLen, fileOffset + row * rowLen);
            }
            catch (IOException)
            {
                throw new FitsException("Unable to read row");
            }

            for (int i = 0; i < nFields; i += 1)
            {
                res.SetValue(ArrayFuncs.NewInstance(types[i], 1), i);
                if (!ExtractElement(offsets[i], lengths[i], res, i, 0, nulls[i]))
                {
                    res.SetValue(null, i);
                }
            }

            // Invalidate buffer for future use.
            buffer = null;
            return(res);
        }
Ejemplo n.º 4
0
        // .99.1 changes
        /// <summary>Delete rows from a FITS table.</summary>
        public virtual void DeleteRows(int start, int len)
        {
            if (nRows == 0 || start < 0 || start >= nRows || len <= 0)
            {
                return;
            }
            if (start + len > nRows)
            {
                len = nRows - start;
            }

            // .99.2 changes
            //  Data d = (Data)DataArray;
            Object dummy_o = DataArray;

            try
            {
                for (int i = 0; i < nFields; i += 1)
                {
                    Object o = ArrayFuncs.NewInstance(types[i], nRows - len);
                    Array.Copy((Array)data[i], 0, (Array)o, 0, start);
                    Array.Copy((Array)data[i], start + len, (Array)o, start, nRows - len - start);
                    data[i] = o;
                }
                nRows -= len;
            }
            catch (Exception e)
            {
                throw new FitsException("Error deleting row:" + e);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// method to write data
        /// </summary>
        /// <param name="o"></param>
        public override void Write(ArrayDataIO o)
        {
            // Don't need to write null data (noted by Jens Knudstrup)
            if (byteSize == 0)
            {
                return;
            }

            // changes suggested in .97 version:
            if (dataArray == null)
            {
                if (tiler != null)
                {
                    // Need to read in the whole image first.
                    try
                    {
                        dataArray = tiler.CompleteImage;
                    }
                    catch (IOException)
                    {
                        throw new FitsException("Error attempting to fill image");
                    }
                }
                else if (dataArray == null && dataDescription != null)
                {
                    // Need to create an array to match a specified header.
                    dataArray = ArrayFuncs.NewInstance(dataDescription.type, dataDescription.dims);
                }
                else
                {
                    // This image isn't ready to be written!
                    throw new FitsException("Null image data");
                }
            }

            try
            {
                o.WriteArray(dataArray);
            }
            catch (IOException e)
            {
                throw new FitsException("IO Error on image write: " + e);
            }

            byte[] padding = new byte[FitsUtil.Padding(TrueSize)];
            try
            {
                o.Write(padding);
                o.Flush();
            }
            catch (IOException e)
            {
                throw new FitsException("Error writing padding: " + e);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Method to read data
        /// </summary>
        /// <param name="i"></param>
        public override void Read(ArrayDataIO i)
        {
            // Don't need to read null data (noted by Jens Knudstrup)
            if (byteSize == 0)
            {
                return;
            }
            SetFileOffset(i);

            //if(i is RandomAccess)
            if (i.CanSeek)
            {
                //tiler = new ImageDataTiler(this, (RandomAccess) i, ((RandomAccess) i).FilePointer, dataDescription);
                tiler = new ImageDataTiler(this, (RandomAccess)i, ((Stream)i).Position, dataDescription);
                try
                {
                    double pos = i.Position;
                    //pos = i.Seek((int)byteSize) - pos;
                    i.Seek(byteSize);
                }
                catch (IOException e)
                {
                    throw new FitsException("Unable to skip over data:" + e);
                }
            }
            else
            {
                dataArray = ArrayFuncs.NewInstance(dataDescription.type, dataDescription.dims);
                try
                {
                    i.ReadArray(dataArray);
                }
                catch (IOException e)
                {
                    throw new FitsException("Unable to read image data:" + e);
                }

                tiler = new ImageDataTiler(this, null, 0, dataDescription);
            }

            int pad = FitsUtil.Padding(TrueSize);

            try
            {
                long pos = i.Seek(pad);
                if (pos != pad)
                {
                    throw new FitsException("Error skipping padding");
                }
            }
            catch (IOException e)
            {
                throw new FitsException("Error reading image padding:" + e);
            }
        }
Ejemplo n.º 7
0
        /// <summary>Extract a single element from a table.  This returns
        /// an array of length 1.</summary>
        private Array SingleElement(int row, int col)
        {
            Array res = null;

            if (isNull_Renamed_Field == null || !isNull_Renamed_Field[row * nFields + col])
            {
                res = ArrayFuncs.NewInstance(types[col], 1);
                Array.Copy((Array)data[col], row, res, 0, 1);
            }
            return(res);
        }
Ejemplo n.º 8
0
        /// <summary>Extract a single row from a table.  This returns
        /// an array of Objects each of which is an array of length 1.</summary>
        private Array SingleRow(int row)
        {
            Array res = new Array[nFields];

            for (int i = 0; i < nFields; i += 1)
            {
                if (isNull_Renamed_Field == null || !isNull_Renamed_Field[row * nFields + i])
                {
                    res.SetValue(ArrayFuncs.NewInstance(types[i], 1), i);
                    Array.Copy((Array)data[i], row, (Array)res.GetValue(i), 0, 1);
                }
            }
            return(res);
        }
Ejemplo n.º 9
0
        /// <summary>Add a row to the FITS table.</summary>
        public virtual int AddRow(Array newRow)
        {
            // If there are no fields, then this is the
            // first row.  We need to add in each of the columns
            // to get the descriptors set up.
            if (nFields == 0)
            {
                for (int i = 0; i < newRow.Length; i += 1)
                {
                    AddColumn(newRow.GetValue(i));
                }
            }
            else
            {
                for (int i = 0; i < nFields; i += 1)
                {
                    try
                    {
                        Array o = ArrayFuncs.NewInstance(types[i], nRows + 1);
                        Array.Copy((Array)data[i], 0, o, 0, nRows);
                        Array.Copy((Array)newRow.GetValue(i), 0, o, nRows, 1);
                        data[i] = o;
                    }
                    catch (Exception e)
                    {
                        throw new FitsException("Error adding row:" + e);
                    }
                }
                nRows += 1;
            }

            // Invalidate the buffer
            buffer = null;

            return(nRows);
        }
Ejemplo n.º 10
0
        /// <summary>Get a subset of the image.  An image tile is returned
        /// as a one-dimensional array although the image will
        /// normally be multi-dimensional.</summary>
        /// <param name="corners">starting corner (using 0 as the start) for the image.</param>
        /// <param name="lenghts">length requested in each dimension.</param>
        public virtual Array GetTile(int[] corners, int[] lengths)
        {
            if (corners.Length != dims.Length || lengths.Length != dims.Length)
            {
                throw new IOException("Inconsistent sub-image request");
            }

            int arraySize = 1;

            for (int i = 0; i < dims.Length; i += 1)
            {
                if (corners[i] < 0 || lengths[i] < 0 || corners[i] + lengths[i] > dims[i])
                {
                    throw new IOException("Sub-image not within image");
                }

                arraySize *= lengths[i];
            }

            Array outArray = ArrayFuncs.NewInstance(base_Renamed, arraySize);

            GetTile(outArray, corners, lengths);
            return(outArray);
        }