@loaders.gl/netcdf 3.1.3 → 4.0.0-alpha.5

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 (50) hide show
  1. package/dist/index.js +3 -7
  2. package/dist/index.js.map +1 -0
  3. package/dist/iobuffer/iobuffer.js +370 -425
  4. package/dist/iobuffer/iobuffer.js.map +1 -0
  5. package/dist/netcdf-loader.js +36 -46
  6. package/dist/netcdf-loader.js.map +1 -0
  7. package/dist/{es5/netcdfjs → netcdfjs}/LICENSE +0 -0
  8. package/dist/netcdfjs/netcdf-reader.js +126 -155
  9. package/dist/netcdfjs/netcdf-reader.js.map +1 -0
  10. package/dist/netcdfjs/netcdf-types.js +2 -2
  11. package/dist/{es5/netcdfjs → netcdfjs}/netcdf-types.js.map +0 -0
  12. package/dist/netcdfjs/read-data.js +26 -47
  13. package/dist/netcdfjs/read-data.js.map +1 -0
  14. package/dist/netcdfjs/read-header.js +173 -206
  15. package/dist/netcdfjs/read-header.js.map +1 -0
  16. package/dist/netcdfjs/read-type.js +117 -130
  17. package/dist/netcdfjs/read-type.js.map +1 -0
  18. package/package.json +5 -5
  19. package/dist/es5/index.js +0 -22
  20. package/dist/es5/index.js.map +0 -1
  21. package/dist/es5/iobuffer/iobuffer.js +0 -432
  22. package/dist/es5/iobuffer/iobuffer.js.map +0 -1
  23. package/dist/es5/netcdf-loader.js +0 -105
  24. package/dist/es5/netcdf-loader.js.map +0 -1
  25. package/dist/es5/netcdfjs/netcdf-reader.js +0 -207
  26. package/dist/es5/netcdfjs/netcdf-reader.js.map +0 -1
  27. package/dist/es5/netcdfjs/netcdf-types.js +0 -2
  28. package/dist/es5/netcdfjs/read-data.js +0 -38
  29. package/dist/es5/netcdfjs/read-data.js.map +0 -1
  30. package/dist/es5/netcdfjs/read-header.js +0 -207
  31. package/dist/es5/netcdfjs/read-header.js.map +0 -1
  32. package/dist/es5/netcdfjs/read-type.js +0 -142
  33. package/dist/es5/netcdfjs/read-type.js.map +0 -1
  34. package/dist/esm/index.js +0 -3
  35. package/dist/esm/index.js.map +0 -1
  36. package/dist/esm/iobuffer/iobuffer.js +0 -372
  37. package/dist/esm/iobuffer/iobuffer.js.map +0 -1
  38. package/dist/esm/netcdf-loader.js +0 -42
  39. package/dist/esm/netcdf-loader.js.map +0 -1
  40. package/dist/esm/netcdfjs/LICENSE +0 -24
  41. package/dist/esm/netcdfjs/netcdf-reader.js +0 -136
  42. package/dist/esm/netcdfjs/netcdf-reader.js.map +0 -1
  43. package/dist/esm/netcdfjs/netcdf-types.js +0 -2
  44. package/dist/esm/netcdfjs/netcdf-types.js.map +0 -1
  45. package/dist/esm/netcdfjs/read-data.js +0 -28
  46. package/dist/esm/netcdfjs/read-data.js.map +0 -1
  47. package/dist/esm/netcdfjs/read-header.js +0 -197
  48. package/dist/esm/netcdfjs/read-header.js.map +0 -1
  49. package/dist/esm/netcdfjs/read-type.js +0 -127
  50. package/dist/esm/netcdfjs/read-type.js.map +0 -1
@@ -1,230 +1,197 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readName = exports.readNetCDFHeader = void 0;
4
- const read_type_1 = require("./read-type");
5
- // Grammar constants
1
+ import { readType, num2str } from './read-type';
6
2
  const ZERO = 0;
7
3
  const NC_DIMENSION = 10;
8
4
  const NC_VARIABLE = 11;
9
5
  const NC_ATTRIBUTE = 12;
10
6
  const NC_UNLIMITED = 0;
