Пример #1
0
        protected virtual Source ReadSource(XmlReader reader)
        {
            var source   = Source.Unknown;
            var sourceID = reader.GetAttribute("sourceID");

            if (!String.IsNullOrEmpty(sourceID))
            {
                source.OriginId = Convert.ToInt32(sourceID);
            }

            if (reader.IsEmptyElement)
            {
                return(source);
            }

            while (reader.Read())
            {
                var nodeName = reader.Name.ToLower();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (XmlContext.AdvanceReaderPastEmptyElement(reader))
                    {
                        //Empty element - advance and continue...
                        continue;
                    }

                    switch (nodeName)
                    {
                    case "organization":     // WML 1.0/1.1
                        reader.Read();
                        source.Organization = reader.Value;
                        break;

                    case "sourcedescription":      // WML 1.0/1.1
                        reader.Read();
                        source.Description = reader.Value;
                        break;

                    case "metadata":     // WML 1.0/1.1
                        source.ISOMetadata = ReadISOMetadata(reader);
                        break;

                    case "contactinformation":     // WML 1.0/1.1. Note: WML 1.1 supports many "ContactInformation" elements
                        var contact = ReadContactInformtaion(reader);

                        source.ContactName = contact.ContactName;
                        source.Email       = contact.Email;
                        source.Phone       = contact.Phone;

                        // Convert WaterML address into HD address
                        if (!String.IsNullOrEmpty(contact.Address))
                        {
                            //Complete address: {Address},{City},{State},{ZipCode}
                            var split = contact.Address.Split(new[] { ',' },
                                                              StringSplitOptions.RemoveEmptyEntries);
                            if (split.Length > 0)
                            {
                                source.Address = split[0].Trim();
                            }
                            if (split.Length > 1)
                            {
                                source.City = split[1].Trim();
                            }
                            if (split.Length > 2)
                            {
                                source.State = split[2].Trim();
                            }
                            if (split.Length > 3)
                            {
                                int zipCode;
                                if (Int32.TryParse(split[3].Trim(), out zipCode))
                                {
                                    source.ZipCode = zipCode;
                                }
                            }
                        }
                        break;

                    case "sourcelink":      // WML 1.0/1.1.  Note: WML 1.1 supports many "SourceLinks" elements
                        reader.Read();
                        source.Link = reader.Value;
                        break;

                    case "sourcecode":     // WML 1.1
                        reader.Read();
                        break;

                    case "citation":     // WML 1.1
                        reader.Read();
                        source.Citation = reader.Value;
                        break;
                    }
                }
                else if (reader.NodeType == XmlNodeType.EndElement && nodeName == "source")
                {
                    break;
                }
            }

            return(source);
        }
