-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathRasterReader.cs
More file actions
50 lines (40 loc) · 1.51 KB
/
RasterReader.cs
File metadata and controls
50 lines (40 loc) · 1.51 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
using System.Buffers.Text;
namespace TestAAIGridReader
{
internal class Program
{
private static void Main()
{
Span<double> raster = stackalloc double[15];
GetRasterArray(@"F:\f.txt", ref raster);
}
private static void GetRasterArray(string path, ref Span<double> raster)
{
using FileStream fileStream = File.OpenRead(path);
long streamLength = fileStream.Length;
Span<byte> buffer = stackalloc byte[(int)Math.Min(4096L, streamLength)];
int r = 0;
int bufferLength;
while ((bufferLength = fileStream.Read(buffer)) != 0)
for (int i = 0; i < bufferLength; i++)
{
byte chr;
while (((chr = buffer[i]) == 32 || (chr > 8 && chr < 14)) && ++i < bufferLength) { }
if (!Utf8Parser.TryParse(buffer[i..bufferLength], out double value, out int bytesConsumed))
continue;
if ((i += bytesConsumed) == bufferLength)
{
if (fileStream.Position == streamLength)
{
raster[r] = value;
return;
}
fileStream.Seek(-bytesConsumed, SeekOrigin.Current);
break;
}
raster[r++] = value;
}
return;
}
}
}