Exemple #1
0
        private static bool LoadFitsFileInternal <TData>(
            string fileName, IFITSTimeStampReader timeStampReader,
            out TData[,] pixels, out TData medianValue, out Type pixelDataType, out float frameExposure, out bool hasNegativePixels, out short minRawValue, out uint maxVal,
            CheckOpenedFitsFileCallback callback, LoadFitsDataCallback <TData> loadDataCallback)
        {
            Fits fitsFile = new Fits();

            using (BufferedFile bf = new BufferedFile(fileName, FileAccess.Read, FileShare.ReadWrite))
            {
                fitsFile.Read(bf);

                BasicHDU imageHDU    = fitsFile.GetHDU(0);
                Array    pixelsArray = (Array)imageHDU.Data.DataArray;
                return(LoadFitsDataInternal <TData>(
                           imageHDU, pixelsArray, fileName, timeStampReader,
                           out pixels, out medianValue, out pixelDataType, out frameExposure, out hasNegativePixels, out minRawValue, out maxVal,
                           callback, loadDataCallback));
            }
        }
Exemple #2
0
        public static ThreeAxisFITSCubeFrameStream OpenFile(string fileName, VideoController videoController)
        {
            var fitsFile     = new Fits();
            var bufferedFile = new BufferedFile(fileName, FileAccess.Read, FileShare.ReadWrite);

            fitsFile.Read(bufferedFile);

            var imageHDU = fitsFile.GetHDU(0);
            ThreeAxisFITSCubeFrameStream fitsStream = null;

            videoController.SetCursor(Cursors.WaitCursor);

            try
            {
                var hasher = new SHA1CryptoServiceProvider();
                hasher.Initialize();
                byte[] combinedFileNamesBytes = Encoding.UTF8.GetBytes(fileName);
                var    hash         = hasher.ComputeHash(combinedFileNamesBytes, 0, combinedFileNamesBytes.Length);
                var    fitsFileHash = Convert.ToBase64String(hash);

                var frm = new frmDefineFitsCube3D(imageHDU, fitsFileHash, videoController);
                if (videoController.ShowDialog(frm) == DialogResult.OK)
                {
                    videoController.SetFlipSettings(frm.FlipVertically, frm.FlipHorizontally);

                    fitsStream = new ThreeAxisFITSCubeFrameStream(
                        fileName, fitsFile, bufferedFile, imageHDU, frm.TimeStampReader,
                        frm.WidthIndex, frm.HeightIndex, frm.FrameIndex,
                        frm.MinPixelValue, frm.MaxPixelValue, frm.BitPix, frm.NegPixCorrection);
                }
            }
            finally
            {
                if (fitsStream == null)
                {
                    bufferedFile.Dispose();
                }
            }

            return(fitsStream);
        }
        private BasicHDU ReadFIT(string fileName)
        {
            Fits f = new Fits(fileName);

            try
            {
                BasicHDU hdus = f.ReadHDU();
                if (hdus != null)
                {
                    hdus.Info();
                    // TEST: NUM SHARP TEST
                    ImageHDU imgHdu = (ImageHDU)f.GetHDU(0);
                    numSharp.InitImage(imgHdu, fileName);
                }
                return(hdus);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemple #4
0
        public static FitsCubeType GetFitsCubeType(string fileName)
        {
            Fits fitsFile = new Fits();

            using (BufferedFile bf = new BufferedFile(fileName, FileAccess.Read, FileShare.ReadWrite))
            {
                fitsFile.Read(bf);

                BasicHDU imageHDU = fitsFile.GetHDU(0);
                if (fitsFile.NumberOfHDUs == 1 && imageHDU.Axes.Length == 3)
                {
                    return(FitsCubeType.ThreeAxisCube);
                }

                if (fitsFile.NumberOfHDUs > 1 && imageHDU.Axes.Length == 2)
                {
                    return(FitsCubeType.MultipleHDUsCube);
                }
            }

            return(FitsCubeType.NotACube);
        }
Exemple #5
0
        public static FitsType GetFitsType(string fileName)
        {
            Fits fitsFile = new Fits();

            using (BufferedFile bf = new BufferedFile(fileName, FileAccess.Read, FileShare.ReadWrite))
            {
                fitsFile.Read(bf);

                BasicHDU imageHDU = fitsFile.GetHDU(0);
                if (fitsFile.NumberOfHDUs == 1 && imageHDU.Axes.Length == 3 && imageHDU.Axes[0] == 3)
                {
                    return(FitsType.RGBFrames);
                }

                if (fitsFile.NumberOfHDUs == 1 && imageHDU.Axes.Length == 2)
                {
                    return(FitsType.SingleFrame);
                }
            }

            return(FitsType.Invalid);
        }
Exemple #6
0
        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());
        }
Exemple #7
0
        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());
        }