Пример #2
0
        /// <summary>
        /// Read the list of series from the XML that is returned by HIS Central
        /// </summary>
        /// <param name="reader">the xml reader</param>
        /// <returns>the list of intermediate 'SeriesDataCart' objects</returns>
        private BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart ReadSeriesFromHISCentral(XmlReader reader)
        {
            var series = new BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart();

            while (reader.Read())
            {
                var nodeName = reader.Name.ToLower();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (XmlContext.AdvanceReaderPastEmptyElement(reader))
                    {
                        //Empty element - advance and continue...
                        continue;
                    }

                    switch (nodeName)
                    {
                    case "servcode":
                        reader.Read();
                        series.ServCode = reader.Value;
                        break;

                    case "servurl":
                        reader.Read();
                        series.ServURL = reader.Value;
                        break;

                    case "location":
                        reader.Read();
                        series.SiteCode = reader.Value;
                        break;

                    case "varcode":
                        reader.Read();
                        series.VariableCode = reader.Value;
                        break;

                    case "varname":
                        reader.Read();
                        series.VariableName = reader.Value;
                        break;

                    case "begindate":
                        reader.Read();
                        if (!String.IsNullOrWhiteSpace(reader.Value))
                        {
                            series.BeginDate = Convert.ToDateTime(reader.Value, _invariantCulture);
                        }
                        else
                        {
                            return(null);
                        }
                        break;

                    case "enddate":
                        reader.Read();
                        if (!String.IsNullOrWhiteSpace(reader.Value))
                        {
                            series.EndDate = Convert.ToDateTime(reader.Value, _invariantCulture);
                        }
                        else
                        {
                            return(null);
                        }
                        break;

                    case "valuecount":
                        reader.Read();
                        if (!String.IsNullOrWhiteSpace(reader.Value))
                        {
                            series.ValueCount = Convert.ToInt32(reader.Value);
                        }
                        else
                        {
                            return(null);
                        }
                        break;

                    case "sitename":
                        reader.Read();
                        series.SiteName = reader.Value;
                        break;

                    case "latitude":
                        reader.Read();
                        if (!String.IsNullOrWhiteSpace(reader.Value))
                        {
                            series.Latitude = Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            return(null);
                        }
                        break;

                    case "longitude":
                        reader.Read();
                        if (!String.IsNullOrWhiteSpace(reader.Value))
                        {
                            series.Longitude = Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            return(null);
                        }
                        break;

                    case "datatype":
                        reader.Read();
                        series.DataType = reader.Value;
                        break;

                    case "valuetype":
                        reader.Read();
                        series.ValueType = reader.Value;
                        break;

                    case "samplemedium":
                        reader.Read();
                        series.SampleMedium = reader.Value;
                        break;

                    case "timeunits":
                        reader.Read();
                        series.TimeUnit = reader.Value;
                        break;

                    case "conceptkeyword":
                        reader.Read();
                        series.ConceptKeyword = reader.Value;
                        break;

                    case "gencategory":
                        reader.Read();
                        series.GeneralCategory = reader.Value;
                        break;

                    case "timesupport":
                        reader.Read();
                        if (!String.IsNullOrWhiteSpace(reader.Value))
                        {
                            series.TimeSupport = Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture);
                        }
                        break;

                    case "isregular":
                        reader.Read();
                        if (!String.IsNullOrWhiteSpace(reader.Value))
                        {
                            series.IsRegular = Convert.ToBoolean(reader.Value);
                        }
                        break;

                    case "variableunitsabbrev":
                        reader.Read();
                        series.VariableUnits = reader.Value;
                        break;

                    //BCC - 07-Sep-2016 - Added additional reads for use with GetSeriesCatalogForBox3...
                    case "qclid":
                        reader.Read();
                        series.QCLID = reader.Value;
                        break;

                    case "qcldesc":
                        reader.Read();
                        series.QCLDesc = reader.Value;
                        break;

                    case "sourceorg":
                        reader.Read();
                        series.SourceOrg = reader.Value;
                        break;

                    case "sourceid":
                        reader.Read();
                        series.SourceId = reader.Value;
                        break;

                    case "methodid":
                        reader.Read();
                        series.MethodId = reader.Value;
                        break;

                    case "methoddesc":
                        reader.Read();
                        series.MethodDesc = reader.Value;
                        break;
                    }
                }
                else if (reader.NodeType == XmlNodeType.EndElement && (nodeName == "seriesrecord" || nodeName == "seriesrecordfull"))
                {
                    return(series);
                }
            }

            return(null);
        }
