public void Map_CalculatesCorrectSegmentWidth(Tuple <double, double, double> testData)
        {
            var area          = testData.Item1;
            var depth         = testData.Item2;
            var expectedWidth = testData.Item3;

            var panelItem = _gaugingSummaryItem.PanelItems.First();

            panelItem.Area  = area;
            panelItem.Depth = depth;

            var result        = _verticalMapper.Map(_gaugingSummaryItem, _deploymentMethod);
            var firstVertical = result.First();

            Assert.That(DoubleHelper.AreEqual(firstVertical.Segment.Width, expectedWidth));
        }
        public void Map_CalculatesCorrectTotalDischargePortionValues(Tuple <double[], double[]> testData)
        {
            var inputDischargeValues = testData.Item1;
            var panelItems           = _gaugingSummaryItem.PanelItems.ToList();

            for (var i = 0; i < panelItems.Count; i++)
            {
                panelItems[i].Flow = inputDischargeValues[i];
            }

            var result = _verticalMapper.Map(_gaugingSummaryItem, _deploymentMethod);

            var expectedTotalDischargePortionValues = testData.Item2;

            for (var i = 0; i < result.Count; i++)
            {
                Assert.That(DoubleHelper.AreEqual(result[i].Segment.TotalDischargePortion,
                                                  expectedTotalDischargePortionValues[i]));
            }
        }
        public IEnumerable <LevelSurvey> Map(EHSN eHsn)
        {
            if (eHsn == null)
            {
                throw new ArgumentNullException(nameof(eHsn));
            }

            var parsedSurvey = ParseLevelSurveyInfo(eHsn);

            var levelSurvey = (LevelSurvey)null;

            var levelSurveyTime = (DateTimeOffset?)null;

            foreach (var table in parsedSurvey.LevelCheckTables)
            {
                if (!table.upload.ToNullableBoolean() ?? false)
                {
                    continue;
                }

                var establishedRows = table.LevelChecksRow
                                      .Where(row => row.establish.ToNullableDouble().HasValue)
                                      .ToList();

                if (!establishedRows.Any())
                {
                    continue;
                }

                var originReferenceName = establishedRows.First().station;

                var measuredRows = establishedRows
                                   .Where(row => row == establishedRows.First() || row.foresight.ToNullableDouble().HasValue)
                                   .ToList();

                if (!measuredRows.Any())
                {
                    continue;
                }

                if (levelSurvey == null)
                {
                    levelSurvey = new LevelSurvey(originReferenceName)
                    {
                        Comments = parsedSurvey.LevelCheckComments,
                        Party    = parsedSurvey.Party
                    };
                }
                else if (levelSurvey.OriginReferencePointName != originReferenceName)
                {
                    _logger.Error($"Can't change the {nameof(LevelSurvey)}.{nameof(levelSurvey.OriginReferencePointName)} from '{levelSurvey.OriginReferencePointName}' to '{originReferenceName}'. Retaining first origin.");
                }

                if (!levelSurveyTime.HasValue)
                {
                    levelSurveyTime = GetLevelSurveyTime(eHsn);
                }

                var distinctRows = measuredRows
                                   .DistinctBy(r => new { r.station })
                                   .ToList();

                var skippedRows = measuredRows
                                  .Where(r => !distinctRows.Contains(r))
                                  .ToList();

                if (skippedRows.Any())
                {
                    foreach (var skippedRow in skippedRows)
                    {
                        var keptRow = distinctRows.First(r => r.station == skippedRow.station);

                        _logger.Error($"'{keptRow.station}' using first circuit measurement of {keptRow.elevation}, and ignoring secondary measurement of {skippedRow.elevation}");
                    }
                }

                var measurements = distinctRows
                                   .Select(row => new LevelSurveyMeasurement(row.station, levelSurveyTime.Value, row.elevation.ToNullableDouble() ?? 0)
                {
                    Comments = row.comments
                })
                                   .ToList();

                var secondaryMeasurements = measurements
                                            .Where(m => IsReferencePointMeasured(levelSurvey, m.ReferencePointName))
                                            .ToList();

                foreach (var secondaryMeasurement in secondaryMeasurements)
                {
                    var existingMeasurement = levelSurvey
                                              .LevelSurveyMeasurements
                                              .Single(m => m.ReferencePointName.Equals(secondaryMeasurement.ReferencePointName, StringComparison.InvariantCultureIgnoreCase));

                    if (!DoubleHelper.AreEqual(secondaryMeasurement.MeasuredElevation, existingMeasurement.MeasuredElevation))
                    {
                        _logger.Error($"'{existingMeasurement.ReferencePointName}' with first measured elevation of {existingMeasurement.MeasuredElevation}. Ignoring secondary measured elevation of {secondaryMeasurement.MeasuredElevation}");
                    }
                    else
                    {
                        _logger.Info($"'{existingMeasurement.ReferencePointName}' was remeasured a second time with the same elevation of {secondaryMeasurement.MeasuredElevation}");
                    }
                }

                var newMeasurements = measurements
                                      .Where(m => !secondaryMeasurements.Contains(m))
                                      .ToList();

                levelSurvey.LevelSurveyMeasurements.AddRange(newMeasurements);
            }

            var levelSurveys = new List <LevelSurvey>();

            if (levelSurvey != null)
            {
                levelSurveys.Add(levelSurvey);
            }

            return(levelSurveys);
        }
        private void AddMeanGageHeight(DischargeActivity dischargeActivity)
        {
            if (_ehsn.StageMeas == null)
            {
                return;
            }

            var stageMeasurementSummary = new StageMeasurementMapper(_ehsn)
                                          .Map();

            if (stageMeasurementSummary == null)
            {
                return;
            }

            var sensorResetCorrectionComment = stageMeasurementSummary.SensorResetCorrection.HasValue
                ? $"Sensor Reset Correction of {stageMeasurementSummary.SensorResetCorrection:F3}"
                : string.Empty;
            var gageCorrectionComment = stageMeasurementSummary.GageCorrection.HasValue
                ? $"Gage Correction of {stageMeasurementSummary.GageCorrection:F3}"
                : string.Empty;

            var meanGaugeHeightComment = string.Empty;

            if (!DoubleHelper.AreEqual(stageMeasurementSummary.MeanGageHeight, stageMeasurementSummary.CorrectedMeanGageHeight))
            {
                meanGaugeHeightComment = $"Corrected M.G.H. includes {string.Join(" and ", new[]{sensorResetCorrectionComment, gageCorrectionComment}.Where(s => !string.IsNullOrWhiteSpace(s)))} applied to Weighted M.G.H of {stageMeasurementSummary.MeanGageHeight:F3}";

                if (!string.IsNullOrWhiteSpace(dischargeActivity.Comments))
                {
                    meanGaugeHeightComment = "\n" + meanGaugeHeightComment;
                }
            }

            var gageHeightMeasurements = GetGageHeightMeasurements(stageMeasurementSummary.Selector)
                                         .ToList();

            var isAverage = gageHeightMeasurements.Any() &&
                            "Average".Equals(_ehsn.StageMeas.MghMethod, StringComparison.InvariantCultureIgnoreCase);

            if (isAverage)
            {
                var meanGageHeight = gageHeightMeasurements
                                     .Where(ghm => ghm.Include)
                                     .Average(ghm => ghm.GageHeight.Value);

                isAverage = stageMeasurementSummary.CorrectedMeanGageHeight.ToString("F3").Equals(meanGageHeight.ToString("F3"));
            }

            if (isAverage)
            {
                foreach (var gageHeightMeasurement in gageHeightMeasurements)
                {
                    dischargeActivity.GageHeightMeasurements.Add(gageHeightMeasurement);
                }
            }
            else
            {
                dischargeActivity.ManuallyCalculatedMeanGageHeight = new Measurement(stageMeasurementSummary.MeanGageHeight, Units.DistanceUnitId);
                dischargeActivity.GageHeightAdjustmentAmount       = stageMeasurementSummary.SensorResetCorrection +
                                                                     stageMeasurementSummary.GageCorrection;
                dischargeActivity.GageHeightComments = meanGaugeHeightComment;
            }

            dischargeActivity.Comments = string.Join("\n",
                                                     new[] { dischargeActivity.Comments, _ehsn.StageMeas?.stageRemark, _ehsn.InstrumentDeployment?.GeneralInfo?.methodType, _ehsn.InstrumentDeployment?.GeneralInfo?.structureType, _ehsn.InstrumentDeployment?.GeneralInfo?.monitoringMethod }
                                                     .Where(s => !string.IsNullOrWhiteSpace(s)));
        }