/// <summary> /// Bulk read the times and data for a dfs0 file, putting it all in /// a matrix structure. /// <para> /// First column in the result are the times, then a column for each /// item in the file. There are as many rows as there are timesteps. /// All item data are converted to doubles. /// </para> /// </summary> public static double[,] ReadDfs0DataDouble(IDfsFile dfs0File) { int itemCount = dfs0File.ItemInfo.Count; int timestepCount = dfs0File.FileInfo.TimeAxis.NumberOfTimeSteps; double[,] res = new double[timestepCount, itemCount + 1]; // Preload a set of item data IDfsItemData[] itemDatas = new IDfsItemData[itemCount]; for (int j = 0; j < itemCount; j++) { itemDatas[j] = dfs0File.CreateEmptyItemData(j + 1); } dfs0File.Reset(); for (int i = 0; i < timestepCount; i++) { for (int j = 0; j < itemCount; j++) { IDfsItemData itemData = itemDatas[j]; dfs0File.ReadItemTimeStep(itemData, i); // First column is time, remaining colums are data if (j == 0) { res[i, 0] = itemData.TimeInSeconds(dfs0File.FileInfo.TimeAxis); } res[i, j + 1] = Convert.ToDouble(itemData.Data.GetValue(0)); } } return(res); }
/// <summary> /// Bulk read the times and data for a dfs0 file, putting it all in /// a matrix structure. /// <para> /// First column in the result are the times, then a column for each /// item in the file. There are as many rows as there are timesteps. /// All item data are converted to doubles. /// </para> /// </summary> public static double[,] ReadDfs0DataDouble(IDfsFile dfs0File) { int itemCount = dfs0File.ItemInfo.Count; int timestepCount = dfs0File.FileInfo.TimeAxis.NumberOfTimeSteps; double[,] res = new double[timestepCount,itemCount+1]; // Preload a set of item data IDfsItemData[] itemDatas = new IDfsItemData[itemCount]; for (int j = 0; j < itemCount; j++) { itemDatas[j] = dfs0File.CreateEmptyItemData(j+1); } dfs0File.Reset(); for (int i = 0; i < timestepCount; i++) { for (int j = 0; j < itemCount; j++) { IDfsItemData itemData = itemDatas[j]; dfs0File.ReadItemTimeStep(itemData, i); // First column is time, remaining colums are data if (j == 0) { res[i, 0] = itemData.TimeInSeconds(dfs0File.FileInfo.TimeAxis); } res[i, j+1] = Convert.ToDouble(itemData.Data.GetValue(0)); } } return (res); }
/// <summary> /// Bulk read the times and data for a dfs0 file, putting it all in /// a matrix structure. /// <para> /// First column in the result are the times, then a column for each /// item in the file. There are as many rows as there are timesteps. /// All item data are converted to doubles. /// </para> /// </summary> public static double[,] ReadDfs0DataDouble(IDfsFile dfs0File) { int itemCount = dfs0File.ItemInfo.Count; int timestepCount = dfs0File.FileInfo.TimeAxis.NumberOfTimeSteps; double[,] res = new double[timestepCount, itemCount + 1]; // Preload a set of item data IDfsItemData[] itemDatas = new IDfsItemData[itemCount]; for (int j = 0; j < itemCount; j++) { itemDatas[j] = dfs0File.CreateEmptyItemData(j + 1); } dfs0File.Reset(); // Check if time axis is really a time axis, or if it is a non-time axis eumUnit timeUnit = dfs0File.FileInfo.TimeAxis.TimeUnit; bool isTimeUnit = EUMWrapper.eumUnitsEqv((int)eumUnit.eumUsec, (int)timeUnit); for (int i = 0; i < timestepCount; i++) { for (int j = 0; j < itemCount; j++) { IDfsItemData itemData = itemDatas[j]; dfs0File.ReadItemTimeStep(itemData, i); // First column is time, remaining colums are data if (j == 0) { if (isTimeUnit) { res[i, 0] = itemData.TimeInSeconds(dfs0File.FileInfo.TimeAxis); } else // not a time-unit, just return the value { res[i, 0] = itemData.Time; } } res[i, j + 1] = Convert.ToDouble(itemData.Data.GetValue(0)); } } return(res); }
/// <summary> /// Find maximum value and time of maximum for a specified item in dfs0 file /// </summary> /// <param name="filename">Path and name of file, e.g. data_ndr_roese.dfs0 test file</param> /// <param name="itemNumber">Item number to find maximum for</param> public static double FindMaxValue(string filename, int itemNumber) { // Open file, using stream class Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); IDfsFile file = DfsFileFactory.DfsGenericOpen(stream); //IDfsFile file = DfsFileFactory.DfsGenericOpen(filename); // Extract Start date-time of file - assuming file is equidistant-calendar axis IDfsEqCalendarAxis timeAxis = (IDfsEqCalendarAxis)file.FileInfo.TimeAxis; DateTime startDateTime = timeAxis.StartDateTime; // Empty item data, reused when calling ReadItemTimeStep IDfsItemData <float> itemData = (IDfsItemData <float>)file.CreateEmptyItemData(itemNumber); // max value and time variables double maxValue = double.MinValue; double maxTimeSeconds = -1; DateTime maxDateTime = DateTime.MinValue; // Loop over all times in file for (int i = 0; i < file.FileInfo.TimeAxis.NumberOfTimeSteps; i++) { // Read time step for item, and extract value file.ReadItemTimeStep(itemData, i); double value = itemData.Data[0]; // Check if value is larger than maxValue if (value > maxValue) { maxValue = value; maxTimeSeconds = itemData.TimeInSeconds(timeAxis); maxDateTime = itemData.TimeAsDateTime(timeAxis); } } // Report results Console.Out.WriteLine("Max Value : {0} {1}", maxValue, file.ItemInfo[itemNumber - 1].Quantity.UnitAbbreviation); Console.Out.WriteLine("Max Value time : {0}", maxDateTime.ToString("yyyy-MM-dd HH:mm:ss")); return(maxValue); }