Modify your scripts to push the image files onto Nexus


EnSight with python scripting is a powerful tool that allows users to get their analysis performed in a reliable, repeatable, and fast fashion across a series of simulation results. One of the typical outcomes of these scripts is a set of images to be visually reviewed at a later time by the engineer to determine which models are the most promising. Due to the nature of the analysis and to the large number of simulation results that are available to the engineer today, the number of images to visually review can easily be in the hundreds, when not in the thousands. This poses some challenges in terms of data organization: organizing the files in such a way that they can easily be found, retrieved and categorized, is not a trivial task. This is one of the many reasons why engineers are switching to using structured databases for their data storage and organization. As a user approaching Nexus, you probably already have your python scripts for EnSight, and do not want to re-create them from scratch. The good news is that you do not have to! With the addition of a handful of lines in your script, you can store and keep organized your results in Nexus instead than in image files, scattered around your hard drive.

So, how to change your script to push the data into Nexus, instead of (or in addition to) creating images? How about a worked example...

A Clip Image Example
Let’s say you have a script that sweeps a section cut of the model and saves an image for each position of the section cut. The script might be structured similar to:

load_dataset()

for i in range(-20, 31, 1):

    if i == -20:

        create_clip(-2.0)

        create_text()

    else:

        change_clip(i*0.1)

    set_view()

    save_image(i+20)

In this example, the section cut is swept between the position X = -2.0 and X = 30, taking a slice at each 0.1 delta.

Nexus Script Modifications

How to modify the script so that it sends the image to Nexus instead than saving it to disk? Let’s start by loading the modules you will need. At the top of the script, add:

import cei

from template_editor import report_remote_server

from template_editor import report_objects

Connecting to the Nexus Server

The first step will be to connect to the Nexus server. In this example, we will suppose that there already is an existing Nexus server that is up and running. We will use the http://localhost:8000 server.


serverobj = report_remote_server.Server("http://localhost:8000", "nexus", "cei")

try:

    serverobj.validate()

except:

    print("Can't connect to the server")

    exit()


The first line will connect to the Nexus server (username = nexus, password = cei). The try/except verifies that the connection was successful.

The Nexus Session

The next step is to get a Nexus session. If you want to create a new session with every run of your script, you just need to change any attributes you want on the default session.  For example, to create a new session with every run with the session name 'Cube Analysis', the code would be:


session = serverobj.get_default_session()

session.application = 'Cube Analysis'


If, on the other hand, you want to add the information to an existing Nexus session, then you will need to get the session guid (identification string), and assign it to the session you are working on in this script. The following code will query the database, looking for an existing session with the name 'Cube Analysis'.  If it finds one, it will set the guid and application name of the default session to the existing session and all data pushed to that session will be assigned to the existing session. Otherwise, the code will create a new session with the desired application name. The corresponding lines are:


session = serverobj.get_default_session()

items = serverobj.get_objects(objtype=report_objects.SessionREST,

                              query='A|s_app|eq|Cube Analysis')

if len(items) > 0:

  session.guid = items[0].guid

  session.application = items[0].application

else:

  session.application = 'Cube Analysis'

Adding an Image

Finally, you get to the section where you push the image data into Nexus. You will first need to create an image item that corresponds to the current EnSight viewport, and then push it to Nexus. You can add some tags that will help you store some extra information for this image item, which you can later use to retrieve the image from the Nexus database. So in the original script, replace (or add) the following lines to the save_image(i+20) function:

img = ensight.render()

item = serverobj.create_item(name = "Clip_" + str(i+20).zfill(2))  

item.set_payload_image(img)

item.set_tags(str("X=" + str(i*0.1) + " var=temperature"))  # Creates two tags, X and var

error = serverobj.put_objects([item])

The first line creates img, an object that corresponds to the current visualization in EnSight. The second and third lines create a Nexus image item that corresponds to img. The fourth line adds some tags to the item, and finally the last line pushes it into the Nexus server.

More Complete Example

After running this new script, your Nexus database will contain one image item for each section cut that you have created in the script, with corresponding tags that identify the X position of the cut and the variable the section cut is colored by (in this example, temperature). You can find the scripts (the original clip_images.py and the modified clip_images_to_nexus.py) described above here. The scripts use the example cube.case dataset contained in the standard installation of EnSight. If you want to run directly these scripts, please consider changing the ensight.data.replace() and the ensight.file.image_file() lines to reflect the paths on your machine.


Created with the Personal Edition of HelpNDoc: Free Web Help generator