Skip to content

Commit

Permalink
Merge pull request #255 from StoXProject/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
arnejohannesholmin authored Nov 20, 2022
2 parents ebabc48 + cd1eba2 commit 981db2e
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 19 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: RstoxData
Version: 1.7.3
Date: 2022-11-14
Version: 1.7.4-9001
Date: 2022-11-18
Title: Tools to Read and Manipulate Fisheries Data
Authors@R: c(
person(given = "Edvin",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export(getRstoxDataDefinitions)
export(getStoxKeys)
export(getUnit)
export(getUnitOptions)
export(hasUnit)
export(is.LandingData)
export(is.StoxLandingData)
export(lapplyOnCores)
Expand Down
11 changes: 10 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# RstoxData v1.7.3-9002 (2022-11-17)
* Fixed typo in warning for more sa in B than in P.
* Added a unique fo the Log table in StoxAcoustic for ICESAcoutsic.
* Added units for area number density, and one for megaton.
* Added the exported hasUnit().


# RstoxData v1.7.3-9001 (2022-11-15)
* Fixed bug in ReadAcoustic when an ICESAcoustic xml files contains more than one instrument.

# RstoxData v1.7.2 (2022-11-13)
* Added warning when an ICESAcoustic xml files contains more than one instrument, in which case the xml file does not contain enough information for StoX to link the Sample and Data tables.
* Fixed bug with R < 4.2, where a filter process with unspecified FilterExpression retuns error "zero-length inputs cannot be mixed with those of non-zero length". The error is returned both when opening the FilterExpression and when running the process.

# RstoxData v1.7.1-9002 (2022-10-31)
Expand Down
13 changes: 3 additions & 10 deletions R/StoxAcoustic.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ StoxAcousticOne <- function(data_list) {
sum_sa_merged_onlyError <- sum_sa_merged_onlyError[, do.call(paste, c(.SD, list(sep = " - ")))]

if(NROW(sum_sa_merged_onlyError)) {
warning("StoX: There are data of the file ", data_list$metadata$file, " that contain more sa in ch_type \"B\" than \"P\". This coculd be an indication of loss of data from raw files + work files (scrutinization) to the NMDEchosounder file. The problem occurs for the following cruise, log-distance, frequency and sum of sa for \"P\" and \"B\" (CruiseKey - LogKey - BeamKey - Sum_of_NASC_for_P - Sum_of_NASC_for_P):", printErrorIDs(sum_sa_merged_onlyError))
warning("StoX: There are data of the file ", data_list$metadata$file, " that contain more sa in ch_type \"B\" than \"P\". This coculd be an indication of loss of data from raw files + work files (scrutinization) to the NMDEchosounder file. The problem occurs for the following cruise, log-distance, frequency and sum of sa for \"P\" and \"B\" (CruiseKey - LogKey - BeamKey - Sum_of_NASC_for_P - Sum_of_NASC_for_B):", printErrorIDs(sum_sa_merged_onlyError))
}
}

Expand Down Expand Up @@ -304,7 +304,6 @@ StoxAcousticOne <- function(data_list) {


else{

#################################################################
# Description: protocol to convert ICESacoustic to StoxAcoustic #
#################################################################
Expand All @@ -319,22 +318,16 @@ StoxAcousticOne <- function(data_list) {
# Fiks to correct time format, and add to key #
#################################################################
#data_list$Log[, LogKey:= paste0(gsub(' ','T',Time),'.000Z')]
data_list$Log <- unique(data_list$Log)

data_list$Log[, LogKey := getLogKey_ICESAcoustic(Time)]

data_list$Log[, EDSU:= paste(LocalID,LogKey,sep='/')]

#browser()

#################################################################
# MAKE other general level #
#################################################################
# ICESACoustic does not seem to support multiple frequencies in the XML format, as Instrument is present in the Sample table but not in the Data table:
uniqueInstruments <- data_list$Sample[, unique(Instrument)]
if(length(uniqueInstruments) > 1) {
stop("The ICESAcoutsic XML file ", data_list$metadata$file, " contain multiple Instruments. However StocAcoustic can currently not convert data from ICESAcoutsic XML format as the Instrument tag is presesnt in the Sample but not the Data table.")
}


tmp <- merge(data_list$Sample,data_list$NASC)
tmp <- merge(tmp,data_list$Log[,c('Distance','Time','LogKey','Origin')],by='Distance')
names(tmp)[names(tmp)=="Instrument"]='ID'
Expand Down
17 changes: 11 additions & 6 deletions R/readXmlFile.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,22 @@ icesAcousticPreprocess <- function(xsdObject) {

newAC$prefixLens[allDatawithPrefix] <- 1
newAC$prefixLens["Log"] <- 2
newAC$prefixLens["Sample"] <- 3
newAC$prefixLens["Data"] <- 4
newAC$prefixLens["Sample"] <- 4
newAC$prefixLens["Data"] <- 5

newAC$tableHeaders$Log <- c("LocalID", newAC$tableHeaders$Log)
newAC$tableTypes$Log <- c("xsd:string", newAC$tableTypes$Log)

newAC$tableHeaders$Sample <- c("LocalID", "Distance", newAC$tableHeaders$Sample)
newAC$tableTypes$Sample <- c("xsd:string", "xsd:float", newAC$tableTypes$Sample)
# We need here to add Instrument as the first header, since it must serve as a key:
newAC$tableHeaders$Sample <- c("LocalID", "Distance", "Instrument", newAC$tableHeaders$Sample)
newAC$tableTypes$Sample <- c("xsd:string", "xsd:float", "xsd:string", newAC$tableTypes$Sample)
# Remove the duplicated Instrument:
atDup <- duplicated(newAC$tableHeaders$Sample)
newAC$tableHeaders$Sample <- newAC$tableHeaders$Sample[!atDup]
newAC$tableTypes$Sample <- newAC$tableTypes$Sample[!atDup]

newAC$tableHeaders$Data <- c("LocalID", "Distance", "ChannelDepthUpper", newAC$tableHeaders$Data)
newAC$tableTypes$Data <- c("xsd:string", "xsd:float", "xsd:float", newAC$tableTypes$Data)
newAC$tableHeaders$Data <- c("LocalID", "Distance", "Instrument", "ChannelDepthUpper", newAC$tableHeaders$Data)
newAC$tableTypes$Data <- c("xsd:string", "xsd:float", "xsd:string", "xsd:float", newAC$tableTypes$Data)


# Modify cruise structure to get LocalID as prefix (the types order are the same, as they are all type of string)
Expand Down
16 changes: 16 additions & 0 deletions R/units.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@ getUnit <- function(value, property=c("id", "shortname", "symbol", "name"), unit

}

#' Does the input have unit?
#' @description
#' Get unit id, shortname, symbol or name of a value in accordance with StoX convention.
#' @inheritParams getUnit
#' @return TRUE if the \code{value} has unit, FALSE if not.
#' @examples
#' dt <- data.table::data.table(weight=c(1000,1200))
#' dt$weight <- setUnit(dt$weight, "mass-g")
#' print(hasUnit(dt$weight))
#' print(hasUnit(1))
#' @export
hasUnit <- function(value, property=c("id", "shortname", "symbol", "name"), unitTable=RstoxData::StoxUnits){
unit <- getUnit(value, property=property, unitTable=unitTable)
!is.na(unit)
}

#' Get available units
#' @description
#' Get the unit shortnames, symbols or names that are available for a given quantity.
Expand Down
13 changes: 13 additions & 0 deletions inst/prepResources/prepUnits.R → data-raw/prepUnits.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
# Typically, the user "sees" shortname, symbol and name.
#


setwd(file.path(getwd(), "RstoxData"))
setwd(file.path(getwd(), "data-raw"))

StoxUnits <- data.table::data.table(id=character(),
quantity=character(),
symbol=character(),
Expand All @@ -21,16 +25,25 @@ StoxUnits <- rbind(StoxUnits, list("mass-g", "mass", "g", "g", "gram", 1e-3))
StoxUnits <- rbind(StoxUnits, list("mass-kg", "mass", "kg", "kg", "kilogram", 1))
StoxUnits <- rbind(StoxUnits, list("mass-ton", "mass", "t", "ton", "metric ton", 1e3))
StoxUnits <- rbind(StoxUnits, list("mass-kt", "mass", "kt", "kiloton", "kiloton", 1e6))
StoxUnits <- rbind(StoxUnits, list("mass-Mt", "mass", "Mt", "megaton", "megaton", 1e9))

StoxUnits <- rbind(StoxUnits, list("length-mm", "length", "mm", "mm", "millimeter", 1e-3))
StoxUnits <- rbind(StoxUnits, list("length-cm", "length", "cm","cm", "centimeter", 1e-2))
StoxUnits <- rbind(StoxUnits, list("length-m", "length", "m", "m", "meter", 1))
StoxUnits <- rbind(StoxUnits, list("length-km", "length", "km", "km", "kilometer", 1e3))
StoxUnits <- rbind(StoxUnits, list("length-nmi", "length", "M", "nmi", "nautical mile", 1852)) #M is hydrographic standard

StoxUnits <- rbind(StoxUnits, list("cardinality-N", "cardinality", "N", "individuals", "individuals", 1))
StoxUnits <- rbind(StoxUnits, list("cardinality-kN", "cardinality", "kN", "10^3 individuals", "thousand individuals", 1e3))
StoxUnits <- rbind(StoxUnits, list("cardinality-MN", "cardinality", "MN", "10^6 individuals", "million individuals", 1e6))

StoxUnits <- rbind(StoxUnits, list("area_number_density-N/nmi^2", "area_number_density", "N/nmi^2", "individuals/nmi^2", "individuals per sqare nautical mile", 1))
StoxUnits <- rbind(StoxUnits, list("area_number_density-kN/nmi^2", "area_number_density", "kN/nmi^2", "10^3 individuals/nmi^2", "thousand individuals per sqare nautical mile", 1e3))
StoxUnits <- rbind(StoxUnits, list("area_number_density-MN/nmi^2", "area_number_density", "MN/nmi^2", "10^6 individuals/nmi^2", "million individuals per sqare nautical mile", 1e6))

StoxUnits <- rbind(StoxUnits, list("fraction-decimal", "fraction", "0.", "fraction", "decimal", 1))
StoxUnits <- rbind(StoxUnits, list("fraction-percent", "fraction", "%", "%", "percent", 1e-2))

# treat age separate from time, as it is not a fixed ratio to actual time units
StoxUnits <- rbind(StoxUnits, list("age-year", "age", "y", "year", "year", 1))
StoxUnits <- rbind(StoxUnits, list("time-s", "time", "s", "second", "second", 1))
Expand Down
Binary file modified data/StoxUnits.rda
Binary file not shown.
Binary file modified inst/extdata/functionArguments.rds
Binary file not shown.
31 changes: 31 additions & 0 deletions man/hasUnit.Rd

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

0 comments on commit 981db2e

Please sign in to comment.