/// <summary> /// Get a list of monthly sums for the specified data view. /// </summary> /// <param name="view"></param> /// <returns></returns> static public DataTable MonthlySums(DataView view) { // ---------------------------------------------------------------------------------- // From the daily data in the Metfile object, calculate monthly sums of all variables // ---------------------------------------------------------------------------------- DataTable monthlyData = new DataTable(); monthlyData.TableName = "MonthlyData"; if (view.Table.Columns.IndexOf("Date") == -1) { monthlyData.Columns.Add("Date", Type.GetType("System.DateTime")); } foreach (DataColumn Column in view.Table.Columns) { monthlyData.Columns.Add(Column.ColumnName, Column.DataType); } int previousMonth = 0; DataRow monthRow = null; for (int row = 0; row != view.Count; row++) { DateTime rowDate = DataTableUtilities.GetDateFromRow(view[row].Row); if (previousMonth != rowDate.Month) { monthRow = monthlyData.NewRow(); monthlyData.Rows.Add(monthRow); monthRow["Date"] = rowDate; previousMonth = rowDate.Month; } foreach (DataColumn Column in view.Table.Columns) { if (Convert.IsDBNull(monthRow[Column.ColumnName])) { monthRow[Column.ColumnName] = view[row][Column.ColumnName]; } else if (Column.DataType.ToString() == "System.Single" || Column.DataType.ToString() == "System.Double") { monthRow[Column.ColumnName] = Convert.ToDouble(monthRow[Column.ColumnName], System.Globalization.CultureInfo.InvariantCulture) + Convert.ToDouble(view[row][Column.ColumnName], System.Globalization.CultureInfo.InvariantCulture); } else { monthRow[Column.ColumnName] = view[row][Column.ColumnName]; } } } return(monthlyData); }
/// <summary>Calculates the MaxRadiation on all rows in datatable</summary> /// <param name="table">The datatable containing weather data</param> /// <param name="latitude">The latitude for the weather data location</param> public static void CalcQmax(DataTable table, double latitude) { if (!double.IsNaN(latitude)) { // ---------------------------------------------------------------------------------- // Add a calculated QMax column to the daily data. // ---------------------------------------------------------------------------------- if (((table.Columns["Qmax"] == null))) { table.Columns.Add("Qmax", Type.GetType("System.Single")); } // Do we have a VP column? bool haveVPColumn = (table.Columns["VP"] != null); //do we have a "doy" or "day" column, and which column is it in int dayCol = table.Columns.IndexOf("day"); if (dayCol < 0) { dayCol = table.Columns.IndexOf("doy"); } bool haveDOYColumn = dayCol >= 0; // Loop through all rows and calculate a QMax DateTime cDate; DataRow row; int doy = 0; for (int r = 0; r <= table.Rows.Count - 1; r++) { if (haveDOYColumn == true) { doy = Convert.ToInt16(table.Rows[r][dayCol], CultureInfo.InvariantCulture); } else { row = table.Rows[r]; cDate = DataTableUtilities.GetDateFromRow(row); doy = cDate.DayOfYear; } if (haveVPColumn && !Convert.IsDBNull(table.Rows[r]["vp"])) { table.Rows[r]["Qmax"] = MetUtilities.QMax(doy + 1, latitude, MetUtilities.Taz, MetUtilities.Alpha, Convert.ToSingle(table.Rows[r]["vp"], CultureInfo.InvariantCulture)); } else { table.Rows[r]["Qmax"] = MetUtilities.QMax(doy + 1, latitude, MetUtilities.Taz, MetUtilities.Alpha, MetUtilities.svp(Convert.ToSingle(table.Rows[r]["mint"], CultureInfo.InvariantCulture))); } } } }
/// <summary>Get a column as dates.</summary> /// <param name="table">The data table that contains the data required</param> /// <param name="colName">The name of the Date Column</param> /// <param name="firstDate">The Start date for the date range required</param> /// <param name="lastDate">The ending date for the date range required</param> /// <returns>An array of dates</returns> static public DateTime[] GetColumnAsDates(DataTable table, string colName, DateTime firstDate, DateTime lastDate) { //where row.Field<DateTime>(colName) >= firstDate var result = from row in table.AsEnumerable() where (DataTableUtilities.GetDateFromRow(row) >= firstDate && DataTableUtilities.GetDateFromRow(row) <= lastDate) select row; List<DateTime> rValues = new List<DateTime>(); foreach (var row in result) rValues.Add(Convert.ToDateTime(row[colName])); return rValues.ToArray(); }
/// <summary>Get columns as doubles within specific data range</summary> /// <param name="table">The data table to get the values from</param> /// <param name="colName">The name of the column to be referenced in the data table</param> /// <param name="firstDate">The start date of the data to be returned</param> /// <param name="lastDate">The end date of the data to be returned</param> /// <returns>The specified column of data, filtered by the dates, as an array of doubles. </returns> public static double[] GetColumnAsDoubles(DataTable table, string colName, DateTime firstDate, DateTime lastDate) { var result = from row in table.AsEnumerable() where (DataTableUtilities.GetDateFromRow(row) >= firstDate && DataTableUtilities.GetDateFromRow(row) <= lastDate) select new { val = row.Field<float>(colName) }; List<double> rValues = new List<double>(); foreach (var row in result) rValues.Add(row.val); return rValues.ToArray(); }
/// <summary>Gets string array of the months from a datatable</summary> /// <param name="table">The datatable to seach</param> /// <param name="firstDate">The start of the date range to search</param> /// <param name="lastDate">The end of the date range to search</param> /// <returns>A String array containing the distinct month names (abbreviated), in order, ie, "Jan, Feb, Mar"</returns> static public string[] GetDistinctMonthsasStrings(DataTable table, DateTime firstDate, DateTime lastDate) { //where row.Field<DateTime>(colName) >= firstDate var result = (from row in table.AsEnumerable() where (DataTableUtilities.GetDateFromRow(row) >= firstDate && DataTableUtilities.GetDateFromRow(row) <= lastDate) orderby DataTableUtilities.GetDateFromRow(row) select new { Month = DataTableUtilities.GetDateFromRow(row).Month }).Distinct(); List<string> rValues = new List<string>(); foreach (var row in result) rValues.Add(CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(row.Month)); return rValues.ToArray(); }