@@ -64,22 +64,21 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
6464 } else if (! is.null(file.dir )){
6565 file.dir <- file.dir
6666 } else if (is.null(file.dir ) && ! is.null(settings.file $ output.dir )){
67- file.dir <- paste (settings.file $ output.dir ,dlm , " jc_files/ " , sep = " " )
67+ file.dir <- file.path (settings.file $ output.dir ," jc_files" )
6868 }
6969
7070 # ## create output directory if it doesn't already exist
7171 if (! is.null(out.dir )) {
7272 out.dir <- out.dir
7373 } else if (! is.null(settings.file $ output.dir )) {
74- out.dir <- paste (settings.file $ output.dir ,dlm , " averaged_files/ " , sep = " " )
74+ out.dir <- file.path (settings.file $ output.dir ," averaged_files" )
7575 } else {
76- ind <- gregexpr(dlm , file.dir )[[1 ]]
77- out.dir <- paste(substr(file.dir ,ind [1 ], ind [length(ind )- 1 ]- 1 ),dlm ," averaged_files" ,sep = " " )
76+ out.dir <- file.path(dirname(file.dir )," averaged_files" )
7877 }
7978 if (! file.exists(out.dir )) dir.create(out.dir ,recursive = TRUE )
8079
8180 # ## Create bad spectra folder. Spectra not corrected
82- badspec.dir <- paste (out.dir ,dlm , " Bad_Spectra" , sep = " " )
81+ badspec.dir <- file.path (out.dir ," Bad_Spectra" )
8382 if (! file.exists(badspec.dir )) dir.create(badspec.dir ,recursive = TRUE )
8483
8584 # ## Remove any previous output in out.dir
@@ -147,7 +146,7 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
147146 jc.files <- list.files(path = file.dir ,pattern = output.file.ext ,
148147 full.names = FALSE )
149148 jc.files <- jc.files [- grep(pattern = " Diagnostic" ,jc.files )]
150- num.files = length(jc.files )
149+ num.files <- length(jc.files )
151150
152151 # ## Check whether files exist. STOP if files missing and display an error
153152 if (num.files < 1 ){
@@ -164,10 +163,10 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
164163
165164 # ## Spectra metadata.
166165 if (is.null(metadata.file )) {
167- metadata.dir <- gsub(pattern = " jc_files/ " ," " ,file.dir )
166+ metadata.dir <- gsub(pattern = " jc_files" ," " ,file.dir )
168167 metadata.file <- list.files(path = metadata.dir ,pattern = " metadata" ,full.names = FALSE )
169168 print(paste(" ------- Using metadata file: " ,metadata.file ,sep = " " ))
170- metadata <- read.csv(paste (settings.file $ output.dir ,dlm , metadata.file , sep = " " ))
169+ metadata <- read.csv(file = file.path (settings.file $ output.dir ,metadata.file ))
171170 } else {
172171 metadata <- read.csv(metadata.file ,header = T )
173172 }
@@ -189,9 +188,7 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
189188 }
190189
191190 # ## Display info to console
192- tmp = unlist(strsplit(file.dir ,dlm ))
193- current = tmp [length(tmp )]
194- print(paste(" ----- Processing directory: " ,current ) )
191+ print(paste(" ----- Processing directory: " ,file.dir ) )
195192 flush.console() # <--- show output in real-time
196193
197194 j <- 1 # <--- Numeric counter for progress bar
@@ -235,9 +232,9 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
235232 dims <- dim(good.spec )
236233
237234 # ## Update diagnostic info
238- for (i in 1 : dim(in .spec3 )[1 ]){
239- info [i ,1 ] <- as.character(droplevels( in .spec3 [i ,1 ]) )
240- if (as.character(droplevels( in .spec3 [i ,1 ])) %in% droplevels (bad.spec [,1 ])){
235+ for (i in seq_along( 1 : dim(in .spec3 )[1 ])) {
236+ info [i ,1 ] <- as.character(in .spec3 [i ,1 ])
237+ if (as.character(in .spec3 [i ,1 ]) %in% as.character (bad.spec [,1 ])) {
241238 info [i ,2 ] <- " Failed"
242239 info [i ,3 ] <- as.numeric(round(in .spec3 [i ," Wave_450" ],4 ))
243240 info [i ,4 ] <- " No"
@@ -251,8 +248,9 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
251248 flush.console() # <--- show output in real-time
252249
253250 # #### Initial average
254- ind <- factor (as.numeric(good.spec $ Spectra ))
255- mat.data <- as.matrix(good.spec [,3 : dims [2 ]])
251+ # ind <- factor(as.numeric(good.spec$Spectra))
252+ ind <- factor (good.spec $ Spectra )
253+ mat.data <- as.matrix(good.spec [,3 : dim(good.spec )[2 ]])
256254 if (dim(mat.data )[1 ]== 0 ){
257255 stop(" ******** ERROR: Bias threshold too strict, no remaining spectra to average. Please correct ********" )
258256 }
@@ -267,25 +265,21 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
267265 names(spec.avg ) <- c(" Spectra" ,waves )
268266
269267 # ## Get spectra sdev
270- dims <- dim(good.spec )
271- spec.sdev <- aggregate(. ~ Spectra ,data = good.spec [,2 : dims [2 ]],sd ,simplify = TRUE )
272- spec.sdev [is.na(spec.sdev )]= 0
273- spec.avg.names <- droplevels(spec.avg $ Spectra )
274- dims <- dim(spec.avg )
268+ spec.sdev <- aggregate(. ~ Spectra ,data = good.spec [,2 : dim(good.spec )[2 ]],sd ,simplify = TRUE )
269+ spec.sdev [is.na(spec.sdev )] <- 0
270+ spec.avg.names <- spec.avg $ Spectra
275271 rm(mat.data ,ind )
276272
277273 # Debug
278274 # plot(seq(350,2500,1),unlist(spec.sdev[9,2:dim(spec.sdev)[2]]),ylim=c(0,0.04),type="l")
279275
280-
281276 # ## Setup spectral checks
282277 dims <- dim(spec.avg )
283- n <- rle(as.numeric(good.spec $ Spectra ))$ lengths
284- spec.upper <- spec.avg [, 2 : dims [ 2 ]] + ( spec.sdev [, 2 : dims [ 2 ]] * outlier.cutoff ) # 2*SD outlier check
285- # spec.upper <- spec.avg[,2:dims [2]] + (( spec.sdev[,2:dims [2]]/sqrt(n))*2.96)
278+ # n <- rle(as.numeric(good.spec$Spectra))$lengths
279+ n <- rle( good.spec $ Spectra ) $ lengths
280+ spec.upper <- spec.avg [,2 : dim( spec.avg ) [2 ]] + (spec.sdev [,2 : dim( spec.avg ) [2 ]]* outlier.cutoff ) # 2*SD outlier check
286281 spec.upper <- data.frame (Spectra = spec.avg.names ,spec.upper )
287- spec.lower <- spec.avg [,2 : dims [2 ]] - (spec.sdev [,2 : dims [2 ]]* outlier.cutoff )
288- # spec.lower <- spec.avg[,2:dims[2]] - ((spec.sdev[,2:dims[2]]/sqrt(n))*2.96)
282+ spec.lower <- spec.avg [,2 : dim(spec.avg )[2 ]] - (spec.sdev [,2 : dim(spec.avg )[2 ]]* outlier.cutoff )
289283 spec.lower <- data.frame (Spectra = spec.avg.names ,spec.lower )
290284
291285 # ## For debugging
@@ -301,13 +295,14 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
301295 flush.console() # <--- show output in real-time
302296
303297 # Do second sdev check of spectra
304- ind <- as.numeric(good.spec $ Spectra )
298+ # ind <- as.numeric(good.spec$Spectra)
299+ ind <- good.spec $ Spectra
305300 good.spec2 <- as.matrix(good.spec [,- c(1 ,2 )])
306301 spec.upper <- as.matrix(spec.upper [,- 1 ])
307302 spec.lower <- as.matrix(spec.lower [,- 1 ])
308- for (i in 1 : length(ind )){
303+ for (i in seq_along( 1 : length(ind ))) {
309304 ind.avg <- ind [i ]
310- orig.spec.name <- droplevels( good.spec [i ,1 ])
305+ orig.spec.name <- good.spec [i ,1 ]
311306 # run check of individual spectra against the group mean
312307 # need to make sure indices are alligned
313308 # window <- 151:2051 # 500 - 2400
@@ -319,8 +314,8 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
319314 window <- 151 : 1351 # 500 - 1700
320315 } else if (spec.type == " Canopy" ) {
321316 # window <- c(151:1441) # 500 - 1790
322- window <- c(151 : 1441 ,1671 : 1901 )
323- }
317+ window <- c(151 : 1000 , 1080 : 1441 , 1671 : 1901 )
318+ }
324319 if (any(good.spec2 [i ,window ]> spec.upper [ind.avg ,window ]) ||
325320 any(good.spec2 [i ,window ] < spec.lower [ind.avg ,window ])) {
326321 bad.spec <- rbind(bad.spec ,good.spec [i ,])
@@ -345,8 +340,9 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
345340 # ## Create final spectral averages
346341 rm(spec.avg )
347342 # spec.avg <- aggregate(.~Spectra,data=good.spec,mean,simplify=TRUE)
348- ind <- factor (as.numeric(good.spec $ Spectra ))
349- mat.data <- as.matrix(good.spec [,2 : dims [2 ]])
343+ # ind <- factor(as.numeric(good.spec$Spectra))
344+ ind <- factor (good.spec $ Spectra )
345+ mat.data <- as.matrix(good.spec [,2 : dim(good.spec )[2 ]])
350346 spec.avg <- mApply(mat.data ,ind ,colMeans ,simplify = TRUE )
351347 # ## Reformat spectral data for output
352348 if (is.null(dim(spec.avg )[1 ])){
@@ -361,24 +357,23 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
361357 flush.console() # <--- show output in real-time
362358
363359 # ## Output bad spectra files and figs
364- dims <- dim(bad.spec )
365- if (dims [1 ]> 0 ){
366- for (i in 1 : dims [1 ]){
367- spec.name <- droplevels(bad.spec [i ,1 ])
368- out.spec <- t(bad.spec [i ,3 : dims [2 ]])
360+ if (dim(bad.spec )[1 ]> 0 ){
361+ for (i in seq_along(1 : dim(bad.spec )[1 ])) {
362+ spec.name <- as.character(bad.spec [i ,1 ])
363+ out.spec <- t(bad.spec [i ,3 : dim(bad.spec )[2 ]])
369364 out.spec <- data.frame (Wavelength = seq(start.wave ,end.wave ,step.size ),
370365 Spectra = out.spec )
371- names(out.spec )= c(" Wavelength" ,paste(spec.name ))
372- out.filename <- paste(droplevels (bad.spec [i ,1 ]),output.file.ext ,sep = " " )
373- write.csv(out.spec ,paste (badspec.dir ,dlm , out.filename , sep = " " ),row.names = FALSE )
366+ names(out.spec ) <- c(" Wavelength" ,paste(spec.name ))
367+ out.filename <- paste(as.character (bad.spec [i ,1 ]),output.file.ext ,sep = " " )
368+ write.csv(x = out.spec ,file = file.path (badspec.dir ,out.filename ),row.names = FALSE )
374369
375370 # Output plot of bad spectra for quick reference
376- if (image == " TRUE" | settings.file $ options $ diagnostic.images == " TRUE" ){
371+ if (image == " TRUE" | settings.file $ options $ diagnostic.images == " TRUE" ) {
377372 rng <- range(out.spec [,2 ])
378373 if (rng [1 ]< 0 ) rng [1 ] <- 0
379374 if (rng [2 ]> 1 ) rng [2 ] <- 1
380375 ylimit <- c(rng [1 ],rng [2 ])
381- png(file = paste (badspec.dir ,dlm , spec.name ," .png" , sep = " " ),width = 800 ,height = 600 ,res = 100 )
376+ png(file = file.path (badspec.dir ,paste0( spec.name ," .png" ) ),width = 800 ,height = 600 ,res = 100 )
382377 plot(out.spec [,1 ], out.spec [,2 ],cex = 0.01 ,xlim = c(350 ,2500 ),ylim = ylimit ,xlab = " Wavelength (nm)" ,
383378 ylab = " Reflectance (%)" , main = out.filename ,cex.axis = 1.3 ,cex.lab = 1.3 )
384379 lines(out.spec [,1 ], out.spec [,2 ],lwd = 2 )
@@ -387,20 +382,19 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
387382 dev.off()
388383 }
389384 }
390- rm(spec.name ,out.spec ,out.filename , dims )
385+ rm(spec.name ,out.spec ,out.filename )
391386 } # End of output of bad spectra
392387
393388 # Output good spec
394- dims <- dim(spec.avg )
395- if (dims [1 ]> 0 ){
396- for (i in 1 : dims [1 ]){
397- spec.name <- droplevels(spec.avg [i ,1 ])
398- out.spec <- t(spec.avg [i ,2 : dims [2 ]])
389+ if (dim(spec.avg )[1 ]> 0 ){
390+ for (i in seq_along(1 : dim(spec.avg )[1 ])) {
391+ spec.name <- as.character(spec.avg [i ,1 ])
392+ out.spec <- t(spec.avg [i ,2 : dim(spec.avg )[2 ]])
399393 out.spec <- data.frame (Wavelength = seq(start.wave ,end.wave ,step.size ),
400394 Spectra = out.spec )
401395 names(out.spec )= c(" Wavelength" ,paste(spec.name ))
402396 out.filename <- paste(spec.name ,output.file.ext ,sep = " " )
403- write.csv(out.spec ,paste (out.dir ,dlm , out.filename , sep = " " ),row.names = FALSE )
397+ write.csv(x = out.spec ,file = file.path (out.dir ,out.filename ),row.names = FALSE )
404398
405399 # ## Output plot of average spectra for quick reference
406400 if (image == " TRUE" | settings.file $ options $ diagnostic.images == " TRUE" ){
@@ -421,16 +415,15 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
421415 }
422416
423417 # ## Write out concatenated averaged spectra
424- write.csv(spec.avg ,paste (out.dir ,dlm , " Averaged_Spectra" ,output.file.ext , sep = " " ),row.names = FALSE )
418+ write.csv(x = spec.avg ,file = file.path (out.dir ,paste0( " Averaged_Spectra" ,output.file.ext ) ),row.names = FALSE )
425419
426420 # ## Output diagnostic info
427- write.csv(info ,paste (out.dir ,dlm , " Spectra_Diagnostic_Information" ," .txt" , sep = " " ),row.names = FALSE )
421+ write.csv(x = info ,file = file.path (out.dir ,paste0( " Spectra_Diagnostic_Information" ," .txt" ) ),row.names = FALSE )
428422
429423 # ## Output plot of averaged spectra for quick reference
430- dims <- dim(spec.avg )
431424 if (image == " TRUE" | settings.file $ options $ diagnostic.images == " TRUE" ){
432- if (dims [1 ]> 0 ){
433- plot.data <- as.matrix(spec.avg [,2 : dims [2 ]])
425+ if (dim( spec.avg ) [1 ]> 0 ){
426+ plot.data <- as.matrix(spec.avg [,2 : dim( spec.avg ) [2 ]])
434427 png(file = paste(out.dir ,dlm ," Averaged_Spectra" ," .png" ,sep = " " ),width = 800 ,height = 600 ,res = 100 )
435428 matplot(seq(start.wave ,end.wave ,step.size ), t(plot.data ),cex = 0.01 ,xlim = c(350 ,2500 ),ylim = c(0 ,1 ),
436429 xlab = " Wavelength (nm)" ,ylab = " Reflectance (%)" , main = " Averaged Spectra" ,
0 commit comments