Пример #3
0
        protected virtual Site ReadSite(XmlReader r)
        {
            var site = new Site();

            while (r.Read())
            {
                var nodeName = r.Name.ToLower();

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

                    if (nodeName == "sitename")
                    {
                        r.Read();
                        site.Name = r.Value;
                    }
                    else if (nodeName == "geolocation")
                    {
                        ReadSpatialReference(r, site);
                    }
                    else if (nodeName.IndexOf("sitecode", StringComparison.Ordinal) >= 0)
                    {
                        string networkPrefix = r.GetAttribute("network");

                        //BCC - 08-Aug-2016 - Retrieve siteID attribute value...
                        string siteID = r.GetAttribute("siteID");
                        long   result = 0;
                        if (long.TryParse(siteID, out result))
                        {
                            site.Id = result;
                        }

                        r.Read();
                        string siteCode = r.Value;
                        if (!String.IsNullOrEmpty(networkPrefix))
                        {
                            siteCode = networkPrefix + ":" + siteCode;
                        }
                        site.Code          = siteCode;
                        site.NetworkPrefix = networkPrefix;
                    }
                    else if (nodeName == "verticaldatum")
                    {
                        r.Read();
                        site.VerticalDatum = r.Value;
                    }
                    else if (nodeName == "timezoneinfo")
                    {
                        site.DefaultTimeZone = ReadTimeZoneInfo(r);
                    }
                    else if (nodeName == "elevation_m")
                    {
                        r.Read();
                        site.Elevation_m = Convert.ToDouble(r.Value, CultureInfo.InvariantCulture);
                    }
                    // WaterML 1.0 notes
                    else if (nodeName == "note")
                    {
                        var title = r.GetAttribute("title");
                        if (!String.IsNullOrEmpty(title))
                        {
                            title = title.ToLower();
                            r.Read();
                            var value = r.Value;
                            switch (title)
                            {
                            case "county":
                                site.County = value;
                                break;

                            case "state":
                                site.State = value;
                                break;

                            case "comments":
                                site.Comments = value;
                                break;
                            }
                        }
                    }
                    // WaterML 1.1 site properties
                    else if (nodeName == "siteproperty")
                    {
                        var title = r.GetAttribute("name");
                        if (!String.IsNullOrEmpty(title))
                        {
                            title = title.ToLower();
                            r.Read();
                            var value = r.Value;
                            switch (title)
                            {
                            case "county":
                                site.County = value;
                                break;

                            case "state":
                                site.State = value;
                                break;

                            case "comments":
                                site.Comments = value;
                                break;

                            case "sitetype":
                                site.SiteType = value;
                                break;

                            case "country":
                                site.Country = value;
                                break;

                            case "posaccuracy_m":
                                site.PosAccuracy_m = Convert.ToDouble(value, CultureInfo.InvariantCulture);
                                break;
                            }
                        }
                    }
                }
                else if (r.NodeType == XmlNodeType.EndElement &&
                         (nodeName.StartsWith("source") || nodeName.StartsWith("siteinfo")))
                {
                    //ensure that spatial reference is set
                    if (site.SpatialReference == null)
                    {
                        site.SpatialReference = new SpatialReference {
                            SRSID = 0, SRSName = "unknown"
                        };
                    }

                    return(site);
                }
            }
            return(null);
        }
Пример #4
0
        protected virtual void ReadSpatialReference(XmlReader r, Site site)
        {
            while (r.Read())
            {
                //lat long datum (srs)
                if (r.NodeType == XmlNodeType.Element && r.Name == "geogLocation")
                {
                    if (r.HasAttributes)
                    {
                        site.SpatialReference = new SpatialReference();
                        string srsName = r.GetAttribute("srs");
                        if (String.IsNullOrEmpty(srsName))
                        {
                            srsName = "unknown";
                        }
                        site.SpatialReference.SRSName = srsName;
                    }
                }

                //local projection
                if (r.NodeType == XmlNodeType.Element && r.Name == "localSiteXY" && r.HasAttributes)
                {
                    site.LocalProjection         = new SpatialReference();
                    site.LocalProjection.SRSName = r.GetAttribute("projectionInformation");
                }

                if (XmlContext.AdvanceReaderPastEmptyElement(r))
                {
                    //Empty element - advance and continue...
                    continue;
                }

                //latitude
                if (r.NodeType == XmlNodeType.Element && r.Name == "latitude")
                {
                    r.Read();
                    site.Latitude = r.ReadContentAsDouble();
                }

                //longitude
                if (r.NodeType == XmlNodeType.Element && r.Name == "longitude")
                {
                    r.Read();
                    site.Longitude = r.ReadContentAsDouble();
                }

                //local projection
                if (r.NodeType == XmlNodeType.Element && r.Name == "localSiteXY" && r.HasAttributes)
                {
                    site.LocalProjection         = new SpatialReference();
                    site.LocalProjection.SRSName = r.GetAttribute("projectionInformation");
                }

                if (r.NodeType == XmlNodeType.Element && r.Name == "X")
                {
                    r.Read();
                    site.LocalX = r.ReadContentAsDouble();
                }

                if (r.NodeType == XmlNodeType.Element && r.Name == "Y")
                {
                    r.Read();
                    site.LocalY = r.ReadContentAsDouble();
                }

                if (r.NodeType == XmlNodeType.EndElement && r.Name == "geoLocation")
                {
                    return;
                }
            }
        }
