Пример #1
0
        internal AbsFluxSpectra(AbsFluxInputFile inputFile)
        {
            InputFile  = inputFile;
            IsComplete = false;

            if (!string.IsNullOrEmpty(inputFile.Target))
            {
                // First try to read the target from the export
                m_DisplayName       = inputFile.Target;
                m_ExtractedStarName = null;
            }
            else
            {
                // Then try to parse the star from the file
                Match match = STAR_DESIGNATION_REGEX.Match(inputFile.FileName);
                if (match.Success && !string.IsNullOrEmpty(match.Value))
                {
                    m_ExtractedStarName = match.Value.Replace('_', ' ').Replace("  ", " ").Trim();
                    m_DisplayName       = m_ExtractedStarName;
                }
            }

            HasObjectCoordinates = !float.IsNaN(inputFile.Latitude) && !float.IsNaN(inputFile.Longitude) &&
                                   !float.IsNaN(inputFile.RAHours) && !float.IsNaN(inputFile.DEDeg);

            HasObservationTime = inputFile.Epoch != DateTime.MinValue;

            if (HasObservationTime && HasObjectCoordinates)
            {
                // If we have the center of the field saved in the export then identify the standard star by the position
                List <CalSpecStar> standardsInOneDegreeRadius = CalSpecDatabase.Instance.Stars.Where(x => AngleUtility.Elongation(x.RA_J2000_Hours * 15, x.DE_J2000_Deg, InputFile.RAHours * 15, InputFile.DEDeg) < 1).ToList();
                if (standardsInOneDegreeRadius.Count == 1)
                {
                    m_CalSpecStar = standardsInOneDegreeRadius[0];
                    m_DisplayName = m_CalSpecStar.AbsFluxStarId;

                    IsComplete  = true;
                    PlotSpectra = true;
                }
            }

            if (inputFile.Epoch > DateTime.MinValue)
            {
                m_DisplayName += string.Format(" ({0} UT)", inputFile.Epoch.ToString("HH:mm"));
            }

            if (string.IsNullOrEmpty(m_DisplayName))
            {
                m_DisplayName = Path.GetFileNameWithoutExtension(inputFile.FileName);
            }


            DataFromWavelength = (int)Math.Ceiling(inputFile.Wavelengths[0]);
            DataFromWavelength = (int)Math.Floor(inputFile.Wavelengths[inputFile.Wavelengths.Count - 1]);
        }
Пример #2
0
        internal CalSpecDatabase(BinaryReader reader)
        {
            int count = reader.ReadInt32();
            Stars.Clear();

            for (int i = 0; i < count; i++)
            {
                var star = new CalSpecStar(reader);
                Stars.Add(star);
            }
        }
Пример #3
0
        internal CalSpecDatabase(BinaryReader reader)
        {
            int count = reader.ReadInt32();

            Stars.Clear();

            for (int i = 0; i < count; i++)
            {
                var star = new CalSpecStar(reader);
                Stars.Add(star);
            }
        }
Пример #4
0
        internal AbsFluxSpectra(AbsFluxInputFile inputFile)
        {
            InputFile = inputFile;
            IsComplete = false;

            if (!string.IsNullOrEmpty(inputFile.Target))
            {
                // First try to read the target from the export
                m_DisplayName = inputFile.Target;
                m_ExtractedStarName = null;
            }
            else
            {
                // Then try to parse the star from the file
                Match match = STAR_DESIGNATION_REGEX.Match(inputFile.FileName);
                if (match.Success && !string.IsNullOrEmpty(match.Value))
                {
                    m_ExtractedStarName = match.Value.Replace('_', ' ').Replace("  ", " ").Trim();
                    m_DisplayName = m_ExtractedStarName;
                }
            }

            HasObjectCoordinates = !float.IsNaN(inputFile.Latitude) && !float.IsNaN(inputFile.Longitude) &&
                                   !float.IsNaN(inputFile.RAHours) && !float.IsNaN(inputFile.DEDeg);

            HasObservationTime = inputFile.Epoch != DateTime.MinValue;

            if (HasObservationTime && HasObjectCoordinates)
            {
                // If we have the center of the field saved in the export then identify the standard star by the position
                List<CalSpecStar> standardsInOneDegreeRadius = CalSpecDatabase.Instance.Stars.Where(x => AngleUtility.Elongation(x.RA_J2000_Hours * 15, x.DE_J2000_Deg, InputFile.RAHours * 15, InputFile.DEDeg) < 1).ToList();
                if (standardsInOneDegreeRadius.Count == 1)
                {
                    m_CalSpecStar = standardsInOneDegreeRadius[0];
                    m_DisplayName = m_CalSpecStar.AbsFluxStarId;

                    IsComplete = true;
                    PlotSpectra = true;
                }
            }

            if (inputFile.Epoch > DateTime.MinValue)
                m_DisplayName += string.Format(" ({0} UT)", inputFile.Epoch.ToString("HH:mm"));

            if (string.IsNullOrEmpty(m_DisplayName))
                m_DisplayName = Path.GetFileNameWithoutExtension(inputFile.FileName);

            DataFromWavelength = (int)Math.Ceiling(inputFile.Wavelengths[0]);
            DataFromWavelength = (int)Math.Floor(inputFile.Wavelengths[inputFile.Wavelengths.Count - 1]);
        }
Пример #5
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());
        }
Пример #6
0
 private double GetReferenceMagnitude(FilterResponse filterResponse, CalSpecStar star)
 {
     switch (filterResponse.Band)
     {
         case PhotometricBand.B:
             return star.MagB;
         case PhotometricBand.V:
             return star.MagV;
         case PhotometricBand.R:
             return star.MagR;
         case PhotometricBand.Sloan_g:
             return star.Mag_g;
         case PhotometricBand.Sloan_r:
             return star.Mag_r;
         case PhotometricBand.Sloan_i:
             return star.Mag_i;
         default:
             return star.MagV;
     }
 }
Пример #7
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());
        }