Exemplo n.º 1
0
        /// <summary>
        /// Method Write ArrayDataIO
        /// </summary>
        /// <param name="o"></param>
        public override void Write(ArrayDataIO o)
        {
            if (data == null)
            {
                Object generatedAux = DataArray;
            }

            if (data == null)
            {
                throw new FitsException("Null unknown data");
            }

            try
            {
                o.Write(data);
            }
            catch (IOException e)
            {
                throw new FitsException("IO Error on unknown data write" + e);
            }

            byte[] padding = new byte[FitsUtil.Padding(TrueSize)];
            try
            {
                o.Write(padding);
            }
            catch (IOException e)
            {
                throw new FitsException("Error writing padding: " + e);
            }
        }
Exemplo n.º 2
0
        /// <summary>Read the RandomGroupsData.</summary>
        public override void Read(ArrayDataIO str)
        {
            SetFileOffset(str);

            try
            {
                str.ReadArray(dataArray);
            }
            catch (IOException e)
            {
                throw new FitsException("IO error reading Random Groups data " + e);
            }
            int pad = FitsUtil.Padding(TrueSize);

            try
            {
                //System.IO.BinaryReader temp_BinaryReader;
                System.Int64 temp_Int64;
                //temp_BinaryReader = str;
                temp_Int64 = str.Position;                  //temp_BinaryReader.BaseStream.Position;
                temp_Int64 = str.Seek(pad) - temp_Int64;    //temp_BinaryReader.BaseStream.Seek(pad, System.IO.SeekOrigin.Current) - temp_Int64;
                int generatedAux = (int)temp_Int64;
            }
            catch (IOException)
            {
                throw new FitsException("IO error reading padding.");
            }
        }
Exemplo n.º 3
0
 protected void SetFileOffset(Object o)
 {
     if (o is RandomAccess)
     {
         fileOffset = FitsUtil.FindOffset(o);
         dataSize   = TrueSize;
         input      = (RandomAccess)o;
     }
 }
Exemplo n.º 4
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);
            }
        }
Exemplo 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);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Method to read the ArrayDataIO
        /// </summary>
        /// <param name="i"></param>
        public override void Read(ArrayDataIO i)
        {
            SetFileOffset(i);

            if (i is RandomAccess)
            {
                try
                {
                    //BinaryReader temp_BinaryReader;
                    System.Int64 temp_Int64;
                    //temp_BinaryReader = i;
                    temp_Int64 = i.Position;                         //temp_BinaryReader.BaseStream.Position;
                    temp_Int64 = i.Seek((int)byteSize) - temp_Int64; //temp_BinaryReader.BaseStream.Seek((int) byteSize, SeekOrigin.Current) - temp_Int64;
                    int generatedAux = (int)temp_Int64;
                }
                catch (IOException e)
                {
                    throw new FitsException("Unable to skip over data:" + e);
                }
            }
            else
            {
                try
                {
                    i.Read(data);
                }
                catch (IOException e)
                {
                    throw new FitsException("Unable to read unknown data:" + e);
                }
            }

            int pad = FitsUtil.Padding(TrueSize);

            try
            {
                //BinaryReader temp_BinaryReader2;
                System.Int64 temp_Int65;
                //temp_BinaryReader2 = i;
                temp_Int65 = i.Position;                  //temp_BinaryReader2.BaseStream.Position;
                temp_Int65 = i.Seek(pad) - temp_Int65;    //temp_BinaryReader2.BaseStream.Seek(pad, SeekOrigin.Current) - temp_Int65;
                //	if (temp_Int65 != pad)
                if (i.Seek(pad) != pad)
                {
                    throw new FitsException("Error skipping padding");
                }
            }
            catch (IOException e)
            {
                throw new FitsException("Error reading unknown padding:" + e);
            }
        }
Exemplo n.º 7
0
 /// <summary>Associate the FITS object with a given URL</summary>
 /// <param name="myURL">The URL to be associated with the FITS file.</param>
 /// <param name="compressed">Is the data compressed?</param>
 /// <exception cref="FitsException">Thrown if unable to find or open
 /// a file or URL from the string given.</exception>
 public Fits(Uri myURL, bool compressed)
 {
     InitBlock();
     try
     {
         Stream s = FitsUtil.GetURLStream(myURL.OriginalString, 0);
         StreamInit(s, compressed, false);
     }
     catch (IOException)
     {
         throw new FitsException("Unable to open input from URL:" + myURL);
     }
 }
