public MainWindowViewModel([NotNull] IViewAwareStatus viewAwareStatus,
                                           IMessageBoxService messageBox,
                                           IUIVisualizerService visualizer,
                                           IHRCSaveFileService saveFile)
 {
     _viewAwareStatus = viewAwareStatus;
     _messageBox = messageBox;
     _visualizer = visualizer;
     _saveFile = saveFile;
     _viewAwareStatus.ViewLoaded += () => _visualizer.ShowWindow("PSMBrowserView", new PSMBrowserViewModel(),false,(s, e) => Application.Current.Shutdown());
 }
        public async Task Analyze(Dispatcher dispatcher, IUIVisualizerService visualizer)
        {
            if (SelectedModeIds == null) throw new NullReferenceException("SelectedModeIds may not be null");
            if (SelectedSpeciesGuids == null) throw new NullReferenceException("SelectedSpeciesGuids may not be null");
            if (SelectedModeIds.Count == 0) throw new InvalidOperationException("SelectedModeIds may not be an empty list");
            if (SelectedSpeciesGuids.Count == 0) throw new InvalidOperationException("SelectedSpeciesGuids may not be an empty list");
            if (double.IsNaN(StartBinValue) || (StartBinValue < 0)) throw new InvalidOperationException("StartBinValue must be a positive value");
            if (double.IsNaN(BinWidth) || (BinWidth < 0)) throw new InvalidOperationException("BinWidth must be a positive value");
            if (BinCount <= 0) throw new InvalidOperationException("BinCount must be a positive value");
            if (FilterStartTime.Ticks < 0) throw new InvalidOperationException("FilterStartTime must be a positive value");
            if (FilterEndTime.Ticks < 0) throw new InvalidOperationException("FilterEndTime must be a positive value");
            if (FilterEndTime.Ticks <= FilterStartTime.Ticks) throw new InvalidOperationException("FilterEndTime must be greater than FilterStartTime");
            SimulationLog simulationLog = null;
            try
            {
                simulationLog = SimulationLog.Open(LogFilename);
                if (FilterEndTime.Ticks > (simulationLog.TimeStepSize.Ticks * simulationLog.TimeStepCount)) throw new InvalidOperationException("FilterEndTime cannot be greater than the last time step in the simulation");
                for (var speciesIndex = 0; speciesIndex < simulationLog.SpeciesRecords.Count; speciesIndex++) GuidToColorMap.Add(simulationLog.SpeciesRecords[speciesIndex].Guid, BarColors[speciesIndex % BarColors.Count]);

                Func<ActorExposureRecord, bool> modeFilter = record =>
                {
                    var speciesRecord = simulationLog.RecordFromActorID(record.ActorID) as SpeciesNameGuid;
                    return speciesRecord != null && SelectedModeIds.Contains(record.ModeID);
                };
                Func<ActorExposureRecord, bool> speciesFilter = record =>
                {
                    var speciesRecord = simulationLog.RecordFromActorID(record.ActorID) as SpeciesNameGuid;
                    return speciesRecord != null && SelectedSpeciesGuids.Contains(speciesRecord.Guid);
                };
                var histogramBinsViewModels = new ObservableCollection<HistogramBinsViewModel>();
                var scenarioName = simulationLog.ScenarioRecord.Name;
                var simulationStartTime = simulationLog.StartTime;
                SimulationExposuresViewModel viewModel = null;
                dispatcher.InvokeIfRequired(() =>
                {
                    viewModel = new SimulationExposuresViewModel(histogramBinsViewModels) { WindowTitle = string.Format("Scenario: {0} simulated on {1} (Processing)", scenarioName, simulationStartTime) };
                    Window = visualizer.ShowWindow("SimulationExposuresView", viewModel);
                });
                var modeThresholdHistogram = new ModeThresholdHistogram(this, simulationLog, StartBinValue, BinWidth, BinCount, modeFilter, speciesFilter);
                _modeBinsCollectionObserver = new CollectionObserver(modeThresholdHistogram.GroupedExposures.Groups)
                    .RegisterHandler((s, e) =>
                    {
                        switch (e.Action)
                        {
                            case NotifyCollectionChangedAction.Add:
                                foreach (GroupedExposuresHistogram histogram in e.NewItems) histogramBinsViewModels.Add(new HistogramBinsViewModel(histogram));
                                break;
                        }
                    });
                var timeStepIndex = 0;
                Task<bool> processTask = null;
                foreach (var timeStepRecord in simulationLog.Where(timeStepRecord => timeStepRecord.StartTime >= FilterStartTime && FilterEndTime >= timeStepRecord.StartTime))
                {
                    timeStepRecord.ReadAll();
                    timeStepIndex++;
                    var record = timeStepRecord;
                    if (processTask != null) await processTask;

                    processTask = modeThresholdHistogram.Process(record, dispatcher);

                    if (timeStepIndex % 10 == 0) UpdateHistogramDisplay();
                }
                if (processTask != null) await processTask;
                UpdateHistogramDisplay();
                viewModel.WindowTitle = string.Format("Scenario: {0} simulated on {1}", scenarioName, simulationStartTime);
                Debug.WriteLine("Finished processing simulation exposure file");
            }
            finally
            {
                if (simulationLog != null) simulationLog.Close();
            }
        }