Python Library TUFLOW Results: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(10 intermediate revisions by 4 users not shown)
Line 1:
[[File:TUPython.png | 400px435px]]
 
'''THIS PAGE IS PRESENTLY BEING UPGRADED'''
=Introduction=
For the 2016 version of TUFLOW a new output format for time-series was made available, this has the following changes from previous versions:
Line 9:
This is the default approach for the 2016 version but can be modified with the TUFLOW command "<tt>Output Approach == Pre 2016</tt>" (refer to the manual for more information on this command.<br>
To make this data easier to work with an open source python library has been created and made available. This allows the user to load results and then interact with these. This library does not have any plotting functionality directly, with the returns typically being arrays (e.g. time and flows) however, the examples below give some examples using common plotting libraries such as matplotlib.<br>
=Getting the TUFLOW results python library=
You can find the TUFLOW_results.py (previously TUFLOW_Results2016.py) on the QGIS plugin repository, on the TUFLOW support GitHub repository or if you have QGIS and the TUFLOW plugin installed, you will already have it on your computer.
<ol>
<li>QGIS plugin repo: [https://plugins.qgis.org/plugins/tuflow/ https://plugins.qgis.org/plugins/tuflow/]<br>
: If you select '''Download Latest''' and save the .zip file, the TUFLOW_results.py sits inside the .zip file.</li>
<li>GitHub: [https://github.com/TUFLOW-Support/QGIS-TUFLOW-Plugin QGIS plugin https://github.com/TUFLOW-Support/QGIS-TUFLOW-Plugin]<br>
: If you select '''Clone or Download''' and then select '''Download Zip''' and save the .zip file, the TUFLOW_results.py sits inside the .zip file.</li>
<li>QGIS<br>
: If you have QGIS2 with the TUFLOW plugin installed via the plugin repository, the TUFLOW_results.py sits within: C:\Users\<User Name>\.qgis2\python\plugins\tuflow<br>
: If you have QGIS3 with the TUFLOW plugin installed via the plugin repository, the TUFLOW_results.py sits within: C:\Users\<User Name>\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\tuflow<br></li>
</ol>
The TUFLOW python library does not need to be installed in python (through pip or otherwise) like other common modules, the TUFLOW_results.py file just needs to be in the same location as your python project.
 
Note it shouldn’t matter if you’re using python 2 or python 3.
 
=Dependencies=
The TUFLOW results library calls a number of python librariesmodules, these are:
* csv
* numpy
* os
* sys
Of these the csv, os and sys functionality should be available directly with the python install. Depending on the method used to install python, numpy may need to be installed. If installation of numpy is required, please refer to the numpy documentation [http://www.numpy.org/ http://www.numpy.org/].
=Compatible Python Versions=
The functionality has been developed for Python 2.7, but should be compatible with Python 3.5. Other versions of python are currently untested.
Line 23 ⟶ 37:
==Import==
This library is imported with the typical python syntax.<br>
<pre>import TUFLOW_results2016TUFLOW_results</pre>
==Initialise Results (ResData)==
The results data functionality has a class defined, this is named '''ResData'''. An instance of the results can be initialised with the following syntax:
<pre>res = TUFLOW_results2016TUFLOW_results.ResData()</pre>
==Defined Methods (Functions)==
The following are the defined '''functions''' in the results data class (ResData).
Line 44 ⟶ 58:
<li>Domain - The domain to return the results for, this will be one of '''1D''', '''2D''' or '''RL''' (for reporting location data)</li>
<li>Results Type - The results type to be returned. This includes H (level), Q (flow), V (velocity), E (energy), QA (flow area) depending on the data types that have been output!</li>
<li>Geometry - The GIS geometry of the object, '''L''' for line, or '''P''' for point. If unsure or unknown this can be set to a dummyblank string such as 'dummy'.</li>
The return arguments are:
<li>found (logical) - Returns True if the data has been found, False if the data can not be found or an error has occurred.</li>
Line 145 ⟶ 159:
 
=Examples=
These examples canwork bewith downloadedthe inTutorial Model module 4 results.py format alongThe withtutorial themodel examplecan resultsbe downloaded fromon the [https://www.tuflow.com/Tuflow%20Tutorial%20Models.aspx TUFLOW website (link required)].
==Plot Flow in Channel==
<pre>
Line 151 ⟶ 165:
import sys #system functions
import matplotlib.pyplot as plt
import TUFLOW_results2016TUFLOW_results
 
input_res = r'C:\TUFLOW\Models\Tutorials\QGIS\Complete_Model\TUFLOW\results\M04\2d\plot\M04_5m_001.tpc'
 
# initialise the results class
res = TUFLOW_results2015TUFLOW_results.ResData()
 
# Load the data and terminate if error returned
Line 169 ⟶ 185:
#Get flow data
chan_id = 'ds3' # this is the channel ID to use
found, results, message = res.getTSgetTSData(chan_id,'1D','Q','L')
if found:
print ('found requested data')
# plot data
ax1.plot(res.times,results,color='b',label='Flow - '+chan_id)
else:
print (message)
sys.exit()
 
Line 192 ⟶ 208:
This will create a figure that looks like the above.
<br>
 
==Plot Multiple Water Levels==
In this example, it is assumed that the results data has been loaded as per the above.
Line 204 ⟶ 221:
#Get water level data
for node_id in node_ids: #for each node in the list above
found, results, message = res.getTSgetTSData(node_id,'1D','H','P')
if found:
print ('found requested data')
# plot data
ax1.plot(res.times,results,label='Level - '+node_id)
else:
print (message)
sys.exit()
 
Line 238 ⟶ 255:
 
#Get flow data
found, results, message = res.getTSgetTSData(chan_id,'1D','Q','L')
if found:
print ('found requested data')
ax1.plot(res.times,results,color='b',label='Flow - '+chan_id)
else:
print (message)
sys.exit()
 
#Get velocity data
found, results, message = res.getTSgetTSData(chan_id,'1D','V','L')
if found:
print ('found requested data')
ax2.plot(res.times,results,color='r',label='Velocity - '+chan_id)
else:
print (message)
sys.exit()
 
Line 279 ⟶ 296:
error, message = res.LP_getConnectivity(us_chan,ds_chan)
if error:
print (message)
sys.exit()
else:
print ('LP connectivity determined.')
 
print ('Get LP static data')
error, message = res.LP_getStaticData()
if error:
print (message)
sys.exit()
else:
print ('LP static data retrieved')
 
#create plot
Line 316 ⟶ 333:
This will create a figure that looks like the above.<br>
 
==Long Profile with additional data==
To be completed.
==Plotting Results from Multiple Simulations==
To be completed.
=Other Versions=
An older version of the library that is compatible with results from the 2013 version of TUFLOW (this is limited to the 1D results) is also available. This is currently undocumented, however, if you would like this please contact [mailto:support@tuflow.com support@tuflow.com].
 
 
 
{{Tips Navigation
|uplink=[[TuPlot| Back to TuPlot]]
}}