R/f_SWMM_R.R

Defines functions SWMM_run

Documented in SWMM_run

# SWMM run --------------------------------------------------------------------

#' Aggregate values in time
#' @export
#' @param type Sensor type
#' @param type_sub Number of sensors considered 
#' @param interpolation Interpolation type
#' @param adjustment Type of adjustment 
#' @param adjustment_sub Subtype of adjustment
#' @param time Temporal aggregation
SWMM_run <- function(key_id) {
  
  if(type=="") stop("Warning: choose measurement type")
  
  path.library <- paste0(baseLoc,"/exec")
  if(time!=""){
    path.rainfall <- paste0(baseLoc,"/../data-processed/SWMM/",time,"/")
  }else{
    path.rainfall <- paste0(baseLoc,"/../data-processed/SWMM/")
  }
  
  key <- key_id
  
  stations <- c("1111","1112","1113","1114","1115","1116","1117","1118","1119","1120","1121","1122","1123","1124","1125","1126","1127","1128","1129","1130","1131","1132","1133","1134","1135","1136","1137","1138","1139","1140","1141","1142","1143","1144","1145","1146","1147","1148","1149","1150","1151","1152","1153","1154","1155","1156","1157","1158","1159","1160","1161","1162","1163","1164","1165","1166","1167","1168","1169","1170","1171","1172","1173","1174","1175","1176","1177","1178","1179","1180","1181","1182","1183","1184","1185","1186","1187","1188","1189","1190","1191","1192","1193","1194","1195","1196","1197","1198","1199","1200","1201","1202","1203","1204","1205","1206","1207","1208","1209","1210","1211")
  subc <- c("E0484","E0485","E0486","E0487","E0490","E0494","E0496","E0497","E0502","E0503","E0506","E0519","E0532","E0538","E0542","E0544","E0545","E0548","E0549","E0550","E0551","E0552","E0554","E0555","E0556","E0557","E0559","E0561","E0562","E0563","E0564","E0567","E0568","E0569","E0570","E0573","E0574","E0575","E0594","E0595","E0596","E0603","E0605","E0606","E0607","E0611","E0615","E0619","E0620","E0622","E0623","E0626","E0627","E0628","E0629","E0634","E0638","E0640","E0641","E0648","E0650","E0651","E0652","E0653","E0654","E0655","E0656","E0657","E0659","E0661","E0662","E0663","E0664","E0665","E0666","E0667","E0668","E0669","E0677","E0678","E0679","E0680","E0681","E0689","E0690","E0693","E0695","E0697","E0699","E0700","E0701","E0702","E0703","E0704","E0760","E0763","E0764","E0770","E0771","E0772","E1102")
  
  total <- length(stations)
  for (i in 1:total){
    for(intr in 1:35){
      rain.RG <- get(load(file=paste0(baseLoc,"/../data-processed/",type,"/",key_id,"_event_",intr,".RData")))
      tmp <- rain.RG[which(rain.RG$ID==subc[i]),-1]
      datetime <- colnames(tmp)
      datetime <- as.POSIXct(as.numeric(datetime), origin="1970-01-01 00:00:00",tz="UTC")
      datetime <- as.data.frame(strftime(datetime,format="%Y %m %d %H %M",tz="UTC"))
      tmp <- t(tmp)
      tmp[is.na(tmp)] <- 0
      station<-rep(as.numeric(stations[i]),nrow(tmp))
      if(time!=""){
        write.table(cbind(station,datetime,tmp),file=paste0(baseLoc,"/../data-processed/SWMM/",time,"/",subc[i],"_",intr,".dat"), sep=" ",row.names=F,col.names=F,quote=F)
        tmp <- 0
        write.table(cbind("0000",datetime,tmp),file=paste0(baseLoc,"/../data-processed/SWMM/",time,"/no_rain.dat"), sep=" ",row.names=F,col.names=F,quote=F)
      }else{
        write.table(cbind(station,datetime,tmp),file=paste0(baseLoc,"/../data-processed/SWMM/",subc[i],"_",intr,".dat"), sep=" ",row.names=F,col.names=F,quote=F)
        tmp <- 0
        write.table(cbind("0000",datetime,tmp),file=paste0(baseLoc,"/../data-processed/SWMM/no_rain.dat"), sep=" ",row.names=F,col.names=F,quote=F)
      }
    }
  }
  
  par.dirac <- c(mult.imp = 0.65, mult.wid =.1, mult.slo=.95, mult.Nim=2.59, mult.Sim=0.44,
                 mult.Spe =1.23, mult.Pze=4.35, mult.rou=0.73, mult.tue=1.6, mult.zue=1.1,
                 sd.Eps_Q=0.02,sd.B_Q=1.59,corrlen=.81)
  dt.mod = 4/60
  
  for (i in 1:35){
    assign(paste0("Q.dat.",i), get(load(file=paste0(baseLoc,"/../data-processed/flow/flow_event_",i,".RDATA"))))
  }
  
  df.list <- lapply(seq(35), function(i) get(paste0("Q.dat.",i)))
  df.list <- lapply(df.list, function(x) {colnames(x) <- c("Date","flow"); x})
  
  for (i in seq(df.list)){
    df.list[[i]]$Date<- format(df.list[[i]]$Date,format="%d/%m/%Y %H:%M")
    assign(paste0("Q.dat.",i), df.list[[i]])
  }
  
  for (i in 1:35){
    assign(paste0("out.data.",i), get(paste0("Q.dat.",i)))
    assign(paste0("input.",i), paste0("InitModConf",i,".inp"))
  }
  
  for (i in 1:35){
    assign(paste0("m",i),model.swmm(par=par.dirac,inp.file=get(paste0("input.",i)),out.data=get(paste0("Q.dat.",i)),path.library,path.rainfall,key))
  }
  
  simulation_results <- lapply(seq(35), function(i) get(paste0("m",i)))
  
  save(simulation_results,file = paste0(baseLoc,"/../data-processed/SWMM/Simulation_Results_",key_id,".RData"))
  
}
adisch87/COMCORDEcml documentation built on Dec. 22, 2017, 11:11 a.m.