pyofx is an OrcFxAPI extension.
The module contains a number of helper functions useful to those using OrcaFlex.
It also wraps the OrcFxAPI.Model class to provide extra functionality and offers interfaces to directories of OrcaFlex files and Distributed OrcaFlex.
pyofx has been built and tested on Python 2.7 and with version 9.7 of the OrcaFlex DLL
pyofx is pure Python and does not rely on any external packages with the exception of OrcFxAPI, which you will need to have installed.
pyofx can be found at the Python Package Index and so can be installed with pip/easy_install. If you don’t have pip or easy install you should consider installing pip-win on Windows for a pain-free interface to python packages.
There are also Windows installers for the 64-bit version of python.
You can check if it is installed properly like so:
>>> import pyofx
Once installed pyofx behaves exactly as OrcFxAPI does and provides all the same functions and classes.
The only class that is altered is the OrcFxAPI.Model class.
pyofx.Model has:
The class docstring contains some further details and examples:
Wrapper around OrcFxAPI.Model to add extra functionality.
>>> model = Model(r"C:\path o\data_file.dat")
>>> model.path
"C:\path o\data_file.dat"
2. added a model_name attribute (added in v0.0.9) of the file name without path or extensions:
>>> model = Model(r"C:\path o\data_file.dat")
>>> model.model_name
"data_file"
3. added the open method. This will open the model in the OrcaFlex GUI, if the model does not exist on disc then a windows temp file will be created. Won’t return until the OrcaFlex.exe instance is closed.
>>> model = Model()
>>> model.open() # Opens a temp file in OrcaFlex.exe
4. added objects_of_type() to return a list of objects in the model of a certain types/ e.g. model.objects_of_type(‘Line’)
>>> model = Model()
>>> model.CreateObject(otVessel, 'Bigger Boat')
>>> for v in model.objects_of_type('Vessel'):
... print v.name
"Bigger Boat"
Often when using the OrcaFlex API you want to apply some function to a folder or folders containing OrcaFlex .dat/.sim/.yml files. pyofx.Models provides an interface to make this easier.
a generator which yields OrcaFlex files in directories.
Suppose we have saved some simulations in C:UsersUserProjectOrcaFlex, to iterate over all those simulations we can use:
>>> for model in Models(r"C:\Users\User\Project\OrcaFlex"):
... print model
<pyofx.Model object at 0x00000000031B6DD8>
<pyofx.Model object at 0x00000000031B6E80>
Note that in the above example the generator yields Model objects of the .dat files. To return the path to the file rather than the Model object we can pass return_model=False and to return .sim file filetype=”sim”
There are various other options as detailed in the api_docs_:
There is also a way to add files to Distributed OrcaFlex from python with:
Python interface to Distributed OrcaFlex
>>> from pyofx import Jobs
>>> j = Jobs(r"\\network\path\to\OrcFxAPI.dll")
Methods:
add(filepath, variables=None)
Adds an orcaflex file to the list of jobs with optional variables object.
>>> j.add(r"\\network\folder\Hs=2.2m_model.dat", {'Hs':'2.2m'})
run(wait=False)
Submits jobs to Distributed Orcaflex. If wait is True it will not return unitll all jobs have completed.
>>> j.run(True)
>>> print "All jobs finished" # This won't print unitll the simulations are complete.