PageRenderTime 36ms CodeModel.GetById 32ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/R/read.unformatted.R

http://github.com/holstius/aermod
R | 36 lines | 24 code | 0 blank | 12 comment | 0 complexity | 3f47451c846d71c821fcb3fdae001fab MD5 | raw file
 1#'
 2#' Read an "unformatted" (binary) AERMOD output file.
 3#'
 4#' @param filename file to read
 5#' @param n.receptors number of receptors in the model
 6#' @param endian (optional)
 7#' @return a list of records, one per modelled interval. Each record contains a date (to the hour); 
 8#'   the length of the averaging period; the group ID; and a list of concentrations, one per receptor.
 9#' @export
10#' @references
11#' EPA (2004). User's Guide for the AMS/EPA Regulatory Model AERMOD. EPA-454/B-03-001.
12#'
13read.unformatted <- function(filename, n.receptors, endian=.Platform$endian) {
14	require(lubridate)
15	con <- file(filename, open='rb')
16	read.record <- function(con) {
17		YYMMDDHH <- readBin(con, 'integer', size=4, signed=FALSE, endian=endian)
18		avg.period <- readBin(con, 'integer', size=4, signed=FALSE, endian=endian)
19		group.id <- readChar(con, 8)
20		concentrations <- readBin(con, 'double', n=n.receptors, endian=endian)
21		return(list(
22			date = as.Date(ymd(floor(YYMMDDHH / 100))),
23			hour = YYMMDDHH %% 100,
24			avg.period = avg.period,
25			group.id = sub("[ ]+$", "", group.id),
26			concentrations = concentrations))
27	}	
28	records <- list()
29	while(magic <- length(readBin(con, 'integer', size=4))) {
30		rec <- read.record(con)
31		records <- c(records, list(rec))
32		magic <- readBin(con, 'integer', size=4)
33	}
34	close(con)
35	return(records)
36}