def run(config, tim=None): """ This method is where the data reduction process gets done. @param config: Object containing the data reduction configuration information. @type config: L{hlr_utils.Configure} @param tim: (OPTIONAL) Object that will allow the method to perform timing evaluations. @type tim: C{sns_time.DiffTime} """ import common_lib import dr_lib import DST if tim is not None: tim.getTime(False) old_time = tim.getOldTime() if config.data is None: raise RuntimeError("Need to pass a data filename to the driver "\ +"script.") # Read in geometry if one is provided if config.inst_geom is not None: if config.verbose: print "Reading in instrument geometry file" inst_geom_dst = DST.getInstance("application/x-NxsGeom", config.inst_geom) else: inst_geom_dst = None config.so_axis = "time_of_flight" # Steps 1-3: Produce a scaled summed dark current dataset dc_som = dr_lib.scaled_summed_data(config.dkcur, config, dataset_type="dark_current", timer=tim) # Perform Steps 3-6 on black can data if config.bcan is not None: b_som1 = dr_lib.calibrate_dgs_data(config.bcan, config, dc_som, dataset_type="black_can", inst_geom_dst=inst_geom_dst, tib_const=config.tib_const, cwp=config.cwp_bcan, timer=tim) else: b_som1 = None # Perform Steps 3-6 on empty can data if config.ecan is not None: e_som1 = dr_lib.calibrate_dgs_data(config.ecan, config, dc_som, dataset_type="empty_can", inst_geom_dst=inst_geom_dst, tib_const=config.tib_const, cwp=config.cwp_ecan, timer=tim) else: e_som1 = None # Perform Steps 3-6 on normalization data n_som1 = dr_lib.calibrate_dgs_data(config.data, config, dc_som, dataset_type="normalization", inst_geom_dst=inst_geom_dst, tib_const=config.tib_const, cwp=config.cwp_data, timer=tim) # Perform Steps 7-16 on normalization data if config.norm_trans_coeff is None: norm_trans_coeff = None else: norm_trans_coeff = config.norm_trans_coeff.toValErrTuple() # Determine if we need to rebin the empty or black can data if config.ecan is not None and e_som1 is not None: ecan_cwp = True else: ecan_cwp = False if config.bcan is not None and b_som1 is not None: bcan_cwp = True else: bcan_cwp = False cwp_used = ecan_cwp or bcan_cwp n_som2 = dr_lib.process_dgs_data(n_som1, config, b_som1, e_som1, norm_trans_coeff, dataset_type="normalization", cwp_used=cwp_used, timer=tim) del n_som1, b_som1, e_som1 # Step 17: Integrate normalization spectra if config.verbose: print "Integrating normalization spectra" if tim is not None: tim.getTime(False) if config.norm_int_range is None: start_val = float("inf") end_val = float("inf") else: if not config.wb_norm: # Translate energy transfer to final energy ef_start = config.initial_energy.getValue() - \ config.norm_int_range[0] ef_end = config.initial_energy.getValue() - \ config.norm_int_range[1] # Convert final energy to final wavelength start_val = common_lib.energy_to_wavelength((ef_start, 0.0))[0] end_val = common_lib.energy_to_wavelength((ef_end, 0.0))[0] else: start_val = config.norm_int_range[0] end_val = config.norm_int_range[1] n_som3 = dr_lib.integrate_spectra(n_som2, start=start_val, end=end_val, width=True) del n_som2 if tim is not None: tim.getTime(msg="After integrating normalization spectra ") file_comment = "Normalization Integration range: %0.3fA, %0.3fA" \ % (start_val, end_val) hlr_utils.write_file(config.output, "text/num-info", n_som3, output_ext="norm", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="normalization values", comments=[file_comment], tag="Integral", units="counts") if tim is not None: tim.getTime(False) if config.verbose: print "Making mask file" # Make mask file from threshold dr_lib.filter_normalization(n_som3, config.lo_threshold, config.hi_threshold, config) if tim is not None: tim.getTime(msg="After making mask file ") # Write out RMD file n_som3.attr_list["config"] = config hlr_utils.write_file(config.output, "text/rmd", n_som3, output_ext="rmd", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="metadata") if tim is not None: tim.setOldTime(old_time) tim.getTime(msg="Total Running Time")
def run(config, tim=None): """ This method is where the data reduction process gets done. @param config: Object containing the data reduction configuration information. @type config: L{hlr_utils.Configure} @param tim: (OPTIONAL) Object that will allow the method to perform timing evaluations. @type tim: C{sns_time.DiffTime} """ import common_lib import dr_lib import DST if tim is not None: tim.getTime(False) old_time = tim.getOldTime() if config.data is None: raise RuntimeError("Need to pass a data filename to the driver "\ +"script.") # Read in geometry if one is provided if config.inst_geom is not None: if config.verbose: print "Reading in instrument geometry file" inst_geom_dst = DST.getInstance("application/x-NxsGeom", config.inst_geom) else: inst_geom_dst = None # Perform early background subtraction if the hwfix flag is used if config.hwfix: if not config.mc: so_axis = "time_of_flight" else: so_axis = "Time_of_Flight" bkg_som0 = dr_lib.add_files(config.back, Data_Paths=config.data_paths.toPath(), SO_Axis=so_axis, Signal_ROI=config.roi_file, dataset_type="background", Verbose=config.verbose, Timer=tim) bkg_som = dr_lib.fix_bin_contents(bkg_som0) del bkg_som0 else: bkg_som = None # Perform Steps 1-15 on sample data d_som1 = dr_lib.process_igs_data(config.data, config, timer=tim, inst_geom_dst=inst_geom_dst, tib_const=config.tib_data_const, bkg_som=bkg_som) # Perform Steps 1-15 on empty can data if config.ecan is not None: e_som1 = dr_lib.process_igs_data(config.ecan, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="empty_can", tib_const=config.tib_ecan_const, bkg_som=bkg_som) else: e_som1 = None # Perform Steps 1-15 on normalization data if config.norm is not None: n_som1 = dr_lib.process_igs_data(config.norm, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="normalization", tib_const=config.tib_norm_const, bkg_som=bkg_som) else: n_som1 = None # Perform Steps 1-15 on background data if config.back is not None and not config.hwfix: b_som1 = dr_lib.process_igs_data(config.back, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="background", tib_const=config.tib_back_const) else: b_som1 = None # Perform Step 1-15 on direct scattering background data if config.dsback is not None: ds_som1 = dr_lib.process_igs_data(config.dsback, config, timer=tim, inst_geom_dst=inst_geom_dst, tib_const=config.tib_dsback_const, dataset_type="dsbackground", bkg_som=bkg_som) # Note: time_zero_slope MUST be a tuple if config.time_zero_slope is not None: ds_som1.attr_list["Time_zero_slope"] = \ config.time_zero_slope.toValErrTuple() # Note: time_zero_offset MUST be a tuple if config.time_zero_offset is not None: ds_som1.attr_list["Time_zero_offset"] = \ config.time_zero_offset.toValErrTuple() # Step 16: Linearly interpolate TOF elastic range in direct scattering # background data # First convert TOF elastic range to appropriate pixel initial # wavelengths if config.verbose: print "Determining initial wavelength range for elastic line" if tim is not None: tim.getTime(False) if config.tof_elastic is None: # Units are in microseconds tof_elastic_range = (140300, 141300) else: tof_elastic_range = config.tof_elastic ctof_elastic_low = dr_lib.convert_single_to_list(\ "tof_to_initial_wavelength_igs_lin_time_zero", (tof_elastic_range[0], 0.0), ds_som1) ctof_elastic_high = dr_lib.convert_single_to_list(\ "tof_to_initial_wavelength_igs_lin_time_zero", (tof_elastic_range[1], 0.0), ds_som1) ctof_elastic_range = [(ctof_elastic_low[i][0], ctof_elastic_high[i][0]) for i in xrange(len(ctof_elastic_low))] if tim is not None: tim.getTime(msg="After calculating initial wavelength range for "\ +"elastic line ") del ctof_elastic_low, ctof_elastic_high # Now interpolate spectra between TOF elastic range (converted to # initial wavelength) if config.verbose: print "Linearly interpolating direct scattering spectra" if tim is not None: tim.getTime(False) ds_som2 = dr_lib.lin_interpolate_spectra(ds_som1, ctof_elastic_range) if tim is not None: tim.getTime(msg="After linearly interpolating direct scattering "\ +"spectra ") if config.dump_dslin: ds_som2_1 = dr_lib.sum_all_spectra(ds_som2,\ rebin_axis=config.lambda_bins.toNessiList()) hlr_utils.write_file(config.output, "text/Spec", ds_som2_1, output_ext="lin", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="dsbackground linear interpolation") del ds_som2_1 del ds_som1 else: ds_som2 = None if inst_geom_dst is not None: inst_geom_dst.release_resource() # Steps 17-18: Subtract background spectrum from sample spectrum if config.dsback is None: back_som = b_som1 bkg_type = "background" else: back_som = ds_som2 bkg_type = "dsbackground" d_som2 = dr_lib.subtract_bkg_from_data(d_som1, back_som, verbose=config.verbose, timer=tim, dataset1="data", dataset2=bkg_type, scale=config.scale_bs) if config.dsback is not None: del ds_som2 # Step 19: Zero region outside TOF elastic for background for empty can if config.dsback is None: bcs_som = b_som1 cs_som = e_som1 else: if config.verbose and b_som1 is not None: print "Zeroing background spectra" if tim is not None and b_som1 is not None: tim.getTime(False) bcs_som = dr_lib.zero_spectra(b_som1, ctof_elastic_range) if tim is not None and b_som1 is not None: tim.getTime(msg="After zeroing background spectra") if config.verbose and e_som1 is not None: print "Zeroing empty can spectra" if tim is not None and e_som1 is not None: tim.getTime(False) cs_som = dr_lib.zero_spectra(e_som1, ctof_elastic_range) if tim is not None and e_som1 is not None: tim.getTime(msg="After zeroing empty can spectra") del ctof_elastic_range # Steps 20-21: Subtract background spectrum from empty can spectrum e_som2 = dr_lib.subtract_bkg_from_data(cs_som, bcs_som, verbose=config.verbose, timer=tim, dataset1="empty_can", dataset2="background", scale=config.scale_bcs) # Steps 22-23: Subtract background spectrum from empty can spectrum for # normalization e_som3 = dr_lib.subtract_bkg_from_data(e_som1, b_som1, verbose=config.verbose, timer=tim, dataset1="empty_can", dataset2="background", scale=config.scale_bcn) # Steps 24-25: Subtract background spectrum from normalization spectrum n_som2 = dr_lib.subtract_bkg_from_data(n_som1, b_som1, verbose=config.verbose, timer=tim, dataset1="normalization", dataset2="background", scale=config.scale_bn) del b_som1, e_som1, bcs_som, cs_som # Steps 26-27: Subtract empty can spectrum from sample spectrum d_som3 = dr_lib.subtract_bkg_from_data(d_som2, e_som2, verbose=config.verbose, timer=tim, dataset1="data", dataset2="empty_can", scale=config.scale_cs) del d_som2, e_som2 # Steps 28-29: Subtract empty can spectrum from normalization spectrum n_som3 = dr_lib.subtract_bkg_from_data(n_som2, e_som3, verbose=config.verbose, timer=tim, dataset1="normalization", dataset2="empty_can", scale=config.scale_cn) del n_som2, e_som3 # Step 30-32: Integrate normalization spectra if config.verbose and n_som3 is not None: print "Integrating normalization spectra" norm_int = dr_lib.integrate_spectra(n_som3, start=config.norm_start, end=config.norm_end, norm=True) del n_som3 # Step 33: Normalize data by integrated values if config.verbose and norm_int is not None: print "Normalizing data by normalization data" if norm_int is not None: d_som4 = common_lib.div_ncerr(d_som3, norm_int) else: d_som4 = d_som3 if norm_int is not None: if tim is not None: tim.getTime(msg="After normalizing data ") del d_som3, norm_int # Step 35: Convert initial wavelength to E_initial if config.verbose: print "Converting initial wavelength to E_initial" if tim is not None: tim.getTime(False) d_som6 = common_lib.wavelength_to_energy(d_som4) if tim is not None: tim.getTime(msg="After converting initial wavelength to E_initial ") if config.dump_initial_energy: hlr_utils.write_file(config.output, "text/Spec", d_som6, output_ext="ixl", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="pixel initial energy information") del d_som4 # Steps 36-37: Calculate energy transfer if config.verbose: print "Calculating energy transfer" if tim is not None: tim.getTime(False) d_som7 = dr_lib.igs_energy_transfer(d_som6) if tim is not None: tim.getTime(msg="After calculating energy transfer ") if config.dump_energy: hlr_utils.write_file(config.output, "text/Spec", d_som7, output_ext="exl", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="pixel energy transfer information") # Write 3-column ASCII file for E_t d_som7_1 = dr_lib.sum_all_spectra(d_som7, rebin_axis=config.E_bins.toNessiList()) hlr_utils.write_file(config.output, "text/Spec", d_som7_1, output_ext="etr", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="combined energy transfer information") del d_som7_1 # Steps 34,36-37: Calculate scaled energy transfer if config.verbose: print "Calculating scaled energy transfer" d_som9 = dr_lib.igs_energy_transfer(d_som6, scale=True) if tim is not None: tim.getTime(msg="After calculating scaled energy transfer ") if config.dump_energy: hlr_utils.write_file(config.output, "text/Spec", d_som9, output_ext="sexl", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="pixel scaled energy transfer "\ +"information") # Write 3-column ASCII file for scaled E_t d_som9_1 = dr_lib.sum_all_spectra(d_som9, rebin_axis=config.E_bins.toNessiList()) hlr_utils.write_file(config.output, "text/Spec", d_som9_1, output_ext="setr", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="combined scaled energy transfer "\ +"information") del d_som9_1 del d_som6, d_som7 d_som9.attr_list["config"] = config hlr_utils.write_file(config.output, "text/rmd", d_som9, output_ext="rmd", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="metadata") if tim is not None: tim.setOldTime(old_time) tim.getTime(msg="Total Running Time")
def calibrate_dgs_data(datalist, conf, dkcur, **kwargs): """ This function combines Steps 3 through 6 in Section 2.1.1 of the data reduction process for Direct Geometry Spectrometers as specified by the document at U{http://neutrons.ornl.gov/asg/projects/SCL/reqspec/DR_Lib_RS.doc}. The function takes a list of file names, a L{hlr_utils.Configure} object and processes the data accordingly. @param datalist: A list containing the filenames of the data to be processed. @type datalist: C{list} of C{string}s @param conf: Object that contains the current setup of the driver. @type conf: L{hlr_utils.Configure} @param dkcur: The object containing the TOF dark current data. @type dkcur: C{SOM.SOM} @param kwargs: A list of keyword arguments that the function accepts: @keyword inst_geom_dst: File object that contains instrument geometry information. @type inst_geom_dst: C{DST.GeomDST} @keyword tib_const: A time-independent background constant to subtract from every pixel. @type tib_const: L{hlr_utils.DrParameter} @keyword dataset_type: The practical name of the dataset being processed. The default value is I{data}. @type dataset_type: C{string} @keyword cwp: A list of chopper phase corrections in units of microseconds. @type cwp: C{list} of C{float}s @keyword timer: Timing object so the function can perform timing estimates. @type timer: C{sns_timer.DiffTime} @return: Object that has undergone all requested processing steps @rtype: C{SOM.SOM} """ import common_lib import dr_lib import hlr_utils # Check keywords try: tib_const = kwargs["tib_const"] except KeyError: tib_const = None try: dataset_type = kwargs["dataset_type"] except KeyError: dataset_type = "data" try: t = kwargs["timer"] except KeyError: t = None try: i_geom_dst = kwargs["inst_geom_dst"] except KeyError: i_geom_dst = None dataset_cwp = kwargs.get("cwp") # Open the appropriate datafiles if conf.verbose: print "Reading %s file" % dataset_type data_paths = conf.data_paths.toPath() if conf.no_mon_norm: mon_paths = None else: mon_paths = conf.usmon_path.toPath() # Check for mask file since normalization drive doesn't understand option try: mask_file = conf.mask_file except AttributeError: mask_file = None if t is not None: oldtime = t.getOldTime() (dp_som0, dm_som0) = dr_lib.add_files_dm(datalist, Data_Paths=data_paths, Mon_Paths=mon_paths, SO_Axis=conf.so_axis, Signal_ROI=conf.roi_file, Signal_MASK=mask_file, dataset_type=dataset_type, dataset_cwp=dataset_cwp, Verbose=conf.verbose, Timer=t) if t is not None: t.setOldTime(oldtime) t.getTime(msg="After reading %s file" % dataset_type) # Cut the spectra if necessary dp_somA = dr_lib.cut_spectra(dp_som0, conf.tof_cut_min, conf.tof_cut_max) del dp_som0 dp_somB = dr_lib.fix_bin_contents(dp_somA) del dp_somA if dp_somB.attr_list.instrument.get_name() != "CNCS": if conf.verbose: print "Cutting spectrum at minimum TOF" if t is not None: t.getTime(False) # Calculate minimum TOF for physical neutrons if conf.initial_energy is not None: initial_wavelength = common_lib.energy_to_wavelength(\ conf.initial_energy.toValErrTuple()) initial_velocity = common_lib.wavelength_to_velocity(\ initial_wavelength) else: # This should actually calculate it, but don't have a way right now pass if conf.time_zero_offset is not None: time_zero_offset = conf.time_zero_offset.toValErrTuple() else: # This should actually calculate it, but don't have a way right now time_zero_offset = (0.0, 0.0) ss_length = dp_somB.attr_list.instrument.get_primary() tof_min = (ss_length[0] / initial_velocity[0]) + time_zero_offset[0] # Cut all spectra a the minimum TOF dp_som1 = dr_lib.cut_spectra(dp_somB, tof_min, None) if t is not None: t.getTime(msg="After cutting spectrum at minimum TOF ") else: dp_som1 = dp_somB del dp_somB if dm_som0 is not None: dm_som1 = dr_lib.fix_bin_contents(dm_som0) else: dm_som1 = dm_som0 del dm_som0 # Override geometry if necessary if conf.inst_geom is not None: i_geom_dst.setGeometry(data_paths, dp_som1) if conf.inst_geom is not None and dm_som1 is not None: i_geom_dst.setGeometry(mon_paths, dm_som1) # Step 3: Integrate the upstream monitor if dm_som1 is not None: if conf.verbose: print "Integrating upstream monitor spectrum" if t is not None: t.getTime(False) if conf.mon_int_range is None: start_val = float("inf") end_val = float("inf") else: start_val = conf.mon_int_range[0] end_val = conf.mon_int_range[1] dm_som2 = dr_lib.integrate_spectra(dm_som1, start=start_val, end=end_val, width=True) if t is not None: t.getTime(msg="After integrating upstream monitor spectrum ") else: dm_som2 = dm_som1 del dm_som1 tib_norm_const = None # Step 4: Divide data set by summed monitor spectrum if dm_som2 is not None: if conf.verbose: print "Normalizing %s by monitor sum" % dataset_type if t is not None: t.getTime(False) dp_som2 = common_lib.div_ncerr(dp_som1, dm_som2, length_one_som=True) tib_norm_const = dm_som2[0].y if t is not None: t.getTime(msg="After normalizing %s by monitor sum" % dataset_type) elif conf.pc_norm: if conf.verbose: print "Normalizing %s by proton charge" % dataset_type pc_tag = dataset_type+"-proton_charge" pc = dp_som1.attr_list[pc_tag] # Scale the proton charge and then set the scale PC back to attributes if conf.scale_pc is not None: if conf.verbose: print "Scaling %s proton charge" % dataset_type pc = hlr_utils.scale_proton_charge(pc, conf.scale_pc) dp_som1.attr_list[pc_tag] = pc tib_norm_const = pc.getValue() if t is not None: t.getTime(False) dp_som2 = common_lib.div_ncerr(dp_som1, (pc.getValue(), 0.0)) if t is not None: t.getTime(msg="After normalizing %s by proton charge" \ % dataset_type) else: dp_som2 = dp_som1 del dp_som1, dm_som2 # Step 5: Scale dark current by data set measurement time if dkcur is not None: if conf.verbose: print "Scaling dark current by %s acquisition time" % dataset_type if t is not None: t.getTime(False) dstime_tag = dataset_type+"-duration" dstime = dp_som2.attr_list[dstime_tag] dkcur1 = common_lib.div_ncerr(dkcur, (dstime.getValue(), 0.0)) if t is not None: t.getTime(msg="After scaling dark current by %s acquisition time" \ % dataset_type) else: dkcur1 = dkcur del dkcur # Step 6: Subtract scaled dark current from data set if dkcur1 is not None: if conf.verbose: print "Subtracting %s by scaled dark current" % dataset_type if t is not None: t.getTime(False) dp_som3 = common_lib.sub_ncerr(dp_som2, dkcur1) if t is not None: t.getTime(msg="After subtracting %s by scaled dark current" \ % dataset_type) elif tib_const is not None and dkcur1 is None: if conf.verbose: print "Subtracting TIB constant from %s" % dataset_type # Normalize the TIB constant by dividing by the current normalization # the duration (if necessary) and the conversion from seconds to # microseconds tib_c = tib_const.toValErrTuple() conv_sec_to_usec = 1.0e-6 if tib_norm_const is None: tib_norm_const = 1 duration = 1 else: duration_tag = dataset_type+"-duration" duration = dp_som2.attr_list[duration_tag].getValue() norm_const = (duration * conv_sec_to_usec) / tib_norm_const tib_val = tib_c[0] * norm_const tib_err2 = tib_c[1] * (norm_const * norm_const) if t is not None: t.getTime(False) dp_som3 = common_lib.sub_ncerr(dp_som2, (tib_val, tib_err2)) if t is not None: t.getTime(msg="After subtracting TIB constant from %s" \ % dataset_type) elif conf.tib_range is not None and dkcur1 is None: if conf.verbose: print "Determining TIB constant from %s" % dataset_type if t is not None: t.getTime(False) TIB = dr_lib.determine_time_indep_bkg(dp_som2, conf.tib_range, is_range=True) if t is not None: t.getTime(msg="After determining TIB constant from %s" \ % dataset_type) if conf.dump_tib: file_comment = "TIB TOF Range: [%d, %d]" % (conf.tib_range[0], conf.tib_range[1]) hlr_utils.write_file(conf.output, "text/num-info", TIB, output_ext="tib", extra_tag=dataset_type, verbose=conf.verbose, data_ext=conf.ext_replacement, path_replacement=conf.path_replacement, message="time-independent background "\ +"information", tag="Average TIB", units="counts/usec", comments=[file_comment]) if conf.verbose: print "Subtracting TIB constant from %s" % dataset_type if t is not None: t.getTime(False) dp_som3 = common_lib.sub_ncerr(dp_som2, TIB) if t is not None: t.getTime(msg="After subtracting TIB constant from %s" \ % dataset_type) del TIB else: dp_som3 = dp_som2 del dp_som2, dkcur1 if conf.dump_ctof_comb: dp_som3_1 = dr_lib.sum_all_spectra(dp_som3) hlr_utils.write_file(conf.output, "text/Spec", dp_som3_1, output_ext="ctof", extra_tag=dataset_type, data_ext=conf.ext_replacement, path_replacement=conf.path_replacement, verbose=conf.verbose, message="combined calibrated TOF information") del dp_som3_1 return dp_som3
def run(config, tim=None): """ This method is where the data reduction process gets done. @param config: Object containing the data reduction configuration information. @type config: L{hlr_utils.Configure} @param tim: (OPTIONAL) Object that will allow the method to perform timing evaluations. @type tim: C{sns_time.DiffTime} """ import common_lib import dr_lib import DST if tim is not None: tim.getTime(False) old_time = tim.getOldTime() if config.data is None: raise RuntimeError("Need to pass a data filename to the driver "\ +"script.") # Read in geometry if one is provided if config.inst_geom is not None: if config.verbose: print "Reading in instrument geometry file" inst_geom_dst = DST.getInstance("application/x-NxsGeom", config.inst_geom) else: inst_geom_dst = None # Perform early background subtraction if the hwfix flag is used if config.hwfix: if not config.mc: so_axis = "time_of_flight" else: so_axis = "Time_of_Flight" bkg_som0 = dr_lib.add_files(config.back, Data_Paths=config.data_paths.toPath(), SO_Axis=so_axis, Signal_ROI=config.roi_file, dataset_type="background", Verbose=config.verbose, Timer=tim) bkg_som = dr_lib.fix_bin_contents(bkg_som0) del bkg_som0 else: bkg_som = None # Perform Steps 1-15 on sample data d_som1 = dr_lib.process_igs_data(config.data, config, timer=tim, inst_geom_dst=inst_geom_dst, tib_const=config.tib_data_const, bkg_som=bkg_som) # Perform Steps 1-15 on empty can data if config.ecan is not None: e_som1 = dr_lib.process_igs_data(config.ecan, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="empty_can", tib_const=config.tib_ecan_const, bkg_som=bkg_som) else: e_som1 = None # Perform Steps 1-15 on normalization data if config.norm is not None: n_som1 = dr_lib.process_igs_data(config.norm, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="normalization", tib_const=config.tib_norm_const, bkg_som=bkg_som) else: n_som1 = None # Perform Steps 1-15 on background data if config.back is not None: b_som1 = dr_lib.process_igs_data(config.back, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="background", tib_const=config.tib_back_const, bkg_som=bkg_som) else: b_som1 = None # Perform Step 1-15 on direct scattering background data if config.dsback is not None: ds_som1 = dr_lib.process_igs_data(config.dsback, config, timer=tim, inst_geom_dst=inst_geom_dst, tib_const=config.tib_dsback_const, dataset_type="dsbackground", bkg_som=bkg_som) # Note: time_zero_slope MUST be a tuple if config.time_zero_slope is not None: ds_som1.attr_list["Time_zero_slope"] = \ config.time_zero_slope.toValErrTuple() # Note: time_zero_offset MUST be a tuple if config.time_zero_offset is not None: ds_som1.attr_list["Time_zero_offset"] = \ config.time_zero_offset.toValErrTuple() # Step 16: Linearly interpolate TOF elastic range in direct scattering # background data # First convert TOF elastic range to appropriate pixel initial # wavelengths if config.verbose: print "Determining initial wavelength range for elastic line" if tim is not None: tim.getTime(False) if config.tof_elastic is None: # Units are in microseconds tof_elastic_range = (140300, 141300) else: tof_elastic_range = config.tof_elastic ctof_elastic_low = dr_lib.convert_single_to_list(\ "tof_to_initial_wavelength_igs_lin_time_zero", (tof_elastic_range[0], 0.0), ds_som1) ctof_elastic_high = dr_lib.convert_single_to_list(\ "tof_to_initial_wavelength_igs_lin_time_zero", (tof_elastic_range[1], 0.0), ds_som1) ctof_elastic_range = [(ctof_elastic_low[i][0], ctof_elastic_high[i][0]) for i in xrange(len(ctof_elastic_low))] if tim is not None: tim.getTime(msg="After calculating initial wavelength range for "\ +"elastic line ") del ctof_elastic_low, ctof_elastic_high if config.split: lambda_filter = [(d_som1[i].axis[0].val[0], d_som1[i].axis[0].val[-1]) for i in xrange(len(d_som1))] else: lambda_filter = None # Now interpolate spectra between TOF elastic range (converted to # initial wavelength) if config.verbose: print "Linearly interpolating direct scattering spectra" if tim is not None: tim.getTime(False) ds_som2 = dr_lib.lin_interpolate_spectra(ds_som1, ctof_elastic_range, filter_axis=lambda_filter) if tim is not None: tim.getTime(msg="After linearly interpolating direct scattering "\ +"spectra ") if config.dump_dslin: ds_som2_1 = dr_lib.sum_all_spectra(ds_som2,\ rebin_axis=config.lambda_bins.toNessiList()) hlr_utils.write_file(config.output, "text/Spec", ds_som2_1, output_ext="lin", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="dsbackground linear interpolation") del ds_som2_1 del ds_som1 else: ds_som2 = None if inst_geom_dst is not None: inst_geom_dst.release_resource() # Steps 17-18: Subtract background spectrum from sample spectrum if config.dsback is None: back_som = b_som1 bkg_type = "background" else: back_som = ds_som2 bkg_type = "dsbackground" d_som2 = dr_lib.subtract_bkg_from_data(d_som1, back_som, verbose=config.verbose, timer=tim, dataset1="data", dataset2=bkg_type, scale=config.scale_bs) if config.dsback is not None: del ds_som2 # Step 19: Zero region outside TOF elastic for background for empty can if config.dsback is None: bcs_som = b_som1 cs_som = e_som1 else: if config.verbose and b_som1 is not None: print "Zeroing background spectra" if tim is not None and b_som1 is not None: tim.getTime(False) bcs_som = dr_lib.zero_spectra(b_som1, ctof_elastic_range) if tim is not None and b_som1 is not None: tim.getTime(msg="After zeroing background spectra") if config.verbose and e_som1 is not None: print "Zeroing empty can spectra" if tim is not None and e_som1 is not None: tim.getTime(False) cs_som = dr_lib.zero_spectra(e_som1, ctof_elastic_range) if tim is not None and e_som1 is not None: tim.getTime(msg="After zeroing empty can spectra") del ctof_elastic_range # Steps 20-21: Subtract background spectrum from empty can spectrum e_som2 = dr_lib.subtract_bkg_from_data(cs_som, bcs_som, verbose=config.verbose, timer=tim, dataset1="data-empty_can", dataset2="background", scale=config.scale_bcs) # Steps 22-23: Subtract background spectrum from empty can spectrum for # normalization try: config.pre_norm except AttributeError: config.pre_norm = False if not config.pre_norm: e_som3 = dr_lib.subtract_bkg_from_data(e_som1, b_som1, verbose=config.verbose, timer=tim, dataset1="norm-empty_can", dataset2="background", scale=config.scale_bcn) else: e_som3 = None # Steps 24-25: Subtract background spectrum from normalization spectrum if not config.pre_norm: n_som2 = dr_lib.subtract_bkg_from_data(n_som1, b_som1, verbose=config.verbose, timer=tim, dataset1="normalization", dataset2="background", scale=config.scale_bn) else: n_som2 = n_som1 del b_som1, e_som1, bcs_som, cs_som # Steps 26-27: Subtract empty can spectrum from sample spectrum d_som3 = dr_lib.subtract_bkg_from_data(d_som2, e_som2, verbose=config.verbose, timer=tim, dataset1="data", dataset2="empty_can", scale=config.scale_cs) del d_som2, e_som2 # Steps 28-29: Subtract empty can spectrum from normalization spectrum if not config.pre_norm: n_som3 = dr_lib.subtract_bkg_from_data(n_som2, e_som3, verbose=config.verbose, timer=tim, dataset1="normalization", dataset2="empty_can", scale=config.scale_cn) else: n_som3 = n_som2 del n_som2, e_som3 # Step 30-31: Integrate normalization spectra if config.verbose and n_som3 is not None and not config.pre_norm: print "Integrating normalization spectra" if not config.pre_norm: norm_int = dr_lib.integrate_spectra(n_som3, start=config.norm_start, end=config.norm_end, norm=True) else: norm_int = n_som3 del n_som3 # Step 32: Normalize data by integrated values if config.verbose and norm_int is not None: print "Normalizing data by normalization data" if norm_int is not None: d_som4 = common_lib.div_ncerr(d_som3, norm_int) else: d_som4 = d_som3 if norm_int is not None: if tim is not None: tim.getTime(msg="After normalizing data ") del d_som3, norm_int if config.dump_norm: if tim is not None: tim.getTime(False) hlr_utils.write_file(config.output, "text/Spec", d_som4, output_ext="wvn", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="wavelength (vanadium norm) information") if tim is not None: tim.getTime(msg="After writing wavelength (vanadium norm) info ") # Steps 33 to end: Creating S(Q,E) if config.Q_bins is not None: if config.verbose: print "Creating 2D spectrum" if tim is not None: tim.getTime(False) d_som5 = dr_lib.create_E_vs_Q_igs(d_som4, config.E_bins.toNessiList(), config.Q_bins.toNessiList(), so_id="Full Detector", y_label="counts", y_units="counts / (ueV * A^-1)", x_labels=["Q transfer", "energy transfer"], x_units=["1/Angstroms","ueV"], split=config.split, Q_filter=False, configure=config) if tim is not None: tim.getTime(msg="After creation of final spectrum ") del d_som4 # Steps 33 to 36: Create S(-cos(polar), E) elif config.ncospol_bins is not None: if config.verbose: print "Convert wavelength to energy transfer" if tim is not None: tim.getTime(False) d_som4a = dr_lib.energy_transfer(d_som4, "IGS", "Wavelength_final", sa_norm=True, scale=True, change_units=True) if tim is not None: tim.getTime(msg="After wavelength to energy transfer conversion ") del d_som4 if config.verbose: print "Creating 2D spectrum" if tim is not None: tim.getTime(False) d_som5 = dr_lib.create_param_vs_Y(d_som4a, "polar", "negcos_param_array", config.ncospol_bins.toNessiList(), rebin_axis=config.E_bins.toNessiList(), y_label="counts", y_units="counts / ueV", x_labels=["-cos(polar)", "Energy Transfer"], x_units=["", "ueV"]) if tim is not None: tim.getTime(msg="After creation of final spectrum ") # If rescaling factor present, rescale the data if config.rescale_final is not None and not config.split: d_som6 = common_lib.mult_ncerr(d_som5, (config.rescale_final, 0.0)) else: d_som6 = d_som5 if tim is None: old_time = None if not __name__ == "amorphous_reduction_sqe": del d_som5 __write_output(d_som6, config, tim, old_time) else: if config.create_output: del d_som5 __write_output(d_som6, config, tim, old_time) else: return d_som6
def calibrate_dgs_data(datalist, conf, dkcur, **kwargs): """ This function combines Steps 3 through 6 in Section 2.1.1 of the data reduction process for Direct Geometry Spectrometers as specified by the document at U{http://neutrons.ornl.gov/asg/projects/SCL/reqspec/DR_Lib_RS.doc}. The function takes a list of file names, a L{hlr_utils.Configure} object and processes the data accordingly. @param datalist: A list containing the filenames of the data to be processed. @type datalist: C{list} of C{string}s @param conf: Object that contains the current setup of the driver. @type conf: L{hlr_utils.Configure} @param dkcur: The object containing the TOF dark current data. @type dkcur: C{SOM.SOM} @param kwargs: A list of keyword arguments that the function accepts: @keyword inst_geom_dst: File object that contains instrument geometry information. @type inst_geom_dst: C{DST.GeomDST} @keyword tib_const: A time-independent background constant to subtract from every pixel. @type tib_const: L{hlr_utils.DrParameter} @keyword dataset_type: The practical name of the dataset being processed. The default value is I{data}. @type dataset_type: C{string} @keyword cwp: A list of chopper phase corrections in units of microseconds. @type cwp: C{list} of C{float}s @keyword timer: Timing object so the function can perform timing estimates. @type timer: C{sns_timer.DiffTime} @return: Object that has undergone all requested processing steps @rtype: C{SOM.SOM} """ import common_lib import dr_lib import hlr_utils # Check keywords try: tib_const = kwargs["tib_const"] except KeyError: tib_const = None try: dataset_type = kwargs["dataset_type"] except KeyError: dataset_type = "data" try: t = kwargs["timer"] except KeyError: t = None try: i_geom_dst = kwargs["inst_geom_dst"] except KeyError: i_geom_dst = None dataset_cwp = kwargs.get("cwp") # Open the appropriate datafiles if conf.verbose: print "Reading %s file" % dataset_type data_paths = conf.data_paths.toPath() if conf.no_mon_norm: mon_paths = None else: mon_paths = conf.usmon_path.toPath() # Check for mask file since normalization drive doesn't understand option try: mask_file = conf.mask_file except AttributeError: mask_file = None if t is not None: oldtime = t.getOldTime() (dp_som0, dm_som0) = dr_lib.add_files_dm(datalist, Data_Paths=data_paths, Mon_Paths=mon_paths, SO_Axis=conf.so_axis, Signal_ROI=conf.roi_file, Signal_MASK=mask_file, dataset_type=dataset_type, dataset_cwp=dataset_cwp, Verbose=conf.verbose, Timer=t) if t is not None: t.setOldTime(oldtime) t.getTime(msg="After reading %s file" % dataset_type) # Cut the spectra if necessary dp_somA = dr_lib.cut_spectra(dp_som0, conf.tof_cut_min, conf.tof_cut_max) del dp_som0 dp_somB = dr_lib.fix_bin_contents(dp_somA) del dp_somA if dp_somB.attr_list.instrument.get_name() != "CNCS": if conf.verbose: print "Cutting spectrum at minimum TOF" if t is not None: t.getTime(False) # Calculate minimum TOF for physical neutrons if conf.initial_energy is not None: initial_wavelength = common_lib.energy_to_wavelength(\ conf.initial_energy.toValErrTuple()) initial_velocity = common_lib.wavelength_to_velocity(\ initial_wavelength) else: # This should actually calculate it, but don't have a way right now pass if conf.time_zero_offset is not None: time_zero_offset = conf.time_zero_offset.toValErrTuple() else: # This should actually calculate it, but don't have a way right now time_zero_offset = (0.0, 0.0) ss_length = dp_somB.attr_list.instrument.get_primary() tof_min = (ss_length[0] / initial_velocity[0]) + time_zero_offset[0] # Cut all spectra a the minimum TOF dp_som1 = dr_lib.cut_spectra(dp_somB, tof_min, None) if t is not None: t.getTime(msg="After cutting spectrum at minimum TOF ") else: dp_som1 = dp_somB del dp_somB if dm_som0 is not None: dm_som1 = dr_lib.fix_bin_contents(dm_som0) else: dm_som1 = dm_som0 del dm_som0 # Override geometry if necessary if conf.inst_geom is not None: i_geom_dst.setGeometry(data_paths, dp_som1) if conf.inst_geom is not None and dm_som1 is not None: i_geom_dst.setGeometry(mon_paths, dm_som1) # Step 3: Integrate the upstream monitor if dm_som1 is not None: if conf.verbose: print "Integrating upstream monitor spectrum" if t is not None: t.getTime(False) if conf.mon_int_range is None: start_val = float("inf") end_val = float("inf") else: start_val = conf.mon_int_range[0] end_val = conf.mon_int_range[1] dm_som2 = dr_lib.integrate_spectra(dm_som1, start=start_val, end=end_val, width=True) if t is not None: t.getTime(msg="After integrating upstream monitor spectrum ") else: dm_som2 = dm_som1 del dm_som1 tib_norm_const = None # Step 4: Divide data set by summed monitor spectrum if dm_som2 is not None: if conf.verbose: print "Normalizing %s by monitor sum" % dataset_type if t is not None: t.getTime(False) dp_som2 = common_lib.div_ncerr(dp_som1, dm_som2, length_one_som=True) tib_norm_const = dm_som2[0].y if t is not None: t.getTime(msg="After normalizing %s by monitor sum" % dataset_type) elif conf.pc_norm: if conf.verbose: print "Normalizing %s by proton charge" % dataset_type pc_tag = dataset_type + "-proton_charge" pc = dp_som1.attr_list[pc_tag] # Scale the proton charge and then set the scale PC back to attributes if conf.scale_pc is not None: if conf.verbose: print "Scaling %s proton charge" % dataset_type pc = hlr_utils.scale_proton_charge(pc, conf.scale_pc) dp_som1.attr_list[pc_tag] = pc tib_norm_const = pc.getValue() if t is not None: t.getTime(False) dp_som2 = common_lib.div_ncerr(dp_som1, (pc.getValue(), 0.0)) if t is not None: t.getTime(msg="After normalizing %s by proton charge" \ % dataset_type) else: dp_som2 = dp_som1 del dp_som1, dm_som2 # Step 5: Scale dark current by data set measurement time if dkcur is not None: if conf.verbose: print "Scaling dark current by %s acquisition time" % dataset_type if t is not None: t.getTime(False) dstime_tag = dataset_type + "-duration" dstime = dp_som2.attr_list[dstime_tag] dkcur1 = common_lib.div_ncerr(dkcur, (dstime.getValue(), 0.0)) if t is not None: t.getTime(msg="After scaling dark current by %s acquisition time" \ % dataset_type) else: dkcur1 = dkcur del dkcur # Step 6: Subtract scaled dark current from data set if dkcur1 is not None: if conf.verbose: print "Subtracting %s by scaled dark current" % dataset_type if t is not None: t.getTime(False) dp_som3 = common_lib.sub_ncerr(dp_som2, dkcur1) if t is not None: t.getTime(msg="After subtracting %s by scaled dark current" \ % dataset_type) elif tib_const is not None and dkcur1 is None: if conf.verbose: print "Subtracting TIB constant from %s" % dataset_type # Normalize the TIB constant by dividing by the current normalization # the duration (if necessary) and the conversion from seconds to # microseconds tib_c = tib_const.toValErrTuple() conv_sec_to_usec = 1.0e-6 if tib_norm_const is None: tib_norm_const = 1 duration = 1 else: duration_tag = dataset_type + "-duration" duration = dp_som2.attr_list[duration_tag].getValue() norm_const = (duration * conv_sec_to_usec) / tib_norm_const tib_val = tib_c[0] * norm_const tib_err2 = tib_c[1] * (norm_const * norm_const) if t is not None: t.getTime(False) dp_som3 = common_lib.sub_ncerr(dp_som2, (tib_val, tib_err2)) if t is not None: t.getTime(msg="After subtracting TIB constant from %s" \ % dataset_type) elif conf.tib_range is not None and dkcur1 is None: if conf.verbose: print "Determining TIB constant from %s" % dataset_type if t is not None: t.getTime(False) TIB = dr_lib.determine_time_indep_bkg(dp_som2, conf.tib_range, is_range=True) if t is not None: t.getTime(msg="After determining TIB constant from %s" \ % dataset_type) if conf.dump_tib: file_comment = "TIB TOF Range: [%d, %d]" % (conf.tib_range[0], conf.tib_range[1]) hlr_utils.write_file(conf.output, "text/num-info", TIB, output_ext="tib", extra_tag=dataset_type, verbose=conf.verbose, data_ext=conf.ext_replacement, path_replacement=conf.path_replacement, message="time-independent background "\ +"information", tag="Average TIB", units="counts/usec", comments=[file_comment]) if conf.verbose: print "Subtracting TIB constant from %s" % dataset_type if t is not None: t.getTime(False) dp_som3 = common_lib.sub_ncerr(dp_som2, TIB) if t is not None: t.getTime(msg="After subtracting TIB constant from %s" \ % dataset_type) del TIB else: dp_som3 = dp_som2 del dp_som2, dkcur1 if conf.dump_ctof_comb: dp_som3_1 = dr_lib.sum_all_spectra(dp_som3) hlr_utils.write_file(conf.output, "text/Spec", dp_som3_1, output_ext="ctof", extra_tag=dataset_type, data_ext=conf.ext_replacement, path_replacement=conf.path_replacement, verbose=conf.verbose, message="combined calibrated TOF information") del dp_som3_1 return dp_som3
def run(config, tim=None): """ This method is where the data reduction process gets done. @param config: Object containing the data reduction configuration information. @type config: L{hlr_utils.Configure} @param tim: (OPTIONAL) Object that will allow the method to perform timing evaluations. @type tim: C{sns_time.DiffTime} """ import common_lib import dr_lib import DST if tim is not None: tim.getTime(False) old_time = tim.getOldTime() if config.data is None: raise RuntimeError("Need to pass a data filename to the driver "\ +"script.") # Read in geometry if one is provided if config.inst_geom is not None: if config.verbose: print "Reading in instrument geometry file" inst_geom_dst = DST.getInstance("application/x-NxsGeom", config.inst_geom) else: inst_geom_dst = None # Perform early background subtraction if the hwfix flag is used if config.hwfix: if not config.mc: so_axis = "time_of_flight" else: so_axis = "Time_of_Flight" bkg_som0 = dr_lib.add_files(config.back, Data_Paths=config.data_paths.toPath(), SO_Axis=so_axis, Signal_ROI=config.roi_file, dataset_type="background", Verbose=config.verbose, Timer=tim) bkg_som = dr_lib.fix_bin_contents(bkg_som0) del bkg_som0 else: bkg_som = None # Perform Steps 1-15 on sample data d_som1 = dr_lib.process_igs_data(config.data, config, timer=tim, inst_geom_dst=inst_geom_dst, tib_const=config.tib_data_const, bkg_som=bkg_som) # Perform Steps 1-15 on empty can data if config.ecan is not None: e_som1 = dr_lib.process_igs_data(config.ecan, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="empty_can", tib_const=config.tib_ecan_const, bkg_som=bkg_som) else: e_som1 = None # Perform Steps 1-15 on normalization data if config.norm is not None: n_som1 = dr_lib.process_igs_data(config.norm, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="normalization", tib_const=config.tib_norm_const, bkg_som=bkg_som) else: n_som1 = None # Perform Steps 1-15 on background data if config.back is not None: b_som1 = dr_lib.process_igs_data(config.back, config, timer=tim, inst_geom_dst=inst_geom_dst, dataset_type="background", tib_const=config.tib_back_const, bkg_som=bkg_som) else: b_som1 = None # Perform Step 1-15 on direct scattering background data if config.dsback is not None: ds_som1 = dr_lib.process_igs_data(config.dsback, config, timer=tim, inst_geom_dst=inst_geom_dst, tib_const=config.tib_dsback_const, dataset_type="dsbackground", bkg_som=bkg_som) # Note: time_zero_slope MUST be a tuple if config.time_zero_slope is not None: ds_som1.attr_list["Time_zero_slope"] = \ config.time_zero_slope.toValErrTuple() # Note: time_zero_offset MUST be a tuple if config.time_zero_offset is not None: ds_som1.attr_list["Time_zero_offset"] = \ config.time_zero_offset.toValErrTuple() # Step 16: Linearly interpolate TOF elastic range in direct scattering # background data # First convert TOF elastic range to appropriate pixel initial # wavelengths if config.verbose: print "Determining initial wavelength range for elastic line" if tim is not None: tim.getTime(False) if config.tof_elastic is None: # Units are in microseconds tof_elastic_range = (140300, 141300) else: tof_elastic_range = config.tof_elastic ctof_elastic_low = dr_lib.convert_single_to_list(\ "tof_to_initial_wavelength_igs_lin_time_zero", (tof_elastic_range[0], 0.0), ds_som1) ctof_elastic_high = dr_lib.convert_single_to_list(\ "tof_to_initial_wavelength_igs_lin_time_zero", (tof_elastic_range[1], 0.0), ds_som1) ctof_elastic_range = [(ctof_elastic_low[i][0], ctof_elastic_high[i][0]) for i in xrange(len(ctof_elastic_low))] if tim is not None: tim.getTime(msg="After calculating initial wavelength range for "\ +"elastic line ") del ctof_elastic_low, ctof_elastic_high if config.split: lambda_filter = [(d_som1[i].axis[0].val[0], d_som1[i].axis[0].val[-1]) for i in xrange(len(d_som1))] else: lambda_filter = None # Now interpolate spectra between TOF elastic range (converted to # initial wavelength) if config.verbose: print "Linearly interpolating direct scattering spectra" if tim is not None: tim.getTime(False) ds_som2 = dr_lib.lin_interpolate_spectra(ds_som1, ctof_elastic_range, filter_axis=lambda_filter) if tim is not None: tim.getTime(msg="After linearly interpolating direct scattering "\ +"spectra ") if config.dump_dslin: ds_som2_1 = dr_lib.sum_all_spectra(ds_som2,\ rebin_axis=config.lambda_bins.toNessiList()) hlr_utils.write_file(config.output, "text/Spec", ds_som2_1, output_ext="lin", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="dsbackground linear interpolation") del ds_som2_1 del ds_som1 else: ds_som2 = None if inst_geom_dst is not None: inst_geom_dst.release_resource() # Steps 17-18: Subtract background spectrum from sample spectrum if config.dsback is None: back_som = b_som1 bkg_type = "background" else: back_som = ds_som2 bkg_type = "dsbackground" d_som2 = dr_lib.subtract_bkg_from_data(d_som1, back_som, verbose=config.verbose, timer=tim, dataset1="data", dataset2=bkg_type, scale=config.scale_bs) if config.dsback is not None: del ds_som2 # Step 19: Zero region outside TOF elastic for background for empty can if config.dsback is None: bcs_som = b_som1 cs_som = e_som1 else: if config.verbose and b_som1 is not None: print "Zeroing background spectra" if tim is not None and b_som1 is not None: tim.getTime(False) bcs_som = dr_lib.zero_spectra(b_som1, ctof_elastic_range) if tim is not None and b_som1 is not None: tim.getTime(msg="After zeroing background spectra") if config.verbose and e_som1 is not None: print "Zeroing empty can spectra" if tim is not None and e_som1 is not None: tim.getTime(False) cs_som = dr_lib.zero_spectra(e_som1, ctof_elastic_range) if tim is not None and e_som1 is not None: tim.getTime(msg="After zeroing empty can spectra") del ctof_elastic_range # Steps 20-21: Subtract background spectrum from empty can spectrum e_som2 = dr_lib.subtract_bkg_from_data(cs_som, bcs_som, verbose=config.verbose, timer=tim, dataset1="data-empty_can", dataset2="background", scale=config.scale_bcs) # Steps 22-23: Subtract background spectrum from empty can spectrum for # normalization try: config.pre_norm except AttributeError: config.pre_norm = False if not config.pre_norm: e_som3 = dr_lib.subtract_bkg_from_data(e_som1, b_som1, verbose=config.verbose, timer=tim, dataset1="norm-empty_can", dataset2="background", scale=config.scale_bcn) else: e_som3 = None # Steps 24-25: Subtract background spectrum from normalization spectrum if not config.pre_norm: n_som2 = dr_lib.subtract_bkg_from_data(n_som1, b_som1, verbose=config.verbose, timer=tim, dataset1="normalization", dataset2="background", scale=config.scale_bn) else: n_som2 = n_som1 del b_som1, e_som1, bcs_som, cs_som # Steps 26-27: Subtract empty can spectrum from sample spectrum d_som3 = dr_lib.subtract_bkg_from_data(d_som2, e_som2, verbose=config.verbose, timer=tim, dataset1="data", dataset2="empty_can", scale=config.scale_cs) del d_som2, e_som2 # Steps 28-29: Subtract empty can spectrum from normalization spectrum if not config.pre_norm: n_som3 = dr_lib.subtract_bkg_from_data(n_som2, e_som3, verbose=config.verbose, timer=tim, dataset1="normalization", dataset2="empty_can", scale=config.scale_cn) else: n_som3 = n_som2 del n_som2, e_som3 # Step 30-31: Integrate normalization spectra if config.verbose and n_som3 is not None and not config.pre_norm: print "Integrating normalization spectra" if not config.pre_norm: norm_int = dr_lib.integrate_spectra(n_som3, start=config.norm_start, end=config.norm_end, norm=True) else: norm_int = n_som3 del n_som3 # Step 32: Normalize data by integrated values if config.verbose and norm_int is not None: print "Normalizing data by normalization data" if norm_int is not None: d_som4 = common_lib.div_ncerr(d_som3, norm_int) else: d_som4 = d_som3 if norm_int is not None: if tim is not None: tim.getTime(msg="After normalizing data ") del d_som3, norm_int if config.dump_norm: if tim is not None: tim.getTime(False) hlr_utils.write_file(config.output, "text/Spec", d_som4, output_ext="wvn", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="wavelength (vanadium norm) information") if tim is not None: tim.getTime(msg="After writing wavelength (vanadium norm) info ") # Steps 33 to end: Creating S(Q,E) if config.Q_bins is not None: if config.verbose: print "Creating 2D spectrum" if tim is not None: tim.getTime(False) d_som5 = dr_lib.create_E_vs_Q_igs( d_som4, config.E_bins.toNessiList(), config.Q_bins.toNessiList(), so_id="Full Detector", y_label="counts", y_units="counts / (ueV * A^-1)", x_labels=["Q transfer", "energy transfer"], x_units=["1/Angstroms", "ueV"], split=config.split, Q_filter=False, configure=config) if tim is not None: tim.getTime(msg="After creation of final spectrum ") del d_som4 # Steps 33 to 36: Create S(-cos(polar), E) elif config.ncospol_bins is not None: if config.verbose: print "Convert wavelength to energy transfer" if tim is not None: tim.getTime(False) d_som4a = dr_lib.energy_transfer(d_som4, "IGS", "Wavelength_final", sa_norm=True, scale=True, change_units=True) if tim is not None: tim.getTime(msg="After wavelength to energy transfer conversion ") del d_som4 if config.verbose: print "Creating 2D spectrum" if tim is not None: tim.getTime(False) d_som5 = dr_lib.create_param_vs_Y( d_som4a, "polar", "negcos_param_array", config.ncospol_bins.toNessiList(), rebin_axis=config.E_bins.toNessiList(), y_label="counts", y_units="counts / ueV", x_labels=["-cos(polar)", "Energy Transfer"], x_units=["", "ueV"]) if tim is not None: tim.getTime(msg="After creation of final spectrum ") # If rescaling factor present, rescale the data if config.rescale_final is not None and not config.split: d_som6 = common_lib.mult_ncerr(d_som5, (config.rescale_final, 0.0)) else: d_som6 = d_som5 if tim is None: old_time = None if not __name__ == "amorphous_reduction_sqe": del d_som5 __write_output(d_som6, config, tim, old_time) else: if config.create_output: del d_som5 __write_output(d_som6, config, tim, old_time) else: return d_som6
def scaled_summed_data(datalist, conf, **kwargs): """ This function takes a list of TOF datafiles, creates the dataset representation, integrates each pixel spectrum and then scales those integrations with the acquisition time. @param datalist: A list containing the filenames of the data to be processed. @type datalist: C{list} of C{string}s @param conf: Object that contains the current setup of the driver. @type conf: L{hlr_utils.Configure} @param kwargs: A list of keyword arguments that the function accepts: @keyword dataset_type: The practical name of the dataset being processed. The default value is I{data}. @type dataset_type: C{string} @keyword timer: Timing object so the function can perform timing estimates. @type timer: C{sns_timer.DiffTime} @return: Object that has undergone all requested processing steps @rtype: C{SOM.SOM} """ # Kick-out is no data list is present if datalist is None: return None import common_lib import dr_lib # Check keywords try: dataset_type = kwargs["dataset_type"] except KeyError: dataset_type = "data" try: t = kwargs["timer"] except KeyError: t = None # Open the appropriate datafiles if conf.verbose: print "Reading %s file" % dataset_type dst_type = "application/x-NeXus" data_paths = conf.data_paths.toPath() dp_som0 = dr_lib.add_files(datalist, Data_Paths=data_paths, SO_Axis=conf.so_axis, Signal_ROI=conf.roi_file, Signal_MASK=conf.mask_file, dataset_type=dataset_type, dst_type=dst_type, Verbose=conf.verbose, Timer=t) if t is not None: t.getTime(msg="After reading %s " % dataset_type) dp_som1 = dr_lib.fix_bin_contents(dp_som0) del dp_som0 if conf.verbose: print "Integrating %s data" % dataset_type if t is not None: t.getTime(False) dp_som2 = dr_lib.integrate_spectra(dp_som1, width=True) if t is not None: t.getTime(msg="After integrating %s data " % dataset_type) del dp_som1 # Make dataset duration tag duration_tag = dataset_type+"-duration" duration = dp_som2.attr_list[duration_tag] if conf.verbose: print "Scaling %s integration by acquisition duration " % dataset_type if t is not None: t.getTime(False) dp_som3 = common_lib.div_ncerr(dp_som2, (duration.getValue(), 0.0)) if t is not None: t.getTime("After scaling %s integration by acquisition duration " \ % dataset_type) del dp_som2 return dp_som3
def run(config, tim=None): """ This method is where the data reduction process gets done. @param config: Object containing the data reduction configuration information. @type config: L{hlr_utils.Configure} @param tim: (OPTIONAL) Object that will allow the method to perform timing evaluations. @type tim: C{sns_time.DiffTime} """ import common_lib import dr_lib import DST if tim is not None: tim.getTime(False) old_time = tim.getOldTime() if config.data is None: raise RuntimeError("Need to pass a data filename to the driver " + "script.") # Read in geometry if one is provided if config.inst_geom is not None: if config.verbose: print "Reading in instrument geometry file" inst_geom_dst = DST.getInstance("application/x-NxsGeom", config.inst_geom) else: inst_geom_dst = None config.so_axis = "time_of_flight" # Steps 1-3: Produce a scaled summed dark current dataset dc_som = dr_lib.scaled_summed_data(config.dkcur, config, dataset_type="dark_current", timer=tim) # Perform Steps 3-6 on black can data if config.bcan is not None: b_som1 = dr_lib.calibrate_dgs_data( config.bcan, config, dc_som, dataset_type="black_can", inst_geom_dst=inst_geom_dst, tib_const=config.tib_const, cwp=config.cwp_bcan, timer=tim, ) else: b_som1 = None # Perform Steps 3-6 on empty can data if config.ecan is not None: e_som1 = dr_lib.calibrate_dgs_data( config.ecan, config, dc_som, dataset_type="empty_can", inst_geom_dst=inst_geom_dst, tib_const=config.tib_const, cwp=config.cwp_ecan, timer=tim, ) else: e_som1 = None # Perform Steps 3-6 on normalization data n_som1 = dr_lib.calibrate_dgs_data( config.data, config, dc_som, dataset_type="normalization", inst_geom_dst=inst_geom_dst, tib_const=config.tib_const, cwp=config.cwp_data, timer=tim, ) # Perform Steps 7-16 on normalization data if config.norm_trans_coeff is None: norm_trans_coeff = None else: norm_trans_coeff = config.norm_trans_coeff.toValErrTuple() # Determine if we need to rebin the empty or black can data if config.ecan is not None and e_som1 is not None: ecan_cwp = True else: ecan_cwp = False if config.bcan is not None and b_som1 is not None: bcan_cwp = True else: bcan_cwp = False cwp_used = ecan_cwp or bcan_cwp n_som2 = dr_lib.process_dgs_data( n_som1, config, b_som1, e_som1, norm_trans_coeff, dataset_type="normalization", cwp_used=cwp_used, timer=tim ) del n_som1, b_som1, e_som1 # Step 17: Integrate normalization spectra if config.verbose: print "Integrating normalization spectra" if tim is not None: tim.getTime(False) if config.norm_int_range is None: start_val = float("inf") end_val = float("inf") else: if not config.wb_norm: # Translate energy transfer to final energy ef_start = config.initial_energy.getValue() - config.norm_int_range[0] ef_end = config.initial_energy.getValue() - config.norm_int_range[1] # Convert final energy to final wavelength start_val = common_lib.energy_to_wavelength((ef_start, 0.0))[0] end_val = common_lib.energy_to_wavelength((ef_end, 0.0))[0] else: start_val = config.norm_int_range[0] end_val = config.norm_int_range[1] n_som3 = dr_lib.integrate_spectra(n_som2, start=start_val, end=end_val, width=True) del n_som2 if tim is not None: tim.getTime(msg="After integrating normalization spectra ") file_comment = "Normalization Integration range: %0.3fA, %0.3fA" % (start_val, end_val) hlr_utils.write_file( config.output, "text/num-info", n_som3, output_ext="norm", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="normalization values", comments=[file_comment], tag="Integral", units="counts", ) if tim is not None: tim.getTime(False) if config.verbose: print "Making mask file" # Make mask file from threshold dr_lib.filter_normalization(n_som3, config.lo_threshold, config.hi_threshold, config) if tim is not None: tim.getTime(msg="After making mask file ") # Write out RMD file n_som3.attr_list["config"] = config hlr_utils.write_file( config.output, "text/rmd", n_som3, output_ext="rmd", data_ext=config.ext_replacement, path_replacement=config.path_replacement, verbose=config.verbose, message="metadata", ) if tim is not None: tim.setOldTime(old_time) tim.getTime(msg="Total Running Time")
def scaled_summed_data(datalist, conf, **kwargs): """ This function takes a list of TOF datafiles, creates the dataset representation, integrates each pixel spectrum and then scales those integrations with the acquisition time. @param datalist: A list containing the filenames of the data to be processed. @type datalist: C{list} of C{string}s @param conf: Object that contains the current setup of the driver. @type conf: L{hlr_utils.Configure} @param kwargs: A list of keyword arguments that the function accepts: @keyword dataset_type: The practical name of the dataset being processed. The default value is I{data}. @type dataset_type: C{string} @keyword timer: Timing object so the function can perform timing estimates. @type timer: C{sns_timer.DiffTime} @return: Object that has undergone all requested processing steps @rtype: C{SOM.SOM} """ # Kick-out is no data list is present if datalist is None: return None import common_lib import dr_lib # Check keywords try: dataset_type = kwargs["dataset_type"] except KeyError: dataset_type = "data" try: t = kwargs["timer"] except KeyError: t = None # Open the appropriate datafiles if conf.verbose: print "Reading %s file" % dataset_type dst_type = "application/x-NeXus" data_paths = conf.data_paths.toPath() dp_som0 = dr_lib.add_files(datalist, Data_Paths=data_paths, SO_Axis=conf.so_axis, Signal_ROI=conf.roi_file, Signal_MASK=conf.mask_file, dataset_type=dataset_type, dst_type=dst_type, Verbose=conf.verbose, Timer=t) if t is not None: t.getTime(msg="After reading %s " % dataset_type) dp_som1 = dr_lib.fix_bin_contents(dp_som0) del dp_som0 if conf.verbose: print "Integrating %s data" % dataset_type if t is not None: t.getTime(False) dp_som2 = dr_lib.integrate_spectra(dp_som1, width=True) if t is not None: t.getTime(msg="After integrating %s data " % dataset_type) del dp_som1 # Make dataset duration tag duration_tag = dataset_type + "-duration" duration = dp_som2.attr_list[duration_tag] if conf.verbose: print "Scaling %s integration by acquisition duration " % dataset_type if t is not None: t.getTime(False) dp_som3 = common_lib.div_ncerr(dp_som2, (duration.getValue(), 0.0)) if t is not None: t.getTime("After scaling %s integration by acquisition duration " \ % dataset_type) del dp_som2 return dp_som3