This provides a concrete implementation of ITimeSeriesTrace for use internal to TimeSeriesLibrary.
Inheritance: ITimeSeriesTrace
Example #1
0
        /// <summary>
        /// This method prepares a new record for the trace table for a regular time step series.
        /// The method converts the given valueArray into the BLOB that is actually stored in
        /// the table.  The method computes the checksum of the trace, and computes a new checksum
        /// for the parameters table to reflect the fact that a new trace has been added to the ensemble.
        /// For both the insertion to the trace table and the update to the parameters table, this method
        /// only stores changes in DataTable objects--nothing is changed in the database. In order for
        /// the changes to be sent to the database, the method TSConnection.CommitNewTraceWrites must
        /// be called after WriteTraceRegular has been called for all new traces.
        /// </summary>
        /// <param name="id">identifying primary key value of the the parameters table for the record
        /// that this trace belongs to</param>
        /// <param name="doWriteToDB">true if the method should actually save the timeseries to the database</param>
        /// <param name="tsImport">TSImport object into which the method will record values that it has computed.
        /// If this parameter is null, then the method will skip the recording of such paramters to an object.</param>
        /// <param name="traceNumber">number of the trace to write</param>
        /// <param name="valueArray">The array of values to be written to the database</param>
        public unsafe void WriteTraceRegular(int id,
                                             bool doWriteToDB, TSImport tsImport,
                                             int traceNumber,
                                             double[] valueArray)
        {
            // Initialize class fields other than the BLOB of data values
            if (!IsInitialized)
            {
                Initialize(id, true);
            }

            // This method can only process regular-time-step series
            if (TimeStepUnit == TSDateCalculator.TimeStepUnitCode.Irregular)
            {
                throw new TSLibraryException(ErrCode.Enum.Record_Not_Regular,
                                             String.Format("The method can only process regular time series, but" +
                                                           "the record with Id {0} is irregular.", id));
            }
            // Create a trace object
            int timeStepCount            = valueArray.Count();
            ITimeSeriesTrace traceObject = new TSTrace
            {
                TraceNumber   = traceNumber,
                TimeStepCount = timeStepCount,
                EndDate       = TSDateCalculator.IncrementDate(BlobStartDate,
                                                               TimeStepUnit, TimeStepQuantity, timeStepCount - 1)
            };

            if (tsImport != null)
            {
                tsImport.TraceList.Add(traceObject);
            }
            else
            {
                TraceList.Add(traceObject);
            }
            // Convert the array of double values into a byte array...a BLOB
            TSBlobCoder.ConvertArrayToBlobRegular(valueArray, CompressionCode, traceObject);

            // Create a new record for the trace table
            // (but for now it is only stored in a DataTable object)
            if (doWriteToDB)
            {
                WriteTrace(traceObject);
            }
            // Compute a new checksum for the parameters table
            // (but for now it is only stored in a DataTable object)
            UpdateParametersChecksum(doWriteToDB, tsImport);
        }
        void ChecksumTimer()
        {
            int i;

            var ts = new TSParameters
            {
                BlobStartDate = DateTime.Parse("1/1/2000"),
                CompressionCode = TSBlobCoder.currentCompressionCode,
                TimeStepQuantity = 2,
                TimeStepUnit = TSDateCalculator.TimeStepUnitCode.Day
            };
            var traces = new List<ITimeSeriesTrace>();

            for (int traceIndex = 0; traceIndex < 80; traceIndex++)
            {
                var iterList = new List<double>();
                for (int stepIndex = 0; stepIndex < nVals; stepIndex++)
                {
                    iterList.Add(1.5 * stepIndex + 0.3373);
                }
                var trace = new TSTrace { TraceNumber = traceIndex + 1 };
                trace.ValueBlob = TSBlobCoder.ConvertArrayToBlobRegular(iterList.ToArray(),
                                            TSBlobCoder.currentCompressionCode, trace);
                //trace.Checksum = new Guid().ToByteArray();
                traces.Add(trace);
            }

            // Start the timer
            DateTime timerStart = DateTime.Now;

            // Create dummy time series that we can write to the database
            for (i = 0; i < nIter; i++)
            {
                foreach(var trace in traces)
                {
                    TSBlobCoder.ComputeTraceChecksum(trace.TraceNumber, trace.ValueBlob);
                }
                TSBlobCoder.ComputeChecksum(ts, traces);
            }

            // Stop the timer
            DateTime timerEnd = DateTime.Now;
            TimeSpan timerDiff = timerEnd - timerStart;
            TimeLabelBlob.Content = String.Format("BLOBWRI --- Iterations: {0};  Duration: {1:hh\\:mm\\:ss\\.f}", i, timerDiff);
        }
        public void ChecksumDiffTest1()
        {
            List<TimeSeriesValue> duplicateList = new List<TimeSeriesValue>();

            // Create an identical array by deep copy
            foreach (TimeSeriesValue tsv in IrregList1)
            {
                duplicateList.Add(new TimeSeriesValue { Date = tsv.Date, Value = tsv.Value });
            }
            // Slightly alter one date in the list
            duplicateList[5].Date = duplicateList[5].Date.AddMinutes(12);

            TSTrace trace1 = new TSTrace { TraceNumber = 1 };
            TSTrace trace2 = new TSTrace { TraceNumber = 1 };

            Boolean ret = ComputeTestChecksums(
                    TSDateCalculator.TimeStepUnitCode.Irregular, 0, IrregList1, trace1,
                    TSDateCalculator.TimeStepUnitCode.Irregular, 0, duplicateList, trace2);

            Assert.IsFalse(ret);
        }
        public List<ITimeSeriesTrace> Get3TracesRegular(
                    TSDateCalculator.TimeStepUnitCode u1, short q1, int c1,
                    DateTime sDate1, out DateTime eDate1,
                    Boolean shouldPerturb = false,
                    int perturbTraceIndex = 0, int perturbStep = 0, 
                    double perturbVal = 0, double perturbMinutes = 0)
        {
            eDate1 = DateTime.Now;  // dummy
            TSLibrary tsLib = new TSLibrary();
            List<ITimeSeriesTrace> traceList = new List<ITimeSeriesTrace>();
            int compressionCode;

            for (int t = 0; t < 3; t++)
            {
                TSTrace trace1 = new TSTrace { TraceNumber = t + 1 };
                List<TimeSeriesValue> tsValues = new List<TimeSeriesValue>();
                DateTime curDate = sDate1;
                Double curVal = 20;
                for (int i = 0; i < c1; i++)
                {
                    tsValues.Add(new TimeSeriesValue { Date = curDate, Value = curVal });
                    curDate = tsLib.IncrementDate(curDate, u1, q1, 1);
                    curVal = curVal + i / (t + 1);
                }
                // make a perturbation if called for
                if (shouldPerturb && t==perturbTraceIndex)
                {
                    tsValues[perturbStep].Value += perturbVal;
                    tsValues[perturbStep].Date = tsValues[perturbStep].Date.AddMinutes(perturbMinutes);
                }
                eDate1 = tsValues.Last().Date;
                tsLib.ConvertListToBlobWithChecksum(
                        u1, q1, c1,
                        sDate1, eDate1, tsValues, trace1, out compressionCode);

                traceList.Add(trace1);
            }
            return traceList;
        }
        public void ConvertListToBlobWithChecksum_Err4()
        {
            TSLibrary tsLib = new TSLibrary();
            TSTrace traceObject = new TSTrace { TraceNumber = 1 };
            int compressionCode;

            try
            {
                byte[] blobData = tsLib.ConvertListToBlobWithChecksum(
                        TSDateCalculator.TimeStepUnitCode.Irregular, 0,
                        IrregList1.Count, IrregList1.First().Date, IrregList1.Last().Date.AddDays(2),
                        IrregList1, traceObject, out compressionCode);
                Assert.Fail("Should have thrown exception");
            }
            catch (TSLibraryException e)
            {
                Assert.AreEqual(ErrCode.Enum.Checksum_Improper_EndDate, e.ErrCode);
            }
        }
        public void ConvertListToBlobWithChecksum_Err0()
        {
            TSLibrary tsLib = new TSLibrary();
            TSTrace traceObject = new TSTrace { TraceNumber = 1 };
            int compressionCode;

            try
            {
                byte[] blobData = tsLib.ConvertListToBlobWithChecksum(
                        TSDateCalculator.TimeStepUnitCode.Irregular, 0,
                        IrregList1.Count, IrregList1.First().Date, IrregList1.Last().Date,
                        IrregList1, traceObject, out compressionCode);
                Assert.IsTrue(true);
            }
            catch (TSLibraryException)
            {
                Assert.Fail("Should not throw any exceptions");
            }
        }
        public void ComputeChecksum_Err1()
        {
            TSLibrary tsLib = new TSLibrary();
            TSTrace traceObject = new TSTrace { TraceNumber = 1 };

            try
            {
                byte[] blobData = tsLib.ComputeChecksum(
                        TSDateCalculator.TimeStepUnitCode.Irregular, 3,
                        IrregList1.First().Date, new List<ITimeSeriesTrace>());
                Assert.Fail("Should have thrown exception");
            }
            catch (TSLibraryException e)
            {
                Assert.AreEqual(ErrCode.Enum.Checksum_Quantity_Nonzero, e.ErrCode);
            }
        }
        public void ChecksumIdenticalReg()
        {
            List<TimeSeriesValue> duplicateList = new List<TimeSeriesValue>();

            // Create an identical array by deep copy
            foreach (TimeSeriesValue tsv in IrregList1)
            {
                duplicateList.Add(new TimeSeriesValue { Date = tsv.Date, Value = tsv.Value });
            }
            TSTrace trace1 = new TSTrace { TraceNumber = 1 };
            TSTrace trace2 = new TSTrace { TraceNumber = 1 };

            Boolean ret = ComputeTestChecksums(
                    TSDateCalculator.TimeStepUnitCode.Irregular, 0, IrregList1, trace1,
                    TSDateCalculator.TimeStepUnitCode.Irregular, 0, duplicateList, trace2);

            Assert.IsTrue(ret);
        }
        public void ChecksumDiffTest4()
        {
            List<TimeSeriesValue> duplicateList = new List<TimeSeriesValue>();

            // Create an identical array by deep copy
            foreach (TimeSeriesValue tsv in IrregList1)
            {
                duplicateList.Add(new TimeSeriesValue { Date = tsv.Date, Value = tsv.Value });
            }
            // remove a value from the middle of the list
            duplicateList.RemoveAt(150);

            TSTrace trace1 = new TSTrace { TraceNumber = 1 };
            TSTrace trace2 = new TSTrace { TraceNumber = 1 };

            Boolean ret = ComputeTestChecksums(
                    TSDateCalculator.TimeStepUnitCode.Irregular, 0, IrregList1, trace1,
                    TSDateCalculator.TimeStepUnitCode.Irregular, 0, duplicateList, trace2);

            Assert.IsFalse(ret);
        }
        public void Test(Boolean inBulk, Boolean doParam)
        {
            // Create dummy time series that we can write to the database
            var valList = new List<double[]>();
            for (int i = 0; i < nIter; i++)
            {
                var iterList = new List<double>();
                for (int t = 0; t < nVals; t++)
                {
                    iterList.Add(1.5 * t + i + 0.33);
                }
                valList.Add(iterList.ToArray());
            }

            if (inBulk)
            {
                var traceObjects = new List<ITimeSeriesTrace>();
                for (int i = 0; i < nIter; i++)
                {
                    ITimeSeriesTrace traceObject = new TSTrace { TraceNumber = 1 };
                    // Convert the array of double values into a byte array...a BLOB
                    traceObject.ValueBlob = TSBlobCoder.ConvertArrayToBlobRegular
                                (valList[i], TSBlobCoder.currentCompressionCode, traceObject);
                    traceObjects.Add(traceObject);
                }
                WriteBulkTraces(traceObjects);
            }
            else if (doParam)
            {
                // reference: http://stackoverflow.com/questions/2449827/pros-and-cons-of-using-sqlcommand-prepare-in-c
                // this does appear to be faster!!

                SqlCommand cmd = new SqlCommand("INSERT INTO " + TableName
                                    + "(TimeSeries_Id, TraceNumber, ValueBlob, Checksum) "
                                    + "VALUES (@TimeSeries_Id, @TraceNumber, @ValueBlob, @Checksum)", Connx);
                cmd.Parameters.Add("@TimeSeries_Id", SqlDbType.Int);
                cmd.Parameters.Add("@TraceNumber", SqlDbType.Int);
                cmd.Parameters.Add("@ValueBlob", SqlDbType.VarBinary, -1);
                cmd.Parameters.Add("@Checksum", SqlDbType.Binary, 16);
                cmd.Prepare();

                SqlCommand selectCmd = new SqlCommand("SELECT  TraceNumber, Checksum from "
                            + TableName + " where TimeSeries_Id=@TimeSeries_Id order by TraceNumber", Connx);
                selectCmd.Parameters.Add("@TimeSeries_Id", SqlDbType.Int);
                selectCmd.Prepare();

                SqlCommand updateCmd = new SqlCommand("UPDATE " + "OutputTimeSeries"
                                        + " SET Checksum=@Checksum WHERE Id=@Id", Connx);
                updateCmd.Parameters.Add("@Checksum", SqlDbType.Binary, 16);
                updateCmd.Parameters.Add("@Id", SqlDbType.Int);
                updateCmd.Prepare();

                for (int i = 0; i < nIter; i++)
                {
                    ITimeSeriesTrace traceObject = new TSTrace { TraceNumber = 1 };
                    // Convert the array of double values into a byte array...a BLOB
                    traceObject.ValueBlob = TSBlobCoder.ConvertArrayToBlobRegular
                                (valList[i], TSBlobCoder.currentCompressionCode, traceObject);
                    WriteOneTraceParam(i + 1, traceObject, cmd);

                    // update the checksum in the parameters table
                    UpdateParametersChecksum(i + 1, selectCmd, updateCmd);
                }
            }
            else
            {
                for (int i = 0; i < nIter; i++)
                {
                    ITimeSeriesTrace traceObject = new TSTrace { TraceNumber = 1 };
                    // Convert the array of double values into a byte array...a BLOB
                    traceObject.ValueBlob = TSBlobCoder.ConvertArrayToBlobRegular
                                (valList[i], TSBlobCoder.currentCompressionCode, traceObject);
                    WriteOneTrace(traceObject);
                }
            }
        }