1# Author: Robert J. Hijmans
2# Date :  June 2008
3# Version 0.9
4# Licence GPL v3
5
6
7.writeHdrErdasRaw <- function(raster) {
8	hdrfile <- filename(raster)
9	extension(hdrfile) <- ".raw"
10	thefile <- file(hdrfile, "w")  # open an txt file connectionis
11	cat("IMAGINE_RAW_FILE\n", file = thefile)
12	cat("PIXEL_FILES ", .setFileExtensionValues(raster@file@name), "\n", file = thefile)
13# this may not work. Some implementations may ignore this keyword and expect the pixelfile to have the same file name, no extension.
14
15	cat("HEIGHT ",  nrow(raster), "\n", file = thefile)
16	cat("WIDTH ",  ncol(raster), "\n", file = thefile)
17	cat("NUM_LAYERS ",  nbands(raster), "\n", file = thefile)
18
19	if (.shortDataType(raster@file@datanotation) == 'INT') {
20		dd <- "S"
21	} else {
22		dd <- "F"
23	}
24	nbits <- dataSize(raster@file@datanotation) * 8
25    dtype <- paste(dd, nbits, sep="")
26	cat("DATA_TYPE ",  dtype, "\n", file = thefile)
27#U1, U2, U4, U8, U16, U32
28#S16, S32
29#F32, and F64.
30	if (.Platform$endian == "little") { btorder <- "LSB"
31	} else { btorder <- "MSB" }
32	cat("BYTE_ORDER ", btorder, "\n", file = thefile)
33#Required for DATA_TYPE values of U16, S16, U32, S32
34
35	cat("FORMAT ", "BIL", "\n", file = thefile)
36	cat("DATA_OFFSET 0\n", file = thefile)
37	cat("END_RAW_FILE\n", file = thefile)
38
39	cat("\n\n", file = thefile)
40	cat("The below is additional metadata, not part of the ERDAS raw format\n", file = thefile)
41	cat("----------------------------------------------------------------\n", file = thefile)
42	cat("CREATOR=R package:raster\n", file = thefile)
43	cat("CREATED=", format(Sys.time(), "%Y-%m-%d %H:%M:%S"), "\n", file = thefile)
44	cat("Projection=", proj4string(raster), "\n", file = thefile)
45	cat("MinValue=",  minValue(raster), "\n", file = thefile)
46	cat("MaxValue=",  maxValue(raster), "\n", file = thefile)
47	close(thefile)
48
49	.worldFile(raster, ".rww")
50 }
51
52