joari

readEAF.R

Dec 19th, 2018
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. # Find names and total number of REEs in file "pmo.dat"
  2. con = file("pmo.dat", "r")
  3. pat <- "ASSOCIACAO ENTRE REEs E SUBSISTEMAS"
  4. while ( TRUE ) {
  5. line = readLines(con, n = 1)
  6. # print(line)
  7. if ( length(line) == 0 ) break
  8. if(grepl(pattern = pat, line) == TRUE) {
  9. print(line)
  10. break
  11. }
  12. }
  13. line = readLines(con, n = 6)
  14. # Save REEs names and total number
  15. NameREE <- list()
  16. while (TRUE) {
  17. line <- readLines(con, n = 1)
  18. aa <- strsplit(line, split="\\s+")
  19. if (substring(line, 5, 7) != "---"){
  20. NameREE <- c(NameREE, aa[[1]][3])
  21. }
  22. else {
  23. print("NameREE", str(NameREE))
  24. break
  25. }
  26. }
  27. totREE <- length(NameREE)
  28. #
  29. # ***********************************************************************************************
  30. # Get initial and final years for EAF historical data in file pmo.dat
  31. # Caveat: here we take for granted that all inflows have the same common period of data
  32. pat <- "CONFIGURACAO DOS REEs"
  33. while ( TRUE ) {
  34. line = readLines(con, n = 1)
  35. # print(line)
  36. if ( length(line) == 0 ) break
  37. if(grepl(pattern = pat, line) == TRUE) {
  38. print(line)
  39. break
  40. }
  41. }
  42. line <- readLines(con, n = 4)
  43. line <- readLines(con, n = 1)
  44. aa <- strsplit(line, split="\\s+")
  45. anoFim <- as.numeric(aa[[1]][11])
  46. anoIni <- as.numeric(aa[[1]][10])
  47. totYears <- anoFim - anoIni + 1
  48. close(con)
  49. #
  50. # ***********************************************************************************************
  51. # Read energy inflows in file parp.dat for all REEs, for configuration No. 1
  52. EAFh = array(0, dim=c(totREE,totYears,13))
  53. rownames(EAFh) <- NameREE
  54. con <- file("parp.dat", "r")
  55. nREE <- 0
  56. # for (nREE in 1:length(NameREE)) {
  57. # for (nREE in 1:9) {
  58. while (nREE <= totREE) {
  59. line = readLines(con, n = 1)
  60. # print(line)
  61. if ( length(line) == 0 ) break
  62. if(grepl(pattern = "CONFIGURACAO No. 1", line) == TRUE) {
  63. # print(line)
  64. aa <- strsplit(line, split="\\s+")
  65. line = readLines(con, n = 4)
  66. nREE <- nREE + 1
  67. print(paste0("nREE = ", nREE, " name = ", aa[[1]][7]))
  68. EAFh[nREE,,] <- as.matrix(read.table(con, nrows = totYears))
  69. }
  70. }
  71.  
  72. names_REE <- unlist(NameREE)
  73. REE_lt <- setNames(NameREE, names_REE)
  74. # print(" fim de leitura de dados ")
  75.  
  76. # bla <- readEAF("CONFIGURACAO No. 1")
  77.  
  78. # ***********************************************************************************************
  79.  
  80.  
  81.  
  82.  
  83. readEAF <- function(confNumber){
  84. # ***********************************************************************************************
  85. # read energy inflow for configuration confNumber
  86. # ***********************************************************************************************
  87. # Find names and total number of REEs in file "pmo.dat"
  88. con = file("pmo.dat", "r")
  89. pat <- "ASSOCIACAO ENTRE REEs E SUBSISTEMAS"
  90. while ( TRUE ) {
  91. line = readLines(con, n = 1)
  92. # print(line)
  93. if ( length(line) == 0 ) break
  94. if(grepl(pattern = pat, line) == TRUE) {
  95. print(line)
  96. break
  97. }
  98. }
  99. line = readLines(con, n = 6)
  100. # Save REEs names and total number
  101. NameREE <- list()
  102. while (TRUE) {
  103. line <- readLines(con, n = 1)
  104. aa <- strsplit(line, split="\\s+")
  105. if (substring(line, 5, 7) != "---"){
  106. NameREE <- c(NameREE, aa[[1]][3])
  107. }
  108. else {
  109. print("NameREE", str(NameREE))
  110. break
  111. }
  112. }
  113. totREE <- length(NameREE)
  114. #
  115. # ***********************************************************************************************
  116. # Get initial and final years for EAF historical data in file pmo.dat
  117. # Caveat: here we take for granted that all inflows have the same common period of data
  118. pat <- "CONFIGURACAO DOS REEs"
  119. while ( TRUE ) {
  120. line = readLines(con, n = 1)
  121. # print(line)
  122. if ( length(line) == 0 ) break
  123. if(grepl(pattern = pat, line) == TRUE) {
  124. print(line)
  125. break
  126. }
  127. }
  128. line <- readLines(con, n = 4)
  129. line <- readLines(con, n = 1)
  130. aa <- strsplit(line, split="\\s+")
  131. anoFim <- as.numeric(aa[[1]][11])
  132. anoIni <- as.numeric(aa[[1]][10])
  133. totYears <- anoFim - anoIni + 1
  134. close(con)
  135. #
  136. # ***********************************************************************************************
  137. # Read energy inflows in file parp.dat for all REEs, for configuration No. 1
  138. EAFh = array(0, dim=c(totREE,totYears,13))
  139. rownames(EAFh) <- NameREE
  140. con <- file("parp.dat", "r")
  141. nREE <- 0
  142. # for (nREE in 1:length(NameREE)) {
  143. # for (nREE in 1:9) {
  144. while (nREE <= totREE) {
  145. line = readLines(con, n = 1)
  146. # print(line)
  147. if ( length(line) == 0 ) break
  148. if(grepl(pattern = "CONFIGURACAO No. 1", line) == TRUE) {
  149. # print(line)
  150. aa <- strsplit(line, split="\\s+")
  151. line = readLines(con, n = 4)
  152. nREE <- nREE + 1
  153. print(paste0("nREE = ", nREE, " name = ", aa[[1]][7]))
  154. EAFh[nREE,,] <- as.matrix(read.table(con, nrows = totYears))
  155. }
  156. }
  157.  
  158. names_REE <- unlist(NameREE)
  159. REE_lt <- setNames(NameREE, names_REE)
  160. # print(" fim de leitura de dados ")
  161. return(list(EAFh = EAFh, REE_lt = REE_lt))
  162. }
Advertisement
Add Comment
Please, Sign In to add comment