|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace TileDB.CSharp.Examples |
| 6 | +{ |
| 7 | + public static class ExampleDimensionLabels |
| 8 | + { |
| 9 | + private static readonly string ArrayPath = ExampleUtil.MakeExamplePath("dimension-labels"); |
| 10 | + private static readonly Context Ctx = Context.GetDefault(); |
| 11 | + |
| 12 | + private static void CreateArray() |
| 13 | + { |
| 14 | + using var xIndex = Dimension.Create(Ctx, "x_index", 0, 5, 6); |
| 15 | + using var sample = Dimension.Create(Ctx, "sample", 0, 3, 4); |
| 16 | + |
| 17 | + using var domain = new Domain(Ctx); |
| 18 | + domain.AddDimensions(xIndex, sample); |
| 19 | + |
| 20 | + using var a = Attribute.Create<short>(Ctx, "a"); |
| 21 | + |
| 22 | + using var schema = new ArraySchema(Ctx, ArrayType.Dense); |
| 23 | + schema.SetCellOrder(LayoutType.RowMajor); |
| 24 | + schema.SetTileOrder(LayoutType.RowMajor); |
| 25 | + schema.SetDomain(domain); |
| 26 | + schema.AddAttribute(a); |
| 27 | + schema.AddDimensionLabel(0, "x", DataOrder.Increasing, DataType.Float64); |
| 28 | + schema.AddDimensionLabel(0, "y", DataOrder.Increasing, DataType.Float64); |
| 29 | + schema.AddDimensionLabel(1, "timestamp", DataOrder.Increasing, DataType.DateTimeSecond); |
| 30 | + |
| 31 | + Array.Create(Ctx, ArrayPath, schema); |
| 32 | + } |
| 33 | + |
| 34 | + private static void WriteArrayAndLabels() |
| 35 | + { |
| 36 | + short[] a = Enumerable.Range(1, 24).Select(x => (short)x).ToArray(); |
| 37 | + double[] x = { -1.0, -0.6, -0.2, 0.2, 0.6, 1.0 }; |
| 38 | + double[] y = { 0.0, 2.0, 4.0, 6.0, 8.0, 10.0 }; |
| 39 | + long[] timestamp = { 31943, 32380, 33131, 33228 }; |
| 40 | + |
| 41 | + using Array array = new Array(Ctx, ArrayPath); |
| 42 | + array.Open(QueryType.Write); |
| 43 | + |
| 44 | + using Query query = new Query(Ctx, array); |
| 45 | + query.SetLayout(LayoutType.RowMajor); |
| 46 | + query.SetDataBuffer("a", a); |
| 47 | + query.SetDataBuffer("x", x); |
| 48 | + query.SetDataBuffer("y", y); |
| 49 | + query.SetDataBuffer("timestamp", timestamp); |
| 50 | + |
| 51 | + query.Submit(); |
| 52 | + |
| 53 | + if (query.Status() != QueryStatus.Completed) |
| 54 | + { |
| 55 | + throw new Exception("Write query did not complete."); |
| 56 | + } |
| 57 | + |
| 58 | + array.Close(); |
| 59 | + } |
| 60 | + |
| 61 | + private static void ReadArrayAndLabels() |
| 62 | + { |
| 63 | + Console.WriteLine("Read from main array"); |
| 64 | + |
| 65 | + using var array = new Array(Ctx, ArrayPath); |
| 66 | + array.Open(QueryType.Read); |
| 67 | + |
| 68 | + using var subarray = new Subarray(array); |
| 69 | + subarray.AddRange(0, 1, 2); |
| 70 | + subarray.AddRange(1, 0, 2); |
| 71 | + |
| 72 | + short[] a = new short[6]; |
| 73 | + double[] x = new double[2]; |
| 74 | + double[] y = new double[2]; |
| 75 | + long[] timestamp = new long[3]; |
| 76 | + |
| 77 | + using var query = new Query(Ctx, array); |
| 78 | + query.SetLayout(LayoutType.RowMajor); |
| 79 | + query.SetSubarray(subarray); |
| 80 | + query.SetDataBuffer("a", a); |
| 81 | + query.SetDataBuffer("x", x); |
| 82 | + query.SetDataBuffer("y", y); |
| 83 | + query.SetDataBuffer("timestamp", timestamp); |
| 84 | + |
| 85 | + query.Submit(); |
| 86 | + |
| 87 | + if (query.Status() != QueryStatus.Completed) |
| 88 | + { |
| 89 | + throw new Exception("Read query did not complete."); |
| 90 | + } |
| 91 | + |
| 92 | + for (int i = 0; i < 2; i++) |
| 93 | + { |
| 94 | + for (int j = 0; j < 3; j++) |
| 95 | + { |
| 96 | + int x_val = i + 1; |
| 97 | + int sample_val = j; |
| 98 | + Console.WriteLine($"Cell ({x_val}, {sample_val})"); |
| 99 | + Console.WriteLine($" * a({x_val}, {sample_val}) = {a[3 * i + j]}"); |
| 100 | + Console.WriteLine($" * x({x_val}) = {x[i]}"); |
| 101 | + Console.WriteLine($" * y({x_val}) = {y[i]}"); |
| 102 | + Console.WriteLine($" * timestamp({sample_val}) = {TimeSpan.FromSeconds(timestamp[j])}"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + array.Close(); |
| 107 | + } |
| 108 | + |
| 109 | + private static void ReadTimestampData() |
| 110 | + { |
| 111 | + Console.WriteLine("Read from dimension label"); |
| 112 | + |
| 113 | + using var array = new Array(Ctx, ArrayPath); |
| 114 | + array.Open(QueryType.Read); |
| 115 | + |
| 116 | + using var subarray = new Subarray(array); |
| 117 | + subarray.AddRange(1, 1, 3); |
| 118 | + |
| 119 | + long[] timestamp = new long[3]; |
| 120 | + |
| 121 | + using var query = new Query(Ctx, array); |
| 122 | + query.SetLayout(LayoutType.RowMajor); |
| 123 | + query.SetSubarray(subarray); |
| 124 | + query.SetDataBuffer("timestamp", timestamp); |
| 125 | + |
| 126 | + query.Submit(); |
| 127 | + |
| 128 | + if (query.Status() != QueryStatus.Completed) |
| 129 | + { |
| 130 | + throw new Exception("Read query did not complete."); |
| 131 | + } |
| 132 | + |
| 133 | + for (int i = 0; i < 3; i++) |
| 134 | + { |
| 135 | + int sample_val = i + 1; |
| 136 | + Console.WriteLine($"Cell ({sample_val})"); |
| 137 | + Console.WriteLine($" * timestamp({sample_val}) = {TimeSpan.FromSeconds(timestamp[i])}"); |
| 138 | + } |
| 139 | + |
| 140 | + array.Close(); |
| 141 | + } |
| 142 | + |
| 143 | + private static void ReadArrayByLabel() |
| 144 | + { |
| 145 | + Console.WriteLine("Read array from label ranges"); |
| 146 | + |
| 147 | + using var array = new Array(Ctx, ArrayPath); |
| 148 | + array.Open(QueryType.Read); |
| 149 | + |
| 150 | + using var subarray = new Subarray(array); |
| 151 | + subarray.AddLabelRange("y", 3.0, 8.0); |
| 152 | + subarray.AddLabelRange<long>("timestamp", 31943, 32380); |
| 153 | + |
| 154 | + short[] a = new short[6]; |
| 155 | + double[] y = new double[3]; |
| 156 | + long[] timestamp = new long[2]; |
| 157 | + |
| 158 | + using var query = new Query(Ctx, array); |
| 159 | + query.SetLayout(LayoutType.RowMajor); |
| 160 | + query.SetSubarray(subarray); |
| 161 | + query.SetDataBuffer("y", y); |
| 162 | + query.SetDataBuffer("timestamp", timestamp); |
| 163 | + query.SetDataBuffer("a", a); |
| 164 | + |
| 165 | + query.Submit(); |
| 166 | + |
| 167 | + if (query.Status() != QueryStatus.Completed) |
| 168 | + { |
| 169 | + throw new Exception("Read query did not complete."); |
| 170 | + } |
| 171 | + |
| 172 | + for (int i = 0; i < 3; i++) |
| 173 | + { |
| 174 | + for (int j = 0; j < 2; j++) |
| 175 | + { |
| 176 | + Console.WriteLine($"Cell ({y[i]}, {TimeSpan.FromSeconds(timestamp[j])})"); |
| 177 | + Console.WriteLine($" * a = {a[2 * i + j]}"); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + array.Close(); |
| 182 | + } |
| 183 | + |
| 184 | + public static void Run() |
| 185 | + { |
| 186 | + if (Directory.Exists(ArrayPath)) |
| 187 | + { |
| 188 | + Directory.Delete(ArrayPath, true); |
| 189 | + } |
| 190 | + |
| 191 | + CreateArray(); |
| 192 | + WriteArrayAndLabels(); |
| 193 | + ReadArrayAndLabels(); |
| 194 | + Console.WriteLine(); |
| 195 | + ReadTimestampData(); |
| 196 | + Console.WriteLine(); |
| 197 | + ReadArrayByLabel(); |
| 198 | + Console.WriteLine(); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments