/// <summary> /// Creates the "ecd" formatted file. /// </summary> /// <param name="savedDirName">The saved directory name.</param> /// <param name="logData">The list of the "LogData"</param> /// <param name="saveType">The type of saved file.</param> /// <param name="startTime">The start time to save the log.</param> /// <param name="endTime">The end time to save the log.</param> public void Create(string savedDirName, LogData logData, SaveType saveType, double startTime, double endTime) { string fileName = null; string fileExtension = ""; string splitter = ""; if (saveType == SaveType.CSV) { fileExtension = ".csv"; splitter = Constants.delimiterComma; } else { fileExtension = ".ecd"; splitter = Constants.delimiterTab; } try { // // Initializes. // if (savedDirName == null || savedDirName.Length <= 0) { return; } else if (logData == null) { return; } // // Sets the file name. // fileName = logData.type + Constants.delimiterUnderbar + logData.key + Constants.delimiterUnderbar + logData.propName; fileName = fileName.Replace(Constants.delimiterPath, Constants.delimiterUnderbar); fileName = fileName.Replace(Constants.delimiterColon, Constants.delimiterUnderbar); fileName = savedDirName + Constants.delimiterPath + fileName + fileExtension; // // Checks the old model file. // if (File.Exists(fileName)) { string date = File.GetLastAccessTime(fileName).ToString().Replace( Constants.delimiterColon, Constants.delimiterUnderbar); date = date.Replace(Constants.delimiterPath, Constants.delimiterUnderbar); date = date.Replace(Constants.delimiterSpace, Constants.delimiterUnderbar); string destFileName = Path.GetDirectoryName(fileName) + Constants.delimiterPath + Constants.delimiterUnderbar + date + Constants.delimiterUnderbar + Path.GetFileName(fileName); File.Move(fileName, destFileName); } // // Saves the "LogData". // m_writer = new StreamWriter( new FileStream(fileName, FileMode.CreateNew), System.Text.Encoding.UTF8); // // Writes the header. // if (saveType != SaveType.CSV) { m_writer.WriteLine( Constants.delimiterSharp + Constants.headerData + Constants.delimiterColon + Constants.delimiterSpace + logData.type + Constants.delimiterColon + logData.key + Constants.delimiterColon + logData.propName ); int headerColumn = 0; if (Double.IsNaN(logData.logValueList[0].avg) && Double.IsNaN(logData.logValueList[0].min) && Double.IsNaN(logData.logValueList[0].max) ) { headerColumn = 2; } else { headerColumn = 5; } m_writer.WriteLine( Constants.delimiterSharp + Constants.headerSize + Constants.delimiterColon + Constants.delimiterSpace + headerColumn + Constants.delimiterSpace + logData.logValueList.Count ); m_writer.WriteLine( Constants.delimiterSharp + Constants.headerLabel + Constants.delimiterColon + Constants.delimiterSpace + Constants.headerTime + splitter + Constants.headerValue + splitter + Constants.headerAverage + splitter + Constants.headerMinimum.ToLower() + splitter + Constants.headerMaximum.ToLower() ); m_writer.WriteLine( Constants.delimiterSharp + Constants.headerNote + Constants.delimiterColon + Constants.delimiterSpace ); m_writer.WriteLine( Constants.delimiterSharp ); string separator = ""; for (int i = 0; i < 22; i++) { separator += Constants.delimiterHyphen; } m_writer.WriteLine( Constants.delimiterSharp + separator ); m_writer.Flush(); } // // Writes the "LogData". // foreach (LogValue logValue in logData.logValueList) { if (m_oldTime == logValue.time) { continue; } double ltime; double.TryParse(logValue.time.ToString(), out ltime); if (startTime > ltime || endTime < ltime) continue; if (Double.IsNaN(logValue.avg) && Double.IsNaN(logValue.min) && Double.IsNaN(logValue.max) ) { m_writer.WriteLine( logValue.time + splitter + logValue.value ); } else { m_writer.WriteLine( logValue.time + splitter + logValue.value + splitter + logValue.avg + splitter + logValue.min + splitter + logValue.max ); } m_oldTime = logValue.time; } } catch (Exception ex) { if (m_writer != null) { m_writer.Close(); m_writer = null; } throw new EcellException(string.Format(MessageResources.ErrCreFile, new object[] { fileName }), ex); } }
/// <summary> /// Append the log data to the writer object. /// </summary> /// <param name="savedDirName">the save directory.</param> /// <param name="logData">the list of log data.</param> /// <param name="saveType">the save type.</param> /// <param name="startTime">the start time to save the log.</param> /// <param name="endTime">the end time to save the log.</param> public void Append(string savedDirName, LogData logData, SaveType saveType, double startTime, double endTime) { string fileName = null; string fileExtension = ""; string splitter = ""; if (saveType == SaveType.CSV) { fileExtension = ".csv"; splitter = Constants.delimiterComma; } else { fileExtension = ".ecd"; splitter = Constants.delimiterTab; } try { // // Initializes. // if (savedDirName == null || savedDirName.Length <= 0) { return; } else if (logData == null) { return; } // // Sets the file name. // fileName = logData.type + Constants.delimiterUnderbar + logData.key + Constants.delimiterUnderbar + logData.propName; fileName = fileName.Replace(Constants.delimiterPath, Constants.delimiterUnderbar); fileName = fileName.Replace(Constants.delimiterColon, Constants.delimiterUnderbar); fileName = savedDirName + Constants.delimiterPath + fileName + fileExtension; // // Writes the "LogData". // foreach (LogValue logValue in logData.logValueList) { if (m_oldTime == logValue.time) { continue; } double ltime; double.TryParse(logValue.time.ToString(), out ltime); if (startTime > ltime || endTime < ltime) continue; if (Double.IsNaN(logValue.avg) && Double.IsNaN(logValue.min) && Double.IsNaN(logValue.max) ) { m_writer.WriteLine( logValue.time + splitter + logValue.value ); } else { m_writer.WriteLine( logValue.time + splitter + logValue.value + splitter + logValue.avg + splitter + logValue.min + splitter + logValue.max ); } } } catch (Exception ex) { if (m_writer != null) { m_writer.Close(); m_writer = null; } throw new EcellException(string.Format(MessageResources.ErrCreFile, new object[] { fileName }), ex); } }
public void TestLogData() { string expectedModeID = null; string expectedKey = null; string expectedPropName = null; string expectedType = null; bool expectedIsLoad = false; string expectedFileName = null; LogData data = new LogData(); Assert.AreEqual(expectedModeID, data.model, "LogData accessor is unexpected value."); Assert.AreEqual(expectedKey, data.key, "LogData accessor is unexpected value."); Assert.AreEqual(expectedType, data.type, "LogData accessor is unexpected value."); Assert.AreEqual(expectedPropName, data.propName, "LogData accessor is unexpected value."); Assert.AreEqual(expectedIsLoad, data.IsLoaded, "LogData accessor is unexpected value."); Assert.AreEqual(expectedFileName, data.FileName, "LogData accessor is unexpected value."); Assert.IsNull(data.logValueList, "LogData accessor is unexpected value."); expectedModeID = "Model"; expectedKey = "KeyData"; expectedType = "Type"; expectedPropName = "PropData"; expectedIsLoad = true; expectedFileName = "FileName"; data.IsLoaded = expectedIsLoad; data.FileName = expectedFileName; Assert.AreEqual(expectedIsLoad, data.IsLoaded, "LogData accessor is unexpected value."); Assert.AreEqual(expectedFileName, data.FileName, "LogData accessor is unexpected value."); LogData d = new LogData(expectedModeID, expectedKey, expectedType, expectedPropName, new List<LogValue>()); Assert.AreEqual(expectedModeID, d.model, "LogData accessor is unexpected value."); Assert.AreEqual(expectedKey, d.key, "LogData accessor is unexpected value."); Assert.AreEqual(expectedType, d.type, "LogData accessor is unexpected value."); Assert.AreEqual(expectedPropName, d.propName, "LogData accessor is unexpected value."); Assert.AreEqual(0, d.logValueList.Count, "LogData accessor is unexpected value."); }
/// <summary> /// Load the log data from the log file. /// </summary> /// <param name="fileName">the log file.</param> /// <returns>the list of log data.</returns> public static LogData LoadSavedLogData(string fileName) { string type = ""; string key = ""; string prop = ""; string line = ""; char splitter = '\t'; List<LogValue> valueList = new List<LogValue>(); StreamReader strread = new StreamReader(fileName); string ext = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(ext) && ext.ToLower().Equals(".csv")) { splitter = ','; prop = Path.GetFileNameWithoutExtension(fileName); } while ((line = strread.ReadLine()) != null) { if (line.StartsWith(Constants.delimiterSharp)) { if (line.StartsWith(Constants.delimiterSharp + Constants.headerData)) { string[] ele = line.Split(new char[] { ':' }); type = ele[1].Replace(" ", ""); key = ele[2].Replace(" ", "") + Constants.delimiterColon + ele[3].Replace(" ", ""); prop = line.Replace(ele[0], "").Replace(" ", ""); } continue; } string[] points = line.Split(new char[] { splitter }); if (points.Length < 5) continue; double time = Convert.ToDouble(points[0]); double value = Convert.ToDouble(points[1]); double ave = Convert.ToDouble(points[2]); double min = Convert.ToDouble(points[3]); double max = Convert.ToDouble(points[4]); valueList.Add(new LogValue(time, value, ave, min, max)); } strread.Close(); LogData d = new LogData("", key, type, prop, valueList); d.IsLoaded = true; return d; }
/// <summary> /// Get the uniqueue log data from the current simulator. /// </summary> /// <param name="startTime">The start time of log file.</param> /// <param name="endTime">The endt time of log file.</param> /// <param name="interval">The interval of log file.</param> /// <param name="fullID">The FullID of log data.</param> /// <returns>The list of log data.</returns> private LogData GetUniqueLogData( double startTime, double endTime, double interval, string fullID) { if (startTime < 0.0) { startTime = 0.0; } if (endTime <= 0.0) { endTime = m_currentProject.Simulator.GetCurrentTime(); } if (this.m_simulationTimeLimit > 0.0 && endTime > this.m_simulationTimeLimit) { endTime = this.m_simulationTimeLimit; } if (startTime > endTime) { double tmpTime = startTime; startTime = endTime; endTime = tmpTime; } WrappedDataPointVector dataPointVector = null; if (interval <= 0.0) { lock (m_currentProject.Simulator) { dataPointVector = m_currentProject.Simulator.GetLoggerData( fullID, startTime, endTime ); } } else { lock (m_currentProject.Simulator) { dataPointVector = m_currentProject.Simulator.GetLoggerData( fullID, startTime, endTime, interval ); } } List<LogValue> logValueList = new List<LogValue>(); double lastTime = -1.0; for (int i = 0; i < dataPointVector.GetArraySize(); i++) { if (lastTime == dataPointVector.GetTime(i)) { continue; } LogValue logValue = new LogValue( dataPointVector.GetTime(i), dataPointVector.GetValue(i), dataPointVector.GetAvg(i), dataPointVector.GetMin(i), dataPointVector.GetMax(i) ); logValueList.Add(logValue); lastTime = dataPointVector.GetTime(i); } string modelID = null; if (m_currentProject.LogableEntityPathDic.ContainsKey(fullID)) { modelID = m_currentProject.LogableEntityPathDic[fullID]; } string key = null; string type = null; string propName = null; Util.ParseFullPN(fullID, out type, out key, out propName); if (logValueList.Count == 1 && logValueList[0].time == 0.0) { LogValue logValue = new LogValue( endTime, logValueList[0].value, logValueList[0].avg, logValueList[0].min, logValueList[0].max ); logValueList.Add(logValue); } LogData logData = new LogData( modelID, key, type, propName, logValueList ); dataPointVector.Dispose(); return logData; }