def test_DNSVanadiumCorrection_Masked(self): outputWorkspaceName = "DNSComputeDetCorrCoefsTest_Test3" vanalist = [self.sfvanaws.name(), self.nsfvanaws.name()] bglist = [self.sfbkgrws.name(), self.nsfbkgrws.name()] MaskDetectors(self.sfvanaws, DetectorList=[1]) MaskDetectors(self.nsfvanaws, DetectorList=[1]) alg_test = run_algorithm("DNSComputeDetEffCorrCoefs", VanadiumWorkspaces=vanalist, BackgroundWorkspaces=bglist, OutputWorkspace=outputWorkspaceName) self.assertTrue(alg_test.isExecuted()) # check whether the data are correct ws = AnalysisDataService.retrieve(outputWorkspaceName) # dimensions self.assertEqual(24, ws.getNumberHistograms()) self.assertEqual(2, ws.getNumDims()) # reference data refdata = np.linspace(1.0, 24, 24) / 13.0 refdata[0] = 0 # detector is masked # data array for i in range(24): self.assertAlmostEqual(refdata[i], ws.readY(i)) run_algorithm("DeleteWorkspace", Workspace=outputWorkspaceName) return
def PyExec(self): input_ws = self.getProperty("InputWorkspace").value eff_ws = self.getProperty("DetectorEfficiencyWorkspace").value transposed = Transpose(InputWorkspace=eff_ws, StoreInADS=False) efficiencies = transposed.extractY().flatten() errors = transposed.extractE().flatten() n_hist = input_ws.getNumberHistograms() if n_hist % efficiencies.size != 0: raise ValueError( 'Number of histograms in input workspace is not a multiple of number of entries in detector efficiency ' 'workspace.') n_time_indexes = n_hist / efficiencies.size to_multiply = CreateWorkspace(DataY=np.repeat(efficiencies, n_time_indexes), DataE=np.repeat(errors, n_time_indexes), DataX=np.zeros(n_hist), NSpec=n_hist, StoreInADS=False) output = Multiply( LHSWorkspace=input_ws, RHSWorkspace=to_multiply, OutputWorkspace=self.getPropertyValue("OutputWorkspace")) # In the output we should mask the detectors where calibration constant is masked det_IDs = '' n_pixels_per_tube = eff_ws.getNumberHistograms() for spectrum in range(n_pixels_per_tube): if eff_ws.hasMaskedBins(spectrum): masked = eff_ws.maskedBinsIndices(spectrum) for bin in masked: det_IDs += str(bin * n_pixels_per_tube + spectrum + 1) + ',' if det_IDs: MaskDetectors(Workspace=output, DetectorList=det_IDs[:-1]) self.setProperty("OutputWorkspace", output)
def _apply_corrections(self, w, target='sample'): """ Apply a series of corrections and normalizations to the input workspace Parameters ---------- w: Mantid.EventsWorkspace Input workspace target: str Specify the entity the workspace refers to. Valid options are 'sample', 'background', and 'vanadium' Returns ------- Mantid.EventsWorkspace """ MaskDetectors(w, MaskedWorkspace=self._t_mask) _t_corr = ModeratorTzeroLinear(w) # delayed emission from moderator _t_corr = self.add_previous_pulse(_t_corr) _t_corr = ConvertUnits(_t_corr, Target='Wavelength', Emode='Elastic') l_s, l_e = self._wavelength_band[0], self._wavelength_band[1] _t_corr = CropWorkspace(_t_corr, XMin=l_s, XMax=l_e) _t_corr = Rebin(_t_corr, Params=[l_s, self._wavelength_dl, l_e], PreserveEvents=False) if self.getProperty('MonitorNormalization').value is True: _t_corr = self._monitor_normalization(_t_corr, target) return _t_corr
def load_file_and_apply(self, filename, ws_name): Load(Filename=filename, OutputWorkspace=ws_name, FilterByTofMin=self.getProperty("FilterByTofMin").value, FilterByTofMax=self.getProperty("FilterByTofMax").value) if self._load_inst: LoadInstrument(Workspace=ws_name, Filename=self.getProperty("LoadInstrument").value, RewriteSpectraMap=False) if self._apply_cal: ApplyCalibration( Workspace=ws_name, CalibrationTable=self.getProperty("ApplyCalibration").value) if self._detcal: LoadIsawDetCal(InputWorkspace=ws_name, Filename=self.getProperty("DetCal").value) if self._copy_params: CopyInstrumentParameters(OutputWorkspace=ws_name, InputWorkspace=self.getProperty( "CopyInstrumentParameters").value) if self._masking: if not mtd.doesExist('__mask'): LoadMask(Instrument=mtd[ws_name].getInstrument().getName(), InputFile=self.getProperty("MaskFile").value, OutputWorkspace='__mask') MaskDetectors(Workspace=ws_name, MaskedWorkspace='__mask') if self.XMin != Property.EMPTY_DBL and self.XMax != Property.EMPTY_DBL: ConvertUnits(InputWorkspace=ws_name, OutputWorkspace=ws_name, Target='Momentum') CropWorkspaceForMDNorm(InputWorkspace=ws_name, OutputWorkspace=ws_name, XMin=self.XMin, XMax=self.XMax)
def _table_to_workspace( input_workspace: Union[str, TableWorkspace], output_workspace: Optional[str] = None) -> MaskWorkspace: r""" @brief Convert a CORELLI calibration mask table to a MaskWorkspace :param input_workspace : the table containing the detector ID's for the masked detectors :param output_workspace : name of the output MaskWorkspace :return: handle to the MaskWorkspace """ table_handle = mtd[str(input_workspace)] detectors_masked = table_handle.column(0) # list of masked detectors if output_workspace is None: output_workspace = str(input_workspace) LoadEmptyInstrument(InstrumentName='CORELLI', OutputWorkspace=output_workspace) ClearMaskFlag(Workspace=output_workspace) # for good measure MaskDetectors( Workspace=output_workspace, DetectorList=detectors_masked) # output_workspace is a Workspace2D # output_workspace is converted to a MaskWorkspace, where the Y-values of the spectra are now either 0 or 1 ExtractMask(InputWorkspace=output_workspace, OutputWorkspace=output_workspace) return mtd[output_workspace]
def _mask_t0_crop(self, run_number, name): """ Load a run into a workspace with: 1. Masked detectors 2. Delayed emission time from moderator removed 3. Conversion of units to momentum 4. Remove events outside the valid momentum range :param run_number: BASIS run number :param name: name for the output workspace :return: workspace object """ ws = self._load_single_run(run_number, name) MaskDetectors(ws, MaskedWorkspace=self._t_mask) ws = ModeratorTzeroLinear(InputWorkspace=ws.name(), Gradient=self._tzero['gradient'], Intercept=self._tzero['intercept'], OutputWorkspace=ws.name()) # Correct old DAS shift of fast neutrons. See GitHub issue 23855 if self._das_version == VDAS.v1900_2018: ws = self.add_previous_pulse(ws) ws = ConvertUnits(ws, Target='Momentum', OutputWorkspace=ws.name()) ws = CropWorkspace(ws, OutputWorkspace=ws.name(), XMin=self._momentum_range[0], XMax=self._momentum_range[1]) return ws
def PyExec(self): """ Main execution body """ inputws = self.getProperty("InputWorkspace").value epptable = self.getProperty("EPPTable").value outws_name = self.getPropertyValue("OutputWorkspace") run = inputws.getRun() tof1 = float(run.getLogData('TOF1').value) wavelength = float(run.getLogData('wavelength').value) velocity = sp.constants.h / (sp.constants.m_n * wavelength * 1e-10 ) # m/s instrument = inputws.getInstrument() sample = instrument.getSample() t_fit = np.array(epptable.column('PeakCentre')) - tof1 outws = CloneWorkspace(inputws, OutputWorkspace=outws_name) # mask detectors with EPP=0 bad_data = np.where(t_fit <= 0)[0] if len(bad_data) > 0: self.log().warning("Detectors " + str(bad_data) + " have EPP=0 and will be masked.") MaskDetectors(outws, DetectorList=bad_data) for idx in range(outws.getNumberHistograms()): # correct TOF only if fit of EPP was succesful if t_fit[idx] > 0: det = instrument.getDetector( outws.getSpectrum(idx).getDetectorIDs()[0]) sdd = det.getDistance(sample) t2_el = sdd * 1.0e+6 / velocity # microseconds newX = inputws.readX(idx) + t2_el - t_fit[idx] outws.setX(idx, newX) self.setProperty("OutputWorkspace", outws)
def processData(self, filename, wsName): if filename != '': if self._SystemTest: Load(Filename=filename, OutputWorkspace=wsName, BankName = 'bank22') else: Load(Filename=filename, OutputWorkspace=wsName) FindDetectorsPar(InputWorkspace=wsName, ReturnLinearRanges=self._returnLinearRanges, ParFile=self._parFile, OutputParTable=self._outputParTable) FilterBadPulses(InputWorkspace=wsName, Outputworkspace=wsName, LowerCutoff=self._lowerCutoff) RemovePromptPulse(InputWorkspace=wsName, OutputWorkspace=wsName, Width=self._width, Frequency=self._frequency) LoadDiffCal(InputWorkspace=wsName, InstrumentName=self._instrumentName, InstrumentFilename=self._instrumentFilename, Filename=self._filename, MakeGroupingWorkspace=self._makeGroupingWorkspace, MakeCalWorkspace=self._makeCalWorkspace, MakeMaskWorkspace=self._makeMaskWorkspace, WorkspaceName=self._workspaceName, TofMin=self._tofMin, TofMax=self._tofMax, FixConversionIssues=self._fixConversionIssues) MaskDetectors(Workspace=wsName, SpectraList=self._spectraList, DetectorList=self._detectorList, WorkspaceIndexList=self._workspaceIndexList, MaskedWorkspace=self._maskedWorkspace, ForceInstrumentMasking=self._forceInstrumentMasking, StartWorkspaceIndex=self._startWorkspaceIndex, EndWorkspaceIndex=self._endWorkspaceIndex, ComponentList=self._componentList) AlignDetectors(InputWorkspace=wsName, OutputWorkspace=wsName, CalibrationFile=self._calibrationFile) ConvertUnits(InputWorkspace=wsName, OutputWorkspace=wsName, Target='Wavelength')
def _applyDiagnostics(self, mainWS): """Mask workspace according to diagnostics.""" if self.getProperty(common.PROP_DIAGNOSTICS_WS).isDefault: return mainWS diagnosticsWS = self.getProperty(common.PROP_DIAGNOSTICS_WS).value MaskDetectors(Workspace=mainWS, MaskedWorkspace=diagnosticsWS, EnableLogging=self._subalgLogging) return mainWS
def nominal_integrated_flux(self, name): """ Generate a flux independent of momentum :param name: Name of the output workspace :return: reference to flux workspace """ ws = LoadNexus(Filename=self._flux_ws_, OutputWorkspace=name) ClearMaskFlag(ws) MaskDetectors(ws, MaskedWorkspace=self._t_mask) return ws
def _maskDiagnosedDetectors(ws, diagnosticsWS, wsNames, algorithmLogging): """Mask detectors according to diagnostics.""" maskedWSName = wsNames.withSuffix('diagnostics_applied') maskedWS = CloneWorkspace(InputWorkspace=ws, OutputWorkspace=maskedWSName, EnableLogging=algorithmLogging) MaskDetectors(Workspace=maskedWS, MaskedWorkspace=diagnosticsWS, EnableLogging=algorithmLogging) return maskedWS
def load_file_and_apply(self, filename, ws_name, offset): Load(Filename=filename, OutputWorkspace=ws_name, FilterByTofMin=self.getProperty("FilterByTofMin").value, FilterByTofMax=self.getProperty("FilterByTofMax").value) if self._load_inst: LoadInstrument(Workspace=ws_name, Filename=self.getProperty("LoadInstrument").value, RewriteSpectraMap=False) if self._apply_cal: ApplyCalibration( Workspace=ws_name, CalibrationTable=self.getProperty("ApplyCalibration").value) if self._detcal: LoadIsawDetCal(InputWorkspace=ws_name, Filename=self.getProperty("DetCal").value) if self._copy_params: CopyInstrumentParameters(OutputWorkspace=ws_name, InputWorkspace=self.getProperty( "CopyInstrumentParameters").value) MaskDetectors(Workspace=ws_name, MaskedWorkspace='__sa') if offset != 0: if self.getProperty('SetGoniometer').value: SetGoniometer( Workspace=ws_name, Goniometers=self.getProperty('Goniometers').value, Axis0='{},0,1,0,1'.format(offset), Axis1=self.getProperty('Axis0').value, Axis2=self.getProperty('Axis1').value, Axis3=self.getProperty('Axis2').value) else: SetGoniometer(Workspace=ws_name, Axis0='{},0,1,0,1'.format(offset), Axis1='omega,0,1,0,1', Axis2='chi,0,0,1,1', Axis3='phi,0,1,0,1') else: if self.getProperty('SetGoniometer').value: SetGoniometer( Workspace=ws_name, Goniometers=self.getProperty('Goniometers').value, Axis0=self.getProperty('Axis0').value, Axis1=self.getProperty('Axis1').value, Axis2=self.getProperty('Axis2').value) ConvertUnits(InputWorkspace=ws_name, OutputWorkspace=ws_name, Target='Momentum') CropWorkspaceForMDNorm(InputWorkspace=ws_name, OutputWorkspace=ws_name, XMin=self.XMin, XMax=self.XMax)
def nominal_solid_angle(self, name): """ Generate an isotropic solid angle :param name: Name of the output workspace :return: reference to solid angle workspace """ ws = LoadNexus(Filename=self._solid_angle_ws_, OutputWorkspace=name) ClearMaskFlag(ws) MaskDetectors(ws, MaskedWorkspace=self._t_mask) for i in range(ws.getNumberHistograms()): ws.dataY(i)[0] = 0.0 if ws.getDetector(i).isMasked() else 1.0 ws.setX(i, self._momentum_range) return ws
def _sensitivity_correction(self, w): """ Divide each pixel by the vanadium count Parameters ---------- w: Events workspace in units of wavelength Returns ------- Mantid.EventWorkspace """ MaskDetectors(w, MaskedWorkspace=self._v_mask) _t_w = Divide(w, self._van, OutputWorkspace=w.name()) return _t_w
def _userMask(self, mainWS, wsNames, wsCleanup, algorithmLogging): """Return combined masked spectra and components.""" userMask = self.getProperty(common.PROP_USER_MASK).value maskComponents = self.getProperty(common.PROP_USER_MASK_COMPONENTS).value maskWSName = wsNames.withSuffix('user_mask') maskWS = _createMaskWS(mainWS, maskWSName, algorithmLogging) MaskDetectors(Workspace=maskWS, DetectorList=userMask, ComponentList=maskComponents, EnableLogging=algorithmLogging) maskWS, detectorLsit = ExtractMask(InputWorkspace=maskWS, OutputWorkspace=maskWSName, EnableLogging=algorithmLogging) return maskWS
def _userMask(self, mainWS): """Return combined masked spectra and components.""" userMask = self.getProperty(common.PROP_USER_MASK).value maskComponents = self.getProperty( common.PROP_USER_MASK_COMPONENTS).value maskWSName = self._names.withSuffix('user_mask') maskWS = _createMaskWS(mainWS, maskWSName, self._subalgLogging) MaskDetectors(Workspace=maskWS, DetectorList=userMask, ComponentList=maskComponents, EnableLogging=self._subalgLogging) extractResult = ExtractMask(InputWorkspace=maskWS, OutputWorkspace=maskWSName, EnableLogging=self._subalgLogging) return extractResult.OutputWorkspace
def _applyDiagnostics(self, mainWS, wsNames, wsCleanup, subalgLogging): """Mask workspace according to diagnostics.""" if self.getProperty(common.PROP_DIAGNOSTICS_WS).isDefault: return mainWS diagnosticsWS = self.getProperty(common.PROP_DIAGNOSTICS_WS).value maskedWSName = wsNames.withSuffix('diagnostics_applied') maskedWS = CloneWorkspace(InputWorkspace=mainWS, OutputWorkspace=maskedWSName, EnableLogging=subalgLogging) wsCleanup.cleanup(mainWS) diagnosticsWS = self.getProperty(common.PROP_DIAGNOSTICS_WS).value MaskDetectors(Workspace=maskedWS, MaskedWorkspace=diagnosticsWS, EnableLogging=subalgLogging) wsCleanup.cleanup(mainWS) return maskedWS
def _apply_corrections(self, w, target='sample'): """ Apply a series of corrections and normalizations to the input workspace Parameters ---------- w: Mantid.EventsWorkspace Input workspace target: str Specify the entity the workspace refers to. Valid options are 'sample', 'background', and 'vanadium' Returns ------- Mantid.EventsWorkspace """ MaskDetectors(w, MaskedWorkspace=self._t_mask) local_name = tws('corr') _t_corr = ModeratorTzeroLinear(w, Gradient=self._tzero['gradient'], Intercept=self._tzero['intercept'], OutputWorkspace=local_name) # Correct old DAS shift of fast neutrons. See GitHub issue 23855 if self._das_version == VDAS.v1900_2018: _t_corr = self.add_previous_pulse(_t_corr) _t_corr = ConvertUnits(_t_corr, Target='Wavelength', Emode='Elastic', OutputWorkspace=local_name) l_s, l_e = self._wavelength_band[0], self._wavelength_band[1] _t_corr = CropWorkspace(_t_corr, XMin=l_s, XMax=l_e, OutputWorkspace=local_name) _t_corr = Rebin(_t_corr, Params=[l_s, self._wavelength_dl, l_e], PreserveEvents=False, OutputWorkspace=local_name) if self.getProperty('DoFluxNormalization').value is True: _t_corr = self._flux_normalization(_t_corr, target) RenameWorkspace(_t_corr, OutputWorkspace=w.name()) return _t_corr
def _load_vanadium_runs(self): """ Initialize the vanadium workspace and the related mask to avoid using pixels with low-counts. """ runs = self.getProperty('VanadiumRuns').value _t_van_name = tws('vanadium') _t_van = self._load_runs(runs, _t_van_name) _t_van = self._apply_corrections(_t_van, target='vanadium') _t_van = Integration(_t_van, RangeLower=self._wavelength_band[0], RangeUpper=self._wavelength_band[1], OutputWorkspace=_t_van_name) _t_v_mask_name = tws('vanadium_mask') output = MedianDetectorTest(_t_van, OutputWorkspace=_t_v_mask_name) self._v_mask = output.OutputWorkspace MaskDetectors(_t_van, MaskedWorkspace=self._v_mask) self._van = _t_van
def test_get_masked_det_ids(self): # Arrange test_workspace_for_masked_det_ids = CreateSampleWorkspace("Histogram") MaskDetectors(Workspace=test_workspace_for_masked_det_ids, DetectorList=[100, 102, 104]) # Act masked_det_ids = list( get_masked_det_ids(test_workspace_for_masked_det_ids)) # Assert self.assertTrue(100 in masked_det_ids) self.assertTrue(102 in masked_det_ids) self.assertTrue(104 in masked_det_ids) self.assertEqual(len(masked_det_ids), 3) # Clean up DeleteWorkspace(test_workspace_for_masked_det_ids)
def _load_vanadium_runs(self): """ Initialize the vanadium workspace and the related mask to avoid using pixels with low-counts. """ runs = self.getProperty('VanadiumRuns').value _t_van = self._load_runs(runs, '_t_van') _t_van = self._apply_corrections(_t_van, target='vanadium') RenameWorkspace(_t_van, OutputWorkspace='_t_van') wave_band = self._wavelength_band[1] - self._wavelength_band[0] _t_van = Rebin(_t_van, Params=[ self._wavelength_band[0], wave_band, self._wavelength_band[1] ]) output = MedianDetectorTest(_t_van, OutputWorkspace='_t_v_mask') self._v_mask = output.OutputWorkspace MaskDetectors(_t_van, MaskedWorkspace=self._v_mask) self._van = _t_van
def loadIntegrateData(filename, OutputWorkspace='__ws', wavelength=1.488): LoadEventNexus(Filename=filename, OutputWorkspace=OutputWorkspace, LoadMonitors=True) Integration(InputWorkspace=OutputWorkspace, OutputWorkspace=OutputWorkspace) MaskDetectors(OutputWorkspace, DetectorList=range(16384)) mtd[OutputWorkspace].getAxis(0).setUnit("Wavelength") w = np.array([wavelength - 0.001, wavelength + 0.001]) for idx in range(mtd[OutputWorkspace].getNumberHistograms()): mtd[OutputWorkspace].setX(idx, w) SetGoniometer(OutputWorkspace, Axis0="HB2C:Mot:s1,0,1,0,1") AddSampleLog(OutputWorkspace, LogName="gd_prtn_chrg", LogType='Number', NumberType='Double', LogText=str(mtd[OutputWorkspace + '_monitors'].getNumberEvents())) return OutputWorkspace
def create_test_ws_and_group(): myFunc = "name=Gaussian, PeakCentre=2, Height=100, Sigma=0.01;" + \ "name=Gaussian, PeakCentre=1, Height=100, Sigma=0.01;" + \ "name=Gaussian, PeakCentre=4, Height=100, Sigma=0.01" ws = CreateSampleWorkspace("Event", "User Defined", myFunc, BankPixelWidth=1, XUnit='dSpacing', XMax=5, BinWidth=0.001, NumEvents=100000, NumBanks=8) for n in range(1, 5): MoveInstrumentComponent(ws, ComponentName=f'bank{n}', X=1 + n / 10, Y=0, Z=1 + n / 10, RelativePosition=False) MoveInstrumentComponent(ws, ComponentName=f'bank{n+4}', X=2 + n / 10, Y=0, Z=2 + n / 10, RelativePosition=False) MaskDetectors(ws, WorkspaceIndexList=[3, 7]) ws = ScaleX(ws, Factor=1.05, IndexMin=1, IndexMax=1) ws = ScaleX(ws, Factor=0.95, IndexMin=2, IndexMax=2) ws = ScaleX(ws, Factor=1.05, IndexMin=4, IndexMax=6) ws = ScaleX(ws, Factor=1.02, IndexMin=5, IndexMax=5) ws = ScaleX(ws, Factor=0.98, IndexMin=6, IndexMax=6) ws = Rebin(ws, '0,0.001,5') ws = ConvertUnits(ws, Target='TOF') groups, _, _ = CreateGroupingWorkspace(InputWorkspace=ws, ComponentName='basic_rect', CustomGroupingString='1-4,5-8') return ws, groups
def _mask_t0_crop(self, run_number, name): """ Load a run into a workspace with: 1. Masked detectors 2. Delayed emission time from moderator removed 3. Conversion of units to momentum 4. Remove events outside the valid momentum range :param run_number: BASIS run number :param name: name for the output workspace :return: workspace object """ ws = LoadEventNexus(Filename=self._makeRunFile(run_number), NXentryName='entry-diff', SingleBankPixelsOnly=False, OutputWorkspace=name) MaskDetectors(ws, MaskedWorkspace=self._t_mask) ws = ModeratorTzeroLinear(InputWorkspace=ws.name(), OutputWorkspace=ws.name()) ws = ConvertUnits(ws, Target='Momentum', OutputWorkspace=ws.name()) ws = CropWorkspace(ws, OutputWorkspace=ws.name(), XMin=self._momentum_range[0], XMax=self._momentum_range[1]) return ws
def PyExec(self): # Retrieve all relevant notice in_Runs = self.getProperty("RunNumbers").value maskWSname = self._getMaskWSname() # either type of file-based calibration is stored in the same variable calib = self.getProperty("Calibration").value if calib == "Calibration File": cal_File = self.getProperty("CalibrationFilename").value elif calib == 'DetCal File': cal_File = self.getProperty('DetCalFilename').value cal_File = ','.join(cal_File) else: cal_File = None params = self.getProperty("Binning").value norm = self.getProperty("Normalization").value if norm == "From Processed Nexus": norm_File = self.getProperty("NormalizationFilename").value LoadNexusProcessed(Filename=norm_File, OutputWorkspace='normWS') normWS = 'normWS' elif norm == "From Workspace": normWS = str(self.getProperty("NormalizationWorkspace").value) else: normWS = None group_to_real = { 'Banks': 'Group', 'Modules': 'bank', '2_4 Grouping': '2_4Grouping' } group = self.getProperty('GroupDetectorsBy').value real_name = group_to_real.get(group, group) if not mtd.doesExist(group): if group == '2_4 Grouping': group = '2_4_Grouping' CreateGroupingWorkspace(InstrumentName='SNAP', GroupDetectorsBy=real_name, OutputWorkspace=group) Process_Mode = self.getProperty("ProcessingMode").value prefix = self.getProperty("OptionalPrefix").value # --------------------------- REDUCE DATA ----------------------------- Tag = 'SNAP' for r in in_Runs: self.log().notice("processing run %s" % r) self.log().information(str(self.get_IPTS_Local(r))) if self.getProperty("LiveData").value: Tag = 'Live' LoadPreNexusLive(Instrument='SNAP', OutputWorkspace='WS') else: Load(Filename='SNAP' + str(r), OutputWorkspace='WS') NormaliseByCurrent(InputWorkspace='WS', OutputWorkspace='WS') CompressEvents(InputWorkspace='WS', OutputWorkspace='WS') CropWorkspace(InputWorkspace='WS', OutputWorkspace='WS', XMax=50000) RemovePromptPulse(InputWorkspace='WS', OutputWorkspace='WS', Width='1600', Frequency='60.4') if maskWSname is not None: MaskDetectors(Workspace='WS', MaskedWorkspace=maskWSname) self._alignAndFocus(params, calib, cal_File, group) normWS = self._generateNormalization('WS_red', norm, normWS) WS_nor = None if normWS is not None: WS_nor = 'WS_nor' Divide(LHSWorkspace='WS_red', RHSWorkspace=normWS, OutputWorkspace='WS_nor') ReplaceSpecialValues(Inputworkspace='WS_nor', OutputWorkspace='WS_nor', NaNValue='0', NaNError='0', InfinityValue='0', InfinityError='0') new_Tag = Tag if len(prefix) > 0: new_Tag += '_' + prefix # Edit instrument geomety to make final workspace smaller on disk det_table = PreprocessDetectorsToMD( Inputworkspace='WS_red', OutputWorkspace='__SNAP_det_table') polar = np.degrees(det_table.column('TwoTheta')) azi = np.degrees(det_table.column('Azimuthal')) EditInstrumentGeometry(Workspace='WS_red', L2=det_table.column('L2'), Polar=polar, Azimuthal=azi) if WS_nor is not None: EditInstrumentGeometry(Workspace='WS_nor', L2=det_table.column('L2'), Polar=polar, Azimuthal=azi) mtd.remove('__SNAP_det_table') # Save requested formats basename = '%s_%s_%s' % (new_Tag, r, group) self._save(r, basename, norm) # temporary workspace no longer needed DeleteWorkspace(Workspace='WS') # rename everything as appropriate and determine output workspace name RenameWorkspace(Inputworkspace='WS_d', OutputWorkspace='%s_%s_d' % (new_Tag, r)) RenameWorkspace(Inputworkspace='WS_red', OutputWorkspace=basename + '_red') if norm == 'None': outputWksp = basename + '_red' else: outputWksp = basename + '_nor' RenameWorkspace(Inputworkspace='WS_nor', OutputWorkspace=basename + '_nor') if norm == "Extracted from Data": RenameWorkspace(Inputworkspace='peak_clip_WS', OutputWorkspace='%s_%s_normalizer' % (new_Tag, r)) # delte some things in production if Process_Mode == "Production": DeleteWorkspace(Workspace='%s_%s_d' % (new_Tag, r)) # was 'WS_d' if norm != "None": DeleteWorkspace(Workspace=basename + '_red') # was 'WS_red' if norm == "Extracted from Data": DeleteWorkspace(Workspace='%s_%s_normalizer' % (new_Tag, r)) # was 'peak_clip_WS' propertyName = 'OutputWorkspace_' + str(outputWksp) self.declareProperty( WorkspaceProperty(propertyName, outputWksp, Direction.Output)) self.setProperty(propertyName, outputWksp)
def process_json(json_filename): """This will read a json file, process the data and save the calibration. Only ``Calibrant`` and ``Groups`` are required. An example input showing every possible options is: .. code-block:: JSON { "Calibrant": "12345", "Groups": "/path/to/groups.xml", "Mask": "/path/to/mask.xml", "Instrument": "NOM", "Date" : "2019_09_04", "SampleEnvironment": "shifter", "PreviousCalibration": "/path/to/cal.h5", "CalDirectory": "/path/to/output_directory", "CrossCorrelate": {"Step": 0.001, "DReference: 1.5, "Xmin": 1.0, "Xmax": 3.0, "MaxDSpaceShift": 0.25}, "PDCalibration": {"PeakPositions": [1, 2, 3], "TofBinning": (300,0.001,16666), "PeakFunction": 'Gaussian', "PeakWindow": 0.1, "PeakWidthPercent": 0.001} } """ with open(json_filename) as json_file: args = json.load(json_file) calibrant_file = args.get('CalibrantFile', None) if calibrant_file is None: calibrant = args['Calibrant'] groups = args['Groups'] out_groups_by = args.get('OutputGroupsBy', 'Group') sample_env = args.get('SampleEnvironment', 'UnknownSampleEnvironment') mask = args.get('Mask') instrument = args.get('Instrument', 'NOM') cc_kwargs = args.get('CrossCorrelate', {}) pdcal_kwargs = args.get('PDCalibration', {}) previous_calibration = args.get('PreviousCalibration') date = str(args.get('Date', datetime.datetime.now().strftime('%Y_%m_%d'))) caldirectory = str(args.get('CalDirectory', os.path.abspath('.'))) if calibrant_file is not None: ws = Load(calibrant_file) calibrant = ws.getRun().getProperty('run_number').value else: filename = f'{instrument}_{calibrant}' ws = Load(filename) calfilename = f'{caldirectory}/{instrument}_{calibrant}_{date}_{sample_env}.h5' logger.notice(f'going to create calibration file: {calfilename}') groups = LoadDetectorsGroupingFile(groups, InputWorkspace=ws) if mask: mask = LoadMask(instrument, mask) MaskDetectors(ws, MaskedWorkspace=mask) if previous_calibration: previous_calibration = LoadDiffCal(previous_calibration, MakeGroupingWorkspace=False, MakeMaskWorkspace=False) diffcal = do_group_calibration(ws, groups, previous_calibration, cc_kwargs=cc_kwargs, pdcal_kwargs=pdcal_kwargs) mask = mtd['group_calibration_pd_diffcal_mask'] CreateGroupingWorkspace(InputWorkspace=ws, GroupDetectorsBy=out_groups_by, OutputWorkspace='out_groups') SaveDiffCal(CalibrationWorkspace=diffcal, MaskWorkspace=mask, GroupingWorkspace=mtd['out_groups'], Filename=calfilename)
def PyExec(self): fn = self.getPropertyValue("Filename") wsn = self.getPropertyValue("OutputWorkspace") #print (fn, wsn) self.fxml = self.getPropertyValue("InstrumentXML") #load data parms_dict, det_udet, det_count, det_tbc, data = self.read_file(fn) nrows = int(parms_dict['NDET']) #nbins=int(parms_dict['NTC']) xdata = np.array(det_tbc) xdata_mon = np.linspace(xdata[0], xdata[-1], len(xdata)) ydata = data.astype(np.float) ydata = ydata.reshape(nrows, -1) edata = np.sqrt(ydata) #CreateWorkspace(OutputWorkspace=wsn,DataX=xdata,DataY=ydata,DataE=edata, # NSpec=nrows,UnitX='TOF',WorkspaceTitle='Data',YUnitLabel='Counts') nr, nc = ydata.shape ws = WorkspaceFactory.create("Workspace2D", NVectors=nr, XLength=nc + 1, YLength=nc) for i in range(nrows): ws.setX(i, xdata) ws.setY(i, ydata[i]) ws.setE(i, edata[i]) ws.getAxis(0).setUnit('tof') AnalysisDataService.addOrReplace(wsn, ws) #self.setProperty("OutputWorkspace", wsn) #print ("ws:", wsn) #ws=mtd[wsn] # fix the x values for the monitor for i in range(nrows - 2, nrows): ws.setX(i, xdata_mon) self.log().information("set detector IDs") #set detetector IDs for i in range(nrows): ws.getSpectrum(i).setDetectorID(det_udet[i]) #Sample_logs the header values are written into the sample logs log_names = [sl.encode('ascii', 'ignore') for sl in parms_dict.keys()] log_values = [ sl.encode('ascii', 'ignore') if isinstance(sl, types.UnicodeType) else str(sl) for sl in parms_dict.values() ] AddSampleLogMultiple(Workspace=wsn, LogNames=log_names, LogValues=log_values) SetGoniometer(Workspace=wsn, Goniometers='Universal') if (self.fxml == ""): LoadInstrument(Workspace=wsn, InstrumentName="Exed", RewriteSpectraMap=True) else: LoadInstrument(Workspace=wsn, Filename=self.fxml, RewriteSpectraMap=True) RotateInstrumentComponent( Workspace=wsn, ComponentName='Tank', Y=1, Angle=-float(parms_dict['phi'].encode('ascii', 'ignore')), RelativeRotation=False) # Separate monitors into seperate workspace ExtractSpectra(InputWorkspace=wsn, WorkspaceIndexList=','.join( [str(s) for s in range(nrows - 2, nrows)]), OutputWorkspace=wsn + '_Monitors') MaskDetectors(Workspace=wsn, WorkspaceIndexList=','.join( [str(s) for s in range(nrows - 2, nrows)])) RemoveMaskedSpectra(InputWorkspace=wsn, OutputWorkspace=wsn) self.setProperty("OutputWorkspace", wsn)
def PyExec(self): # remove possible old temp workspaces [ DeleteWorkspace(ws) for ws in self.temp_workspace_list if mtd.doesExist(ws) ] _background = bool(self.getProperty("Background").value) _load_inst = bool(self.getProperty("LoadInstrument").value) _detcal = bool(self.getProperty("DetCal").value) _masking = bool(self.getProperty("MaskFile").value) _outWS_name = self.getPropertyValue("OutputWorkspace") UBList = self._generate_UBList() dim0_min, dim0_max, dim0_bins = self.getProperty('BinningDim0').value dim1_min, dim1_max, dim1_bins = self.getProperty('BinningDim1').value dim2_min, dim2_max, dim2_bins = self.getProperty('BinningDim2').value MinValues = "{},{},{}".format(dim0_min, dim1_min, dim2_min) MaxValues = "{},{},{}".format(dim0_max, dim1_max, dim2_max) AlignedDim0 = ",{},{},{}".format(dim0_min, dim0_max, int(dim0_bins)) AlignedDim1 = ",{},{},{}".format(dim1_min, dim1_max, int(dim1_bins)) AlignedDim2 = ",{},{},{}".format(dim2_min, dim2_max, int(dim2_bins)) LoadNexus(Filename=self.getProperty("SolidAngle").value, OutputWorkspace='__sa') LoadNexus(Filename=self.getProperty("Flux").value, OutputWorkspace='__flux') if _masking: LoadMask(Instrument=mtd['__sa'].getInstrument().getName(), InputFile=self.getProperty("MaskFile").value, OutputWorkspace='__mask') MaskDetectors(Workspace='__sa', MaskedWorkspace='__mask') DeleteWorkspace('__mask') XMin = mtd['__sa'].getXDimension().getMinimum() XMax = mtd['__sa'].getXDimension().getMaximum() if _background: Load(Filename=self.getProperty("Background").value, OutputWorkspace='__bkg', FilterByTofMin=self.getProperty("FilterByTofMin").value, FilterByTofMax=self.getProperty("FilterByTofMax").value) if _load_inst: LoadInstrument( Workspace='__bkg', Filename=self.getProperty("LoadInstrument").value, RewriteSpectraMap=False) if _detcal: LoadIsawDetCal(InputWorkspace='__bkg', Filename=self.getProperty("DetCal").value) MaskDetectors(Workspace='__bkg', MaskedWorkspace='__sa') ConvertUnits(InputWorkspace='__bkg', OutputWorkspace='__bkg', Target='Momentum') CropWorkspace(InputWorkspace='__bkg', OutputWorkspace='__bkg', XMin=XMin, XMax=XMax) progress = Progress( self, 0.0, 1.0, len(UBList) * len(self.getProperty("Filename").value)) for run in self.getProperty("Filename").value: logger.notice("Working on " + run) Load(Filename=run, OutputWorkspace='__run', FilterByTofMin=self.getProperty("FilterByTofMin").value, FilterByTofMax=self.getProperty("FilterByTofMax").value) if _load_inst: LoadInstrument( Workspace='__run', Filename=self.getProperty("LoadInstrument").value, RewriteSpectraMap=False) if _detcal: LoadIsawDetCal(InputWorkspace='__run', Filename=self.getProperty("DetCal").value) MaskDetectors(Workspace='__run', MaskedWorkspace='__sa') ConvertUnits(InputWorkspace='__run', OutputWorkspace='__run', Target='Momentum') CropWorkspace(InputWorkspace='__run', OutputWorkspace='__run', XMin=XMin, XMax=XMax) if self.getProperty('SetGoniometer').value: SetGoniometer( Workspace='__run', Goniometers=self.getProperty('Goniometers').value, Axis0=self.getProperty('Axis0').value, Axis1=self.getProperty('Axis1').value, Axis2=self.getProperty('Axis2').value) # Set background Goniometer to be the same as data if _background: mtd['__bkg'].run().getGoniometer().setR( mtd['__run'].run().getGoniometer().getR()) for ub in UBList: SetUB(Workspace='__run', UB=ub) ConvertToMD(InputWorkspace='__run', OutputWorkspace='__md', QDimensions='Q3D', dEAnalysisMode='Elastic', Q3DFrames='HKL', QConversionScales='HKL', Uproj=self.getProperty('Uproj').value, Vproj=self.getProperty('Vproj').value, Wproj=self.getProperty('wproj').value, MinValues=MinValues, MaxValues=MaxValues) MDNormSCD( InputWorkspace=mtd['__md'], FluxWorkspace='__flux', SolidAngleWorkspace='__sa', OutputWorkspace='__data', SkipSafetyCheck=True, TemporaryDataWorkspace='__data' if mtd.doesExist('__data') else None, OutputNormalizationWorkspace='__norm', TemporaryNormalizationWorkspace='__norm' if mtd.doesExist('__norm') else None, AlignedDim0=mtd['__md'].getDimension(0).name + AlignedDim0, AlignedDim1=mtd['__md'].getDimension(1).name + AlignedDim1, AlignedDim2=mtd['__md'].getDimension(2).name + AlignedDim2) DeleteWorkspace('__md') if _background: SetUB(Workspace='__bkg', UB=ub) ConvertToMD(InputWorkspace='__bkg', OutputWorkspace='__bkg_md', QDimensions='Q3D', dEAnalysisMode='Elastic', Q3DFrames='HKL', QConversionScales='HKL', Uproj=self.getProperty('Uproj').value, Vproj=self.getProperty('Vproj').value, Wproj=self.getProperty('Wproj').value, MinValues=MinValues, MaxValues=MaxValues) MDNormSCD( InputWorkspace='__bkg_md', FluxWorkspace='__flux', SolidAngleWorkspace='__sa', SkipSafetyCheck=True, OutputWorkspace='__bkg_data', TemporaryDataWorkspace='__bkg_data' if mtd.doesExist('__bkg_data') else None, OutputNormalizationWorkspace='__bkg_norm', TemporaryNormalizationWorkspace='__bkg_norm' if mtd.doesExist('__bkg_norm') else None, AlignedDim0=mtd['__bkg_md'].getDimension(0).name + AlignedDim0, AlignedDim1=mtd['__bkg_md'].getDimension(1).name + AlignedDim1, AlignedDim2=mtd['__bkg_md'].getDimension(2).name + AlignedDim2) DeleteWorkspace('__bkg_md') progress.report() DeleteWorkspace('__run') if _background: # outWS = data / norm - bkg_data / bkg_norm * BackgroundScale DivideMD(LHSWorkspace='__data', RHSWorkspace='__norm', OutputWorkspace=_outWS_name + '_normalizedData') DivideMD(LHSWorkspace='__bkg_data', RHSWorkspace='__bkg_norm', OutputWorkspace=_outWS_name + '_normalizedBackground') CreateSingleValuedWorkspace( OutputWorkspace='__scale', DataValue=self.getProperty('BackgroundScale').value) MultiplyMD(LHSWorkspace=_outWS_name + '_normalizedBackground', RHSWorkspace='__scale', OutputWorkspace='__scaled_background') DeleteWorkspace('__scale') MinusMD(LHSWorkspace=_outWS_name + '_normalizedData', RHSWorkspace='__scaled_background', OutputWorkspace=_outWS_name) if self.getProperty('KeepTemporaryWorkspaces').value: RenameWorkspaces(InputWorkspaces=[ '__data', '__norm', '__bkg_data', '__bkg_norm' ], WorkspaceNames=[ _outWS_name + '_data', _outWS_name + '_normalization', _outWS_name + '_background_data', _outWS_name + '_background_normalization' ]) else: # outWS = data / norm DivideMD(LHSWorkspace='__data', RHSWorkspace='__norm', OutputWorkspace=_outWS_name) if self.getProperty('KeepTemporaryWorkspaces').value: RenameWorkspaces(InputWorkspaces=['__data', '__norm'], WorkspaceNames=[ _outWS_name + '_data', _outWS_name + '_normalization' ]) self.setProperty("OutputWorkspace", mtd[_outWS_name]) # remove temp workspaces [ DeleteWorkspace(ws) for ws in self.temp_workspace_list if mtd.doesExist(ws) ]
def PyExec(self): _background = bool(self.getProperty("Background").value) _load_inst = bool(self.getProperty("LoadInstrument").value) _norm_current = bool(self.getProperty("NormaliseByCurrent").value) _detcal = bool(self.getProperty("DetCal").value) _masking = bool(self.getProperty("MaskFile").value) _grouping = bool(self.getProperty("GroupingFile").value) _anvred = bool(self.getProperty("SphericalAbsorptionCorrection").value) _SA_name = self.getPropertyValue("SolidAngleOutputWorkspace") _Flux_name = self.getPropertyValue("FluxOutputWorkspace") XMin = self.getProperty("MomentumMin").value XMax = self.getProperty("MomentumMax").value rebin_param = ','.join([str(XMin), str(XMax), str(XMax)]) Load(Filename=self.getPropertyValue("Filename"), OutputWorkspace='__van', FilterByTofMin=self.getProperty("FilterByTofMin").value, FilterByTofMax=self.getProperty("FilterByTofMax").value) if _norm_current: NormaliseByCurrent(InputWorkspace='__van', OutputWorkspace='__van') if _background: Load(Filename=self.getProperty("Background").value, OutputWorkspace='__bkg', FilterByTofMin=self.getProperty("FilterByTofMin").value, FilterByTofMax=self.getProperty("FilterByTofMax").value) if _norm_current: NormaliseByCurrent(InputWorkspace='__bkg', OutputWorkspace='__bkg') else: pc_van = mtd['__van'].run().getProtonCharge() pc_bkg = mtd['__bkg'].run().getProtonCharge() mtd['__bkg'] *= pc_van / pc_bkg mtd['__bkg'] *= self.getProperty('BackgroundScale').value Minus(LHSWorkspace='__van', RHSWorkspace='__bkg', OutputWorkspace='__van') DeleteWorkspace('__bkg') if _load_inst: LoadInstrument(Workspace='__van', Filename=self.getProperty("LoadInstrument").value, RewriteSpectraMap=False) if _detcal: LoadIsawDetCal(InputWorkspace='__van', Filename=self.getProperty("DetCal").value) if _masking: LoadMask(Instrument=mtd['__van'].getInstrument().getName(), InputFile=self.getProperty("MaskFile").value, OutputWorkspace='__mask') MaskDetectors(Workspace='__van', MaskedWorkspace='__mask') DeleteWorkspace('__mask') ConvertUnits(InputWorkspace='__van', OutputWorkspace='__van', Target='Momentum') Rebin(InputWorkspace='__van', OutputWorkspace='__van', Params=rebin_param) CropWorkspace(InputWorkspace='__van', OutputWorkspace='__van', XMin=XMin, XMax=XMax) if _anvred: AnvredCorrection(InputWorkspace='__van', OutputWorkspace='__van', LinearScatteringCoef=self.getProperty( "LinearScatteringCoef").value, LinearAbsorptionCoef=self.getProperty( "LinearAbsorptionCoef").value, Radius=self.getProperty("Radius").value, OnlySphericalAbsorption='1', PowerLambda='0') # Create solid angle Rebin(InputWorkspace='__van', OutputWorkspace=_SA_name, Params=rebin_param, PreserveEvents=False) # Create flux if _grouping: GroupDetectors(InputWorkspace='__van', OutputWorkspace='__van', MapFile=self.getProperty("GroupingFile").value) else: SumSpectra(InputWorkspace='__van', OutputWorkspace='__van') Rebin(InputWorkspace='__van', OutputWorkspace='__van', Params=rebin_param) flux = mtd['__van'] for i in range(flux.getNumberHistograms()): el = flux.getSpectrum(i) if flux.readY(i)[0] > 0: el.divide(flux.readY(i)[0], flux.readE(i)[0]) SortEvents(InputWorkspace='__van', SortBy="X Value") IntegrateFlux(InputWorkspace='__van', OutputWorkspace=_Flux_name, NPoints=10000) DeleteWorkspace('__van') self.setProperty("SolidAngleOutputWorkspace", mtd[_SA_name]) self.setProperty("FluxOutputWorkspace", mtd[_Flux_name])
def int3samples(runs, name, masks, binning='0.5, 0.05, 8.0'): """ Finds the polarisation versus wavelength for a set of detector tubes. Parameters ---------- runs: list of RunData objects The runs whose polarisation we are interested in. name: string The name of this set of runs masks: list of string The file names of the masks for the sequential tubes that are being used for the SEMSANS measurements. binning: string The binning values to use for the wavelength bins. The default value is '0.5, 0.025, 10.0' """ for tube, _ in enumerate(masks): for i in [1, 2]: final_state = "{}_{}_{}".format(name, tube, i) if final_state in mtd.getObjectNames(): DeleteWorkspace(final_state) for rnum in runs: w1 = Load(BASE.format(rnum.number), LoadMonitors=True) w1mon = ExtractSingleSpectrum('w1_monitors', 0) w1 = ConvertUnits('w1', 'Wavelength', AlignBins=1) w1mon = ConvertUnits(w1mon, 'Wavelength') w1 = Rebin(w1, binning, PreserveEvents=False) w1mon = Rebin(w1mon, binning) w1 = w1 / w1mon for tube, mask in enumerate(masks): Mask_Tube = LoadMask('LARMOR', mask) w1temp = CloneWorkspace(w1) MaskDetectors(w1temp, MaskedWorkspace="Mask_Tube") Tube_Sum = SumSpectra(w1temp) for i in [1, 2]: final_state = "{}_{}_{}".format(name, tube, i) if final_state in mtd.getObjectNames(): mtd[final_state] += mtd["Tube_Sum_{}".format(i)] else: mtd[final_state] = mtd["Tube_Sum_{}".format(i)] x = mtd["{}_0_1".format(name)].extractX()[0] dx = (x[1:] + x[:-1]) / 2 pols = [] for run in runs: he_stat = he3_stats(run) start = (run.start - he_stat.dt).seconds / 3600 / he_stat.t1 end = (run.end - he_stat.dt).seconds / 3600 / he_stat.t1 for time in np.linspace(start, end, 10): temp = he3pol(he_stat.scale, time)(dx) pols.append(temp) wpol = CreateWorkspace( x, np.mean(pols, axis=0), # and the blank UnitX="Wavelength", YUnitLabel="Counts") for tube, _ in enumerate(masks): up = mtd["{}_{}_2".format(name, tube)] dn = mtd["{}_{}_1".format(name, tube)] pol = (up - dn) / (up + dn) pol /= wpol DeleteWorkspaces( ["{}_{}_{}".format(name, tube, i) for i in range(1, 3)]) RenameWorkspace("pol", OutputWorkspace="{}_{}".format(name, tube)) DeleteWorkspaces(["Tube_Sum_1", "Tube_Sum_2"]) GroupWorkspaces([ "{}_{}".format(name, tube) for tube, _ in enumerate(masks) for i in range(1, 3) ], OutputWorkspace=str(name))