Convert laboratory data to the Portuguese Information System for Water Resources (SNIRH) file format. The snirh.lab package provides tools to validate station data, convert parameters and units, and generate compliant output files for submission to SNIRH.
The enhanced SNIRH lab package now includes automatic station validation against the SNIRH database for surface water and biota data. This ensures that only valid, active stations are used for data conversion.
- ✅ Automatic validation against SNIRH database
- ✅ Station status checking (existence and active status)
- ✅ Surface water and biota matrices support
- ✅ Clear, informative error messages
- ✅ Step-by-step progress tracking
- ✅ Detailed validation reports
install.packages("snirh.lab")# install.packages("devtools")
devtools::install_github("lpereira-ue/snirh.lab")# Core requirements
install.packages(c("data.table", "cli"))
# For station validation
install.packages("sf")
# For better internet connectivity checks (optional)
install.packages("curl")convert_to_snirh()- Convert laboratory data to SNIRH formatget_snirh_stations()- Download station informationcheck_station_status()- Validate specific stationslist_snirh_parameters()- Browse available parameters
library(snirh.lab)
library(data.table)
# Prepare your data
lab_data <- data.table(
snirh_entity = "LAB001",
station_name = "Rio Douro - Crestuma",
station_id = "01F/01", # Must be valid SNIRH station
sampling_date = as.POSIXct("2024-01-15 10:30:00"),
parameter = "pH - Campo",
unit = "Escala Sorensen",
value = "7.2"
)
# Convert with automatic station validation
result <- convert_to_snirh(lab_data, "surface.water")# Check if your stations are valid and active
my_stations <- c("01F/01", "25G/07", "16H/03")
station_check <- check_station_status(my_stations, "surface.water")
print(station_check)
# Only proceed with active stations
active_stations <- station_check[active == TRUE, station_id]
filtered_data <- lab_data[station_id %in% active_stations]# Get all active surface water stations
active_stations <- get_snirh_stations("surface.water", active_only = TRUE)
print(paste("Available stations:", nrow(active_stations)))
# Find stations in your region (example with spatial filtering)
if (requireNamespace("sf", quietly = TRUE)) {
library(sf)
stations_sf <- get_snirh_stations("surface.water")
# Add your spatial filtering logic here
}# List all water quality parameters
water_params <- list_snirh_parameters("water")
print(head(water_params))
# Get detailed conversion information
detailed_params <- list_snirh_parameters("water", include_conversion_info = TRUE)
print(detailed_params[1:5, .(param_lab, unit_lab, param_snirh, unit_snirh, factor)])# This will fail with clear error message
bad_data <- data.table(
snirh_entity = "LAB001",
station_name = "Invalid Station",
station_id = "INVALID_ID",
sampling_date = as.POSIXct("2024-01-15 10:30:00"),
parameter = "pH - Campo",
unit = "Escala Sorensen",
value = "7.2"
)
# Will produce error: "Station ID(s) not found in SNIRH database: INVALID_ID"
try(convert_to_snirh(bad_data, "surface.water"))Note: If a station exists but is inactive, you'll get:
- "Station(s) not active in SNIRH database: STATION_ID (EXTINTA)"
- "Only stations with status 'ATIVA' can receive data"
# Will produce: "Internet connection required for station validation"
# Solution: Check connection or use validate_stations = FALSE
result <- convert_to_snirh(lab_data, "surface.water", validate_stations = FALSE)# For testing or when working offline
result <- convert_to_snirh(lab_data, "surface.water", validate_stations = FALSE)
# For slow connections
result <- convert_to_snirh(lab_data, "surface.water", timeout = 60)# Process multiple files with error handling
process_lab_files <- function(file_paths) {
results <- list()
for (file_path in file_paths) {
tryCatch({
# Read your data
lab_data <- read_your_data_function(file_path)
# Check stations first
unique_stations <- unique(lab_data$station_id)
station_status <- check_station_status(unique_stations, "surface.water")
# Filter to active stations only
active_stations <- station_status[active == TRUE, station_id]
filtered_data <- lab_data[station_id %in% active_stations]
if (nrow(filtered_data) > 0) {
# Convert filtered data
result <- convert_to_snirh(filtered_data, "surface.water")
results[[file_path]] <- result
cat("✅ Successfully processed:", file_path, "\n")
} else {
cat("⚠️ No active stations in:", file_path, "\n")
}
}, error = function(e) {
cat("❌ Error processing:", file_path, "-", e$message, "\n")
})
}
return(results)
}- Check internet connection
- Verify station IDs are correct
- Check if stations are active in SNIRH
- Install with:
install.packages("sf") - May require system dependencies on Linux
- Increase timeout parameter
- Check network connection
- Try during off-peak hours
# View package help
help(package = "snirh.lab")
# Function-specific help
?convert_to_snirh
?get_snirh_stations
?check_station_status
# List all available parameters
list_snirh_parameters("all")- ✅ Always validate stations first for production workflows
- ✅ Cache station data for batch processing to avoid repeated downloads
- ✅ Handle errors gracefully in automated systems
- ✅ Keep the package updated for latest SNIRH compatibility
- ✅ Test with small datasets before processing large files
The package performs comprehensive validation:
- ✅ Column structure and naming
- ✅ Station existence and status
- ✅ Duplicate detection
- ✅ Parameter conversion availability
- ✅ Value format validation
- ✅ Unit conversion accuracy
- ✅ Output format compliance
This ensures high-quality data submission to SNIRH with minimal manual intervention.
- SNIRH Portal: https://snirh.apambiente.pt/
- Report Issues: https://github.com/lpereira-ue/snirh.lab/issues
- Source Code: https://github.com/lpereira-ue/snirh.lab
This package is licensed under the MIT License.
To cite snirh.lab in publications, use:
citation("snirh.lab")Contributions are welcome! Please feel free to submit a Pull Request.
