Esempio n. 1
0
        // -----------------------------------------------------------------------------------------------------------------------------------------------------

        private static void _parseTsValue(XmlReader reader, TimeSeries currTS, List <string> currentPath, PI pi)
        {
            DateTime dt    = _parseDate(reader, currentPath);
            double   value = double.Parse(_getRequiredAttribute(reader, "value", currentPath), CultureInfo.InvariantCulture);
            int      flag  = 0;
            string   attr  = reader["flag"];

            if (attr != null)
            {
                int.TryParse(attr, out flag);
            }
            ////int.Parse(_getRequiredAttribute(reader, "flag", currentPath), CultureInfo.InvariantCulture);

            currTS.Values.Add(dt, new TSValue(value, flag));
        }
Esempio n. 2
0
        /// <summary>
        /// Pars FEWS PI file to memory time series data structure
        /// </summary>
        /// <param name="path">PI file path</param>
        public static PI Read(string path)
        {
            var pi = new PI();

            bool       inHeaderSection = false;
            TimeSeries currTS          = null;
            var        currentPath     = new List <string>();

            using (XmlReader reader = XmlReader.Create(path))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        currentPath.Add(reader.Name);
                        if (!inHeaderSection)
                        {
                            switch (reader.Name)
                            {
                            case "TimeSeries":
                                break;

                            case "timeZone":
                                reader.Read();
                                pi.TimeZone = double.Parse(reader.Value.Trim(), CultureInfo.InvariantCulture);
                                break;

                            case "series":
                                currTS = new TimeSeries();
                                break;

                            case "header":
                                inHeaderSection = true;
                                break;

                            case "event":
                                _parseTsValue(reader, currTS, currentPath, pi);
                                break;

                            default:
                                throw new Exception($"Unknown element {_getCurrElemPath(currentPath)}.");
                            }

                            if (reader.IsEmptyElement)
                            {
                                currentPath.RemoveAt(currentPath.Count - 1);
                            }
                        }
                        else
                        {
                            // TS header section

                            switch (reader.Name)
                            {
                            case "type":
                                reader.Read();
                                currTS.Type = reader.Value.Trim();
                                break;

                            case "locationId":
                                reader.Read();
                                currTS.LocationId = reader.Value.Trim();
                                break;

                            case "parameterId":
                                reader.Read();
                                currTS.ParameterId = reader.Value.Trim();
                                break;

                            case "timeStep":
                                currTS.TimeStep = _parseTimeStep(reader, currentPath);
                                break;

                            case "startDate":
                                currTS.StartDate = _parseDate(reader, currentPath);
                                break;

                            case "endDate":
                                currTS.EndDate = _parseDate(reader, currentPath);
                                break;

                            case "missVal":
                                reader.Read();
                                currTS.MissVal = double.Parse(reader.Value.Trim(), CultureInfo.InvariantCulture);
                                break;

                            case "stationName":
                                reader.Read();
                                currTS.StationName = reader.Value.Trim();
                                break;

                            case "lat":
                                reader.Read();
                                currTS.Lat = double.Parse(reader.Value.Trim(), CultureInfo.InvariantCulture);
                                break;

                            case "lon":
                                reader.Read();
                                currTS.Lon = double.Parse(reader.Value.Trim(), CultureInfo.InvariantCulture);
                                break;

                            case "x":
                                reader.Read();
                                currTS.X = double.Parse(reader.Value.Trim(), CultureInfo.InvariantCulture);
                                break;

                            case "y":
                                reader.Read();
                                currTS.Y = double.Parse(reader.Value.Trim(), CultureInfo.InvariantCulture);
                                break;

                            case "z":
                                reader.Read();
                                currTS.Z = double.Parse(reader.Value.Trim(), CultureInfo.InvariantCulture);
                                break;

                            case "units":
                                reader.Read();
                                currTS.Units = reader.Value.Trim();
                                break;

                            case "forecastDate":
                                currTS.ForecastDate = _parseDate(reader, currentPath);
                                break;

                            case "ensembleId":
                                reader.Read();
                                currTS.EnsembleId = reader.Value.Trim();
                                break;

                            case "ensembleMemberId":
                                reader.Read();
                                currTS.EnsembleMemberId = reader.Value.Trim();
                                break;

                            case "ensembleMemberIndex":
                                reader.Read();
                                currTS.EnsembleMemberIndex = int.Parse(reader.Value.Trim(), CultureInfo.InvariantCulture);
                                break;

                            case "thresholds":
                                break;

                            case "creationDate":
                                break;

                            case "creationTime":
                                break;

                            case "highLevelThreshold":
                                var threshoald = _ParseThresholdValue(reader, currentPath, currTS.LocationId, currTS.ParameterId);
                                pi.ThresholdsValues.AddThreshold(threshoald);
                                break;

                            default:
                                throw new Exception($"Unknown header element {_getCurrElemPath(currentPath)}.");
                            }

                            if (reader.IsEmptyElement)
                            {
                                currentPath.RemoveAt(currentPath.Count - 1);
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        currentPath.RemoveAt(currentPath.Count - 1);

                        switch (reader.Name)
                        {
                        case "series":
                            pi.TimeSeries.Add(currTS);
                            currTS = null;
                            break;

                        case "header":
                            inHeaderSection = false;
                            break;
                        }
                    }
                }
            }

            return(pi);
        }