Пример #5
0
        protected virtual SeriesMetadata ReadSeriesFromSiteInfo(XmlReader r, Site site)
        {
            var series = new SeriesMetadata {
                Site = site
            };

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

                    string nodeName = r.Name.ToLower();
                    if (nodeName == "variable")
                    {
                        series.Variable = ReadVariable(r);
                    }
                    else if (nodeName == "valuecount")
                    {
                        r.Read();
                        series.ValueCount = Convert.ToInt32(r.Value);
                    }
                    else if (nodeName == "begindatetime")
                    {
                        r.Read();
                        series.BeginDateTime    = Convert.ToDateTime(r.Value, CultureInfo.InvariantCulture);
                        series.BeginDateTimeUTC = series.BeginDateTime;
                    }
                    else if (nodeName == "enddatetime")
                    {
                        r.Read();
                        series.EndDateTime    = Convert.ToDateTime(r.Value, CultureInfo.InvariantCulture);
                        series.EndDateTimeUTC = series.EndDateTime;
                    }
                    else if (nodeName == "begindatetimeutc")
                    {
                        r.Read();
                        series.BeginDateTimeUTC = Convert.ToDateTime(r.Value, CultureInfo.InvariantCulture);
                    }
                    else if (nodeName == "enddatetimeutc")
                    {
                        r.Read();
                        series.EndDateTimeUTC = Convert.ToDateTime(r.Value, CultureInfo.InvariantCulture);
                    }
                    else if (nodeName == "method")
                    {
                        series.Method = ReadMethod(r);
                    }
                    else if (nodeName == "source")
                    {
                        series.Source = ReadSource(r);
                    }
                    else if (nodeName == "qualitycontrollevel")
                    {
                        series.QualityControlLevel = ReadQualityControlLevel(r);
                    }
                }
                else
                {
                    if (r.NodeType == XmlNodeType.EndElement && r.Name == "series")
                    {
                        return(series);
                    }
                }
            }
            return(series);
        }
Пример #6
0
        private IList <Series> ReadValues(XmlReader reader)
        {
            Site           site       = null;
            Source         source     = null;
            Variable       varInfo    = null;
            IList <Series> seriesList = null;

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

                    string readerName = reader.Name.ToLower();

                    if (readerName == "queryinfo")
                    {
                        //Read the 'Query Info'
                        //var qry = ReadQueryInfo(reader);
                        //xmlFileInfo.QueryInfo = qry;
                    }
                    else if (readerName == "sourceinfo")//sourceinfo does contains site information but does not contain source values. Source is separate tag in response
                    {
                        //Read the site information
                        site = ReadSite(reader);
                    }
                    else if (readerName == "variable")
                    {
                        //Read the variable information
                        varInfo = ReadVariable(reader);
                    }
                    else if (readerName == "values")
                    {
                        //Read the time series and data values information
                        seriesList = ReadDataValues(reader);
                        foreach (var series in seriesList)
                        {
                            if (varInfo != null)
                            {
                                series.Variable = varInfo;
                            }
                            if (source != null)
                            {
                                series.Source = source;
                            }
                            if (site != null)
                            {
                                series.Site = site;
                            }

                            //set the checked and creation date time
                            series.CreationDateTime    = DateTime.Now;
                            series.LastCheckedDateTime = DateTime.Now;
                            series.UpdateDateTime      = series.LastCheckedDateTime;
                        }
                    }
                }
            }

            return(seriesList ?? (new List <Series>(0)));
        }
