@loaders.gl/scientific 0.0.0-stage → 5.0.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/README.md +21 -2
  2. package/dist/index.cjs +4313 -0
  3. package/dist/index.cjs.map +6 -0
  4. package/dist/index.d.ts +11 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +8 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/netcdf/bundled.cjs +1394 -0
  9. package/dist/netcdf/bundled.d.ts +2 -0
  10. package/dist/netcdf/bundled.d.ts.map +1 -0
  11. package/dist/netcdf/bundled.js +5 -0
  12. package/dist/netcdf/bundled.js.map +1 -0
  13. package/dist/netcdf/index.cjs +1394 -0
  14. package/dist/netcdf/index.cjs.map +6 -0
  15. package/dist/netcdf/index.d.ts +7 -0
  16. package/dist/netcdf/index.d.ts.map +1 -0
  17. package/dist/netcdf/index.js +8 -0
  18. package/dist/netcdf/index.js.map +1 -0
  19. package/dist/netcdf/iobuffer/iobuffer.d.ts +253 -0
  20. package/dist/netcdf/iobuffer/iobuffer.d.ts.map +1 -0
  21. package/dist/netcdf/iobuffer/iobuffer.js +450 -0
  22. package/dist/netcdf/iobuffer/iobuffer.js.map +1 -0
  23. package/dist/netcdf/netcdf-format.cjs +39 -0
  24. package/dist/netcdf/netcdf-format.d.ts +11 -0
  25. package/dist/netcdf/netcdf-format.d.ts.map +1 -0
  26. package/dist/netcdf/netcdf-format.js +14 -0
  27. package/dist/netcdf/netcdf-format.js.map +1 -0
  28. package/dist/netcdf/netcdf-loader-with-parser.cjs +995 -0
  29. package/dist/netcdf/netcdf-loader-with-parser.d.ts +60 -0
  30. package/dist/netcdf/netcdf-loader-with-parser.d.ts.map +1 -0
  31. package/dist/netcdf/netcdf-loader-with-parser.js +35 -0
  32. package/dist/netcdf/netcdf-loader-with-parser.js.map +1 -0
  33. package/dist/netcdf/netcdf-loader.cjs +84 -0
  34. package/dist/netcdf/netcdf-loader.d.ts +81 -0
  35. package/dist/netcdf/netcdf-loader.d.ts.map +1 -0
  36. package/dist/netcdf/netcdf-loader.js +41 -0
  37. package/dist/netcdf/netcdf-loader.js.map +1 -0
  38. package/dist/netcdf/netcdf-source-loader-types.cjs +78 -0
  39. package/dist/netcdf/netcdf-source-loader-types.d.ts +28 -0
  40. package/dist/netcdf/netcdf-source-loader-types.d.ts.map +1 -0
  41. package/dist/netcdf/netcdf-source-loader-types.js +32 -0
  42. package/dist/netcdf/netcdf-source-loader-types.js.map +1 -0
  43. package/dist/netcdf/netcdf-source-loader.cjs +1319 -0
  44. package/dist/netcdf/netcdf-source-loader.d.ts +59 -0
  45. package/dist/netcdf/netcdf-source-loader.d.ts.map +1 -0
  46. package/dist/netcdf/netcdf-source-loader.js +421 -0
  47. package/dist/netcdf/netcdf-source-loader.js.map +1 -0
  48. package/dist/netcdf/netcdfjs/netcdf-reader.d.ts +68 -0
  49. package/dist/netcdf/netcdfjs/netcdf-reader.d.ts.map +1 -0
  50. package/dist/netcdf/netcdfjs/netcdf-reader.js +172 -0
  51. package/dist/netcdf/netcdfjs/netcdf-reader.js.map +1 -0
  52. package/dist/netcdf/netcdfjs/netcdf-types.d.ts +70 -0
  53. package/dist/netcdf/netcdfjs/netcdf-types.d.ts.map +1 -0
  54. package/dist/netcdf/netcdfjs/netcdf-types.js +3 -0
  55. package/dist/netcdf/netcdfjs/netcdf-types.js.map +1 -0
  56. package/dist/netcdf/netcdfjs/read-data.d.ts +19 -0
  57. package/dist/netcdf/netcdfjs/read-data.d.ts.map +1 -0
  58. package/dist/netcdf/netcdfjs/read-data.js +51 -0
  59. package/dist/netcdf/netcdfjs/read-data.js.map +1 -0
  60. package/dist/netcdf/netcdfjs/read-header.d.ts +16 -0
  61. package/dist/netcdf/netcdfjs/read-header.d.ts.map +1 -0
  62. package/dist/netcdf/netcdfjs/read-header.js +227 -0
  63. package/dist/netcdf/netcdfjs/read-header.js.map +1 -0
  64. package/dist/netcdf/netcdfjs/read-type.d.ts +36 -0
  65. package/dist/netcdf/netcdfjs/read-type.d.ts.map +1 -0
  66. package/dist/netcdf/netcdfjs/read-type.js +135 -0
  67. package/dist/netcdf/netcdfjs/read-type.js.map +1 -0
  68. package/dist/netcdf/unbundled.cjs +1394 -0
  69. package/dist/netcdf/unbundled.d.ts +2 -0
  70. package/dist/netcdf/unbundled.d.ts.map +1 -0
  71. package/dist/netcdf/unbundled.js +5 -0
  72. package/dist/netcdf/unbundled.js.map +1 -0
  73. package/dist/netcdf.dev.js +3153 -0
  74. package/dist/netcdf.min.js +12 -0
  75. package/dist/zarr/bundled.d.ts +2 -0
  76. package/dist/zarr/bundled.d.ts.map +1 -0
  77. package/dist/zarr/bundled.js +5 -0
  78. package/dist/zarr/bundled.js.map +1 -0
  79. package/dist/zarr/geo-zarr-source-loader.d.ts +121 -0
  80. package/dist/zarr/geo-zarr-source-loader.d.ts.map +1 -0
  81. package/dist/zarr/geo-zarr-source-loader.js +692 -0
  82. package/dist/zarr/geo-zarr-source-loader.js.map +1 -0
  83. package/dist/zarr/index.cjs +14747 -0
  84. package/dist/zarr/index.cjs.map +6 -0
  85. package/dist/zarr/index.d.ts +11 -0
  86. package/dist/zarr/index.d.ts.map +1 -0
  87. package/dist/zarr/index.js +10 -0
  88. package/dist/zarr/index.js.map +1 -0
  89. package/dist/zarr/lib/consolidated-zarr.d.ts +31 -0
  90. package/dist/zarr/lib/consolidated-zarr.d.ts.map +1 -0
  91. package/dist/zarr/lib/consolidated-zarr.js +113 -0
  92. package/dist/zarr/lib/consolidated-zarr.js.map +1 -0
  93. package/dist/zarr/lib/load-zarr.d.ts +11 -0
  94. package/dist/zarr/lib/load-zarr.d.ts.map +1 -0
  95. package/dist/zarr/lib/load-zarr.js +22 -0
  96. package/dist/zarr/lib/load-zarr.js.map +1 -0
  97. package/dist/zarr/lib/spatial-multiscales.d.ts +14 -0
  98. package/dist/zarr/lib/spatial-multiscales.d.ts.map +1 -0
  99. package/dist/zarr/lib/spatial-multiscales.js +34 -0
  100. package/dist/zarr/lib/spatial-multiscales.js.map +1 -0
  101. package/dist/zarr/lib/utils.d.ts +28 -0
  102. package/dist/zarr/lib/utils.d.ts.map +1 -0
  103. package/dist/zarr/lib/utils.js +137 -0
  104. package/dist/zarr/lib/utils.js.map +1 -0
  105. package/dist/zarr/lib/zarr-data-cache.d.ts +13 -0
  106. package/dist/zarr/lib/zarr-data-cache.d.ts.map +1 -0
  107. package/dist/zarr/lib/zarr-data-cache.js +70 -0
  108. package/dist/zarr/lib/zarr-data-cache.js.map +1 -0
  109. package/dist/zarr/lib/zarr-pixel-source.d.ts +41 -0
  110. package/dist/zarr/lib/zarr-pixel-source.d.ts.map +1 -0
  111. package/dist/zarr/lib/zarr-pixel-source.js +97 -0
  112. package/dist/zarr/lib/zarr-pixel-source.js.map +1 -0
  113. package/dist/zarr/lib/zarrita-pixel-source.d.ts +40 -0
  114. package/dist/zarr/lib/zarrita-pixel-source.d.ts.map +1 -0
  115. package/dist/zarr/lib/zarrita-pixel-source.js +96 -0
  116. package/dist/zarr/lib/zarrita-pixel-source.js.map +1 -0
  117. package/dist/zarr/ome-zarr-source-loader.d.ts +195 -0
  118. package/dist/zarr/ome-zarr-source-loader.d.ts.map +1 -0
  119. package/dist/zarr/ome-zarr-source-loader.js +523 -0
  120. package/dist/zarr/ome-zarr-source-loader.js.map +1 -0
  121. package/dist/zarr/spatial-data-source.d.ts +95 -0
  122. package/dist/zarr/spatial-data-source.d.ts.map +1 -0
  123. package/dist/zarr/spatial-data-source.js +242 -0
  124. package/dist/zarr/spatial-data-source.js.map +1 -0
  125. package/dist/zarr/types.d.ts +105 -0
  126. package/dist/zarr/types.d.ts.map +1 -0
  127. package/dist/zarr/types.js +5 -0
  128. package/dist/zarr/types.js.map +1 -0
  129. package/dist/zarr/unbundled.d.ts +2 -0
  130. package/dist/zarr/unbundled.d.ts.map +1 -0
  131. package/dist/zarr/unbundled.js +5 -0
  132. package/dist/zarr/unbundled.js.map +1 -0
  133. package/dist/zarr/zarr-array-source.d.ts +102 -0
  134. package/dist/zarr/zarr-array-source.d.ts.map +1 -0
  135. package/dist/zarr/zarr-array-source.js +132 -0
  136. package/dist/zarr/zarr-array-source.js.map +1 -0
  137. package/dist/zarr/zarr-loader-types.d.ts +236 -0
  138. package/dist/zarr/zarr-loader-types.d.ts.map +1 -0
  139. package/dist/zarr/zarr-loader-types.js +128 -0
  140. package/dist/zarr/zarr-loader-types.js.map +1 -0
  141. package/dist/zarr.dev.js +16574 -0
  142. package/dist/zarr.min.js +79 -0
  143. package/package.json +62 -4
  144. package/src/index.ts +52 -0
  145. package/src/netcdf/bundled.ts +5 -0
  146. package/src/netcdf/index.ts +14 -0
  147. package/src/netcdf/iobuffer/iobuffer.ts +510 -0
  148. package/src/netcdf/netcdf-format.ts +16 -0
  149. package/src/netcdf/netcdf-loader-with-parser.ts +54 -0
  150. package/src/netcdf/netcdf-loader.ts +62 -0
  151. package/src/netcdf/netcdf-source-loader-types.ts +45 -0
  152. package/src/netcdf/netcdf-source-loader.ts +526 -0
  153. package/src/netcdf/netcdfjs/LICENSE +24 -0
  154. package/src/netcdf/netcdfjs/netcdf-reader.ts +196 -0
  155. package/src/netcdf/netcdfjs/netcdf-types.ts +75 -0
  156. package/src/netcdf/netcdfjs/read-data.ts +71 -0
  157. package/src/netcdf/netcdfjs/read-header.ts +277 -0
  158. package/src/netcdf/netcdfjs/read-type.ts +147 -0
  159. package/src/netcdf/unbundled.ts +5 -0
  160. package/src/zarr/bundled.ts +5 -0
  161. package/src/zarr/geo-zarr-source-loader.ts +998 -0
  162. package/src/zarr/index.ts +60 -0
  163. package/src/zarr/lib/consolidated-zarr.ts +190 -0
  164. package/src/zarr/lib/load-zarr.ts +31 -0
  165. package/src/zarr/lib/spatial-multiscales.ts +47 -0
  166. package/src/zarr/lib/utils.ts +162 -0
  167. package/src/zarr/lib/zarr-data-cache.ts +91 -0
  168. package/src/zarr/lib/zarr-pixel-source.ts +134 -0
  169. package/src/zarr/lib/zarrita-pixel-source.ts +141 -0
  170. package/src/zarr/ome-zarr-source-loader.ts +837 -0
  171. package/src/zarr/spatial-data-source.ts +370 -0
  172. package/src/zarr/types.ts +129 -0
  173. package/src/zarr/unbundled.ts +5 -0
  174. package/src/zarr/zarr-array-source.ts +225 -0
  175. package/src/zarr/zarr-loader-types.ts +166 -0