11
- /**
12
- * Read the header of the file
13
- * @param buffer - Buffer for the file data
14
- * @param version - Version of the file
15
- * @return - Header
16
- */
17
- function readNetCDFHeader(buffer, version) {
18
- // Length of record dimension
19
- // sum of the varSize's of all the record variables.
20
- const recordDimensionLength = buffer.readUint32();
21
- // List of dimensions
22
- const dimList = readDimensionsList(buffer);
23
- // List of global attributes
24
- const attributes = readAttributesList(buffer);
25
- // List of variables
26
- const variableList = readVariablesList(buffer, dimList.recordId, version);
27
- const header = {
28
- version,
29
- recordDimension: {
30
- length: recordDimensionLength,
31
- id: dimList.recordId,
32
- name: dimList.recordName,
33
- recordStep: variableList.recordStep
34
- },
35
- dimensions: dimList.dimensions,
36
- variables: variableList.variables,
37
- attributes
38
- };
39
- return header;
7
+ export function readNetCDFHeader(buffer, version) {
8
+ const recordDimensionLength = buffer.readUint32();
9
+ const dimList = readDimensionsList(buffer);
10
+ const attributes = readAttributesList(buffer);
11
+ const variableList = readVariablesList(buffer, dimList.recordId, version);
12
+ const header = {
13
+ version,
14
+ recordDimension: {
15
+ length: recordDimensionLength,
16
+ id: dimList.recordId,
17
+ name: dimList.recordName,
18
+ recordStep: variableList.recordStep
19
+ },
20
+ dimensions: dimList.dimensions,
21
+ variables: variableList.variables,
22
+ attributes
23
+ };
24
+ return header;
40
25
  }
41
- exports.readNetCDFHeader = readNetCDFHeader;
42
- /**
43
- * Read list of dimensions
44
- * @ignore
45
- * @param {IOBuffer} buffer - Buffer for the file data
46
- */
26
+
47
27
  function readDimensionsList(buffer) {
48
- const dimList = buffer.readUint32();
49
- if (dimList === ZERO) {
50
- if (buffer.readUint32() !== ZERO) {
51
- throw new Error('NetCDF: wrong empty tag for list of dimensions');
52
- }
53
- // TODO - is this empty dimension list supported / recoverable?
54
- return {
55
- recordId: 0,
56
- recordName: '',
57
- dimensions: []
58
- };
59
- }
60
- if (dimList !== NC_DIMENSION) {
61
- throw new Error('NetCDF: wrong tag for list of dimensions');
62
- }
63
- // Length of dimensions
64
- const dimensionSize = buffer.readUint32();
65
- const dimensions = new Array(dimensionSize);
66
- let recordId;
67
- let recordName;
68
- for (let dim = 0; dim < dimensionSize; dim++) {
69
- // Read name
70
- const name = readName(buffer);
71
- // Read dimension size
72
- const size = buffer.readUint32();
73
- if (size === NC_UNLIMITED) {
74
- // in netcdf 3 one field can be of size unlimmited
75
- recordId = dim;
76
- recordName = name;
77
- }
78
- dimensions[dim] = {
79
- name,
80
- size
81
- };
28
+ const dimList = buffer.readUint32();
29
+
30
+ if (dimList === ZERO) {
31
+ if (buffer.readUint32() !== ZERO) {
32
+ throw new Error('NetCDF: wrong empty tag for list of dimensions');
82
33
  }
34
+
83
35
  return {
84
- dimensions,
85
- recordId,
86
- recordName
36
+ recordId: 0,
37
+ recordName: '',
38
+ dimensions: []
87
39
  };
40
+ }
41
+
42
+ if (dimList !== NC_DIMENSION) {
43
+ throw new Error('NetCDF: wrong tag for list of dimensions');
44
+ }
45
+
46
+ const dimensionSize = buffer.readUint32();
47
+ const dimensions = new Array(dimensionSize);
48
+ let recordId;
49
+ let recordName;
50
+
51
+ for (let dim = 0; dim < dimensionSize; dim++) {
52
+ const name = readName(buffer);
53
+ const size = buffer.readUint32();
54
+
55
+ if (size === NC_UNLIMITED) {
56
+ recordId = dim;
57
+ recordName = name;
58
+ }
59
+
60
+ dimensions[dim] = {
61
+ name,
62
+ size
63
+ };
64
+ }
65
+
66
+ return {
67
+ dimensions,
68
+ recordId,
69
+ recordName
70
+ };
88
71
  }
89
- /**
90
- * List of attributes
91
- * @ignore
92
- * @param buffer - Buffer for the file data
93
- * @return List of attributes with:
94
- */
72
+
95
73
  function readAttributesList(buffer) {
96
- const gAttList = buffer.readUint32();
97
- if (gAttList === ZERO) {
98
- if (buffer.readUint32() !== ZERO) {
99
- throw new Error('NetCDF: wrong empty tag for list of attributes');
100
- }
101
- return [];
102
- }
103
- if (gAttList !== NC_ATTRIBUTE) {
104
- throw new Error('NetCDF: wrong tag for list of attributes');
74
+ const gAttList = buffer.readUint32();
75
+
76
+ if (gAttList === ZERO) {
77
+ if (buffer.readUint32() !== ZERO) {
78
+ throw new Error('NetCDF: wrong empty tag for list of attributes');
105
79
  }
106
- // Length of attributes
107
- const attributeSize = buffer.readUint32();
108
- const attributes = new Array(attributeSize);
109
- for (let gAtt = 0; gAtt < attributeSize; gAtt++) {
110
- // Read name
111
- const name = readName(buffer);
112
- // Read type
113
- const type = buffer.readUint32();
114
- if (type < 1 || type > 6) {
115
- throw new Error(`NetCDF: non valid type ${type}`);
116
- }
117
- // Read attribute
118
- const size = buffer.readUint32();
119
- const value = (0, read_type_1.readType)(buffer, type, size);
120
- // Apply padding
121
- padding(buffer);
122
- attributes[gAtt] = {
123
- name,
124
- type: (0, read_type_1.num2str)(type),
125
- value
126
- };
80
+
81
+ return [];
82
+ }
83
+
84
+ if (gAttList !== NC_ATTRIBUTE) {
85
+ throw new Error('NetCDF: wrong tag for list of attributes');
86
+ }
87
+
88
+ const attributeSize = buffer.readUint32();
89
+ const attributes = new Array(attributeSize);
90
+
91
+ for (let gAtt = 0; gAtt < attributeSize; gAtt++) {
92
+ const name = readName(buffer);
93
+ const type = buffer.readUint32();
94
+
95
+ if (type < 1 || type > 6) {
96
+ throw new Error("NetCDF: non valid type ".concat(type));
127
97
  }
128
- return attributes;
98
+
99
+ const size = buffer.readUint32();
100
+ const value = readType(buffer, type, size);
101
+ padding(buffer);
102
+ attributes[gAtt] = {
103
+ name,
104
+ type: num2str(type),
105
+ value
106
+ };
107
+ }
108
+
109
+ return attributes;
129
110
  }
130
- /**
131
- * List of variables
132
- * @param buffer - Buffer for the file data
133
- * @param recordId - Id of the unlimited dimension (also called record dimension)
134
- * This value may be undefined if there is no unlimited dimension
135
- * @param {number} version - Version of the file
136
- */
137
- // eslint-disable-next-line max-statements, complexity
111
+
138
112
  function readVariablesList(buffer, recordId, version) {
139
- const varList = buffer.readUint32();
140
- let recordStep = 0;
141
- if (varList === ZERO) {
142
- if (buffer.readUint32() !== ZERO) {
143
- throw new Error('NetCDF: wrong empty tag for list of variables');
144
- }
145
- return {
146
- recordStep,
147
- variables: []
148
- };
113
+ const varList = buffer.readUint32();
114
+ let recordStep = 0;
115
+
116
+ if (varList === ZERO) {
117
+ if (buffer.readUint32() !== ZERO) {
118
+ throw new Error('NetCDF: wrong empty tag for list of variables');
149
119
  }
150
- if (varList !== NC_VARIABLE) {
151
- throw new Error('NetCDF: wrong tag for list of variables');
120
+
121
+ return {
122
+ recordStep,
123
+ variables: []
124
+ };
125
+ }
126
+
127
+ if (varList !== NC_VARIABLE) {
128
+ throw new Error('NetCDF: wrong tag for list of variables');
129
+ }
130
+
131
+ const variableSize = buffer.readUint32();
132
+ const variables = new Array(variableSize);
133
+
134
+ for (let v = 0; v < variableSize; v++) {
135
+ const name = readName(buffer);
136
+ const dimensionality = buffer.readUint32();
137
+ const dimensionsIds = new Array(dimensionality);
138
+
139
+ for (let dim = 0; dim < dimensionality; dim++) {
140
+ dimensionsIds[dim] = buffer.readUint32();
152
141
  }
153
- // Length of variables
154
- const variableSize = buffer.readUint32();
155
- const variables = new Array(variableSize);
156
- for (let v = 0; v < variableSize; v++) {
157
- // Read name
158
- const name = readName(buffer);
159
- // Read dimensionality of the variable
160
- const dimensionality = buffer.readUint32();
161
- // Index into the list of dimensions
162
- const dimensionsIds = new Array(dimensionality);
163
- for (let dim = 0; dim < dimensionality; dim++) {
164
- dimensionsIds[dim] = buffer.readUint32();
165
- }
166
- // Read variables size
167
- const attributes = readAttributesList(buffer);
168
- // Read type
169
- const type = buffer.readUint32();
170
- if (type < 1 && type > 6) {
171
- throw new Error(`NetCDF: non valid type ${type}`);
172
- }
173
- // Read variable size
174
- // The 32-bit varSize field is not large enough to contain the size of variables that require
175
- // more than 2^32 - 4 bytes, so 2^32 - 1 is used in the varSize field for such variables.
176
- const varSize = buffer.readUint32();
177
- // Read offset
178
- let offset = buffer.readUint32();
179
- if (version === 2) {
180
- if (offset > 0) {
181
- throw new Error('NetCDF: offsets larger than 4GB not supported');
182
- }
183
- offset = buffer.readUint32();
184
- }
185
- let record = false;
186
- // Count amount of record variables
187
- if (typeof recordId !== 'undefined' && dimensionsIds[0] === recordId) {
188
- recordStep += varSize;
189
- record = true;
190
- }
191
- variables[v] = {
192
- name,
193
- dimensions: dimensionsIds,
194
- attributes,
195
- type: (0, read_type_1.num2str)(type),
196
- size: varSize,
197
- offset,
198
- record
199
- };
142
+
143
+ const attributes = readAttributesList(buffer);
144
+ const type = buffer.readUint32();
145
+
146
+ if (type < 1 && type > 6) {
147
+ throw new Error("NetCDF: non valid type ".concat(type));
200
148
  }
201
- return {
202
- variables,
203
- recordStep
149
+
150
+ const varSize = buffer.readUint32();
151
+ let offset = buffer.readUint32();
152
+
153
+ if (version === 2) {
154
+ if (offset > 0) {
155
+ throw new Error('NetCDF: offsets larger than 4GB not supported');
156
+ }
157
+
158
+ offset = buffer.readUint32();
159
+ }
160
+
161
+ let record = false;
162
+
163
+ if (typeof recordId !== 'undefined' && dimensionsIds[0] === recordId) {
164
+ recordStep += varSize;
165
+ record = true;
166
+ }
167
+
168
+ variables[v] = {
169
+ name,
170
+ dimensions: dimensionsIds,
171
+ attributes,
172
+ type: num2str(type),
173
+ size: varSize,
174
+ offset,
175
+ record
204
176
  };
177
+ }
178
+
179
+ return {
180
+ variables,
181
+ recordStep
182
+ };
205
183
  }
206
- // HELPERS
207
- /**
208
- * Reads the name
209
- * @param buffer - Buffer for the file data
210
- * @return Name
211
- */
212
- function readName(buffer) {
213
- // Read name
214
- const nameLength = buffer.readUint32();
215
- const name = buffer.readChars(nameLength);
216
- // validate name
217
- // TODO
218
- // Apply padding
219
- padding(buffer);
220
- return name;
184
+
185
+ export function readName(buffer) {
186
+ const nameLength = buffer.readUint32();
187
+ const name = buffer.readChars(nameLength);
188
+ padding(buffer);
189
+ return name;
221
190
  }
222
- exports.readName = readName;
223
- /**
224
- * Moves 1, 2, or 3 bytes to next 4-byte boundary
225
- */
191
+
226
192
  function padding(buffer) {
227
- if (buffer.offset % 4 !== 0) {
228
- buffer.skip(4 - (buffer.offset % 4));
229
- }
193
+ if (buffer.offset % 4 !== 0) {
194
+ buffer.skip(4 - buffer.offset % 4);
195
+ }
230
196
  }
197
+ //# sourceMappingURL=read-header.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/netcdfjs/read-header.ts"],"names":["readType","num2str","ZERO","NC_DIMENSION","NC_VARIABLE","NC_ATTRIBUTE","NC_UNLIMITED","readNetCDFHeader","buffer","version","recordDimensionLength","readUint32","dimList","readDimensionsList","attributes","readAttributesList","variableList","readVariablesList","recordId","header","recordDimension","length","id","name","recordName","recordStep","dimensions","variables","Error","dimensionSize","Array","dim","readName","size","gAttList","attributeSize","gAtt","type","value","padding","varList","variableSize","v","dimensionality","dimensionsIds","varSize","offset","record","nameLength","readChars","skip"],"mappings":"AAEA,SAAQA,QAAR,EAAkBC,OAAlB,QAAgC,aAAhC;AAGA,MAAMC,IAAI,GAAG,CAAb;AACA,MAAMC,YAAY,GAAG,EAArB;AACA,MAAMC,WAAW,GAAG,EAApB;AACA,MAAMC,YAAY,GAAG,EAArB;AAEA,MAAMC,YAAY,GAAG,CAArB;AAQA,OAAO,SAASC,gBAAT,CAA0BC,MAA1B,EAA4CC,OAA5C,EAA2E;AAGhF,QAAMC,qBAAqB,GAAGF,MAAM,CAACG,UAAP,EAA9B;AAGA,QAAMC,OAAO,GAAGC,kBAAkB,CAACL,MAAD,CAAlC;AAGA,QAAMM,UAAU,GAAGC,kBAAkB,CAACP,MAAD,CAArC;AAGA,QAAMQ,YAAY,GAAGC,iBAAiB,CAACT,MAAD,EAASI,OAAO,CAACM,QAAjB,EAA2BT,OAA3B,CAAtC;AAEA,QAAMU,MAAoB,GAAG;AAC3BV,IAAAA,OAD2B;AAE3BW,IAAAA,eAAe,EAAE;AACfC,MAAAA,MAAM,EAAEX,qBADO;AAEfY,MAAAA,EAAE,EAAEV,OAAO,CAACM,QAFG;AAGfK,MAAAA,IAAI,EAAEX,OAAO,CAACY,UAHC;AAIfC,MAAAA,UAAU,EAAET,YAAY,CAACS;AAJV,KAFU;AAQ3BC,IAAAA,UAAU,EAAEd,OAAO,CAACc,UARO;AAS3BC,IAAAA,SAAS,EAAEX,YAAY,CAACW,SATG;AAU3Bb,IAAAA;AAV2B,GAA7B;AAaA,SAAOK,MAAP;AACD;;AAOD,SAASN,kBAAT,CAA4BL,MAA5B,EAIE;AACA,QAAMI,OAAO,GAAGJ,MAAM,CAACG,UAAP,EAAhB;;AACA,MAAIC,OAAO,KAAKV,IAAhB,EAAsB;AACpB,QA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b;;AAEA,QAAI,OAAO7B,QAAP,KAAoB,WAApB,IAAmC0B,aAAa,CAAC,CAAD,CAAb,KAAqB1B,QAA5D,EAAsE;AACpEO,MAAAA,UAAU,IAAIoB,OAAd;AACAE,MAAAA,MAAM,GAAG,IAAT;AACD;;AACDpB,IAAAA,SAAS,CAACe,CAAD,CAAT,GAAe;AACbnB,MAAAA,IADa;AAEbG,MAAAA,UAAU,EAAEkB,aAFC;AAGb9B,MAAAA,UAHa;AAIbuB,MAAAA,IAAI,EAAEpC,OAAO,CAACoC,IAAD,CAJA;AAKbJ,MAAAA,IAAI,EAAEY,OALO;AAMbC,MAAAA,MANa;AAObC,MAAAA;AAPa,KAAf;AASD;;AAED,SAAO;AACLpB,IAAAA,SADK;AAELF,IAAAA;AAFK,GAAP;AAID;;AASD,OAAO,SAASO,QAAT,CAAkBxB,MAAlB,EAA4C;AAEjD,QAAMwC,UAAU,GAAGxC,MAAM,CAACG,UAAP,EAAnB;AACA,QAAMY,IAAI,GAAGf,MAAM,CAACyC,SAAP,CAAiBD,UAAjB,CAAb;AAMAT,EAAAA,OAAO,CAAC/B,MAAD,CAAP;AACA,SAAOe,IAAP;AACD;;AAKD,SAASgB,OAAT,CAAiB/B,MAAjB,EAAmC;AACjC,MAAIA,MAAM,CAACsC,MAAP,GAAgB,CAAhB,KAAsB,CAA1B,EAA6B;AAC3BtC,IAAAA,MAAM,CAAC0C,IAAP,CAAY,IAAK1C,MAAM,CAACsC,MAAP,GAAgB,CAAjC;AACD;AACF","sourcesContent":["import type {IOBuffer} from '../iobuffer/iobuffer';\nimport type {NetCDFHeader, NetCDFDimension, NetCDFVariable, NetCDFAttribute} from './netcdf-types';\nimport {readType, num2str} from './read-type';\n\n// Grammar constants\nconst ZERO = 0;\nconst NC_DIMENSION = 10;\nconst NC_VARIABLE = 11;\nconst NC_ATTRIBUTE = 12;\n\nconst NC_UNLIMITED = 0;\n\n/**\n * Read the header of the file\n * @param buffer - Buffer for the file data\n * @param version - Version of the file\n * @return - Header\n */\nexport function readNetCDFHeader(buffer: IOBuffer, version: number): NetCDFHeader {\n // Length of record dimension\n // sum of the varSize's of all the record variables.\n const recordDimensionLength = buffer.readUint32();\n\n // List of dimensions\n const dimList = readDimensionsList(buffer);\n\n // List of global attributes\n const attributes = readAttributesList(buffer);\n\n // List of variables\n const variableList = readVariablesList(buffer, dimList.recordId, version);\n\n const header: NetCDFHeader = {\n version,\n recordDimension: {\n length: recordDimensionLength,\n id: dimList.recordId, // id of the unlimited dimension\n name: dimList.recordName, // name of the unlimited dimension\n recordStep: variableList.recordStep\n },\n dimensions: dimList.dimensions,\n variables: variableList.variables,\n attributes\n };\n\n return header;\n}\n\n/**\n * Read list of dimensions\n * @ignore\n * @param {IOBuffer} buffer - Buffer for the file data\n */\nfunction readDimensionsList(buffer: IOBuffer): {\n recordId: number;\n recordName: string;\n dimensions: NetCDFDimension[];\n} {\n const dimList = buffer.readUint32();\n if (dimList === ZERO) {\n if (buffer.readUint32() !== ZERO) {\n throw new Error('NetCDF: wrong empty tag for list of dimensions');\n }\n // TODO - is this empty dimension list supported / recoverable?\n return {\n recordId: 0,\n recordName: '',\n dimensions: []\n };\n }\n\n if (dimList !== NC_DIMENSION) {\n throw new Error('NetCDF: wrong tag for list of dimensions');\n }\n\n // Length of dimensions\n const dimensionSize = buffer.readUint32();\n const dimensions = new Array(dimensionSize);\n let recordId;\n let recordName;\n for (let dim = 0; dim < dimensionSize; dim++) {\n // Read name\n const name = readName(buffer);\n\n // Read dimension size\n const size = buffer.readUint32();\n if (size === NC_UNLIMITED) {\n // in netcdf 3 one field can be of size unlimmited\n recordId = dim;\n recordName = name;\n }\n\n dimensions[dim] = {\n name,\n size\n };\n }\n\n return {\n dimensions,\n recordId,\n recordName\n };\n}\n\n/**\n * List of attributes\n * @ignore\n * @param buffer - Buffer for the file data\n * @return List of attributes with:\n */\nfunction readAttributesList(buffer: IOBuffer): NetCDFAttribute[] {\n const gAttList = buffer.readUint32();\n if (gAttList === ZERO) {\n if (buffer.readUint32() !== ZERO) {\n throw new Error('NetCDF: wrong empty tag for list of attributes');\n }\n return [];\n }\n\n if (gAttList !== NC_ATTRIBUTE) {\n throw new Error('NetCDF: wrong tag for list of attributes');\n }\n\n // Length of attributes\n const attributeSize = buffer.readUint32();\n const attributes = new Array(attributeSize);\n for (let gAtt = 0; gAtt < attributeSize; gAtt++) {\n // Read name\n const name = readName(buffer);\n\n // Read type\n const type = buffer.readUint32();\n if (type < 1 || type > 6) {\n throw new Error(`NetCDF: non valid type ${type}`);\n }\n\n // Read attribute\n const size = buffer.readUint32();\n const value = readType(buffer, type, size);\n\n // Apply padding\n padding(buffer);\n\n attributes[gAtt] = {\n name,\n type: num2str(type),\n value\n };\n }\n return attributes;\n}\n\n/**\n * List of variables\n * @param buffer - Buffer for the file data\n * @param recordId - Id of the unlimited dimension (also called record dimension)\n * This value may be undefined if there is no unlimited dimension\n * @param {number} version - Version of the file\n */\n// eslint-disable-next-line max-statements, complexity\nfunction readVariablesList(\n buffer: IOBuffer,\n recordId: number,\n version: number\n): {\n recordStep: number;\n variables: NetCDFVariable[];\n} {\n const varList = buffer.readUint32();\n let recordStep = 0;\n if (varList === ZERO) {\n if (buffer.readUint32() !== ZERO) {\n throw new Error('NetCDF: wrong empty tag for list of variables');\n }\n return {\n recordStep,\n variables: []\n };\n }\n\n if (varList !== NC_VARIABLE) {\n throw new Error('NetCDF: wrong tag for list of variables');\n }\n\n // Length of variables\n const variableSize = buffer.readUint32();\n const variables = new Array(variableSize);\n for (let v = 0; v < variableSize; v++) {\n // Read name\n const name = readName(buffer);\n\n // Read dimensionality of the variable\n const dimensionality = buffer.readUint32();\n\n // Index into the list of dimensions\n const dimensionsIds = new Array(dimensionality);\n for (let dim = 0; dim < dimensionality; dim++) {\n dimensionsIds[dim] = buffer.readUint32();\n }\n\n // Read variables size\n const attributes = readAttributesList(buffer);\n\n // Read type\n const type = buffer.readUint32();\n if (type < 1 && type > 6) {\n throw new Error(`NetCDF: non valid type ${type}`);\n }\n\n // Read variable size\n // The 32-bit varSize field is not large enough to contain the size of variables that require\n // more than 2^32 - 4 bytes, so 2^32 - 1 is used in the varSize field for such variables.\n const varSize = buffer.readUint32();\n\n // Read offset\n let offset = buffer.readUint32();\n if (version === 2) {\n if (offset > 0) {\n throw new Error('NetCDF: offsets larger than 4GB not supported');\n }\n offset = buffer.readUint32();\n }\n\n let record = false;\n // Count amount of record variables\n if (typeof recordId !== 'undefined' && dimensionsIds[0] === recordId) {\n recordStep += varSize;\n record = true;\n }\n variables[v] = {\n name,\n dimensions: dimensionsIds,\n attributes,\n type: num2str(type),\n size: varSize,\n offset,\n record\n };\n }\n\n return {\n variables,\n recordStep\n };\n}\n\n// HELPERS\n\n/**\n * Reads the name\n * @param buffer - Buffer for the file data\n * @return Name\n */\nexport function readName(buffer: IOBuffer): string {\n // Read name\n const nameLength = buffer.readUint32();\n const name = buffer.readChars(nameLength);\n\n // validate name\n // TODO\n\n // Apply padding\n padding(buffer);\n return name;\n}\n\n/**\n * Moves 1, 2, or 3 bytes to next 4-byte boundary\n */\nfunction padding(buffer: IOBuffer) {\n if (buffer.offset % 4 !== 0) {\n buffer.skip(4 - (buffer.offset % 4));\n }\n}\n"],"file":"read-header.js"}