Пример #7
0
        /// <summary>
        /// Reads information about variable
        /// </summary>
        protected override Variable ReadVariable(XmlReader r)
        {
            Variable varInfo = new Variable();

            //Unit timeUnit = Unit.Unknown;
            //Unit valueUnit = Unit.Unknown;
            varInfo.Speciation      = "Not Applicable";
            varInfo.DataType        = "Unknown";
            varInfo.GeneralCategory = "Unknown";
            varInfo.SampleMedium    = "Unknown";
            varInfo.ValueType       = "Unknown";

            while (r.Read())
            {
                string nodeName = r.Name.ToLower();

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

                    if (nodeName.IndexOf("variablecode") >= 0)
                    {
                        string prefix = r.GetAttribute("vocabulary");
                        if (string.IsNullOrEmpty(prefix))
                        {
                            prefix = r.GetAttribute("network");
                        }

                        //BCC - 08-Aug-2016 - Retrieve variableID attribute value
                        string variableID = r.GetAttribute("variableID");
                        long   result     = 0;
                        if (long.TryParse(variableID, out result))
                        {
                            varInfo.Id = result;
                        }

                        r.Read();
                        string variableCode = r.Value;
                        if (!String.IsNullOrEmpty(prefix))
                        {
                            variableCode             = prefix + ":" + variableCode;
                            varInfo.VocabularyPrefix = prefix;
                        }
                        varInfo.Code = variableCode;
                    }
                    else if (nodeName == "variablename")
                    {
                        r.Read();
                        varInfo.Name = r.Value;
                    }
                    else if (nodeName.IndexOf("valuetype") >= 0)
                    {
                        r.Read();
                        varInfo.ValueType = r.Value;
                    }
                    else if (nodeName.IndexOf("datatype") >= 0)
                    {
                        r.Read();
                        varInfo.DataType = r.Value;
                    }
                    else if (nodeName == "generalcategory")
                    {
                        r.Read();
                        varInfo.GeneralCategory = r.Value;
                    }
                    else if (nodeName == "samplemedium")
                    {
                        r.Read();
                        varInfo.SampleMedium = r.Value;
                    }
                    else if (nodeName == "speciation")
                    {
                        r.Read();
                        varInfo.Speciation = r.Value;
                    }
                    else if (nodeName == "unit")
                    {
                        Unit u = ReadUnit(r);
                        if (u.UnitsType.ToLower() == "time")
                        {
                            varInfo.TimeUnit = u;
                        }
                        else
                        {
                            varInfo.VariableUnit = u;
                        }
                    }
                    else if (nodeName == "units")
                    {
                        string unitAbbreviation = r.GetAttribute("unitsAbbreviation");
                        if (!String.IsNullOrEmpty(unitAbbreviation))
                        {
                            Unit u = Unit.Unknown;
                            u.Abbreviation       = unitAbbreviation;
                            u.Name               = unitAbbreviation;
                            varInfo.VariableUnit = u;
                            varInfo.TimeUnit     = Unit.UnknownTimeUnit;
                        }
                        else
                        {
                            varInfo.VariableUnit = Unit.Unknown;
                            varInfo.TimeUnit     = Unit.UnknownTimeUnit;
                        }
                    }

                    else if (nodeName == "nodatavalue")
                    {
                        r.Read();
                        varInfo.NoDataValue = Convert.ToDouble(r.Value, CultureInfo.InvariantCulture);
                    }
                    else if (nodeName == "timescale") //some versions of WaterML 1.1 use 'timescale'
                    {
                        string isRegular = r.GetAttribute("isRegular");
                        if (!String.IsNullOrEmpty(isRegular))
                        {
                            varInfo.IsRegular = Convert.ToBoolean(isRegular);
                        }
                    }
                    else if (nodeName == "timesupport") //other versions use 'timesupport'
                    {
                        r.Read();
                        try
                        {
                            varInfo.TimeSupport = Convert.ToSingle(r.Value);
                        }
                        catch
                        { }
                    }
                    else if (nodeName == "timeinterval")
                    {
                        r.Read();
                        varInfo.TimeSupport = Convert.ToSingle(r.Value);
                    }
                }
                else if (r.NodeType == XmlNodeType.EndElement && nodeName == "variable")
                {
                    return(varInfo);
                }
            }
            return(varInfo);
        }
