Exemplo n.º 1
0
        /// <summary>
        /// This method converts a List of TimeSeriesValue objects into a BLOB (byte array) of
        /// time series values and computes a checksum from the BLOB.  This method assigns the new values
        /// of the ValueBlob, Checksum, EndDate, and TimeStepCount to the object given in the traceObject
        /// parameter. The TraceNumber property of the traceObject parameter must be set before calling
        /// this method.
        ///
        /// The entire List is converted into the BLOB--i.e., the method does not take any
        /// parameters for limiting the size of the List that is created.  This method will
        /// throw exceptions if the meta-parameters that are passed in are not consistent
        /// with the List of TimeSeriesValue objects.
        /// </summary>
        /// <param name="timeStepUnit">TSDateCalculator.TimeStepUnitCode value for
        /// Minute, Hour, Day, Week, Month, Year, or Irregular</param>
        /// <param name="timeStepQuantity">The number of the given unit that defines the time step.
        /// For instance, if the time step is 6 hours long, then this value is 6.</param>
        /// <param name="timeStepCount">The number of time steps stored in the BLOB</param>
        /// <param name="blobStartDate">Date of the first time step in the BLOB</param>
        /// <param name="blobEndDate">Date of the last time step in the BLOB</param>
        /// <param name="dateValueList">A List of TimeSeriesValue objects that will be converted to a BLOB</param>
        /// <param name="traceObject">an object which contains the a TraceNumber property that is used to
        /// compute the checksum.  The computed BLOB and checksum are both saved to the appropriate properties
        /// of this object.</param>
        /// <param name="compressionCode">a generation number that indicates what compression technique to use</param>
        /// <returns>The BLOB (byte array) of time series values that was created from dateValueList</returns>
        public byte[] ConvertListToBlobWithChecksum(
            TSDateCalculator.TimeStepUnitCode timeStepUnit, short timeStepQuantity,
            int timeStepCount, DateTime blobStartDate, DateTime blobEndDate,
            List <TimeSeriesValue> dateValueList,
            ITimeSeriesTrace traceObject, out int compressionCode)
        {
            // Error checks
            if (dateValueList.Count != timeStepCount)
            {
                throw new TSLibraryException(ErrCode.Enum.Checksum_Improper_Count);
            }
            if (dateValueList[0].Date != blobStartDate)
            {
                throw new TSLibraryException(ErrCode.Enum.Checksum_Improper_StartDate);
            }
            if (dateValueList.Last().Date != blobEndDate)
            {
                throw new TSLibraryException(ErrCode.Enum.Checksum_Improper_EndDate);
            }

            // When compressing, we always use the latest compression method
            compressionCode = TSBlobCoder.currentCompressionCode;
            // Assign properties to the Trace object
            if (traceObject.EndDate != blobEndDate)
            {
                traceObject.EndDate = blobEndDate;
            }
            if (traceObject.TimeStepCount != timeStepCount)
            {
                traceObject.TimeStepCount = timeStepCount;
            }

            // Convert the List dateValueList into a BLOB.
            if (timeStepUnit == TSDateCalculator.TimeStepUnitCode.Irregular)
            {
                // IRREGULAR TIME SERIES

                // The method in TSBlobCoder can only process an array of TSDateValueStruct.  Therefore
                // we convert the List of objects to an Array of struct instances.
                TSDateValueStruct[] dateValueArray = dateValueList.Select(tsv => (TSDateValueStruct)tsv).ToArray();
                // Let the method in TSBlobCoder class do all the work
                TSBlobCoder.ConvertArrayToBlobIrregular(dateValueArray, compressionCode, traceObject);
            }
            else
            {
                // REGULAR TIME SERIES

                // The method in TSBlobCoder can only process an array of double values.  Therefore
                // we convert the List of date/value objects to an Array values.
                double[] valueArray = dateValueList.Select(dv => dv.Value).ToArray();
                // Let the method in TSBlobCoder class do all the work
                TSBlobCoder.ConvertArrayToBlobRegular(valueArray, compressionCode, traceObject);
            }

            return(traceObject.ValueBlob);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method prepares a new record for the trace table for an irregular time step series.
        /// The method converts the given dateValueArray 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 WriteTraceIrregular 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="dateValueArray">The array of values to be written to the database</param>
        public unsafe void WriteTraceIrregular(int id,
                                               bool doWriteToDB, TSImport tsImport,
                                               int traceNumber,
                                               TSDateValueStruct[] dateValueArray)
        {
            // Initialize class fields other than the BLOB of data values
            if (!IsInitialized)
            {
                Initialize(id, true);
            }

            // This method can only process irregular-time-step series
            if (TimeStepUnit != TSDateCalculator.TimeStepUnitCode.Irregular)
            {
                throw new TSLibraryException(ErrCode.Enum.Record_Not_Irregular,
                                             String.Format("The method can only process irregular time series, but" +
                                                           "the record with Id {0} is regular.", id));
            }
            // Create a trace object
            ITimeSeriesTrace traceObject = new TSTrace
            {
                TraceNumber   = traceNumber,
                TimeStepCount = dateValueArray.Count(),
                EndDate       = dateValueArray.Any() ? dateValueArray.Last().Date : BlobStartDate
            };

            if (tsImport != null)
            {
                tsImport.TraceList.Add(traceObject);
            }
            else
            {
                TraceList.Add(traceObject);
            }
            // Convert the array of double values into a byte array...a BLOB
            TSBlobCoder.ConvertArrayToBlobIrregular(dateValueArray, 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);
        }