################################################################################ # 기상관측계 : CWS # 지하수위계 : GWL # 다층 토양수분계 : MSM # 초음파관유량계(S) : PFS # 레이다유량계 : RFM # 초음파수위계 : UWL iwmp.combined.summary.and.graphs <- function(EnvList){ library(data.table) library(dplyr) library(zoo) library(tidyr) library(readr) prjdir <- EnvList$prjdir station.file <- EnvList$station.file cross.rect.file <- EnvList$cross.rect.file cross.tri.file <- EnvList$cross.tri.file cross.trape.file <- EnvList$cross.trape.file cross.circle.file <- EnvList$cross.circle.file cross.table.file <- EnvList$cross.table.file rating.table.file <- EnvList$rating.table.file info.dir <- file.path(prjdir, "00_station-info") qc.dir <- file.path(prjdir, "03_quality.control") cbn.dir <- file.path(prjdir, "04_combined.summary") if(!dir.exists(cbn.dir)) dir.create(cbn.dir, showWarnings = F, recursive = T) flist <- list.files(qc.dir, pattern = glob2rx("*.csv"), full.names = F) stypes <- unique(matrix(unlist(strsplit(flist, "-")), nrow=length(flist), byrow=T)[,1]) for(i in 1:length(stypes)){ stype <- stypes[i] flist <- list.files(qc.dir, pattern = glob2rx(sprintf("%s*.csv", stype)), full.names = T) for(j in 1:length(flist)){ fname <- flist[j] stnid <- strsplit(basename(fname), '-')[[1]][2] cat(sprintf("%s started for flow calculation!\n", basename(fname))) stn.dt <- as.data.table(read.csv(fname, header = T)) OrgChk=F; DayChk=F; MonChk=F if(stype == "cws"){ stn.dt$time <- format(stn.dt$time, format = "%Y-%m-%d %H:%M:%S") stn.day.dt <- setDT(stn.dt[, .(prcp=sum(prcp), tmin=min(temp), tmax=max(temp), tavg=mean(temp), dew=mean(dew), rhum=mean(rhum), wspd=mean(wspd), wdir=mean(wdir), rsds=sum(rsds), sshine=sum(sshine), lux=mean(lux), pet=mean(pet), presure=mean(presure)), by = .(date = as.Date(time))]) OrgChk=T; DayChk=T } if(stype == "msm"){ stn.dt$time <- format(stn.dt$time, format = "%Y-%m-%d %H:%M:%S") stn.day.dt <- setDT(stn.dt[, lapply(.SD, mean, na.rm = TRUE), by = .(date = as.Date(time)), .SDcols = !c("time")]) OrgChk=T; DayChk=T } if(stype == "pfs"){ stn.mon.dt <- setDT(stn.dt[, .(flow.mon=sum(flow.day)), by = .(yrmon = substr(date, 1, 7))]) stn.mon.dt[, flow.mon := round(flow.mon, 3)] OrgChk=T; MonChk=T } if(stype == "rfm"){ # Read UTF-8 Linux file info.dt <- read_csv(file.path(info.dir, station.file), show_col_types = FALSE) colnames(info.dt) <- c("no", "stype", "stnid", "modem", "install", "reset", "radmin", "user", "desc", "lat", "lon", "elev", "ref", "ctype", "adj_factor", "active") adj_coef <- info.dt[which(info.dt$stnid == stnid), "adj_factor"] adj_coef <- dplyr::pull(adj_coef) stn.dt$time <- format(stn.dt$time, format = "%Y-%m-%d %H:%M:%S") area.df <- NA depth.df <- as.data.frame(stn.dt$depth) area.df <- calculate.cross.section.area(EnvList, stnid, depth.df) if(!all(is.na(area.df))) { stn.dt$flow <- as.data.frame(stn.dt$velocity) * area.df * adj_coef stn.dt <- stn.dt[, c('time', 'velocity', 'depth', 'flow', 'h.ref')] stn.day.dt <- setDT(stn.dt[, .(velocity = mean(velocity, na.rm=T), depth = mean(depth, na.rm=T), flow = mean(flow, na.rm=T), h.ref = mean(h.ref, na.rm=T)), by = .(date = as.Date(time))]) stn.day.dt[, flow := round(flow, 3)] } else { stn.day.dt <- setDT(stn.dt[, .(velocity = mean(velocity, na.rm=T), depth = mean(depth, na.rm=T), h.ref = mean(h.ref, na.rm=T)), by = .(date = as.Date(time))]) } stn.day.dt[, depth := round(depth, 3)] OrgChk=T; DayChk=T } if(stype == "uwl"){ stn.dt[depth==h.ref, depth := NA] stn.day.dt <- setDT(stn.dt[, .(depth = mean(depth, na.rm=T), h.ref = mean(h.ref, na.rm=T)), by = .(date = as.Date(time))]) stn.day.dt[, depth := round(depth, 3)] OrgChk=T; DayChk=T } cat(sprintf(" => %s has been finished!\n\n", basename(fname))) OrgDFile <- file.path(cbn.dir, basename(fname)) if(OrgChk==T) write.csv(stn.dt, OrgDFile, na="", row.names = F) DayDFile <- file.path(cbn.dir, gsub(".csv", "-day.csv", basename(fname))) if(DayChk==T) write.csv(stn.day.dt, DayDFile, na="", row.names = F) MonDFile <- file.path(cbn.dir, gsub(".csv", "-mon.csv", basename(fname))) if(MonChk==T) write.csv(stn.mon.dt, MonDFile, na="", row.names = F) } # Station IDs } # If sensor type is all }