Проблема, с которой я сталкиваюсь, заключается в том, что функция теперь читает выходной файл от начала до конца на каждом этапе. timestep, что отнимает много времени, поскольку с каждым временным шагом выходной файл становится больше и может содержать миллионы строк текста. Есть ли лучший способ улучшить этот код, чтобы после извлечения правильных данных из строки на следующем временном шаге он начинал поиск данных с этого номера строки, а не с начала файла выходных данных? Большое спасибо за любой совет. Прикрепляю код функции:
Код: Выделить всё
def co2fromOUT(tStart):
#tStart = 2
import os
t_find = "TIME: " + str(tStart); #+ " days"; # from column 1 to 12 in CMG input data file
str2find_1 = "Compositions (mole fractions)"; # from column 47 to 84 in CMG input data file
str2find_2 = "CO2"
#################################### 2 locations - This file & python_GEM one
path_to_file = 'C:/Users/name/Desktop/test 1/run2/2.OUT'
file_name = "2" + "_" + "molCO2" + ".data" # file to save moles of CO2
init = str(0) + "\t" + str(0) + "\t" + str(0)
with open(path_to_file, 'r') as f:
lines = f.readlines()
# Create a new file to write CO2 moles
co2store = open(file_name,"a+")
if os.stat(file_name).st_size == 0:
co2store.write(init)
co2store.close()
count = 0
curr_molCO2 = 0
incr_molCO2 = 0
for i in range(0, len(lines)):
res1 = lines[i].find(t_find) # search for t_find in line i
if res1 == -1: # if t_find is not found in current line
continue
else:
# Case for solubility
res2 = lines[i - 12].find(str2find_1) # find str2find_1 in line i-12
if res2 == -1:
print("NOT FOUND 11 lines behind current time - Compositions (mole fractions)")
continue
else:
# Case for solubility
res3 = lines[i - 7].find(str2find_2) # find str2find_2 in line i-7
if res3 == -1:
print("NOT FOUND 6 lines behind current time - CO2")
continue
else:
with open(file_name,'r') as co2stores:
for co2line in co2stores:
pass
#last_line = lin
#co2line = co2stores.readlines()
#print(co2line)
prev_molCO2 = co2line.split("\t")
#print(prev_molCO2)
#print(prev_molCO2[1])
#lst = line[i - 24].strip().split()
#print( lines[i - 24][42:54] )
curr_molCO2 = float( lines[i - 7][42:55] ) # CO2 moles appear 6 lines below str2find_1
incr_molCO2 = curr_molCO2 - float( prev_molCO2[1] )
molCO2store = str(tStart) + "\t" + str(curr_molCO2) + "\t" + str(incr_molCO2)
co2store = open(file_name,"a+")
co2store.write("\n" + molCO2store)
co2store.close()
return incr_molCO2, curr_molCO2
Код: Выделить всё
Stream Cum Inj Cum Prod
----- ------- --------
Oil 0.00000E+00 0.00000E+00 bbl
Gas 9.37050E+07 6.24700E+07 cuft
Wet Gas 0.00000E+00 6.26270E+07 cuft
Water 9.29089E+01 2.22459E+02 bbl
Inj Rate Prod Rate
Phase moles/d moles/d
----- ----------- -----------
Oil 0.00000E+00 0.00000E+00
Gas 1.80444E+05 1.19860E+05
Wet Gas 0.00000E+00 1.19860E+05
Water 0.00000E+00 8.61632E+03
Compositions (mole fractions) of Total Field Produced and Injected Streams:
Oil Gas Wet Gas
Component Produced Injected Produced Injected Produced Injected
--------- -------- -------- -------- -------- -------- --------
CO2 0.00000E+00 0.00000E+00 1.43080E-05 1.00000E+00 1.43080E-05 0.00000E+00
C1 0.00000E+00 0.00000E+00 9.99986E-01 0.00000E+00 9.99986E-01 0.00000E+00
C2 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
C3 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
CO2_T 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
1
***********************************************************************************************************************************
TIME: 624.7 days G E M S E C T O R S U M M A R Y DATE: 1981:09:16
Подробнее здесь: https://stackoverflow.com/questions/786 ... hon-script