// Read a FITS file public void ReadFile() { Initialize(); Fits f = null; try { f = new Fits(TestFileSetup.GetTargetFilename("image1.fits")); BasicHDU[] hdus = f.Read(); Assert.AreEqual(f.NumberOfHDUs, 8); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bimg, hdus[0].Data.Kernel)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(simg, hdus[1].Data.Kernel)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(iimg, hdus[2].Data.Kernel)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(limg, hdus[3].Data.Kernel)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(fimg, hdus[4].Data.Kernel)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(dimg, hdus[5].Data.Kernel)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(img3, hdus[6].Data.Kernel)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(img1, hdus[7].Data.Kernel)); } finally { if (f != null) { f.Close(); } } }
public void TestVar() { Object[] data = new Object[] { floats, vf, vs, vd, shorts, vbool }; Fits f = new Fits(); f.AddHDU(Fits.MakeHDU(data)); //BufferedDataStream bdos = new BufferedDataStream(new FileStream("bt2.fits", FileMode.Open, FileAccess.ReadWrite)); BufferedFile bdos = new BufferedFile("bt2.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); f = new Fits("bt2.fits", FileAccess.Read); f.Read(); BinaryTableHDU bhdu = (BinaryTableHDU)f.GetHDU(1); Header hdr = bhdu.Header; Assertion.AssertEquals("var1", true, hdr.GetIntValue("PCOUNT") > 0); Assertion.AssertEquals("var2", 6, hdr.GetIntValue("TFIELDS")); for (int i = 0; i < data.Length; i += 1) { Assertion.AssertEquals("vardata(" + i + ")", true, ArrayFuncs.ArrayEquals(data[i], bhdu.GetColumn(i))); } }
public void TestRowDelete() { Fits f = null; try { f = new Fits( TestFileSetup.GetTargetFilename( TestFileSetup.GetTargetFilename("bt1.fits"))); f.Read(); BinaryTableHDU thdu = (BinaryTableHDU)f.GetHDU(1); Assert.AreEqual(50, thdu.NRows); thdu.DeleteRows(10, 20); Assert.AreEqual(30, thdu.NRows); double[] dbl = (double[])thdu.GetColumn(6); Assert.AreEqual(dbl[9], doubles[9]); Assert.AreEqual(dbl[10], doubles[30]); BufferedFile bf = new BufferedFile( TestFileSetup.GetTargetFilename("bt1x.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Close(); bf.Dispose(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("bt1x.fits")); f.Read(); thdu = (BinaryTableHDU)f.GetHDU(1); dbl = (double[])thdu.GetColumn(6); Assert.AreEqual(30, thdu.NRows); Assert.AreEqual(9, thdu.NCols); Assert.AreEqual(dbl[9], doubles[9]); Assert.AreEqual(dbl[10], doubles[30]); thdu.DeleteRows(20); Assert.AreEqual(20, thdu.NRows); dbl = (double[])thdu.GetColumn(6); Assert.AreEqual(20, dbl.Length); Assert.AreEqual(dbl[0], doubles[0]); Assert.AreEqual(dbl[19], doubles[39]); } finally { if (f != null) { f.Close(); } } }
public void TestSimpleIO() { FitsFactory.UseAsciiTables = false; Fits 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("bt1.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Flush(); bf.Close(); f = new Fits("bt1.fits"); f.Read(); Assertion.AssertEquals("NHDU", 2, f.NumberOfHDUs); BinaryTableHDU thdu = (BinaryTableHDU)f.GetHDU(1); Header hdr = thdu.Header; Assertion.AssertEquals("HDR1", 9, hdr.GetIntValue("TFIELDS")); Assertion.AssertEquals("HDR2", 2, hdr.GetIntValue("NAXIS")); Assertion.AssertEquals("HDR3", 8, hdr.GetIntValue("BITPIX")); Assertion.AssertEquals("HDR4", "BINTABLE", hdr.GetStringValue("XTENSION")); Assertion.AssertEquals("HDR5", "bytes", hdr.GetStringValue("TTYPE1")); Assertion.AssertEquals("HDR6", "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(); } } Assertion.AssertEquals("Data" + i, true, ArrayFuncs.ArrayEquals(data[i], col)); } f.Close(); }
// Read a FITS file public void ReadFile() { Initialize(); Fits f = new Fits("image1.fits"); BasicHDU[] hdus = f.Read(); Assertion.AssertEquals("HDU count after", f.NumberOfHDUs, 8); Assertion.AssertEquals("byte image", true, ArrayFuncs.ArrayEquals(bimg, hdus[0].Data.Kernel)); Assertion.AssertEquals("short image", true, ArrayFuncs.ArrayEquals(simg, hdus[1].Data.Kernel)); Assertion.AssertEquals("int image", true, ArrayFuncs.ArrayEquals(iimg, hdus[2].Data.Kernel)); Assertion.AssertEquals("long image", true, ArrayFuncs.ArrayEquals(limg, hdus[3].Data.Kernel)); Assertion.AssertEquals("float image", true, ArrayFuncs.ArrayEquals(fimg, hdus[4].Data.Kernel)); Assertion.AssertEquals("double image", true, ArrayFuncs.ArrayEquals(dimg, hdus[5].Data.Kernel)); Assertion.AssertEquals("int3 image", true, ArrayFuncs.ArrayEquals(img3, hdus[6].Data.Kernel)); Assertion.AssertEquals("double1 image", true, ArrayFuncs.ArrayEquals(img1, hdus[7].Data.Kernel)); }
public void TestRowDelete() { Fits f = new Fits("testdocs\\bt1.fits"); f.Read(); BinaryTableHDU thdu = (BinaryTableHDU)f.GetHDU(1); Assertion.AssertEquals("Del1", 50, thdu.NRows); thdu.DeleteRows(10, 20); Assertion.AssertEquals("Del2", 30, thdu.NRows); double[] dbl = (double[])thdu.GetColumn(6); Assertion.AssertEquals("del3", dbl[9], doubles[9]); Assertion.AssertEquals("del4", dbl[10], doubles[30]); BufferedFile bf = new BufferedFile("bt1x.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Close(); f.Close(); f = new Fits("bt1x.fits"); f.Read(); thdu = (BinaryTableHDU)f.GetHDU(1); dbl = (double[])thdu.GetColumn(6); Assertion.AssertEquals("del5", 30, thdu.NRows); Assertion.AssertEquals("del6", 9, thdu.NCols); Assertion.AssertEquals("del7", dbl[9], doubles[9]); Assertion.AssertEquals("del8", dbl[10], doubles[30]); thdu.DeleteRows(20); Assertion.AssertEquals("del9", 20, thdu.NRows); dbl = (double[])thdu.GetColumn(6); Assertion.AssertEquals("del10", 20, dbl.Length); Assertion.AssertEquals("del11", dbl[0], doubles[0]); Assertion.AssertEquals("del12", dbl[19], doubles[39]); f.Close(); }
public void TestVar() { Fits f = null; try { Object[] data = new Object[] { floats, vf, vs, vd, shorts, vbool }; f = new Fits(); f.AddHDU(Fits.MakeHDU(data)); BufferedFile bdos = new BufferedFile( TestFileSetup.GetTargetFilename("bt2.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); bdos.Dispose(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("bt2.fits"), FileAccess.Read); f.Read(); BinaryTableHDU bhdu = (BinaryTableHDU)f.GetHDU(1); Header hdr = bhdu.Header; Assert.AreEqual(true, hdr.GetIntValue("PCOUNT") > 0); Assert.AreEqual(6, hdr.GetIntValue("TFIELDS")); for (int i = 0; i < data.Length; i += 1) { Assert.AreEqual(true, ArrayFuncs.ArrayEquals(data[i], bhdu.GetColumn(i))); } } finally { 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 BuildByRow() { Fits f = new Fits("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 = new Fits(); f.AddHDU(Fits.MakeHDU(btab)); BufferedFile bf = new BufferedFile("bt4.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Flush(); bf.Close(); f = new Fits("bt4.fits", FileAccess.Read); btab = (BinaryTable)f.GetHDU(1).Data; Assertion.AssertEquals("row1", 100, btab.nRow); // 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); Assertion.AssertEquals("row2", (float)0, (float)xftt[0]); xft = (Array[])xf.GetValue(99); xftt = (float[])xft.GetValue(0); Assertion.AssertEquals("row3", (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; Assertion.AssertEquals("row4", true, ArrayFuncs.ArrayEquals(ba, vbool[trow])); // prob 1 Assertion.AssertEquals("row6", 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); Assertion.AssertEquals("row7", 0F, (float)xftt[0]); xft = (Array[])xf.GetValue(99); xftt = (float[])xft.GetValue(0); Assertion.AssertEquals("row8", (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; Assertion.AssertEquals("row9", true, ArrayFuncs.ArrayEquals(ba, vbool[trow])); // prob 2 Assertion.AssertEquals("row11", true, ArrayFuncs.ArrayEquals(fx, vf[trow])); } f.Close(); }
public void TestSimpleIO() { FitsFactory.UseAsciiTables = false; Fits 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("bt1.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Flush(); bf.Close(); f = new Fits("bt1.fits"); f.Read(); Assertion.AssertEquals("NHDU", 2, f.NumberOfHDUs); BinaryTableHDU thdu = (BinaryTableHDU)f.GetHDU(1); Header hdr = thdu.Header; Assertion.AssertEquals("HDR1", 9, hdr.GetIntValue("TFIELDS")); Assertion.AssertEquals("HDR2", 2, hdr.GetIntValue("NAXIS")); Assertion.AssertEquals("HDR3", 8, hdr.GetIntValue("BITPIX")); Assertion.AssertEquals("HDR4", "BINTABLE", hdr.GetStringValue("XTENSION")); Assertion.AssertEquals("HDR5", "bytes", hdr.GetStringValue("TTYPE1")); Assertion.AssertEquals("HDR6", "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(); } } Assertion.AssertEquals("Data" + i, true, ArrayFuncs.ArrayEquals(data[i], col)); } f.Close(); }
private void BuildCalSpecDb() { var db = new CalSpecDatabase(); int totalBad = 0; string[] lines2 = File.ReadAllLines(@"F:\WORK\tangra3\Tangra 3\VideoOperations\Spectroscopy\AbsFluxCalibration\Standards\AbsFlux-TangraStars.csv"); for (int i = 1; i < lines2.Length; i++) { string[] tokens = lines2[i].Split(','); string calSpecId = tokens[0].Trim(); string RA_FK5_Hours = tokens[1].Trim(); string DEG_FK5_Deg = tokens[2].Trim(); string pmRA = tokens[3].Trim(); string pmDec = tokens[4].Trim(); string specType = tokens[5].Trim(); string magV = tokens[6].Trim(); string magBV = tokens[7].Trim(); string absFluxId = tokens[8].Trim(); string stisFlag = tokens[9].Trim(); string fitsFilePath = tokens[10].Trim(); int stisFrom = int.Parse(tokens[11].Trim()); int stisTo = int.Parse(tokens[12].Trim()); string tyc2 = tokens[13].Trim(); string ucac4 = tokens[14].Trim(); if (!string.IsNullOrEmpty(pmRA) && !string.IsNullOrEmpty(pmDec)) { var star = new CalSpecStar() { CalSpecStarId = calSpecId, AbsFluxStarId = absFluxId, STIS_Flag = stisFlag, FITS_File = fitsFilePath, TYC2 = tyc2, U4 = ucac4, pmRA = double.Parse(pmRA), pmDE = double.Parse(pmDec), MagV = double.Parse(magV), MagBV = double.Parse(magBV), SpecType = specType, RA_J2000_Hours = AstroConvert.ToRightAcsension(RA_FK5_Hours), DE_J2000_Deg = AstroConvert.ToDeclination(DEG_FK5_Deg) }; string filePath = Path.GetFullPath(@"Z:\CALSPEC\current_calspec\" + fitsFilePath); using (var bf = new BufferedFile(filePath, FileAccess.Read, FileShare.ReadWrite)) { var fitsFile = new Fits(); fitsFile.Read(bf); BasicHDU imageHDU = fitsFile.GetHDU(1); var table = (ColumnTable)imageHDU.Data.DataArray; double[] wavelengths = (double[])table.Columns[0]; float[] fluxes = (float[])table.Columns[1]; short[] goodnessFlags = (short[])table.Columns[5]; for (int j = 0; j < fluxes.Length; j++) { if (wavelengths[j] < stisFrom) continue; if (wavelengths[j] > stisTo) break; if (goodnessFlags[j] != 0) star.DataPoints.Add(wavelengths[j], fluxes[j]); else totalBad++; } fitsFile.Close(); } db.Stars.Add(star); } } using (var compressedStream = new FileStream(@"F:\WORK\tangra3\Tangra 3\VideoOperations\Spectroscopy\AbsFluxCalibration\Standards\CalSpec.db", FileMode.CreateNew, FileAccess.Write)) using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Compress, true)) { using (var writer = new BinaryWriter(deflateStream)) { db.Serialize(writer); } } using (var compressedStream = new FileStream(@"F:\WORK\tangra3\Tangra 3\VideoOperations\Spectroscopy\AbsFluxCalibration\Standards\CalSpec.db", FileMode.Open, FileAccess.Read)) using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress, true)) { using (var reader = new BinaryReader(deflateStream)) { var db2 = new CalSpecDatabase(reader); Trace.WriteLine(db2.Stars.Count); } } MessageBox.Show(totalBad.ToString() + " bad entries excluded."); string[] fitsFiles = Directory.GetFiles(@"Z:\CALSPEC\current_calspec", "*.fit"); var dist = new Dictionary<int, int>(); foreach (string filePath in fitsFiles) { using (var bf = new BufferedFile(filePath, FileAccess.Read, FileShare.ReadWrite)) { var fitsFile = new Fits(); fitsFile.Read(bf); var bld = new StringBuilder(); BasicHDU headerHDU = fitsFile.GetHDU(0); BasicHDU imageHDU = fitsFile.GetHDU(1); for (int i = 0; i < headerHDU.Header.NumberOfCards; i++) { string cardString = headerHDU.Header.GetCard(i); bld.AppendFormat("# {0}\r\n", cardString); } for (int i = 0; i < imageHDU.Header.NumberOfCards; i++) { string cardString = imageHDU.Header.GetCard(i); bld.AppendFormat("# {0}\r\n", cardString); } var table = (ColumnTable)imageHDU.Data.DataArray; if (table.Columns.Length == 7 && table.Columns[0] is double[] && table.Columns[1] is float[] && table.Columns[2] is float[] && table.Columns[3] is float[] && table.Columns[4] is float[] && table.Columns[5] is short[] && table.Columns[6] is float[]) { double[] wavelengths = (double[])table.Columns[0]; float[] fluxes = (float[])table.Columns[1]; float[] col2 = (float[])table.Columns[2]; float[] col3 = (float[])table.Columns[3]; float[] col4 = (float[])table.Columns[4]; short[] goodnessFlags = (short[])table.Columns[5]; float[] exposures = (float[])table.Columns[6]; for (int i = 0; i < fluxes.Length; i++) { if (wavelengths[i] < 2000) continue; if (wavelengths[i] > 15000) break; bld.Append(wavelengths[i].ToString().PadLeft(20)); bld.Append(fluxes[i].ToString().PadLeft(20)); bld.Append(col2[i].ToString().PadLeft(20)); bld.Append(col3[i].ToString().PadLeft(20)); bld.Append(col4[i].ToString().PadLeft(20)); bld.Append(goodnessFlags[i].ToString().PadLeft(15)); bld.Append(exposures[i].ToString().PadLeft(15)); bld.AppendLine(); int expMS = (int)Math.Round(exposures[i] * 1000); if (!dist.ContainsKey(expMS)) dist.Add(expMS, 0); dist[expMS]++; } } string outFileName = Path.ChangeExtension(filePath, ".txt"); File.WriteAllText(outFileName, bld.ToString()); fitsFile.Close(); } } var output = new StringBuilder(); foreach (int key in dist.Keys) { output.AppendFormat("{0}s = {1}\r\n", (key / 1000.0).ToString("0.0"), dist[key]); } MessageBox.Show(output.ToString()); }
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 TestSet() { Fits f = new Fits("testdocs\\bt2.fits", FileAccess.Read); f.Read(); BinaryTableHDU bhdu = (BinaryTableHDU)f.GetHDU(1); Header hdr = bhdu.Header; // Check the various set methods on variable length data. float[] dta = (float[])bhdu.GetElement(4, 1); dta = new float[] { 22, 21, 20 }; bhdu.SetElement(4, 1, dta); // BufferedDataStream bdos = new BufferedDataStream(new FileStream("bt2a.fits",FileMode.Open)); BufferedFile bdos = new BufferedFile("bt2a.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); f = new Fits("bt2a.fits"); bhdu = (BinaryTableHDU)f.GetHDU(1); float[] xdta = (float[])bhdu.GetElement(4, 1); Assertion.AssertEquals("ts1", true, ArrayFuncs.ArrayEquals(dta, xdta)); Assertion.AssertEquals("ts2", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts5", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); Assertion.AssertEquals("ts4", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), dta)); float[] tvf = new float[] { 101, 102, 103, 104 }; vf[4] = tvf; bhdu.SetColumn(1, vf); Assertion.AssertEquals("ts6", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts7", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assertion.AssertEquals("ts8", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); // bdos = new BufferedDataStream(new FileStream("bt2b.fits",FileMode.Open)); bdos = new BufferedFile("bt2b.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); f.Close(); f = new Fits("bt2b.fits"); bhdu = (BinaryTableHDU)f.GetHDU(1); Assertion.AssertEquals("ts9", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts10", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assertion.AssertEquals("ts11", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); Object[] rw = (Object[])bhdu.GetRow(4); float[] trw = new float[] { -1, -2, -3, -4, -5, -6 }; rw[1] = trw; bhdu.SetRow(4, rw); Assertion.AssertEquals("ts12", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts13", false, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assertion.AssertEquals("ts14", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), trw)); Assertion.AssertEquals("ts15", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); // bdos = new BufferedDataStream(new FileStream("bt2c.fits",FileMode.Open)); bdos = new BufferedFile("bt2c.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); f.Close(); f = new Fits("bt2c.fits"); bhdu = (BinaryTableHDU)f.GetHDU(1); Assertion.AssertEquals("ts16", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts17", false, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assertion.AssertEquals("ts18", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), trw)); Assertion.AssertEquals("ts19", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); 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("rg1.fits", FileAccess.ReadWrite, FileShare.ReadWrite); Object[][] data = new Object[1][]; data[0] = new Object[2]; data[0][0] = pa; data[0][1] = fa; System.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); System.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))]; System.Console.Out.WriteLine("****** Write padding ******"); bf.Write(padding); bf.Flush(); bf.Close(); System.Console.Out.WriteLine("****** Read data back in ******"); Fits f = new Fits("rg1.fits"); BasicHDU[] hdus = f.Read(); data = (Object[][])hdus[0].Kernel; // data = hdus[0].Kernel; System.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) { Assertion.AssertEquals("paramTest:" + i + " " + j, (float)(i + j), pa[j]); } for (int j = 0; j < fa.Length; j += 1) { // Assertion.AssertEquals("dataTest:" + i + " " + j, (float)(i * j), fa[j,j]); Assertion.AssertEquals("dataTest:" + i + " " + j, (float)(i * j), ((Array)tfa.GetValue(j)).GetValue(j)); } } f = new Fits(); System.Console.Out.WriteLine("**** Create HDU from kernel *****"); // Generate a FITS HDU from the kernel. f.AddHDU(Fits.MakeHDU(data)); bf = new BufferedFile("rg2.fits", FileAccess.ReadWrite, FileShare.ReadWrite); System.Console.Out.WriteLine("**** Write new file *****"); f.Write(bf); bf.Flush(); bf.Close(); System.Console.Out.WriteLine("**** Read and check *****"); f = new Fits("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) { Assertion.AssertEquals("paramTest:" + i + " " + j, (float)(i + j), pa[j]); } for (int j = 0; j < fa.Length; j += 1) { //Assertion.AssertEquals("dataTest:" + i + " " + j, (float)(i * j), fa[j,j]); Assertion.AssertEquals("dataTest:" + i + " " + j, (float)(i * j), ((Array)tfa.GetValue(j)).GetValue(j)); } } }
public void TestSet() { 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; // Check the various set methods on variable length data. float[] dta = (float[])bhdu.GetElement(4, 1); dta = new float[] { 22, 21, 20 }; bhdu.SetElement(4, 1, dta); BufferedFile bdos = new BufferedFile( TestFileSetup.GetTargetFilename("bt2a.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); bdos.Dispose(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("bt2a.fits")); bhdu = (BinaryTableHDU)f.GetHDU(1); float[] xdta = (float[])bhdu.GetElement(4, 1); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(dta, xdta)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), dta)); float[] tvf = new float[] { 101, 102, 103, 104 }; vf[4] = tvf; bhdu.SetColumn(1, vf); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); bdos = new BufferedFile( TestFileSetup.GetTargetFilename("bt2b.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); bdos.Dispose(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("bt2b.fits")); bhdu = (BinaryTableHDU)f.GetHDU(1); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); Object[] rw = (Object[])bhdu.GetRow(4); float[] trw = new float[] { -1, -2, -3, -4, -5, -6 }; rw[1] = trw; bhdu.SetRow(4, rw); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assert.AreEqual(false, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), trw)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); // bdos = new BufferedDataStream(new FileStream("bt2c.fits",FileMode.Open)); bdos = new BufferedFile( TestFileSetup.GetTargetFilename("bt2c.fits"), FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); bdos.Dispose(); f.Close(); f = new Fits(TestFileSetup.GetTargetFilename("bt2c.fits")); bhdu = (BinaryTableHDU)f.GetHDU(1); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assert.AreEqual(false, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), trw)); Assert.AreEqual(true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); } finally { if (f != null) { f.Close(); } } }
public void TestRowDelete() { Fits f = new Fits("bt1.fits"); f.Read(); BinaryTableHDU thdu = (BinaryTableHDU)f.GetHDU(1); Assertion.AssertEquals("Del1", 50, thdu.NRows); thdu.DeleteRows(10, 20); Assertion.AssertEquals("Del2", 30, thdu.NRows); double[] dbl = (double[])thdu.GetColumn(6); Assertion.AssertEquals("del3", dbl[9], doubles[9]); Assertion.AssertEquals("del4", dbl[10], doubles[30]); BufferedFile bf = new BufferedFile("bt1x.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bf); bf.Close(); f.Close(); f = new Fits("bt1x.fits"); f.Read(); thdu = (BinaryTableHDU)f.GetHDU(1); dbl = (double[])thdu.GetColumn(6); Assertion.AssertEquals("del5", 30, thdu.NRows); Assertion.AssertEquals("del6", 9, thdu.NCols); Assertion.AssertEquals("del7", dbl[9], doubles[9]); Assertion.AssertEquals("del8", dbl[10], doubles[30]); thdu.DeleteRows(20); Assertion.AssertEquals("del9", 20, thdu.NRows); dbl = (double[])thdu.GetColumn(6); Assertion.AssertEquals("del10", 20, dbl.Length); Assertion.AssertEquals("del11", dbl[0], doubles[0]); Assertion.AssertEquals("del12", dbl[19], doubles[39]); f.Close(); }
public void TestSet() { Fits f = new Fits("bt2.fits", FileAccess.Read); f.Read(); BinaryTableHDU bhdu = (BinaryTableHDU)f.GetHDU(1); Header hdr = bhdu.Header; // Check the various set methods on variable length data. float[] dta = (float[])bhdu.GetElement(4, 1); dta = new float[] { 22, 21, 20 }; bhdu.SetElement(4, 1, dta); // BufferedDataStream bdos = new BufferedDataStream(new FileStream("bt2a.fits",FileMode.Open)); BufferedFile bdos = new BufferedFile("bt2a.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); f = new Fits("bt2a.fits"); bhdu = (BinaryTableHDU)f.GetHDU(1); float[] xdta = (float[])bhdu.GetElement(4, 1); Assertion.AssertEquals("ts1", true, ArrayFuncs.ArrayEquals(dta, xdta)); Assertion.AssertEquals("ts2", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts5", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); Assertion.AssertEquals("ts4", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), dta)); float[] tvf = new float[] { 101, 102, 103, 104 }; vf[4] = tvf; bhdu.SetColumn(1, vf); Assertion.AssertEquals("ts6", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts7", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assertion.AssertEquals("ts8", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); // bdos = new BufferedDataStream(new FileStream("bt2b.fits",FileMode.Open)); bdos = new BufferedFile("bt2b.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); f.Close(); f = new Fits("bt2b.fits"); bhdu = (BinaryTableHDU)f.GetHDU(1); Assertion.AssertEquals("ts9", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts10", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assertion.AssertEquals("ts11", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); Object[] rw = (Object[])bhdu.GetRow(4); float[] trw = new float[] { -1, -2, -3, -4, -5, -6 }; rw[1] = trw; bhdu.SetRow(4, rw); Assertion.AssertEquals("ts12", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts13", false, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assertion.AssertEquals("ts14", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), trw)); Assertion.AssertEquals("ts15", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); // bdos = new BufferedDataStream(new FileStream("bt2c.fits",FileMode.Open)); bdos = new BufferedFile("bt2c.fits", FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(bdos); bdos.Close(); f.Close(); f = new Fits("bt2c.fits"); bhdu = (BinaryTableHDU)f.GetHDU(1); Assertion.AssertEquals("ts16", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(3, 1), vf[3])); Assertion.AssertEquals("ts17", false, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), vf[4])); Assertion.AssertEquals("ts18", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(4, 1), trw)); Assertion.AssertEquals("ts19", true, ArrayFuncs.ArrayEquals(bhdu.GetElement(5, 1), vf[5])); f.Close(); }
private void BuildAbsFlux() { var indexFile = new StringBuilder(); indexFile.AppendLine("BD and HD CALSPEC stars with STIS fluxes between 3000 and 10000 Angstroms"); indexFile.AppendLine("Name FK5_Coordinates_J2000 Type Mag B-V File_Name"); foreach (CalSpecStar star in CalSpecDatabase.Instance.Stars) { if (star.AbsFluxStarId.StartsWith("TYC")) continue; string modifiedId = star.AbsFluxStarId.Replace(" ", "_"); if (modifiedId.Length < 10) modifiedId += "_"; while (modifiedId.Length < 10) { int firstUnderScorePos = modifiedId.IndexOf("_"); modifiedId = modifiedId.Substring(0, firstUnderScorePos) + "_" + modifiedId.Substring(firstUnderScorePos); } string raStr = AstroConvert.ToStringValue(star.RA_J2000_Hours, "HH MM SS.TTT"); string deStr = AstroConvert.ToStringValue(star.DE_J2000_Deg, "+DD MM SS.TT"); string dataFileName = Path.GetFileNameWithoutExtension(star.FITS_File) + "_t1.txt"; string line = string.Format("{0}{1} {2} {3}{4} {5} {6}\r\n", modifiedId.PadRight(14), raStr, deStr, star.SpecType.PadRight(7), star.MagV.ToString("0.00").PadLeft(5), star.MagBV.ToString("0.00").PadLeft(5), dataFileName); indexFile.Append(line); string filePath = Path.GetFullPath(@"Z:\CALSPEC\current_calspec\" + star.FITS_File); double wavelengthFrom = star.DataPoints.Keys.Min(); double wavelengthTo = star.DataPoints.Keys.Max(); using (var bf = new BufferedFile(filePath, FileAccess.Read, FileShare.ReadWrite)) { var fitsFile = new Fits(); fitsFile.Read(bf); BasicHDU imageHDU = fitsFile.GetHDU(1); var table = (ColumnTable) imageHDU.Data.DataArray; double[] wavelengths = (double[])table.Columns[0]; float[] fluxes = (float[])table.Columns[1]; float[] col2 = (float[])table.Columns[2]; float[] col3 = (float[])table.Columns[3]; float[] col4 = (float[])table.Columns[4]; short[] goodnessFlags = (short[])table.Columns[5]; float[] exposures = (float[])table.Columns[6]; var dataFile = new StringBuilder(); dataFile.AppendLine(" WAVELENGTH FLUX STATERROR SYSERROR FWHM DATAQUAL TOTEXP"); dataFile.AppendLine(" 1D 1E 1E 1E 1E 1I 1E"); dataFile.AppendLine(" ANGSTROMS FLAM FLAM FLAM ANGSTROMS NONE SEC"); for (int j = 0; j < fluxes.Length; j++) { if (wavelengths[j] < wavelengthFrom) continue; if (wavelengths[j] > wavelengthTo) break; string dataLine = string.Format("{0}{1}{2}{3}{4} {5}{6}", ((int)Math.Round(wavelengths[j])).ToString().PadLeft(16), fluxes[j].ToString("E4").PadLeft(14), col2[j].ToString("E4").PadLeft(14), col3[j].ToString("E4").PadLeft(14), col4[j].ToString("#.0").PadLeft(13), goodnessFlags[j].ToString(), exposures[j].ToString("E1").PadLeft(12)); dataFile.AppendLine(dataLine); } fitsFile.Close(); File.WriteAllText(@"Z:\AbsFlux\v3\" + dataFileName, dataFile.ToString()); } } File.WriteAllText(@"Z:\AbsFlux\v3\AbsFluxCALSPECstars.txt", indexFile.ToString()); }