
0
Writing calculated script data back to the block model
Hi all expert python scripting people - I am converting a profit algorithm script from a Vulcan bcf script to Micromine python one and have a hurdle. I can call the file, make a copy and then hit a snag, first I have to create two new fields in the model called orsource and oretype, then pull data from several block model fields to use in filling the orsource field (oretype gets used later). The portion of the script attached runs wonderfully and does absolutely nothing....
Can someone point out what changes I need to a) create the fields and b) write the data to the fields I specify?
Pasted below because my file uplink wont work...
#
# profit algorithum script
# declare parameters
blkmdlin = 'HVK_LMIK_OBM.DAT'
blkmdlout = 'hvk_lmik_rsv_201412.DAT'
(Xfield,Yfield,Zfield) = ("East","North","RL")
BlockModelZoneName = "zone"
BlockmodeloxstateName = "oxstate"
BlockmodelRockTypeName = "rocktype"
#
# set conversion parameters
#XXXXX - Series of parameters defined in this portion for the calculations"""
#--------------------------------------------------------------
#
# set up block model
Project = MMpy.Project() #Make a variable that points to the current project
Path = Project.path() #Get the path of the current project
print (blkmdlin)
print (blkmdlout)
print (Path)
myBlockModel = Path + blkmdlin
Myblkmdl = MMpy.BlockModel()
if Myblkmdl.open(myBlockModel,Xfield,Yfield,Zfield) == False:
sys.exit("Error: Problem opening Block Model")
else:
print("%s contains %d records." %(myBlockModel,Myblkmdl.records_count))
Myblkmdl.copy(myBlockModel,Path + blkmdlout)
print("Copied \"" + blkmdlin + " to \"" + blkmdlout + "\"")
Myblkmdl.close()
blkmdl = Path + blkmdlout #define the copried blockmodel
Myblkmdl_new = MMpy.BlockModel()
Myblkmdl_new.open(blkmdl,Xfield,Yfield,Zfield)
print("Opened \"" + blkmdl + "\"")
NumRecords = Myblkmdl_new.records_count
print('It contains {0} records.'.format(NumRecords))
# add in the oretype and orsource fields
Myblkmdl_new.structure.add_field("orsource",MMpy.FieldType.short,1,1)
Myblkmdl_new.structure.add_field("oretype",MMpy.FieldType.short,1,1)
BlockModelZoneField = Myblkmdl_new.get_field_id(BlockModelZoneName) #Get the field ID we'll need for the Blockmodel file
BlockmodeloxstateField = Myblkmdl_new.get_field_id(BlockmodeloxstateName)
BlockmodelRockTypeField = Myblkmdl_new.get_field_id(BlockmodelRockTypeName)
#for record_id in range(1, NumRecords+1): #Go through all records of the file
# set up ore source
#
# ore source 11 = hv fresh
# ore source 12 = hv transitional
# ore source 14 = hv oxide
# ore source 21 = kv fresh
# ore source 22 = kv transitional
# ore source 24 = kv oxide
# ore source 31 = hm fresh
# ore source 32 = hm transitional
# ore source 34 = hm oxide
# set up ore sources and ox states for recoveries, will also classify fill
for record_id in range(1, NumRecords+1):
Currentzone = Myblkmdl_new.get_num_block_value(BlockModelZoneField, record_id) #Get the Zone data of the current record
Currentoxstate = Myblkmdl_new.get_num_block_value(BlockmodeloxstateField, record_id) #Get the oxstate of the current record
CurrentRockType = Myblkmdl_new.get_num_block_value(BlockmodelRockTypeField, record_id) #Get the Rocktype of the current record
orsource = 0
if Currentzone == 10 and Currentoxstate == 1:
Myblkmdl_new.set_block_value(orsource,record_id,11)
elif Currentzone == 10 and Currentoxstate == 2:
Myblkmdl_new.set_block_value(orsource,record_id,12)
elif Currentzone == 10 and Currentoxstate == 3:
Myblkmdl_new.set_block_value(orsource,record_id,14)
elif Currentzone == 20 and Currentoxstate == 1:
Myblkmdl_new.set_block_value(orsource,record_id,21)
elif Currentzone == 20 and Currentoxstate == 2:
Myblkmdl_new.set_block_value(orsource,record_id,22)
elif Currentzone == 20 and Currentoxstate == 3:
Myblkmdl_new.set_block_value(orsource,record_id,24)
elif Currentzone == 30 and Currentoxstate == 1:
Myblkmdl_new.set_block_value(orsource,record_id,31)
elif Currentzone == 30 and Currentoxstate == 2:
Myblkmdl_new.set_block_value(orsource,record_id,32)
elif Currentzone == 30 and Currentoxstate == 3:
Myblkmdl_new.set_block_value(orsource,record_id,34)
elif Currentzone == -99:
Myblkmdl_new.set_block_value(orsource,record_id,4)
elif Currentzone > 50:
Myblkmdl_new.set_block_value(orsource,record_id,4)
elif CurrentRockType == 1:
Myblkmdl_new.set_block_value(orsource,record_id,4)
else:
Myblkmdl_new.set_block_value(orsource,record_id,0)
#
#------------------------------------------------------
Thanks
Customer support service by UserEcho
Hi Ronald,
I'm not a python expert but this might help.
In order to add a field to a file I normally use the following code. I altered this code from the code produced by recording the File-> Create or Modify File process. You could copy this function and put it at the beginning or end of your script. I tend to keep my functions in a separate python file and import as I would do any module. By making it a function you can call it repeatedly.
The file_name does not include ".DAT". so if you want to use it with blkmdlout you should write blkmdlout[:-4]
You could use the function by calling it using the following line to add a column of Short type:
modify_add_column(blkmdlout[:-4], "orsource ", MMpy, field_type= "s")
You can add this line after you have created the file i.e. after you close the first block model file and before you open the blkmdlout file in Python.
It looks like the rest of your code is fine if you have the indentations correct except that you may need to replace
orsource = 0 #with something like:
orsource = Myblkmdl_new.get_field_id("orsource")
Just to be sure, the indentations at the end should look like this:
I hope that helps and gets things working. Apologies if I've totally missed the point!Rupert
Hi Ronald,
For the sort of control that you want, you need to use MMpy.File rather than MMpy.BlockModel. The script snippet below adds 3 new fields to a COLLAR file, and then populates those fields.
I have 3 screenshots that show the original file, the file after the fields were added and then finally after the fields are populated.
Thanks guys,
Paul, thanks for your help Monday. I got it all sorted Monday night and yesterday and ran the script and worked as it should - will need to throw in a check for the fields before trying to add them though - only throws up an error and then still runs but it is annoying all the same.