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