예제 #1
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List<int> hrs = new List<int>();
            List<string> keys = new List<string>();
            Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_Number> valtree = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.GH_Number>();
            if ((DA.GetDataList(1, keys)) && (DA.GetDataTree(2, out valtree)) && (DA.GetDataList(0, hrs)))
            {

                bool has_color = false;
                List<Color> colors = new List<Color>();
                has_color = DA.GetDataList(4, colors);

                bool has_pt = false;
                List<Point3d> points = new List<Point3d>();
                has_pt = DA.GetDataList(4, points);

                if (hrs.Count != valtree.Branches.Count) this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "List matching error.  Hours and Vals must match.  If you pass in more than one hour number, then you must pass in a tree of values with one branch per hour number, and vice-versa.");
                else foreach (List<GH_Number> branch in valtree.Branches) if (keys.Count != branch.Count) this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "List matching error.  Keys and Vals must offer lists of the same length.  If you pass in a tree of values, each branch must contain a list of the same length as the list of keys.");
                        else
                        {
                            if (((has_color) && (colors.Count != hrs.Count)) || ((has_pt) && (points.Count != hrs.Count)))
                            {
                                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "List matching error.");
                                return;
                            }
                            List<DHr> hours = new List<DHr>();
                            for (int n = 0; n < valtree.Branches.Count; n++)
                            {
                                DHr hr = new DHr(hrs[n]);
                                for (int m = 0; m < keys.Count; m++) hr.put(keys[m], (float)valtree.Branches[n][m].Value);
                                if (has_color) hr.color = colors[n];
                                if (has_pt) hr.pos = points[n];
                                hours.Add(hr);
                            }
                            DA.SetDataList(0, hours);
                        }
            }
        }
예제 #2
0
        private void CalculateStats(List<DHr> dhrs, string[] keys, Dictionary<string, List<DHr>> stat_hours_dict, HourMask mask, int assigned_hour_of_year, bool calculate_mode = false)
        {
            Dictionary<string, List<float>> value_dict = new Dictionary<string, List<float>>();
            foreach (string key in keys) { value_dict.Add(key, new List<float>()); }
            int count = 0;
            List<int> a = new List<int>();
            List<int> r = new List<int>();
            List<int> g = new List<int>();
            List<int> b = new List<int>();
            foreach (DHr hour in dhrs) {
                if (mask.eval(hour)) {
                    count++;
                    a.Add(hour.color.A);
                    r.Add(hour.color.R);
                    g.Add(hour.color.G);
                    b.Add(hour.color.B);
                    foreach (string key in keys) { value_dict[key].Add(hour.val(key)); }
                }
            }
            Color c = Color.FromArgb((int)a.Average(), (int)r.Average(), (int)g.Average(), (int)b.Average());

            DHr meanHr = new DHr(assigned_hour_of_year); meanHr.is_surrogate = true; meanHr.color = c;
            DHr modeHr = new DHr(assigned_hour_of_year); modeHr.is_surrogate = true; modeHr.color = c;
            DHr highHr = new DHr(assigned_hour_of_year); highHr.is_surrogate = true; highHr.color = c;
            DHr uqHr = new DHr(assigned_hour_of_year); uqHr.is_surrogate = true; uqHr.color = c;
            DHr medianHr = new DHr(assigned_hour_of_year); medianHr.is_surrogate = true; medianHr.color = c;
            DHr lqHr = new DHr(assigned_hour_of_year); lqHr.is_surrogate = true; lqHr.color = c;
            DHr lowHr = new DHr(assigned_hour_of_year); lowHr.is_surrogate = true; lowHr.color = c;
            DHr sumHr = new DHr(assigned_hour_of_year); sumHr.is_surrogate = true; sumHr.color = c;

            if (calculate_mode) {
                foreach (string key in keys) {
                    value_dict[key].Sort();
                    meanHr.put(key, value_dict[key].Mean());
                    highHr.put(key, value_dict[key][value_dict[key].Count - 1]);
                    uqHr.put(key, value_dict[key].Quartile(0.75f));
                    medianHr.put(key, value_dict[key].Median());
                    lqHr.put(key, value_dict[key].Quartile(0.25f));
                    lowHr.put(key, value_dict[key][0]);
                    sumHr.put(key, value_dict[key].Sum());

                    List<float> modes = value_dict[key].Modes().ToList<float>();
                    //if (modes.Count > 1) this.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, String.Format("Multiple values associated with the key '{0}' occur equally often, resulting in multiple Mode values.  I've returned the first mode encountered", key));
                    if (modes.Count >= 1) modeHr.put(key, modes[0]);
                    else {
                        //this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, String.Format("Each value associated with the key '{0}' is unique.  Unable to calculate Mode", key));
                        modeHr.put(key, MDHr.INVALID_VAL);
                    }
                }
            }
            stat_hours_dict["meanHrs"].Add(meanHr);
            if (calculate_mode) stat_hours_dict["modeHrs"].Add(modeHr);
            stat_hours_dict["highHrs"].Add(highHr);
            stat_hours_dict["uqHrs"].Add(uqHr);
            stat_hours_dict["medianHrs"].Add(medianHr);
            stat_hours_dict["lqHrs"].Add(lqHr);
            stat_hours_dict["lowHrs"].Add(lowHr);
            stat_hours_dict["sumHrs"].Add(sumHr);
        }
