/// <summary> /// Exports a data set to a CSV file. /// The file is saved in the same format as the original CSV files. /// </summary> /// <param name="data">The dataset to export</param> /// <param name="filePath">The desired path and file name of the file to be saved. No not include an extension.</param> /// <param name="format">The format to save the file in.</param> /// <param name="includeEmptyLines">Wether to export the file with empty lines or not.</param> /// <param name="addMetaDataFile">Wether to export the file with embedded site meta data.</param> /// <param name="includeChangeLog">Wether to include a seperate log file that details the changes made to the data.</param> /// <param name="exportedPoints">What points to export.</param> /// <param name="dateColumnFormat">Wether to split the two date/time columns into five seperate columns</param> /// <param name="exportRaw">Whether to export the raw data or the current state.</param> /// <param name="loadInUnloadedValues">Whether or not to load in any unloaded values</param> /// <param name="copyLogFiles">Whether or not to copy the log files</param> public static void Export(Dataset data, string filePath, ExportFormat format, bool includeEmptyLines, bool addMetaDataFile, bool includeChangeLog, ExportedPoints exportedPoints, DateColumnFormat dateColumnFormat, bool exportRaw, bool loadInUnloadedValues, bool copyLogFiles = false) { if (data == null) { throw new ArgumentNullException("Dataset cannot be null"); } //LOAD IN ALL THE VALUES var firstYearLoaded = data.LowestYearLoaded; var lastYearLoaded = data.HighestYearLoaded; if (loadInUnloadedValues) { if (firstYearLoaded != 0) { for (var i = 0; i < firstYearLoaded; i++) { data.LoadInSensorData(i, true); } } if (data.EndYear > data.StartYear.AddYears(lastYearLoaded + 1)) { for (var i = lastYearLoaded + 1; data.EndYear >= data.StartYear.AddYears(i + 1); i++) { data.LoadInSensorData(i, true); } } } EventLogger.LogInfo(data, "EXPORTER", "Data export started."); if (String.IsNullOrWhiteSpace(filePath)) { throw new ArgumentNullException("filePath cannot be null"); } if (format == null) { throw new ArgumentNullException("Export format cannot be null"); } //Strip the existing extension and add the one specified in the method args filePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath), Path.GetFileName(filePath)); if (!Directory.Exists(Path.GetDirectoryName(filePath))) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)); } else { throw new Exception("Cannot Overwrite Existing Data"); } filePath = Path.ChangeExtension(filePath, format.Extension); string metaDataFilePath = filePath + " Site Meta Data.txt"; string changeMatrixFilePath = filePath + " Changes Matrix.csv"; var changesFilePath = filePath + " Changes Log.txt"; var numOfPointsToSummarise = 1; if (exportedPoints.NumberOfMinutes != 0) { numOfPointsToSummarise = exportedPoints.NumberOfMinutes / data.DataInterval; } if (format.Equals(ExportFormat.CSV)) { ExportCSV(data, filePath, includeEmptyLines, dateColumnFormat, false, numOfPointsToSummarise); if (exportRaw) { ExportCSV(data, filePath + " Raw.csv", includeEmptyLines, dateColumnFormat, true, numOfPointsToSummarise); } if (addMetaDataFile && data.Site != null) { ExportMetaData(data, filePath, metaDataFilePath); } if (includeChangeLog) { ExportChangesFile(data, filePath, changeMatrixFilePath, changesFilePath, dateColumnFormat); } EventLogger.LogInfo(data, "EXPORTER", "Data export complete. File saved to: " + filePath); } else if (format.Equals(ExportFormat.XLSX)) { throw new NotImplementedException("Cannot export as XLSX yet."); } else { throw new NotImplementedException("File format not supported."); } if (copyLogFiles && data.Site != null && Directory.Exists(Path.Combine(Common.AppDataPath, "Logs", data.Site.Name, "SensorLogs"))) { var sourcePath = Path.Combine(Common.AppDataPath, "Logs", data.Site.Name, "SensorLogs"); var destinationPath = Path.Combine(Path.GetDirectoryName(filePath), data.Site.Name + " Sensor Logs"); if (!Directory.Exists(destinationPath)) { Directory.CreateDirectory(destinationPath); } //Now Create all of the directories foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) { Directory.CreateDirectory(dirPath.Replace(sourcePath, destinationPath)); } //Copy all the files foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) { File.Copy(newPath, newPath.Replace(sourcePath, destinationPath)); } } if (loadInUnloadedValues) { //Unload all values not in our time range foreach (var sensor in data.Sensors) { var currentValuesToRemove = sensor.CurrentState.Values.Where( x => x.Key < data.StartYear.AddYears(firstYearLoaded) || x.Key >= data.StartYear.AddYears(lastYearLoaded + 1)).ToArray(); foreach (var keyValuePair in currentValuesToRemove) { sensor.CurrentState.Values.Remove(keyValuePair.Key); } var rawValuesToRemove = sensor.RawData.Values.Where( x => x.Key < data.StartYear.AddYears(firstYearLoaded) || x.Key >= data.StartYear.AddYears(lastYearLoaded + 1)).ToArray(); foreach (var keyValuePair in rawValuesToRemove) { sensor.RawData.Values.Remove(keyValuePair.Key); } } data.LowestYearLoaded = firstYearLoaded; data.HighestYearLoaded = lastYearLoaded; } }
/// <summary> /// Exports a data set to a CSV file. /// The file is saved in the same format as the original CSV files. /// </summary> /// <param name="data">The dataset to export</param> /// <param name="filePath">The desired path and file name of the file to be saved. No not include an extension.</param> /// <param name="format">The format to save the file in.</param> /// <param name="includeEmptyLines">Wether to export the file with empty lines or not.</param> /// <param name="addMetaDataFile">Wether to export the file with embedded site meta data.</param> /// <param name="includeChangeLog">Wether to include a seperate log file that details the changes made to the data.</param> /// <param name="exportedPoints">What points to export.</param> /// <param name="dateColumnFormat">Wether to split the two date/time columns into five seperate columns</param> /// <param name="loadInUnloadedValues">Whether or not to load in any unloaded values</param> public static void Export(Dataset data, string filePath, ExportFormat format, bool includeEmptyLines, bool addMetaDataFile, bool includeChangeLog, ExportedPoints exportedPoints, DateColumnFormat dateColumnFormat, bool loadInUnloadedValues = true) { Export(data, filePath, format, includeEmptyLines, addMetaDataFile, includeChangeLog, exportedPoints, dateColumnFormat, false, loadInUnloadedValues); }
/// <summary> /// Exports a data set to a CSV file. /// The file is saved in the same format as the original CSV files. /// </summary> /// <param name="data">The dataset to export</param> /// <param name="filePath">The desired path and file name of the file to be saved. No not include an extension.</param> /// <param name="format">The format to save the file in.</param> /// <param name="includeEmptyLines">Wether to export the file with empty lines or not.</param> /// <param name="loadInUnloadedValues">Whether or not to load in any unloaded values</param> public static void Export(Dataset data, string filePath, ExportFormat format, bool includeEmptyLines, bool loadInUnloadedValues = true) { Export(data, filePath, format, includeEmptyLines, false, false, ExportedPoints.AllPoints, DateColumnFormat.TwoDateColumn, false, loadInUnloadedValues); }
/// <summary> /// Exports a data set to a CSV file. /// The file is saved in the same format as the original CSV files. /// </summary> /// <param name="data">The dataset to export</param> /// <param name="filePath">The desired path and file name of the file to be saved. No not include an extension.</param> /// <param name="format">The format to save the file in.</param> /// <param name="includeEmptyLines">Wether to export the file with empty lines or not.</param> /// <param name="addMetaDataFile">Wether to export the file with embedded site meta data.</param> /// <param name="includeChangeLog">Wether to include a seperate log file that details the changes made to the data.</param> /// <param name="exportedPoints">What points to export.</param> /// <param name="dateColumnFormat">Wether to split the two date/time columns into five seperate columns</param> /// <param name="exportRaw">Whether to export the raw data or the current state.</param> /// <param name="loadInUnloadedValues">Whether or not to load in any unloaded values</param> /// <param name="copyLogFiles">Whether or not to copy the log files</param> public static void Export(Dataset data, string filePath, ExportFormat format, bool includeEmptyLines, bool addMetaDataFile, bool includeChangeLog, ExportedPoints exportedPoints, DateColumnFormat dateColumnFormat, bool exportRaw, bool loadInUnloadedValues, bool copyLogFiles = false) { if (data == null) throw new ArgumentNullException("Dataset cannot be null"); //LOAD IN ALL THE VALUES var firstYearLoaded = data.LowestYearLoaded; var lastYearLoaded = data.HighestYearLoaded; if (loadInUnloadedValues) { if (firstYearLoaded != 0) { for (var i = 0; i < firstYearLoaded; i++) { data.LoadInSensorData(i, true); } } if (data.EndYear > data.StartYear.AddYears(lastYearLoaded + 1)) { for (var i = lastYearLoaded + 1; data.EndYear >= data.StartYear.AddYears(i + 1); i++) { data.LoadInSensorData(i, true); } } } EventLogger.LogInfo(data, "EXPORTER", "Data export started."); if (String.IsNullOrWhiteSpace(filePath)) throw new ArgumentNullException("filePath cannot be null"); if (format == null) throw new ArgumentNullException("Export format cannot be null"); //Strip the existing extension and add the one specified in the method args filePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath), Path.GetFileName(filePath)); if (!Directory.Exists(Path.GetDirectoryName(filePath))) Directory.CreateDirectory(Path.GetDirectoryName(filePath)); else { throw new Exception("Cannot Overwrite Existing Data"); } string metaDataFilePath = Path.ChangeExtension(filePath, "_") + "Metadata.txt"; string changeMatrixFilePath = Path.ChangeExtension(filePath, "_") + "ChangesMatrix.txt"; var changesFilePath = Path.ChangeExtension(filePath, "_") + "Changes Log.txt"; var numOfPointsToSummarise = 1; if (exportedPoints.NumberOfMinutes != 0) numOfPointsToSummarise = exportedPoints.NumberOfMinutes / data.DataInterval; if (format.Equals(ExportFormat.CSV)) { ExportCSV(data, filePath, includeEmptyLines, dateColumnFormat, false, numOfPointsToSummarise); if (exportRaw) ExportCSV(data, Path.ChangeExtension(filePath, "_") + "Raw.txt", includeEmptyLines, dateColumnFormat, true, numOfPointsToSummarise); if (addMetaDataFile && data.Site != null) ExportMetaData(data, filePath, metaDataFilePath); if (includeChangeLog) ExportChangesFile(data, filePath, changeMatrixFilePath, changesFilePath, dateColumnFormat); EventLogger.LogInfo(data, "EXPORTER", "Data export complete. File saved to: " + filePath); } else if (format.Equals(ExportFormat.XLSX)) { throw new NotImplementedException("Cannot export as XLSX yet."); } else { throw new NotImplementedException("File format not supported."); } if (copyLogFiles && data.Site != null && Directory.Exists(Path.Combine(Common.AppDataPath, "Logs", data.Site.Name, "SensorLogs"))) { var sourcePath = Path.Combine(Common.AppDataPath, "Logs", data.Site.Name, "SensorLogs"); using (TextWriter tw = new StreamWriter(changesFilePath, true)) { tw.WriteLine("Log of change reasons for associated file " + Path.GetFileNameWithoutExtension(filePath)); //Copy all the files into one File Log foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) { tw.WriteLine(""); tw.WriteLine("Change reasons for sensor " + Path.GetFileNameWithoutExtension(newPath)); tw.WriteLine(""); using (TextReader tr = new StreamReader(newPath)) { tw.WriteLine(tr.ReadToEnd()); tr.Close(); } Console.WriteLine("File Processed : " + filePath); } tw.Close(); } } if (loadInUnloadedValues) { //Unload all values not in our time range foreach (var sensor in data.Sensors) { var currentValuesToRemove = sensor.CurrentState.Values.Where( x => x.Key < data.StartYear.AddYears(firstYearLoaded) || x.Key >= data.StartYear.AddYears(lastYearLoaded + 1)).ToArray(); foreach (var keyValuePair in currentValuesToRemove) { sensor.CurrentState.Values.Remove(keyValuePair.Key); } var rawValuesToRemove = sensor.RawData.Values.Where( x => x.Key < data.StartYear.AddYears(firstYearLoaded) || x.Key >= data.StartYear.AddYears(lastYearLoaded + 1)).ToArray(); foreach (var keyValuePair in rawValuesToRemove) { sensor.RawData.Values.Remove(keyValuePair.Key); } } data.LowestYearLoaded = firstYearLoaded; data.HighestYearLoaded = lastYearLoaded; } }