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);
        }
Exemple #2
0
        /// <summary>
        /// Load csv.
        /// e.g. Load(csv, true, new string[]{"ExpandedPoints", "OriginalPoints"});
        /// </summary>
        /// <param name="csv">csv string used to load into AQCSV instance.</param>
        /// <param name="withMetaHader">If csv with metadata, this parameter should be true, otherwise set to false.</param>
        /// <param name="tablesRecordCount">string array, items used to show the number of records for each table.</param>
        /// <returns></returns>
        public static AQCSV Load(string csv, bool withMetaHader, string[] tablesRecordCount)
        {
            AQCSV ret = new AQCSV();

            if (string.IsNullOrEmpty(csv))
            {
                return(ret);
            }
            if (tablesRecordCount == null || tablesRecordCount.Length < 1)
            {
                return(ret);
            }
            ret.metaData = new AQCSVMetaData();
            ret.dataSet  = new AQCSVDataSet[tablesRecordCount.Length];
            int[] tablePoints     = new int[tablesRecordCount.Length];
            int[] tablePointsRead = new int[tablesRecordCount.Length];
            for (int i = 0; i < tablesRecordCount.Length; i++)
            {
                ret.dataSet[i] = new AQCSVDataSet();
                tablePoints[i] = tablePointsRead[i] = 0;
            }
            using (StringReader reader = new StringReader(csv))
            {
                string          line        = null;
                int             state       = 0; //0, meta header, 1, meta data, 2 data1 header, 3, data 1, 4 data2 header, 5, data 2 ....
                int             dataSection = 0;
                List <string[]> dateItems   = new List <string[]>();
                if (!withMetaHader)
                {
                    state = 2;
                }
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Length > 0)
                    {
                        if (state == 0)//meta header
                        {
                            ret.metaData.headers = CSVUtil.splitCCVLine(line);
                            ret.metaData.columns = ret.metaData.headers.Length;
                            state = 1;
                        }
                        else if (state == 1)//meta data
                        {
                            ret.metaData.datas = CSVUtil.splitCCVLine(line);
                            state = 2;
                            for (int i = 0; i < tablesRecordCount.Length; i++)
                            {
                                tablePoints[i] = int.Parse(ret.metaData[tablesRecordCount[i]]);
                            }
                        }
                        else
                        {
                            if (state % 2 == 0)//data header, oven state
                            {
                                ret.dataSet[dataSection].headers = CSVUtil.splitCCVLine(line);
                                ret.dataSet[dataSection].columns = ret.dataSet[0].headers.Length;
                                state++;
                            }
                            else//data, odd data
                            {
                                string[] dataItem = CSVUtil.splitCCVLine(line);
                                dateItems.Add(dataItem);
                                tablePointsRead[dataSection]++;
                                if (tablePointsRead[dataSection] == tablePoints[dataSection])
                                {
                                    ret.dataSet[dataSection].datas = dateItems.ToArray();
                                    dateItems.Clear();
                                    state++;
                                    dataSection++;
                                }
                            }
                        }
                    }
                }
                reader.Close();
            }
            return(ret);
        }
        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);
        }