From 0f33dd64dc5012cf57f5b593b6dc2a2707a96126 Mon Sep 17 00:00:00 2001 From: arnejh Date: Fri, 23 Jan 2026 00:40:16 +0100 Subject: [PATCH] Fixed bugs in ICESDatsusc. --- DESCRIPTION | 2 +- NEWS.md | 5 ++++- R/Definitions.R | 51 +++++++++++++++++++++++++++++++++++++++++++++++-- R/StoxExport.R | 22 ++++++++++----------- 4 files changed, 64 insertions(+), 16 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e1fa1a25..7021ac32 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: RstoxData Version: 2.2.0-9011 -Date: 2026-01-19 +Date: 2026-01-23 Title: Tools to Read and Manipulate Fisheries Data Authors@R: c( person(given = "Arne Johannes", diff --git a/NEWS.md b/NEWS.md index dbc18b42..4512ee03 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,7 @@ -# RstoxData v2.2.0-9011 (2026-01-11) +# RstoxData v2.2.0-9011 (2026-01-23) +* Fixed bug in TranslateICESDatsusc(), where conditional values were ignored. +* Changed NA to empty string ("") in WriteICESDatsusc(). +* Changed Weight in ICESDatsusc() to always be grouped (changed in the column Notes). * Fixed bug in ICESAcoustic, where reading an NMDEchosounder file in the ReadAcoustic process used as input failed in the merging of tables "distance", "frequency", "sa", Now expanded to merge "distance", "frequency", "ch_type", "sa_by_acocat" and "sa". * Added units for area_weight_density. diff --git a/R/Definitions.R b/R/Definitions.R index 123bc35c..2ae5ff57 100644 --- a/R/Definitions.R +++ b/R/Definitions.R @@ -84,10 +84,57 @@ initiateRstoxData <- function(){ "LengthClass", "FishID" ) + ), + # ICESDatsusc keys: + ICESDatsusc = list( + FI = character(), + HH = c( + "Ship", + "Gear", + "HaulNo", + "StationNumber", + "Year", + "Month", + "Day", + "Time" + ), + PI = c( + "Ship", + "Gear", + "HaulNo", + "StationNumber", + "Year", + "Month", + "Day", + "Time", + "FishID", + "AphiaIDPredator" + ), + PP = c( + "Ship", + "Gear", + "HaulNo", + "StationNumber", + "Year", + "Month", + "Day", + "Time", + "FishID", + "AphiaIDPredator", + "AphiaIDPrey", + "IdentMet", + "DigestionStage", + "GravMethod", + "SubFactor", + "PreySequence" + ) ) ) - # Add the ICESDatsusc keys as the same as the ICESDatras: - keys$ICESDatsusc <- keys$ICESDatras + + + + + diff --git a/R/StoxExport.R b/R/StoxExport.R index 4729f9f8..3e1c5784 100644 --- a/R/StoxExport.R +++ b/R/StoxExport.R @@ -1938,10 +1938,11 @@ ICESDatsuscOne <- function( AphiaIDPredator,AphiaIDPrey,IdentMet,DigestionStage,GravMethod, SubFactor,PreySequence)] - # Set the weight to computed weight if possible: + # Set the weight to grouped weight: hasCountAndTotalCount <- !is.na(finalPP$TotalCount) - finalPP[hasCountAndTotalCount, Notes := 'Computed using: total weight * count in length group / count for all length group'] - finalPP[hasCountAndTotalCount, Weight := Weight * Count / TotalCount] + finalPP[, Notes := 'Grouped weight'] + # Removed: + # finalPP[hasCountAndTotalCount, Weight := Weight * Count / TotalCount] #Handling number of unique species in prey sampled in each individual fish #TODO: Needs to be checked @@ -2002,23 +2003,21 @@ WriteICESDatsusc <- function(ICESDatsuscData){ # na = "-9" #) - WriteICESDatsuscData <- WriteICESDatsuscOne(ICESDatsuscData) + WriteICESDatsuscData <- WriteICESDatsuscOne(ICESDatsuscData, na = "") return(WriteICESDatsuscData) } -WriteICESDatsuscOne <- function(ICESDatsuscData){ +WriteICESDatsuscOne <- function(ICESDatsuscData, na = ""){ # Convert all tables to string matrix with header and record, and rbind: ICESDatsuscCSVDataOne<- convertToRecordTypeMatrix(ICESDatsuscData) - ### # Replace NAs: - ### if(length(na)) { - ### ICESDatsuscCSVDataOne <- lapply(ICESDatsuscCSVDataOne, function(x) {x[is.na(x)] <- na; x}) - ### } - - # IMPORTANT NOTE: The ICES DATRAS format assumes that the three tables HH, HL and CA are stacked in a comma separated file but without padding the tables to equal number of columns as is done for ICESAcoustic and ICESBiotic. For this reason we need to paste the data to a chatacter vector and write as lines. RstoxFramework detects that the output as a vector of characters and uses writeLines() to produce the file. + # Replace NAs: + if(length(na)) { + ICESDatsuscCSVDataOne <- lapply(ICESDatsuscCSVDataOne, function(x) {x[is.na(x)] <- na; x}) + } # Convert each line of each table to comma separated: ICESDatsuscCSVDataOne <- lapply(ICESDatsuscCSVDataOne, apply, 1, paste, collapse = ",") @@ -2031,4 +2030,3 @@ WriteICESDatsuscOne <- function(ICESDatsuscData){ -