Ejemplo n.º 1
0
        /// <summary>
        /// Parses a Source Extractor catalog file.
        /// </summary>
        /// <returns>The detections in the catalog.</returns>
        /// <param name="Lines">Catalog file lines.</param>
        /// <param name="AssociatedImage">Image to which the catalog is associated to.</param>
        public static List <ImageDetection> ParseSEFile(IEnumerable <string> Lines, FitsImage AssociatedImage)
        {
            List <string>            ColList = new List <string>();
            Dictionary <string, int> Columns = new Dictionary <string, int>();
            List <ObsEntry>          Entries = new List <ObsEntry>();

            foreach (string Line in Lines)
            {
                if (Line[0] == '#')
                {
                    string[] Hdata = Line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                    if (Hdata[0] != "#")
                    {
                        throw new FormatException("Cannot understand SE file");
                    }
                    int    CNum  = int.Parse(Hdata[1]) - 1;
                    string CName = Hdata[2];
                    ColList.Add(CName);
                    Columns.Add(CName, CNum);
                }
                else
                {
                    string[] Ldata = Line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                    ObsEntry Entry = new ObsEntry();
                    Entry.Flux        = Parse(Columns, Ldata, "FLUX_AUTO");
                    Entry.Mag         = Parse(Columns, Ldata, "MAG_AUTO");
                    Entry.X           = Parse(Columns, Ldata, "X_IMAGE").Value;
                    Entry.Y           = Parse(Columns, Ldata, "Y_IMAGE").Value;
                    Entry.RA          = Parse(Columns, Ldata, "ALPHA_J2000").Value;
                    Entry.Dec         = Parse(Columns, Ldata, "DELTA_J2000").Value;
                    Entry.FWHM        = Parse(Columns, Ldata, "FWHM_IMAGE");
                    Entry.Ellipticity = Parse(Columns, Ldata, "ELLIPTICITY");
                    Entry.A           = Parse(Columns, Ldata, "A_IMAGE");
                    Entry.B           = Parse(Columns, Ldata, "B_IMAGE");
                    Entry.Flags       = (int)Parse(Columns, Ldata, "FLAGS");

                    if (!Entry.Flags.HasValue || Entry.Flags.Value < 4)
                    {
                        Entries.Add(Entry);
                    }
                }
            }
            List <ImageDetection> Detections = Entries.Select((x) => Transform(x, AssociatedImage)).ToList();

            return(Detections);
        }