Exemplo n.º 8
0
 /// <summary>Write the RandomGroupsData.</summary>
 public override void Write(ArrayDataIO str)
 {
     try
     {
         str.WriteArray(dataArray);
         byte[] padding = new byte[FitsUtil.Padding(TrueSize)];
         str.Write(padding);
         str.Flush();
     }
     catch (IOException e)
     {
         throw new FitsException("IO error writing random groups data " + e);
     }
 }
Exemplo n.º 9
0
        /// <summary>Read in an ASCII table.  Reading is deferred if
        /// we are reading from a random access device</summary>
        public override void Read(ArrayDataIO str)
        {
            SetFileOffset(str);
            currInput = str;

            //if(str is RandomAccess)
            if (str.CanSeek)
            {
                try
                {
                    //BinaryReader temp_BinaryReader;
                    Int64 temp_Int64;
                    //temp_BinaryReader = str;
                    temp_Int64 = str.Position; //temp_BinaryReader.BaseStream.Position;
                    temp_Int64 = str.Seek(nRows * rowLen) - temp_Int64;
                    //temp_BinaryReader.BaseStream.Seek(nRows * rowLen, SeekOrigin.Current) - temp_Int64;
                    int generatedAux = (int)temp_Int64;
                }
                catch (IOException e)
                {
                    throw new FitsException("Error skipping data: " + e);
                }
            }
            else
            {
                try
                {
                    GetBuffer(rowLen * nRows, 0);
                }
                catch (IOException e)
                {
                    throw new FitsException("Error reading ASCII table:" + e);
                }
            }

            try
            {
                //BinaryReader temp_BinaryReader2;
                Int64 temp_Int65;
                //temp_BinaryReader2 = str;
                temp_Int65 = str.Position; //temp_BinaryReader2.BaseStream.Position;
                temp_Int65 = str.Seek(FitsUtil.Padding(nRows * rowLen)) - temp_Int65;
                //temp_BinaryReader2.BaseStream.Seek(FitsUtil.padding(nRows * rowLen), SeekOrigin.Current) - temp_Int65;
                int generatedAux2 = (int)temp_Int65;
            }
            catch (IOException e)
            {
                throw new FitsException("Error skipping padding:" + e);
            }
        }
Exemplo n.º 10
0
        /// <summary>Associate the FITS object with a file or URL.
        /// *
        /// The string is assumed to be a URL if it begins with
        /// http:  otherwise it is treated as a file name.
        /// If the string ends in .gz it is assumed that
        /// the data is in a compressed format.
        /// All string comparisons are case insensitive.
        /// *
        /// </summary>
        /// <param name="filename">The name of the file or URL to be processed.</param>
        /// <exception cref=""> FitsException Thrown if unable to find or open
        /// a file or URL from the string given.</exception>
        public Fits(String filename)
        {
            InitBlock();

            //Stream inp;

            if (filename == null)
            {
                throw new FitsException("Null FITS Identifier String");
            }

            bool compressed = FitsUtil.IsCompressed(filename);

            int len = filename.Length;

            if (len > 4 && filename.Substring(0, (5) - (0)).ToUpper().Equals("http:".ToUpper()))
            {
                // This seems to be a URL.
                System.Uri myURL;
                try
                {
                    //UPGRADE_TODO: Class 'java.net.URL' was converted to a 'System.Uri' which does not throw an exception if a URL specifies an unknown protocol. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1132"'
                    myURL = new Uri(filename);
                }
                catch (IOException)
                {
                    throw new FitsException("Unable to convert string to URL: " + filename);
                }
                try
                {
                    Stream is_Renamed = System.Net.WebRequest.Create(myURL).GetResponse().GetResponseStream();
                    streamInit(is_Renamed, compressed, false);
                }
                catch (IOException e)
                {
                    throw new FitsException("Unable to open stream from URL:" + filename + " Exception=" + e);
                }
            }
            else if (compressed)
            {
                fileInit(new FileInfo(filename), true);
            }
            else
            {
                randomInit(filename);
            }
        }
