Commit 403139aeb28efb1bc2498f0ac66069f26c31d8cc
1 parent
f8af84f9
added the ability to save ENVI headers and convert Python arrays to ENVI files
Showing
1 changed file
with
46 additions
and
4 deletions
Show diff stats
python/envi.py
... | ... | @@ -27,15 +27,15 @@ class envi_header: |
27 | 27 | self.header_offset = int(0) |
28 | 28 | self.data_type = int(4) |
29 | 29 | self.interleave = "bsq" |
30 | - self.sensor_type = "" | |
30 | + self.sensor_type = "Unknown" | |
31 | 31 | self.byte_order = int(0) |
32 | 32 | self.x_start = int(0) |
33 | 33 | self.y_start = int(0) |
34 | - self.z_plot_titles = "" | |
34 | + self.z_plot_titles = "Unknown, Unknown" | |
35 | 35 | self.pixel_size = [float(0), float(0)] |
36 | 36 | self.pixel_size_units = "Meters" |
37 | 37 | self.wavelength_units = "Wavenumber" |
38 | - self.description = "" | |
38 | + self.description = "no description" | |
39 | 39 | self.band_names = [] |
40 | 40 | self.wavelength = [] |
41 | 41 | |
... | ... | @@ -181,6 +181,35 @@ class envi_header: |
181 | 181 | li += 1 |
182 | 182 | |
183 | 183 | f.close() |
184 | + | |
185 | + #save an ENVI header | |
186 | + def save(self, fname): | |
187 | + f = open(fname, "w") | |
188 | + f.write("ENVI\n") | |
189 | + f.write("description = {" + self.description + "}" + "\n") | |
190 | + f.write("samples = " + str(self.samples) + "\n") | |
191 | + f.write("lines = " + str(self.lines) + "\n") | |
192 | + f.write("bands = " + str(self.bands) + "\n") | |
193 | + f.write("header offset = " + str(self.header_offset) + "\n") | |
194 | + f.write("file type = ENVI Standard" + "\n") | |
195 | + f.write("data type = " + str(self.get_envi_type(self.type)) + "\n") | |
196 | + f.write("interleave = " + self.interleave + "\n") | |
197 | + f.write("sensor type = " + self.sensor_type + "\n") | |
198 | + f.write("byte order = " + str(self.byte_order) + "\n") | |
199 | + f.write("x start = " + str(self.x_start) + "\n") | |
200 | + f.write("y start = " + str(self.y_start) + "\n") | |
201 | + f.write("wavelength units = " + self.wavelength_units + "\n") | |
202 | + f.write("z plot titles = {" + self.z_plot_titles + "}" + "\n") | |
203 | + | |
204 | + f.close() | |
205 | + | |
206 | + #sets the properties of the header to match those of the input array | |
207 | + def set(self, A): | |
208 | + self.type = A.dtype | |
209 | + self.samples = A.shape[2] | |
210 | + self.lines = A.shape[1] | |
211 | + self.bands = A.shape[0] | |
212 | + | |
184 | 213 | |
185 | 214 | class envi: |
186 | 215 | def __init__(self, filename, headername = "", mask = []): |
... | ... | @@ -348,4 +377,17 @@ class envi: |
348 | 377 | self.file.close() |
349 | 378 | |
350 | 379 | def __del__(self): |
351 | - self.file.close() | |
352 | 380 | \ No newline at end of file |
381 | + self.file.close() | |
382 | + | |
383 | +#saves an array as an ENVI file | |
384 | +def save_envi(A, fname): | |
385 | + | |
386 | + #create and save a header file | |
387 | + header = envi_header(); | |
388 | + header.set(A) | |
389 | + header.save(fname + ".hdr") | |
390 | + | |
391 | + #save the raw data | |
392 | + file = open(fname, "wb") | |
393 | + file.write(bytearray(A)) | |
394 | + file.close() | |
353 | 395 | \ No newline at end of file | ... | ... |