Esempio n. 1
0
        /// <summary>
        /// Method to generate a list of offset (from OD OffsetTypes table) in a ValuesDataSet
        /// This is done as a separate method since Values can could contain other VariableValue Types
        ///
        /// </summary>
        /// <param name="ds">ValuesDataSet with the values used in the timeSeries</param>
        /// <returns></returns>
        public static List <OffsetType> datasetOffsetTypes(ValuesDataSet ds)
        {
            /* generate a list
             * create a distinct DataSet
             * - new data view
             * - set filter (no nulls)
             * - use toTable with unique to get unique list
             * foreach to generate qualifiers
             * */
            string            COLUMN    = "OffsetTypeID";
            string            TABLENAME = "offsetTypes";
            List <OffsetType> list      = new List <OffsetType>();

            try
            {
                DataView view = new DataView(ds.DataValues);
                view.RowFilter = COLUMN + " is not Null";
                DataTable ids = view.ToTable(TABLENAME, true, new string[] { COLUMN });

                foreach (DataRow r in ids.Rows)
                {
                    try
                    {
                        Object aId = r[COLUMN];
                        // edit here
                        ValuesDataSet.OffsetTypesRow offset = ds.OffsetTypes.FindByOffsetTypeID((int)aId);
                        if (offset != null)
                        {
                            OffsetType t = new OffsetType();
                            t.offsetTypeID          = offset.OffsetTypeID;
                            t.offsetTypeIDSpecified = true;

                            if (!String.IsNullOrEmpty(offset.OffsetDescription))
                            {
                                t.offsetDescription = offset.OffsetDescription;
                            }

                            ValuesDataSet.UnitsRow offUnit = ds.Units.FindByUnitsID(offset.OffsetUnitsID);
                            string offUnitsCode;
                            string offUnitsName         = null;
                            string offUnitsAbbreviation = null;
                            if (!String.IsNullOrEmpty(offUnit.UnitsAbbreviation))
                            {
                                offUnitsAbbreviation = offUnit.UnitsAbbreviation;
                            }
                            if (!String.IsNullOrEmpty(offUnit.UnitsName))
                            {
                                offUnitsName = offUnit.UnitsName;
                            }
                            if (offUnit != null)
                            {
                                t.units = CuahsiBuilder.CreateUnitsElement(
                                    null, offUnit.UnitsID.ToString(), offUnitsAbbreviation, offUnitsName);
                            }

                            list.Add(t);
                        }
                    }
                    catch (Exception e)
                    {
                        log.Error("Error generating a qualifier " + r.ToString() + e.Message);
                    }
                }
                return(list);
            }

            catch (Exception e)
            {
                log.Error("Error generating a qualifiers " + e.Message);
                // non fatal exceptions
                return(null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// DataValue creation. Converts a ValuesDataSet to the XML schema ValueSingleVariable
        /// If variable is null, it will return all
        /// If variable has extra options (variable:code/Value=Key/Value=Key)
        ///
        /// </summary>
        /// <param name="ds">Dataset that you want converted</param>
        /// <param name="variable">Variable that you want to use to place limits on the returned data</param>
        /// <returns></returns>
        public static List <ValueSingleVariable> dataset2ValuesList(ValuesDataSet ds, VariableParam variable)
        {
            List <ValueSingleVariable> tsTypeList = new List <ValueSingleVariable>();


            /* logic
             * if there is a variable that has options, then get a set of datarows
             * using a select clause
             * use an enumerator, since it is generic
             * */
            IEnumerator dataValuesEnumerator;

            ValuesDataSet.DataValuesRow[] dvRows; // if we find options, we need to use this.

            String select = OdValuesCommon.CreateValuesWhereClause(variable, null);

            if (select.Length > 0)
            {
                dvRows = (ValuesDataSet.DataValuesRow[])ds.DataValues.Select(select.ToString());

                dataValuesEnumerator = dvRows.GetEnumerator();
            }
            else
            {
                dataValuesEnumerator = ds.DataValues.GetEnumerator();
            }

            //  foreach (ValuesDataSet.DataValuesRow aRow in ds.DataValues){
            while (dataValuesEnumerator.MoveNext())
            {
                ValuesDataSet.DataValuesRow aRow = (ValuesDataSet.DataValuesRow)dataValuesEnumerator.Current;
                try
                {
                    ValueSingleVariable tsTypeValue = new ValueSingleVariable();

                    #region DateTime Standard
                    tsTypeValue.dateTime = Convert.ToDateTime(aRow.DateTime);
                    //tsTypeValue.dateTimeSpecified = true;
                    DateTime temprealdate;



                    //<add key="returnUndefinedUTCorLocal" value="Undefined"/>
                    if (ConfigurationManager.AppSettings["returnUndefinedUTCorLocal"].Equals("Undefined"))
                    {
                        temprealdate = Convert.ToDateTime(aRow.DateTime); // not time zone shift
                    }
                    else if (ConfigurationManager.AppSettings["returnUndefinedUTCorLocal"].Equals("Local"))
                    {
                        TimeSpan zone   = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
                        Double   offset = Convert.ToDouble(aRow.UTCOffset);

                        if (zone.TotalHours.Equals(offset))
                        {
                            // zone is the same as server. Shift
                            temprealdate = DateTime.SpecifyKind(Convert.ToDateTime(aRow.DateTime), DateTimeKind.Local);
                        }
                        else
                        {
                            //// zone is not the same. Output in UTC.
                            //temprealdate = DateTime.SpecifyKind(Convert.ToDateTime(aRow.DateTime), DateTimeKind.Utc);
                            //// correct time with shift.
                            //temprealdate = temprealdate.AddHours(offset);

                            // just use the DateTime UTC
                            temprealdate = DateTime.SpecifyKind(Convert.ToDateTime(aRow.DateTimeUTC), DateTimeKind.Utc);
                        }
                    }
                    else if (ConfigurationManager.AppSettings["returnUndefinedUTCorLocal"].Equals("UTC"))
                    {
                        temprealdate = DateTime.SpecifyKind(Convert.ToDateTime(aRow.DateTimeUTC), DateTimeKind.Utc);
                    }
                    else
                    {
                        temprealdate = Convert.ToDateTime(aRow.DateTime); // not time zone shift
                    }

                    temprealdate = Convert.ToDateTime(aRow.DateTime); // not time zone shift
                    #endregion
                    #region DateTimeOffset Failed
                    /// using XML overrides
                    // Attemp to use DateTimeOffset in xml Schema
                    //TimeSpan zone = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
                    //Double offset = Convert.ToDouble(aRow.UTCOffset);

                    //DateTimeOffset temprealdate;
                    //temprealdate = new DateTimeOffset(Convert.ToDateTime(aRow.DateTime),
                    //    new TimeSpan(Convert.ToInt32(offset),0,0));
                    //DateTimeOffset temprealdate;
                    //temprealdate = new DateTimeOffset(Convert.ToDateTime(aRow.DateTime),
                    //    new TimeSpan(Convert.ToInt32(offset), 0, 0));

                    //tsTypeValue.dateTime = temprealdate;
                    //tsTypeValue.dateTimeSpecified = true;
                    #endregion

                    //tsTypeValue.censored = string.Empty;
                    if (string.IsNullOrEmpty(aRow.Value.ToString()))
                    {
                        continue;
                    }
                    else
                    {
                        tsTypeValue.Value = Convert.ToDecimal(aRow.Value);
                    }

                    try
                    {
                        tsTypeValue.censorCode          = (CensorCodeEnum)Enum.Parse(typeof(CensorCodeEnum), aRow.CensorCode, true);
                        tsTypeValue.censorCodeSpecified = true;

                        if (!aRow.IsOffsetTypeIDNull())
                        {
                            tsTypeValue.offsetTypeID          = aRow.OffsetTypeID;
                            tsTypeValue.offsetTypeIDSpecified = true;

                            // enabled to fix issue with hydroobjects
                            ValuesDataSet.OffsetTypesRow off = ds.OffsetTypes.FindByOffsetTypeID(aRow.OffsetTypeID);


                            ValuesDataSet.UnitsRow offUnit = ds.Units.FindByUnitsID(off.OffsetUnitsID);
                            tsTypeValue.offsetUnitsCode         = offUnit.UnitsID.ToString();
                            tsTypeValue.offsetUnitsAbbreviation = offUnit.UnitsAbbreviation;
                        }

                        // offset value may be separate from the units... anticpating changes for USGS
                        if (!aRow.IsOffsetValueNull())
                        {
                            tsTypeValue.offsetValue          = aRow.OffsetValue;
                            tsTypeValue.offsetValueSpecified = true;
                        }


                        ValuesDataSet.MethodsRow meth = ds.Methods.FindByMethodID(aRow.MethodID);
                        tsTypeValue.methodID          = aRow.MethodID;
                        tsTypeValue.methodIDSpecified = true;


                        if (!aRow.IsQualifierIDNull())
                        {
                            ValuesDataSet.QualifiersRow qual = ds.Qualifiers.FindByQualifierID(aRow.QualifierID);
                            if (qual != null)
                            {
                                tsTypeValue.qualifiers = qual.QualifierCode;
                            }
                        }


                        ValuesDataSet.QualityControlLevelsRow qcl =
                            ds.QualityControlLevels.FindByQualityControlLevelID(aRow.QualityControlLevelID);
                        string qlName = qcl.Definition.Replace(" ", "");

                        if (Enum.IsDefined(typeof(QualityControlLevelEnum), qlName))
                        {
                            tsTypeValue.qualityControlLevel = (QualityControlLevelEnum)
                                                              Enum.Parse(
                                typeof(QualityControlLevelEnum), qlName, true);
                            if (tsTypeValue.qualityControlLevel != QualityControlLevelEnum.Unknown)
                            {
                                tsTypeValue.qualityControlLevelSpecified = true;
                            }
                        }
                        //}
                        tsTypeValue.sourceID          = aRow.SourceID;
                        tsTypeValue.sourceIDSpecified = true;

                        if (!aRow.IsSampleIDNull())
                        {
                            tsTypeValue.sampleID          = aRow.SampleID;
                            tsTypeValue.sampleIDSpecified = true;
                        }
                    }
                    catch (Exception e)
                    {
                        log.Debug("Error generating a value " + e.Message);
                        // non fatal exceptions
                    }

                    tsTypeList.Add(tsTypeValue);
                }
                catch (Exception e)
                {
                    //  ignore any value errors
                }
            }
            return(tsTypeList);
        }