get_datatype: Data type of an LPJmL input/output file

View source: R/get_datatype.R

get_datatypeR Documentation

Data type of an LPJmL input/output file

Description

Provides information on the data type used in an LPJmL input/output file based on the 'datatype' attribute included in the file header.

Usage

get_datatype(header, fail = TRUE)

Arguments

header

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").

fail

Determines whether the function should fail if the datatype is invalid (default: TRUE).

Value

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.

See Also

  • 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.

Examples

## Not run: 
# 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)

## End(Not run)


lpjmlkit documentation built on March 31, 2023, 9:35 p.m.