Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.git
^.*\.Rproj$
^\.Rproj\.user$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
62 changes: 62 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

permissions: read-all

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:

- {os: ubuntu-latest, r: 'release'}
# - {os: macos-latest, r: 'release'}
# - {os: windows-latest, r: 'release'}
# - {os: ubuntu-latest, r: 'oldrel-1'}
# - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
PKG_SYSREQS: false

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true
extra-repositories: https://pecanproject.r-universe.dev

- name: dependencies on Linux
if: runner.os == 'Linux'
run: sudo apt-get install -y make pandoc git libssl-dev libgdal-dev gdal-bin libgeos-dev libproj-dev libsqlite3-dev libicu-dev libudunits2-dev librdf0-dev libxml2-dev libfreetype6-dev libjpeg-dev libpng-dev libtiff-dev libfontconfig1-dev libfribidi-dev libharfbuzz-dev libcurl4-gnutls-dev pkg-config


- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck, testthat, PEcAnRTM, rjags
needs: check
dependencies: '"hard"'

- uses: r-lib/actions/check-r-package@v2
with:
error-on: '"error"'
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
settings.xml~
*.RData
*.Rdata

R-FieldSpectra.Rproj
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ Suggests:
spectrolab
Depends:
R (>= 2.10)
Remotes:
meireles/spectrolab
NeedsCompilation: no
SystemRequirements: OS_type: unix, mac
License: GNU GENERAL PUBLIC LICENSE Version 3
Copyright: Authors
LazyLoad: yes
LazyData: FALSE
Encoding: UTF-8
RoxygenNote: 7.1.2
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export(convolve.spectra)
export(extract.metadata)
export(jump.correction)
export(read.asd)
export(read.sig)
export(settings)
export(smooth.spectra)
104 changes: 104 additions & 0 deletions R/read.sig.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#--------------------------------------------------------------------------------------------------------------#
##' Function to read .sig files and extract spectral data
##'
##'This function will read ".sig" files and extract the speczral data. The resulting data frame will
##'have the option for all data as well as selected data , which can be obtained by providing the
##'boolean parameter.
##'
##' @name read.sig
##' @title read .sig files from SVC
##'
##' @param path A character string specifying the path to the directory or a single sig file.
##' @param default A logical value indicating whether to include all columns from the `.sig` files
#' (`TRUE`) or only selected columns (`FALSE`). Default is `TRUE`.
#'
##'
##' @return A data data frame with the following columns :
##' \describe{
##' \item{Wavelengths(nm)}{Numeric vector representing the wavelengths in nanometers.}
##' \item{file}{gives information on the file name}
##' \item{Reflectance(\%)}{Numeric vector of reflectance percentages.}
##' \item{Target Values (optional)}{Provides the targer values}
##' \item{Reference Values (optional)}{Provides information on the reference values}
##' }
##'
##' @details
##' The function can read a single file or multiple files in a directory.
##' The file is then read line by line and the spectral data is extracted from the line after "data=".
##' The extracted data is then converted into a data frame with appropriate names.
##'
##'@examples
##'\dontrun{
##'path <- "/path/to/your/file.sig"
##'data <- read.sig(path, default = FALSE)
##'print(head(data))
##'}
##'
##'@export
##'
##'@author Methun George, Steffen Neumann
##'





read.sig <- function(path, default = TRUE) {
# Check if the path exists
if (!file.exists(path)) {
stop("The path does not exist: ", path)
}

# Check if the path is a directory or a single file
path_info <- file.info(path)
if (path_info$isdir) {
sig_files <- list.files(path = path, pattern = "\\.sig$", full.names = TRUE)
} else {
sig_files <- path
}

# An empty list to store the data frames
all_data <- list()

# Read each file and then combine the data
for (file in sig_files) {
file_content <- readLines(file)
data_start <- grep("^data=", file_content)
data_lines <- file_content[(data_start + 1):length(file_content)]
data <- read.table(text = data_lines, header = FALSE, fill = TRUE)

# Add the filename
name_line <- grep("^name=", file_content, value = TRUE)
if (length(name_line) > 0) {
file_name <- sub("^name=", "", name_line)
} else {
file_name <- basename(file) # Default to file name if not found in metadata
}

if (default) {
# If parameter,'all' is TRUE, include all columns with original column names
data$file_name <- file_name
if (ncol(data) >= 4) {
colnames(data) <- c("Wavelengths(nm)", "Reference Values", "Target Values", "Reflectance(%)", "file_name")
}
} else {
# If 'all' is FALSE, only select the wavelength and reflectance columns
if (ncol(data) >= 4) {
data <- data[, c(1, 4), drop = FALSE]
colnames(data) <- c("Wavelengths(nm)", "Reflectance(%)")
} else {
stop("Data does not have enough columns to extract the required columns!!!")
}
data$file_name <- file_name
}

# Store the data frame in the list
all_data[[length(all_data) + 1]] <- data
}

# Combine all data frames
combined_data <- do.call(rbind, all_data)
return(combined_data)
}


4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<!-- badges: start -->
[![R-CMD-check](https://github.com/georgejr45/R-FieldSpectra/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/georgejr45/R-FieldSpectra/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

# R-FieldSpectra
R library for importing and processing field spectroscopy data collected with ASD, Spectra Vista, and Spectral Evolution instruments

Expand Down
48 changes: 48 additions & 0 deletions man/read.sig.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions tests/testthat/test_openASD.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
context("opening spectra files")

test_that("open ASD spectra file", {
test_that("open single ASD spectra file", {

file.dir <- system.file("extdata/PM01_TIAM_B_LC_REFL00005.asd",package="FieldSpectra")
spec <- read.asd(file.dir, out.dir=file.path('~'), start.wave=350, end.wave=2500, step.size=1)
expect_equal(round(sum(spec$Spectra)), 517)

})
})

# test_that("open multiple ASD spectra file", {
#
# file.dir <- system.file("extdata/",package="FieldSpectra")
# spec <- read.asd(file.dir, out.dir=file.path('~'), start.wave=350, end.wave=2500, step.size=1)
# expect_equal(round(sum(spec$Spectra)), 517)
#
# })
#

33 changes: 33 additions & 0 deletions tests/testthat/test_openSIG.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Create test
test_that("read.sig function works correctly with default parameter", {
# Create a temporary directory and file for testing
temp_dir <- tempdir()
temp_file <- file.path(temp_dir, "test.sig")

# Create a mock .sig file with some sample data
writeLines(c(
"name=test_file.sig",
"data=",
"400 1 2 10",
"500 1 2 20",
"600 1 2 30",
"700 1 2 40"
), temp_file)

filepath <- system.file("extdata/gr070214_003.sig",package="FieldSpectra")

# Test with default = TRUE (all columns)
data_all <- read.sig(filepath, default = TRUE)
expect_equal(ncol(data_all), 5) # Should have 5 columns
expect_equal(colnames(data_all), c("Wavelengths(nm)", "Reference Values", "Target Values", "Reflectance(%)", "file_name"))

# Test with default = FALSE (selected columns)
data_selected <- read.sig(filepath, default = FALSE)
expect_equal(ncol(data_selected), 3) # Should have 3 columns: Wavelengths(nm), Reflectance(%), file_name
expect_equal(colnames(data_selected), c("Wavelengths(nm)", "Reflectance(%)", "file_name"))

# Clean up
unlink(temp_file)
})