Ejemplo n.º 2
0
        // Avert thy gaze
        private void NewMapBT_Click(object sender, EventArgs e)
        {
            if ((maps_.Count == 0) || string.IsNullOrEmpty(working_dir_))
            {
                ErrMsg("No data loaded!");
                return;
            }

            using (var dialog = new NewMapDialog(map_names_.Length - 1))
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    var madjson  = new MadJson();
                    var fmfjson  = new FmfJson();
                    var obsjson  = new ObsJson();
                    var id       = dialog.MapID;
                    var name     = dialog.MapName;
                    var w        = dialog.MapW;
                    var h        = dialog.MapH;
                    var layer_sz = w * h * 2;

                    var idstr = id.ToString("D3");
                    var dir   = is_ynk_ ? @"\gn_dat5.arc\map\data\" : @"\gn_dat3.arc\map\data\";
                    dir = working_dir_ + dir + idstr + @"\";

                    madjson.id       = id;
                    madjson.filepath = dir + idstr + ".json";
                    var madpath = dir + idstr + ".mad";
                    fmfjson.id       = id;
                    fmfjson.filepath = dir + idstr + "_fmf.json";
                    var fmfpath = dir + idstr + ".fmf";
                    obsjson.id       = id;
                    obsjson.filepath = dir + idstr + "_obs.json";
                    var obspath = dir + idstr + ".obs";

                    if (File.Exists(madpath))
                    {
                        ErrMsg("File already exists: " + madpath);
                        return;
                    }
                    if (File.Exists(fmfpath))
                    {
                        ErrMsg("File already exists: " + fmfpath);
                        return;
                    }
                    if (File.Exists(obspath))
                    {
                        ErrMsg("File already exists: " + obspath);
                        return;
                    }
                    if (!Directory.Exists(dir))
                    {
                        try
                        {
                            Directory.CreateDirectory(dir);
                        }
                        catch
                        {
                            ErrMsg("Failed to create directory: " + dir);
                            return;
                        }
                    }

                    // FMF
                    fmfjson.width          = (uint)w;
                    fmfjson.height         = (uint)h;
                    fmfjson.num_layers     = 13;
                    fmfjson.payload_length = (uint)layer_sz * 13;
                    fmfjson.unknown_1      = 0x20;
                    fmfjson.unknown_2      = 0x20;
                    fmfjson.unknown_3      = 0x10;

                    var fmf_sz  = fmfjson.payload_length + 20;
                    var fmfdata = new byte[fmf_sz];
                    Array.Copy(new byte[] { (byte)'F', (byte)'M', (byte)'F', (byte)'_' }, fmfdata, 4);
                    Array.Copy(BitConverter.GetBytes(fmfjson.payload_length), 0, fmfdata, 4, 4);
                    Array.Copy(BitConverter.GetBytes(fmfjson.width), 0, fmfdata, 8, 4);
                    Array.Copy(BitConverter.GetBytes(fmfjson.height), 0, fmfdata, 0x0C, 4);
                    fmfdata[0x10] = 0x20;
                    fmfdata[0x11] = 0x20;
                    fmfdata[0x12] = 13;
                    fmfdata[0x13] = 0x10;
                    for (var i = 0; i < 13; ++i)
                    {
                        fmfjson.layers[i] = Convert.ToBase64String(fmfdata, 20, layer_sz);
                    }
                    File.WriteAllBytes(fmfpath, fmfdata);
                    SaveFMF(fmfjson);

                    // OBS
                    obsjson.entries = new ObsEntry[1024];
                    for (var i = 0; i < 1024; ++i)
                    {
                        var entry = new ObsEntry();
                        entry.index          = (uint)i;
                        entry.event_index    = (uint)i;
                        entry.movement_delay = 255;
                        entry.flags[0]       = 1;
                        entry.flags[1]       = 1;
                        obsjson.entries[i]   = entry;
                    }
                    File.WriteAllBytes(obspath, new byte[1024 * 20]);
                    SaveOBS(obsjson);

                    // MAD
                    madjson.location_name = name;
                    for (var i = 0; i < 10; ++i)
                    {
                        madjson.normal_encounters[i] = new MadEncounter();
                        if (i < 5)
                        {
                            madjson.special_encounters[i] = new MadEncounter();
                        }
                    }
                    File.WriteAllBytes(madpath, new byte[0x8B]);
                    SaveMAD(madjson);

                    if (id < map_names_.Length)
                    {
                        map_names_[id] = name;
                    }
                    maps_.Add(madjson);
                    fmfs_.Add(fmfjson);
                    obss_.Add(obsjson);

                    MapListBox.Items.Add(name);
                    MapDesignCB.Items.Add(name);
                    EventMapCB.Items.Add(name);

                    for (var i = 0; i < 128; ++i)
                    {
                        event_flags_[(id * 128) + i] = 0xFF;
                    }

                    SelectMap(maps_.Count - 1);
                }
            }
        }
Ejemplo n.º 3
0
        static ImageDetection Transform(ObsEntry Entry, FitsImage AssociatedImage)
        {
            EquatorialPoint eqp = new EquatorialPoint()
            {
                RA = Entry.RA / 180 * Math.PI, Dec = Entry.Dec / 180 * Math.PI
            };
            PixelPoint pp = new PixelPoint()
            {
                X = Entry.X, Y = Entry.Y
            };
            Position       p   = new Position(eqp, pp);
            ImageDetection det = new ImageDetection(p, AssociatedImage.GetProperty <ObservationTime>(), AssociatedImage);

            bool       Ellipse = false;
            ObjectSize sz      = new ObjectSize();

            if (Entry.A.HasValue && Entry.B.HasValue)
            {
                sz.PixelEllipse = new SourceEllipse()
                {
                    SemiaxisMajor = Entry.A.Value, SemiaxisMinor = Entry.B.Value
                };
                Ellipse = true;
            }
            else if (Entry.FWHM.HasValue && Entry.Ellipticity.HasValue)
            {
                sz.PixelEllipse = new SourceEllipse()
                {
                    SemiaxisMajor = Entry.FWHM.Value / Math.Sqrt(1 - Entry.Ellipticity.Value),
                    SemiaxisMinor = Entry.FWHM.Value * Math.Sqrt(1 - Entry.Ellipticity.Value),
                };
                Ellipse = true;
            }
            if (Ellipse)
            {
                if (Entry.EllipseTheta.HasValue)
                {
                    sz.PixelEllipse.SemiaxisMajorAngle = Entry.EllipseTheta.Value / 180 * Math.PI;
                }
                det.AppendProperty(sz);
            }

            PairingProperties pprop = new PairingProperties()
            {
                IsDotDetection = Ellipse && (sz.PixelEllipse.SemiaxisMajor < 2 * sz.PixelEllipse.SemiaxisMinor),
                IsPaired       = false,
                PearsonR       = 0,
                StarPolluted   = false,
                Algorithm      = DetectionAlgorithm.SourceExtractor
            };

            det.AppendProperty(pprop);
            if (Entry.Flux.HasValue)
            {
                ObjectPhotometry oph = new ObjectPhotometry()
                {
                    Flux = Entry.Flux.Value, Magnitude = Entry.Mag.Value
                };
                det.AppendProperty(oph);
            }

            return(det);
        }