Пример #8
0
        /// <summary>
        /// Reads information about a sample
        /// </summary>
        private static void ReadSample(XmlReader r, IDictionary <string, Sample> samples, Dictionary <string, LabMethod> labMethods)
        {
            Sample sample = Sample.Unknown;

            sample.LabMethod = LabMethod.Unknown;

            var newLabMethod = LabMethod.Unknown;

            string labSampleCode = String.Empty;

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

                    string nodeName = r.Name.ToLower();
                    if (nodeName == "labsamplecode")
                    {
                        r.Read();
                        labSampleCode        = r.Value;
                        sample.LabSampleCode = labSampleCode;
                    }
                    else if (nodeName == "sampletype")
                    {
                        r.Read();
                        sample.SampleType = r.Value;
                    }
                    else if (nodeName == "labname")
                    {
                        r.Read();
                        newLabMethod.LabName = r.Value;
                    }
                    else if (nodeName == "laborganization")
                    {
                        r.Read();
                        newLabMethod.LabOrganization = r.Value;
                    }
                    else if (nodeName == "labmethodname")
                    {
                        r.Read();
                        newLabMethod.LabMethodName = r.Value;
                    }
                }
                else if (r.NodeType == XmlNodeType.EndElement && r.Name.ToLower() == "sample")
                {
                    //assign the lab method
                    string labMethodKey = newLabMethod.LabName + "|" + newLabMethod.LabMethodName;
                    if (!labMethods.ContainsKey(labMethodKey))
                    {
                        labMethods.Add(labMethodKey, newLabMethod);
                        sample.LabMethod = newLabMethod;
                    }
                    else
                    {
                        sample.LabMethod = null;
                        sample.LabMethod = labMethods[labMethodKey];
                    }

                    //check existing sample
                    if (samples.ContainsKey(labSampleCode))
                    {
                        samples[labSampleCode] = null;
                        samples[labSampleCode] = sample;
                    }
                    return;
                }
            }
        }
Пример #9
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());
        }
Пример #10
0
        private List <WebServiceNode> getWebserviceNodeList(string xmlData)
        {
            var list = new List <WebServiceNode>();

            var xmlReaderSettings = new XmlReaderSettings
            {
                CloseInput       = true,
                IgnoreComments   = true,
                IgnoreWhitespace = true,
            };

            using (var reader = XmlReader.Create(new StringReader(xmlData), xmlReaderSettings))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (XmlContext.AdvanceReaderPastEmptyElement(reader))
                        {
                            //Empty element - advance and continue...
                            continue;
                        }

                        if (reader.Name == "ServiceInfo")
                        {
                            string desciptionUrl = null;
                            string serviceUrl    = null;
                            string title         = null;
                            int    serviceID     = -1;
                            string serviceCode   = null;
                            string organization  = null;

                            int    variables = -1, values = -1, sites = -1;
                            double xmin = double.NaN, xmax = double.NaN, ymin = double.NaN, ymax = double.NaN;

                            while (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "ServiceInfo")
                                {
                                    break;
                                }

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

                                    switch (reader.Name)
                                    {
                                    case "Title":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        title = reader.Value.Trim();
                                        break;

                                    case "ServiceID":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        serviceID = Convert.ToInt32(reader.Value.Trim());
                                        break;

                                    case "ServiceDescriptionURL":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        desciptionUrl = reader.Value.Trim();
                                        break;

                                    case "organization":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        organization = reader.Value.Trim();
                                        break;

                                    case "servURL":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        serviceUrl = reader.Value.Trim();
                                        break;

                                    case "valuecount":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        values = Convert.ToInt32(reader.Value.Trim());
                                        break;

                                    case "variablecount":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        variables = Convert.ToInt32(reader.Value.Trim());
                                        break;

                                    case "sitecount":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        sites = Convert.ToInt32(reader.Value.Trim());
                                        break;

                                    case "NetworkName":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        serviceCode = reader.Value.Trim();
                                        break;

                                    case "minx":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        double.TryParse(reader.Value.Trim(), NumberStyles.Number, CultureInfo.InvariantCulture,
                                                        out xmin);
                                        break;

                                    case "maxx":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        double.TryParse(reader.Value.Trim(), NumberStyles.Number, CultureInfo.InvariantCulture,
                                                        out xmax);
                                        break;

                                    case "miny":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        double.TryParse(reader.Value.Trim(), NumberStyles.Number, CultureInfo.InvariantCulture,
                                                        out ymin);
                                        break;

                                    case "maxy":
                                        if (!reader.Read())
                                        {
                                            continue;
                                        }
                                        double.TryParse(reader.Value.Trim(), NumberStyles.Number, CultureInfo.InvariantCulture,
                                                        out ymax);
                                        break;
                                    }
                                }
                            }

                            var boundingBox = (Box)null;
                            if (!double.IsNaN(xmin) && !double.IsNaN(xmax) && !double.IsNaN(ymin) && !double.IsNaN(ymax))
                            {
                                boundingBox = new Box(xmin, xmax, ymin, ymax);
                            }

                            var node = new WebServiceNode(title, serviceCode, serviceID, desciptionUrl, serviceUrl, boundingBox, organization, sites, variables, values);
                            list.Add(node);
                        }
                    }
                }
            }

            return(list);
        }
