public void TestSimpleIO() { Fits f = null; try { FitsFactory.UseAsciiTables = false; f = new Fits(); Object[] data = new Object[] { bytes, bits, bools, shorts, ints, floats, doubles, longs, strings }; f.AddHDU(Fits.MakeHDU(data)); BinaryTableHDU bhdu = (BinaryTableHDU)f.GetHDU(1); bhdu.SetColumnName(0, "bytes", null); bhdu.SetColumnName(1, "bits", "bits later on"); bhdu.SetColumnName(6, "doubles", null); bhdu.SetColumnName(5, "floats", "4 x 4 array"); BufferedFile bf = new BufferedFile( TestFileSetup.GetTargetFilename("bt1.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Flush(); bf.Close(); bf.Dispose(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("bt1.fits")); f.Read(); Assert.AreEqual(2, f.NumberOfHDUs); BinaryTableHDU thdu = (BinaryTableHDU)f.GetHDU(1); Header hdr = thdu.Header; Assert.AreEqual(9, hdr.GetIntValue("TFIELDS")); Assert.AreEqual(2, hdr.GetIntValue("NAXIS")); Assert.AreEqual(8, hdr.GetIntValue("BITPIX")); Assert.AreEqual("BINTABLE", hdr.GetStringValue("XTENSION")); Assert.AreEqual("bytes", hdr.GetStringValue("TTYPE1")); Assert.AreEqual("doubles", hdr.GetStringValue("TTYPE7")); for (int i = 0; i < data.Length; i += 1) { Object col = thdu.GetColumn(i); if (i == 8) { String[] st = (String[])col; for (int j = 0; j < st.Length; j += 1) { st[j] = st[j].Trim(); } } Assert.AreEqual(true, ArrayFuncs.ArrayEquals(data[i], col)); } } finally { if (f != null) { f.Close(); } } }
public void TestDegen2() { FitsFactory.UseAsciiTables = false; Object[] data = new Object[] { new String[] { "a", "b", "c", "d", "e", "f" }, new int[] { 1, 2, 3, 4, 5, 6 }, new float[] { 1f, 2f, 3f, 4f, 5f, 6f }, new String[] { "", "", "", "", "", "" }, new String[] { "a", "", "c", "", "e", "f" }, new String[] { "", "b", "c", "d", "e", "f" }, new String[] { "a", "b", "c", "d", "e", "" }, new String[] { null, null, null, null, null, null }, new String[] { "a", null, "c", null, "e", "f" }, new String[] { null, "b", "c", "d", "e", "f" }, new String[] { "a", "b", "c", "d", "e", null } }; Fits f = null; try { f = new Fits(); f.AddHDU(Fits.MakeHDU(data)); BufferedFile ff = new BufferedFile(TestFileSetup.GetTargetFilename("bt8.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(ff); ff.Flush(); ff.Close(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("bt8.fits")); BinaryTableHDU bhdu = (BinaryTableHDU)f.GetHDU(1); Assert.AreEqual("e", bhdu.GetElement(4, data.Length - 1)); Assert.AreEqual("", bhdu.GetElement(5, data.Length - 1)); String[] col = (String[])bhdu.GetColumn(0); Assert.AreEqual("a", col[0]); Assert.AreEqual("f", col[5]); col = (String[])bhdu.GetColumn(3); Assert.AreEqual("", col[0]); Assert.AreEqual("", col[5]); col = (String[])bhdu.GetColumn(7); // All nulls Assert.AreEqual("", col[0]); Assert.AreEqual("", col[5]); col = (String[])bhdu.GetColumn(8); Assert.AreEqual("a", col[0]); Assert.AreEqual("", col[1]); } finally { if (f != null) { f.Close(); } } }
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 = null; try { f = new Fits(); BasicHDU hdu = Fits.MakeHDU(x); f.AddHDU(hdu); BufferedFile bf = new BufferedFile( TestFileSetup.GetTargetFilename("bt5.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Close(); bf.Dispose(); /* Now get rid of some columns */ BinaryTableHDU xhdu = (BinaryTableHDU)hdu; // First column Assert.AreEqual(3, xhdu.NCols); xhdu.DeleteColumnsIndexOne(1, 1); Assert.AreEqual(2, xhdu.NCols); xhdu.DeleteColumnsIndexZero(1, 1); Assert.AreEqual(1, xhdu.NCols); bf = new BufferedFile( TestFileSetup.GetTargetFilename("bt6.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Close(); bf.Dispose(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("bt6.fits")); xhdu = (BinaryTableHDU)f.GetHDU(1); Assert.AreEqual(1, xhdu.NCols); } finally { if (f != null) { f.Close(); } } }
public void BuildByRow() { Fits f = null; try { f = new Fits( TestFileSetup.GetTargetFilename("bt2.fits"), FileAccess.Read); f.Read(); BinaryTableHDU bhdu = (BinaryTableHDU)f.GetHDU(1); Header hdr = bhdu.Header; BinaryTable btab = (BinaryTable)bhdu.Data; for (int i = 0; i < 50; i += 1) { Object[] row = (Object[])btab.GetRow(i); float[] qx = (float[])row[1]; Array[] p = (Array[])row[0]; float[] pt = (float[])p.GetValue(0); pt[0] = (float)(i * Math.Sin(i)); btab.AddRow(row); } f.Close(); f = new Fits(); f.AddHDU(Fits.MakeHDU(btab)); BufferedFile bf = new BufferedFile( TestFileSetup.GetTargetFilename("bt4.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Flush(); bf.Close(); bf.Dispose(); f.Close(); f = new Fits( TestFileSetup.GetTargetFilename("bt4.fits"), FileAccess.Read); btab = (BinaryTable)f.GetHDU(1).Data; Assert.AreEqual(100, btab.NRows); // Try getting data before we Read in the table. Array[] xf = (Array[])btab.GetColumn(0); Array[] xft = (Array[])xf.GetValue(50); float[] xftt = (float[])xft.GetValue(0); Assert.AreEqual((float)0, (float)xftt[0]); xft = (Array[])xf.GetValue(99); xftt = (float[])xft.GetValue(0); Assert.AreEqual((float)(49 * Math.Sin(49)), (float)xftt[0]); for (int i = 0; i < xf.Length; i += 3) { bool[] ba = (bool[])btab.GetElement(i, 5); float[] fx = (float[])btab.GetElement(i, 1); int trow = i % 50; Assert.AreEqual(true, ArrayFuncs.ArrayEquals(ba, vbool[trow])); // prob 1 Assert.AreEqual(true, ArrayFuncs.ArrayEquals(fx, vf[trow])); } // Fill the table. Data data = f.GetHDU(1).Data; xf = (Array[])btab.GetColumn(0); xft = (Array[])xf.GetValue(50); xftt = (float[])xft.GetValue(0); Assert.AreEqual(0F, (float)xftt[0]); xft = (Array[])xf.GetValue(99); xftt = (float[])xft.GetValue(0); Assert.AreEqual((float)(49 * Math.Sin(49)), (float)xftt[0]); for (int i = 0; i < xf.Length; i += 3) { bool[] ba = (bool[])btab.GetElement(i, 5); float[] fx = (float[])btab.GetElement(i, 1); int trow = i % 50; Assert.AreEqual(true, ArrayFuncs.ArrayEquals(ba, vbool[trow])); // prob 2 Assert.AreEqual(true, ArrayFuncs.ArrayEquals(fx, vf[trow])); } } finally { if (f != null) { f.Close(); } } }
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(); } } }
public void TestSimpleImages() { float[][] img = new float[300][]; for (int i = 0; i < 300; i++) { img[i] = new float[300]; } Fits f = null; try { f = new Fits(); ImageHDU hdu = (ImageHDU)Fits.MakeHDU(img); BufferedFile bf = new BufferedFile( TestFileSetup.GetTargetFilename("ht1.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.AddHDU(hdu); f.Write(bf); bf.Close(); bf.Dispose(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("ht1.fits")); hdu = (ImageHDU)f.GetHDU(0); Header hdr = hdu.Header; Assert.AreEqual(2, hdr.GetIntValue("NAXIS")); Assert.AreEqual(300, hdr.GetIntValue("NAXIS1")); Assert.AreEqual(300, hdr.GetIntValue("NAXIS2")); Assert.AreEqual(300, hdr.GetIntValue("NAXIS2", -1)); Assert.AreEqual(-1, hdr.GetIntValue("NAXIS3", -1)); Assert.AreEqual(-32, hdr.GetIntValue("BITPIX")); Cursor c = hdr.GetCursor(); c.MoveNext(); HeaderCard hc = (HeaderCard)((DictionaryEntry)c.Current).Value; Assert.AreEqual("SIMPLE", hc.Key); c.MoveNext(); hc = (HeaderCard)((DictionaryEntry)c.Current).Value; Assert.AreEqual("BITPIX", hc.Key); c.MoveNext(); hc = (HeaderCard)((DictionaryEntry)c.Current).Value; Assert.AreEqual("NAXIS", hc.Key); c.MoveNext(); hc = (HeaderCard)((DictionaryEntry)c.Current).Value; Assert.AreEqual("NAXIS1", hc.Key); c.MoveNext(); hc = (HeaderCard)((DictionaryEntry)c.Current).Value; Assert.AreEqual("NAXIS2", hc.Key); } finally { if (f != null) { f.Close(); } } }