Esempio n. 1
0
        public static TempSpec Sum(TempSpec spec1, TempSpec spec2, double rv1, double rv2)
        {
            if (spec1.Length != spec2.Length)
            {
                return(null);
            }

            TempSpec specSum = new TempSpec(spec1.Length);

            LinInterpolator li1     = new LinInterpolator(spec1.lambs, spec1.norm_intes);
            LinInterpolator li2     = new LinInterpolator(spec2.lambs, spec2.norm_intes);
            LinInterpolator liCont1 = new LinInterpolator(spec1.lambs, spec1.cont);
            LinInterpolator liCont2 = new LinInterpolator(spec2.lambs, spec2.cont);

            double lambda1 = spec1.Lambdas[0];
            double lambda2 = spec1.Lambdas[spec1.Length - 1];
            double dlambda = (lambda2 - lambda1) / spec1.Length;

            double c = 299792.458;
            double lambda, cont1, cont2;

            for (int i = 0; i < specSum.Length; i++)
            {
                lambda                = lambda1 + i * dlambda;
                specSum.lambs[i]      = lambda;
                cont1                 = liCont1.InterpUni(lambda - lambda * rv1 / c);
                cont2                 = liCont2.InterpUni(lambda - lambda * rv2 / c);
                specSum.cont[i]       = cont1 + cont2;
                specSum.norm_intes[i] = (li1.InterpUni(lambda - lambda * rv1 / c) * cont1 +
                                         li2.InterpUni(lambda - lambda * rv2 / c) * cont2) / specSum.cont[i];
            }

            return(specSum);
        }
Esempio n. 2
0
File: Form1.cs Progetto: arneb89/SN2
        private void DrawObsSpecGraph()
        {
            int n = lbOrders.SelectedIndex;

            if (n == -1)
            {
                return;
            }

            double lam_min = lambds[n].First();
            double lam_max = lambds[n].Last();

            if (lam_min > lam_max)
            {
                double buff;
                buff    = lam_max;
                lam_max = lam_min;
                lam_min = buff;
            }
            plot.Clear();
            if (tellur != null)
            {
                for (int i = 0; i < tellur.Size(); i++)
                {
                    if (tellur.GetLeftBound(i) > lam_min && tellur.GetRightBound(i) < lam_max)
                    {
                        VerticalLine vl1 = new VerticalLine(tellur.GetLeftBound(i));
                        VerticalLine vl2 = new VerticalLine(tellur.GetRightBound(i));
                        FilledRegion fr  = new FilledRegion(vl1, vl2);
                        fr.Brush = Brushes.LightBlue;
                        plot.Add(fr);
                    }
                }
            }
            for (int i = 0; i < cutMask.Size(); i++)
            {
                if (cutMask.GetLeftBound(i) > lam_min && cutMask.GetRightBound(i) < lam_max)
                {
                    VerticalLine vl1 = new VerticalLine(cutMask.GetLeftBound(i));
                    VerticalLine vl2 = new VerticalLine(cutMask.GetRightBound(i));
                    FilledRegion fr  = new FilledRegion(vl1, vl2);
                    fr.Brush = Brushes.Coral;
                    plot.Add(fr);
                }
            }

            LinePlot lp = new LinePlot();

            lp.AbscissaData = this.lambds[n];
            lp.OrdinateData = this.fluxes[n];


            plot.Add(lp);

            if (this.cont != null)
            {
                int nums = lambds[0].Length;

                LinePlot lp0 = new LinePlot();
                lp0.AbscissaData = this.lambds[n];
                lp0.OrdinateData = this.cont[n];
                lp0.Color        = Color.Green;
                plot.Add(lp0);

                if (this.tspec != null)
                {
                    li = new LinInterpolator(tspec.Lambdas, tspec.NormFluxes);

                    double[] ll = new double[nums];
                    double[] ff = new double[nums];
                    for (int i = 0; i < nums; i++)
                    {
                        ll[i] = lambds[n][i];
                        ff[i] = li.InterpUni(ll[i]) * cont[n][i];
                    }
                    LinePlot lp1 = new LinePlot();
                    lp1.AbscissaData = ll;
                    lp1.OrdinateData = ff;
                    lp1.Color        = Color.Red;
                    plot.Add(lp1);
                }
            }

            plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
            plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());
            plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(true));

            plot.XAxis1.Label = "Wavelength";
            plot.YAxis1.Label = "Flux";
            plot.Show();
            plot.Refresh();
        }
