public AllTablesResult LoadAllTables(int stepPrecision)
        {
            var result = new AllTablesResult();

            var tablesCsv = HydroModel.GetExpandedRatingTables(stepPrecision, CsvMetaHeader, CsvMetadata);

            if (string.IsNullOrEmpty(tablesCsv))
            {
                return(result);
            }

            var csvRatings = AQCSV.Load(tablesCsv, true, new[] { "NumTables", "NumPeriods", "NumPoints" });

            var tableNumber = CSVUtil.ToDouble(csvRatings.dataSet[0].datas, 0);
            var tableName   = CSVUtil.ToString(csvRatings.dataSet[0].datas, 1);
            var numPeriods  = CSVUtil.ToDouble(csvRatings.dataSet[0].datas, 2);
            var numPoints   = CSVUtil.ToDouble(csvRatings.dataSet[0].datas, 3);

            for (var i = 0; i < tableNumber.Length; ++i)
            {
                result.Tables.Add(new Table
                {
                    TableNumber = tableNumber[i],
                    TableName   = tableName[i],
                    NumPeriods  = numPeriods[i],
                    NumPoints   = numPoints[i]
                });
            }

            var tableNumber2 = CSVUtil.ToDouble(csvRatings.dataSet[1].datas, 0);
            var startDate    = CSVUtil.ToString(csvRatings.dataSet[1].datas, 1);
            var endDate      = CSVUtil.ToString(csvRatings.dataSet[1].datas, 2);

            for (var i = 0; i < tableNumber2.Length; ++i)
            {
                result.TableDates.Add(new TableDate
                {
                    TableNumber = tableNumber2[i],
                    StartDate   = DateTimeParser.Parse(LocationData, startDate[i]),
                    EndDate     = string.IsNullOrEmpty(endDate[i]) ? DateTimeOffset.MaxValue : DateTimeParser.Parse(LocationData, endDate[i])
                });
            }

            var tableNumber3 = CSVUtil.ToDouble(csvRatings.dataSet[2].datas, 0);
            var input        = CSVUtil.ToDouble(csvRatings.dataSet[2].datas, 1);
            var output       = CSVUtil.ToDouble(csvRatings.dataSet[2].datas, 2);

            for (var i = 0; i < tableNumber3.Length; ++i)
            {
                result.TableValues.Add(new TableValue
                {
                    TableNumber = tableNumber3[i],
                    Input       = input[i],
                    Output      = output[i]
                });
            }

            return(result);
        }
        public RatingCurveResult LoadRatingCurve(RatingCurve curve, int stepPrecision, DateTimeOffset curveEffectiveTime)
        {
            var result = new RatingCurveResult();

            var curveCsv = HydroModel.GetExpandedRatingTable(
                curveEffectiveTime.UtcDateTime.ToOADate(),
                stepPrecision,
                CsvMetaHeader,
                CsvMetadata);

            if (string.IsNullOrEmpty(curveCsv))
            {
                return(result);
            }

            var csvRating = AQCSV.Load(curveCsv, true, new[] { "ExpandedPoints", "OriginalPoints" });

            var regularStages     = CSVUtil.ToDouble(csvRating.dataSet[0].datas, 0);
            var regularDischarges = CSVUtil.ToDouble(csvRating.dataSet[0].datas, 1);
            var shiftStages       = CSVUtil.ToDouble(csvRating.dataSet[0].datas, 2);
            var shiftDischarges   = CSVUtil.ToDouble(csvRating.dataSet[0].datas, 3);

            for (var i = 0; i < regularStages.Length; ++i)
            {
                result.ExpandedPoints.Add(new ExpandedPoint
                {
                    Stage            = regularStages[i],
                    Discharge        = regularDischarges[i],
                    ShiftedStage     = shiftStages[i],
                    ShiftedDischarge = shiftDischarges[i]
                });
            }

            var equationStages     = CSVUtil.ToDouble(csvRating.dataSet[1].datas, 0);
            var equationDischarges = CSVUtil.ToDouble(csvRating.dataSet[1].datas, 1);

            for (var i = 0; i < equationStages.Length; ++i)
            {
                result.EquationPoints.Add(new EquationPoint
                {
                    Stage     = equationStages[i],
                    Discharge = equationDischarges[i]
                });
            }

            var curveStartTime = curve.PeriodsOfApplicability.First().StartTime;
            var curveEndTime   = curve.PeriodsOfApplicability.Last().EndTime;

            var exceptions = GetRatingExceptions(curve.Id);

            var fieldVisitReadings = FieldVisitReadings.Where(r =>
            {
                var time = new DateTimeOffset(DateTime.SpecifyKind(r.MeasurementTime, DateTimeKind.Unspecified), TimeSpan.FromHours(r.UtcOffset));

                return(exceptions.IncludeIds.Contains(r.DiscreteMeasurementId) ||
                       !exceptions.ExcludeIds.Contains(r.DiscreteMeasurementId) && curveStartTime <= time && time < curveEndTime);
            }).ToList();

            var measurementIds = fieldVisitReadings
                                 .Select(f => f.DiscreteMeasurementId)
                                 .Distinct();

            result.RatingMeasurements = measurementIds
                                        .Select(id => CreateRatingMeasurement(fieldVisitReadings.Where(f => f.DiscreteMeasurementId == id).ToList()))
                                        .Where(r => r != null)
                                        .ToList();

            return(result);
        }