예제 #3
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List<DHr> dhrs = new List<DHr>();
            int scope = -1;
            if ((DA.GetDataList(0, dhrs)) && (DA.GetData(1, ref scope))) {
                string[] commonKeys = DHr.commonkeys(dhrs.ToArray());

                List<DHr> hrsOut = new List<DHr>();
                for (int n = 0; n < dhrs.Count; n++) {
                    DHr dhr = new DHr(dhrs[n]);
                    foreach (string key in commonKeys) {
                        float sum = 0;
                        for (int di = -scope / 2; di <= scope / 2; di++) {
                            int m = n + di;
                            if (m < 0) m = dhrs.Count + m;
                            if (m > dhrs.Count - 1) m = m - dhrs.Count;
                            sum += dhrs[m].val(key);
                        }
                        sum = sum / ((float)scope + 1);
                        dhr.put(key, sum);
                    }
                    hrsOut.Add(dhr);
                }

                DA.SetDataList(0, hrsOut);
            }
        }
예제 #4
0
 protected override void SolveInstance(IGH_DataAccess DA)
 {
     DHr hrIn = new DHr();
     if (DA.GetData(1, ref hrIn)) {
         DHr hrOut = new DHr(hrIn);
         hrOut.clear();
         List<string> keys_to_keep = new List<string>();
         DA.GetDataList(0, keys_to_keep);
         foreach (string key in keys_to_keep) {
             if (hrIn.containsKey(key)) hrOut.put(key, hrIn.val(key));
             else this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "key not found in given hour: " + key + "\nIf you are streaming from a panel component did you remember to uncheck the 'multiline data' option?");
         }
         DA.SetData(0, hrOut);
     }
 }