Exemplo n.º 11
0
        // change suggested in .99.4 version: Added bool compressed field.
        /// <summary>Associate the FITS object with a file or URL.
        ///
        /// The string is assumed to be a URL if it begins one of the
        /// protocol strings.
        /// If the string ends in .gz it is assumed that
        /// the data is in a compressed format.
        /// All string comparisons are case insensitive.
        ///
        /// <param name="filename"> The name of the file or URL to be processed.</summary>
        /// <exception cref="FitsException"> Thrown if unable to find or open
        /// a file or URL from the string given.
        public Fits(String filename, bool compressed, FileAccess access)
        {
            InitBlock();

            //Stream inp;

            if (filename == null)
            {
                throw new FitsException("Null FITS Identifier String");
            }


            int    len = filename.Length;
            String lc  = filename.ToLower();

            // for (String protocol: urlProtocols)
            for (int i = 0; i < UrlProtocols.Length; i++)
            {
                if (lc.StartsWith(UrlProtocols[i]))
                {
                    // This seems to be a URL.
                    try
                    {
                        // change suggested in .99.5 version: Added to retrieve GZIP stream.
                        Stream s = FitsUtil.GetURLStream(filename, 0);
                        StreamInit(s, compressed, false);
                    }
                    catch (IOException e)
                    {
                        throw new FitsException("Unable to open stream from URL:" + filename + " Exception=" + e);
                    }
                    return;
                }
            }
            if (compressed)
            {
                FileInit(new FileInfo(filename), true);
            }
            else
            {
                RandomInit(new FileInfo(filename), access);
            }
        }
Exemplo n.º 12
0
        /// <summary>Read the heap</summary>
        public virtual void Read(ArrayDataIO str)
        {
            if (str is RandomAccess)
            {
                fileOffset = FitsUtil.FindOffset(str);
                input      = str;
            }

            if (heap != null)
            {
                try
                {
                    str.Read(heap, 0, heapSize);
                }
                catch (IOException e)
                {
                    throw new FitsException("Error reading heap:" + e);
                }
            }
        }
        protected int WriteTable(ArrayDataIO s)
        {
            myHeader.Write(s);

            // write the table
            int nRows = 0;

            for (Array[] els = _rs.GetNextRow(ref _row); els != null;)
            {
                ++nRows;
                for (int col = 0; col < _byteRenderers.Length; ++col)
                {
                    _byteRenderers[col].Write(els[col], s);
                }

                els = _rs.GetNextRow(ref _row);
            }

            // pad
            s.Write(new byte[FitsUtil.Padding((long)nRows * (long)_rowSizeInBytes)]);

            return(nRows);
        }
