# $Id: CDFgetdata,v 1.8 1993/08/26 19:47:44 mintha Exp $ # # Get the data for a specified variable # Given the global structure and varid # # $Log: CDFgetdata,v $ # Revision 1.8 1993/08/26 19:47:44 mintha # Fixes dnames attribute to be a character vector not a list # # Revision 1.7 1993/08/26 19:22:09 mintha # Added dnames attribute to variable (Dimension names) # # Revision 1.6 1993/08/26 10:11:27 mintha # Better error checking for invalid var names # # Revision 1.5 1993/08/25 21:34:58 mintha # Splus parse seems to have strange behavior, calling C routine to parse strings. # # Revision 1.4 1993/08/19 00:52:00 mintha # Further fixed dim names lengths. If the number of labels is not # equal to the length of that dimension then NULL even if we are # only taking a hyperslab which there are labels for. # # Revision 1.3 1993/08/19 00:39:20 mintha # Only add dim labels if there is a label for each element otherwise NULL # # Revision 1.2 1993/08/13 22:23:07 mintha # Added "-1" parameter which is replaced by the remaining length for # the count. (startdims & slicedims replaced by start & count) # # Revision 1.1 1993/08/10 23:28:20 mintha # Initial revision # "CDFgetdata" <- function(info, var, start=NULL, count=NULL) { if(is.character(var)) var <- match(var, names(info), nomatch=-1)-2 if(var < 0 || var > info$global$numvars) stop("Variable specified does not exist\n"); varinfo <- info[[var+2]] # # Get the Data # if(is.null(start)) start <- rep(1, length(varinfo$dims)) if(is.null(count)) count <- rep(-1, length(varinfo$dims)) # # Any parameter not given for count will be set to the rest of the array. # for(ctr in seq(to = length(varinfo$dims))){ if(count[ctr] == -1) count[ctr] <- varinfo$dims[ctr] - start[ctr] + 1 } datasize <- prod(count) type <- varinfo$type data <- .C("cdf_read_data", as.integer(info$global$ncid), as.integer(var), as.integer(type), as.integer(rev(start-1)), as.integer(rev(count)), cdata = character(datasize), fdata = single(datasize), ddata = double(datasize)) if(type == 2) vdata <- array(data$cdata, dim=count) if(type == 5) vdata <- array(data$fdata, dim=count) if(type == 6) vdata <- array(data$ddata, dim=count) dlabels <- vector("list", length(varinfo$dims)) dnames <- character(length(varinfo$dims)) for(ctr in seq(to = length(varinfo$dims))){ dnames[ctr] <- info$global$dnames[varinfo$dimids[ctr]+1] if(varinfo$labels[ctr] != "NULL"){ temp <- .C("parse_string", as.character(varinfo$labels[ctr]), strs=character(0), pointers=c(F,T)) if(length(temp$strs) == varinfo$dims[ctr]) dlabels[[ctr]] <- temp$strs[(start[ctr]):(start[ctr]+count[ctr]-1)] } } attr(vdata, "dnames") <- dnames if(length(varinfo$dims) > 1) dimnames(vdata) <- dlabels else names(vdata) <- dlabels[[1]] # # Other attibutes # if(length(varinfo) > 5){ for(ctr in seq(from=6, to=length(varinfo))) attr(vdata, names(varinfo)[[ctr]]) <- varinfo[[ctr]] } vdata }