Project

General

Profile

« Previous | Next » 

Revision 38aa5c07

Added by Jim Regetz over 12 years ago

  • ID 38aa5c074cc9fec577465c93f5a25924b8cd5a3c

replaced reshape with faster manual split-rbind approach

View differences:

climate/extra/ghcn-to-sqlite.R
79 79
    }
80 80
}
81 81

  
82
# split data columnwise by day, then recombine into long format; note
83
# that the indexing here is hard-coded to work for the *.dly files, and
84
# simply assumes that they are all consistent
85
wideToLong <- function(dat, days) {
86
    daily.data <- lapply(seq_along(days), function(i) {
87
        dat <- data.frame(dat[1:4], day=i, dat[days[[i]]])
88
        dat$srcrowid <- seq(nrow(dat))
89
        names(dat) <- 1:ncol(dat)
90
        dat
91
        })
92
    do.call("rbind", daily.data)
93
}
94

  
95
NUM.WIDE.COLS <- 4 + 4*31
96
DAYS <- lapply(seq(from=5, to=NUM.WIDE.COLS, by=4), function(i) i:(i+3))
82 97
dailies <- list.files(pattern="*.dly")
83 98
for (file in dailies) {
84
    x <- loadAsCSV(file, c("TMAX", "TMIN"))
85
    if (is.null(x)) {
86
        message("no rows imported for ", file)
87
    } else {
88
        long <- reshape(x, direction="long",
89
            varying=matrix(5:ncol(x), nrow=4))
99
    dly <- loadAsCSV(file, c("TMAX", "TMIN"))
100
    if (!is.null(dly)) {
101
        long <- wideToLong(dly, DAYS)
90 102
        ghcn_bulk_insert(db, sql, long)
103
    } else {
104
        message("no rows imported for ", file)
91 105
    }
92 106
}

Also available in: Unified diff