joari

readfile.R

Nov 28th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.38 KB | None | 0 0
  1. processFile = function(filepath) {
  2.   ENA <- NULL
  3.   con = file(filepath, "r")
  4.   # nline <- 0
  5.   while ( TRUE ) {
  6.     line = readLines(con, n = 1)
  7.     # nline <- nline + 1
  8.     if ( length(line) == 0 ) {
  9.       break
  10.     }
  11.     # print(c(nline, length(line)))
  12.     dd <- grep("\\d+", line)
  13.     dd <- as.numeric(dd[[1]])
  14.     ENA <- rbind (ENA, dd)
  15.   }
  16.   close(con)
  17.   # print(c("ultimo ",nline, length(line)))
  18.   return(ENA)
  19. }
  20.  
  21. processChunk = function(connection) {
  22.   ENA <- NULL
  23.   nline <- 0
  24.   while ( TRUE ) {
  25.     line = readLines(connection, n = 1)
  26.     if(grepl(pattern = "\f", line) == TRUE) {
  27.       # if ( length(line) == 0 ) {
  28.       break
  29.     }
  30.     # print(c(nline, length(line)))
  31.     if ( length(line) > 0 ) {
  32.       nline <- nline + 1
  33.       ENA <- rbind (ENA, as.numeric(grep("\\d+", line))[[1]])
  34.     }
  35.   }
  36.   print(c("ultimo nline = ",nline, "length line = ", length(line)))
  37.   return(ENA)
  38. }
  39.  
  40. # ***********************************************************************************************
  41. # Clean variables & Set working directory
  42. rm(list=ls());                     # limpa vari?veis
  43. workdir <- "D:/ProjetosONS/Cursos/Alex/R-project/ENAs/2018/"; # diretorio de trabalho
  44. # workdir <- "~/ProjetosONS/Cursos/Alex/R-project/ENAs/2018/readtxt/"; # diretorio de trabalho
  45. setwd(workdir);
  46. # ***********************************************************************************************
  47. # Find REEs names and total number in file pmo.dat
  48. con = file("pmo.dat", "r")
  49. pat <- "ASSOCIACAO ENTRE REEs E SUBSISTEMAS"
  50. while ( TRUE ) {
  51.   line = readLines(con, n = 1)
  52.   # print(line)
  53.   if ( length(line) == 0 ) break
  54.   if(grepl(pattern = pat, line) == TRUE) {
  55.     print(line)
  56.     break
  57.   }
  58. }  
  59. line = readLines(con, n = 6)
  60. # Save REEs names
  61. NameREE <- list()
  62. while (TRUE) {
  63.   line <- readLines(con, n = 1)
  64.   aa <- strsplit(line, split="\\s+")
  65.   if (substring(line, 5, 7) != "---"){
  66.     NameREE <- c(NameREE, aa[[1]][3])
  67.   }
  68.   else {
  69.     print("NameREE", str(NameREE))
  70.     break
  71.   }
  72. }
  73. totREE <- length(NameREE)
  74.  
  75. # ***********************************************************************************************
  76. # Get initial and fnal year for ENA in file pmo.dat
  77. pat <- "CONFIGURACAO DOS REEs"
  78. while ( TRUE ) {
  79.   line = readLines(con, n = 1)
  80.   # print(line)
  81.   if ( length(line) == 0 ) break
  82.   if(grepl(pattern = pat, line) == TRUE) {
  83.     print(line)
  84.     break
  85.   }
  86. }  
  87. line <- readLines(con, n = 4)
  88. line <- readLines(con, n = 1)
  89. aa <- strsplit(line, split="\\s+")          
  90. anoFim <- as.numeric(aa[[1]][11])
  91. anoIni <- as.numeric(aa[[1]][10])
  92. totYears <- anoFim - anoIni + 1
  93.  
  94. # ***********************************************************************************************
  95. # Read energy inflow in parp.dat for all REEs
  96. Btt = array(0, dim=c(totREE,totYears,13))
  97. rownames(Btt) <- NameREE
  98.  
  99. close(con)
  100. con = file("parp.dat", "r")
  101. nREE <- 0
  102. # for (nREE in 1:length(NameREE)) {
  103. # for (nREE in 1:9) {
  104. # while ( nREE <= 9) {
  105. while (nREE <= totREE) {
  106.   line = readLines(con, n = 1)
  107.   # print(line)
  108.   if ( length(line) == 0 ) break
  109.   if(grepl(pattern = "CONFIGURACAO No.    1", line) == TRUE) {
  110.     # print(line)
  111.     aa <- strsplit(line, split="\\s+")
  112.     line = readLines(con, n = 4)
  113.     nREE <- nREE + 1
  114.     print(paste0("nREE = ", nREE, "  name = ", aa[[1]][7]))
  115.     Btt[nREE,,] <- as.matrix(read.table(con, nrows = totYears))
  116.   }
  117. }
  118.  
  119. print(" fim de leitura de dados ")
  120.  
  121. for (nREE in 1:totREE ) {
  122.   print(paste0("nREE = ", nREE, "  name = ", NameREE[[nREE]]))
  123.   print(Btt[nREE,1:5,1:13])
  124. }
  125.  
  126.  
  127. # Xtt = array(0, dim=c(15,85,13))
  128.  
  129. # AAA <- processChunk(con)
  130. # X[nREE,,] <- AAA
  131. # X[nREE,1:5,1:13]
  132. # break
  133.  
  134. # print(line)
  135. # library(rlist)
  136. # list.append
  137. # list <- c(list, newelement)
  138. # aa <- strsplit(line, split="\\s+")
  139. # aa[[1]][7]
  140. # line = readLines(con, n = 4)
  141. #line = readLines(con, n = 1)
  142. #print(line)
  143.  
  144. # nREE <- nREE + 1
  145. # print(c("nREE = ", nREE, "name = ", aa[[1]][7]))
  146. # AAA <- processChunk(con)
  147.  
  148. # X[nREE,,] <- AAA
  149. #
  150. # X[nREE,1:8,1:13]
  151.  
  152.  
  153.  
  154. # test.fixed <- read.table(connection, nrows = 85)
  155. #
  156. # BBB[[4]] <- test.fixed
  157. #
  158. # Tmp file
  159. # fifoname <- "tmp.txt"
  160. # myfifo <- file(fifoname, "w+")
  161. # con <- file(filepath, "r")
  162. # for (i in 1:10) {
  163. #   writeLines( readLines(con, n=1), myfifo )
  164. # }
  165. # flush(myfifo)
  166. # close(myfifo)
  167. # myfifo = file(fifoname, "r")
  168. # test.pipe <- read.fwf(myfifo, width = c(4, 10, rep(11,11)))
  169. #
  170. #
  171. #
  172. # # Tmp stream (fifo)
  173. # fifoname <- tempfile(fileext="-fifo")
  174. # myfifo <- fifo(fifoname, "w+")
  175. # con <- file("foo", "r")
  176. # for (i in 1:2) {
  177. #   writeLines( readLines(con, n=1), myfifo )
  178. # }
  179. # for (i in 1:20) {
  180. #   print(i)
  181. #   x <- readLines(myfifo, n=1)
  182. #   print(x)
  183. # }
  184. #
  185. # test.pipe2 <- read.fwf(myfifo, width = c(4, 10, rep(11,11)))
  186. # close(myfifo)
  187. # unlink(fifoname)
  188. #
  189. # x <- readLines(myfifo, n=1)
  190. #
  191. #
  192. #
  193. # fd <- file("foo.Rmd", open = "wt")
  194. # writeLines('first line', fd)
  195. # writeLines('second line', fd)
  196. # close(fd)
  197. #
  198. # ENA <- as.matrix(dado[,2:13])
  199. # processFile(filepath)
  200. #
  201. # aa <- array(as.numeric(unlist(line)), dim=c(1, 12))
  202. # as.matrix(line(aa))
  203. #
  204. # mylist <- c(1, 2, 3, 4)
  205. #
  206. # array(as.numeric(unlist(mylist)))#, dim=c(14, 5, 10))BBB[[4]] <- test.fixed
  207.  
  208.  
  209. # Try checking the length of data returned by readBin:
  210. #  
  211. #   while (length(a <- readBin(f, 'int', n=1)) > 0) {
  212. #     # do something
  213. #   }
  214. #
  215. #
  216. # # Tmp file
  217. # fifoname <- "tmp.txt"
  218. # myfifo <- file(fifoname, "w+")
  219. # con <- file(filepath, "r")
  220. # for (i in 1:10) {
  221. #   writeLines( readLines(con, n=1), myfifo )
  222. # }
  223. # flush(myfifo)
  224. # close(myfifo)
  225. # myfifo = file(fifoname, "r")
  226. # test.pipe <- read.fwf(myfifo, width = c(4, 10, rep(11,11)))
  227. #
  228. #
  229. #
  230. # # Tmp stream (fifo)
  231. # fifoname <- tempfile(fileext="-fifo")
  232. # myfifo <- fifo(fifoname, "w+")
  233. # con <- file("foo", "r")
  234. # for (i in 1:2) {
  235. #   writeLines( readLines(con, n=1), myfifo )
  236. # }
  237. # for (i in 1:20) {
  238. #   print(i)
  239. #   x <- readLines(myfifo, n=1)
  240. #   print(x)
  241. # }
  242. #
  243. # test.pipe2 <- read.fwf(myfifo, width = c(4, 10, rep(11,11)))
  244. # close(myfifo)
  245. # unlink(fifoname)
  246. #
  247. # x <- readLines(myfifo, n=1)
  248. #
  249. #
  250. #
  251. # fd <- file("foo.Rmd", open = "wt")
  252. # writeLines('first line', fd)
  253. # writeLines('second line', fd)
  254. # close(fd)
  255. #
  256. # ENA <- as.matrix(dado[,2:13])
  257. # processFile(filepath)
  258. #
  259. # aa <- array(as.numeric(unlist(line)), dim=c(1, 12))
  260. # as.matrix(line(aa))
  261. #
  262. # mylist <- c(1, 2, 3, 4)
  263. #
  264. # array(as.numeric(unlist(mylist)))#, dim=c(14, 5, 10))
Advertisement