Loading INAF.Apps.Uwp.SLabDataManager/Charts/SelectedRefBand.cs +21 −8 Original line number Diff line number Diff line Loading @@ -19,7 +19,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts if (!tryReadXMax()) xMax = 0d; IsReady = false; trySetBoundariesRelatedValues(); } #region properties Loading @@ -41,8 +41,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts if (SetProperty(ref xMax, value)) { saveXMax(value); if (XMin != 0 && XMax != 0) IsReady = true; trySetBoundariesRelatedValues(); } } } Loading @@ -56,8 +55,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts if (SetProperty(ref xMin, value)) { saveXMin(value); if (XMin != 0 && XMax != 0) IsReady = true; trySetBoundariesRelatedValues(); } } } Loading Loading @@ -108,9 +106,24 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts XMin = xMin; XMax = xMax; trySetBoundariesRelatedValues(); } private void trySetBoundariesRelatedValues() { if (XMin != 0 && XMax != 0) { SelectedSegmentText = "AlignmentConfigSelectedSegmentMessage".GetText() .Replace("r_v1", xMin.ToDoubleInvariantString()) .Replace("r_v2", xMax.ToDoubleInvariantString()); IsReady = true; } else { SelectedSegmentText = "AlignmentConfigNoSelectedSegmentMessage".GetText(); IsReady = false; } } } } INAF.Apps.Uwp.SLabDataManager/Charts/SpectraContainer.cs +181 −68 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ using INAF.Libraries.NetStandard.ScienceModels.Extensions; using Microsoft.Extensions.DependencyInjection; using INAF.Apps.Uwp.SLabDataManager.Extensions; using INAF.Libraries.Uwp.Logging; using INAF.Apps.Uwp.SLabDataManager.Models; namespace INAF.Apps.Uwp.SLabDataManager.Charts { Loading @@ -27,6 +28,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts SelectedRefBand = serviceProvider.GetRequiredService<SelectedRefBand>(); Spectra = new ObservableCollection<SpectrumModel>(); Summaries = new ObservableCollection<SpectrumSummaryModel>(); XAxisMajorStepValues = new List<double>(2); XAxisMajorStepValues.Add(Constants.Constants.X_AXIS_MAJOR_STEP_0DOT1); Loading @@ -42,6 +44,13 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts set { SetProperty(ref isError, value); } } private bool isAnySummaryUpdated; public bool IsAnySummaryUpdated { get { return isAnySummaryUpdated; } set { SetProperty(ref isAnySummaryUpdated, value); } } public SelectedRefBand SelectedRefBand { get; private set; } private ObservableCollection<SpectrumModel> spectra; Loading @@ -51,6 +60,13 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts private set { SetProperty(ref spectra, value); } } private ObservableCollection<SpectrumSummaryModel> summaries; public ObservableCollection<SpectrumSummaryModel> Summaries { get { return summaries; } private set { SetProperty(ref summaries, value); } } public SpectrumAlignmentConfigModel SpectrumAlignmentConfig { get; private set; } #region spectrums min/max Loading Loading @@ -89,23 +105,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts #endregion #endregion public async Task addSpectrumAsync(SpectrumModel spectrum) { if (Spectra.Any(x => x.Type == spectrum.Type)) { /* if a spectrum of same type is already present, then replace it... */ int index = Spectra.IndexOf(spectrum); Spectra[index] = spectrum; } else { /* ...otherwise, add it... */ Spectra.Add(spectrum); } await setBoundariesAsync(); } public void clear() { Spectra.Clear(); Loading Loading @@ -147,25 +146,11 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts return (xMin, xMax, yMin, yMax); } public SpectrumModel getSpectrumOfType(SpectrumType spectrumType) public async Task removeSpectrumOfTypeAsync(SpectrumType type) { return Spectra.FirstOrDefault(x => x.Type == spectrumType); } public async Task refreshBoundariesAsync() { (double xMin, double xMax, double yMin, double yMax) result = await getSpectrumAxesBoundariesAsync(); XAxisBoundaries.updateAxisBoundaries(result.xMin, result.xMax); YAxisBoundaries.updateAxisBoundaries(result.yMin, result.yMax); } private async Task setBoundariesAsync() { (double xMin, double xMax, double yMin, double yMax) result = await getSpectrumAxesBoundariesAsync(); XAxisBoundaries = new AxisBoundariesModel("x", result.xMin, result.xMax, spectra[0].Elements[0].MeasureUnit); YAxisBoundaries = new AxisBoundariesModel("y", result.yMin, result.yMax); var requestedSpectrum = tryGetSpectrumOfType(type); Spectra.Remove(requestedSpectrum); await updateBoundariesAsync(); } private void setXAxisMajorStep(WavelengthMeasureUnit wavelengthMeasureUnit) Loading Loading @@ -193,10 +178,100 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts //setXAxisMajorStep(wavelengthMeasureUnit); } #region spectra public async Task addOrUpdateSpectrumAsync(SpectrumModel spectrum) { if (Spectra.Any(x => x.Type == spectrum.Type)) { /* if a spectrum of same type is already present, then replace it... */ int index = Spectra.IndexOf(spectrum); Spectra[index] = spectrum; await updateBoundariesAsync(); } else { /* ...otherwise, add it... */ Spectra.Add(spectrum); await setBoundariesAsync(); } tryGetSummaryOfType(spectrum.Type).init(spectrum); } public SpectrumModel tryGetSpectrumOfType(SpectrumType spectrumType) { return Spectra.FirstOrDefault(x => x.Type == spectrumType); } #endregion #region summaries public void addOrUpdateSpectrumSummary(SpectrumSummaryModel spectrumSummary) { if (Summaries.Any(x => x.Type == spectrumSummary.Type)) { /* if a spectrum of same type is already present, then replace it... */ int index = Summaries.IndexOf(spectrumSummary); Summaries[index] = spectrumSummary; } else { /* ...otherwise, add it... */ Summaries.Add(spectrumSummary); } spectrumSummary.PropertyChanged += SpectrumSummary_PropertyChanged; } private void SpectrumSummary_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { bool isPropertyChangedRequired = false; if (e.PropertyName.Equals(nameof(SpectrumSummaryModel.Filepath)) || e.PropertyName.Equals(nameof(SpectrumSummaryModel.FileReadingStatus))) isPropertyChangedRequired = true; if (isPropertyChangedRequired) { IsAnySummaryUpdated = true; isAnySummaryUpdated = false; // for the next usage } } public void resetSummaryOfType(SpectrumType type) { var requestedSummary = tryGetSummaryOfType(type); if (requestedSummary != null) requestedSummary.reset(); } public SpectrumSummaryModel tryGetSummaryOfType(SpectrumType spectrumType) { return Summaries.FirstOrDefault(x => x.Type == spectrumType); } #endregion #region boundaries private async Task setBoundariesAsync() { (double xMin, double xMax, double yMin, double yMax) result = await getSpectrumAxesBoundariesAsync(); XAxisBoundaries = new AxisBoundariesModel("x", result.xMin, result.xMax, spectra[0].Elements[0].MeasureUnit); YAxisBoundaries = new AxisBoundariesModel("y", result.yMin, result.yMax); } public async Task updateBoundariesAsync() { (double xMin, double xMax, double yMin, double yMax) result = await getSpectrumAxesBoundariesAsync(); XAxisBoundaries.updateAxisBoundaries(result.xMin, result.xMax); YAxisBoundaries.updateAxisBoundaries(result.yMin, result.yMax); } #endregion #region alignment private SpectrumModel createAligningSpectrum() { var aligningSpectrum = (SpectrumModel)getSpectrumOfType(SpectrumType.Raw).Clone(); var aligningSpectrum = (SpectrumModel)tryGetSpectrumOfType(SpectrumType.Raw).Clone(); aligningSpectrum.setType(SpectrumType.Aligned); aligningSpectrum.updateTitle(); return aligningSpectrum; Loading @@ -216,6 +291,13 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts { aligningSpectrum = createAligningSpectrum(); var refSpectrum = tryGetSpectrumOfType(SpectrumType.Ref); if (refSpectrum != null) { multiplyByRef(ref aligningSpectrum, refSpectrum); } /* retrieve the reference segment and calculate the mean values at the borders of the segment for aligning other segments */ var refLowSeparator = SpectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault(x => x.CurrentValue >= SelectedRefBand.XMin && x.CurrentValue <= SelectedRefBand.XMax); var refHighSeparator = SpectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault(x => x.CurrentValue > refLowSeparator.CurrentValue); Loading @@ -227,10 +309,38 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts /* retrieve segments on the left of selected one for later alignment */ alignLeftSide(ref aligningSpectrum, refLowSeparator, refSegmentLeftMean); /* retrieve segments on the right of the selected one for later alignment */ alignRightSide(ref aligningSpectrum, refHighSeparator, refSegmentRightMean); } catch (Exception ex) { logger.Write<SpectraContainer>($"{nameof(alignRawSpectrumAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error); exMsg = ex.Message; } }); if (string.IsNullOrEmpty(exMsg)) Spectra.Add(aligningSpectrum); else { ErrorMessage = exMsg; IsError = true; } } private void alignLeftSide(ref SpectrumModel aligningSpectrum, WavelengthModel refLowSeparator, double refSegmentLeftMean) { var leftSeparators = SpectrumAlignmentConfig.WavelengthSeparators .Where(x => x.MinValue < refLowSeparator.MinValue) .OrderByDescending(x => x.MinValue); List<IEnumerable<ElementModel>> leftSegments = new List<IEnumerable<ElementModel>>(); double rightSeparatorCurrentValue = refLowSeparator.CurrentValue; double refLeftValue = refSegmentLeftMean; foreach (var leftSeparator in leftSeparators) Loading @@ -239,15 +349,19 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts .GetLeftSegmentElements(leftSeparator.CurrentValue, rightSeparatorCurrentValue) .FixLeftSegment(refLeftValue, SpectrumAlignmentConfig.NumOfPointsForAlignment); leftSegments.Add(elements); aligningSpectrum.UpdateElements(elements); rightSeparatorCurrentValue = leftSeparator.CurrentValue; refLeftValue = elements.GetSegmentLeftMeanValue(SpectrumAlignmentConfig.NumOfPointsForAlignment); } } /* retrieve segments on the right of the selected one for later alignment */ private void alignRightSide(ref SpectrumModel aligningSpectrum, WavelengthModel refHighSeparator, double refSegmentRightMean) { var rightSeparators = SpectrumAlignmentConfig.WavelengthSeparators.Where(x => x.MinValue > refHighSeparator.MinValue); List<IEnumerable<ElementModel>> rightSegments = new List<IEnumerable<ElementModel>>(); double leftSeparatorCurrentValue = refHighSeparator.CurrentValue; double refRightValue = refSegmentRightMean; foreach (var rightSeparator in rightSeparators) Loading @@ -256,26 +370,25 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts .GetRightSegmentElements(leftSeparatorCurrentValue, rightSeparator.CurrentValue) .FixRightSegment(refRightValue, SpectrumAlignmentConfig.NumOfPointsForAlignment); rightSegments.Add(elements); aligningSpectrum.UpdateElements(elements); leftSeparatorCurrentValue = rightSeparator.CurrentValue; refRightValue = elements.GetSegmentRightMeanValue(SpectrumAlignmentConfig.NumOfPointsForAlignment); } } catch (Exception ex) private void multiplyByRef(ref SpectrumModel aligningSpectrum, SpectrumModel refSpectrum) { logger.Write<SpectraContainer>($"{nameof(alignRawSpectrumAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error); exMsg = ex.Message; } }); double refMaxY = refSpectrum.Elements.Max(x => x.Y); if (string.IsNullOrEmpty(exMsg)) Spectra.Add(aligningSpectrum); else aligningSpectrum.Elements .AsParallel() .ForAll(x => { ErrorMessage = exMsg; IsError = true; } var refElement = refSpectrum.Elements.FirstOrDefault(y => y.X == x.X); x.updateY(x.Y * refElement.Y / refMaxY); }); } #endregion } Loading INAF.Apps.Uwp.SLabDataManager/Constants/Enums.cs +1 −1 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Constants Txt } public enum Status public enum FileReadingStatus { None, [Description("Ok")] Loading INAF.Apps.Uwp.SLabDataManager/Converters/Converters.cs +1 −90 Original line number Diff line number Diff line Loading @@ -7,11 +7,8 @@ using System; using System.Collections.Generic; using System.IO; using Telerik.Charting; using Windows.UI.Xaml; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; using static INAF.Apps.Uwp.SLabDataManager.Constants.Enums; namespace INAF.Apps.Uwp.SLabDataManager.Converters { Loading Loading @@ -141,7 +138,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Converters try { SpectraContainer spectraContainer = value as SpectraContainer; return spectraContainer.getSpectrumOfType(Libraries.NetStandard.ScienceModels.Enums.Enums.SpectrumType.Raw).Elements; return spectraContainer.tryGetSpectrumOfType(Libraries.NetStandard.ScienceModels.Enums.Enums.SpectrumType.Raw).Elements; } catch (Exception) { Loading @@ -155,92 +152,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Converters } } public sealed class StatusToImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { BitmapImage bitmapImage = null; Status status = (Status)value; switch (status) { case Status.Ok: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Ok.png")); break; case Status.Warning: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Warning.png")); break; case Status.Error: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Error.png")); break; } return bitmapImage; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class StatusToSolidColorBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { SolidColorBrush solidColorBrush = null; Status status = (Status)value; switch (status) { case Status.Ok: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.GREEN); break; case Status.Warning: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.YELLOW); break; case Status.Error: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.RED); break; } return solidColorBrush; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class StatusToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { Visibility visibility = Visibility.Collapsed; Status status = (Status)value; switch (status) { case Status.None: visibility = Visibility.Collapsed; break; case Status.Ok: case Status.Warning: case Status.Error: visibility = Visibility.Visible; break; } return visibility; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class ThemeBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) Loading INAF.Apps.Uwp.SLabDataManager/Converters/SpectrumSummaryConverters.cs 0 → 100644 +147 −0 Original line number Diff line number Diff line using INAF.Apps.Uwp.SLabDataManager.Charts; using INAF.Apps.Uwp.SLabDataManager.Helpers; using INAF.Apps.Uwp.SLabDataManager.Helpers.UI; using Microsoft.Toolkit.Mvvm.DependencyInjection; using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; using static INAF.Apps.Uwp.SLabDataManager.Constants.Enums; namespace INAF.Apps.Uwp.SLabDataManager.Converters { public sealed class SpectrumSummaryFilepathConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { try { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); return XamlHelpers.GetSpectrumFilepath(spectraContainer, spectrumType); } catch (Exception) { return string.Empty; } } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class SpectrumSummaryFileReadingStatusToImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); BitmapImage bitmapImage = null; FileReadingStatus status = XamlHelpers.GetSpectrumFileReadingStatus(spectraContainer, spectrumType); switch (status) { case FileReadingStatus.Ok: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Ok.png")); break; case FileReadingStatus.Warning: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Warning.png")); break; case FileReadingStatus.Error: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Error.png")); break; } return bitmapImage; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class SpectrumSummaryFileReadingStatusToMessageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); return XamlHelpers.GetSpectrumFileReadingText(spectraContainer, spectrumType); } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class SpectrumSummaryFileReadingStatusToSolidColorBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); SolidColorBrush solidColorBrush = null; FileReadingStatus status = XamlHelpers.GetSpectrumFileReadingStatus(spectraContainer, spectrumType); switch (status) { case FileReadingStatus.Ok: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.GREEN); break; case FileReadingStatus.Warning: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.YELLOW); break; case FileReadingStatus.Error: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.RED); break; } return solidColorBrush; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class SpectrumSummaryFileReadingStatusToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); Visibility visibility = Visibility.Collapsed; FileReadingStatus status = XamlHelpers.GetSpectrumFileReadingStatus(spectraContainer, spectrumType); switch (status) { case FileReadingStatus.None: visibility = Visibility.Collapsed; break; case FileReadingStatus.Ok: case FileReadingStatus.Warning: case FileReadingStatus.Error: visibility = Visibility.Visible; break; } return visibility; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } } Loading
INAF.Apps.Uwp.SLabDataManager/Charts/SelectedRefBand.cs +21 −8 Original line number Diff line number Diff line Loading @@ -19,7 +19,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts if (!tryReadXMax()) xMax = 0d; IsReady = false; trySetBoundariesRelatedValues(); } #region properties Loading @@ -41,8 +41,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts if (SetProperty(ref xMax, value)) { saveXMax(value); if (XMin != 0 && XMax != 0) IsReady = true; trySetBoundariesRelatedValues(); } } } Loading @@ -56,8 +55,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts if (SetProperty(ref xMin, value)) { saveXMin(value); if (XMin != 0 && XMax != 0) IsReady = true; trySetBoundariesRelatedValues(); } } } Loading Loading @@ -108,9 +106,24 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts XMin = xMin; XMax = xMax; trySetBoundariesRelatedValues(); } private void trySetBoundariesRelatedValues() { if (XMin != 0 && XMax != 0) { SelectedSegmentText = "AlignmentConfigSelectedSegmentMessage".GetText() .Replace("r_v1", xMin.ToDoubleInvariantString()) .Replace("r_v2", xMax.ToDoubleInvariantString()); IsReady = true; } else { SelectedSegmentText = "AlignmentConfigNoSelectedSegmentMessage".GetText(); IsReady = false; } } } }
INAF.Apps.Uwp.SLabDataManager/Charts/SpectraContainer.cs +181 −68 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ using INAF.Libraries.NetStandard.ScienceModels.Extensions; using Microsoft.Extensions.DependencyInjection; using INAF.Apps.Uwp.SLabDataManager.Extensions; using INAF.Libraries.Uwp.Logging; using INAF.Apps.Uwp.SLabDataManager.Models; namespace INAF.Apps.Uwp.SLabDataManager.Charts { Loading @@ -27,6 +28,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts SelectedRefBand = serviceProvider.GetRequiredService<SelectedRefBand>(); Spectra = new ObservableCollection<SpectrumModel>(); Summaries = new ObservableCollection<SpectrumSummaryModel>(); XAxisMajorStepValues = new List<double>(2); XAxisMajorStepValues.Add(Constants.Constants.X_AXIS_MAJOR_STEP_0DOT1); Loading @@ -42,6 +44,13 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts set { SetProperty(ref isError, value); } } private bool isAnySummaryUpdated; public bool IsAnySummaryUpdated { get { return isAnySummaryUpdated; } set { SetProperty(ref isAnySummaryUpdated, value); } } public SelectedRefBand SelectedRefBand { get; private set; } private ObservableCollection<SpectrumModel> spectra; Loading @@ -51,6 +60,13 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts private set { SetProperty(ref spectra, value); } } private ObservableCollection<SpectrumSummaryModel> summaries; public ObservableCollection<SpectrumSummaryModel> Summaries { get { return summaries; } private set { SetProperty(ref summaries, value); } } public SpectrumAlignmentConfigModel SpectrumAlignmentConfig { get; private set; } #region spectrums min/max Loading Loading @@ -89,23 +105,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts #endregion #endregion public async Task addSpectrumAsync(SpectrumModel spectrum) { if (Spectra.Any(x => x.Type == spectrum.Type)) { /* if a spectrum of same type is already present, then replace it... */ int index = Spectra.IndexOf(spectrum); Spectra[index] = spectrum; } else { /* ...otherwise, add it... */ Spectra.Add(spectrum); } await setBoundariesAsync(); } public void clear() { Spectra.Clear(); Loading Loading @@ -147,25 +146,11 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts return (xMin, xMax, yMin, yMax); } public SpectrumModel getSpectrumOfType(SpectrumType spectrumType) public async Task removeSpectrumOfTypeAsync(SpectrumType type) { return Spectra.FirstOrDefault(x => x.Type == spectrumType); } public async Task refreshBoundariesAsync() { (double xMin, double xMax, double yMin, double yMax) result = await getSpectrumAxesBoundariesAsync(); XAxisBoundaries.updateAxisBoundaries(result.xMin, result.xMax); YAxisBoundaries.updateAxisBoundaries(result.yMin, result.yMax); } private async Task setBoundariesAsync() { (double xMin, double xMax, double yMin, double yMax) result = await getSpectrumAxesBoundariesAsync(); XAxisBoundaries = new AxisBoundariesModel("x", result.xMin, result.xMax, spectra[0].Elements[0].MeasureUnit); YAxisBoundaries = new AxisBoundariesModel("y", result.yMin, result.yMax); var requestedSpectrum = tryGetSpectrumOfType(type); Spectra.Remove(requestedSpectrum); await updateBoundariesAsync(); } private void setXAxisMajorStep(WavelengthMeasureUnit wavelengthMeasureUnit) Loading Loading @@ -193,10 +178,100 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts //setXAxisMajorStep(wavelengthMeasureUnit); } #region spectra public async Task addOrUpdateSpectrumAsync(SpectrumModel spectrum) { if (Spectra.Any(x => x.Type == spectrum.Type)) { /* if a spectrum of same type is already present, then replace it... */ int index = Spectra.IndexOf(spectrum); Spectra[index] = spectrum; await updateBoundariesAsync(); } else { /* ...otherwise, add it... */ Spectra.Add(spectrum); await setBoundariesAsync(); } tryGetSummaryOfType(spectrum.Type).init(spectrum); } public SpectrumModel tryGetSpectrumOfType(SpectrumType spectrumType) { return Spectra.FirstOrDefault(x => x.Type == spectrumType); } #endregion #region summaries public void addOrUpdateSpectrumSummary(SpectrumSummaryModel spectrumSummary) { if (Summaries.Any(x => x.Type == spectrumSummary.Type)) { /* if a spectrum of same type is already present, then replace it... */ int index = Summaries.IndexOf(spectrumSummary); Summaries[index] = spectrumSummary; } else { /* ...otherwise, add it... */ Summaries.Add(spectrumSummary); } spectrumSummary.PropertyChanged += SpectrumSummary_PropertyChanged; } private void SpectrumSummary_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { bool isPropertyChangedRequired = false; if (e.PropertyName.Equals(nameof(SpectrumSummaryModel.Filepath)) || e.PropertyName.Equals(nameof(SpectrumSummaryModel.FileReadingStatus))) isPropertyChangedRequired = true; if (isPropertyChangedRequired) { IsAnySummaryUpdated = true; isAnySummaryUpdated = false; // for the next usage } } public void resetSummaryOfType(SpectrumType type) { var requestedSummary = tryGetSummaryOfType(type); if (requestedSummary != null) requestedSummary.reset(); } public SpectrumSummaryModel tryGetSummaryOfType(SpectrumType spectrumType) { return Summaries.FirstOrDefault(x => x.Type == spectrumType); } #endregion #region boundaries private async Task setBoundariesAsync() { (double xMin, double xMax, double yMin, double yMax) result = await getSpectrumAxesBoundariesAsync(); XAxisBoundaries = new AxisBoundariesModel("x", result.xMin, result.xMax, spectra[0].Elements[0].MeasureUnit); YAxisBoundaries = new AxisBoundariesModel("y", result.yMin, result.yMax); } public async Task updateBoundariesAsync() { (double xMin, double xMax, double yMin, double yMax) result = await getSpectrumAxesBoundariesAsync(); XAxisBoundaries.updateAxisBoundaries(result.xMin, result.xMax); YAxisBoundaries.updateAxisBoundaries(result.yMin, result.yMax); } #endregion #region alignment private SpectrumModel createAligningSpectrum() { var aligningSpectrum = (SpectrumModel)getSpectrumOfType(SpectrumType.Raw).Clone(); var aligningSpectrum = (SpectrumModel)tryGetSpectrumOfType(SpectrumType.Raw).Clone(); aligningSpectrum.setType(SpectrumType.Aligned); aligningSpectrum.updateTitle(); return aligningSpectrum; Loading @@ -216,6 +291,13 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts { aligningSpectrum = createAligningSpectrum(); var refSpectrum = tryGetSpectrumOfType(SpectrumType.Ref); if (refSpectrum != null) { multiplyByRef(ref aligningSpectrum, refSpectrum); } /* retrieve the reference segment and calculate the mean values at the borders of the segment for aligning other segments */ var refLowSeparator = SpectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault(x => x.CurrentValue >= SelectedRefBand.XMin && x.CurrentValue <= SelectedRefBand.XMax); var refHighSeparator = SpectrumAlignmentConfig.WavelengthSeparators.FirstOrDefault(x => x.CurrentValue > refLowSeparator.CurrentValue); Loading @@ -227,10 +309,38 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts /* retrieve segments on the left of selected one for later alignment */ alignLeftSide(ref aligningSpectrum, refLowSeparator, refSegmentLeftMean); /* retrieve segments on the right of the selected one for later alignment */ alignRightSide(ref aligningSpectrum, refHighSeparator, refSegmentRightMean); } catch (Exception ex) { logger.Write<SpectraContainer>($"{nameof(alignRawSpectrumAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error); exMsg = ex.Message; } }); if (string.IsNullOrEmpty(exMsg)) Spectra.Add(aligningSpectrum); else { ErrorMessage = exMsg; IsError = true; } } private void alignLeftSide(ref SpectrumModel aligningSpectrum, WavelengthModel refLowSeparator, double refSegmentLeftMean) { var leftSeparators = SpectrumAlignmentConfig.WavelengthSeparators .Where(x => x.MinValue < refLowSeparator.MinValue) .OrderByDescending(x => x.MinValue); List<IEnumerable<ElementModel>> leftSegments = new List<IEnumerable<ElementModel>>(); double rightSeparatorCurrentValue = refLowSeparator.CurrentValue; double refLeftValue = refSegmentLeftMean; foreach (var leftSeparator in leftSeparators) Loading @@ -239,15 +349,19 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts .GetLeftSegmentElements(leftSeparator.CurrentValue, rightSeparatorCurrentValue) .FixLeftSegment(refLeftValue, SpectrumAlignmentConfig.NumOfPointsForAlignment); leftSegments.Add(elements); aligningSpectrum.UpdateElements(elements); rightSeparatorCurrentValue = leftSeparator.CurrentValue; refLeftValue = elements.GetSegmentLeftMeanValue(SpectrumAlignmentConfig.NumOfPointsForAlignment); } } /* retrieve segments on the right of the selected one for later alignment */ private void alignRightSide(ref SpectrumModel aligningSpectrum, WavelengthModel refHighSeparator, double refSegmentRightMean) { var rightSeparators = SpectrumAlignmentConfig.WavelengthSeparators.Where(x => x.MinValue > refHighSeparator.MinValue); List<IEnumerable<ElementModel>> rightSegments = new List<IEnumerable<ElementModel>>(); double leftSeparatorCurrentValue = refHighSeparator.CurrentValue; double refRightValue = refSegmentRightMean; foreach (var rightSeparator in rightSeparators) Loading @@ -256,26 +370,25 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts .GetRightSegmentElements(leftSeparatorCurrentValue, rightSeparator.CurrentValue) .FixRightSegment(refRightValue, SpectrumAlignmentConfig.NumOfPointsForAlignment); rightSegments.Add(elements); aligningSpectrum.UpdateElements(elements); leftSeparatorCurrentValue = rightSeparator.CurrentValue; refRightValue = elements.GetSegmentRightMeanValue(SpectrumAlignmentConfig.NumOfPointsForAlignment); } } catch (Exception ex) private void multiplyByRef(ref SpectrumModel aligningSpectrum, SpectrumModel refSpectrum) { logger.Write<SpectraContainer>($"{nameof(alignRawSpectrumAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error); exMsg = ex.Message; } }); double refMaxY = refSpectrum.Elements.Max(x => x.Y); if (string.IsNullOrEmpty(exMsg)) Spectra.Add(aligningSpectrum); else aligningSpectrum.Elements .AsParallel() .ForAll(x => { ErrorMessage = exMsg; IsError = true; } var refElement = refSpectrum.Elements.FirstOrDefault(y => y.X == x.X); x.updateY(x.Y * refElement.Y / refMaxY); }); } #endregion } Loading
INAF.Apps.Uwp.SLabDataManager/Constants/Enums.cs +1 −1 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Constants Txt } public enum Status public enum FileReadingStatus { None, [Description("Ok")] Loading
INAF.Apps.Uwp.SLabDataManager/Converters/Converters.cs +1 −90 Original line number Diff line number Diff line Loading @@ -7,11 +7,8 @@ using System; using System.Collections.Generic; using System.IO; using Telerik.Charting; using Windows.UI.Xaml; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; using static INAF.Apps.Uwp.SLabDataManager.Constants.Enums; namespace INAF.Apps.Uwp.SLabDataManager.Converters { Loading Loading @@ -141,7 +138,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Converters try { SpectraContainer spectraContainer = value as SpectraContainer; return spectraContainer.getSpectrumOfType(Libraries.NetStandard.ScienceModels.Enums.Enums.SpectrumType.Raw).Elements; return spectraContainer.tryGetSpectrumOfType(Libraries.NetStandard.ScienceModels.Enums.Enums.SpectrumType.Raw).Elements; } catch (Exception) { Loading @@ -155,92 +152,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Converters } } public sealed class StatusToImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { BitmapImage bitmapImage = null; Status status = (Status)value; switch (status) { case Status.Ok: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Ok.png")); break; case Status.Warning: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Warning.png")); break; case Status.Error: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Error.png")); break; } return bitmapImage; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class StatusToSolidColorBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { SolidColorBrush solidColorBrush = null; Status status = (Status)value; switch (status) { case Status.Ok: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.GREEN); break; case Status.Warning: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.YELLOW); break; case Status.Error: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.RED); break; } return solidColorBrush; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class StatusToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { Visibility visibility = Visibility.Collapsed; Status status = (Status)value; switch (status) { case Status.None: visibility = Visibility.Collapsed; break; case Status.Ok: case Status.Warning: case Status.Error: visibility = Visibility.Visible; break; } return visibility; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class ThemeBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) Loading
INAF.Apps.Uwp.SLabDataManager/Converters/SpectrumSummaryConverters.cs 0 → 100644 +147 −0 Original line number Diff line number Diff line using INAF.Apps.Uwp.SLabDataManager.Charts; using INAF.Apps.Uwp.SLabDataManager.Helpers; using INAF.Apps.Uwp.SLabDataManager.Helpers.UI; using Microsoft.Toolkit.Mvvm.DependencyInjection; using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; using static INAF.Apps.Uwp.SLabDataManager.Constants.Enums; namespace INAF.Apps.Uwp.SLabDataManager.Converters { public sealed class SpectrumSummaryFilepathConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { try { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); return XamlHelpers.GetSpectrumFilepath(spectraContainer, spectrumType); } catch (Exception) { return string.Empty; } } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class SpectrumSummaryFileReadingStatusToImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); BitmapImage bitmapImage = null; FileReadingStatus status = XamlHelpers.GetSpectrumFileReadingStatus(spectraContainer, spectrumType); switch (status) { case FileReadingStatus.Ok: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Ok.png")); break; case FileReadingStatus.Warning: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Warning.png")); break; case FileReadingStatus.Error: bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/icons/Error.png")); break; } return bitmapImage; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class SpectrumSummaryFileReadingStatusToMessageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); return XamlHelpers.GetSpectrumFileReadingText(spectraContainer, spectrumType); } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class SpectrumSummaryFileReadingStatusToSolidColorBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); SolidColorBrush solidColorBrush = null; FileReadingStatus status = XamlHelpers.GetSpectrumFileReadingStatus(spectraContainer, spectrumType); switch (status) { case FileReadingStatus.Ok: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.GREEN); break; case FileReadingStatus.Warning: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.YELLOW); break; case FileReadingStatus.Error: solidColorBrush = new SolidColorBrush().FromHexString(Constants.Constants.RED); break; } return solidColorBrush; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public sealed class SpectrumSummaryFileReadingStatusToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string spectrumType = (string)parameter; SpectraContainer spectraContainer = Ioc.Default.GetService<SpectraContainer>(); Visibility visibility = Visibility.Collapsed; FileReadingStatus status = XamlHelpers.GetSpectrumFileReadingStatus(spectraContainer, spectrumType); switch (status) { case FileReadingStatus.None: visibility = Visibility.Collapsed; break; case FileReadingStatus.Ok: case FileReadingStatus.Warning: case FileReadingStatus.Error: visibility = Visibility.Visible; break; } return visibility; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } }