/*! * \brief Get value for a given date and find pattern for best matching sum * * \param date The date to get data for. Only month and day of month are extracted * \param sum needed to find best match of monthly rainPattern sum. * * \return The values if available for requested date, else null is returned. */ public Double?getValue(DateTime date, Double sum) { if (rainPatternData == null) { return(null); } SortedDictionary <Double, RainPatternValues> rainPatternList; rainPatternData.TryGetValue(date.Month, out rainPatternList); if (rainPatternList == null) { return(null); } //returns 0..n for exact match //< 0 is the negative index of last smaller int bestMatchIndex = keys[date.Month].BinarySearch(sum); //exact match if (bestMatchIndex >= 0) { return(rainPatternList[keys[date.Month][bestMatchIndex]].pattern[date.Day - 1]); } else { RainPatternValues nextBigger = rainPatternList[keys[date.Month][~bestMatchIndex >= keys[date.Month].Count ? ~bestMatchIndex - 1 : ~bestMatchIndex]]; RainPatternValues lastSmaller = rainPatternList[keys[date.Month][~bestMatchIndex == 0 ? 0 : ~bestMatchIndex - 1]]; if (Math.Abs(nextBigger.sum - sum) < Math.Abs(lastSmaller.sum - sum)) { return(nextBigger.pattern[date.Day - 1] * sum); } return(lastSmaller.pattern[date.Day - 1] * sum); } }
/*! * \brief Add values for a given month and sum. * * \param month The when rainPattern occurs * \param sum The sum of this particular month sample * \param values The rainPattern values. * * \return number of datasets. */ public int addValues(int month, Double sum, RainPatternValues values) { if (!rainPatternData.ContainsKey(month)) { rainPatternData[month] = new SortedDictionary <double, RainPatternValues>(); } rainPatternData[month][sum] = values; keys[month] = new List <Double>(rainPatternData[month].Keys); return(rainPatternData.Count); }
/*! * \brief Clone Constructor. * * \param rainPatternValues The original rainPattern values to clone. */ public RainPatternValues(RainPatternValues rainPatternValues) { //use this for .net 4.0 //foreach (PropertyInfo pi in this.GetType().GetProperties()) //use this for .net 4.5 foreach (PropertyInfo pi in this.GetType().GetRuntimeProperties()) { if (rainPatternValues.GetType().GetRuntimeProperty(pi.Name) == null) { continue; } pi.SetValue(this, rainPatternValues.GetType().GetRuntimeProperty(pi.Name).GetValue(rainPatternValues, null), null); } }
private int loadCsv(Stream stream) { try { _e = null; CsvReader csvReader = new CsvReader(stream); while (!csvReader.EndOfStream()) { IDictionary <String, String> fields; fields = csvReader.readLine(); if (fields == null || ( (!fields.ContainsKey("dataObjName") || String.IsNullOrEmpty(fields["dataObjName"])) && (!fields.ContainsKey("_id") || String.IsNullOrEmpty(fields["_id"])) )) { continue; } if (String.IsNullOrEmpty(_name)) { _name = fields.ContainsKey("dataObjName") ? fields["dataObjName"] : fields["_id"]; } if (fields.ContainsKey("dataObjId") && String.IsNullOrEmpty(_dataObjId) && !String.IsNullOrEmpty(fields["dataObjId"])) { _dataObjId = fields["dataObjId"]; } RainPatternValues values = new RainPatternValues(); values.parseData(fields, _cultureInfo == null ? CultureInfo.InvariantCulture : _cultureInfo); //month and sum are required if (!fields.ContainsKey("month") || !fields.ContainsKey("sum")) { continue; } addValues(values.month, values.sum, values); } return(rainPatternData.Count); } catch (Exception e) { _e = e; return(0); } }