Skip to content

Commit

Permalink
Re #6151 allow for saving output with APIv2
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Dec 11, 2012
1 parent 48bd0b1 commit 5dd056d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mantid.api import *
from mantid.kernel import *
from reduction_workflow.find_data import find_data
import os

class HFIRSANSReduction(PythonAlgorithm):

Expand Down Expand Up @@ -227,6 +228,7 @@ def PyExec(self):
output_msg += alg.getProperty("OutputMessage").value+'\n'

# Compute I(q)
iq_output = None
if "IQAlgorithm" in property_list:
iq_output = self.getPropertyValue("OutputWorkspace")
iq_output = iq_output+'_Iq'
Expand All @@ -240,9 +242,17 @@ def PyExec(self):
output_msg += alg.getProperty("OutputMessage").value

# Compute I(qx,qy)

# Save data

iqxy_output = None

# Verify output directory and save data
if "OutputDirectory" in property_list:
output_dir = property_manager.getProperty("OutputDirectory").value
if os.path.isdir(output_dir):
output_msg += self._save_output(iq_output, iqxy_output,
output_dir, property_manager)
else:
msg = "Output directory doesn't exist: %s\n" % output_dir
Logger.get("HFIRSANSReduction").error(msg)

self.setPropertyValue("OutputWorkspace", output_ws)
self.setProperty("OutputMessage", output_msg)
Expand Down Expand Up @@ -326,8 +336,50 @@ def process_data_file(self, workspace):
output_msg += alg.getProperty("OutputMessage").value+'\n'

return output_msg

def _save_output(self, iq_output, iqxy_output, output_dir, property_manager):
"""
Save the I(Q) and I(QxQy) output to file.
@param iq_output: name of the I(Q) workspace
@param iqxy_output: name of the I(QxQy) workspace
@param output_dir: output director path
@param property_manager: property manager object
"""
output_msg = ""
# Save I(Q)
if iq_output is not None:
if AnalysisDataService.doesExist(iq_output):
proc_xml = ""
if property_manager.existsProperty("ProcessInfo"):
process_file = property_manager.getProperty("ProcessInfo").value
if os.path.isfile(process_file):
proc = open(process_file, 'r')
proc_xml = proc.read()
elif len(process_file)>0:
Logger.get("HFIRSANSReduction").error("Could not read %s\n" % process_file)

filename = os.path.join(output_dir, iq_output+'.txt')
api.SaveAscii(Filename=filename, InputWorkspace=iq_output,
Separator="Tab", CommentIndicator="# ",
WriteXError=True)
filename = os.path.join(output_dir, iq_output+'.xml')
api.SaveCanSAS1D(Filename=filename, InputWorkspace=iq_output,
Process=proc_xml)
output_msg += "I(Q) saved in %s\n" % (filename)
else:
Logger.get("HFIRSANSReduction").error("No I(Q) output found")

# Save I(Qx,Qy)
if iqxy_output is not None:
if AnalysisDataService.doesExist(iqxy_output):
filename = os.path.join(output_dir, iqxy_output+'.dat')
api.SaveNISTDAT(InputWorkspace=iqxy_output, Filename=filename)
output_msg += "I(Qx,Qy) saved in %s\n" % (filename)
else:
Logger.get("HFIRSANSReduction").error("No I(Qx,Qy) output found")

return output_msg
#############################################################################################

registerAlgorithm(HFIRSANSReduction)
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ void SetupHFIRReduction::init()
setPropertyGroup("NumberOfSubpixels", iq1d_grp);
setPropertyGroup("ErrorWeighting", iq1d_grp);

declareProperty("ProcessInfo","", "Additional process information");
declareProperty("OutputDirectory", "", "Directory to put the output files in");
declareProperty("OutputMessage","",Direction::Output);
declareProperty("ReductionProperties","__sans_reduction_properties", Direction::Input);
}
Expand All @@ -445,7 +447,15 @@ void SetupHFIRReduction::exec()
PropertyManagerDataService::Instance().addOrReplace(reductionManagerName, reductionManager);

// Store name of the instrument
reductionManager->declareProperty(new PropertyWithValue<std::string>("InstrumentName", "HFIRSANS") );
reductionManager->declareProperty(new PropertyWithValue<std::string>("InstrumentName", "HFIRSANS"));

// Store additional (and optional) process information
const std::string processInfo = getProperty("ProcessInfo");
reductionManager->declareProperty(new PropertyWithValue<std::string>("ProcessInfo", processInfo));

// Store the output directory
const std::string outputDirectory = getProperty("OutputDirectory");
reductionManager->declareProperty(new PropertyWithValue<std::string>("OutputDirectory", outputDirectory));

// Load algorithm
const double sdd = getProperty("SampleDetectorDistance");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,13 @@ def ResetWavelength():
ReductionSingleton().reduction_properties["Wavelength"] = None
ReductionSingleton().reduction_properties["WavelengthSpread"] = None

def SaveIqAscii(reducer=None, process=None):
#TODO
if reducer is None:
reducer = ReductionSingleton()
reducer.set_save_Iq(sans_reduction_steps.SaveIqAscii(process=process))
def SaveIq(output_dir='', process=''):
ReductionSingleton().reduction_properties["OutputDirectory"] = output_dir
ReductionSingleton().reduction_properties["ProcessInfo"] = process

def NoSaveIq():
#TODO
ReductionSingleton().set_save_Iq(None)
if ReductionSingleton().reduction_properties.has_key("OutputDirectory"):
del ReductionSingleton().reduction_properties["OutputDirectory"]

def IQxQy(nbins=100):
ReductionSingleton().set_IQxQy(mantidsimple.EQSANSQ2D, InputWorkspace=None,
Expand Down

0 comments on commit 5dd056d

Please sign in to comment.