#Toolkit version number
r.tool.name = "Get Station Metadata"
r.data.toolkit.version = "5.4"
svn.version.data = "$ProjectRevision: SVN 13255 (2026-06-03) $"
svn.prj.name.data = "$ProjectName: /cerc_cerc/Products/Utilities/ModelEvaluationToolkit/Tags/ModelEvaluationToolkit_5_4_1 $"

#Suppress warnings that we don't want to worry the user with
options(warn=-1)

#script to extract station meta data
argv = commandArgs(trailingOnly = FALSE)
install_dir = dirname(substring(argv[grep("--file=", argv)], 8))
output.folder="Monitoring_stations_metadata"
output.dir=file.path(install_dir,output.folder)

#if there is no Stations_Metadata folder already, create one
if (!file.exists(output.dir)){
 dir.create(output.dir) 
}

#logfile
output.file.path.root=file.path(output.dir,output.folder)
log.file = file(paste(output.file.path.root,".log",sep=""),open="wt")
sink(file=log.file,type="output",split=TRUE)
sink(file=log.file,type="message")

cat(paste("Model Evaluation Toolkit version ",r.data.toolkit.version,": ",r.tool.name," Tool \n",sep=""))
if (length(grep("$ProjectRevision: ",svn.version.data,fixed=TRUE))>0) {
  svn.version.data.numberonly=sub("$ProjectRevision: ","",svn.version.data,fixed=TRUE)
  svn.version.data.numberonly=sub(" $","",svn.version.data.numberonly,fixed=TRUE)
  cat(paste("Internal version ",svn.version.data.numberonly," \n",sep=""))
} else {
  cat("CERC INTERNAL USE ONLY\n")
}
cat("Running Get Station Metadata tool... \n")
cat("Requires internet connection and the openair package.\n")
cat(paste("Running in ",R.version$version.string," \n",sep=""))
cat(paste(date(),"\n\n",sep=""))

#Get the CRAN mirror URL
cat("Getting the CRAN mirror from CRANMirror.txt...\n")
mirror.file = file.path(install_dir,"CRANMirror.txt")
mirror=readLines(mirror.file)[1]
mirror.data = tryCatch(url(mirror),error=function(e) return(NULL))
if (is.null(mirror.data)) {
  stop("Invalid CRAN mirror web address. Please refer to the 'R Support' menu for a complete list of available CRAN mirrors.")
  quit("no")
}

required.packages=c("curl","openair")
missing.packages = which(!(required.packages %in% installed.packages()))
if (length(missing.packages) > 0) {
  cat("Setting the CRAN mirror...\n")
  r = getOption("repos")
  r["CRAN"] = mirror
  options(repos=r)
  install.packages(required.packages[missing.packages])
}
library(curl)
library(openair)

#install meta data for each station network 
cat("     Downloading AURN metadata...\n")
AURN.station.metadata=importMeta(source="aurn", all=TRUE)
if (nrow(AURN.station.metadata)==0){
  cat("          No metadata downloaded for AURN Stations...\n")
} else {
  write.csv(AURN.station.metadata,file.path(output.dir,"AURN_Stations_metadata.csv"), row.names=FALSE)
}

cat("     Downloading SAQN metadata...\n")
SAQN.station.metadata=importMeta(source="saqn", all=TRUE)
if (nrow(SAQN.station.metadata)==0){
  cat("          No metadata downloaded for SAQN Stations...\n")
} else{
  write.csv(SAQN.station.metadata,file.path(output.dir,"SAQN_Stations_metadata.csv"), row.names=FALSE)
}

cat("     Downloading AQE metadata...\n")
AQE.station.metadata=importMeta(source="aqe", all=TRUE)
if (nrow(AQE.station.metadata)==0){
  cat("          No metadata downloaded for AQE Stations...\n")
} else {
  write.csv(AQE.station.metadata,file.path(output.dir,"AQE_Stations_metadata.csv"), row.names=FALSE)
}

cat("     Downloading NI metadata...\n")
NI.station.metadata=importMeta(source="ni", all=TRUE)
if (nrow(NI.station.metadata)==0){
  cat("          No metadata downloaded for NI Stations...\n")
} else {
  write.csv(NI.station.metadata,file.path(output.dir,"NI_Stations_metadata.csv"), row.names=FALSE)
}

cat("     Downloading KCL metadata...\n")
KCL.station.metadata=importMeta(source="kcl",all=TRUE)
if (nrow(KCL.station.metadata)==0){
  cat("          No metadata downloaded for KCL Stations...\n")
} else {
  write.csv(KCL.station.metadata,file.path(output.dir,"KCL_Stations_metadata.csv"), row.names=FALSE)
}

cat("     Downloading EUROPE metadata...\n")
EUROPE.station.metadata=importMeta(source="europe", all=TRUE)
if (nrow(EUROPE.station.metadata)==0){
  cat("          No metadata downloaded for EUROPE Stations...\n\n\n\n")
} else {
  write.csv(EUROPE.station.metadata,file.path(output.dir,"EUROPE_Stations_metadata.csv"), row.names=FALSE)
  
}
cat("     Downloading WAQN metadata...\n")
WAQN.station.metadata=importMeta(source="waqn", all=TRUE)
if (nrow(WAQN.station.metadata)==0){
  cat("          No metadata downloaded for WAQN Stations...\n\n\n\n")
} else {
  write.csv(WAQN.station.metadata,file.path(output.dir,"WAQN_Stations_metadata.csv"), row.names=FALSE)
  
}

cat("\n \n Finished!")

