diff --git a/Code/Mantid/Framework/PythonAPI/PythonAlgorithms/ConjoinGSASFiles.py b/Code/Mantid/Framework/PythonAPI/PythonAlgorithms/ConjoinGSASFiles.py index b631dc4e77f4be81062cefc2b53dd29cceafa5f0..ab30682d74d168ecb63776aa3647fe1a4a0b82da 100644 --- a/Code/Mantid/Framework/PythonAPI/PythonAlgorithms/ConjoinGSASFiles.py +++ b/Code/Mantid/Framework/PythonAPI/PythonAlgorithms/ConjoinGSASFiles.py @@ -71,6 +71,36 @@ class ConjoinGSASFiles(PythonAlgorithm): return gsasfiledict + def __loadGSASFile(self, filename, workspacename, loader): + """ Load GSAS file + + Arguments: + - filename + - loader + """ + # 1. Construct file name + if not os.path.exists(filename): + self.log().information("File %s does not exist" % (filename)) + return + else: + self.log().information("Load File %s" % (filename)) + # END-IF + + # 2. Load file + try: + self.log().information("Trying to load '%s' to Workspace" % filename) + loader(filename, workspacename) + except Exception, e: + self.log().error("Failed to load run %s" % str(run)) + + # 3. Load title + gfile = open(filename, "r") + title = gfile.readline() + gfile.close() + + return title + + def __load(self, directory, instr, run, loader, exts): """ Load GSAS file by run number and etc information @@ -156,58 +186,114 @@ class ConjoinGSASFiles(PythonAlgorithm): def PyInit(self): """ Declare properties """ + instruments=["POWGEN", "NOMAD", "VULCAN"] + self.declareProperty("Instrument", "POWGEN", Validator=ListValidator(instruments), + Description="Powder diffractometer's name") self.declareListProperty("RunNumbers",[0], Validator=ArrayBoundedValidator(Lower=0)) self.declareFileProperty("OutputFile","", FileAction.Save, ['.gsa'], Description="The file conjoining all input GSAS files") + self.declareProperty("Workspace", True, + Description="GSAS file will be made from existing Workspaces") self.declareFileProperty("Directory", "", FileAction.OptionalDirectory) + self.declareProperty("ForPDFgetN", True, + Description="Output file is for PDFgetN") + self.declareProperty("MonitorValue", 2.0, + Description="Monitor value for PDFgetN. Default = 2.0") return - # def TestExec(self): - # """ Main exec - # """ - # # generic stuff for running - # # wksp = self.getPropertyValue("OutputWorkspace") - # runs = [4866, 4871] - # instr = mtd.getSettings().facility().instrument().shortName() - # outputfilename = "glued" - # directory = "/home/wzz/Projects/Mantid-Project/Tests/Instrument" - - # # change here if you want something other than gsas files - # exts = ['.gsa'] - # loader = LoadGSS - - # # load things and conjoin them - # filedicts = [] - # for run in runs: - # run = str(run) - # wksp, gsasfiledict = self.__load(directory, instr, run, loader, exts) - # filedicts.append(gsasfiledict) - - # self._writeConjoinedGSASFile(filedicts, directory, outputfilename) def PyExec(self): """ Main exec """ - # generic stuff for running - # wksp = self.getPropertyValue("OutputWorkspace") + # 1. Get property + instrument = self.getProperty("Instrument") runs = self.getProperty("RunNumbers") - instr = mtd.getSettings().facility().instrument().shortName() + useworkspace = self.getProperty("Workspace") + forpdfgetn = self.getProperty("ForPDFgetN") outputfilename = self.getPropertyValue("OutputFile") directory = self.getPropertyValue("Directory").strip() + monitorvalue = self.getProperty("MonitorValue") + + instrumentheader = "" + if instrument == "NOMAD": + instrumentheader = "NOM" + elif instrument == "POWGEN": + instrumentheader = "PG3" + elif instrument == "VULCAN": + instrumentheader = "VUL" + else: + raise NotImplementedError("Impossible to have instrument name not defined") + + # 2. Load GSAS file if necessary + joinedtitle = "" + if useworkspace is not True: + ext = ".gsa" + for irun in xrange(len(runs)): + # a) File name + filename = "%s_%s%s" % (instrumentheader, str(runs[irun]), ext) + if len(directory) > 0: + filename = os.path.join(directory, filename) + # b) Workspace + wksp = "%s_%s" % (instrumentheader, str(runs[irun])) + # c) Load + loader = LoadGSS + title = self.__loadGSASFile(filename, wksp, loader) + if len(joinedtitle) == 0: + joinedtitle = title + # ENDFOR + # ENDIF + + # 2. Get Workspaces + dataworkspaces = [] + # Using workspaces + for irun in xrange(len(runs)): + wsname = instrumentheader + "_" + str(runs[irun]) + ws = mtd[wsname] + if ws is None: + raise NotImplementedError("Workspace %s for instrument %s run %d is not in data service" % (wsname, instrument, runs[irun])) + dataworkspaces.append(ws) + # ENDFOR + + # 3. Write out + print "Write Workspace %s" % (str(dataworkspaces[0])) + SaveGSS(InputWorkspace=dataworkspaces[0], FileName=outputfilename, SplitFiles="False", + Append=False, Bank=1, Format="SLOG", MultiplyByBinWidth=False) + + for iw in xrange(1, len(dataworkspaces)): + print "Write workspace %s" % (str(dataworkspaces[iw])) + SaveGSS(InputWorkspace=dataworkspaces[iw], FileName=outputfilename, SplitFiles="False", + Append=True, Bank=iw+1, Format="SLOG", MultiplyByBinWidth=False) + # ENDFOR + + # 4. PDFgetN special + if forpdfgetn is True: + # Add monitor + ofile = open(outputfilename, "r") + lines = ofile.readlines() + ofile.close() - # change here if you want something other than gsas files - exts = ['.gsa'] - loader = LoadGSS + monitorterm = "Monitor: %f" % (monitorvalue) + monitorline = "%-80s\n" % (monitorterm) + lines.insert(1, monitorline) - # load things and conjoin them - filedicts = [] - for run in runs: - run = str(run) - wksp, gsasfiledict = self.__load(directory, instr, run, loader, exts) - filedicts.append(gsasfiledict) + # Title + if len(joinedtitle) > 0: + lines.pop(0) + lines.insert(0, joinedtitle) - self._writeConjoinedGSASFile(filedicts, directory, outputfilename) + wbuf = "" + for line in lines: + wbuf += line + + ofile = open(outputfilename, "w") + ofile.write(wbuf) + ofile.close() + + + # self._writeConjoinedGSASFile(filedicts, directory, outputfilename) + # wksp = self.getPropertyValue("OutputWorkspace") + # instr = mtd.getSettings().facility().instrument().shortName() return