Ejemplo n.º 1
0
 /// <summary>
 /// Creates a new data series associated with the specific site, variable,
 /// method, quality control level and source. This series will contain zero
 /// data values after creation.
 /// </summary>
 /// <param name="site">the observation site (location of measurement)</param>
 /// <param name="variable">the observed variable</param>
 /// <param name="method">the observation method</param>
 /// <param name="qualControl">the quality control level of observed values</param>
 /// <param name="source">the source of the data values for this series</param>
 public Series(Site site, Variable variable, Method method, QualityControlLevel qualControl, Source source) : this()
 {
     Site                = site;
     Variable            = variable;
     Method              = method;
     QualityControlLevel = qualControl;
     Source              = source;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates a new data series associated with the specific site, variable,
 /// method, quality control level and source. This series will contain zero
 /// data values after creation.
 /// </summary>
 /// <param name="site">the observation site (location of measurement)</param>
 /// <param name="variable">the observed variable</param>
 /// <param name="method">the observation method</param>
 /// <param name="qualControl">the quality control level of observed values</param>
 /// <param name="source">the source of the data values for this series</param>
 public SeriesMetadata(Site site, Variable variable, Method method, QualityControlLevel qualControl, Source source)
 {
     ValueCount = 0;
     Site = site;
     Variable = variable;
     Method = method;
     QualityControlLevel = qualControl;
     Source = source;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a new data series associated with the specific site, variable,
 /// method, quality control level and source. This series will contain zero
 /// data values after creation.
 /// </summary>
 /// <param name="site">the observation site (location of measurement)</param>
 /// <param name="variable">the observed variable</param>
 /// <param name="method">the observation method</param>
 /// <param name="qualControl">the quality control level of observed values</param>
 /// <param name="source">the source of the data values for this series</param>
 public Series(Site site, Variable variable, Method method, QualityControlLevel qualControl, Source source)
     : this()
 {
     Site = site;
     Variable = variable;
     Method = method;
     QualityControlLevel = qualControl;
     Source = source;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a new data series associated with the specific site, variable,
 /// method, quality control level and source. This series will contain zero
 /// data values after creation.
 /// </summary>
 /// <param name="site">the observation site (location of measurement)</param>
 /// <param name="variable">the observed variable</param>
 /// <param name="method">the observation method</param>
 /// <param name="qualControl">the quality control level of observed values</param>
 /// <param name="source">the source of the data values for this series</param>
 public SeriesMetadata(Site site, Variable variable, Method method, QualityControlLevel qualControl, Source source)
 {
     ValueCount          = 0;
     Site                = site;
     Variable            = variable;
     Method              = method;
     QualityControlLevel = qualControl;
     Source              = source;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Reads the DataValues section
        /// </summary>
        protected override IList<Series> ReadDataValues(XmlReader r)
        {
            int valueCount;
            var lst = new List<DataValueWrapper>(Int32.TryParse(r.GetAttribute("count"), out valueCount) ? valueCount : 4);

            var qualifiers = new Dictionary<string, Qualifier>();
            var methods = new Dictionary<string, Method>();
            var sources = new Dictionary<string, Source>();
            var qualityControlLevels = new Dictionary<string, QualityControlLevel>();
            var samples = new Dictionary<string, Sample>();
            var labMethods = new Dictionary<string, LabMethod>();
            var offsets = new Dictionary<string, OffsetType>();
            var seriesDictionary = new Dictionary<string, Series>();

            while (r.Read())
            {
                if (r.NodeType == XmlNodeType.Element)
                {
                    if (XmlContext.AdvanceReaderPastEmptyElement(r))
                    {
                        //Empty element - advance and continue...
                        continue;
                    }

                    if (r.Name == "value")
                    {
                        //create a new empty data value and add it to the list
                        var wrapper = new DataValueWrapper();
                        var val = new DataValue();
                        wrapper.DataValue = val;
                        lst.Add(wrapper);

                        if (r.HasAttributes)
                        {
                            var censorCode = r.GetAttribute("censorCode");
                            if (!string.IsNullOrEmpty(censorCode))
                            {
                                val.CensorCode = censorCode;
                            }
                            val.LocalDateTime = Convert.ToDateTime(r.GetAttribute("dateTime"), CultureInfo.InvariantCulture);

                            //utcOffset
                            var utcOffset = r.GetAttribute("timeOffset");
                            val.UTCOffset = !String.IsNullOrEmpty(utcOffset) ? ConvertUtcOffset(utcOffset) : 0.0;

                            //dateTimeUtc
                            var dateTimeUTC = r.GetAttribute("dateTimeUTC");
                            val.DateTimeUTC = !String.IsNullOrEmpty(dateTimeUTC) ? Convert.ToDateTime(dateTimeUTC, CultureInfo.InvariantCulture) : val.LocalDateTime;

                            //method
                            var methodID = r.GetAttribute("methodCode");
                            if (String.IsNullOrEmpty(methodID))
                            {
                                //try methodID instead of methodCode
                                methodID = r.GetAttribute("methodID");
                                if (String.IsNullOrEmpty(methodID))
                                {
                                    methodID = "unknown"; //when a method is unspecified
                                }
                            }
                            if (!methods.ContainsKey(methodID))
                            {
                                var newMethod = Method.Unknown;
                                methods.Add(methodID, newMethod);
                            }
                            wrapper.MethodID = methodID;

                            //quality control level
                            var qualityCode = r.GetAttribute("qualityControlLevelCode");
                            if (String.IsNullOrEmpty(qualityCode))
                            {
                                qualityCode = r.GetAttribute("qualityControlLevelID");
                                if (string.IsNullOrEmpty(qualityCode))
                                {
                                    qualityCode = "unknown"; //when the quality control level is unspecified
                                }
                            }

                            //BCC - 24-Aug-2016 - Check for a quality code of space-delimited terms...
                            string[] terms = qualityCode.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                            foreach (var term in terms)
                            {
                                if (!qualityControlLevels.ContainsKey(term))
                            {
                                var qualControl = QualityControlLevel.Unknown;
                                    qualControl.Code = term;
                                    qualControl.Definition = term;
                                    qualControl.Explanation = term;
                                    qualityControlLevels.Add(term, qualControl);
                            }
                            }

                            wrapper.QualityID = qualityCode;

                            //source
                            string sourceID = r.GetAttribute("sourceCode");
                            if (string.IsNullOrEmpty(sourceID))
                            {
                                sourceID = r.GetAttribute("sourceID");
                                if (String.IsNullOrEmpty(sourceID))
                                {
                                    sourceID = "unknown"; //when a source is unspecified
                                }
                            }
                            if (!sources.ContainsKey(sourceID))
                            {
                                sources.Add(sourceID, Source.Unknown);
                            }
                            wrapper.SourceID = sourceID;
                            wrapper.SeriesCode = SeriesCodeHelper.CreateSeriesCode(methodID, qualityCode, sourceID); //----method-source-qualityControl combination----

                            //sample
                            string sampleCode = r.GetAttribute("labSampleCode");
                            if (!String.IsNullOrEmpty(sampleCode))
                            {
                                if (!samples.ContainsKey(sampleCode))
                                {
                                    samples.Add(sampleCode, Sample.Unknown);
                                }
                            }
                            wrapper.SampleID = sampleCode;

                            //qualifiers
                            string qualifierCodes = r.GetAttribute("qualifiers");
                            if (!String.IsNullOrEmpty(qualifierCodes))
                            {
                                if (!qualifiers.ContainsKey(qualifierCodes))
                                {
                                    var newQualifier = new Qualifier { Code = qualifierCodes };
                                    qualifiers.Add(qualifierCodes, newQualifier);
                                    val.Qualifier = newQualifier;
                                }
                                else
                                {
                                    val.Qualifier = qualifiers[qualifierCodes];
                                }
                            }

                            //vertical offset
                            string offsetCode = r.GetAttribute("offsetTypeCode");
                            if (string.IsNullOrEmpty(offsetCode))
                            {
                                offsetCode = r.GetAttribute("offsetTypeID");
                            }
                            if (!String.IsNullOrEmpty(offsetCode))
                            {
                                if (!offsets.ContainsKey(offsetCode))
                                {
                                    offsets.Add(offsetCode, new OffsetType());
                                }
                                string offsetValue = r.GetAttribute("offsetValue");
                                if (!String.IsNullOrEmpty(offsetValue))
                                {
                                    val.OffsetValue = Convert.ToDouble(offsetValue, CultureInfo.InvariantCulture);
                                }
                            }
                            wrapper.OffsetID = offsetCode;
                        }

                        //data value
                        r.Read();
                        val.Value = r.ReadContentAsDouble();

                    }
                    else if (r.Name == "method")
                    {
                        var method = ReadMethod(r);
                        var methodCodeKey = method.Code.ToString(CultureInfo.InvariantCulture);
                        if (methods.ContainsKey(methodCodeKey))
                        {
                            methods[methodCodeKey] = method;
                        }
                    }
                    else if (r.Name == "source")
                    {
                        var source = ReadSource(r);
                        var sourceCodeKey = source.OriginId.ToString(CultureInfo.InvariantCulture);
                        if (sources.ContainsKey(sourceCodeKey))
                        {
                            sources[sourceCodeKey] = source;
                        }
                    }
                    else if (r.Name == "qualityControlLevel")
                    {
                        var qcLevel = ReadQualityControlLevel(r);
                        var qcCodeKey = qcLevel.Code;
                        if (qualityControlLevels.ContainsKey(qcCodeKey))
                        {
                            qualityControlLevels[qcCodeKey] = qcLevel;
                        }
                    }
                    else if (r.Name == "qualifier")
                    {
                        ReadQualifier(r, qualifiers);
                    }
                    else if (r.Name == "sample")
                    {
                        ReadSample(r, samples, labMethods);
                    }
                    else if (r.Name == "offset")
                    {
                        ReadOffset(r, offsets);
                    }
                }
            }

            //to assign special properties of each data value
            foreach (var wrapper in lst)
            {
                var val = wrapper.DataValue;

                //which series does the data value belong to?
                var seriesCode = wrapper.SeriesCode;
                if (!seriesDictionary.ContainsKey(seriesCode))
                {
                    Series newSeries = new Series();
                    seriesDictionary.Add(seriesCode, newSeries);
                    //assign method, source and qual.control level
                    try
                    {
                        newSeries.Method = methods[SeriesCodeHelper.GetMethodCode(seriesCode)];
                        newSeries.Source = sources[SeriesCodeHelper.GetSourceCode(seriesCode)];

                        //BCC - 24-Aug-2016 - Add logic to handle space-delimited quality codes...
                        var qcCode = SeriesCodeHelper.GetQualityCode(seriesCode);
                        string[] terms = qcCode.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                        if (1 == terms.Length)
                        {
                            //One quality code found...
                        newSeries.QualityControlLevel = qualityControlLevels[SeriesCodeHelper.GetQualityCode(seriesCode)];
                    }
                        else
                        {
                            //Multiple quality codes found...
                            var qcl = new QualityControlLevel();
                            string separator = " | ";

                            qcl.Code = qcCode;
                            qcl.Definition = String.Empty;
                            qcl.Explanation = String.Empty;
                            foreach (var term in terms)
                            {
                                var qclTemp = qualityControlLevels[term];

                                qcl.Definition += qclTemp.Definition + separator;
                                qcl.Explanation += qclTemp.Explanation + separator;
                            }

                            qcl.Definition = qcl.Definition.Substring(0, qcl.Definition.Length - separator.Length);
                            qcl.Explanation = qcl.Explanation.Substring(0, qcl.Explanation.Length - separator.Length);

                            //Set the OriginId to a minimum value to indicate a 'pseudo' instance...
                            int min = qualityControlLevels.Values.Min(value => value.OriginId);
                            qcl.OriginId = min - 1;

                            newSeries.QualityControlLevel = qcl;

                            //Add compound key to dictionary, if indicated...
                            if ( ! qualityControlLevels.ContainsKey(qcl.Code))
                            {
                                qualityControlLevels.Add(qcl.Code, qcl);
                            }
                        }
                    }
                    catch(Exception ex)
                    {
                        //Any exception happening here?
                        string msg = ex.Message;

                        int n = 5;

                        ++n;
                    }
                }

                //add the data value to the correct series
                var series = seriesDictionary[seriesCode];
                series.DataValueList.Add(val);
                val.Series = series;

                Sample sample;
                if (!string.IsNullOrEmpty(wrapper.SampleID) &&
                    samples.TryGetValue(wrapper.SampleID, out sample))
                {
                    val.Sample = sample;
                }
                OffsetType offset;
                if (!string.IsNullOrEmpty(wrapper.OffsetID) &&
                    offsets.TryGetValue(wrapper.OffsetID, out offset))
                {
                    val.OffsetType = offset;
                }
                if (series.Method == null)
                {
                    series.Method = methods[wrapper.MethodID];
                }
                if (series.Source == null)
                {
                    series.Source = sources[wrapper.SourceID];
                }
                if (series.QualityControlLevel == null)
                {
                    series.QualityControlLevel = qualityControlLevels[wrapper.QualityID];
                }
            }
            //to check the qualifiers
            CheckQualifiers(qualifiers);

            return seriesDictionary.Values.ToList();
        }