Exemple #8
0
        //public static FITSData LoadFitsFile(string fileName, BasicHDU hdu, Array pixels, IFITSTimeStampReader timeStampReader, int negativePixelCorrection)
        //{

        //}

        public static FITSData LoadFitsFile(string fileName, IFITSTimeStampReader timeStampReader, int negativePixelCorrection)
        {
            var rv = new FITSData();

            Fits fitsFile = new Fits();

            using (BufferedFile bf = new BufferedFile(fileName, FileAccess.Read, FileShare.ReadWrite))
            {
                fitsFile.Read(bf);

                rv.HDU = fitsFile.GetHDU(0);

                rv.Width  = rv.HDU.Axes[1];
                rv.Height = rv.HDU.Axes[0];

                Array pixelsArray = (Array)rv.HDU.Data.DataArray;

                rv.PixelStats           = new PixelStats();
                rv.PixelStats.BZero     = GetBZero(rv.HDU);
                rv.PixelStats.RawBitPix = rv.HDU.BitPix;

                uint mask = (uint)(((uint)1 << rv.PixelStats.RawBitPix) - 1);
                rv.PixelStats.BitPix = rv.PixelStats.RawBitPix;
                if (rv.PixelStats.BitPix < 0)
                {
                    /* float and double are -32 and -64 respectively */
                    // Pretending float data is 16 bit. This is the maximum currently supported by Tangra
                    // Not using a mask here, and the pixel values will be converted further down
                    mask = 0;
                }

                rv.PixelsFlat = Load16BitImageData(pixelsArray, rv.Height, rv.Width, rv.PixelStats.BZero - negativePixelCorrection, mask,
                                                   out rv.PixelStats.MedianPixelValue, out rv.PixelStats.PixelType,
                                                   out rv.PixelStats.HasNegativePixels, out rv.PixelStats.MinPixelValue,
                                                   out rv.PixelStats.MaxPixelValue);


                rv.PixelStats.BitPix = GetBppForMaxPixelValue(rv.PixelStats.MaxPixelValue);


                if (rv.PixelStats.BitPix == 32)
                {
                    // Tangra only supports up to 16 bit so convert down to 16 bit
                    int shift = Math.Max(0, (int)Math.Ceiling(Math.Abs(rv.HDU.BitPix) / 16.0) - 1);

                    for (int y = 0; y < rv.Height; y++)
                    {
                        for (int x = 0; x < rv.Width; x++)
                        {
                            rv.PixelsFlat[x + y * rv.Width] = rv.PixelsFlat[x + y * rv.Width] >> shift;
                        }
                    }

                    rv.PixelStats.BitPix = 16;
                }
            }


            // Read Timestamp & Exposure
            if (timeStampReader != null)
            {
                bool isMidPoint;
                rv.Timestamp = timeStampReader.ParseExposure(fileName, rv.HDU.Header, out isMidPoint, out rv.Exposure);
            }


            // Read card from header
            rv.Cards = new Dictionary <string, string>();
            var cursor = rv.HDU.Header.GetCursor();

            while (cursor.MoveNext())
            {
                HeaderCard card = rv.HDU.Header.FindCard((string)cursor.Key);
                if (card != null && !string.IsNullOrWhiteSpace(card.Key) && card.Key != "END")
                {
                    if (rv.Cards.ContainsKey(card.Key))
                    {
                        rv.Cards[card.Key] += "\r\n" + card.Value;
                    }
                    else
                    {
                        rv.Cards.Add(card.Key, card.Value);
                    }
                }
            }

            return(rv);
        }
Exemple #9
0
        public void MakeDataFile()
        {
            string pathToCorr = "";

            if (GlobalVariables.LocalPlateSolve)
            {
                pathToCorr = @"c:\cygwin\home\astro\solve.corr";
            }
            else
            {
                pathToCorr = GlobalVariables.Path2 + "\\corr.fits";
            }

            // Fits f = new Fits(GlobalVariables.Path2 + "\\corr.fits"); // original
            Fits           f = new Fits(pathToCorr);
            BinaryTableHDU h = (BinaryTableHDU)f.GetHDU(1);

            //   Object[] row23 = h.GetRow(23);
            Object col_x   = h.GetColumn(0);
            Object col_y   = h.GetColumn(1);
            Object col_ra  = h.GetColumn(2);
            Object col_dec = h.GetColumn(3);

            float[] x   = new float[h.NRows];
            float[] y   = new float[h.NRows];
            float[] ra  = new float[h.NRows];
            float[] dec = new float[h.NRows];
            GlobalVariables.CorrFileLines = h.NRows;
            int i = 0;

            foreach (float item in (dynamic)col_x)
            {
                x[i] = (dynamic)(item);
                i++;
            }
            i = 0;
            foreach (float item2 in (dynamic)col_y)
            {
                y[i] = (dynamic)(item2);
                i++;
            }
            i = 0;
            foreach (float item in (dynamic)col_ra)
            {
                ra[i] = (dynamic)(item) / 15;
                i++;
            }
            i = 0;
            foreach (float item2 in (dynamic)col_dec)
            {
                dec[i] = (dynamic)(item2);
                i++;
            }

            using (StreamWriter sr = new StreamWriter(GlobalVariables.Path2 + "\\corr2text.txt"))
            {
                // sr.WriteLine("   x   " + "         y   ");
                for (i = 0; i < x.Length; i++)
                {
                    sr.WriteLine(x[i] + "    \t" + y[i] + "    \t" + ra[i] + "    \t" + dec[i]);
                }

                sr.Dispose();
            }
            Log("Corr2text file complete in " + GlobalVariables.Path2);
        }