1# Author: Robert J. Hijmans
2# Date :  October 2009
3# Version 0.9
4# Licence GPL v3
5
6
7.writeHdrENVI <- function(r) {
8	hdrfile <- filename(r)
9	extension(hdrfile) <- ".hdr"
10	thefile <- file(hdrfile, "w")
11	cat("ENVI\n", file = thefile)
12	cat("samples = ", ncol(r), "\n", file = thefile)
13	cat("lines = ", nrow(r), "\n", file = thefile)
14	cat("bands = ", r@file@nbands, "\n", file = thefile)
15	cat("header offset = 0\n", file = thefile)
16	cat("file type = ENVI Standard\n", file = thefile)
17	dsize <- dataSize(r@file@datanotation)
18	if (.shortDataType(r@file@datanotation) == 'INT') {
19		if (dsize == 1) { dtype <- 1
20		} else if (dsize == 2) { dtype <- 2
21		} else if (dsize == 4) { dtype <- 3
22		} else if (dsize == 8) { dtype <- 14
23		} else { stop('what?')
24		}
25	} else {
26		if (dsize == 4) { dtype <- 4
27		} else if (dsize == 8) { dtype <- 5
28		} else { stop('what?')
29		}
30	}
31	cat("data type = ", dtype, "\n", file = thefile)
32#1=8-bit byte; 2=16-bit signed integer; 3=32-bit signed long integer; 4=32-bit floating point;
33#5=64-bit double-precision floating point; 6=2x32-bit complex, real-imaginary pair of double precision;
34#9=2x64-bit double-precision complex, real-imaginary pair of double precision; 12=16-bit unsigned integer;
35#13=32-bit unsigned long integer; 14=64-bit signed long integer; and 15=64-bit unsigned long integer.
36
37	cat("data ignore value=", .nodatavalue(r), "\n", file = thefile, sep='')
38	cat("interleave = ", r@file@bandorder, "\n", file = thefile)
39	cat("sensor type = \n", file = thefile)
40
41	btorder <- as.integer(r@file@byteorder != 'little')  # little -> 0, big -> 1
42	cat("byte order = ", btorder, "\n",file = thefile)
43
44	if (couldBeLonLat(r)) {
45		cat("map info = {Geographic Lat/Lon, 1, 1,", xmin(r),", ", ymax(r),", ", xres(r),", ", yres(r), "}\n", file = thefile)
46	} else {
47		cat("map info = {projection, 1, 1,", xmin(r),", ", ymax(r),", ", xres(r),", ", yres(r), "}\n", file = thefile)
48	}
49	if (.requireRgdal(FALSE)) {
50		cat("coordinate system string = {", wkt(r), "}\n", file = thefile, sep="")
51	} else {
52		cat("projection info =", proj4string(r), "\n", file = thefile)
53	}
54	cat("z plot range = {", minValue(r),", ", maxValue(r), "}\n", file = thefile)
55
56	cat("band names = {", paste(names(r),collapse=","), "}", "\n", file = thefile)
57
58	close(thefile)
59}
60
61