forked from hgs-dstarbuc/djs-spectral-lib-notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlse_example_notebook.idlnb
140 lines (140 loc) · 4.8 KB
/
tlse_example_notebook.idlnb
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
{
"version": "2.0.0",
"cells": [
{
"type": "markdown",
"content": [
"### Generating ENVI Spectra Libraries with TLSE Data",
"",
"This notebook shows an example of how to generate ENVI Spectral Libraries using the sprectra data from the TLSE data set. Running the first cell in this notebook will bring up a dialog for you to pick the directory in which the spectral reflectance data is saved. "
],
"metadata": {},
"outputs": []
},
{
"type": "code",
"content": [
"inputDirectory = dialog_pickfile(/directory)"
],
"metadata": {},
"outputs": []
},
{
"type": "markdown",
"content": [
"The next cell will read in the reflectance and wavelengths from each file and then store the data in a large hash. The first line in the file is skipped because it contains header information."
],
"metadata": {},
"outputs": []
},
{
"type": "code",
"content": [
"spectraFiles = file_search(inputDirectory, '*.txt')",
"allSpectraHash = hash()",
"for fileIndex = 0, n_elements(spectraFiles) - 1, 1 do begin",
" print, 'Reading file ' + strtrim(fileIndex,2) + ' of ' + strtrim(n_elements(spectraFiles) - 1,2)",
" openr, lun, spectraFiles[fileIndex], /get_lun",
" currentFileBaseName = file_basename(spectraFiles[fileIndex], '.txt')",
" tempWaveLengthList = list()",
" tempReflectanceList = list()",
" line = ''",
" currentHash = hash()",
" lineNumber = 0",
" while not eof(lun) do begin",
" readf, lun, line",
" lineSplit = strsplit(line, /extract)",
" currentWaveLength = lineSplit[0]",
" currentReflectance = lineSplit[1]",
" if lineNumber gt 0 then begin",
" tempWaveLengthList->add, currentWaveLength",
" tempReflectanceList->add, currentReflectance",
" endif",
" lineNumber++",
" endwhile ",
" currentHash['wv'] = tempWaveLengthList->toArray()",
" currentHash['ref'] = tempReflectanceList->toArray()",
" allSpectraHash[currentFileBaseName] = currentHash",
" obj_destroy, tempWaveLengthList",
" obj_destroy, tempReflectanceList",
" free_lun, lun",
"endfor",
""
],
"metadata": {},
"outputs": []
},
{
"type": "markdown",
"content": [
"Running the next cell will bring up a dialog to select the file that contains the \"bad band\" information for the spectra files. It should be called something like \"atm_bands_mask.txt\". "
],
"metadata": {},
"outputs": []
},
{
"type": "code",
"content": [
"badBandFile = dialog_pickfile()"
],
"metadata": {},
"outputs": []
},
{
"type": "markdown",
"content": [
"The next cell will read in all fo the bad band data from the file selected in the previous cell and then store it into an array. "
],
"metadata": {},
"outputs": []
},
{
"type": "code",
"content": [
"openr, lun, badBandFile, /get_lun",
"line=''",
"badBandsList = list()",
"while not eof(lun) do begin",
" readf, lun, line",
" lineSplit = strsplit(line, /extract)",
" badBandsList->add, lineSplit[1]",
"endwhile",
"badBandsArray = badBandsList.toArray()",
"free_lun, lun"
],
"metadata": {},
"outputs": []
},
{
"type": "markdown",
"content": [
"The final cell will generate a spectral library file and then fill it in with the spectras that are stored in the hash. When generating the spectral library object, it is important to specify the the wavelengths (wl), bad bands (bbl) and wavelength units (wavelegth_units). "
],
"metadata": {},
"outputs": []
},
{
"type": "code",
"content": [
"e = ENVI()",
" ",
"; Create a new spectral library",
"specLibName = filepath('tlse.sli', root_dir=inputDirectory) ",
"specLib = EnviSpectralLibrary(specLibName)",
"spectraNames = allSpectraHash->keys()",
"currentHash = allSpectraHash[spectraNames[0]] ",
"specLib->SetProperty, wl=currentHash['wv'], $",
" bbl=badBandsArray, $",
" WAVELENGTH_UNITS = 'nm'",
"",
"for nameIndex = 0, n_elements(spectraNames)-1 do begin",
" currentHash = allSpectraHash[spectraNames[nameIndex]]",
" specLib->addSpectra, spectraNames[nameIndex], double(currentHash['ref']) ",
"endfor",
" "
],
"metadata": {},
"outputs": []
}
]
}