| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Xml; |
| | 5 | |
|
| | 6 | | namespace Morpho25.Utility |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Util class. |
| | 10 | | /// </summary> |
| | 11 | | public static class Util |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// From °C to Kelvin. |
| | 15 | | /// </summary> |
| | 16 | | public const double TO_KELVIN = 273.15; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Accumulate list of double. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="sequence">List of double.</param> |
| | 22 | | /// <returns>New list of value with partial sums.</returns> |
| | 23 | | public static IEnumerable<double> Accumulate(IEnumerable<double> sequence) |
| 22 | 24 | | { |
| 22 | 25 | | double sum = 0; |
| 1296 | 26 | | foreach (var item in sequence) |
| 615 | 27 | | { |
| 615 | 28 | | sum += item; |
| 615 | 29 | | yield return sum; |
| 615 | 30 | | } |
| 22 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Divide a list into chunks. |
| | 35 | | /// </summary> |
| | 36 | | /// <typeparam name="T">Type to use</typeparam> |
| | 37 | | /// <param name="source">List to transform.</param> |
| | 38 | | /// <param name="chunkSize">Size of the chunk.</param> |
| | 39 | | /// <returns>List of chunks.</returns> |
| | 40 | | public static List<List<T>> ChunkBy<T>(this List<T> source, |
| | 41 | | int chunkSize) |
| 0 | 42 | | { |
| 0 | 43 | | return source |
| 0 | 44 | | .Select((x, i) => new { Index = i, Value = x }) |
| 0 | 45 | | .GroupBy(x => x.Index / chunkSize) |
| 0 | 46 | | .Select(x => x.Select(v => v.Value).ToList()) |
| 0 | 47 | | .ToList(); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Convert a cell to number. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="cell">Cell to convert.</param> |
| | 54 | | /// <returns>List of values.</returns> |
| | 55 | | public static List<double> ConvertToNumber(string cell) |
| 0 | 56 | | { |
| 0 | 57 | | var spacing = cell.Split(',') |
| 0 | 58 | | .Select(v => Convert.ToDouble(v)) |
| 0 | 59 | | .ToList(); |
| | 60 | |
|
| 0 | 61 | | return spacing; |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Return a sublist of an array using upper and lower limits. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="sequence">List to use.</param> |
| | 68 | | /// <param name="upperLimit">Upper limit.</param> |
| | 69 | | /// <param name="lowerLimit">Lower limit.</param> |
| | 70 | | /// <returns>New list of values.</returns> |
| | 71 | | public static IEnumerable<double> FilterByMinMax(double[] sequence, |
| | 72 | | double upperLimit, double lowerLimit) |
| 0 | 73 | | { |
| 0 | 74 | | List<double> values = new List<double>(); |
| | 75 | |
|
| 0 | 76 | | foreach (double value in sequence) |
| 0 | 77 | | { |
| 0 | 78 | | if (value < upperLimit && value > lowerLimit) |
| 0 | 79 | | values.Add(value); |
| 0 | 80 | | } |
| | 81 | |
|
| 0 | 82 | | return values; |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Using an array get the index of the closest value |
| | 87 | | /// by a number. |
| | 88 | | /// </summary> |
| | 89 | | /// <param name="sequence">Array to use.</param> |
| | 90 | | /// <param name="number">Number to use to get the |
| | 91 | | /// closest value.</param> |
| | 92 | | /// <returns>Return the index of the closest value.</returns> |
| | 93 | | public static int ClosestValue(double[] sequence, |
| | 94 | | double number) |
| 0 | 95 | | { |
| 0 | 96 | | double value = sequence.Aggregate((a, b) => Math.Abs( |
| 0 | 97 | | a - number) < Math.Abs(b - number) ? a : b); |
| 0 | 98 | | return Array.IndexOf(sequence, value); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Utility method to create envimet XML part. |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="w">XML writer.</param> |
| | 105 | | /// <param name="sectionTitle">Title of the section.</param> |
| | 106 | | /// <param name="tags">Tags of the section.</param> |
| | 107 | | /// <param name="values">Valued of the section.</param> |
| | 108 | | /// <param name="flag">0, 1, 2 for section type.</param> |
| | 109 | | /// <param name="attributes">Attributes of the root.</param> |
| | 110 | | public static void CreateXmlSection(XmlTextWriter w, |
| | 111 | | string sectionTitle, string[] tags, string[] values, |
| | 112 | | int flag, string[] attributes) |
| 0 | 113 | | { |
| 0 | 114 | | w.WriteStartElement(sectionTitle); |
| 0 | 115 | | w.WriteString("\n "); |
| 0 | 116 | | foreach (var item in tags.Zip(values, (a, b) => new { A = a, B = b })) |
| 0 | 117 | | { |
| 0 | 118 | | if (flag == 0) |
| 0 | 119 | | { |
| 0 | 120 | | w.WriteStartElement(item.A); |
| 0 | 121 | | w.WriteString(item.B); |
| 0 | 122 | | w.WriteEndElement(); |
| 0 | 123 | | } |
| 0 | 124 | | else if (flag == 1) |
| 0 | 125 | | { |
| 0 | 126 | | w.WriteStartElement(item.A); |
| 0 | 127 | | w.WriteAttributeString("", "type", null, attributes[0]); |
| 0 | 128 | | w.WriteAttributeString("", "dataI", null, attributes[1]); |
| 0 | 129 | | w.WriteAttributeString("", "dataJ", null, attributes[2]); |
| 0 | 130 | | w.WriteString(item.B); |
| 0 | 131 | | w.WriteEndElement(); |
| 0 | 132 | | } |
| | 133 | | else |
| 0 | 134 | | { |
| 0 | 135 | | w.WriteStartElement(item.A); |
| 0 | 136 | | w.WriteAttributeString("", "type", null, attributes[0]); |
| 0 | 137 | | w.WriteAttributeString("", "dataI", null, attributes[1]); |
| 0 | 138 | | w.WriteAttributeString("", "dataJ", null, attributes[2]); |
| 0 | 139 | | w.WriteAttributeString("", "zlayers", null, attributes[3]); |
| 0 | 140 | | w.WriteAttributeString("", "defaultValue", null, attributes[4]); |
| 0 | 141 | | w.WriteString(item.B); |
| 0 | 142 | | w.WriteEndElement(); |
| 0 | 143 | | } |
| 0 | 144 | | w.WriteString("\n "); |
| | 145 | |
|
| 0 | 146 | | } |
| 0 | 147 | | w.WriteEndElement(); |
| 0 | 148 | | w.WriteString("\n"); |
| 0 | 149 | | } |
| | 150 | | } |
| | 151 | | } |