Skip to content

Commit

Permalink
Initial conversion of createMappingFile to algorithm
Browse files Browse the repository at this point in the history
Refs #10085
  • Loading branch information
DanNixon committed Aug 12, 2014
1 parent 4be23b6 commit 24a5369
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
@@ -0,0 +1,50 @@
from mantid.api import *
from mantid.kernel import *
from mantid.simpleapi import *
import os

class CreateMappingFile(PythonAlgorithm):

def category(self):
return 'Inelastic;PythonAlgorithms'

def summary(self):
return '' #TODO

def PyInit(self):
self.declareProperty(FileProperty('Filename', '', FileAction.Save))

self.declareProperty(name='GroupCount', defaultValue=0,
doc='Number of goups')

self.declareProperty(name="SpectraRange", defaultValue="0,1",
doc="SpectraRange")

def PyExec(self):
from mantid import config

group_filename = self.getPropertyValue('Filename')
num_groups = int(self.getPropertyValue('GroupCount'))
spectra_range = self.getPropertyValue('SpectraRange').split(',')

num_detectors = (int(spectra_range[1]) - int(spectra_range[0])) + 1
num_spectra = num_detectors / num_groups

filename = config['defaultsave.directory']
filename = os.path.join(filename, group_filename)

handle = open(filename, 'w')
handle.write(str(num_groups) + "\n")

for group in range(0, num_groups):
group_ind = group * num_spectra + int(spectra_range[0])
handle.write(str(group + 1) + '\n')
handle.write(str(num_spectra) + '\n')
for spectra in range(1, num_spectra+1):
spectra_ind = group_ind + spectra - 1
handle.write(str(spectra_ind).center(4) + ' ')
handle.write('\n')

handle.close()

AlgorithmFactory.subscribe(CreateMappingFile)
Expand Up @@ -645,47 +645,41 @@ namespace CustomInterfaces
*/
QString IndirectConvertToEnergy::createMapFile(const QString& groupType)
{
QString groupFile, ngroup, nspec;
QString ndet = "( "+m_uiForm.leSpectraMax->text()+" - "+m_uiForm.leSpectraMin->text()+") + 1";
using namespace Mantid::API;

if ( groupType == "File" )
QString groupFile = m_uiForm.cbInst->itemData(m_uiForm.cbInst->currentIndex()).toString().toLower()
+ "_" + m_uiForm.cbAnalyser->currentText() + m_uiForm.cbReflection->currentText()
+ "_" + groupType + ".map";

QString specRange = m_uiForm.leSpectraMin->text() + "," + m_uiForm.leSpectraMax->text();

if(groupType == "File")
{
groupFile = m_uiForm.ind_mapFile->getFirstFilename();
if ( groupFile == "" )
if(groupFile == "")
{
emit showMessageBox("You must enter a path to the .map file.");
}
return groupFile;
}
else if ( groupType == "Groups" )
else if(groupType == "Groups")
{
ngroup = m_uiForm.leNoGroups->text();
nspec = "( " +ndet+ " ) / " +ngroup;
}
else if ( groupType == "All" )
{
return "All";
}
else if ( groupType == "Individual" )
{
return "Individual";
}
IAlgorithm_sptr mappingAlg = AlgorithmManager::Instance().create("CreateMappingFile");
mappingAlg->initialize();

groupFile = m_uiForm.cbInst->itemData(m_uiForm.cbInst->currentIndex()).toString().toLower();
groupFile += "_" + m_uiForm.cbAnalyser->currentText() + m_uiForm.cbReflection->currentText();
groupFile += "_" + groupType + ".map";
mappingAlg->setProperty("Filename", groupFile.toStdString());
mappingAlg->setProperty("GroupCount", m_uiForm.leNoGroups->text().toStdString());
mappingAlg->setProperty("SpectraRange", specRange.toStdString());

QString pyInput =
"import IndirectEnergyConversion as ind\n"
"mapfile = ind.createMappingFile('"+groupFile+"', %1, %2, %3)\n"
"print mapfile\n";
pyInput = pyInput.arg(ngroup);
pyInput = pyInput.arg(nspec);
pyInput = pyInput.arg(m_uiForm.leSpectraMin->text());

QString pyOutput = m_pythonRunner.runPythonCode(pyInput).trimmed();
mappingAlg->execute();

return pyOutput;
return groupFile;
}
else
{
// Catch All and Individual
return groupType;
}
}

/**
Expand Down
18 changes: 0 additions & 18 deletions Code/Mantid/scripts/Inelastic/IndirectEnergyConversion.py
Expand Up @@ -32,24 +32,6 @@ def loadData(rawfiles, outWS='RawFile', Sum=False, SpecMin=-1, SpecMax=-1,
else:
return workspaces

def createMappingFile(groupFile, ngroup, nspec, first):
if ( ngroup == 1 ): return 'All'
if ( nspec == 1 ): return 'Individual'
filename = config['defaultsave.directory']
filename = os.path.join(filename, groupFile)
handle = open(filename, 'w')
handle.write(str(ngroup) + "\n" )
for n in range(0, ngroup):
n1 = n * nspec + first
handle.write(str(n+1) + '\n' )
handle.write(str(nspec) + '\n')
for i in range(1, nspec+1):
n3 = n1 + i - 1
handle.write(str(n3).center(4) + ' ')
handle.write('\n')
handle.close()
return filename

def resolution(files, iconOpt, rebinParam, bground,
instrument, analyser, reflection,
Res=True, factor=None, Plot=False, Verbose=False, Save=False):
Expand Down

0 comments on commit 24a5369

Please sign in to comment.