Provides information on the data type used in an LPJmL input/output file based on the 'datatype' attribute included in the file header.
get_datatype(header, fail = TRUE)Header list object as returned by read_header() or
create_header(). Alternatively, can be a single integer just giving the
data type code or a single character string giving one of the LPJmL type
names c("byte", "short", "int", "float", "double").
Determines whether the function should fail if the datatype is
invalid (default: TRUE).
On success, the function returns a list object with three components:
type: R data type; can be used with what parameter of readBin().
size: size of data type; can be used with size parameter of
readBin().
signed: whether or not the data type is signed; can be used with signed
parameter of readBin().
If fail = FALSE, the function returns NULL if an invalid datatype is
provided.
read_header() for reading headers from LPJmL input/output files.
create_header() for creating headers from scratch.
get_headersize() for determining the size of file headers.
if (FALSE) {
# Read file header
header <- read_header("filename.clm")
# Open file for reading
fp <- file("filename.clm", "rb")
# Skip over file header
seek(fp, get_headersize(header))
# Read in file data
file_data <- readBin(
fp,
what = get_datatype(header)$type,
size = get_datatype(header)$size,
signed = get_datatype(header)$signed,
n = header$header["ncell"] * header$header["nbands"] *
header$header["nyear"] * header$header["nstep"],
endian = header[["endian"]]
)
# Close file
close(fp)
}