diff --git a/R/read_fs_mgh.R b/R/read_fs_mgh.R index 6e9bac6..fc7d93f 100755 --- a/R/read_fs_mgh.R +++ b/R/read_fs_mgh.R @@ -44,11 +44,9 @@ read.fs.mgh <- function(filepath, is_gzipped = "AUTO") { } if (is_gz) { - cat(sprintf("*Parsing header of file '%s', assuming it is gz-compressed.\n", filepath)); fh = gzfile(filepath, "rb"); } else { - cat(sprintf("*Parsing header of file '%s', assuming it is NOT gz-compressed.\n", filepath)); fh = file(filepath, "rb"); } @@ -60,32 +58,22 @@ read.fs.mgh <- function(filepath, is_gzipped = "AUTO") { dtype = readBin(fh, integer(), n = 1, endian = "big"); dof = readBin(fh, integer(), n = 1, endian = "big"); - - cat(sprintf(" v=%d, ndim1=%d, ndim2=%d, ndim3=%d, nframes=%d, type=%d, dof=%d.\n", v, ndim1, ndim2, ndim3, nframes, dtype, dof)); - - unused_header_space_size_left = 256; ras_good_flag = readBin(fh, numeric(), n = 1, endian = "big"); if(ras_good_flag == 1) { - cat(sprintf(" 'RAS good' flag is set, reading RAS information.\n")); delta = readBin(fh, numeric(), n = 3, endian = "big"); Mdc = readBin(fh, numeric(), n = 9, endian = "big"); Pxyz_c = readBin(fh, numeric(), n = 3, endian = "big"); RAS_space_size = (3*4 + 4*3*4); - cat(sprintf(" Read %d bytes of RAS information.\n", RAS_space_size)); unused_header_space_size_left = unused_header_space_size_left - RAS_space_size; - } else { - cat(sprintf(" 'RAS good' flag is NOT set, no RAS information to read.\n")); } - cat(sprintf(" There are %d unused header bytes left that will be skipped (data starts at fixed index).\n", unused_header_space_size_left)); # Skip to end of header/beginning of data seek(fh, where = unused_header_space_size_left, origin = "current"); nv = ndim1 * ndim2 * ndim3 * nframes; # number of voxels volsz = c(ndim1, ndim2, ndim3, nframes); - cat(sprintf(" Expecting %d voxels total.\n", nv)); # Determine size of voxel data, depending on dtype from header above MRI_UCHAR = 0; @@ -111,19 +99,14 @@ read.fs.mgh <- function(filepath, is_gzipped = "AUTO") { } else { stop(sprintf(" ERROR: Unexpected data type found in header. Expected one of {0, 1, 3, 4} (%s) but got %d.\n", dt_explanation, dtype)); } - cat(sprintf(" Data type found in header is %d (%s), with %d bytes per voxel.\n", dtype, dt_explanation, nbytespervox)); num_read = prod(length(data)); - if (num_read == nv) { - cat(sprintf(" OK, read %d voxel values as expected.\n", num_read)); - } else { - cat(sprintf(" ERROR: read %d voxel values but expected to read %d.\n", num_read, nv)); - quit(status=1); + if (num_read != nv) { + stop(sprintf(" ERROR: read %d voxel values but expected to read %d.\n", num_read, nv)); } # Reshape to expected dimensions data = array(data, dim = c(ndim1, ndim2, ndim3, nframes)); - cat(sprintf(" Reshaped array to expected dimensions: %s\n", paste(dim(data), collapse = ' '))); close(fh); return(data); }