
R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]

> ###################################################################
> # SELECT TIME PERIOD
> # seconds for  2 days
> daystep <- 60*60*24
> step <- daystep*2
> args <- commandArgs(trailingOnly = TRUE)
> message("ARGUMENTS = ",length(args))
ARGUMENTS = 0
> if (length(args)==0) {
+   t2 <- as.POSIXct(Sys.time(),tz="UTC")
+   t1 <- t2 - step
+   #"args:" length(args)
+ } else {
+   t1 <- as.POSIXct(args[1])
+   t2 <- as.POSIXct(args[2])
+ }
> 
> # SET TIME PERIOD HERE:
> #t1 <- as.POSIXct("2021-11-10 00:00:00 CET")
> #t2 <- as.POSIXct("2021-11-13 00:00:00 CET")
> t1 <- as.POSIXct(format(t1,"%Y-%m-%d %H:%M:00"))
> t2 <- as.POSIXct(format(t2,"%Y-%m-%d %H:%M:00"))
> t2 <- t2 + 60*60
> message("PERIOD = ",t1," ",t2)
PERIOD = 2022-03-30 09:09:00 2022-04-01 10:09:00
> runningdate <- seq.Date( from=as.Date(t1), to=as.Date(t2), by="day")
> 
> aggregation <- "60 sec"
> aggregation <- "1 min"
> 
> 
> 
> ###################################################################
> # GET MATCHING FILE NAMES FOR SELECTED DAYS
> flist <- NULL
> for (i in seq_along(runningdate)) {
+   name1 <- runningdate[i]
+   #name1 <- "2021-11-03"
+   #name1 <- format(t1,"%Y-%m-%d")
+   name2 <- "OPC"
+   #name3 <- runningdate[3]
+   name3 <- ""
+   url <- paste("https://saqn.geo.uni-augsburg.de/asopc.cgi?name1=",name1,"&name2=",name2,"&name3=",name3,sep="")
+   message("URL = ",url)
+   flist_tmp <- try(read.table(url,stringsAsFactors=F))
+   if(inherits(flist_tmp, "try-error")){
+     message("no files")
+   }else{
+     #message("FILE LIST = ",flist_tmp)
+     flist <- rbind(flist,flist_tmp)
+   }
+ }
URL = https://saqn.geo.uni-augsburg.de/asopc.cgi?name1=2022-03-30&name2=OPC&name3=
URL = https://saqn.geo.uni-augsburg.de/asopc.cgi?name1=2022-03-31&name2=OPC&name3=
URL = https://saqn.geo.uni-augsburg.de/asopc.cgi?name1=2022-04-01&name2=OPC&name3=
> 
> ###################################################################
> # READ MATCHING FILES DATA IN OBJECT asopc
> opcid <- ""
> for (i in seq_along(flist$V1)) {
+   #message("flist =",flist$V1[i]," ",i)
+   opctype <- strsplit(flist$V1[i],"_",fixed=T)[[1]][3]
+   opcid <- strsplit(flist$V1[i],"_",fixed=T)[[1]][4]
+   shttype <- strsplit(flist$V1[i],"_",fixed=T)[[1]][5]
+   shtid <- strsplit(flist$V1[i],"_",fixed=T)[[1]][6]
+   shtid <- gsub(".txt", "", shtid)
+   #fname <- paste("https://saqn.geo.uni-augsburg.de/asopc/",substr(flist$V1[i],2,9),"/",flist$V1[i],sep="")
+   fname <- paste("https://saqn.geo.uni-augsburg.de/asopc/",flist$V1[i],sep="")
+   message(opcid," FILE ADRESS = ",fname)
+   if (i == 1) {
+     asopc <- read.table(fname,header=T,stringsAsFactors=F)
+     asopc$opctype <- opctype
+     asopc$opcid <- opcid
+     asopc$shttype <- shttype
+     asopc$shtid <- shtid
+     if(grepl( "OPC-N2", fname, fixed = TRUE)){
+       asopc$Bin_16 <- NA
+       asopc$Bin_17 <- NA
+       asopc$Bin_18 <- NA
+       asopc$Bin_19 <- NA
+       asopc$Bin_20 <- NA
+       asopc$Bin_21 <- NA
+       asopc$Bin_22 <- NA
+       asopc$Bin_23 <- NA
+       asopc$Fan_rev_count <- NA
+       asopc$Laser_status <- NA
+       asopc$PM_1 <- asopc$PM1
+       asopc <- subset( asopc, select = -PM1 )
+       asopc$PM_10 <- asopc$PM10
+       asopc <- subset( asopc, select = -PM10 )
+       asopc$PM_2.5 <- asopc$PM2.5
+       asopc <- subset( asopc, select = -PM2.5 )
+       asopc <- subset( asopc, select = -Pressure )
+       asopc$Reject_count_Glitch  <- NA
+       asopc$Reject_count_LongTOF <- NA
+       asopc$Reject_Count_OutOfRange <- NA
+       asopc$Reject_count_Ratio <- NA
+       asopc$Relative_humidity <- NA
+       asopc$Sample_Flow_Rate <- NA
+       asopc$Sampling_Period <- NA
+       asopc <- subset( asopc, select = -SFR )
+     }
+    }
+   else {
+     asopc_temp <- read.table(fname,header=T,stringsAsFactors=F)
+     if (nrow(asopc_temp)<1) {      
+        next
+     }
+     asopc_temp$opctype <- opctype
+     asopc_temp$opcid <- opcid
+     asopc_temp$shttype <- shttype
+     asopc_temp$shtid <- shtid
+     if(grepl( "OPC-N2", fname, fixed = TRUE)){
+       asopc_temp$Bin_16 <- NA
+       asopc_temp$Bin_17 <- NA
+       asopc_temp$Bin_18 <- NA
+       asopc_temp$Bin_19 <- NA
+       asopc_temp$Bin_20 <- NA
+       asopc_temp$Bin_21 <- NA
+       asopc_temp$Bin_22 <- NA
+       asopc_temp$Bin_23 <- NA
+       asopc_temp$Fan_rev_count <- NA
+       asopc_temp$Laser_status <- NA
+       asopc_temp$PM_1 <- asopc_temp$PM1
+       asopc_temp <- subset( asopc_temp, select = -PM1 )
+       asopc_temp$PM_10 <- asopc_temp$PM10
+       asopc_temp <- subset( asopc_temp, select = -PM10 )
+       asopc_temp$PM_2.5 <- asopc_temp$PM2.5
+       asopc_temp <- subset( asopc_temp, select = -PM2.5 )
+       asopc_temp <- subset( asopc_temp, select = -Pressure )
+       asopc_temp$Reject_count_Glitch  <- NA
+       asopc_temp$Reject_count_LongTOF <- NA
+       asopc_temp$Reject_Count_OutOfRange <- NA
+       asopc_temp$Reject_count_Ratio <- NA
+       asopc_temp$Relative_humidity <- NA
+       asopc_temp$Sample_Flow_Rate <- NA
+       asopc_temp$Sampling_Period <- NA
+       asopc_temp <- subset( asopc_temp, select = -SFR )
+     }
+     #message(str(asopc_temp))
+     asopc <- rbind(asopc,asopc_temp)
+   }
+ }
177470212 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-30_00-00_OPC-N3_177470212_SHT85_327854.txt
177470210 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./alphasense/2022-03/2022-03-31_14-44_OPC-N3_177470210_SHT85_524394.txt
177470210 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./alphasense/2022-03/2022-03-31_14-58_OPC-N3_177470210_SHT85_524394.txt
177470210 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./alphasense/2022-03/2022-03-31_15-24_OPC-N3_177470210_SHT85_524394.txt
176430104 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./alphasense/2022-03/2022-03-31_15-28_OPC-N2_176430104_SHT85_524394.txt
177470215 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_10-20_OPC-N3_177470215_SHT85_327780.txt
176430104 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_10-59_OPC-N2_176430104_SHT85_327739.txt
177470206 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_10-35_OPC-N3_177470206_SHT85_721109.txt
177470206 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_10-54_OPC-N3_177470206_SHT85_721109.txt
177470210 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-03_OPC-N3_177470210_SHT85_327739.txt
177470210 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-07_OPC-N3_177470210_SHT85_327739.txt
177470205 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-13_OPC-N3_177470205_SHT85_720924.txt
177470208 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-13_OPC-N3_177470208_SHT85_327733.txt
177470209 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-22_OPC-N3_177470209_SHT85_327739.txt
177470209 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-29_OPC-N3_177470209_SHT85_327739.txt
177470209 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-34_OPC-N3_177470209_SHT85_327739.txt
177470209 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-36_OPC-N3_177470209_SHT85_327739.txt
177470202 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-58_OPC-N3_177470202_SHT75_16.txt
177470207 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-02_OPC-N3_177470207_SHT85_327747.txt
177470214 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-24_OPC-N3_177470214_SHT85_721131.txt
177470214 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_11-59_OPC-N3_177470214_SHT85_721131.txt
177470214 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_12-08_OPC-N3_177470214_SHT85_721131.txt
177470212 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_00-00_OPC-N3_177470212_SHT85_327854.txt
177470206 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_14-03_OPC-N3_177470206_SHT85_721109.txt
177470206 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_13-58_OPC-N3_177470206_SHT85_721109.txt
177470206 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-03/2022-03-31_14-16_OPC-N3_177470206_SHT85_721109.txt
177470212 FILE ADRESS = https://saqn.geo.uni-augsburg.de/asopc/./2022-04/2022-04-01_00-00_OPC-N3_177470212_SHT85_327854.txt
> asopc$time <- as.POSIXct(asopc$time_UTC,format="%Y-%m-%dT%H:%M:%S")
> #asopc$time <- asopc$time + 60*60 # convert to CET which is used as UTC in plot
> asopc[as.character(asopc)=="nan"] <- NA
> message("reading done!")
reading done!
> 
> 
> ###################################################################
> # CREATE DATA FRAME OF aggregation INTERVALL TIME STEPS
> #secs <- seq.POSIXt(from=as.POSIXct(t1),to=as.POSIXct(t2),by="sec")
> #secs <- data.frame( time=seq.POSIXt(from=as.POSIXct(t1),to=as.POSIXct(t2),by=aggregation))
> secs <- data.frame( time=seq(from=as.POSIXct(t1),to=as.POSIXct(t2),by=aggregation))
> sec5pm10 <- secs
> ids <- sort(unique(asopc$opcid))
> for (i in seq_along(ids)) {
+    message(" ")
+    message("id =",ids[i])
+    idser <- c(0.0,asopc$PM_10[asopc$opcid==ids[i]])
+    #idser[1] <- NA
+    #idtime <- c(as.POSIXct(t1),asopc$time[asopc$opcid==ids[i]])
+    idtime <- c(as.POSIXct(runningdate[1]),asopc$time[asopc$opcid==ids[i]])
+    tmp <- data.frame(time=idtime,pm10=idser)
+ 
+    #tmp$cuts <- cut( idtime,breaks=aggregation)
+    tmp$cuts <- cut( idtime,breaks=secs$time)
+ 
+    #tmp$cuts <- cut( idtime,breaks=seq.POSIXt(from=as.POSIXct(t1),to=as.POSIXct(t2),by="5 sec" ) )
+    #agg <- aggregate( idser, by=list(cuts) , FUN=mean)
+    agg <- aggregate(. ~ cuts, tmp, mean)
+    agg$time <- as.POSIXct(agg$cuts)
+    reg <- merge(secs,agg,by="time",all.x=T)
+    #assign(paste0("id",ids[i]), reg$pm10)
+    message("len =",length(reg$pm10))
+    message("len =",length(sec5pm10$time))
+    sec5pm10$pm10 <- reg$pm10
+    names(sec5pm10)[length(names(sec5pm10))] <- paste0("id",ids[i])
+ }
 
