protected override void SolveInstance(IGH_DataAccess DA) { List<DHr> dhrs = new List<DHr>(); string key = ""; if ((DA.GetDataList(0, dhrs)) && (DA.GetData(1, ref key))) { float maxval = dhrs[0].val(key); float minval = dhrs[0].val(key); int max_day = 0; int min_day = 0; for (int n = 1; n < dhrs.Count; n++) { float val = dhrs[n].val(key); if (val > maxval) { maxval = val; max_day = dhrs[n].day_of_year; } if (val < minval) { minval = val; min_day = dhrs[n].day_of_year; } } HourMask max_mask = new HourMask(); max_mask.maskByDayOfYear(max_day, max_day); HourMask min_mask = new HourMask(); min_mask.maskByDayOfYear(min_day, min_day); List<DHr> maxHrs = new List<DHr>(); foreach (DHr hr in dhrs) if (max_mask.eval(hr)) maxHrs.Add(hr); List<DHr> minHrs = new List<DHr>(); foreach (DHr hr in dhrs) if (min_mask.eval(hr)) minHrs.Add(hr); DA.SetDataList(0, maxHrs); DA.SetDataList(1, minHrs); } }
protected override void SolveInstance(IGH_DataAccess DA) { List<DHr> dhrs = new List<DHr>(); string period_string = ""; if ((DA.GetDataList(0, dhrs)) && (DA.GetData(1, ref period_string))) { if (period_string == "") { return; } period_string = period_string.ToLowerInvariant().Trim(); this.cycle_type = CType.Invalid; if (period_string.Contains("year")) { this.cycle_type = CType.Yearly; } else if (period_string.Contains("monthly diurnal")) { this.cycle_type = CType.MonthlyDiurnal; } else if (period_string.Contains("month")) { this.cycle_type = CType.Monthly; } else if (period_string.Contains("day") || period_string.Contains("daily")) { this.cycle_type = CType.Daily; } else if (period_string.Contains("weekly diurnal")) { this.cycle_type = CType.WeeklyDiurnal; } else if (period_string.Contains("weekly")) { this.cycle_type = CType.Weekly; } else { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "I don't understand the time period you're looking for.\nPlease choose 'yearly', 'monthly', 'monthly diurnal', 'weekly', 'weekly diurnal', or 'daily'."); } string[] commonKeys = DHr.commonkeys(dhrs.ToArray()); Dictionary<string, List<DHr>> stat_hours = new Dictionary<string, List<DHr>>(); InitStatHours(ref stat_hours); HourMask mask = new HourMask(); switch (this.cycle_type) { case CType.MonthlyDiurnal: case CType.WeeklyDiurnal: Grasshopper.Kernel.Data.GH_Structure<DHr> meanTree = new Grasshopper.Kernel.Data.GH_Structure<DHr>(); Grasshopper.Kernel.Data.GH_Structure<DHr> modeTree = new Grasshopper.Kernel.Data.GH_Structure<DHr>(); Grasshopper.Kernel.Data.GH_Structure<DHr> highTree = new Grasshopper.Kernel.Data.GH_Structure<DHr>(); Grasshopper.Kernel.Data.GH_Structure<DHr> uqTree = new Grasshopper.Kernel.Data.GH_Structure<DHr>(); Grasshopper.Kernel.Data.GH_Structure<DHr> medianTree = new Grasshopper.Kernel.Data.GH_Structure<DHr>(); Grasshopper.Kernel.Data.GH_Structure<DHr> lqTree = new Grasshopper.Kernel.Data.GH_Structure<DHr>(); Grasshopper.Kernel.Data.GH_Structure<DHr> lowTree = new Grasshopper.Kernel.Data.GH_Structure<DHr>(); Grasshopper.Kernel.Data.GH_Structure<DHr> sumTree = new Grasshopper.Kernel.Data.GH_Structure<DHr>(); switch (this.cycle_type) { case CType.MonthlyDiurnal: for (int mth = 0; mth < 12; mth++) { InitStatHours(ref stat_hours); for (int hour = 0; hour < 24; hour++) { mask.maskByMonthAndHour(mth, hour); int hh = Util.hourOfYearFromDatetime(Util.baseDatetime().AddMonths(mth).AddHours(hour)) + 1; // had to add one, looks like Util function was designed for parsing non-zero-indexed hours CalculateStats(dhrs, commonKeys, stat_hours, mask, hh, true); } meanTree.AppendRange(stat_hours["meanHrs"], new Grasshopper.Kernel.Data.GH_Path(mth)); modeTree.AppendRange(stat_hours["modeHrs"], new Grasshopper.Kernel.Data.GH_Path(mth)); highTree.AppendRange(stat_hours["highHrs"], new Grasshopper.Kernel.Data.GH_Path(mth)); uqTree.AppendRange(stat_hours["uqHrs"], new Grasshopper.Kernel.Data.GH_Path(mth)); medianTree.AppendRange(stat_hours["medianHrs"], new Grasshopper.Kernel.Data.GH_Path(mth)); lqTree.AppendRange(stat_hours["lqHrs"], new Grasshopper.Kernel.Data.GH_Path(mth)); lowTree.AppendRange(stat_hours["lowHrs"], new Grasshopper.Kernel.Data.GH_Path(mth)); sumTree.AppendRange(stat_hours["sumHrs"], new Grasshopper.Kernel.Data.GH_Path(mth)); } break; case CType.WeeklyDiurnal: for (int wk = 0; wk < 52; wk++) { InitStatHours(ref stat_hours); for (int hour = 0; hour < 24; hour++) { mask.maskByWeekAndHour(wk, hour); int hh = Util.hourOfYearFromDatetime(Util.baseDatetime().AddDays(wk * 7).AddHours(hour)) + 1; // had to add one, looks like Util function was designed for parsing non-zero-indexed hours CalculateStats(dhrs, commonKeys, stat_hours, mask, hh, true); } meanTree.AppendRange(stat_hours["meanHrs"], new Grasshopper.Kernel.Data.GH_Path(wk)); modeTree.AppendRange(stat_hours["modeHrs"], new Grasshopper.Kernel.Data.GH_Path(wk)); highTree.AppendRange(stat_hours["highHrs"], new Grasshopper.Kernel.Data.GH_Path(wk)); uqTree.AppendRange(stat_hours["uqHrs"], new Grasshopper.Kernel.Data.GH_Path(wk)); medianTree.AppendRange(stat_hours["medianHrs"], new Grasshopper.Kernel.Data.GH_Path(wk)); lqTree.AppendRange(stat_hours["lqHrs"], new Grasshopper.Kernel.Data.GH_Path(wk)); lowTree.AppendRange(stat_hours["lowHrs"], new Grasshopper.Kernel.Data.GH_Path(wk)); sumTree.AppendRange(stat_hours["sumHrs"], new Grasshopper.Kernel.Data.GH_Path(wk)); } break; } DA.SetDataTree(0, meanTree); DA.SetDataTree(1, modeTree); DA.SetDataTree(2, highTree); DA.SetDataTree(3, uqTree); DA.SetDataTree(4, medianTree); DA.SetDataTree(5, lqTree); DA.SetDataTree(6, lowTree); DA.SetDataTree(7, sumTree); break; case CType.Daily: for (int day = 0; day < 365; day++) { mask.maskByDayOfYear(day, day); // passing in same day twice masks to this single day int hh = Util.hourOfYearFromDatetime(Util.baseDatetime().AddDays(day).AddHours(0)) + 1; // had to add one, looks like Util function was designed for parsing non-zero-indexed hours CalculateStats(dhrs, commonKeys, stat_hours, mask, hh, true); } SetOutputData(DA, stat_hours); break; case CType.Weekly: for (int wk = 0; wk < 52; wk++) { mask.maskByWeek(wk); int hh = Util.hourOfYearFromDatetime(Util.baseDatetime().AddDays(wk * 7).AddHours(0)) + 1; // had to add one, looks like Util function was designed for parsing non-zero-indexed hours CalculateStats(dhrs, commonKeys, stat_hours, mask, hh, true); } SetOutputData(DA, stat_hours); break; case CType.Monthly: for (int mth = 0; mth < 12; mth++) { mask.maskByMonthOfYear(mth); int hh = Util.hourOfYearFromDatetime(Util.baseDatetime().AddMonths(mth).AddHours(0)) + 1; // had to add one, looks like Util function was designed for parsing non-zero-indexed hours CalculateStats(dhrs, commonKeys, stat_hours, mask, hh, true); } SetOutputData(DA, stat_hours); break; case CType.Yearly: mask.fillMask(true); // all hours may pass int hhh = Util.hourOfYearFromDatetime(Util.baseDatetime().AddMonths(6).AddDays(15).AddHours(0)) + 1; // had to add one, looks like Util function was designed for parsing non-zero-indexed hours CalculateStats(dhrs, commonKeys, stat_hours, mask, hhh, true); SetOutputData(DA, stat_hours); break; default: this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Time period option not yet implimented. Cannot produce statistics."); break; } } }