private void ExecuteWithRealData()
        {
            ILightCurveDataProvider dataProvider = m_TangraHost.GetLightCurveDataProvider();

            if (dataProvider != null)
            {
                if (dataProvider.NumberOfMeasuredComparisonObjects == 0)
                {
                    ShowErrorMessage("At least one comparison object is required to determine the target eclipsing binary minimum.");
                    return;
                }

                ISingleMeasurement[] measurements = dataProvider.GetTargetMeasurements();

                bool hasReliableTimeBase = dataProvider.HasReliableTimeBase;

                double[] frameIds = measurements.Select(x => (double)x.CurrFrameNo).ToArray();
                double[] dataWithBg;
                double[] dataBg;

                var frm = new frmConfigureRun();
                frm.DataProvider = dataProvider;
                frm.NumStars = dataProvider.NumberOfMeasuredComparisonObjects + 1;
                frm.FromFrameNo = dataProvider.MinFrameNumber;
                frm.ToFrameNo = dataProvider.MaxFrameNumber;
                frm.TargetData = dataProvider.GetTargetMeasurements();
                if (frm.ShowDialog(m_TangraHost.ParentWindow) == DialogResult.Cancel)
                    return;

                bool useCurveFitting = frm.RunCurveFitting;
                bool useFineGrainedBins = frm.UseFineGrainedBins;

                if (frm.VariableStarIndex == 0)
                {
                    dataWithBg = measurements.Select(x => x.IsSuccessful ? (double)(x.Measurement + x.Background) : double.NaN).ToArray();
                    dataBg = measurements.Select(x => x.IsSuccessful ? (double)x.Background : double.NaN).ToArray();
                }
                else
                {
                    ISingleMeasurement[] altMeasurements = dataProvider.GetComparisonObjectMeasurements(frm.VariableStarIndex - 1);

                    dataWithBg = altMeasurements.Select(x => x.IsSuccessful ? (double)(x.Measurement + x.Background) : double.NaN).ToArray();
                    dataBg = altMeasurements.Select(x => x.IsSuccessful ? (double)x.Background : double.NaN).ToArray();
                }

                List<DateTime> timestamps = measurements.Select(x => x.Timestamp).ToList();

                hasReliableTimeBase = hasReliableTimeBase &&
                    timestamps[0].Date != DateTime.MinValue &&
                    timestamps[measurements.Length - 1].Date != DateTime.MinValue &&
                    timestamps[0].Date.Ticks < timestamps[measurements.Length - 1].Ticks;

                int numAvailableTimestamps = timestamps.Count(x => x != DateTime.MinValue && x != DateTime.MaxValue);
                if (numAvailableTimestamps < 2)
                {
                    MessageBox.Show(
                        m_TangraHost.ParentWindow,
                        "This light curve does not seem to have timestamps saved and cannot be processed.",
                        "Eclipsing Binaries Addin for Tangra",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return;
                }

                if (!hasReliableTimeBase)
                {
                    if (MessageBox.Show(
                        m_TangraHost.ParentWindow,
                        "This light curve may not have a reliable time base. Do you want to continue?",
                        "Eclipsing Binaries Addin for Tangra",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning) == DialogResult.No)
                    {
                        return;
                    }
                }

                // Fill in missing timestamps when times are entered manually
                if (!dataProvider.HasEmbeddedTimeStamps)
                {
                    int idx = 0;
                    for (; idx < timestamps.Count / 2 && timestamps[idx] == DateTime.MinValue; idx++);
                    int firstTimeStampIndex = idx;
                    DateTime firstTimeStamp = timestamps[idx];

                    idx = timestamps.Count - 1;
                    for (; idx > timestamps.Count / 2 && timestamps[idx] == DateTime.MinValue; idx--) ;
                    int lastTimeStampIndex = idx;
                    DateTime lastTimeStamp = timestamps[idx];

                    long ticksPerFrame = (lastTimeStamp.Ticks - firstTimeStamp.Ticks) / (lastTimeStampIndex - firstTimeStampIndex);
                    for (int i = 0; i < timestamps.Count; i++)
                        timestamps[i] = firstTimeStamp.AddTicks(ticksPerFrame * (i - firstTimeStampIndex));
                }

                double[] secondsFromUTMidnight = new double[timestamps.Count];
                long startFrameStartDayTicks = timestamps[0].Date.Ticks;

                for (int i = 0; i < timestamps.Count; i++)
                {
                    if (timestamps[i] != DateTime.MinValue)
                        secondsFromUTMidnight[i] = Math.Truncate(new TimeSpan(timestamps[i].Ticks - startFrameStartDayTicks).TotalSeconds * 10000) / 10000.0;
                    else
                        secondsFromUTMidnight[i] = 0;
                }

                // Now go and get the comparison star data
                if (dataProvider.NumberOfMeasuredComparisonObjects > 0)
                {
                    ISingleMeasurement[] compMeasurements = frm.ComparisonStarIndex == 0 ? dataProvider.GetTargetMeasurements() : dataProvider.GetComparisonObjectMeasurements(frm.ComparisonStarIndex - 1);

                    if (compMeasurements != null)
                    {
                        double[] compDataWithBg;
                        double[] compBg;

                        if (frm.UseNormalisation)
                        {
                            compDataWithBg = compMeasurements.Select(x => x.IsSuccessful ? (double)(x.Measurement + x.Background) : double.NaN).ToArray();
                            compBg = compMeasurements.Select(x => x.IsSuccessful ? (double)x.Background : double.NaN).ToArray();
                        }
                        else
                        {
                            double compDataWithBgAverage = compMeasurements.Average(x => x.IsSuccessful ? x.Measurement + x.Background : double.NaN);
                            double compBgAverage = compMeasurements.Average(x => x.IsSuccessful ? x.Background : double.NaN);
                            compDataWithBg = compMeasurements.Select(x => compDataWithBgAverage).ToArray();
                            compBg = compMeasurements.Select(x => compBgAverage).ToArray();
                        }

                        double jdAtUtcMidnight = DateUtcToJulian(new DateTime(startFrameStartDayTicks));

                        KweeVanWoerdenResult result;
                        PolynomialFitResult polyResult;

                        var times = new List<double>();
                        var dataWithBgLst = new List<double>();
                        var dataBgLst = new List<double>();
                        var compDataWithBgLst = new List<double>();
                        var compBgLst = new List<double>();

                        if (frm.IncludeDataFrom > 0 || frm.IncludeDataTo < dataWithBg.Length - 1)
                        {
                            double[] timesAlt = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            double[] dataWithBgAlt = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            double[] dataBgAlt = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            double[] compDataWithBgAlt = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            double[] compBgAlt = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            for (int i = frm.IncludeDataFrom; i < frm.IncludeDataTo; i++)
                            {
                                timesAlt[i - frm.IncludeDataFrom] = secondsFromUTMidnight[i];
                                dataWithBgAlt[i - frm.IncludeDataFrom] = dataWithBg[i];
                                dataBgAlt[i - frm.IncludeDataFrom] = dataBg[i];
                                compDataWithBgAlt[i - frm.IncludeDataFrom] = compDataWithBg[i];
                                compBgAlt[i - frm.IncludeDataFrom] = compBg[i];
                            }

                            times.AddRange(timesAlt);
                            dataWithBgLst.AddRange(dataWithBgAlt);
                            dataBgLst.AddRange(dataBgAlt);
                            compDataWithBgLst.AddRange(compDataWithBgAlt);
                            compBgLst.AddRange(compBgAlt);

                            RemoveInvalidMeasurements(times, dataWithBgLst, dataBgLst, compDataWithBgLst, compBgLst);
                        }
                        else
                        {
                            times.AddRange(secondsFromUTMidnight);
                            dataWithBgLst.AddRange(dataWithBg);
                            dataBgLst.AddRange(dataBg);
                            compDataWithBgLst.AddRange(compDataWithBg);
                            compBgLst.AddRange(compBg);

                            RemoveInvalidMeasurements(times, dataWithBgLst, dataBgLst, compDataWithBgLst, compBgLst);
                        }

                        result = Kwee_van_Woerden(times.Count, jdAtUtcMidnight, times.ToArray(), dataWithBgLst.ToArray(), dataBgLst.ToArray(), compDataWithBgLst.ToArray(), compBgLst.ToArray(), useFineGrainedBins);
                        polyResult = useCurveFitting
                            ? PolynomialFit(times.Count, jdAtUtcMidnight, times.ToArray(), dataWithBgLst.ToArray(), dataBgLst.ToArray(), compDataWithBgLst.ToArray(), compBgLst.ToArray(), (int)result.Start_Light_Curve_Obs_Index, (int)result.Stop_Light_Curve_Obs_Index, result.Time_Of_Minimum)
                            : null;

                        PresentResults(frm.VariableStarIndex, result, polyResult);
                    }
                }
            }
        }
Exemple #2
0
        private void ExecuteWithRealData()
        {
            ILightCurveDataProvider dataProvider = m_TangraHost.GetLightCurveDataProvider();

            if (dataProvider != null)
            {
                if (dataProvider.NumberOfMeasuredComparisonObjects == 0)
                {
                    ShowErrorMessage("At least one comparison object is required to determine the target eclipsing binary minimum.");
                    return;
                }

                ISingleMeasurement[] measurements = dataProvider.GetTargetMeasurements();

                bool hasReliableTimeBase = dataProvider.HasReliableTimeBase;

                double[] frameIds = measurements.Select(x => (double)x.CurrFrameNo).ToArray();
                double[] dataWithBg;
                double[] dataBg;

                var frm = new frmConfigureRun();
                frm.DataProvider = dataProvider;
                frm.NumStars     = dataProvider.NumberOfMeasuredComparisonObjects + 1;
                frm.FromFrameNo  = dataProvider.MinFrameNumber;
                frm.ToFrameNo    = dataProvider.MaxFrameNumber;
                frm.TargetData   = dataProvider.GetTargetMeasurements();
                if (frm.ShowDialog(m_TangraHost.ParentWindow) == DialogResult.Cancel)
                {
                    return;
                }

                bool useCurveFitting    = frm.RunCurveFitting;
                bool useFineGrainedBins = frm.UseFineGrainedBins;

                if (frm.VariableStarIndex == 0)
                {
                    dataWithBg = measurements.Select(x => x.IsSuccessful ? (double)(x.Measurement + x.Background) : double.NaN).ToArray();
                    dataBg     = measurements.Select(x => x.IsSuccessful ? (double)x.Background : double.NaN).ToArray();
                }
                else
                {
                    ISingleMeasurement[] altMeasurements = dataProvider.GetComparisonObjectMeasurements(frm.VariableStarIndex - 1);

                    dataWithBg = altMeasurements.Select(x => x.IsSuccessful ? (double)(x.Measurement + x.Background) : double.NaN).ToArray();
                    dataBg     = altMeasurements.Select(x => x.IsSuccessful ? (double)x.Background : double.NaN).ToArray();
                }

                List <DateTime> timestamps = measurements.Select(x => x.Timestamp).ToList();

                hasReliableTimeBase = hasReliableTimeBase &&
                                      timestamps[0].Date != DateTime.MinValue &&
                                      timestamps[measurements.Length - 1].Date != DateTime.MinValue &&
                                      timestamps[0].Date.Ticks < timestamps[measurements.Length - 1].Ticks;

                int numAvailableTimestamps = timestamps.Count(x => x != DateTime.MinValue && x != DateTime.MaxValue);
                if (numAvailableTimestamps < 2)
                {
                    MessageBox.Show(
                        m_TangraHost.ParentWindow,
                        "This light curve does not seem to have timestamps saved and cannot be processed.",
                        "Eclipsing Binaries Addin for Tangra",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return;
                }

                if (!hasReliableTimeBase)
                {
                    if (MessageBox.Show(
                            m_TangraHost.ParentWindow,
                            "This light curve may not have a reliable time base. Do you want to continue?",
                            "Eclipsing Binaries Addin for Tangra",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Warning) == DialogResult.No)
                    {
                        return;
                    }
                }

                // Fill in missing timestamps when times are entered manually
                if (!dataProvider.HasEmbeddedTimeStamps)
                {
                    int idx = 0;
                    for (; idx < timestamps.Count / 2 && timestamps[idx] == DateTime.MinValue; idx++)
                    {
                        ;
                    }
                    int      firstTimeStampIndex = idx;
                    DateTime firstTimeStamp      = timestamps[idx];

                    idx = timestamps.Count - 1;
                    for (; idx > timestamps.Count / 2 && timestamps[idx] == DateTime.MinValue; idx--)
                    {
                        ;
                    }
                    int      lastTimeStampIndex = idx;
                    DateTime lastTimeStamp      = timestamps[idx];

                    long ticksPerFrame = (lastTimeStamp.Ticks - firstTimeStamp.Ticks) / (lastTimeStampIndex - firstTimeStampIndex);
                    for (int i = 0; i < timestamps.Count; i++)
                    {
                        timestamps[i] = firstTimeStamp.AddTicks(ticksPerFrame * (i - firstTimeStampIndex));
                    }
                }

                double[] secondsFromUTMidnight   = new double[timestamps.Count];
                long     startFrameStartDayTicks = timestamps[0].Date.Ticks;

                for (int i = 0; i < timestamps.Count; i++)
                {
                    if (timestamps[i] != DateTime.MinValue)
                    {
                        secondsFromUTMidnight[i] = Math.Truncate(new TimeSpan(timestamps[i].Ticks - startFrameStartDayTicks).TotalSeconds * 10000) / 10000.0;
                    }
                    else
                    {
                        secondsFromUTMidnight[i] = 0;
                    }
                }

                // Now go and get the comparison star data
                if (dataProvider.NumberOfMeasuredComparisonObjects > 0)
                {
                    ISingleMeasurement[] compMeasurements = frm.ComparisonStarIndex == 0 ? dataProvider.GetTargetMeasurements() : dataProvider.GetComparisonObjectMeasurements(frm.ComparisonStarIndex - 1);

                    if (compMeasurements != null)
                    {
                        double[] compDataWithBg;
                        double[] compBg;

                        if (frm.UseNormalisation)
                        {
                            compDataWithBg = compMeasurements.Select(x => x.IsSuccessful ? (double)(x.Measurement + x.Background) : double.NaN).ToArray();
                            compBg         = compMeasurements.Select(x => x.IsSuccessful ? (double)x.Background : double.NaN).ToArray();
                        }
                        else
                        {
                            double compDataWithBgAverage = compMeasurements.Average(x => x.IsSuccessful ? x.Measurement + x.Background : double.NaN);
                            double compBgAverage         = compMeasurements.Average(x => x.IsSuccessful ? x.Background : double.NaN);
                            compDataWithBg = compMeasurements.Select(x => compDataWithBgAverage).ToArray();
                            compBg         = compMeasurements.Select(x => compBgAverage).ToArray();
                        }

                        double jdAtUtcMidnight = DateUtcToJulian(new DateTime(startFrameStartDayTicks));


                        KweeVanWoerdenResult result;
                        PolynomialFitResult  polyResult;

                        var times             = new List <double>();
                        var dataWithBgLst     = new List <double>();
                        var dataBgLst         = new List <double>();
                        var compDataWithBgLst = new List <double>();
                        var compBgLst         = new List <double>();

                        if (frm.IncludeDataFrom > 0 || frm.IncludeDataTo < dataWithBg.Length - 1)
                        {
                            double[] timesAlt          = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            double[] dataWithBgAlt     = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            double[] dataBgAlt         = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            double[] compDataWithBgAlt = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            double[] compBgAlt         = new double[frm.IncludeDataTo - frm.IncludeDataFrom];
                            for (int i = frm.IncludeDataFrom; i < frm.IncludeDataTo; i++)
                            {
                                timesAlt[i - frm.IncludeDataFrom]          = secondsFromUTMidnight[i];
                                dataWithBgAlt[i - frm.IncludeDataFrom]     = dataWithBg[i];
                                dataBgAlt[i - frm.IncludeDataFrom]         = dataBg[i];
                                compDataWithBgAlt[i - frm.IncludeDataFrom] = compDataWithBg[i];
                                compBgAlt[i - frm.IncludeDataFrom]         = compBg[i];
                            }

                            times.AddRange(timesAlt);
                            dataWithBgLst.AddRange(dataWithBgAlt);
                            dataBgLst.AddRange(dataBgAlt);
                            compDataWithBgLst.AddRange(compDataWithBgAlt);
                            compBgLst.AddRange(compBgAlt);

                            RemoveInvalidMeasurements(times, dataWithBgLst, dataBgLst, compDataWithBgLst, compBgLst);
                        }
                        else
                        {
                            times.AddRange(secondsFromUTMidnight);
                            dataWithBgLst.AddRange(dataWithBg);
                            dataBgLst.AddRange(dataBg);
                            compDataWithBgLst.AddRange(compDataWithBg);
                            compBgLst.AddRange(compBg);

                            RemoveInvalidMeasurements(times, dataWithBgLst, dataBgLst, compDataWithBgLst, compBgLst);
                        }


                        result     = Kwee_van_Woerden(times.Count, jdAtUtcMidnight, times.ToArray(), dataWithBgLst.ToArray(), dataBgLst.ToArray(), compDataWithBgLst.ToArray(), compBgLst.ToArray(), useFineGrainedBins);
                        polyResult = useCurveFitting
                                                        ? PolynomialFit(times.Count, jdAtUtcMidnight, times.ToArray(), dataWithBgLst.ToArray(), dataBgLst.ToArray(), compDataWithBgLst.ToArray(), compBgLst.ToArray(), (int)result.Start_Light_Curve_Obs_Index, (int)result.Stop_Light_Curve_Obs_Index, result.Time_Of_Minimum)
                                                        : null;

                        PresentResults(frm.VariableStarIndex, result, polyResult);
                    }
                }
            }
        }