示例#1
0
        static void Main(string[] args)
        {
            if (GlobalVariables.ParseCommands(args) == false)
            {
                return;
            }

            if (GlobalVariables.BuildStarDatabase == true)
            {
                StarCatalog.BuildPPMCatalog(GlobalVariables.PPMCatalogSource);
                StarCatalog.BuildVSXCatalog(GlobalVariables.VSXCatalogSource);
                return;
            }

            using (var db = new StarCatalogContext())
            {
                if (GlobalVariables.LoadFrames == true)
                {
                    FrameManager.GetFrameInfo(GlobalVariables.InputDirectory, db);
                    return;
                }

                if (GlobalVariables.LoadResults == true)
                {
                    string chartname =
                        new ChartManager().LoadChart(GlobalVariables.InputDirectory, db);

                    new SampleManager().LoadStarField(GlobalVariables.InputDirectory, chartname, db);

                    return;
                }

                var field = StarField.LoadStarField(GlobalVariables.Chart, db);

                // Apply quality filter.
                field.Stars = field.Stars.OrderByDescending(x => x.AvgInstrumentalMag).Where(x => x.MinSNR > GlobalVariables.MinSNR && x.Flags < 16).ToList();

                field.DoPhotometricReduction();

                // Output results
                StringBuilder candidates = new StringBuilder();
                candidates.AppendLine(String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", "Star", "RMag", "C-VMag", "C-VMag-E", "VMag", "C-ColorIndex", "ColorIndex", "Label", "Known Variable", "Uncertainty", "Score", "Flags", "URL"));

                StringBuilder timeseries = new StringBuilder();
                timeseries.AppendLine(String.Format("{0},{1},{2},{3},{4},{5}", "Score", "Star", "ObsDate", "VMag", "TFAMag", "Uncertainty"));

                StringBuilder aavsolog = new StringBuilder();

                aavsolog.AppendLine("#TYPE=EXTENDED");
                aavsolog.AppendLine("#OBSCODE=" + GlobalVariables.ObsCode);
                aavsolog.AppendLine("#SOFTWARE=VSA V" + Assembly.GetEntryAssembly().GetName().Version);
                aavsolog.AppendLine("#DELIM=,");
                aavsolog.AppendLine("#DATE=JD");
                aavsolog.AppendLine("#OBSTYPE=DSLR");
                aavsolog.AppendLine("#NAME,DATE,MAG,MERR,FILT,TRANS,MTYPE,CNAME,CMAG,KNAME,KMAG,AMASS,GROUP,CHART,NOTES");

                Console.WriteLine("Outputing results.");
                foreach (Star star in field.Stars)
                {
                    D_VSXEntry entry = StarCatalog.GetVSXEntry((double)star.RA, (double)star.DEC);

                    string variabletype = "";
                    if (entry != null && entry.VFlag < 2)
                    {
                        Console.WriteLine("Found variable star: " + entry.Name);
                        variabletype = "http://www.aavso.org/vsx/index.php?view=detail.top&oid=" + entry.OID.Trim();

                        string ensemble = "Custom ensemble of " + (star.Comparables.Count() - 1).ToString() + " due to low chart count.";
                        if (field.ChartComparables.Count > 1)
                        {
                            ensemble = field.ChartComparables[1].Label;
                            for (int f = 2; f < field.ChartComparables.Count; f++)
                            {
                                ensemble += "/" + field.ChartComparables[f].Label;
                            }
                        }

                        // Query to get auid.
                        string auid = StarCatalog.GetAUID(entry);

                        if (auid != null)
                        {
                            aavsolog.AppendLine(String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14}",
                                                              auid,
                                                              Math.Round(field.Frames[0].Time, 4),
                                                              Math.Round(star.VMag, 3).ToString().PadLeft(7, ' '),
                                                              Math.Round(star.e_VMag, 3),
                                                              "V",
                                                              "YES",
                                                              "STD",
                                                              "ENSEMBLE",
                                                              "na",
                                                              field.ChartComparables[0].Label.Replace("[CHECK]", "").Trim(),
                                                              Math.Round(field.ChartComparables[0].VMag, 3).ToString().PadLeft(7, ' '),
                                                              "na",
                                                              "na",
                                                              field.Frames[0].Chart,
                                                              String.Format("MAG=avg {0} img|MERR=StdDe|ENSEMBLE={1}", star.Samples.Count(), ensemble)));
                        }
                    }

                    string label = "";
                    if (field.ChartComparables.Contains(star))
                    {
                        label = star.Label;
                    }

                    candidates.Append(String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}",
                                                    star.Name,
                                                    star.CatalogEntry.Rmag,
                                                    star.VMag,
                                                    star.e_VMag,
                                                    star.CatalogEntry.Vmag,
                                                    star.CalculatedColorIndex,
                                                    star.ColorIndex,
                                                    label,
                                                    variabletype,
                                                    star.EnsembleError,
                                                    star.Score,
                                                    Convert.ToString(star.Flags, 2).PadLeft(5, '0')));

                    // Output the calculated differential magnitude with the comparable star.
                    int i = 0;
                    foreach (SampleData sample in star.Samples)
                    {
                        timeseries.AppendLine(String.Format("{0},{1},{2},{3},{4},{5}", star.Score, star.Name, field.Frames[i].Time, sample.ApparentVMag - star.VMag, sample.TFAVMag - star.VMag, sample.Uncertainty));
                        i++;
                    }
                    candidates.Append(String.Format(",http://simbad.u-strasbg.fr/simbad/sim-coo?Coord={0}%20{1}%20{2}%20{3}%20{4}%20{5}&Radius=2&Radius.unit=arcsec&",
                                                    star.Name.Substring(0, 2), star.Name.Substring(2, 2), star.Name.Substring(4, 4), star.Name.Substring(8, 3), star.Name.Substring(11, 2), star.Name.Substring(13, 2)));
                    candidates.AppendLine();
                }
                File.WriteAllText(GlobalVariables.GetOutput(".\\AAVSO\\" + field.Frames[0].Chart + ".txt"), aavsolog.ToString());
                File.WriteAllText(GlobalVariables.GetOutput(".\\candidates_out.csv"), candidates.ToString());
                File.WriteAllText(GlobalVariables.GetOutput(".\\timeseries_out.csv"), timeseries.ToString());
            }
        }
        /// <summary>
        /// Load star-field specified in file
        /// </summary>
        /// <param name="filename">CSV file for star-field to load</param>
        /// <returns></returns>
        public void LoadStarField(string directory, string chartname, StarCatalogContext db)
        {
            // Load and parse csv.
            TextReader reader = File.OpenText(directory + "\\series_g.csv");
            var        csv    = new CsvReader(reader);

            csv.Configuration.Delimiter = ";";
            csv.ReadHeader();

            // Load headers
            int fluxsample = 6;

            while (fluxsample < csv.FieldHeaders.Count())
            {
                string filename = csv.FieldHeaders.GetValue(fluxsample++).ToString();
                var    match    = db.Frames.Where(x => filename.Contains(x.File)).First();
                match.Chart = chartname;
                frames.Add(match);
            }
            db.SaveChanges();

            csv.Read();
            csv.Read();
            csv.Read();

            int aperture = GetAperture(csv.GetField(6));

            csv.Read();

            // Pre-load all existing sample data for frames.
            List <F_Sample> existingsamples = new List <F_Sample>();

            foreach (D_Frame d_frame in frames)
            {
                existingsamples.AddRange(db.Samples.Where(x => x.ID_Frame == d_frame.ID).ToList());
            }

            int starcount = 0;

            Console.WriteLine();
            do
            {
                // Create star if it doesn't exist.
                string name = csv.GetField(0);

                D_Star star = db.Stars.Where(x => x.Nameplate == name).FirstOrDefault();
                if (star == null)
                {
                    star = new D_Star()
                    {
                        Nameplate = name,
                        J2000_RA  = csv.GetField <double>(1),
                        J2000_DEC = csv.GetField <double>(2)
                    };
                    D_VSXEntry entry = StarCatalog.GetVSXEntry(star.J2000_RA, star.J2000_DEC);
                    if (entry != null)
                    {
                        star.OID = Convert.ToInt32(entry.OID);
                    }
                    else
                    {
                        star.OID = -1;
                    }

                    db.Stars.Add(star);
                }
                stars.Add(star);

                /////
                // Load flux samples.
                string parsedflux;
                fluxsample = 6;

                int frame = 0;
                int flag  = csv.GetField <int>(3);
                while (csv.TryGetField(fluxsample++, out parsedflux))
                {
                    if (existingsamples.Where(x => x.ID_Frame == frames[frame].ID && x.ID_Star == star.ID).Count() == 0)
                    {
                        double flux = 0.0;
                        double.TryParse(parsedflux, out flux);

                        F_Sample sample = new F_Sample()
                        {
                            Star     = star,
                            ID_Frame = frames[frame].ID,
                            Aperture = aperture,
                            FluxV    = flux
                        };

                        // Save flux measurement as instrumental mag.
                        samples.Add(sample);

                        frame++;
                    }
                }
                Console.Write("\rStars loaded: " + ++starcount);
            }while (csv.Read());

            // Save stars
            db.SaveChanges();

            // Supplement and persist sample data.
            if (samples.Count > 0)
            {
                foreach (F_Sample sample in samples)
                {
                    // Set star ID.
                    sample.ID_Star = sample.Star.ID;
                    db.Samples.Add(sample);
                }

                LoadInstrumentalColorIndex(directory);
                LoadSNR(directory);
                db.SaveChanges();
            }

            csv.Dispose();
        }