Changeset 5397

Show
Ignore:
Timestamp:
29/07/10 16:46:34 (6 weeks ago)
Author:
Michael Whitty
Message:

re #1385 - adding demon, slice, elwin, res (from modes) to indirectenergyconversion script file, minor changes to other scripts for generalisation/reuse. adding options for res to 'calibration' tab of UI, and having interface use these when calibration is run.

Location:
trunk/Code
Files:
4 modified

Legend:

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

    r5367 r5397  
    33 
    44import ConvertToEnergy 
    5 import CommonFunctions as common 
    65from mantidsimple import * 
    7  
     6from mantidplot import * 
    87 
    98def loadData(rawfiles, outWS='RawFile', Sum=False): 
     9        ( dir, file ) = os.path.split(rawfiles[0]) 
     10        ( name, ext ) = os.path.splitext(file) 
    1011        try: 
    11                 LoadRaw(rawfiles[0], outWS) 
     12                LoadRaw(rawfiles[0], name) 
    1213        except ValueError, message: 
    1314                print message 
     
    1819                        LoadRaw(rawfiles[i], tmp_ws) 
    1920                        try: 
    20                                 Plus(outWS, tmp_ws, outWS) 
     21                                Plus(name, tmp_ws, name) 
    2122                        except: 
    2223                                print 'Rawfiles do not match, not suitable for summing.' 
    2324                                sys.exit('Rawfiles not suitable for summing.') 
    2425                        mantid.deleteWorkspace(tmp_ws) 
    25                 workspace = mtd.getMatrixWorkspace(outWS) 
    26                 return [workspace], [outWS] 
     26                workspace = mtd.getMatrixWorkspace(name) 
     27                return [workspace], [name] 
    2728        else: 
    2829                workspace_list = [] 
    29                 ws_name_list = [outWS] 
     30                ws_name_list = [name] 
    3031                if ( len(rawfiles) > 1 ): 
    3132                        for i in range(1, len(rawfiles)): 
    32                                 ws_name = outWS + str(i) 
    33                                 LoadRaw(rawfiles[i], ws_name) 
    34                                 ws_name_list.append(ws_name) 
     33                                ( dir, file ) = os.path.split(rawfiles[i]) 
     34                                ( name, ext ) = os.path.splitext(file) 
     35                                LoadRaw(rawfiles[i], name) 
     36                                ws_name_list.append(name) 
    3537                for i in ws_name_list: 
    3638                        workspace_list.append(mtd.getMatrixWorkspace(i)) 
     
    135137        Divide(outWS_n,monWS_n,outWS_n) 
    136138        mantid.deleteWorkspace(monWS_n) 
    137         mantid.deleteWorkspace(inWS_n) 
     139        if (inWS_n != outWS_n): 
     140                mantid.deleteWorkspace(inWS_n) 
    138141        return outWS_n 
    139142 
     
    210213        if ( saveFormats != [] ): 
    211214                saveItems(output_workspace_names, runNos, saveFormats, instrument, savesuffix, directory = savedir) 
     215        return output_workspace_names, runNos 
    212216 
    213217 
     
    232236                LoadRaw(rawfile, 'Raw', SpectrumMin = specMin, SpectrumMax = specMax) 
    233237        except: 
    234                 sys.exit('Could not load raw file.') 
     238                sys.exit('Calib: Could not load raw file.') 
    235239        tmp = mantid.getMatrixWorkspace('Raw') 
    236240        nhist = tmp.getNumberHistograms() - 1 
     
    243247        SaveNexusProcessed(outWS_n, savefile, 'Vanadium') 
    244248        return outWS_n 
     249 
     250def res(file, nspec, iconOpt, rebinParam, background): 
     251        ''' ? ''' 
     252        (direct, filename) = os.path.split(file) 
     253        (root, ext) = os.path.splitext(filename) 
     254        mapping = createMappingFile('res.map', 1, nspec, iconOpt['first']) 
     255        rawfiles = [file] 
     256        workspace_list, runNos = convert_to_energy(rawfiles, mapping, iconOpt['first'], iconOpt['last'], iconOpt['efixed']) 
     257        iconWS = workspace_list[0] 
     258        Rebin(iconWS, iconWS, rebinParam) 
     259        FFTSmooth(iconWS,iconWS,0) 
     260        name = root[:3] + mantid.getMatrixWorkspace(workspace_list[0]).getRun().getLogData("run_number").value() + '_res' 
     261        FlatBackground(iconWS, name, background[0], background[1]) 
     262        mantid.deleteWorkspace(iconWS) 
     263        SaveNexusProcessed(name, name+'.nxs') 
     264        return name 
    245265 
    246266def saveItems(workspaces, runNos, fileFormats, ins, suffix, directory = ''): 
     
    257277                                print 'Save: unknown file type.' 
    258278                                system.exit('Save: unknown file type.') 
     279 
     280def demon(rawFiles, calFile, first, last, SumFiles=False, CleanUp=True, plotOpt=False): 
     281        ''' 
     282        DEMON function for unit conversion on diffraction backs of IRIS/OSIRIS. 
     283        MANDATORY PARAMS: 
     284        @param rawFiles list of files to load (list[string]) 
     285        @param calFile CalFile/Grouping file (string) 
     286        @param first first spectra number of diffraction block (integer) 
     287        @param last last spectra number of diffraction block (integer) 
     288        OPTIONAL PARAMS: 
     289        @param SumFiles whether to sum input files, or run through them sequentially (boolean) 
     290        @param CleanUp whether to remove intermediate workspaces from memory (boolean) 
     291        @param plotOpt whether to plot the spectra of the result (boolean) 
     292                -- WARNING - with large numbers of spectra and/or lots of input files plotOpt will slow down the script significantly 
     293        ''' 
     294        ws_list, ws_names = loadData(rawFiles, Sum=SumFiles) 
     295        runNos = [] 
     296        workspaces = [] 
     297        for i in range(0, len(ws_names)): 
     298                # Get Monitor WS 
     299                MonitorWS = timeRegime(ws_list[i], inWS_n=ws_names[i]) 
     300                monitorEfficiency(inWS_n=MonitorWS) 
     301                # Get Run no, crop file 
     302                runNo = ws_list[i].getRun().getLogData("run_number").value() 
     303                runNos.append(runNo) 
     304                savefile = rawFiles[0][:3] + runNo + '_dem' 
     305                CropWorkspace(ws_names[i], ws_names[i], StartWorkspaceIndex = (first-1), EndWorkspaceIndex = (last-1) ) 
     306                # Normalise to Monitor 
     307                normalised = normToMon(inWS_n=ws_names[i], outWS_n=ws_names[i], monWS_n=MonitorWS) 
     308                # Convert to dSpacing 
     309                ConvertUnits(ws_names[i], savefile, 'dSpacing') 
     310                # DiffractionFocussing(ws_names[i], savefile, calFile) -- not needed? 
     311                workspaces.append(savefile) 
     312                if CleanUp: 
     313                        mantid.deleteWorkspace(ws_names[i]) 
     314                SaveNexusProcessed(savefile, savefile+'.nxs') 
     315        if plotOpt: 
     316                for demon in workspaces: 
     317                        nspec = mantid.getMatrixWorkspace(demon).getNumberHistograms() 
     318                        plotSpectrum(demon, range(0, nspec)) 
     319        return workspaces, runNos 
     320 
     321def elwin(inputFiles, eRange, iconOpt = {}): 
     322        outWS_list = [] 
     323        for file in inputFiles: 
     324                (direct, filename) = os.path.split(file) 
     325                (root, ext) = os.path.splitext(filename) 
     326                if ext == '.nxs': 
     327                        LoadNexus(file, root) 
     328                        savefile = root[:3] + mantid.getMatrixWorkspace(root).getRun().getLogData("run_number").value() + '_elw' 
     329                        nhist = mantid.getMatrixWorkspace(root).getNumberHistograms() 
     330                        Integration(root, savefile, eRange[0], eRange[1], 0, nhist-1) 
     331                        SaveNexusProcessed(savefile, savefile+'.nxs') 
     332                        outWS_list.append(savefile) 
     333                        mantid.deleteWorkspace(root) 
     334                elif ext == '.raw': 
     335                        if ( len(iconOpt) != 8 ): 
     336                                message = 'Elwin: Number of values for iconOpt parameter do not match expectation. Please view function definition for details.' 
     337                                print message 
     338                                sys.exit(message) 
     339                        inWS_l, runNos = convert_to_energy([file], iconOpt['map'], iconOpt['first'], iconOpt['last'], iconOpt['efixed'], bgremove = iconOpt['bgremove'], tempK=iconOpt['tempK'], calib=iconOpt['calib'], rebinParam=iconOpt['rebin']) 
     340                        inWS = inWS_l[0] 
     341                        savefile = file[:3] + runNos[0] + '_elw' 
     342                        nhist = mantid.getMatrixWorkspace(inWS).getNumberHistograms() 
     343                        Integration(inWS, savefile, eRange[0], eRange[1], 0, nhist-1) 
     344                        SaveNexusProcessed(savefile, savefile+'.nxs') 
     345                        outWS_list.append(savefile) 
     346                        mantid.deleteWorkspace(inWS) 
     347                else: 
     348                        print 'Unrecognised file type.' 
     349                        sys.exit('Elwin: unrecognised input file type (' +ext+ ')') 
     350        return outWS_list 
     351 
     352def slice(inputfiles, enXRange = [], tofXRange = [], inWS_n='Energy', outWS_n='Time', spectra = [0,0]): 
     353        ''' 
     354        This function does the "Slice" part of modes. To be passed in a list of input files (either .raw or .nxs), 
     355        and a (2) 4-element list of X-values to integrate over for the files passed in, one for Energy and one for TOF. 
     356        If your files only deal with one of these, you can omit the other. 
     357        ''' 
     358        if ( enXRange == [] and tofXRange == [] ): 
     359                message = 'Slice: no x-ranges for integration were provided.' 
     360                print message 
     361                sys.exit(message) 
     362        for file in inputfiles: 
     363                (direct, filename) = os.path.split(file) 
     364                (root, ext) = os.path.splitext(filename) 
     365                if ext == '.nxs': 
     366                        if (enXRange == []): 
     367                                message = 'Slice: values for integration over energy have not been supplied.' 
     368                                print message 
     369                                sys.exit(message) 
     370                        root = root[:-3] 
     371                        LoadNexus(file, root) 
     372                        nhist = mantid.getMatrixWorkspace(root).getNumberHistograms() 
     373                        savefile = root[:3] + mantid.getMatrixWorkspace(root).getRun().getLogData("run_number").value() + '_sle' 
     374                        Integration(root, 'Unit1', enXRange[0], enXRange[1], 0, nhist-1) 
     375                        Integration(root, 'Unit2', enXRange[2], enXRange[3], 0, nhist-1) 
     376                        Minus('Unit1', 'Unit2', savefile) 
     377                        SaveNexusProcessed(savefile, savefile+'.nxs') 
     378                        mantid.deleteWorkspace(root) 
     379                elif ext == '.raw': 
     380                        if (tofXRange == []): 
     381                                message = 'Slice: values for integration over time of flight have not been supplied.' 
     382                                print message 
     383                                sys.exit(message) 
     384                        unit = 'Time' 
     385                        if spectra == [0, 0]: 
     386                                LoadRaw(file, root) 
     387                        else: 
     388                                LoadRaw(file, root, SpectrumMin = spectra[0], SpectrumMax = spectra[1]) 
     389                        nhist = mantid.getMatrixWorkspace(root).getNumberHistograms() 
     390                        savefile = root[:3] + mantid.getMatrixWorkspace(root).getRun().getLogData("run_number").value() + '_slt' 
     391                        Integration(root, 'Unit1', tofXRange[0], tofXRange[1], 0, nhist-1) 
     392                        Integration(root, 'Unit2', tofXRange[2], tofXRange[3], 0, nhist-1) 
     393                        Minus('Unit1', 'Unit2', savefile) 
     394                        SaveNexusProcessed(savefile, savefile+'.nxs') 
     395                        mantid.deleteWorkspace(root) 
     396                else: 
     397                        message = 'Slice: Unrecognised file extension ('+ext+')' 
     398                        print message 
     399                        sys.exit(message) 
     400        mantid.deleteWorkspace('Unit1') 
     401        mantid.deleteWorkspace('Unit2') 
     402 
     403def fury(sample, resolution): 
     404        ''' S(Q,w) to I(Q,t) via FFT (FastFourierTrans) ''' 
     405        LoadNexus(sample, 'sample') 
     406        LoadNexus(resolution, 'resolution') 
  • trunk/Code/qtiplot/MantidQt/CustomInterfaces/inc/ConvertToEnergy.ui

    r5335 r5397  
    15781578       </item> 
    15791579       <item> 
     1580        <widget class="QGroupBox" name="groupBox_2" > 
     1581         <property name="title" > 
     1582          <string>RES Options</string> 
     1583         </property> 
     1584         <layout class="QVBoxLayout" name="verticalLayout_22" > 
     1585          <item> 
     1586           <layout class="QHBoxLayout" name="horizontalLayout_28" > 
     1587            <item> 
     1588             <widget class="QLabel" name="cal_lbRebin" > 
     1589              <property name="text" > 
     1590               <string>Rebinning</string> 
     1591              </property> 
     1592             </widget> 
     1593            </item> 
     1594            <item> 
     1595             <spacer name="horizontalSpacer_34" > 
     1596              <property name="orientation" > 
     1597               <enum>Qt::Horizontal</enum> 
     1598              </property> 
     1599              <property name="sizeHint" stdset="0" > 
     1600               <size> 
     1601                <width>40</width> 
     1602                <height>20</height> 
     1603               </size> 
     1604              </property> 
     1605             </spacer> 
     1606            </item> 
     1607            <item> 
     1608             <widget class="QLabel" name="cal_lbElow" > 
     1609              <property name="text" > 
     1610               <string>ELow</string> 
     1611              </property> 
     1612             </widget> 
     1613            </item> 
     1614            <item> 
     1615             <widget class="QLineEdit" name="cal_leELow" > 
     1616              <property name="maximumSize" > 
     1617               <size> 
     1618                <width>75</width> 
     1619                <height>16777215</height> 
     1620               </size> 
     1621              </property> 
     1622             </widget> 
     1623            </item> 
     1624            <item> 
     1625             <widget class="QLabel" name="cal_lbEWidth" > 
     1626              <property name="text" > 
     1627               <string>EWidth</string> 
     1628              </property> 
     1629             </widget> 
     1630            </item> 
     1631            <item> 
     1632             <widget class="QLineEdit" name="cal_leEWidth" > 
     1633              <property name="maximumSize" > 
     1634               <size> 
     1635                <width>75</width> 
     1636                <height>16777215</height> 
     1637               </size> 
     1638              </property> 
     1639             </widget> 
     1640            </item> 
     1641            <item> 
     1642             <widget class="QLabel" name="cal_lbEHigh" > 
     1643              <property name="text" > 
     1644               <string>EHigh</string> 
     1645              </property> 
     1646             </widget> 
     1647            </item> 
     1648            <item> 
     1649             <widget class="QLineEdit" name="cal_leEHigh" > 
     1650              <property name="maximumSize" > 
     1651               <size> 
     1652                <width>75</width> 
     1653                <height>16777215</height> 
     1654               </size> 
     1655              </property> 
     1656             </widget> 
     1657            </item> 
     1658            <item> 
     1659             <spacer name="horizontalSpacer_35" > 
     1660              <property name="orientation" > 
     1661               <enum>Qt::Horizontal</enum> 
     1662              </property> 
     1663              <property name="sizeHint" stdset="0" > 
     1664               <size> 
     1665                <width>40</width> 
     1666                <height>20</height> 
     1667               </size> 
     1668              </property> 
     1669             </spacer> 
     1670            </item> 
     1671           </layout> 
     1672          </item> 
     1673          <item> 
     1674           <layout class="QHBoxLayout" name="horizontalLayout_27" > 
     1675            <item> 
     1676             <widget class="QLabel" name="cal_lbBackgroundSubtraction" > 
     1677              <property name="text" > 
     1678               <string>Background Subtraction</string> 
     1679              </property> 
     1680             </widget> 
     1681            </item> 
     1682            <item> 
     1683             <spacer name="horizontalSpacer_36" > 
     1684              <property name="orientation" > 
     1685               <enum>Qt::Horizontal</enum> 
     1686              </property> 
     1687              <property name="sizeHint" stdset="0" > 
     1688               <size> 
     1689                <width>40</width> 
     1690                <height>20</height> 
     1691               </size> 
     1692              </property> 
     1693             </spacer> 
     1694            </item> 
     1695            <item> 
     1696             <widget class="QLabel" name="cal_lbStartX" > 
     1697              <property name="text" > 
     1698               <string>StartX</string> 
     1699              </property> 
     1700             </widget> 
     1701            </item> 
     1702            <item> 
     1703             <widget class="QLineEdit" name="cal_leStartX" > 
     1704              <property name="sizePolicy" > 
     1705               <sizepolicy vsizetype="Fixed" hsizetype="Expanding" > 
     1706                <horstretch>0</horstretch> 
     1707                <verstretch>0</verstretch> 
     1708               </sizepolicy> 
     1709              </property> 
     1710              <property name="maximumSize" > 
     1711               <size> 
     1712                <width>75</width> 
     1713                <height>16777215</height> 
     1714               </size> 
     1715              </property> 
     1716             </widget> 
     1717            </item> 
     1718            <item> 
     1719             <widget class="QLabel" name="cal_lbEndX" > 
     1720              <property name="text" > 
     1721               <string>EndX</string> 
     1722              </property> 
     1723             </widget> 
     1724            </item> 
     1725            <item> 
     1726             <widget class="QLineEdit" name="cal_leEndX" > 
     1727              <property name="maximumSize" > 
     1728               <size> 
     1729                <width>75</width> 
     1730                <height>16777215</height> 
     1731               </size> 
     1732              </property> 
     1733             </widget> 
     1734            </item> 
     1735            <item> 
     1736             <spacer name="horizontalSpacer_33" > 
     1737              <property name="orientation" > 
     1738               <enum>Qt::Horizontal</enum> 
     1739              </property> 
     1740              <property name="sizeHint" stdset="0" > 
     1741               <size> 
     1742                <width>40</width> 
     1743                <height>20</height> 
     1744               </size> 
     1745              </property> 
     1746             </spacer> 
     1747            </item> 
     1748           </layout> 
     1749          </item> 
     1750         </layout> 
     1751        </widget> 
     1752       </item> 
     1753       <item> 
    15801754        <widget class="QGroupBox" name="cal_gbButtons" > 
    15811755         <property name="title" > 
  • trunk/Code/qtiplot/MantidQt/CustomInterfaces/inc/Indirect.h

    r5367 r5397  
    6868          QString createMapFile(const QString& groupType); ///< create the mapping file with which to group results 
    6969          QString savePyCode(); ///< create python code as string to save files 
     70          void createRESfile(const QString& file); ///< create a RES file for use in Fury 
    7071          bool isDirty(); ///< state whether interface has had any changes 
    7172          void isDirty(bool state); ///< signify changes to interface 
  • trunk/Code/qtiplot/MantidQt/CustomInterfaces/src/Indirect.cpp

    r5367 r5397  
    504504} 
    505505/** 
     506* This function is called after calib has run and creates a RES file for use in later analysis (Fury,etc) 
     507* @param file the input file (WBV run.raw) 
     508*/ 
     509void Indirect::createRESfile(const QString& file) 
     510{ 
     511        // 
     512        QString pyInput = 
     513                "import IndirectEnergyConversion as ind\n" 
     514                "iconOpt = { 'first': " +m_uiForm.leSpectraMin->text()+ 
     515                ", 'last': " +m_uiForm.leSpectraMax->text()+ 
     516                ", 'efixed': " +m_uiForm.leEfixed->text()+ "}\n"; 
     517        QString rebinParam = m_uiForm.cal_leELow->text() + "," + 
     518                m_uiForm.cal_leEWidth->text() + "," + 
     519                m_uiForm.cal_leEHigh->text(); 
     520        QString background = "[ " +m_uiForm.cal_leStartX->text()+ ", " +m_uiForm.cal_leEndX->text()+"]"; 
     521 
     522        pyInput += 
     523                "nspec = iconOpt['last'] - iconOpt['first'] + 1\n" 
     524                "background = " + background + "\n" 
     525                "rebinParam = '" + rebinParam + "'\n" 
     526                "file = r'" + file + "'\n" 
     527 
     528                "outWS = ind.res(file, nspec, iconOpt, rebinParam, background)\n"; 
     529 
     530        QString pyOutput = runPythonCode(pyInput).trimmed(); 
     531 
     532        if ( pyOutput != "" ) 
     533                showInformationBox("Unable to create RES file."); 
     534} 
     535/** 
    506536* Used to check whether any changes have been made by the user to the interface. 
    507537* @return boolean m_isDirty 
     
    907937                return; 
    908938        } 
     939        else 
     940        { 
     941                createRESfile(input_path); 
     942        } 
    909943 
    910944        m_uiForm.leCalibrationFile->setText(output_path);