tpl_path = '../../pyfas/test/test_files/'
fname = '11_2022_BD.tpl'
tpl = fa.Tpl(tpl_path+fname)

"""
Explanation: OLGA tpl files, examples and howto
For an tpl file the following methods are available:

<b>filter_data</b> - return a filtered subset of trends
<b>extract</b> - extract a single trend variable
<b>to_excel</b> - dump all the data to an excel file

The usual workflow should be:

Load the correct tpl
Select the desired variable(s)
Extract the results or dump all the variables to an excel file
Post-process your data in Excel or in the notebook itself

Tpl loading
To load a specific tpl file the correct path and filename have to be provided:
End of explanation
"""


tpl.filter_data('PT')

"""
Explanation: Trend selection
A tpl file may contain hundreds of trends, in particular for complex networks. For this reason a filtering method is quite useful. A trend can be specified in an OLGA input files in differnet ways, the identification of a single trend may be not trivial.
The easiest way is to filter all the trends using patters, the command tpl.filter_trends("PT") filters all the pressure trends (or better, all the trends with "PT" in the description, if you have defined a temperature trend in the position "PTTOPSIDE", for example, this trend will be selected too).
The resulting python dictionaly will have a unique index for each filtered trend that can be used to identify the interesting trend(s).
In case of an emply pattern all the available trends will be reported.
End of explanation
"""


tpl.filter_data("'POSITION:' 'EXIT'")

"""
Explanation: or
End of explanation
"""


pd.DataFrame(tpl.filter_data('PT'), index=("Trends",)).T

"""
Explanation: The same outpout can be reported as a pandas dataframe:
End of explanation
"""


tpl.view_trends('PT')

"""
Explanation: The view_trends method provides the same info better arranged:
End of explanation
"""


tpl.view_trends('TM')

tpl.view_trends('PT')

"""
Explanation: Dump to excel
To dump all the variables in an excel file use tpl.to_excel()
If no path is provided an excel file with the same name of the tpl file is generated in the working folder. Depending on the tpl size this may take a while.
Extract a specific variable
Once you know the variable(s) index you are interested in (see the filtering paragraph above for more info) you can extract it (or them) and use the data directly in python.
Let's assume you are interested in the inlet pressure and the outlet temperature:
End of explanation
"""


# single trend extraction
tpl.extract(11)
tpl.extract(38)

# multiple trends extraction
tpl.extract(12, 37)

"""
Explanation: Our targets are:
<i>variable 11</i> - TM 'POSITION:' 'EXIT' '(C)' 'Fluid temperature'
and  
<i>variable 38</i> - PT 'POSITION:' 'TUBINGHEAD' '(PA)' 'Pressure'\
Now we can proceed with the data extraction:
End of explanation
"""


tpl.data.keys()

"""
Explanation: The tpl object now has the four trends available in the data attribute:
End of explanation
"""


tpl.label

"""
Explanation: while the label attibute stores the variable type as a dictionary:
End of explanation
"""


%matplotlib inline

pt_inlet = tpl.data[38]
tm_outlet = tpl.data[11]

fig, ax1 = plt.subplots(figsize=(12, 7));
ax1.grid(True)
p0, = ax1.plot(tpl.time/3600, tm_outlet)
ax1.set_ylabel("Outlet T [C]", fontsize=16)
ax1.set_xlabel("Time [h]", fontsize=16)

ax2 = ax1.twinx()
p1, = ax2.plot(tpl.time/3600, pt_inlet/1e5, 'r')
ax2.grid(False)
ax2.set_ylabel("Inlet P [bara]", fontsize=16)

ax1.tick_params(axis="both", labelsize=16)
ax2.tick_params(axis="both", labelsize=16)


plt.legend((p0, p1), ("Outlet T", "Inlet P"), loc=4, fontsize=16)
plt.title("Inlet P and Outlet T for case FC1", size=20);

"""
Explanation: Data processing
The results available in the data attribute are numpy arrays and can be easily manipulated and plotted:
End of explanation
"""


import os
import sys
import time
import pyfas as fa


def getVarsInds(tpl, emptyLst):

	for _, pos in enumerate(allPos):
	
		lst = []
	
		# dictionary of the following kind:
		# {3: "GLT 'POSITION:' 'EXIT' '(KG/S)' 'Total liquid mass flow'\n",
		#  4: "GLTHL 'POSITION:' 'EXIT' '(KG/S)' 'Mass flow rate of oil'\n"}
		myDic = tpl.filter_trends("'POSITION:' '{0}'".format(pos))
	
		for _, var in enumerate(allVar):

			for _, (k, v) in enumerate(myDic.items()):
			
				lstStr = v.split(" ")
				
				if lstStr[0] == var:
				
					lst.append(int(k))
		
		emptyLst.append(lst)


def getData(tplFileName, fullLst):

	myFlag = False
	
	fout = open("{0}.csv".format(tplFileName), 'w')
	
	# write header
	outLine = ""
	for _, pos in enumerate(allPos):
		for _, var in enumerate(allVar):
			outLine += "{0},{1} {2},".format( "Time [H]", pos, var )
	
	fout.writelines("{0}\n".format(outLine))

	# write data
	with open(tplFileName) as infile:
		for line in infile:
			
			if myFlag:
				
				myValList = line.split()
				
				myTime = float(myValList[0]) / 3600.0 # in hours
				
				outLine = ""
				
				for i in range(len(allPos)):
					for j in range(len(allVar)):
						outLine += "{0},{1},".format( myTime, float(myValList[fullLst[i][j]]) * allMul[j] )
				
				fout.write("{0}\n".format(outLine))
			
			else:
			
				if line.find("TIME SERIES") > -1: myFlag = True

	fout.close()


def main():

	print( "{0} initialization".format(time.strftime("%H:%M:%S", time.localtime())) )
	fname = myTPLFile
	tpl = fa.Tpl(fname)
	
	# list of indices for vars at every position (separate list for every position in order)
	varIndLst = []
	
	getVarsInds(tpl, varIndLst)
	
	print( "{0} extraction".format(time.strftime("%H:%M:%S", time.localtime())) )
	getData(fname, varIndLst)
	print( "{0} done".format(time.strftime("%H:%M:%S", time.localtime())) )


# multiplication factors and variables
allMul = [1.0, 1.0, 1.0e-5, 1.0]
allVar = ["ROL", "ROG", "PT", "TM"]

# positions
allPos = ["FLOWLINE_1KM", "FLOWLINE_2KM", "FLOWLINE_3KM", "FLOWLINE_4KM", "FLOWLINE_5KM"]

myTPLFile = "OLGA_Simulation.tpl"

main()

"""
Explanation: Advanced data processing
An example of advanced data processing for Python enthusiasts and professional flow assurance. Script below extracts variable trends at given positions. Usage instructions:
- For unit conversion multiplication factors for every variable can be provided.
- Only few global variables and lists have to be defined (allMul, allVar, allPos, and myTPLFile), there is no need to edit functions, unless you understand what you are doing.
- Extracted trends are written in to a CSV file "OLGA_Simulation.tpl.csv", where "OLGA_Simulation.tpl" is the simulation file.
- The script does not perform error checks, make sure that all variables and positions are present in the simulation file.
End of explanation
"""