Exemplo n.º 14
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();
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>Write the data to an output stream.</summary>
        public override void Write(ArrayDataIO str)
        {
            // If buffer is still around we can just reuse it,
            // since nothing we've done has invalidated it.
            if (buffer == null)
            {
                if (data == null)
                {
                    throw new FitsException("Attempt to write undefined ASCII Table");
                }

                buffer = new byte[nRows * rowLen];
                bp     = new ByteParser(buffer);
                for (int i = 0; i < buffer.Length; i += 1)
                {
                    buffer[i] = (byte)' ';                    //SupportClass.Identity(' ');
                }

                ByteFormatter bf = new ByteFormatter();
                bf.TruncationThrow    = false;
                bf.TruncateOnOverflow = true;

                for (int i = 0; i < nRows; i += 1)
                {
                    for (int j = 0; j < nFields; j += 1)
                    {
                        int offset = i * rowLen + offsets[j];
                        int len    = lengths[j];

                        try
                        {
                            if (isNull_Renamed_Field != null && isNull_Renamed_Field[i * nFields + j])
                            {
                                if (nulls[j] == null)
                                {
                                    throw new FitsException("No null value set when needed");
                                }
                                bf.format(nulls[j], buffer, offset, len);
                            }
                            else
                            {
                                if (types[j] == typeof(String))
                                {
                                    String[] s = (String[])data[j];
                                    bf.format(s[i], buffer, offset, len);
                                }
                                else if (types[j] == typeof(int))
                                {
                                    int[] ia = (int[])data[j];
                                    bf.format(ia[i], buffer, offset, len);
                                }
                                else if (types[j] == typeof(float))
                                {
                                    float[] fa = (float[])data[j];
                                    bf.format(fa[i], buffer, offset, len);
                                }
                                else if (types[j] == typeof(double))
                                {
                                    double[] da = (double[])data[j];
                                    bf.format(da[i], buffer, offset, len);
                                }
                                else if (types[j] == typeof(long))
                                {
                                    long[] la = (long[])data[j];
                                    bf.format(la[i], buffer, offset, len);
                                }
                            }
                        }
                        catch (TruncationException)
                        {
                            Console.Error.WriteLine("Ignoring truncation error:" + i + "," + j);
                        }
                    }
                }
            }

            // Now write the buffer.
            try
            {
                str.Write(buffer);
                byte[] padding = new byte[FitsUtil.Padding(buffer.Length)];
                for (int i = 0; i < padding.Length; i += 1)
                {
                    padding[i] = (byte)SupportClass.Identity(' ');
                }
                if (buffer.Length > 0)
                {
                    str.Write(padding);
                }
                str.Flush();
            }
            catch (IOException)
            {
                throw new FitsException("Error writing ASCII Table data");
            }
        }
        /// <summary>
        ///   Writes this binary table with data first going
        ///   to a temp file (and heap file if necessary),
        ///   then with header going to destination stream,
        ///   then merging heap with table data if necessary
        ///   and copying these to destination stream.
        /// </summary>
        /// <param name="s">The destination stream.</param>
        /// steps:
        /// 1) write the table to a tempfile
        ///    byterenderers write data to the heap if necessary
        ///    byterenderers return heap positions and lengths if necessary
        ///    these are returned as a byte sequence like any other data
        ///    and are written to the table like any other data
        /// 2) fix the header
        ///    write the header to the main stream
        /// 3) write the table tempfile to the main stream, merging heap if necessary
        /// what a pain
        protected void WritePadOutput(ArrayDataIO s)
        {
            String tempFilename = CreateTempFilename() + "temp.tmp";
            String heapFilename = CreateTempFilename() + "heap.tmp";
            Stream tempS        = new ActualBufferedStream(new FileStream(tempFilename, FileMode.Create));
            Stream heapS        = null;

            //Stream tempS = new BufferedStream(new FileStream(tempFilename, FileMode.Create), 4096);
            int[] maxColWidths  = null;
            int[] stringIndices = GetStringIndices(_rs.ModelRow);
            int[] byteWidths    = ComputeByteWidths(CopyModelRowStripUnknowns(ReplaceTroolean(_rs.ModelRow), new byte[1]));
            int   nRows         = 0;
            int   maxColWidth   = 0;

            if (_hasStrings)
            {
                maxColWidths = new int[_byteRenderers.Length];
                heapS        = new HeapStream(new FileStream(heapFilename, FileMode.Create));
                //heapS = new BufferedStream(new FileStream(heapFilename, FileMode.Create));
                for (int col = 0; col < _byteRenderers.Length; ++col)
                {
                    _byteRenderers[col].Heap = heapS;
                    maxColWidths[col]        = -1;
                }
            }

            #region 1) write the table
            for (Array[] els = _rs.GetNextRow(ref _row); els != null;)
            {
                ++nRows;
                for (int col = 0; col < _byteRenderers.Length; ++col)
                {
                    _byteRenderers[col].Write(els[col], tempS);
                    if (els[col] is String[] && maxColWidths[col] < ((String[])els[col])[0].Length)
                    {
                        maxColWidths[col] = ((String[])els[col])[0].Length;

                        if (maxColWidth < maxColWidths[col])
                        {
                            maxColWidth = maxColWidths[col];
                        }
                    }
                }

                els = _rs.GetNextRow(ref _row);
            }
            tempS.Flush();
            heapS.Flush();
            #endregion

            #region 2) fix the header and write it to the main stream
            if (_hasStrings)
            {
                // fix NAXIS1, NAXIS2
                Array[] modelRow2 = CopyModelRowReplaceStrings(ReplaceTroolean(_rs.ModelRow), null);
                //modelRow2 = CopyModelRowStripUnknowns(modelRow2, new byte[1]);
                for (int i = 0; i < modelRow2.Length; ++i)
                {
                    if (modelRow2[i] == null)
                    {
                        modelRow2[i] = new String[] { new String(' ', maxColWidths[i]) };
                        myHeader.RemoveCard("TFORM" + (i + 1));
                        myHeader.InsertValue("TFORM" + (i + 1), maxColWidths[i] + "A", null, "TDIM" + (i + 1));
                    }
                }
                modelRow2 = CopyModelRowStripUnknowns(modelRow2, new byte[1]);
                myHeader.RemoveCard("NAXIS1");
                myHeader.InsertValue("NAXIS1", ArrayFuncs.ComputeSize(modelRow2), "row width in bytes", "NAXIS2");
                myHeader.RemoveCard("NAXIS2");
                myHeader.InsertValue("NAXIS2", nRows, "number of rows", "PCOUNT");
                myHeader.RemoveCard("THEAP");
            }
            myHeader.Write(s);
            #endregion

            #region 3) write the table tempfile to the main stream
            tempS.Seek(0, SeekOrigin.Begin);
            heapS.Seek(0, SeekOrigin.Begin);
            // man, if you can't even fit a row into memory, I give up
            byte[] row    = new byte[_rowSizeInBytes]; // this is the old size
            byte[] padBuf = SupportClass.ToByteArray(new String(_padChar, maxColWidth));
            int    len    = 0;
            int    off    = 0;
            for (int nRead = tempS.Read(row, 0, row.Length), rowOffset = 0; nRead > 0; rowOffset = 0)
            {
                for (int i = 0; i < byteWidths.Length; ++i)
                {
                    if (stringIndices[i] != -1)
                    {
                        Array.Reverse(row, stringIndices[i], 4);     // fix the length bytes
                        Array.Reverse(row, stringIndices[i] + 4, 4); // fix the pos bytes
                        len = BitConverter.ToInt32(row, stringIndices[i]);
                        off = BitConverter.ToInt32(row, stringIndices[i] + 4);
                        if (_padLeft)
                        {
                            s.Write(padBuf, 0, maxColWidths[i] - len);
                            heapS.Seek(off, SeekOrigin.Begin);
                            int bufread = heapS.Read(_buf, 0, len);
                            s.Write(_buf, 0, len);
                        }
                        else
                        {
                            heapS.Seek(off, SeekOrigin.Begin);
                            heapS.Read(_buf, 0, len);
                            s.Write(_buf, 0, len);
                            s.Write(padBuf, 0, maxColWidths[i] - len);
                        }
                        rowOffset += 8; // advance 2 ints into the row
                    }
                    else
                    {
                        // s better be buffered, or this is going to be slow.  But since s is ArrayDataIO,
                        // and the only current concrete ArrayDataIO implementations are buffered,
                        // I think we're good.
                        // **** MAKE SURE BUFFEREDSTREAM USED BY BUFFEREDDATASTREAM IS GOOD *****
                        s.Write(row, rowOffset, byteWidths[i]);
                        rowOffset += byteWidths[i];
                    }
                }
                nRead = tempS.Read(row, 0, row.Length);
            }
            tempS.Close();
            heapS.Close();
            File.Delete(tempFilename);
            File.Delete(heapFilename);

            // pad the table
            int tableWidth = 0;
            for (int i = 0; i < byteWidths.Length; ++i)
            {
                if (stringIndices[i] != -1)
                {
                    tableWidth += maxColWidths[i];
                }
                else
                {
                    tableWidth += byteWidths[i];
                }
            }
            int pad = FitsUtil.Padding((long)nRows * (long)tableWidth);
            s.Write(new byte[pad], 0, pad);
            #endregion
        }
        /// <summary>
        ///   Writes this binary table with data first going
        ///   to a temp file (and heap file if necessary),
        ///   then with header going to destination stream,
        ///   then copying data from temp file to destination stream,
        ///   then if necessary copying heap file to destination stream.
        /// </summary>
        /// <param name="s">The destination stream.</param>
        /// steps:
        /// 1) write the table to a tempfile
        ///    byterenderers write data to the heap if necessary
        ///    byterenderers return heap positions and lengths if necessary
        ///    these are returned as a byte sequence like any other data
        ///    and are written to the table like any other data
        /// 2) fix the header
        ///    write the header to the main stream
        /// 3) write the table tempfile to the main stream
        /// 4) write the heap tempfile to the main stream
        /// what a pain
        protected void WriteHeapOutputWithTempTableAndHeapFiles(ArrayDataIO s)
        {
            String     tempFilename = CreateTempFilename() + "temp.tmp";
            String     heapFilename = CreateTempFilename() + "heap.tmp";
            Stream     tempS        = new ActualBufferedStream(new FileStream(tempFilename, FileMode.Create));
            HeapStream heapS        = null;

            int[] maxColWidths = null;
            bool  _doHeap      = _hasStrings && _writeMode != StringWriteMode.TRUNCATE;

            if (_doHeap)
            {
                maxColWidths = new int[_byteRenderers.Length];
                heapS        = new HeapStream(new FileStream(heapFilename, FileMode.Create));
                for (int col = 0; col < _byteRenderers.Length; ++col)
                {
                    _byteRenderers[col].Heap = heapS;
                    maxColWidths[col]        = -1;
                }
            }

            #region 1) write the table
            int nRows = 0;
            for (Array[] els = _rs.GetNextRow(ref _row); els != null;)
            {
                ++nRows;
                for (int col = 0; col < _byteRenderers.Length; ++col)
                {
                    _byteRenderers[col].Write(els[col], tempS);
                    if (_doHeap && els[col] is String[])
                    {
                        maxColWidths[col] = maxColWidths[col] < ((String[])els[col])[0].Length ?
                                            ((String[])els[col])[0].Length : maxColWidths[col];
                    }
                }

                els = _rs.GetNextRow(ref _row);
            }
            tempS.Flush();
            #endregion

            #region 2) fix the header and write it to the main stream
            myHeader.RemoveCard("NAXIS2");
            myHeader.SetNaxis(2, nRows);
            // shoehorn correct heap information into header
            // PCOUNT, THEAP, and TFORMn
            // fix NAXIS1
            if (_doHeap)
            {
                heapS.Flush();
                int theap  = (nRows * _rowSizeInBytes);
                int pad    = FitsUtil.Padding((long)theap + heapS.Position);
                int pcount = (int)heapS.Position + pad;
                // here we correct for swapping out actual strings with heap indices/lengths
                myHeader.RemoveCard("NAXIS1");
                myHeader.InsertCard(new HeaderCard("NAXIS1", _rowSizeInBytes, null), "NAXIS2");
                myHeader.RemoveCard("PCOUNT");
                myHeader.InsertCard(new HeaderCard("PCOUNT", pcount, "Length of heap area in bytes"),
                                    "GCOUNT");
                myHeader.RemoveCard("THEAP");
                myHeader.AddValue("THEAP", theap, "Position of heap wrt start of binary table");
            }

            // fix the TFORMn entries for string columns
            IEnumerator ie    = null;
            bool        found = false;
            //for(int i = 0; i < maxColWidths.Length; ++i, found = false)
            for (int i = 0; i < _rs.ModelRow.Length; ++i, found = false)
            {
                if (_rs.ModelRow[i] is String[])
                {
                    ie = myHeader.GetEnumerator();
                    ie.MoveNext();
                    for (int j = 0; !found && ie.Current != null; ++j, ie.MoveNext())
                    {
                        if (("TFORM" + (i + 1)).Equals(((DictionaryEntry)ie.Current).Key))
                        {
                            if (_doHeap)
                            {
                                myHeader.RemoveCard(j);
                                myHeader.
                                InsertCard(new HeaderCard("TFORM" + (i + 1),
                                                          "1PA(" + maxColWidths[i] + ")",
                                                          null), j);
                                found = true;
                            }
                            else
                            {
                                myHeader.RemoveCard(j);
                                myHeader.
                                InsertCard(new HeaderCard("TFORM" + (i + 1),
                                                          _stringTruncationLength + "A",
                                                          null), j);
                                found = true;
                            }
                        }
                    }
                }
            }
            myHeader.Write(s);
            #endregion

            #region 3) write the table tempfile to the main stream
            tempS.Seek(0, SeekOrigin.Begin);
            for (int nRead = tempS.Read(_buf, 0, _buf.Length); nRead > 0;)
            {
                s.Write(_buf, 0, nRead);
                nRead = tempS.Read(_buf, 0, _buf.Length);
            }
            tempS.Close();
            File.Delete(tempFilename);

            // if there's a heap, pad the heap instead of the table
            if (!_doHeap)
            {
                int pad = FitsUtil.Padding((long)nRows * (long)_rowSizeInBytes);
                s.Write(new byte[pad], 0, pad);
            }
            #endregion

            #region 4) write the heap tempfile to the main stream
            if (_doHeap)
            {
                // calculate the pad
                int pad = FitsUtil.Padding((long)nRows * (long)_rowSizeInBytes + heapS.Position);

                // write to the main stream
                heapS.Seek(0, SeekOrigin.Begin);
                for (int nRead = heapS.Read(_buf, 0, _buf.Length); nRead > 0;)
                {
                    s.Write(_buf, 0, nRead);
                    nRead = heapS.Read(_buf, 0, _buf.Length);
                }
                heapS.Close();
                File.Delete(heapFilename);

                // pad the file
                s.Write(new byte[pad], 0, pad);
            }
            #endregion
        }
        /// <summary>
        ///   Writes this binary table with heap temp file if necessary,
        ///   then fixing nRows and PCOUNT in header,
        ///   then if necessary copying heap file to destination stream.
        /// </summary>
        /// <param name="s">The destination stream.</param>
        /// steps:
        /// 1) write the header to the main stream
        ///    write the table to the main stream
        ///    byterenderers write data to the heap if necessary
        ///    byterenderers return heap positions and lengths if necessary
        ///    these are returned as a byte sequence like any other data
        ///    and are written to the table like any other data
        /// 2) fix the header
        /// 3) write the heap tempfile to the main stream
        /// what a pain
        protected void WriteHeapOutputWithTempHeapFile(ArrayDataIO s)
        {
            String     heapFilename = CreateTempFilename() + "heap.tmp";
            HeapStream heapS        = null;

            int[] maxColWidths = null;

            if (_hasStrings)
            {
                maxColWidths = new int[_byteRenderers.Length];
                heapS        = new HeapStream(new FileStream(heapFilename, FileMode.Create));
                for (int col = 0; col < _byteRenderers.Length; ++col)
                {
                    _byteRenderers[col].Heap = heapS;
                    maxColWidths[col]        = -1;
                }
            }

            #region 1) write the header and the table
            // prep header to make sure it will line up properly with the table later on.
            // since we made the header, we know that anything we add later
            // (except THEAP, accounted for here) will already have been there,
            // so we're not inflating the header
            myHeader.RemoveCard("THEAP");
            if (_hasStrings && _writeMode == StringWriteMode.HEAP)
            {
                myHeader.AddValue("THEAP", 0, null);
            }
            long headerMark = s.Position;
            myHeader.Write(s);

            int nRows = 0;
            for (Array[] els = _rs.GetNextRow(ref _row); els != null;)
            {
                ++nRows;
                for (int col = 0; col < _byteRenderers.Length; ++col)
                {
                    _byteRenderers[col].Write(els[col], s);
                    if (els[col] is String[])
                    {
                        maxColWidths[col] = maxColWidths[col] < ((String[])els[col])[0].Length ?
                                            ((String[])els[col])[0].Length : maxColWidths[col];
                    }
                }

                els = _rs.GetNextRow(ref _row);
            }

            // pad the table.  if there's a heap, pad the heap instead
            if (!_hasStrings)
            {
                int pad = FitsUtil.Padding((long)nRows * (long)_rowSizeInBytes);
                s.Write(new byte[pad], 0, pad);
            }
            s.Flush();
            #endregion

            #region 2) fix the header and write it to the main stream
            myHeader.RemoveCard("NAXIS2");
            myHeader.SetNaxis(2, nRows);
            // shoehorn correct heap information into header
            // PCOUNT, THEAP, and TFORMn
            // fix NAXIS1
            if (_hasStrings)
            {
                long theap  = (long)nRows * (long)_rowSizeInBytes;
                int  pad    = FitsUtil.Padding(theap + heapS.Position);
                int  pcount = (int)heapS.Position + pad;
                // here we correct for swapping out actual strings with heap indices/lengths
                myHeader.RemoveCard("NAXIS1");
                myHeader.InsertCard(new HeaderCard("NAXIS1", _rowSizeInBytes, null), "NAXIS2");
                myHeader.RemoveCard("PCOUNT");
                myHeader.InsertCard(new HeaderCard("PCOUNT", pcount, "Length of heap area in bytes"),
                                    "GCOUNT");
                myHeader.RemoveCard("THEAP");
                // can't fit a long in here!
                if (pcount > 0 && _writeMode == StringWriteMode.HEAP)
                {
                    myHeader.AddValue("THEAP", (int)theap, "Position of heap wrt start of binary table");
                }

                // fix the TFORMn entries for string columns
                IEnumerator ie    = null;
                bool        found = false;
                for (int i = 0; i < maxColWidths.Length; ++i, found = false)
                {
                    if (maxColWidths[i] > -1)
                    {
                        ie = myHeader.GetEnumerator();
                        ie.MoveNext();
                        for (int j = 0; !found && ie.Current != null; ++j, ie.MoveNext())
                        {
                            if (("TFORM" + (i + 1)).Equals(((DictionaryEntry)ie.Current).Key))
                            {
                                myHeader.RemoveCard(j);
                                myHeader.
                                InsertCard(new HeaderCard("TFORM" + (i + 1),
                                                          "1PA(" + maxColWidths[i] + ")",
                                                          null), j);
                                found = true;
                            }
                        }
                    }
                }
            }
            // rewrite the header
            long heapMark = s.Position;
            s.Seek(headerMark, SeekOrigin.Begin);
            myHeader.Write(s);
            #endregion

            #region 3) write the heap tempfile to the main stream
            if (_hasStrings)
            {
                // calculate the pad
                int pad = FitsUtil.Padding((long)nRows * (long)_rowSizeInBytes + heapS.Position);

                s.Seek(heapMark, SeekOrigin.Begin);

                // write heap to the main stream
                heapS.Seek(0, SeekOrigin.Begin);
                for (int nRead = heapS.Read(_buf, 0, _buf.Length); nRead > 0;)
                {
                    s.Write(_buf, 0, nRead);
                    nRead = heapS.Read(_buf, 0, _buf.Length);
                }
                heapS.Close();
                File.Delete(heapFilename);

                // pad the file
                s.Write(new byte[pad], 0, pad);
            }
            #endregion
        }
