def filter_normalization(obj, lothreshold, hithreshold, config=None): """ This function takes an object with normalization integration information and a threshold and creates a mask file containing the pixel IDs that do not make it above the threshold. @param obj: The object containing the normalization information @type obj: C{SOM.SOM} @param lothreshold: The value below which a pixel will be masked. @type lothreshold: C{float} @param hithreshold: The value above which a pixel will be masked. @type hithreshold: C{float} @param config: The object holding the DR configuration @type config: L{hlr_utils.Configure} @raise TypeError: The incoming object is not a C{SOM}. """ import hlr_utils o_descr = hlr_utils.get_descr(obj) if o_descr != "SOM": raise TypeError("Only SOMs are allowed in this function!") if config is None: # Make mask file name from object information instname = obj.attr_list.inst.get_name() runnum = obj.attr_list["run_number"] outfile = "%s_%s_mask.dat" % (instname, str(runnum)) else: # Make mask file name from configuration information outfile = hlr_utils.ext_replace(config.output, config.ext_replacement, "dat") outfile = hlr_utils.add_tag(outfile, "mask") ofile = open(outfile, "w") import SOM import utils len_obj = hlr_utils.get_length(obj) for i in xrange(len_obj): norm = hlr_utils.get_value(obj, i, o_descr) if utils.compare(norm, lothreshold) <= 0 or \ utils.compare(norm, hithreshold) >= 0: map_so = hlr_utils.get_map_so(obj, None, i) pix_id = SOM.NeXusId.fromString(str(map_so.id)).toJoinedStr() print >> ofile, pix_id ofile.close()
def filter_normalization(obj, lothreshold, hithreshold, config=None): """ This function takes an object with normalization integration information and a threshold and creates a mask file containing the pixel IDs that do not make it above the threshold. @param obj: The object containing the normalization information @type obj: C{SOM.SOM} @param lothreshold: The value below which a pixel will be masked. @type lothreshold: C{float} @param hithreshold: The value above which a pixel will be masked. @type hithreshold: C{float} @param config: The object holding the DR configuration @type config: L{hlr_utils.Configure} @raise TypeError: The incoming object is not a C{SOM}. """ import hlr_utils o_descr = hlr_utils.get_descr(obj) if o_descr != "SOM": raise TypeError("Only SOMs are allowed in this function!") if config is None: # Make mask file name from object information instname = obj.attr_list.inst.get_name() runnum = obj.attr_list["run_number"] outfile = "%s_%s_mask.dat" % (instname, str(runnum)) else: # Make mask file name from configuration information outfile = hlr_utils.ext_replace(config.output, config.ext_replacement, "dat") outfile = hlr_utils.add_tag(outfile, "mask") ofile = open(outfile, "w") import SOM import utils len_obj = hlr_utils.get_length(obj) for i in xrange(len_obj): norm = hlr_utils.get_value(obj, i, o_descr) if utils.compare(norm, lothreshold) <= 0 or utils.compare(norm, hithreshold) >= 0: map_so = hlr_utils.get_map_so(obj, None, i) pix_id = SOM.NeXusId.fromString(str(map_so.id)).toJoinedStr() print >> ofile, pix_id ofile.close()
if options.data is not None: configure.data = hlr_utils.fix_filename(options.data) if not hlr_utils.file_exists(configure.data): parser.error("Data file [%s] does not exist" % configure.data) else: parser.error("Did not specify a datafile") # create the output file name if there isn't one supplied if options.output: configure.output = hlr_utils.fix_filename(options.output) print "Using %s as output file" % configure.output else: configure.output = None outfile = os.path.basename(configure.data) path = os.path.join(os.getcwd(), outfile) if options.d_bins is not None: configure.output_ds = hlr_utils.ext_replace(path, "nxs", "ds1") print "Using %s as output file" % configure.output_ds else: configure.output_ds = None if options.tof_bins is not None: configure.output_tof = hlr_utils.ext_replace(path, "nxs", "gs") print "Using %s as output file" % configure.output_tof else: configure.output_tof = None if options.Q_bins is not None: configure.output_qt = hlr_utils.ext_replace(path, "nxs", "qt1") print "Using %s as output file" % configure.output_qt else: configure.output_qt = None
def BasicConfiguration(parser, configure, options, args): """ This function sets the incoming C{Configure} object with all the options that have been specified via the C{BasicOptions} object. @param parser: The parser object @type parser: L{hlr_utils.BasicOptions} @param configure: The configuration object @type configure: L{hlr_utils.Configure} @param options: The parsed options from C{BasicOptions} @type options: C{Option} @param args: The parsed arguments from C{BasicOptions} @type args: C{list} """ # Set from a configuration file first if options.config is not None: import xml.dom.minidom conf_doc = xml.dom.minidom.parse(options.config) configure = hlr_utils.ConfigFromXml(conf_doc, configure) # Now override options provided on command-line # Set the verbosity, if present on command-line if not configure.verbose: configure.verbose = options.verbose else: # Got the verbosity from the config file, but check CLI if hlr_utils.cli_checker("-v", "-q"): # Override option configure.verbose = options.verbose else: # No flags present, do nothing pass # Define instrument short name first as stuff below depends on it if hlr_utils.cli_provide_override(configure, "inst", "--inst"): configure.inst = options.inst # Define facility short name as stuff below depends on it if hlr_utils.cli_provide_override(configure, "facility", "--facility"): configure.facility = options.facility # Define proposal as stuff below depends on it. if hlr_utils.cli_provide_override(configure, "proposal", "--proposal"): configure.proposal = options.proposal # Get the datafile name and check it if options.data is not None: configure.data = hlr_utils.determine_files(options.data, configure.inst, configure.facility, configure.proposal, stop_on_none=True) elif len(args) > 0: configure.data = hlr_utils.determine_files(args, configure.inst, configure.facility, configure.proposal, stop_on_none=True) elif configure.data: # We have data from the config file, so everything is OK. pass else: parser.error("Did not specify a datafile") import os # Deal with file or directory from output option if options.output: filepath = hlr_utils.fix_filename(options.output) if os.path.exists(filepath): if os.path.isdir(filepath): outfile = os.path.basename(configure.data[0]) path = os.path.join(filepath, outfile) configure.output = hlr_utils.ext_replace(path, "nxs", "txt") configure.path_replacement = filepath configure.ext_replacement = "txt" elif os.path.isfile(filepath): configure.output = filepath configure.path_replacement = os.path.dirname(filepath) configure.ext_replacement = filepath.split('.')[-1] else: parser.error("Cannot handle %s in output option" % filepath) else: # Assume that this is a file and hope that the directory exists directory = os.path.dirname(filepath) if directory != "": if not os.path.exists(directory): raise RuntimeError("The directory %s must exist!" \ % directory) else: pass else: directory = None configure.output = filepath configure.path_replacement = directory configure.ext_replacement = filepath.split('.')[-1] elif configure.output: # We have an output file, so no need to do anything else try: configure.path_replacement except AttributeError: configure.path_replacement = None # Create the output file name if there isn't one supplied else: outfile = os.path.basename(configure.data[0]) path = os.path.join(os.getcwd(), outfile) configure.output = hlr_utils.ext_replace(path, "nxs", "txt") configure.path_replacement = None configure.ext_replacement = "txt" if configure.verbose and options.output: print "Using %s as output file" % configure.output
def write_file(filename, dst_type, data, **kwargs): """ This function performs the steps necessary to write an output file. One can pass a data filename or an output filename. If a data filename is passed, the data file extension, output file extension and the replace keyword must be passed. The expected data object to write to the file is a C{SOM}. B{NOTE}: Extra keyword arguments can be passed onto the C{DST} instance via calling them in the kwargs list. Those arguments will not be processed by this function, but just pass them on. @param filename: The name of the data file from which the output is generated or the name of an output file @type filename: C{string} @param dst_type: The MIME type of the output formatter @type dst_type: C{string} @param data: Object that contains the output to be written to file @type data: C{SOM.SOM} @param kwargs: A list of keyword arguments that the function accepts: @keyword message: This is part of the message that will be printed to STDOUT if verbose keyword is set to True. The default message is \"output file\" @type message: C{string} @keyword data_ext: This is the extension on the data file. This is used in conjunction with output_ext and replace to convert the data filename into an output filename. The default value is \"nxs\". @type data_ext: C{string} @keyword output_ext: This is the extension to be used for the output file. The default value is \"txt\". @type output_ext: C{string} @keyword verbose: This determines whether or not the print statement is executed. The default value is I{False}. @type verbose: C{boolean} @keyword replace_ext: This determines whether or not the extension on the incoming filename is replaced with output_ext. The default behavior is I{True} (replace extension) @type replace_ext: C{boolean} @keyword replace_path: This determines whether or not the directory path on the incoming filename is replaced with the directory where the driver is running. The default behavior is I{True} (replace path) @type replace_path: C{boolean} @keyword path_replacement: This is a directory path that will be prepended to the output filename. The default value is C{None} and will cause the working directory to be the prepended path. @type path_replacement: C{string} @keyword extra_tag: This is a tag that will be inserted into the file name just before the file extension. @type extra_tag: C{string} @keyword getsom_kwargs: This is a collection of keyword arguments that are to be passed to the writeSOM function call. @type getsom_kwargs: C{dict} """ import os import DST import hlr_utils try: message = kwargs["message"] except KeyError: message = "output file" try: data_ext = kwargs["data_ext"] except KeyError: data_ext = "nxs" try: output_ext = kwargs["output_ext"] except KeyError: output_ext = "txt" try: verbose = kwargs["verbose"] except KeyError: verbose = False try: replace_path = kwargs["replace_path"] except KeyError: replace_path = True try: path_replacement = kwargs["path_replacement"] except KeyError: path_replacement = None try: replace_ext = kwargs["replace_ext"] except KeyError: replace_ext = True try: extra_tag = kwargs["extra_tag"] except KeyError: extra_tag = None try: arguments = kwargs["arguments"] except KeyError: arguments = None getsom_kwargs = kwargs.get("getsom_kwargs", {}) if replace_path: if path_replacement is None: path_replacement = os.getcwd() fixed_filename = os.path.join(path_replacement, os.path.basename(filename)) else: fixed_filename = filename if replace_ext: fixed_filename = hlr_utils.ext_replace(fixed_filename, data_ext, output_ext) else: pass if extra_tag is not None: fixed_filename = hlr_utils.add_tag(fixed_filename, extra_tag) # Handle difference between NeXus and other files if dst_type != "application/x-RedNxs": resource = open(fixed_filename, "w") else: resource = fixed_filename output_dst = DST.getInstance(dst_type, resource, arguments, **kwargs) if verbose: print "Writing %s" % message output_dst.writeSOM(data, **getsom_kwargs) output_dst.release_resource()