Esempio n. 3
0
File: Form1.cs Progetto: arneb89/SN2
        private void btnStart_Click(object sender, EventArgs e)
        {
            int oo, ox, grading_step;

            try
            {
                oo = int.Parse(txtOrderO.Text,
                               System.Globalization.CultureInfo.InvariantCulture);
                ox = int.Parse(txtOrderX.Text,
                               System.Globalization.CultureInfo.InvariantCulture);
                grading_step = int.Parse(txtGradingStep.Text,
                                         System.Globalization.CultureInfo.InvariantCulture);
            }
            catch
            {
                MessageBox.Show("Some error in input parameters...", "Error...");
                return;
            }

            if (this.lambds == null)
            {
                MessageBox.Show("Load observed spectra...", "Error...");
                return;
            }

            this.cont  = null;
            this.files = null;

            if (rbTemplate.Checked)
            {
                if (rbCompTwo.Checked)
                {
                    double rv1, rv2;
                    try
                    {
                        rv1 = double.Parse(txtRV1.Text.Replace(",", "."),
                                           System.Globalization.CultureInfo.InvariantCulture);
                        rv2 = double.Parse(txtRV2.Text.Replace(",", "."),
                                           System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch
                    {
                        MessageBox.Show("Some error in RV1 or RV2 fields...", "Error...");
                        return;
                    }
                    try
                    {
                        tspec1 = new TempSpec(txtTemplate1.Text);
                    }
                    catch
                    {
                        MessageBox.Show("Cannot load Template 1 file...", "Error...");
                        return;
                    }
                    try
                    {
                        tspec2 = new TempSpec(txtTemplate2.Text);
                    }
                    catch
                    {
                        MessageBox.Show("Cannot load Template 2 file...", "Error...");
                        return;
                    }
                    tspec = TempSpec.Sum(tspec1, tspec2, rv1, rv2);
                }
                else
                {
                    double rv;
                    try
                    {
                        rv = double.Parse(txtRV1.Text.Replace(",", "."),
                                          System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch
                    {
                        MessageBox.Show("Some error in RV1 field...", "Error...");
                        return;
                    }
                    try
                    {
                        tspec1 = new TempSpec(txtTemplate1.Text);
                    }
                    catch
                    {
                        MessageBox.Show("Cannot load Template 1 file...", "Error...");
                        return;
                    }
                    tspec = tspec1.RVShift(rv);
                }
            }

            tellur = new Mask(Application.StartupPath + "\\telluric.txt");

            double[][] mask = new double[tellur.Size() + cutMask.Size()][];
            for (int i = 0; i < mask.Length; i++)
            {
                mask[i] = new double[2];
            }
            for (int i = 0; i < tellur.Size(); i++)
            {
                mask[i][0] = tellur.GetLeftBound(i);
                mask[i][1] = tellur.GetRightBound(i);
            }
            for (int i = 0; i < cutMask.Size(); i++)
            {
                mask[i + tellur.Size()][0] = cutMask.GetLeftBound(i);
                mask[i + tellur.Size()][1] = cutMask.GetRightBound(i);
            }

            double[][]   lambds1, fluxes1;
            double[][][] outmas = null;
            if (grading_step > 1)
            {
                outmas = SpectraGrading(grading_step);
            }
            lambds1 = outmas[0];
            fluxes1 = outmas[1];

            Normator norm = null;

            if (rbTemplate.Checked)
            {
                norm = new Normator(lambds1, fluxes1, tspec.Lambdas, tspec.NormFluxes, mask);
            }
            else
            {
                norm = new Normator(lambds1, fluxes1, mask);
            }
            norm.Norm1(oo, ox, 10);

            this.cont = new double[this.lambds.Length][];
            for (int i = 0; i < this.cont.Length; i++)
            {
                this.cont[i] = new double[this.lambds[i].Length];
            }

            for (int i = 0; i < this.cont.Length; i++)
            {
                Array.Reverse(lambds1[i]);
                Array.Reverse(norm.GetContinum[i]);
                LinInterpolator li = new LinInterpolator(lambds1[i], norm.GetContinum[i]);

                for (int j = 0; j < this.cont[i].Length; j++)
                {
                    this.cont[i][j] = li.Interp(this.lambds[i][j]);
                }
            }
        }
Esempio n. 4
0
        public void Norm1(int oo, int ox, int iterMax)
        {
            ord_x = ox;
            ord_o = oo;

            n_orders = this.lambds.Length;
            n_pixels = this.lambds[0].Length;

            if (this.lambds_temp != null)
            {
                li = new LinInterpolator(lambds_temp, intes_temp);
            }

            double[] fluxes_norm = new double[n_orders * n_pixels];
            double[] sigmas      = new double[n_orders * n_pixels];

            double[][] xx = new double[n_orders * n_pixels][];
            for (int i = 0; i < xx.Length; i++)
            {
                xx[i] = new double[2];
            }

            max_flux = this.fluxes[0][0];
            for (int i = 0; i < n_orders; i++)
            {
                for (int j = 0; j < n_pixels; j++)
                {
                    if (max_flux < fluxes[i][j])
                    {
                        max_flux = fluxes[i][j];
                    }
                }
            }

            // Removing regions in observed spectra according the mask;
            int k = 0;

            for (int n = 0; n < n_orders; n++)
            {
                for (int i = 0; i < n_pixels; i++)
                {
                    bool notInMask = true;
                    for (int s = 0; s < mask.Length; s++)
                    {
                        if (lambds[n][i] >= mask[s][0] && lambds[n][i] <= mask[s][1])
                        {
                            notInMask = false;
                        }
                    }
                    if (notInMask)
                    {
                        fluxes_norm[k] = fluxes[n][i] / max_flux;
                        if (fluxes_norm[k] > 0)
                        {
                            sigmas[k] = Math.Sqrt(fluxes_norm[k]);
                        }
                        else
                        {
                            sigmas[k] = 1e20;
                        }
                        xx[k][0] = (double)n / n_orders;
                        xx[k][1] = (double)i / n_pixels;
                        k++;
                    }
                }
            }

            Array.Resize(ref xx, k);
            Array.Resize(ref fluxes_norm, k);

            // Fitting the model spectra to observed spectra;
            FitSVD.fit_finctions_md func = new FitSVD.fit_finctions_md(Pars);

            FitSVD fitter = new FitSVD(xx, fluxes_norm, sigmas, func, 1e-20);

            double stderror;
            int    rejected_poins_number = 0;

            for (int i = 0; i < 1; i++)
            {
                fitter.fit();
                coeff    = fitter.FittedCoeffs;
                stderror = 0;
                //for (int j = 0; j < points_number; j++)
                //{
                //    stderror += Math.Pow(point_lambda[j] - Surface(coeff, point_order[j], point_pixel[j], oo, ox), 2);
                //}
                //stderror = Math.Sqrt(stderror / points_number);
                //double diff;
                //int k = 0;
                //for (int j = 0; j < points_number; j++)
                //{
                //    diff = Math.Abs(point_lambda[j] - Surface(coeff, point_order[j], point_pixel[j], oo, ox));
                //    if (diff < cutLimit * stderror)
                //    {
                //        point_lambda[k] = point_lambda[j];
                //        point_order[k] = point_order[j];
                //        point_pixel[k] = point_pixel[j];
                //        k++;
                //    }
                //    else
                //    {
                //        rejected_poins_number++;
                //    }
                //}
                //points_number = k;
            }

            cont = new double[n_orders][];
            for (int i = 0; i < cont.Length; i++)
            {
                cont[i] = new double[n_pixels];
            }


            for (int i = 0; i < n_orders; i++)
            {
                for (int j = 0; j < n_pixels; j++)
                {
                    cont[i][j] = Surface(coeff, (double)i / n_orders,
                                         (double)j / n_pixels, oo, ox) * max_flux;
                }
            }
        }