public ERROR_CODES RunExperiment(IExperimentData experimentToRun)
        {
            //alot of constants here that later on need to be parametrized
            ERROR_CODES status = ERROR_CODES.AA_OK;
            PhaseData phaseData = null;
            double scopeSampleRate = 0;
            bool[] demodulatorToRead = new bool[LockinAmplifier.NumberOfDemodulators];
            for (int i = 0; i < LockinAmplifier.NumberOfDemodulators; i++)
            {
                demodulatorToRead[i] = true;
            }

            try
            {
                status |= SignalGenerator.UpdateOutputState(true);
                status |= SignalGenerator.UploadArbitrarySignalData(experimentToRun.SignalGeneratorData.ArbitrarySignalData, false);
                //LaserModulation.SignalFixer.Classes.Saver.SaveArrayToFile("C:\\Pts\\bad1.txt", experimentToRun.SignalGeneratorData.ArbitrarySignalData.ToArray());
                System.Threading.Thread.Sleep(500);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            if (status != ERROR_CODES.AA_OK)
            {
                status = status;
            }
            status = ERROR_CODES.NOT_CONNECTED_TO_ZI_SERVER;
            System.Threading.Thread.Sleep(1000);
            IEnumerable<double> signalData;

            double tmpAmplitude = 0;
            try
            {
                signalData = null;
                foreach (SignalTypes signalType in Enum.GetValues(typeof(SignalTypes)))
                {

                    ScopeInputChannel scopeInChl = experimentToRun.GetScopeChannelByDataType(signalType);
                    if (signalType == SignalTypes.PhotodiodeOutput)
                    {
                        try
                        {
                            status = Scope.ReadScopeData(scopeInChl, experimentToRun.ScopeData.PointsToSave, experimentToRun.ScopeData.SampleMode, 512, out signalData);
                            if (status != ERROR_CODES.AA_OK)
                            {
                                status = status;
                            }
                        }
                        catch (Exception ex)
                        {

                            string ds = ex.Message;
                        }
                    }
                    else
                    {
                        signalData = new double[0];
                    }

                    if (status != ERROR_CODES.AA_OK)
                    {
                        status = status;
                    }
                    switch (signalType)
                    {
                        case SignalTypes.PhotodiodeOutput:
                            double[] expectedOutput = new double[scopeCycleSize];
                            double[] normalizeSignalData = SignalDigitizer.NormalizeSignal(signalData.ToArray(), new DigitzerParameters(DigitizingMethods.Absolute, 0, 0, 0, false), out tmpAmplitude).ToArray();
                            expectedOutput = FunctionGenerator.PositivieNormalizeCosine(scopeCycleSize);

                            try
                            {
                                phaseData = PhaseAnalyzer.FindBinPhaseByShift(normalizeSignalData, expectedOutput, scopeCycleSize, -30, 30, 0, false);

                            }
                            catch (Exception ex)
                            {

                                string s = ex.Message;
                            }
                            break;
                        case SignalTypes.SignalGeneratorOutput:
                            phaseData = new PhaseData(0,0,0);
                            if (!workWithLockinScope)
                            {
                                double[] normalizeSignalGeneratorOutput = SignalDigitizer.NormalizeSignal(signalData.ToArray(), new DigitzerParameters(DigitizingMethods.Absolute, 0, 0, 0, false), out tmpAmplitude).ToArray();
                                double[] normalizeExpectedSignalGeneratoroutput = SignalDigitizer.NormalizeSignal(experimentToRun.SignalGeneratorData.ArbitrarySignalData.ToArray(), new DigitzerParameters(DigitizingMethods.Absolute, 0, 0, 0, false), out tmpAmplitude).ToArray();
                                phaseData = PhaseAnalyzer.FindBinPhaseByShift(normalizeSignalGeneratorOutput, normalizeExpectedSignalGeneratoroutput, scopeCycleSize, -30, 30, 0, false);
                            }
                            break;
                        case SignalTypes.SignalGeneratorSync:
                            phaseData = null;
                            break;
                    }
                    try
                    {
                        SignalData tmp = new SignalData(signalType, signalData.ToArray(), scopeSampleRate, phaseData);
                        experimentToRun.ScopeData.SavedData.Add(scopeInChl, tmp);
                    }
                    catch (Exception ex)
                    {
                        String s = ex.Message;
                    }
                }

            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            //add here lockin code.
            IDemodulatorDataPacket[] lockinData = null;
            try
            {
                LockinAmplifier.GetPolledData(demodulatorToRead, 1000, out lockinData);
                experimentToRun.DemodulatorsData = lockinData;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }

            try
            {
                double r1 = experimentToRun.DemodulatorsData[0].R.Mean;
                double std1 = experimentToRun.DemodulatorsData[0].R.StandardDeviation;

                double r2 = experimentToRun.DemodulatorsData[1].R.Mean;
                double std2 = experimentToRun.DemodulatorsData[1].R.StandardDeviation;
                //MatlabFigure dbg = new MatlabFigure();
                //dbg.AddPlot("R2", experimentToRun.DemodulatorsData[1].RSamples, false);
                //dbg.Show();
            }

            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return status;
        }