R/wasim.read.table.R

#internal function to read WASIM-output files
`wasim.read.table` <- function (name, na.strings=c("999", "999.00","9999", "9999.00","9999.000", "9999.000000","-9999","-9999.0","-9999.00") #strings indicating missing data
){

   if(!file.exists(name)){
      warning(paste(name," does not exist"))
      return(NULL)
   }
   cf <- count.fields(name, blank.lines.skip = FALSE)
   col.count <- as.integer(names(which.max(table(cf))))
   max.row <- suppressWarnings(min(which(cf[10:length(cf)]!=col.count)))
   if(is.infinite(max.row))
        max.row=length(cf)
   col <- paste("c", 1:(col.count-4), sep="")
   cols <- c("Year","Month","Day","Hour",col)

    colClasses=c(rep("integer",4),rep("numeric",col.count-4))   #specify data format of column (increases speed, catches "NAN" etc. in output)  


    head <- readLines(name, n=10)
    endhead <- suppressWarnings(min(grep("^[0-9]", head))-1)
    if(is.infinite(endhead)) endhead <- length(head)
    head <- head[1:endhead]
    head.split <- strsplit(head, "[ \t]+")
    
    col.names <-  head.split[[endhead]]

    if(endhead == max.row){
            warning(paste("Empty file",name,"not included"))
            return(NULL)
    } else {
        table <- read.table(name,row.names=NULL, na.strings=na.strings, skip=endhead, nrows=max.row-endhead , col.names=cols, colClasses=colClasses)
         table[is.na(table)]=NaN     #mark NaNs to be distinguished from NAs        
        ind <- wasim.assemble.date(table)
        

        if(length(col.names)==length(cols)){
               cols=col.names
        }
        
        t.data <- as.matrix(table)[,5:col.count, drop=FALSE]
        dimnames(t.data) <- list(paste("r",1:NROW(t.data),sep=""), col.names[5:col.count])
        drop.all.na <- which(colSums(is.na(t.data))==NROW(t.data))
        if(length(drop.all.na)>0){
           warning( paste("Removing stations from",name,"with NA values only:", paste(names(drop.all.na), collapse=",")))
           t.data <- t.data[,-drop.all.na]
           for(headrow in 2:length(head.split)){
                head.split[[headrow]] <- head.split[[headrow]][-(drop.all.na+4)]
           }
        }
    }

    z.data <- zoo(order.by=ind, x=t.data)
     attr(z.data, "head") <- head.split

    return(z.data)

}

Try the RHydro package in your browser

Any scripts or data that you put into this service are public.

RHydro documentation built on May 2, 2019, 6:24 p.m.