Changeset 5367

Show
Ignore:
Timestamp:
27/07/10 11:17:22 (6 weeks ago)
Author:
Michael Whitty
Message:

re #1385 - Indirect interface - adding background removal, now saves files

Location:
trunk/Code
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/Code/Mantid/PythonAPI/scripts/Excitations/IndirectEnergyConversion.py

    r5358 r5367  
    1717                        tmp_ws = outWS + str(i) 
    1818                        LoadRaw(rawfiles[i], tmp_ws) 
    19                         Plus(outWS, tmp_ws, outWS) 
     19                        try: 
     20                                Plus(outWS, tmp_ws, outWS) 
     21                        except: 
     22                                print 'Rawfiles do not match, not suitable for summing.' 
     23                                sys.exit('Rawfiles not suitable for summing.') 
    2024                        mantid.deleteWorkspace(tmp_ws) 
    2125                workspace = mtd.getMatrixWorkspace(outWS) 
     
    168172        return outWS_n 
    169173 
    170 def convert_to_energy(rawfiles, mapfile, first, last, efixed, SumFiles=False, bgremove = [0, 0], tempK=-1, calib='', rebinParam='', cleanUp = True): 
     174def convert_to_energy(rawfiles, mapfile, first, last, efixed, SumFiles=False, bgremove = [0, 0], tempK=-1, calib='', rebinParam='', CleanUp = True, instrument='', savesuffix='', saveFormats = [], savedir=''): 
    171175        ''' 
    172176        This function, when passed the proper arguments, will run through the steps of convert to energy 
     
    177181        or with the createMappingFile function. 
    178182        ''' 
    179         output_workspaces = [] 
     183        output_workspace_names = [] 
     184        runNos = [] 
    180185        workspace, ws_name = loadData(rawfiles, Sum=SumFiles) 
    181186        for i in range(0, len(workspace)): 
     
    183188                MonWS_n = monitorEfficiency() 
    184189                runNo = workspace[i].getRun().getLogData("run_number").value() 
     190                runNos.append(runNo) 
    185191                CropWorkspace(ws_name[i], 'Time', StartWorkspaceIndex= (first - 1), EndWorkspaceIndex=( last - 1)) 
    186192                mantid.deleteWorkspace(ws_name[i]) 
     
    192198                cte = conToEnergy(efixed, outWS_n='EnergyRebinned' + str(i)) 
    193199                if ( rebinParam != ''): 
    194                         rebin = rebinData(rebinParam, inWS_n='EnergyRebinned' + str(i)) 
     200                        rebin = rebinData(rebinParam, inWS_n=cte) 
     201                else: 
     202                        if CleanUp: 
     203                                RenameWorkspace(cte, 'Energy') 
     204                        else: 
     205                                CloneWorkspace(cte, 'Energy') 
    195206                if ( tempK != -1 ): 
    196207                        db = detailedBalance(tempK) 
    197208                scale = scaleAndGroup(mapfile, outWS_n='IconComplete' + str(i) + '_' + runNo) 
    198                 output_workspaces.append(scale) 
    199         return output_workspaces 
     209                output_workspace_names.append(scale) 
     210        if ( saveFormats != [] ): 
     211                saveItems(output_workspace_names, runNos, saveFormats, instrument, savesuffix, directory = savedir) 
    200212 
    201213 
     
    231243        SaveNexusProcessed(outWS_n, savefile, 'Vanadium') 
    232244        return outWS_n 
     245 
     246def saveItems(workspaces, runNos, fileFormats, ins, suffix, directory = ''): 
     247        for i in range(0, len(workspaces)): 
     248                filename = ins + runNos[i] + '_' + suffix 
     249                if directory != '': 
     250                        filename = os.path.join(directory, filename) 
     251                for j in fileFormats: 
     252                        if j == 'spe': 
     253                                SaveSPE(workspaces[i], filename + '.spe') 
     254                        elif j == 'nxs': 
     255                                SaveNexusProcessed(workspaces[i], filename + '.nxs') 
     256                        else: 
     257                                print 'Save: unknown file type.' 
     258                                system.exit('Save: unknown file type.') 
  • trunk/Code/qtiplot/MantidQt/CustomInterfaces/inc/Indirect.h

    r5358 r5367  
    6767          void clearReflectionInfo(); ///< clear various line edit boxes 
    6868          QString createMapFile(const QString& groupType); ///< create the mapping file with which to group results 
    69           QString savePyCode(QString filePrefix); ///< create python code as string to save files 
     69          QString savePyCode(); ///< create python code as string to save files 
    7070          bool isDirty(); ///< state whether interface has had any changes 
    7171          void isDirty(bool state); ///< signify changes to interface 
  • trunk/Code/qtiplot/MantidQt/CustomInterfaces/src/Indirect.cpp

    r5358 r5367  
    101101        QString groupFile = createMapFile(m_uiForm.cbMappingOptions->currentText()); 
    102102        if ( groupFile == "" ) 
    103         { 
    104103                return; 
    105         } 
    106104 
    107105        QString filePrefix = m_uiForm.cbInst->itemData(m_uiForm.cbInst->currentIndex()).toString().toLower(); 
     
    142140 
    143141                if ( m_uiForm.ckUseCalib->isChecked() ) 
    144                 { 
    145                         QString calibFile = m_uiForm.leCalibrationFile->text(); 
    146                         pyInput += "calib = r'"+calibFile+"'\n"; 
    147                 } 
     142                        pyInput += "calib = r'"+m_uiForm.leCalibrationFile->text()+"'\n"; 
    148143                else 
    149                 { 
    150144                        pyInput += "calib = ''\n"; 
    151                 } 
    152145 
    153146                pyInput += "efixed = "+m_uiForm.leEfixed->text()+"\n"; 
     
    169162 
    170163                if ( m_uiForm.ckDetailedBalance->isChecked() ) 
    171                 { 
    172164                        pyInput += "tempK = "+m_uiForm.leDetailedBalance->text()+"\n"; 
    173                 } 
    174165                else 
    175                 { 
    176166                        pyInput += "tempK = -1\n"; 
    177                 } 
    178167 
    179168                pyInput += "mapfile = r'"+groupFile+"'\n"; 
    180169 
    181  
    182                 pyInput += "icon_workspaces = ind.convert_to_energy(rawfiles, mapfile, first, last, efixed, SumFiles=Sum, bgremove = bgRemove, tempK = tempK, calib = calib, rebinParam = rebinParam)\n"; 
    183170        } 
    184171 
    185172        if (tryToSave) 
    186173        { 
    187         //      pyInput += "iconWS = scale\n"; 
    188         //      pyInput += savePyCode(filePrefix); 
    189         } 
     174                pyInput += savePyCode(); 
     175        } 
     176        else 
     177        { 
     178                pyInput += 
     179                        "fileFormats = []\n" 
     180                        "ins = ''\n" 
     181                        "directory = ''\n" 
     182                        "suffix = ''\n"; 
     183        } 
     184 
     185        pyInput += "ind.convert_to_energy(rawfiles, mapfile, " 
     186                "first, last, efixed, SumFiles=Sum, bgremove = bgRemove, tempK = tempK, calib = calib, " 
     187                "rebinParam = rebinParam, instrument = ins, savesuffix = suffix, saveFormats = fileFormats," 
     188                "savedir = directory)\n"; 
    190189 
    191190        QString pyOutput = runPythonCode(pyInput).trimmed(); 
     
    195194                showInformationBox("The following error occurred:\n" + pyOutput 
    196195                        + "\n\nAnalysis did not complete."); 
     196                return; 
    197197        } 
    198198 
     
    464464 
    465465/** 
    466 * This function creates the Python script necessary to save the workspace data 
    467 * in the formats requested. 
     466* This function creates the Python script necessary to set the variables used for saving data 
     467* in the main convert_to_energy script. 
    468468* @return python code as a string 
    469469*/ 
    470 QString Indirect::savePyCode(QString filePrefix) 
    471 { 
    472         QString pyInput = "\n# Save Files\n"; 
    473         if ( m_uiForm.leNameSPE->text() != "" ) 
    474         { 
    475                 pyInput += "savefile = r'" +m_uiForm.leNameSPE->text()+ "'\n"; 
    476         } 
    477         else { 
    478                 pyInput += 
    479                         "savefile = mtd.getConfigProperty('defaultsave.directory')\n" 
    480                         "savefile += '" +filePrefix+ "'\n"; 
    481                 if ( m_uiForm.cbAnalyser->currentText() == "graphite" ) 
    482                 { 
    483                         pyInput += "savefile += 'ipg'\n"; 
    484                 } 
    485                 else if ( m_uiForm.cbAnalyser->currentText() == "mica" || m_uiForm.cbAnalyser->currentText() == "fmica" ) 
    486                 { 
    487                         pyInput += "savefile += 'imi'\n"; 
    488                 } 
    489         } 
     470QString Indirect::savePyCode() 
     471{ 
     472        QString analyser = m_uiForm.cbAnalyser->currentText(); 
     473 
     474        QString ins = m_uiForm.cbInst->itemData(m_uiForm.cbInst->currentIndex()).toString().toLower(); 
     475        QString suffix = analyser + m_uiForm.cbReflection->currentText() + "_"; 
     476        QString directory = m_uiForm.leNameSPE->text(); 
     477 
     478        if ( analyser == "graphite" ) 
     479                suffix += "ipg"; 
     480        else if ( analyser == "mica" || analyser == "fmica" ) 
     481                suffix += "imi"; 
     482 
     483        QStringList fileFormats; 
     484        QString fileFormatList; 
    490485 
    491486        if ( m_uiForm.save_ckNexus->isChecked() ) 
    492         { 
    493                 pyInput += "SaveNexusProcessed(iconWS, savefile + '.nxs')\n"; 
    494         } 
     487                fileFormats << "nxs"; 
    495488        if ( m_uiForm.save_ckSPE->isChecked() ) 
    496         { 
    497                 pyInput += "SaveSPE(iconWS, savefile + '.spe')\n"; 
    498         } 
     489                fileFormats << "spe"; 
     490 
     491        if ( fileFormats.size() != 0 ) 
     492                fileFormatList = "[ '" + fileFormats.join("', '") + "']"; 
     493        else 
     494                fileFormatList = "[]"; 
     495 
     496        QString pyInput = 
     497                "# Save File Parameters\n" 
     498                "ins = '" + ins + "'\n" 
     499                "suffix = '" + suffix + "'\n" 
     500                "fileFormats = " + fileFormatList + "\n" 
     501                "directory = r'" + directory + "'\n"; 
    499502 
    500503        return pyInput; 
     
    695698void Indirect::browseSave() 
    696699{ 
    697         QString Analyser = m_uiForm.cbAnalyser->currentText(); 
    698         QString filePrefix = m_uiForm.cbInst->itemData(m_uiForm.cbInst->currentIndex()).toString().toLower(); 
    699         filePrefix += "_" + Analyser + m_uiForm.cbReflection->currentText() + "_"; 
    700  
    701         QString defSave = m_saveDir + filePrefix; 
    702         if ( Analyser == "graphite" ) 
    703                 defSave += "ipg"; 
    704         else 
    705                 defSave += "imi"; 
    706         QString savFile = QFileDialog::getSaveFileName(this, "Save File Name", 
    707                 defSave, "File Stem (*)"); 
    708  
    709         if ( savFile != "" ) 
    710         { 
    711                 m_uiForm.leNameSPE->setText(savFile); 
     700        QString savDir = QFileDialog::getExistingDirectory(this, "Save Directory", 
     701                m_saveDir, QFileDialog::ShowDirsOnly); 
     702 
     703        if ( savDir != "" ) 
     704        { 
     705                m_uiForm.leNameSPE->setText(savDir); 
    712706                isDirty(true);  
    713707        }