-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnpointmerge
executable file
·56 lines (42 loc) · 1.71 KB
/
npointmerge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env Rscript
#### Make sure packages are installed and load them
#rniftilib is not in this location
#if (!suppressPackageStartupMessages(require(Rniftilib))) install.packages("Rniftilib", repos="http://R-Forge.R-project.org")
suppressPackageStartupMessages(library(RNifti))
# collect arguments
args <- commandArgs(TRUE)
if (length(args) < 2) {
stop("Usage: npointmerge outputfile.nii.gz merge1.nii.gz merge2.nii.gz ...")
}
# get name of outputfile
outputfile <- args[1]
# get name of first merge file
firstmerge <- args[2]
# get the number of files to merge
ntomerge <- length(args)-2
###############################################################################
#### Read in the first file to merge. It must exist.
tryCatch({
firstmerge.nii <- readNifti(firstmerge);
}, error=function(e) {
cat("Could not read nifti file: ", firstmerge, "\n")
stop(e)
})
###############################################################################
#### Check to make sure output file does not exist, renaming if it does,
#### and set up the output file
if (file.exists(outputfile)) {
stop(paste("The output file", outputfile, "exists. Please delete any old results before proceeding."))
}
nimdat <- firstmerge.nii
###############################################################################
#### Read in all the files to merge and add them to the output image
while(ntomerge > 0) {
idx <- ntomerge+2
mim <- readNifti(args[idx])
nimdat <- nimdat + mim
ntomerge <- ntomerge -1
}
###############################################################################
#### Write out the merged output image
writeNifti(nimdat, outputfile, template=firstmerge.nii, datatype="float")