macroReadBin-methods: Read bin file from the Soil and MACRO models.

macroReadBinR Documentation

Read bin file from the Soil and MACRO models.

Description

Read bin file from the Soil and MACRO models, including MACRO intermediate-files for metabolite. Adapted by Kristian Persson from an "anonymous" SLU original code . R code vectorisation by Julien Moeys.

Usage

macroReadBin(f, ...)

## S3 method for class 'character'
macroReadBin(
  f,
  header = TRUE,
  rmSuffixes = TRUE,
  trimLength = integer(),
  rmNonAlphaNum = TRUE,
  rmSpaces = TRUE,
  rmRunID = TRUE,
  dateMethod = 1L,
  tz = "GMT",
  ...
)

Arguments

f

Vector of character strings or a single connection to a binary file. If a vector character strings, it should be the name(s) of the binary file(s) which the data are to be read from. The path of the file(s) may be provided as well, if the file(s) is (are) not in the working directory.

...

Additional options passed to specific methods and to readBin

header

Single logical. If TRUE the header is present in the bin file, if FALSE it is not present.

rmSuffixes

If TRUE, the code automatically tries to identify non alpha-numeric trailing characters following the column name. Contrary to trimLength (see below), this is a generic method independent of the type of bin-files (input, output of parent substances, output of metabolites), but is does not work 100 percent correct.

trimLength

Single integer value. Number of characters expected for column names. All characters beyond trimLength are trimmed. Default to trimLength = integer(0), meaning that the column names is not trimmed to a fixed length. The appropriate length depend on the type of bin-file.

rmNonAlphaNum

Single logical. If TRUE remove all non alpha-numeric characters from the column names (and replace them by underscores). See also the alphaNum parameter. Use this option to obtain database compatible column names. If gui is TRUE, rmNonAlphaNum is ignored, and a menu will ask you what to do.

rmSpaces

Single logical. If TRUE remove extra spaces and minus signs in column names and replace them by underscores _. Multiple spaces are grouped. Trailing (end) space(s) are always removed (whatever is the value of rmSpaces). If gui is TRUE, rmSpaces is ignored, and a menu will ask you what to do.

rmRunID

Single logical. If TRUE remove the simulation ID at the end of each column name. rmSpaces must be TRUE for using this option (otherwise ignored). If gui is TRUE, rmRunID is ignored, and a menu will ask you what to do.

dateMethod

Single integer. If 1 uses a new (shorter) method for converting dates (from the weird bin file format to POSIXct), if 2 uses the old / slower method implemented for the SOIL model (and MACRO?) and if 0 (or any other value than 1 or 2) returns the original date in minutes since 2 days before the 1st of January of year 0001 at 00:00. For 1 and 2 the date returned is POSIXct with time-zone tz, and for 0 it is integers.

tz

Single character string. "A timezone specification to be used for the conversion. System-specific (see as.POSIXlt), but "" is the current time zone, and "GMT" is UTC".

Details

Some global arguments can be set-up and retrieved via muPar and getMuPar. Please check the help page of these functions if you need to tune macroReadBin.

Value

Returns a data.frame with the content of the bin file. If length(f) > 1, then a list of data.frame is returned instead. The Date column in the bin file is converted from "Julian Date" into POSIXct date format.

See Also

readBin.

Examples


library( "macroutils2" )

# ====== Example 1: MACRO In FOCUS output file ====== 

#   Path to the file to be read
( filenm <- system.file( 
    "bintest/chat_winCer_GW-D_1kgHa_d298_annual_output.bin", 
    package = "macroutils2", mustWork = TRUE ) )

#   Read the file - Generic method for reading column names 
#   (result not always 100 percent clean)
tmp1 <- macroReadBin( f = filenm ) 

#   Read the file - Trim the column names to a known length 
#   and do not perform any further column name cleaning.
#   Optimal length depend on the type of bin-file.
tmp2 <- macroReadBin( f = filenm, trimLength = 52L, 
    rmSuffixes = FALSE, rmNonAlphaNum = FALSE, 
    rmSpaces = FALSE, rmRunID = FALSE ) 

#   Read the file - No column names cleaning
tmp3 <- macroReadBin( f = filenm, rmSuffixes = FALSE, 
    rmNonAlphaNum = FALSE, rmSpaces = FALSE, 
    rmRunID = FALSE ) 

colnames( tmp1 ) 
colnames( tmp2 )
colnames( tmp3 ) 

dim( tmp1 ) 
dim( tmp2 )
dim( tmp3 ) 

#   Clean-up
rm( filenm, tmp1, tmp2, tmp3 )



# ====== Example 2: MACRO metabolite intermediate-file ====== 

#   Path to the file to be read
( filenm <- system.file( 
    "bintest/chat_winCer_GW-C_1kgHa_d298_y1926_int-file.bin", 
    package = "macroutils2", mustWork = TRUE ) ) 

#   Note: this file has been shortened to only 1 year of data    

#   Read the file
tmp1 <- macroReadBin( f = filenm ) 

#   Using different settings
tmp3 <- macroReadBin( f = filenm, rmNonAlphaNum = FALSE, 
    rmSpaces = FALSE, rmRunID = FALSE ) 

colnames( tmp1 ) # Some column names are not fully cleaned
colnames( tmp3 )

dim( tmp1 ) 
dim( tmp3 )

#   Clean-up
rm( filenm, tmp1, tmp3 )

julienmoeys/macroutils2 documentation built on Feb. 28, 2024, 2:17 a.m.