@@ -146,7 +146,7 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
146146 jc.files <- list.files(path = file.dir ,pattern = output.file.ext ,
147147 full.names = FALSE )
148148 jc.files <- jc.files [- grep(pattern = " Diagnostic" ,jc.files )]
149- num.files = length(jc.files )
149+ num.files <- length(jc.files )
150150
151151 # ## Check whether files exist. STOP if files missing and display an error
152152 if (num.files < 1 ){
@@ -188,9 +188,7 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
188188 }
189189
190190 # ## Display info to console
191- tmp = unlist(strsplit(file.dir ,dlm ))
192- current = tmp [length(tmp )]
193- print(paste(" ----- Processing directory: " ,current ) )
191+ print(paste(" ----- Processing directory: " ,file.dir ) )
194192 flush.console() # <--- show output in real-time
195193
196194 j <- 1 # <--- Numeric counter for progress bar
@@ -234,7 +232,7 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
234232 dims <- dim(good.spec )
235233
236234 # ## Update diagnostic info
237- for (i in 1 : dim(in .spec3 )[1 ]) {
235+ for (i in seq_along( 1 : dim(in .spec3 )[1 ]) ) {
238236 info [i ,1 ] <- as.character(in .spec3 [i ,1 ])
239237 if (as.character(in .spec3 [i ,1 ]) %in% as.character(bad.spec [,1 ])) {
240238 info [i ,2 ] <- " Failed"
@@ -250,8 +248,9 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
250248 flush.console() # <--- show output in real-time
251249
252250 # #### Initial average
253- ind <- factor (as.numeric(good.spec $ Spectra ))
254- 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 ]])
255254 if (dim(mat.data )[1 ]== 0 ){
256255 stop(" ******** ERROR: Bias threshold too strict, no remaining spectra to average. Please correct ********" )
257256 }
@@ -266,25 +265,21 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
266265 names(spec.avg ) <- c(" Spectra" ,waves )
267266
268267 # ## Get spectra sdev
269- dims <- dim(good.spec )
270- spec.sdev <- aggregate(. ~ Spectra ,data = good.spec [,2 : dims [2 ]],sd ,simplify = TRUE )
271- spec.sdev [is.na(spec.sdev )]= 0
268+ spec.sdev <- aggregate(. ~ Spectra ,data = good.spec [,2 : dim(good.spec )[2 ]],sd ,simplify = TRUE )
269+ spec.sdev [is.na(spec.sdev )] <- 0
272270 spec.avg.names <- spec.avg $ Spectra
273- dims <- dim(spec.avg )
274271 rm(mat.data ,ind )
275272
276273 # Debug
277274 # plot(seq(350,2500,1),unlist(spec.sdev[9,2:dim(spec.sdev)[2]]),ylim=c(0,0.04),type="l")
278275
279-
280276 # ## Setup spectral checks
281277 dims <- dim(spec.avg )
282- n <- rle(as.numeric(good.spec $ Spectra ))$ lengths
283- spec.upper <- spec.avg [, 2 : dims [ 2 ]] + ( spec.sdev [, 2 : dims [ 2 ]] * outlier.cutoff ) # 2*SD outlier check
284- # 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
285281 spec.upper <- data.frame (Spectra = spec.avg.names ,spec.upper )
286- spec.lower <- spec.avg [,2 : dims [2 ]] - (spec.sdev [,2 : dims [2 ]]* outlier.cutoff )
287- # 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 )
288283 spec.lower <- data.frame (Spectra = spec.avg.names ,spec.lower )
289284
290285 # ## For debugging
@@ -300,11 +295,12 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
300295 flush.console() # <--- show output in real-time
301296
302297 # Do second sdev check of spectra
303- ind <- as.numeric(good.spec $ Spectra )
298+ # ind <- as.numeric(good.spec$Spectra)
299+ ind <- good.spec $ Spectra
304300 good.spec2 <- as.matrix(good.spec [,- c(1 ,2 )])
305301 spec.upper <- as.matrix(spec.upper [,- 1 ])
306302 spec.lower <- as.matrix(spec.lower [,- 1 ])
307- for (i in 1 : length(ind )){
303+ for (i in seq_along( 1 : length(ind ))) {
308304 ind.avg <- ind [i ]
309305 orig.spec.name <- good.spec [i ,1 ]
310306 # run check of individual spectra against the group mean
@@ -318,8 +314,8 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
318314 window <- 151 : 1351 # 500 - 1700
319315 } else if (spec.type == " Canopy" ) {
320316 # window <- c(151:1441) # 500 - 1790
321- window <- c(151 : 1441 ,1671 : 1901 )
322- }
317+ window <- c(151 : 1000 , 1080 : 1441 , 1671 : 1901 )
318+ }
323319 if (any(good.spec2 [i ,window ]> spec.upper [ind.avg ,window ]) ||
324320 any(good.spec2 [i ,window ] < spec.lower [ind.avg ,window ])) {
325321 bad.spec <- rbind(bad.spec ,good.spec [i ,])
@@ -344,8 +340,9 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
344340 # ## Create final spectral averages
345341 rm(spec.avg )
346342 # spec.avg <- aggregate(.~Spectra,data=good.spec,mean,simplify=TRUE)
347- ind <- factor (as.numeric(good.spec $ Spectra ))
348- 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 ]])
349346 spec.avg <- mApply(mat.data ,ind ,colMeans ,simplify = TRUE )
350347 # ## Reformat spectral data for output
351348 if (is.null(dim(spec.avg )[1 ])){
@@ -360,24 +357,23 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
360357 flush.console() # <--- show output in real-time
361358
362359 # ## Output bad spectra files and figs
363- dims <- dim(bad.spec )
364- if (dims [1 ]> 0 ){
365- for (i in 1 : dims [1 ]){
366- spec.name <- droplevels(bad.spec [i ,1 ])
367- 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 ]])
368364 out.spec <- data.frame (Wavelength = seq(start.wave ,end.wave ,step.size ),
369365 Spectra = out.spec )
370- names(out.spec )= c(" Wavelength" ,paste(spec.name ))
371- out.filename <- paste(droplevels (bad.spec [i ,1 ]),output.file.ext ,sep = " " )
372- 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 )
373369
374370 # Output plot of bad spectra for quick reference
375- if (image == " TRUE" | settings.file $ options $ diagnostic.images == " TRUE" ){
371+ if (image == " TRUE" | settings.file $ options $ diagnostic.images == " TRUE" ) {
376372 rng <- range(out.spec [,2 ])
377373 if (rng [1 ]< 0 ) rng [1 ] <- 0
378374 if (rng [2 ]> 1 ) rng [2 ] <- 1
379375 ylimit <- c(rng [1 ],rng [2 ])
380- 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 )
381377 plot(out.spec [,1 ], out.spec [,2 ],cex = 0.01 ,xlim = c(350 ,2500 ),ylim = ylimit ,xlab = " Wavelength (nm)" ,
382378 ylab = " Reflectance (%)" , main = out.filename ,cex.axis = 1.3 ,cex.lab = 1.3 )
383379 lines(out.spec [,1 ], out.spec [,2 ],lwd = 2 )
@@ -386,20 +382,19 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
386382 dev.off()
387383 }
388384 }
389- rm(spec.name ,out.spec ,out.filename , dims )
385+ rm(spec.name ,out.spec ,out.filename )
390386 } # End of output of bad spectra
391387
392388 # Output good spec
393- dims <- dim(spec.avg )
394- if (dims [1 ]> 0 ){
395- for (i in 1 : dims [1 ]){
396- spec.name <- droplevels(spec.avg [i ,1 ])
397- 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 ]])
398393 out.spec <- data.frame (Wavelength = seq(start.wave ,end.wave ,step.size ),
399394 Spectra = out.spec )
400395 names(out.spec )= c(" Wavelength" ,paste(spec.name ))
401396 out.filename <- paste(spec.name ,output.file.ext ,sep = " " )
402- 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 )
403398
404399 # ## Output plot of average spectra for quick reference
405400 if (image == " TRUE" | settings.file $ options $ diagnostic.images == " TRUE" ){
@@ -420,16 +415,15 @@ average.spec <- function(file.dir=NULL,out.dir=NULL,spec.type="Reflectance",star
420415 }
421416
422417 # ## Write out concatenated averaged spectra
423- 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 )
424419
425420 # ## Output diagnostic info
426- 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 )
427422
428423 # ## Output plot of averaged spectra for quick reference
429- dims <- dim(spec.avg )
430424 if (image == " TRUE" | settings.file $ options $ diagnostic.images == " TRUE" ){
431- if (dims [1 ]> 0 ){
432- 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 ]])
433427 png(file = paste(out.dir ,dlm ," Averaged_Spectra" ," .png" ,sep = " " ),width = 800 ,height = 600 ,res = 100 )
434428 matplot(seq(start.wave ,end.wave ,step.size ), t(plot.data ),cex = 0.01 ,xlim = c(350 ,2500 ),ylim = c(0 ,1 ),
435429 xlab = " Wavelength (nm)" ,ylab = " Reflectance (%)" , main = " Averaged Spectra" ,
0 commit comments