-
Notifications
You must be signed in to change notification settings - Fork 2
/
matio.formats.gen4.pas
144 lines (129 loc) · 4.92 KB
/
matio.formats.gen4.pas
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
unit matio.formats.gen4;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/matio
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
SysUtils, Types, Propset, ArrayBld, matio, matio.formats, matio.gen4;
Type
T4GMatrixReaderFormat = Class(TMatrixReaderFormat)
public
Function Format: String; override;
Function HasFormat(const Header: TBytes): Boolean; override;
public
Function CreateReader(const [ref] Properties: TPropertySet): TMatrixReader; override;
end;
T4GMatrixWriterFormat = Class(TMatrixWriterFormat)
private
Const
PrecisionProperty = 'prec';
CompressionProperty = 'compress';
CompressionOptions: array[T4GCompression] of String = ('none','gzip');
strict protected
Procedure AppendFormatProperties(const [ref] Properties: TPropertySet); override;
public
Function Format: String; override;
Function PropertyPickList(const PropertyName: string; out PickList: TStringDynArray): Boolean; override;
public
Function CreateWriter(const [ref] Properties: TPropertySet;
const FileLabel: string;
const MatrixLabels: array of String;
const Size: Integer): TMatrixWriter; overload; override;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Function T4GMatrixReaderFormat.Format: String;
begin
Result := '4g';
end;
Function T4GMatrixReaderFormat.HasFormat(const Header: TBytes): Boolean;
begin
if Length(Header) >= 4 then
if TEncoding.ASCII.GetString(Copy(Header,0,2)) = '4G' then
if (Header[2] = 20) and (Header[3] = 1) then
Result := true
else
Result := false
else
Result := false
else
Result := false;
end;
Function T4GMatrixReaderFormat.CreateReader(const [ref] Properties: TPropertySet): TMatrixReader;
begin
if SameText(Properties[FormatProperty],Format) then
Result := T4GMatrixReader.Create(Properties.ToPath(FileProperty))
else
raise Exception.Create('Invalid format-property');
end;
////////////////////////////////////////////////////////////////////////////////
Function T4GMatrixWriterFormat.Format: String;
begin
Result := '4g';
end;
Procedure T4GMatrixWriterFormat.AppendFormatProperties(const [ref] Properties: TPropertySet);
begin
Properties.Append(PrecisionProperty,PrecisionLabels[ftFloat32]);
Properties.Append(CompressionProperty,CompressionOptions[cpGZip]);
end;
Function T4GMatrixWriterFormat.PropertyPickList(const PropertyName: string;
out PickList: TStringDynArray): Boolean;
begin
if not inherited PropertyPickList(PropertyName,PickList) then
if SameText(PropertyName,PrecisionProperty) then
begin
Result := true;
PickList := TStringArrayBuilder.Create(PrecisionLabels);
end else
if SameText(PropertyName,CompressionProperty) then
begin
Result := true;
PickList := TStringArrayBuilder.Create(CompressionOptions);
end else
Result := false;
end;
Function T4GMatrixWriterFormat.CreateWriter(const [ref] Properties: TPropertySet;
const FileLabel: string;
const MatrixLabels: array of String;
const Size: Integer): TMatrixWriter;
Var
Precision: TFloatType;
Compression: T4GCompression;
begin
if SameText(Properties[FormatProperty],Format) then
begin
var ExtendedProperties := ExtendProperties(Properties);
// Set precision
var ValidPrecision := false;
var PrecisionPropertyValue := ExtendedProperties[PrecisionProperty];
for var Prec := low(PrecisionLabels) to high(PrecisionLabels) do
if SameText(PrecisionLabels[Prec],PrecisionPropertyValue) then
begin
Precision := Prec;
ValidPrecision := true;
Break;
end;
if not ValidPrecision then raise Exception.Create('Invalid precision');
// Set compression
var ValidCompression := false;
var CompressionProprtyValue := ExtendedProperties[CompressionProperty];
for var Compress := low(CompressionOptions) to high(CompressionOptions) do
if SameText(CompressionOptions[Compress],CompressionProprtyValue) then
begin
Compression := Compress;
ValidCompression := true;
Break;
end;
if not ValidCompression then raise Exception.Create('Invalid compression');
// Create writer
Result := T4GMatrixWriter.Create(ExtendedProperties.ToPath(FileProperty),FileLabel,MatrixLabels,Size,Precision,Compression);
end else
raise Exception.Create('Invalid format-property');
end;
end.