R/readNoiseBandData.R

Defines functions readNoiseBandData

Documented in readNoiseBandData

#' @title Read Noise Band Data
#' 
#' @description Reads binary data stored by the Noise Band Monitor.
#'   
#' @param fid binary file identifier
#' @param fileInfo structure holding the file header and module header
#' @param data a structure containing standard data
#' @param debug logical flag to show more info on errors
#' @param \dots Arguments passed to other functions
#' 
#' @return a structure containing data from a single object, and a logical
#'   flag if an error has occurred
#' 
#' @author Taiki Sakai \email{taiki.sakai@@noaa.gov}
#' 
readNoiseBandData <- function(fid, fileInfo, data, debug=FALSE, ...) {
    error <- FALSE
    
    tryCatch({
        dataLength <- pamBinRead(fid, 'int32', n=1)
        if(dataLength==0) {
            return(list(data=data, error=error))
        }
        
        version <- fileInfo$moduleHeader$version
        
        if(version <= 2) {
            data$startSample <- pamBinRead(fid, 'int64', n=1)
            data$channelMap <- pamBinRead(fid, 'int32', n=1)
        }
        
        data$rms <- pamBinRead(fid, 'int16', n=1) / 100
        data$zeroPeak <- pamBinRead(fid, 'int16', n=1) / 100
        data$peakPeak <- pamBinRead(fid, 'int16', n=1) / 100
        
        if(version >= 2) {
            data$sel <- pamBinRead(fid, 'int16', n=1) / 100
            data$selSecs <- pamBinRead(fid, 'int16', n=1)
        }
        
        return(list(data=data, error=error))
    # }, warning = function(w) {
    #     print(paste('Warning occurred: ', w))
    #     return(list(data=data, error=error))
    }, error = function(e) {
        if(debug) {
            print(paste0('Error reading ', fileInfo$fileHeader$moduleType, ' Data read:'))
            print(data)
            print(e)
        }
        error <- TRUE
        return(list(data=data, error=error))
    })
}

Try the PamBinaries package in your browser

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

PamBinaries documentation built on May 29, 2024, 12:29 p.m.