Exemplo n.º 19
0
 /// <summary>Associate the FITS object with a given uncompressed URL</summary>
 /// <param name="myURL">The URL to be associated with the FITS file.</param>
 /// <exception cref="">FitsException Thrown if unable to use the specified URL.</exception>
 public Fits(Uri myURL) : this(myURL, FitsUtil.IsCompressed(myURL.AbsolutePath))
 {
     InitBlock();
 }
Exemplo n.º 20
0
 /// <summary>Associate the FITS object with a given uncompressed URL</summary>
 /// <param name="myURL">The URL to be associated with the FITS file.</param>
 /// <exception cref="FitsException">Thrown if unable to use the specified URL.</exception>
 public Fits(Uri myURL) : this(myURL, FitsUtil.IsCompressed(myURL.AbsoluteUri))
 {
 }
Exemplo n.º 21
0
 /// <summary>
 /// Asscoiates a fits object with a file specified by the Fileaccess parameter
 /// </summary>
 /// <param name="filename"></param>
 /// <param name="access"></param>
 public Fits(String filename, FileAccess access)
     : this(filename, FitsUtil.IsCompressed(filename), access)
 {
     InitBlock();
 }
Exemplo n.º 22
0
 /// <summary>Associate the FITS object with a file or URL.
 /// *
 /// The string is assumed to be a URL if it begins one of the
 /// protocol strings.
 /// If the string ends in .gz it is assumed that
 /// the data is in a compressed format.
 /// All string comparisons are case insensitive.
 /// *
 /// </summary>
 /// <param name="filename">The name of the file or URL to be processed.</param>
 /// <exception cref=""> FitsException Thrown if unable to find or open
 /// a file or URL from the string given.</exception>
 public Fits(String filename) : this(filename, FitsUtil.IsCompressed(filename), FileAccess.ReadWrite)
 {
     InitBlock();
 }