Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- processFile = function(filepath) {
- ENA <- NULL
- con = file(filepath, "r")
- # nline <- 0
- while ( TRUE ) {
- line = readLines(con, n = 1)
- # nline <- nline + 1
- if ( length(line) == 0 ) {
- break
- }
- # print(c(nline, length(line)))
- dd <- grep("\\d+", line)
- dd <- as.numeric(dd[[1]])
- ENA <- rbind (ENA, dd)
- }
- close(con)
- # print(c("ultimo ",nline, length(line)))
- return(ENA)
- }
- processChunk = function(connection) {
- ENA <- NULL
- nline <- 0
- while ( TRUE ) {
- line = readLines(connection, n = 1)
- if(grepl(pattern = "\f", line) == TRUE) {
- # if ( length(line) == 0 ) {
- break
- }
- # print(c(nline, length(line)))
- if ( length(line) > 0 ) {
- nline <- nline + 1
- ENA <- rbind (ENA, as.numeric(grep("\\d+", line))[[1]])
- }
- }
- print(c("ultimo nline = ",nline, "length line = ", length(line)))
- return(ENA)
- }
- # ***********************************************************************************************
- # Clean variables & Set working directory
- rm(list=ls()); # limpa vari?veis
- workdir <- "D:/ProjetosONS/Cursos/Alex/R-project/ENAs/2018/"; # diretorio de trabalho
- # workdir <- "~/ProjetosONS/Cursos/Alex/R-project/ENAs/2018/readtxt/"; # diretorio de trabalho
- setwd(workdir);
- # ***********************************************************************************************
- # Find REEs names and total number in file pmo.dat
- con = file("pmo.dat", "r")
- pat <- "ASSOCIACAO ENTRE REEs E SUBSISTEMAS"
- while ( TRUE ) {
- line = readLines(con, n = 1)
- # print(line)
- if ( length(line) == 0 ) break
- if(grepl(pattern = pat, line) == TRUE) {
- print(line)
- break
- }
- }
- line = readLines(con, n = 6)
- # Save REEs names
- NameREE <- list()
- while (TRUE) {
- line <- readLines(con, n = 1)
- aa <- strsplit(line, split="\\s+")
- if (substring(line, 5, 7) != "---"){
- NameREE <- c(NameREE, aa[[1]][3])
- }
- else {
- print("NameREE", str(NameREE))
- break
- }
- }
- totREE <- length(NameREE)
- # ***********************************************************************************************
- # Get initial and fnal year for ENA in file pmo.dat
- pat <- "CONFIGURACAO DOS REEs"
- while ( TRUE ) {
- line = readLines(con, n = 1)
- # print(line)
- if ( length(line) == 0 ) break
- if(grepl(pattern = pat, line) == TRUE) {
- print(line)
- break
- }
- }
- line <- readLines(con, n = 4)
- line <- readLines(con, n = 1)
- aa <- strsplit(line, split="\\s+")
- anoFim <- as.numeric(aa[[1]][11])
- anoIni <- as.numeric(aa[[1]][10])
- totYears <- anoFim - anoIni + 1
- # ***********************************************************************************************
- # Read energy inflow in parp.dat for all REEs
- Btt = array(0, dim=c(totREE,totYears,13))
- rownames(Btt) <- NameREE
- close(con)
- con = file("parp.dat", "r")
- nREE <- 0
- # for (nREE in 1:length(NameREE)) {
- # for (nREE in 1:9) {
- # while ( nREE <= 9) {
- while (nREE <= totREE) {
- line = readLines(con, n = 1)
- # print(line)
- if ( length(line) == 0 ) break
- if(grepl(pattern = "CONFIGURACAO No. 1", line) == TRUE) {
- # print(line)
- aa <- strsplit(line, split="\\s+")
- line = readLines(con, n = 4)
- nREE <- nREE + 1
- print(paste0("nREE = ", nREE, " name = ", aa[[1]][7]))
- Btt[nREE,,] <- as.matrix(read.table(con, nrows = totYears))
- }
- }
- print(" fim de leitura de dados ")
- for (nREE in 1:totREE ) {
- print(paste0("nREE = ", nREE, " name = ", NameREE[[nREE]]))
- print(Btt[nREE,1:5,1:13])
- }
- # Xtt = array(0, dim=c(15,85,13))
- # AAA <- processChunk(con)
- # X[nREE,,] <- AAA
- # X[nREE,1:5,1:13]
- # break
- # print(line)
- # library(rlist)
- # list.append
- # list <- c(list, newelement)
- # aa <- strsplit(line, split="\\s+")
- # aa[[1]][7]
- # line = readLines(con, n = 4)
- #line = readLines(con, n = 1)
- #print(line)
- # nREE <- nREE + 1
- # print(c("nREE = ", nREE, "name = ", aa[[1]][7]))
- # AAA <- processChunk(con)
- # X[nREE,,] <- AAA
- #
- # X[nREE,1:8,1:13]
- # test.fixed <- read.table(connection, nrows = 85)
- #
- # BBB[[4]] <- test.fixed
- #
- # Tmp file
- # fifoname <- "tmp.txt"
- # myfifo <- file(fifoname, "w+")
- # con <- file(filepath, "r")
- # for (i in 1:10) {
- # writeLines( readLines(con, n=1), myfifo )
- # }
- # flush(myfifo)
- # close(myfifo)
- # myfifo = file(fifoname, "r")
- # test.pipe <- read.fwf(myfifo, width = c(4, 10, rep(11,11)))
- #
- #
- #
- # # Tmp stream (fifo)
- # fifoname <- tempfile(fileext="-fifo")
- # myfifo <- fifo(fifoname, "w+")
- # con <- file("foo", "r")
- # for (i in 1:2) {
- # writeLines( readLines(con, n=1), myfifo )
- # }
- # for (i in 1:20) {
- # print(i)
- # x <- readLines(myfifo, n=1)
- # print(x)
- # }
- #
- # test.pipe2 <- read.fwf(myfifo, width = c(4, 10, rep(11,11)))
- # close(myfifo)
- # unlink(fifoname)
- #
- # x <- readLines(myfifo, n=1)
- #
- #
- #
- # fd <- file("foo.Rmd", open = "wt")
- # writeLines('first line', fd)
- # writeLines('second line', fd)
- # close(fd)
- #
- # ENA <- as.matrix(dado[,2:13])
- # processFile(filepath)
- #
- # aa <- array(as.numeric(unlist(line)), dim=c(1, 12))
- # as.matrix(line(aa))
- #
- # mylist <- c(1, 2, 3, 4)
- #
- # array(as.numeric(unlist(mylist)))#, dim=c(14, 5, 10))BBB[[4]] <- test.fixed
- # Try checking the length of data returned by readBin:
- #
- # while (length(a <- readBin(f, 'int', n=1)) > 0) {
- # # do something
- # }
- #
- #
- # # Tmp file
- # fifoname <- "tmp.txt"
- # myfifo <- file(fifoname, "w+")
- # con <- file(filepath, "r")
- # for (i in 1:10) {
- # writeLines( readLines(con, n=1), myfifo )
- # }
- # flush(myfifo)
- # close(myfifo)
- # myfifo = file(fifoname, "r")
- # test.pipe <- read.fwf(myfifo, width = c(4, 10, rep(11,11)))
- #
- #
- #
- # # Tmp stream (fifo)
- # fifoname <- tempfile(fileext="-fifo")
- # myfifo <- fifo(fifoname, "w+")
- # con <- file("foo", "r")
- # for (i in 1:2) {
- # writeLines( readLines(con, n=1), myfifo )
- # }
- # for (i in 1:20) {
- # print(i)
- # x <- readLines(myfifo, n=1)
- # print(x)
- # }
- #
- # test.pipe2 <- read.fwf(myfifo, width = c(4, 10, rep(11,11)))
- # close(myfifo)
- # unlink(fifoname)
- #
- # x <- readLines(myfifo, n=1)
- #
- #
- #
- # fd <- file("foo.Rmd", open = "wt")
- # writeLines('first line', fd)
- # writeLines('second line', fd)
- # close(fd)
- #
- # ENA <- as.matrix(dado[,2:13])
- # processFile(filepath)
- #
- # aa <- array(as.numeric(unlist(line)), dim=c(1, 12))
- # as.matrix(line(aa))
- #
- # mylist <- c(1, 2, 3, 4)
- #
- # array(as.numeric(unlist(mylist)))#, dim=c(14, 5, 10))
Advertisement