-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPDSLoader.js
194 lines (137 loc) · 4.71 KB
/
PDSLoader.js
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
import { VicarLoaderBase } from '../vicar-loader/VicarLoaderBase.js';
import { readHeaderString, parseLabels, getFirstLabelInstance } from './utils.js';
/**
* @typedef {Object} PDSResult
* @param {Array<{ isLabelGroup: Boolean, name: String, value: any }>} labels
* The set of header labels in the file. This includes both the header
* and EOL extension labels if present.
*
* @param {(VicarResult|null)} [product=null]
*
* The image stored in the file based on the [Vicar loader result](../vicar-loader/README.md#VicarResult).
*
* This is present only if there are no product pointers in the header OR if there are only `IMAGE` and
* `IMAGE_HEADER` objects that point to Vicar data in the same file.
*
* @param {Array<VicarResult|null>} [products=null]
* The set of products pointed to by the header in either this file or separate ones.
*
* !> Note this is not currently implemented and separated data products will never be represented here.
*/
// Spec: https://pds.nasa.gov/datastandards/pds3/standards/
/**
* Class for loading and parsing PDS files.
*/
class PDSLoader {
constructor() {
/**
* @member {Object}
* @description Fetch options for loading the file.
* @default { credentials: 'same-origin' }
*/
this.fetchOptions = { credentials: 'same-origin' };
/**
* @member {Object}
* @description Map from embedded format type to parse function
*/
this.parsers = {
'VICAR2': buffer => new VicarLoaderBase().parse( buffer ),
};
}
/**
* Loads and parses the PDS file. The promise resolves with the returned
* data from the {@link #PDSLoader#parse parse} function.
* @param {String} url
* @returns {Promise<PDSResult>}
*/
load( url ) {
return fetch( url, this.fetchOptions )
.then( res => {
if ( ! res.ok ) {
throw new Error( `PDSLoader: Failed to load file "${url}" with status ${res.status} : ${res.statusText}` );
}
return res.arrayBuffer();
} )
.then( buffer => this.parse( buffer ) );
}
/**
* Parses the contents of the given PDS file and returns an object describing
* the telemetry.
* @param {Uint8Array | ArrayBuffer} buffer
* @returns {PDSResult}
*/
parse( buffer ) {
let byteBuffer;
if ( buffer instanceof Uint8Array ) {
byteBuffer = buffer;
} else {
byteBuffer = new Uint8Array( buffer );
}
const headerString = readHeaderString( byteBuffer );
const labels = parseLabels( headerString );
const labelRecords = getFirstLabelInstance( labels, 'LABEL_RECORDS', 1 );
const recordBytes = getFirstLabelInstance( labels, 'RECORD_BYTES' );
const recordType = getFirstLabelInstance( labels, 'RECORD_TYPE' );
const labelSize = labelRecords * recordBytes;
if ( recordType !== 'FIXED_LENGTH' ) {
console.warn( 'PDSLoader: Non FIXED_LENGTH record types not supported' );
return Promise.resolve( null );
}
const products = [];
for ( const i in labels ) {
const { name, value } = labels[ i ];
if ( /^\^/.test( name ) ) {
if ( Array.isArray( value ) ) {
const [ path, index ] = value;
} else if ( typeof value === 'number' || /<BYTES>/.test( value ) ) {
let pointer;
if ( /<BYTES>/.test( value ) ) {
pointer = value.replace( /<BYTES>/, '' );
} else {
pointer = value * recordBytes;
}
} else {
const path = value;
}
products.push( {
name: name.replace( /^\^/, '' ),
value: null,
} );
}
}
const result = {};
result.labels = labels;
result.product = null;
result.products = null;
const noProducts = products.length === 0;
const justVicarProduct =
products.length === 2 &&
getFirstLabelInstance( products, 'IMAGE' ) !== undefined &&
getFirstLabelInstance( products, 'IMAGE_HEADER' ) !== undefined &&
typeof getFirstLabelInstance( labels, '^IMAGE' ) === 'number' &&
typeof getFirstLabelInstance( labels, '^IMAGE_HEADER' ) === 'number' &&
getFirstLabelInstance( labels, 'IMAGE_HEADER.HEADER_TYPE' ) === 'VICAR2';
if ( noProducts || justVicarProduct ) {
const type = getFirstLabelInstance( labels, 'IMAGE_HEADER.HEADER_TYPE' );
const parseFunc = this.parsers[ type ];
if ( ! parseFunc ) {
console.warn( `PDSLoader: No parser available for embedded format "${ type }".` );
} else if ( type === 'VICAR2' ) {
const vicarBuffer = new Uint8Array(
byteBuffer.buffer,
byteBuffer.byteOffset + labelSize,
);
result.product = parseFunc( vicarBuffer );
} else {
console.warn( 'PDSLoader: Could not parse PDS product.' );
}
} else {
result.products = products;
console.warn(
'PDSLoader: File contains product pointers which are not yet supported beyond IMAGE and IMAGE_HEADER for Vicar files.',
);
}
return Promise.resolve( result );
}
}
export { PDSLoader };