id =176430104
len =2941
len =2941
 
id =177470202
len =2941
len =2941
 
id =177470205
len =2941
len =2941
 
id =177470206
len =2941
len =2941
 
id =177470207
len =2941
len =2941
 
id =177470208
len =2941
len =2941
 
id =177470209
len =2941
len =2941
 
id =177470210
len =2941
len =2941
 
id =177470212
len =2941
len =2941
 
id =177470214
len =2941
len =2941
 
id =177470215
len =2941
len =2941
> message("sec5-aggregation done!")
sec5-aggregation done!
> 
> 
> ###################################################################
> # GET LUEB REFERENCE DATA (in CET = Winterzeit = UTC + 1)
> tshift <- -60*90
> # 1/2 hour since LUEB hourly means end at the given time stamp but represent the hour before
> 
> path <- format(t2,"/zugspitze/lappwald/saqn/lueb/%Y_%m/%Y_%m_%d_Bourges-Platz_PM10.csv")
> bou <- read.csv(path,sep=";",header=F,stringsAsFactors=F,na.strings="---")
> colnames(bou) <- c("tdate","ttime","pm10")
> bou$time <- as.POSIXct(paste(bou$tdate," ",bou$ttime,sep=""),format="%d.%m.%Y %H:%M")
> bou$time <- bou$time + tshift
> 
> path <- format(t2,"/zugspitze/lappwald/saqn/lueb/%Y_%m/%Y_%m_%d_Karlstrasse_PM10.csv")
> kar <- read.csv(path,sep=";",header=F,stringsAsFactors=F,na.strings="---")
> colnames(kar) <- c("tdate","ttime","pm10")
> kar$time <- as.POSIXct(paste(kar$tdate," ",kar$ttime,sep=""),format="%d.%m.%Y %H:%M")
> kar$time <- kar$time + tshift
> 
> path <- format(t2,"/zugspitze/lappwald/saqn/lueb/%Y_%m/%Y_%m_%d_Koenigsplatz_PM10.csv")
> koe <- read.csv(path,sep=";",header=F,stringsAsFactors=F,na.strings="---")
> colnames(koe) <- c("tdate","ttime","pm10")
> koe$time <- as.POSIXct(paste(koe$tdate," ",koe$ttime,sep=""),format="%d.%m.%Y %H:%M")
> koe$time <- koe$time + tshift
> 
> path <- format(t2,"/zugspitze/lappwald/saqn/lueb/%Y_%m/%Y_%m_%d_LfU_PM10.csv")
> lfu <- read.csv(path,sep=";",header=F,stringsAsFactors=F,na.strings="---")
> colnames(lfu) <- c("tdate","ttime","pm10")
> lfu$time <- as.POSIXct(paste(lfu$tdate," ",lfu$ttime,sep=""),format="%d.%m.%Y %H:%M")
> lfu$time <- lfu$time + tshift
> 
> luebmax <- max(c(lfu$pm10,koe$pm10,kar$pm10,bou$pm10),na.rm=T)
> luebmax <- ceiling(1+luebmax/10.0)*10
Error in luebmax/10 : non-numeric argument to binary operator
Execution halted