@@ -0,0 +1,24 @@
1
+ This directory is forked from https://github.com/cheminfo/netcdfjs
2
+
3
+ The MIT License (MIT)
4
+
5
+ Copyright (c) 2016 cheminfo
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
24
+
@@ -0,0 +1,196 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ import {IOBuffer} from '../iobuffer/iobuffer';
4
+ import type {
5
+ NetCDFHeader,
6
+ NetCDFDimension,
7
+ NetCDFRecordDimension,
8
+ NetCDFAttribute,
9
+ NetCDFVariable
10
+ } from './netcdf-types';
11
+ import {readNetCDFHeader} from './read-header';
12
+ import {readRecord, readNonRecord} from './read-data';
13
+
14
+ /**
15
+ * Reads a NetCDF v3.x file
16
+ * https://www.unidata.ucar.edu/software/netcdf/docs/file_format_specifications.html
17
+ * @param {ArrayBuffer} data - ArrayBuffer or any Typed Array (including Node.js' Buffer from v4) with the data
18
+ * @constructor
19
+ */
20
+ export class NetCDFReader {
21
+ public header: NetCDFHeader;
22
+ public buffer: IOBuffer;
23
+
24
+ constructor(data) {
25
+ const buffer = new IOBuffer(data);
26
+ buffer.setBigEndian();
27
+
28
+ // Validate that it's a NetCDF file
29
+ const magic = buffer.readChars(3);
30
+ if (magic !== 'CDF') {
31
+ throw new Error(`NetCDF: file should start with 'CDF', found ${magic}`);
32
+ }
33
+
34
+ // Check the NetCDF format
35
+ const version = buffer.readByte();
36
+ if (version > 2) {
37
+ throw new Error(`NetCDF: unsupported version ${version}`);
38
+ }
39
+
40
+ // Read the header
41
+ this.header = readNetCDFHeader(buffer, version);
42
+ this.buffer = buffer;
43
+ }
44
+
45
+ /**
46
+ * @return {string} - Version for the NetCDF format
47
+ */
48
+ get version() {
49
+ if (this.header.version === 1) {
50
+ return 'classic format';
51
+ }
52
+ return '64-bit offset format';
53
+ }
54
+
55
+ /**
56
+ * Get metadata for the record dimension
57
+ */
58
+ get recordDimension(): NetCDFRecordDimension {
59
+ return this.header.recordDimension;
60
+ }
61
+
62
+ /**
63
+ * Get list of dimensions (each with `name` and `size`)
64
+ */
65
+ get dimensions(): NetCDFDimension[] {
66
+ return this.header.dimensions;
67
+ }
68
+
69
+ /**
70
+ * Get list of global attributes with:
71
+ * * `name`: String with the name of the attribute
72
+ * * `type`: String with the type of the attribute
73
+ * * `value`: A number or string with the value of the attribute
74
+ */
75
+ get attributes(): NetCDFAttribute[] {
76
+ return this.header.attributes;
77
+ }
78
+
79
+ /**
80
+ * Get list of variables
81
+ */
82
+ get variables(): NetCDFVariable[] {
83
+ return this.header.variables;
84
+ }
85
+
86
+ /**
87
+ * Check if an attribute exists
88
+ * @param attributeName - Name of the attribute to find
89
+ * @return
90
+ */
91
+ attributeExists(attributeName: string): boolean {
92
+ const attribute = this.attributes.find(val => val.name === attributeName);
93
+ return attribute !== undefined;
94
+ }
95
+
96
+ /**
97
+ * Returns the value of an attribute
98
+ * @param attributeName
99
+ * @return Value of the attributeName or null
100
+ */
101
+ getAttribute(attributeName: string): string | null {
102
+ const attribute = this.attributes.find(val => val.name === attributeName);
103
+ if (attribute) return attribute.value;
104
+ return null;
105
+ }
106
+
107
+ /**
108
+ * Check if a dataVariable exists
109
+ * @param variableName - Name of the variable to find
110
+ * @return
111
+ */
112
+ dataVariableExists(variableName: string): boolean {
113
+ const variable = this.header.variables.find(function (val) {
114
+ return val.name === variableName;
115
+ });
116
+ return variable !== undefined;
117
+ }
118
+
119
+ /**
120
+ * Returns the value of a variable as a string
121
+ * @param variableName
122
+ * @return Value of the variable as a string or null
123
+ */
124
+ getDataVariableAsString(variableName: string): string | null {
125
+ const variable = this.getDataVariable(variableName);
126
+ if (variable) return variable.join('');
127
+ return null;
128
+ }
129
+
130
+ /**
131
+ * Retrieves the data for a given variable
132
+ * @param variableName - Name of the variable to search or variable object
133
+ * @return List with the variable values
134
+ */
135
+ getDataVariable(variableName: string | object): any[] {
136
+ let variable;
137
+ if (typeof variableName === 'string') {
138
+ // search the variable
139
+ variable = this.header.variables.find(function (val) {
140
+ return val.name === variableName;
141
+ });
142
+ } else {
143
+ variable = variableName;
144
+ }
145
+
146
+ // throws if variable not found
147
+ if (variable === undefined) {
148
+ let errorOutput;
149
+ if (typeof variableName === 'string') {
150
+ errorOutput = variableName;
151
+ } else if (typeof variableName === 'object') {
152
+ errorOutput = JSON.stringify(variableName);
153
+ }
154
+ throw new Error(`NetCDF: variable not found: ${errorOutput}`);
155
+ }
156
+
157
+ // go to the offset position
158
+ this.buffer.seek(variable.offset);
159
+
160
+ if (variable.record) {
161
+ // record variable case
162
+ return readRecord(this.buffer, variable, this.header.recordDimension, this.header.dimensions);
163
+ }
164
+ // non-record variable case
165
+ return readNonRecord(this.buffer, variable);
166
+ }
167
+
168
+ toString(): string {
169
+ const result: string[] = [];
170
+
171
+ result.push('DIMENSIONS');
172
+ for (const dimension of this.dimensions) {
173
+ result.push(` ${dimension.name.padEnd(30)} = size: ${dimension.size}`);
174
+ }
175
+
176
+ result.push('');
177
+ result.push('GLOBAL ATTRIBUTES');
178
+ for (const attribute of this.attributes) {
179
+ result.push(` ${attribute.name.padEnd(30)} = ${attribute.value}`);
180
+ }
181
+
182
+ const variables = JSON.parse(JSON.stringify(this.variables));
183
+ result.push('');
184
+ result.push('VARIABLES:');
185
+ for (const variable of variables) {
186
+ variable.value = this.getDataVariable(variable);
187
+ let stringify = JSON.stringify(variable.value);
188
+ if (stringify.length > 50) stringify = stringify.substring(0, 50);
189
+ if (!Number.isNaN(variable.value.length)) {
190
+ stringify += ` (length: ${variable.value.length})`;
191
+ }
192
+ result.push(` ${variable.name.padEnd(30)} = ${stringify}`);
193
+ }
194
+ return result.join('\n');
195
+ }
196
+ }
@@ -0,0 +1,75 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ /**
4
+ * Header describing a NetDCF file
5
+ * @param recordDimension: Number with the length of record dimension
6
+ * @param dimensions: List of dimensions
7
+ * @param attributes: List of global attributes
8
+ * @param variables: List of variables
9
+ */
10
+ export type NetCDFHeader = {
11
+ version: number;
12
+ recordDimension: NetCDFRecordDimension;
13
+ dimensions: NetCDFDimension[];
14
+ attributes: NetCDFAttribute[];
15
+ variables: NetCDFVariable[];
16
+ };
17
+
18
+ /**
19
+ * Metadata for the record dimension
20
+ * @param length Number of elements in the record dimension
21
+ * @param id Id in the list of dimensions for the record dimension
22
+ * @param name name of the record dimension
23
+ * @param recordStep the record variables step size
24
+ */
25
+ export type NetCDFRecordDimension = {
26
+ length: number;
27
+ id: number; // id of the unlimited dimension
28
+ name: string; // name of the unlimited dimension
29
+ recordStep: number;
30
+ };
31
+
32
+ /**
33
+ * `dimensions` is an array of dimension objects:
34
+ * @param name name of the dimension
35
+ * @param size size of the dimension
36
+ * @param recordId: id of the dimension that has unlimited size or undefined,
37
+ * @param recordName: name of the dimension that has unlimited size
38
+ */
39
+ export type NetCDFDimension = {
40
+ name: string;
41
+ size: number;
42
+ recordId: number;
43
+ recordName: string;
44
+ };
45
+
46
+ /**
47
+ * Number of recordStep and list of variables with:
48
+ * @param name String with the name of the variable
49
+ * @param dimensions Array with the dimension IDs of the variable
50
+ * @param attributes Array with the attributes of the variable
51
+ * @param type String with the type of the variable
52
+ * @param size Number with the size of the variable
53
+ * @param offset Number with the offset where of the variable begins
54
+ * @param record True if is a record variable, false otherwise (unlimited size)
55
+ */
56
+ export type NetCDFVariable = {
57
+ name: string;
58
+ dimensions: number[];
59
+ attributes: NetCDFAttribute[];
60
+ type: string;
61
+ size: number;
62
+ offset: number;
63
+ record: boolean;
64
+ };
65
+
66
+ /**
67
+ * @param name name of the attribute
68
+ * @param type type of the attribute
69
+ * @param value number or string with the value of the attribute
70
+ */
71
+ export type NetCDFAttribute = {
72
+ name: string;
73
+ type: string;
74
+ value: string;
75
+ };
@@ -0,0 +1,71 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ import type {IOBuffer} from '../iobuffer/iobuffer';
4
+ import type {NetCDFDimension, NetCDFRecordDimension, NetCDFVariable} from './netcdf-types';
5
+ import {readType, str2num, num2bytes} from './read-type';
6
+
7
+ // const STREAMING = 4294967295;
8
+
9
+ /**
10
+ * Read data for the given non-record variable
11
+ * @param buffer - Buffer for the file data
12
+ * @param variable - Variable metadata
13
+ * @return Data of the element
14
+ */
15
+ export function readNonRecord(
16
+ buffer: IOBuffer,
17
+ variable: NetCDFVariable
18
+ ): (string | number | number[] | Uint8Array)[] {
19
+ // variable type
20
+ const type = str2num(variable.type);
21
+
22
+ // size of the data
23
+ const size = variable.size / num2bytes(type);
24
+
25
+ // iterates over the data
26
+ const data = new Array(size);
27
+ for (let i = 0; i < size; i++) {
28
+ data[i] = readType(buffer, type, 1);
29
+ }
30
+
31
+ return data;
32
+ }
33
+
34
+ /**
35
+ * Read data for the given record variable
36
+ * @param buffer - Buffer for the file data
37
+ * @param variable - Variable metadata
38
+ * @param recordDimension - Record dimension metadata
39
+ * @param dimensions - Declared file dimensions used to exclude record padding
40
+ * @return - Data of the element
41
+ */
42
+ export function readRecord(
43
+ buffer: IOBuffer,
44
+ variable: NetCDFVariable,
45
+ recordDimension: NetCDFRecordDimension,
46
+ dimensions: NetCDFDimension[]
47
+ ): (string | number | number[] | Uint8Array)[] {
48
+ // variable type
49
+ const type = str2num(variable.type);
50
+ // NetCDF stores each record variable on a four-byte boundary. `variable.size` includes that
51
+ // padding, so derive the logical element count from the declared non-record dimensions.
52
+ const width = variable.dimensions
53
+ .filter(dimensionId => dimensionId !== recordDimension.id)
54
+ .reduce((elementCount, dimensionId) => elementCount * dimensions[dimensionId].size, 1);
55
+
56
+ // size of the data
57
+ // TODO streaming data
58
+ const size = recordDimension.length;
59
+
60
+ // iterates over the data
61
+ const data = new Array(size);
62
+ const step = recordDimension.recordStep;
63
+
64
+ for (let i = 0; i < size; i++) {
65
+ const currentOffset = buffer.offset;
66
+ data[i] = readType(buffer, type, width);
67
+ buffer.seek(currentOffset + step);
68
+ }
69
+
70
+ return data;
71
+ }
@@ -0,0 +1,277 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ import type {IOBuffer} from '../iobuffer/iobuffer';
4
+ import type {NetCDFHeader, NetCDFDimension, NetCDFVariable, NetCDFAttribute} from './netcdf-types';
5
+ import {readType, num2str} from './read-type';
6
+
7
+ // Grammar constants
8
+ const ZERO = 0;
9
+ const NC_DIMENSION = 10;
10
+ const NC_VARIABLE = 11;
11
+ const NC_ATTRIBUTE = 12;
12
+
13
+ const NC_UNLIMITED = 0;
14
+
15
+ /**
16
+ * Read the header of the file
17
+ * @param buffer - Buffer for the file data
18
+ * @param version - Version of the file
19
+ * @return - Header
20
+ */
21
+ export function readNetCDFHeader(buffer: IOBuffer, version: number): NetCDFHeader {
22
+ // Length of record dimension
23
+ // sum of the varSize's of all the record variables.
24
+ const recordDimensionLength = buffer.readUint32();
25
+
26
+ // List of dimensions
27
+ const dimList = readDimensionsList(buffer);
28
+
29
+ // List of global attributes
30
+ const attributes = readAttributesList(buffer);
31
+
32
+ // List of variables
33
+ const variableList = readVariablesList(buffer, dimList.recordId, version);
34
+
35
+ const header: NetCDFHeader = {
36
+ version,
37
+ recordDimension: {
38
+ length: recordDimensionLength,
39
+ id: dimList.recordId, // id of the unlimited dimension
40
+ name: dimList.recordName, // name of the unlimited dimension
41
+ recordStep: variableList.recordStep
42
+ },
43
+ dimensions: dimList.dimensions,
44
+ variables: variableList.variables,
45
+ attributes
46
+ };
47
+
48
+ return header;
49
+ }
50
+
51
+ /**
52
+ * Read list of dimensions
53
+ * @ignore
54
+ * @param {IOBuffer} buffer - Buffer for the file data
55
+ */
56
+ function readDimensionsList(buffer: IOBuffer): {
57
+ recordId: number;
58
+ recordName: string;
59
+ dimensions: NetCDFDimension[];
60
+ } {
61
+ const dimList = buffer.readUint32();
62
+ if (dimList === ZERO) {
63
+ if (buffer.readUint32() !== ZERO) {
64
+ throw new Error('NetCDF: wrong empty tag for list of dimensions');
65
+ }
66
+ // TODO - is this empty dimension list supported / recoverable?
67
+ return {
68
+ recordId: 0,
69
+ recordName: '',
70
+ dimensions: []
71
+ };
72
+ }
73
+
74
+ if (dimList !== NC_DIMENSION) {
75
+ throw new Error('NetCDF: wrong tag for list of dimensions');
76
+ }
77
+
78
+ // Length of dimensions
79
+ const dimensionSize = buffer.readUint32();
80
+ const dimensions = new Array(dimensionSize);
81
+ let recordId;
82
+ let recordName;
83
+ for (let dim = 0; dim < dimensionSize; dim++) {
84
+ // Read name
85
+ const name = readName(buffer);
86
+
87
+ // Read dimension size
88
+ const size = buffer.readUint32();
89
+ if (size === NC_UNLIMITED) {
90
+ // in netcdf 3 one field can be of size unlimmited
91
+ recordId = dim;
92
+ recordName = name;
93
+ }
94
+
95
+ dimensions[dim] = {
96
+ name,
97
+ size
98
+ };
99
+ }
100
+
101
+ return {
102
+ dimensions,
103
+ recordId,
104
+ recordName
105
+ };
106
+ }
107
+
108
+ /**
109
+ * List of attributes
110
+ * @ignore
111
+ * @param buffer - Buffer for the file data
112
+ * @return List of attributes with:
113
+ */
114
+ function readAttributesList(buffer: IOBuffer): NetCDFAttribute[] {
115
+ const gAttList = buffer.readUint32();
116
+ if (gAttList === ZERO) {
117
+ if (buffer.readUint32() !== ZERO) {
118
+ throw new Error('NetCDF: wrong empty tag for list of attributes');
119
+ }
120
+ return [];
121
+ }
122
+
123
+ if (gAttList !== NC_ATTRIBUTE) {
124
+ throw new Error('NetCDF: wrong tag for list of attributes');
125
+ }
126
+
127
+ // Length of attributes
128
+ const attributeSize = buffer.readUint32();
129
+ const attributes = new Array(attributeSize);
130
+ for (let gAtt = 0; gAtt < attributeSize; gAtt++) {
131
+ // Read name
132
+ const name = readName(buffer);
133
+
134
+ // Read type
135
+ const type = buffer.readUint32();
136
+ if (type < 1 || type > 6) {
137
+ throw new Error(`NetCDF: non valid type ${type}`);
138
+ }
139
+
140
+ // Read attribute
141
+ const size = buffer.readUint32();
142
+ const value = readType(buffer, type, size);
143
+
144
+ // Apply padding
145
+ padding(buffer);
146
+
147
+ attributes[gAtt] = {
148
+ name,
149
+ type: num2str(type),
150
+ value
151
+ };
152
+ }
153
+ return attributes;
154
+ }
155
+
156
+ /**
157
+ * List of variables
158
+ * @param buffer - Buffer for the file data
159
+ * @param recordId - Id of the unlimited dimension (also called record dimension)
160
+ * This value may be undefined if there is no unlimited dimension
161
+ * @param {number} version - Version of the file
162
+ */
163
+ // eslint-disable-next-line max-statements, complexity
164
+ function readVariablesList(
165
+ buffer: IOBuffer,
166
+ recordId: number,
167
+ version: number
168
+ ): {
169
+ recordStep: number;
170
+ variables: NetCDFVariable[];
171
+ } {
172
+ const varList = buffer.readUint32();
173
+ let recordStep = 0;
174
+ if (varList === ZERO) {
175
+ if (buffer.readUint32() !== ZERO) {
176
+ throw new Error('NetCDF: wrong empty tag for list of variables');
177
+ }
178
+ return {
179
+ recordStep,
180
+ variables: []
181
+ };
182
+ }
183
+
184
+ if (varList !== NC_VARIABLE) {
185
+ throw new Error('NetCDF: wrong tag for list of variables');
186
+ }
187
+
188
+ // Length of variables
189
+ const variableSize = buffer.readUint32();
190
+ const variables = new Array(variableSize);
191
+ for (let v = 0; v < variableSize; v++) {
192
+ // Read name
193
+ const name = readName(buffer);
194
+
195
+ // Read dimensionality of the variable
196
+ const dimensionality = buffer.readUint32();
197
+
198
+ // Index into the list of dimensions
199
+ const dimensionsIds = new Array(dimensionality);
200
+ for (let dim = 0; dim < dimensionality; dim++) {
201
+ dimensionsIds[dim] = buffer.readUint32();
202
+ }
203
+
204
+ // Read variables size
205
+ const attributes = readAttributesList(buffer);
206
+
207
+ // Read type
208
+ const type = buffer.readUint32();
209
+ if (type < 1 && type > 6) {
210
+ throw new Error(`NetCDF: non valid type ${type}`);
211
+ }
212
+
213
+ // Read variable size
214
+ // The 32-bit varSize field is not large enough to contain the size of variables that require
215
+ // more than 2^32 - 4 bytes, so 2^32 - 1 is used in the varSize field for such variables.
216
+ const varSize = buffer.readUint32();
217
+
218
+ // Read offset
219
+ let offset = buffer.readUint32();
220
+ if (version === 2) {
221
+ if (offset > 0) {
222
+ throw new Error('NetCDF: offsets larger than 4GB not supported');
223
+ }
224
+ offset = buffer.readUint32();
225
+ }
226
+
227
+ let record = false;
228
+ // Count amount of record variables
229
+ if (typeof recordId !== 'undefined' && dimensionsIds[0] === recordId) {
230
+ recordStep += varSize;
231
+ record = true;
232
+ }
233
+ variables[v] = {
234
+ name,
235
+ dimensions: dimensionsIds,
236
+ attributes,
237
+ type: num2str(type),
238
+ size: varSize,
239
+ offset,
240
+ record
241
+ };
242
+ }
243
+
244
+ return {
245
+ variables,
246
+ recordStep
247
+ };
248
+ }
249
+
250
+ // HELPERS
251
+
252
+ /**
253
+ * Reads the name
254
+ * @param buffer - Buffer for the file data
255
+ * @return Name
256
+ */
257
+ export function readName(buffer: IOBuffer): string {
258
+ // Read name
259
+ const nameLength = buffer.readUint32();
260
+ const name = buffer.readChars(nameLength);
261
+
262
+ // validate name
263
+ // TODO
264
+
265
+ // Apply padding
266
+ padding(buffer);
267
+ return name;
268
+ }
269
+
270
+ /**
271
+ * Moves 1, 2, or 3 bytes to next 4-byte boundary
272
+ */
273
+ function padding(buffer: IOBuffer) {
274
+ if (buffer.offset % 4 !== 0) {
275
+ buffer.skip(4 - (buffer.offset % 4));
276
+ }
277
+ }