/// <summary> /// Reads all the precipitation data from the excel file /// </summary> /// <param name="path">The path to the file</param> /// <returns>List of precipitation Data, or Null if an error occurs.</returns> public static List <PrecipitationData> ReadAll(string path) { var lst = new List <PrecipitationData>(); if (!File.Exists(path)) { AppLog.Error("Could not find the file: {0}", path); return(null); } try { using (var stream = File.Open(path, FileMode.Open, FileAccess.Read)) { using (var reader = ExcelReaderFactory.CreateReader(stream)) { var result = reader.AsDataSet(); var table = result.Tables["in"]; for (var i = 0; i < table.Rows.Count; i++) { var pd = FromRow(table.Rows[i]); lst.Add(pd); } } } } catch (Exception exception) { AppLog.Exception(exception, "Error reading data file."); lst = null; } return(lst); }
/// <summary> /// Attempts to parse a string into a double value /// </summary> /// <param name="s">A string value to parse into a double</param> /// <returns> /// Returns a double equivalent of the string, /// or double.MinValue if an error occurs. /// </returns> public static double ToDouble(this string s) { try { if (!double.TryParse(s, out var f)) { f = double.MinValue; } return(f); } catch (Exception exception) { AppLog.Exception(exception, "Could not parse string to double variable: {0}", s); return(double.MinValue); } }
/// <summary> /// Attempts to parse a string into a DateTime value /// </summary> /// <param name="s">A string value to parse into a DateTime</param> /// <returns> /// Returns a DateTime equivalent of the string, /// or DateTime.MinValue if an error occurs. /// </returns> public static DateTime ToDateTime(this string s) { try { if (!DateTime.TryParse(s, out var dt)) { dt = DateTime.MinValue; } return(dt); } catch (Exception exception) { AppLog.Exception(exception, "Could not parse string to DateTime variable: {0}", s); return(DateTime.MinValue); } }
/// <summary> /// Reads all the precipitation data from the excel file where the month and day match the request. /// </summary> /// <param name="path">The path to the file</param> /// <param name="month">The numeric month to read data for</param> /// <param name="day">The day to read data for</param> /// <returns>List of precipitation Data, or Null if an error occurs.</returns> public static List <PrecipitationData> ReadForMonthDay(string path, int month, int day) { var lst = new List <PrecipitationData>(); if (!File.Exists(path)) { AppLog.Error("Could not find the file: {0}", path); return(null); } try { using (var stream = File.Open(path, FileMode.Open, FileAccess.Read)) { using (var reader = ExcelReaderFactory.CreateReader(stream)) { var result = reader.AsDataSet(); var table = result.Tables["in"]; for (var i = 0; i < table.Rows.Count; i++) { // Just look at the date row to see if this record to be included var dtString = table.Rows[i][5].ToString(); var dt = dtString.ToDateTime(); if (dt.Month != month || dt.Day != day) { continue; } var pd = FromRow(table.Rows[i]); lst.Add(pd); } } } } catch (Exception exception) { AppLog.Exception(exception, "Error reading data file."); lst = null; } return(lst); }