예제 #5
0
        private bool ParseFilepath()
        {
            if (!FilepathValid()) { return false; }
            col_mapping = new Dictionary<string, Dictionary<string, int>>();

            // get a list of the column headers
            List<string> headers;
            using (StreamReader headreader = new StreamReader(new FileStream(filepath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))) { headers = new List<string>(headreader.ReadLine().Split(',')); }

            for (int n = 0; n < headers.Count; n++)
            {
                if (headers[n].Contains(":"))
                {
                    // process zone name
                    string zone_name = headers[n].Split(':')[0].Trim();
                    zone_name = zone_name.ToLower();
                    zone_name = DHr.cleankey(zone_name);

                    // process col name
                    string col_name = headers[n].Split(':')[1];
                    int offset = col_name.IndexOf("(");
                    if (offset >= 0)
                        col_name = col_name.Substring(0, offset);
                    col_name = col_name.Trim();
                    col_name = col_name.ToLower();
                    col_name = DHr.cleankey(col_name);

                    // register this column header in our zone_dict
                    if (!col_mapping.ContainsKey(zone_name)) { col_mapping.Add(zone_name, new Dictionary<string, int>()); }
                    col_mapping[zone_name].Add(col_name, n);
                }
            }

            #region //CLEAN COLUMN HEADERS
            // eplus does some stupid stuff with the way it names column headers.  let's see if we can fix that.
            // our col_mapping dict may have some duplicate entries such as "lower_zone" and then later "lower_zone lights2"
            // remove "lower_zone lights2", and then move all the entries in into "lower_zone"

            List<string> zonenames_to_delete = new List<string>();
            foreach (KeyValuePair<string, Dictionary<string, int>> entry in col_mapping)
            {
                string this_zonename = entry.Key;
                foreach (string that_zonename in col_mapping.Keys)
                {
                    if (this_zonename.Contains(that_zonename) && (!this_zonename.Equals(that_zonename, StringComparison.OrdinalIgnoreCase)))
                    {
                        // we've found a duplicate.  copy over all the entries, with the keys prepended by this zonename
                        char[] charsToTrim = { '_', ' ', ':'};
                        string prefix = this_zonename.Replace(that_zonename, "").Trim(charsToTrim);
                        foreach (KeyValuePair<string, int> mapping in entry.Value) { col_mapping[that_zonename].Add(prefix + " : " + mapping.Key, mapping.Value); }
                        zonenames_to_delete.Add(this_zonename);
                        break;
                    }
                }
            }
            foreach (string key in zonenames_to_delete) { col_mapping.Remove(key); }
            #endregion

            zone_hours = new Dictionary<string, List<DHr>>();
            foreach (string key in col_mapping.Keys) { zone_hours.Add(key, new List<DHr>()); } // empty list of hours for each zone

            StreamReader reader = new StreamReader(new FileStream(filepath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite));
            string line;
            int lnum = 0;
            bool sizing_complete = false;
            while ((line = reader.ReadLine()) != null)
            {
                if (lnum >= header_lines)
                {
                    string[] linedata = line.Split(',');

                    DateTime dt = Util.baseDatetime();
                    string[] dt_strings = linedata[0].Trim().Split(new char[] {' '},StringSplitOptions.RemoveEmptyEntries);
                    if (!DateTime.TryParse(dt_strings[0] + "/" + Util.defaultYear, out dt)) // we're adding the default year here
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "could not parse datetime on line " + lnum);
                        return false;
                    }
                    int h = 0;
                    if (Int32.TryParse(dt_strings[1].Split(':')[0],out h)){dt = dt.AddHours(h);} else {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "could not parse datetime on line " + lnum);
                        return false;
                    }
                    int hourOfYear = Util.hourOfYearFromDatetime(dt);
                    if ((hourOfYear > 8759) || (hourOfYear < 0)) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Found an odd-looking datetime on line " + lnum); }
                    if ((!sizing_complete) && (hourOfYear == 0)) { sizing_complete = true; }  // check if the two-day sizing period is done with
                    if (sizing_complete)
                    {
                        foreach (KeyValuePair<string, Dictionary<string, int>> column_dict in col_mapping)
                        {
                            DHr hr = new DHr(hourOfYear);
                            try
                            {
                                foreach (KeyValuePair<string, int> column in column_dict.Value)
                                {
                                    hr.put(column.Key, float.Parse(linedata[column.Value]));
                                }
                            }
                            catch
                            {
                                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "parse error on line " + lnum);
                                return false;
                            }
                            zone_hours[column_dict.Key].Add(hr);
                        }
                    }
                }
                lnum++;
            }
            return true;
        }