
Convert laboratory data to the Portuguese Information System forWater Resources (SNIRH) file format. Thesnirh.labpackage provides tools to validate station data, convert parameters andunits, and generate compliant output files for submission to SNIRH.
The enhanced SNIRH lab package now includes automatic stationvalidation against the SNIRH database for surface water and biota data.This ensures that only valid, active stations are used for dataconversion.
install.packages("snirh.lab")# install.packages("devtools")devtools::install_github("lpereira-ue/snirh.lab")# Core requirementsinstall.packages(c("data.table","cli"))# For station validationinstall.packages("sf")# For better internet connectivity checks (optional)install.packages("curl")convert_to_snirh() - Convert laboratory data to SNIRHformatget_snirh_stations() - Download stationinformationcheck_station_status() - Validate specificstationslist_snirh_parameters() - Browse availableparameterslibrary(snirh.lab)library(data.table)# Prepare your datalab_data<-data.table(snirh_entity ="LAB001",station_name ="Rio Douro - Crestuma",station_id ="01F/01",# Must be valid SNIRH stationsampling_date =as.POSIXct("2024-01-15 10:30:00"),parameter ="pH - Campo",unit ="Escala Sorensen",value ="7.2")# Convert with automatic station validationresult<-convert_to_snirh(lab_data,"surface.water")# Check if your stations are valid and activemy_stations<-c("01F/01","25G/07","16H/03")station_check<-check_station_status(my_stations,"surface.water")print(station_check)# Only proceed with active stationsactive_stations<- station_check[active==TRUE, station_id]filtered_data<- lab_data[station_id%in% active_stations]# Get all active surface water stationsactive_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 parameterswater_params<-list_snirh_parameters("water")print(head(water_params))# Get detailed conversion informationdetailed_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 messagebad_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’llget: - “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 = FALSEresult<-convert_to_snirh(lab_data,"surface.water",validate_stations =FALSE)# For testing or when working offlineresult<-convert_to_snirh(lab_data,"surface.water",validate_stations =FALSE)# For slow connectionsresult<-convert_to_snirh(lab_data,"surface.water",timeout =60)# Process multiple files with error handlingprocess_lab_files<-function(file_paths) { results<-list()for (file_pathin 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]]<- resultcat("✅ 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)}sf package notavailableinstall.packages("sf")# View package helphelp(package ="snirh.lab")# Function-specific help?convert_to_snirh?get_snirh_stations?check_station_status# List all available parameterslist_snirh_parameters("all")The package performs comprehensive validation:
This ensures high-quality data submission to SNIRH with minimalmanual intervention.
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 PullRequest.