Skip to content

Commit 8c06bd8

Browse files
author
Shawn P. Serbin
committed
Minor updates to read.asd.R. Next test on Windows
1 parent a21d16c commit 8c06bd8

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

R/read.asd.R

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,20 @@ read.asd <- function(file.dir=NULL,out.dir=NULL,spec.type=NULL,start.wave=NULL,e
5656
# Rathter than reading/writing for each file in serial
5757
## Allow output for single spec files
5858

59-
### Set platform specific file path delimiter. Probably will always be "/"
60-
dlm <- .Platform$file.sep # <--- What is the platform specific delimiter?
61-
6259
### Check for proper input
6360
if (is.null(settings.file) && is.null(file.dir)){
6461
stop("No input file directory given in settings file or function call. Please correct.")
6562
} else if (!is.null(file.dir)){
66-
file.dir <- file.dir
63+
file.dir <- file.path(file.dir)
6764
} else if (!is.null(settings.file$spec.dir)){
68-
file.dir <- settings.file$spec.dir
65+
file.dir <- file.path(settings.file$spec.dir)
6966
}
7067

7168
### create output directory if it doesn't already exist and is set
7269
if (!is.null(out.dir)) {
73-
out.dir <- paste0(out.dir, dlm, "ascii_files/")
70+
out.dir <- file.path(out.dir, "ascii_files/")
7471
} else if (!is.null(settings.file$output.dir)) {
75-
out.dir <- paste0(settings.file$output.dir, dlm, "ascii_files/")
72+
out.dir <- file.path(settings.file$output.dir, "ascii_files/")
7673
}
7774
if (!is.null(out.dir)) {
7875
if (!file.exists(out.dir)) dir.create(out.dir,recursive=TRUE)
@@ -131,7 +128,6 @@ read.asd <- function(file.dir=NULL,out.dir=NULL,spec.type=NULL,start.wave=NULL,e
131128
#--------------------- Begin function -----------------------#
132129

133130
### Run extract.metadata
134-
#print(file.dir)
135131
extract.metadata(file.dir=file.dir,out.dir=gsub(pattern="ascii_files/","",out.dir),instrument="ASD",
136132
spec.file.ext=spec.file.ext,output.file.ext=output.file.ext)
137133

@@ -147,9 +143,12 @@ read.asd <- function(file.dir=NULL,out.dir=NULL,spec.type=NULL,start.wave=NULL,e
147143

148144
### Define wavelengths using file header, if not defined already
149145
if (is.null(start.wave) | is.null(end.wave) | is.null(step.size)) {
150-
start.wave <- extract.metadata(file.dir=asd.files.full[1],instrument="ASD",spec.file.ext=spec.file.ext)$Calibrated_Starting_Wavelength
151-
step.size <- extract.metadata(file.dir=asd.files.full[1],instrument="ASD",spec.file.ext=spec.file.ext)$Calibrated_Wavelength_Step
152-
channels <- extract.metadata(file.dir=asd.files.full[1],instrument="ASD",spec.file.ext=spec.file.ext)$Detector_Channels
146+
start.wave <- extract.metadata(file.dir=asd.files.full[1],instrument="ASD",
147+
spec.file.ext=spec.file.ext)$Calibrated_Starting_Wavelength
148+
step.size <- extract.metadata(file.dir=asd.files.full[1],instrument="ASD",
149+
spec.file.ext=spec.file.ext)$Calibrated_Wavelength_Step
150+
channels <- extract.metadata(file.dir=asd.files.full[1],instrument="ASD",
151+
spec.file.ext=spec.file.ext)$Detector_Channels
153152
end.wave <- start.wave+((channels-1)/step.size)
154153
lambda <- seq(start.wave,end.wave,step.size)
155154
} else {
@@ -182,7 +181,7 @@ read.asd <- function(file.dir=NULL,out.dir=NULL,spec.type=NULL,start.wave=NULL,e
182181
meas <- rep(0, length(lambda))
183182

184183
### Check whether file is an older ASD file. Should update this.
185-
meas = readBin(to.read, what=double(), n=length(lambda), size=4, endian = .Platform$endian)
184+
meas <- readBin(to.read, what=double(), n=length(lambda), size=4, endian = .Platform$endian)
186185
if (range(meas, na.rm=TRUE)[2] < 2){ # Less than 2 for WHITE refl.
187186
close(to.read)
188187
out.spec = array(meas) ### Output spectra array. This is the resulting spectra
@@ -246,7 +245,8 @@ read.asd <- function(file.dir=NULL,out.dir=NULL,spec.type=NULL,start.wave=NULL,e
246245
if (rng[2]>1) rng[2] <- 1
247246
ylimit <- c(rng[1], rng[2])
248247
png(file=paste0(out.dir, dlm, tmp[1], ".png"), width=800, height=600, res=100)
249-
plot(output.spectra[, 1], output.spectra[, 2], cex=0.01, xlim=c(350, 2500), ylim=ylimit, xlab="Wavelength (nm)",
248+
plot(output.spectra[, 1], output.spectra[, 2], cex=0.01, xlim=c(350, 2500),
249+
ylim=ylimit, xlab="Wavelength (nm)",
250250
ylab="Reflectance (%)", main=out.ascii, cex.axis=1.3, cex.lab=1.3)
251251
lines(output.spectra[, 1], output.spectra[, 2], lwd=2)
252252
box(lwd=2.2)
@@ -269,9 +269,12 @@ read.asd <- function(file.dir=NULL,out.dir=NULL,spec.type=NULL,start.wave=NULL,e
269269

270270
### Define wavelengths using file header, if not defined already
271271
if (is.null(start.wave) | is.null(end.wave) | is.null(step.size)) {
272-
start.wave <- extract.metadata(file.dir=file.dir[1],instrument="ASD",spec.file.ext=spec.file.ext)$Calibrated_Starting_Wavelength
273-
step.size <- extract.metadata(file.dir=file.dir[1],instrument="ASD",spec.file.ext=spec.file.ext)$Calibrated_Wavelength_Step
274-
channels <- extract.metadata(file.dir=file.dir[1],instrument="ASD",spec.file.ext=spec.file.ext)$Detector_Channels
272+
start.wave <- extract.metadata(file.dir=file.dir[1],instrument="ASD",
273+
spec.file.ext=spec.file.ext)$Calibrated_Starting_Wavelength
274+
step.size <- extract.metadata(file.dir=file.dir[1],instrument="ASD",
275+
spec.file.ext=spec.file.ext)$Calibrated_Wavelength_Step
276+
channels <- extract.metadata(file.dir=file.dir[1],instrument="ASD",
277+
spec.file.ext=spec.file.ext)$Detector_Channels
275278
end.wave <- start.wave+((channels-1)/step.size)
276279
lambda <- seq(start.wave,end.wave,step.size)
277280
} else {

0 commit comments

Comments
 (0)