forked from NEONScience/NEON-AOP-H5toENVI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoph5metadatafactory__define.pro
321 lines (266 loc) · 8.49 KB
/
aoph5metadatafactory__define.pro
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
; docformat = 'rst'
;+
; :Description:
; AOPh5MetadataFactory is a class with static methods designed
; to create and return metadataobjects such as an ENVIRasterMetaData or an
; ENVIStandardRasterSpatialRef. These objects can then
; be passed to the ENVI Raster constructor.
;
; :Requires:
; ENVI 5.2 / IDL 8.4
;
; :Author: Josh Elliott [email protected]
;
; :History:
; Created Jan 22, 2015 3:32:26 PM
;
; $Rev: 7397 $
; $Date: 2015-10-15 09:45:22 -0600 (Thu, 15 Oct 2015) $
;-
;-------------------------------------------------------------------------------
;+
; :Description:
; Constructor
;
; :Keywords:
; _EXTRA
;-
function AOPh5MetadataFactory::Init, _EXTRA=extra
compile_opt idl2
if (isa(extra)) then begin
self.AOPh5MetadataFactory::SetProperty, _EXTRA=extra
endif
return, 1
end
;-------------------------------------------------------------------------------
;+
; :Description:
; Destructor
;-
pro AOPh5MetadataFactory::Cleanup
compile_opt idl2
end
;-------------------------------------------------------------------------------
;+
; :Description:
; Accessor
;-
pro AOPh5MetadataFactory::GetProperty, _REF_EXTRA=extra
compile_opt idl2
end
;-------------------------------------------------------------------------------
;+
; :Description:
; Mutator
;-
pro AOPh5MetadataFactory::SetProperty, _EXTRA=extra
compile_opt idl2
end
;-------------------------------------------------------------------------------
;+
; :Description:
; Static method to return an ENVIRasterMetadata object for a particular dataset
; contained within an h5 file.
;
; :Params:
; h5 [in, req, Hash] : h5 file structure, Hash() object.
; data_set_name [in, req, IDL_String] : Data set name.
;
; :Returns:
; ENVIRasterMetadata
;-
function AOPh5MetadataFactory::CreateMetadata, h5, dataset_name
compile_opt static, idl2
metadata = !null
dataset_name_parts = strsplit(dataset_name, '/', /EXTRACT)
name_key = strupcase(dataset_name_parts[-1])
case (name_key) of
'REFLECTANCE_DATA': metadata = AOPh5MetadataFactory._Reflectance(h5, dataset_name)
'DARK_DENSE_VEGETATION_CLASSIFICATION' : metadata = AOPh5MetadataFactory._Class(h5, dataset_name)
'HAZE_CLOUD_WATER_MAP' : metadata = AOPh5MetadataFactory._Class(h5, dataset_name)
else: metadata = AOPh5MetadataFactory._Raster(h5, dataset_name)
endcase
return, metadata
end
;-------------------------------------------------------------------------------
;+
; :Description:
; Static method to return an ENVIStandardRasterSpatialRef for the h5 datasets.
;
; :Params:
; h5 [in, req, Hash] : h5 file structure, Hash() object.
;-
function AOPh5MetadataFactory::CreateSpatialRef, h5
compile_opt static, idl2
; Get the site key
site = _find_site_key(h5)
; Get coordinate system information
map_info = h5[site, 'REFLECTANCE', 'METADATA', 'COORDINATE_SYSTEM', 'MAP_INFO','_DATA', 0]
map_info_parts = strsplit(map_info, ',', /EXTRACT)
coord_sys_string = h5[site, 'REFLECTANCE', 'METADATA', 'COORDINATE_SYSTEM', 'COORDINATE_SYSTEM_STRING','_DATA', 0]
; Get the pixel size
ps = double(map_info_parts[5:6])
; Get the pixel tie point
tp = double(map_info_parts[1:2])
; Get the map tie point
tm = double(map_info_parts[3:4])
; Get the rotation, if any
if (map_info_parts[-1].Contains('rotation')) then begin
rotation = double((strsplit(map_info_parts[-1],'=', /EXTRACT))[-1])
endif else begin
rotation = !null
endelse
; Create the spatial ref object
spatialRef = ENVIStandardRasterSpatialRef( $
COORD_SYS_STR=coord_sys_string, $
PIXEL_SIZE=ps, $
TIE_POINT_MAP=tm, $
TIE_POINT_PIXEL=[0,0], $
ROTATION=rotation)
return, spatialRef
end
;+
; :Description:
;
;-
function AOPh5MetadataFactory::CreateTime, h5
compile_opt idl2
; TODO: We should be adding the Acquisition time to the h5 file metadata.
return, 1
end
;-------------------------------------------------------------------------------
;+
; :Description:
; Create the so called "bad bands list", which according to their documentation:
; "... multiplier values of each band in an image,
; typically 0 for bad bands and 1 for good bands."
;
; :Params:
; h5 [in, req, Hash] : h5 file structure, Hash() object.
;-
function AOPh5MetadataFactory::_BBL, h5, wavelengths
compile_opt static, idl2
; Get the site key
site = _find_site_key(h5)
bad_bands_window_1 = h5[site, 'REFLECTANCE', 'BAND_WINDOW_1_NANOMETERS', '_DATA']
bad_bands_window_2 = h5[site, 'REFLECTANCE', 'BAND_WINDOW_2_NANOMETERS', '_DATA']
bbl1 = (wavelengths le bad_bands_window_1[0]) or (bad_bands_window_1[1] le wavelengths)
bbl2 = (wavelengths le bad_bands_window_2[0]) or (bad_bands_window_2[1] le wavelengths)
bbl = bbl1 and bbl2
return, bbl
end
;-------------------------------------------------------------------------------
;+
; :Description:
; Reflectance
;
; :Params:
; h5 [in, req, Hash] : h5 file structure, Hash() object.
; data_set_name [in, req, IDL_String] : Data set name.
;
; :Returns:
; ENVIRasterMetadata
;-
function AOPh5MetadataFactory::_Reflectance, h5, data_set_name
compile_opt static, idl2
metadata = envirastermetadata()
; Get the site key
site = _find_site_key(h5)
; Metadata key
metadata_key = '/' + h5[site, '_NAME'] + $
'/' + h5[site, 'REFLECTANCE', '_NAME'] + $
'/' + h5[site, 'REFLECTANCE', 'METADATA', '_NAME'] + $
'/' + h5[site, 'REFLECTANCE', 'METADATA', 'SPECTRAL_DATA', '_NAME']
; Wavelength
wavelength_key = metadata_key + $
'/' + h5[site, 'REFLECTANCE', 'METADATA', 'SPECTRAL_DATA', 'WAVELENGTH', '_NAME']
metadata.AddItem, 'wavelength', h5_getdata(h5['_FILE'], wavelength_key)
units = h5[site, 'REFLECTANCE', 'METADATA', 'SPECTRAL_DATA', 'WAVELENGTH', 'UNITS', '_DATA']
metadata.AddItem, 'wavelength units', units.CapWords()
; FWHM
fwhm_key = '/' + metadata_key + $
'/' + h5[site, 'REFLECTANCE', 'METADATA', 'SPECTRAL_DATA', 'FWHM', '_NAME']
metadata.AddItem, 'fwhm', h5_getdata(h5['_FILE'], fwhm_key)
; Bad bands list
metadata.AddItem, 'bbl', AOPh5MetadataFactory._BBL(h5, metadata['wavelength'])
AOPh5MetadataFactory._AddCommonRasterMetadata, h5, data_set_name, metadata
return, metadata
end
;+
; :Description:
;
;-
function AOPh5MetadataFactory::_Class, h5, data_set_name
compile_opt static, idl2
metadata = envirastermetadata()
AOPh5MetadataFactory._AddCommonClassificationMetadata, h5, data_set_name, metadata
return, metadata
end
;+
; :Description:
;
;-
function AOPh5MetadataFactory::_Raster, h5, data_set_name
compile_opt static, idl2
metadata = envirastermetadata()
AOPh5MetadataFactory._AddCommonRasterMetadata, h5, data_set_name, metadata
return, metadata
return, 1
end
;+
; :Description:
;
;-
pro AOPh5MetadataFactory::_AddCommonRasterMetadata, _h5, data_set_name, metadata
compile_opt static, idl2
keys = strsplit(strupcase(data_set_name), '/', /EXTRACT)
h5 = _h5
foreach key, keys do h5 = h5[key]
; Scale factor
if (h5.haskey('SCALE_FACTOR')) then begin
metadata.AddItem, 'reflectance scale factor', h5['SCALE_FACTOR','_DATA']
endif
; Description
if (h5.haskey('DESCRIPTION')) then begin
metadata.AddItem, 'description', h5['DESCRIPTION','_DATA']
endif
; data-ignore-value
if (h5.haskey('DATA_IGNORE_VALUE')) then begin
metadata.AddItem, 'data ignore value', fix(h5['DATA_IGNORE_VALUE', '_DATA'])
endif
; Units, if specified
if (h5.haskey('UNITS')) then begin
metadata.AddItem, 'data units', h5['UNITS', '_DATA']
endif
end
;+
; :Description:
;
;-
pro AOPh5MetadataFactory::_AddCommonClassificationMetadata, _h5, data_set_name, metadata
compile_opt static, idl2
keys = strsplit(strupcase(data_set_name), '/', /EXTRACT)
h5 = _h5
foreach key, keys do h5 = h5[key]
; Add class names and look-up-table (LUT)
classNames = h5['CLASS_NAMES', '_DATA']
classLookup = byte(fix(h5['CLASS_LOOKUP', '_DATA']))
classLookup = reform(classLookup, 3, classLookup.length/3)
metadata.AddItem, 'classes', classNames.length
metadata.AddItem, 'class names', classNames
metadata.AddItem, 'class lookup', classLookup
; Add band name(s)
bandNames = h5['BAND_NAMES', '_DATA']
metadata.AddItem, 'band names', bandNames
end
;-------------------------------------------------------------------------------
;+
; :Description:
; Class data definition procedure
;-
pro AOPh5MetadataFactory__define
compile_opt idl2
!NULL = {AOPh5MetadataFactory, $
inherits IDL_Object $
}
end