Пример #11
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 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;
                            }
                            //fix by Jiri - sometimes the dateTime attribute is uppercase
                            var localDateTime = r.GetAttribute("dateTime");
                            if (string.IsNullOrEmpty(localDateTime))
                            {
                                localDateTime = r.GetAttribute("DateTime");
                            }
                            val.LocalDateTime = Convert.ToDateTime(localDateTime, CultureInfo.InvariantCulture);
                            val.DateTimeUTC   = val.LocalDateTime;
                            val.UTCOffset     = 0.0;

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

                            //quality control level
                            string qualityCode = r.GetAttribute("qualityControlLevel");
                            if (String.IsNullOrEmpty(qualityCode))
                            {
                                qualityCode = "unknown"; //when the quality control level is unspecified
                            }
                            if (!qualityControlLevels.ContainsKey(qualityCode))
                            {
                                var qualControl = QualityControlLevel.Unknown;
                                qualControl.Code        = qualityCode;
                                qualControl.Definition  = qualityCode;
                                qualControl.Explanation = qualityCode;
                                qualityControlLevels.Add(qualityCode, qualControl);
                            }

                            //source
                            var sourceID = r.GetAttribute("sourceID");
                            if (String.IsNullOrEmpty(sourceID))
                            {
                                sourceID = "0"; //when a source ID 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
                            var sampleID = r.GetAttribute("sampleID");
                            if (!String.IsNullOrEmpty(sampleID))
                            {
                                if (!samples.ContainsKey(sampleID))
                                {
                                    samples.Add(sampleID, Sample.Unknown);
                                }
                            }
                            wrapper.SampleID = sampleID;

                            //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
                            var offsetID = r.GetAttribute("offsetTypeID");
                            if (!String.IsNullOrEmpty(offsetID))
                            {
                                if (!offsets.ContainsKey(offsetID))
                                {
                                    offsets.Add(offsetID, new OffsetType());
                                }
                                var offsetValue = r.GetAttribute("offsetValue");
                                if (!String.IsNullOrEmpty(offsetValue))
                                {
                                    val.OffsetValue = Convert.ToDouble(offsetValue, CultureInfo.InvariantCulture);
                                }
                            }
                            wrapper.OffsetID = offsetID;

                            //data value
                            val.Value = Convert.ToDouble(r.ReadString(), CultureInfo.InvariantCulture);
                        }
                    }
                    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")
                    {
                        //quality control level seems to be included with each value
                    }
                    else if (r.Name == "qualifier")
                    {
                        ReadQualifier(r, qualifiers);
                    }
                    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))
                {
                    var newSeries = new Series();
                    seriesDictionary.Add(seriesCode, newSeries);
                    //assign method, source and qual.control level
                    //assign method, source and qual.control level
                    try
                    {
                        newSeries.Method = methods[SeriesCodeHelper.GetMethodCode(seriesCode)];
                        //fix by Jiri: fixes the case when sourceID is unspecified sources
                        //has more than one source entry
                        string sourceCode = SeriesCodeHelper.GetSourceCode(seriesCode);
                        if (sourceCode == "0" && sources.Count > 0)
                        {
                            foreach (string sc in sources.Keys)
                            {
                                if (sc != "0")
                                {
                                    sourceCode = sc;
                                    break;
                                }
                            }
                        }
                        newSeries.Source = sources[sourceCode];
                        newSeries.QualityControlLevel = qualityControlLevels[SeriesCodeHelper.GetQualityCode(seriesCode)];
                    }
                    catch { }
                }

                //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];
                }
            }

            //to check the qualifiers
            CheckQualifiers(qualifiers);
            return(seriesDictionary.Values.ToList());
        }