< All Topics

Get user selected parameters for a deposition run

If you’ve got a script with a deposition model in it, many of the parameters may be hard wired.  If the user needs to run a different scenario (e.g. different volume, tailings stream, base grid etc) then it would be useful to have a dialog box pop up at the start of the script execution to get these parameters. This example shows how this can be done using some of the Muk3D Python API.

Building a dialog

The first step is going to be to create a simple macro to ask for some parameters from the user, starting with the deposition volume.  Within the muk3d.ui.forms module, there are some functions to help build dialog boxes.  This is done using the ask function, as well as some get_* functions for other fields.

from muk3d.ui.forms import ask, get_float
from muk3d.util import is_valid

result = ask(fields=[get_float("Volume", default_value=20),],
                    last_values_key='my_macro' 
                    )

is_valid(result) 

volume = result['Volume']

print volume

The ask function takes a named argument called fields that is expected to be a list with the form fields, created using the appropriate get_* functions.  In this example, in line 4, a single field is added to the form that expects a floating point number representing the deposition volume.

In line 5, there is another named argument called last_values_key. This is optional, but if specified will allow the values entered into the field to be cached so that the user can recall them other times using the right-click context menu item Last values for all fields.

When the ask function is executed, it returns a result value.  If the user clicks Cancel on the dialog box, the return value is None. Otherwise the return value is a Python dictionary with the dictionary keys set to be the name of each field.

In line 8, the is_valid function checks the return value and if its None, gracefully exits the script.  If the script continued to execute, an error would occur when trying to get the Volume, since result wouldn’t be a dictionary. 

This validation could be done manually, but the is_valid function is just a convenient way to do this in a single line.

In line 10, a new variable is created called volume, and its assigned the value the user entered into the Volume field.  Since its a dictionary, values are accessed using the square brackets with the name of the dictionary key.

Now we’ll modify this to also get a file for the base grid for the deposition run.  This can be done by importing the function get_filename from the muk3d.ui.forms module.

from muk3d.ui.forms import ask, get_float, get_filename
from muk3d.util import is_valid

result = ask(fields=[get_float("Volume", default_value=20),
                    get_filename("Base grid", filters=['Grids (*.mgrid)',], 
                                select_existing_file=True),],
                    last_values_key='my_macro' 
                    )

is_valid(result) 

volume = result['Volume']
base_grid=  result['Base grid']

print volume
print base_grid

In lines 5 and 6 the filename field is added to the dialog.  The filters argument is a list that specifies the file types you want the file chooser to display.  

Getting the tailings names

The next step is to add an option for the user to select the tailings stream to use for the deposition. This will be done by first getting a list of all the tailings defined in the tailings database, and then adding a choice drop-down to the dialog box.

from muk3d.ui.forms import ask, get_float, get_filename, get_choice
from muk3d.util import is_valid
from muk3d.ooze import get_tailings_names


tailings_names = get_tailings_names()

result = ask(fields=[get_float("Volume", default_value=20),
                    get_filename("Base grid", filters=['Grids (*.mgrid)',], 
                                select_existing_file=True),
                    get_choice("Tailings", tailings_names)], 
                    last_values_key='my_macro' 
                    )

is_valid(result) 

volume = result['Volume']
base_grid=  result['Base grid']
tailings = result['Tailings']

print volume
print base_grid
print tailings

To get the list of tailings names, we can import the function get_tailings_names from the muk3d.ooze module in line 3.  A list of tailings names from the Tailings database is assigned to the tailings_names variable in line 6.  The choice field is added to the form in line 11, with the tailings names.

Updating a deposition run script to use these user values

The final step is to incorporate these user selected values into a deposition run.  The example below shows how this is done. Note that to get the code for executing the deposition run, you just need to record a macro of you doing a deposition run and the appropriate function calls will be generated.

from muk3d.ui.forms import ask, get_float, get_filename, get_choice
from muk3d.util import is_valid
from muk3d.ooze import get_tailings_names


tailings_names = get_tailings_names()

result = ask(fields=[get_float("Volume", default_value=20),
                    get_filename("Base grid", filters=['Grids (*.mgrid)',], 
                                select_existing_file=True),
                    get_choice("Tailings", tailings_names)], 
                    last_values_key='my_macro' 
                    )

is_valid(result) 

volume = result['Volume']
base_grid=  result['Base grid']
tailings = result['Tailings']

cmd = get_command('Single stream no pond, auto flow path')
result = cmd({   'base': base_grid,
    'deposition model': 'Single stream no pond, auto flow path',
    'dischargePoints': u'discharge_1.mcurve',
    'initialDischargeElevation': 120.0,
    'maxChange': 2.0,
    'maxIterations': 40,
    'maxPondIterations': 1,
    'pond_location': None,
    'tailings': selected_tailings,  
    'tolerance': 1.0,
    'tonnage': volume,
    'verticalOffset': 0.0})

In line 22, the static value of the base grid is replaced with the base_grid variable. In line 30, the tailings properties are added, and then in line 32 the volume/tonnage is specified.

Table of Contents