Exemplo n.º 1
0
 def __init__(self, image=None, err_image=None,
              qx_data=None, qy_data=None, q_data=None, 
              mask=None, dqx_data=None, dqy_data=None, 
              xmin=None, xmax=None, ymin=None, ymax=None,
              zmin=None, zmax=None):
     """
     """
     PlotData2D.__init__(self, image=image, err_image=err_image,
                         xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
                         zmin=zmin, zmax=zmax, qx_data=qx_data, 
                         qy_data=qy_data)
     
     LoadData2D.__init__(self, data=image, err_data=err_image,
                         qx_data=qx_data, qy_data=qy_data,
                         dqx_data=dqx_data, dqy_data=dqy_data,
                         q_data=q_data, mask=mask)
     self.id = None
     self.list_group_id = []
     self.group_id = None
     self.is_data = True
     self.path = None
     self.xtransform = None
     self.ytransform = None
     self.title = ""
     self.scale = None
Exemplo n.º 2
0
 def __init__(self, image=None, err_image=None,
              qx_data=None, qy_data=None, q_data=None, 
              mask=None, dqx_data=None, dqy_data=None, 
              xmin=None, xmax=None, ymin=None, ymax=None,
              zmin=None, zmax=None):
     """
     """
     PlotData2D.__init__(self, image=image, err_image=err_image,
                         xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
                         zmin=zmin, zmax=zmax, qx_data=qx_data, 
                         qy_data=qy_data)
     
     LoadData2D.__init__(self, data=image, err_data=err_image,
                         qx_data=qx_data, qy_data=qy_data,
                         dqx_data=dqx_data, dqy_data=dqy_data,
                         q_data=q_data, mask=mask)
     self.id = None
     self.list_group_id = []
     self.group_id = None
     self.is_data = True
     self.path = None
     self.xtransform = None
     self.ytransform = None
     self.title = ""
     self.scale = None
Exemplo n.º 3
0
 def __init__(self, sas_data2d, data=None, err_data=None):
     Data2D.__init__(self, data=data, err_data=err_data)
     # Data can be initialized with a sas plottable or with vectors.
     self.res_err_image = []
     self.num_points = 0  # will be set by set_data
     self.idx = []
     self.qmin = None
     self.qmax = None
     self.smearer = None
     self.radius = 0
     self.res_err_data = []
     self.sas_data = sas_data2d
     self.set_data(sas_data2d)
Exemplo n.º 4
0
 def __init__(self, sas_data2d, data=None, err_data=None):
     Data2D.__init__(self, data=data, err_data=err_data)
     # Data can be initialized with a sas plottable or with vectors.
     self.res_err_image = []
     self.num_points = 0 # will be set by set_data
     self.idx = []
     self.qmin = None
     self.qmax = None
     self.smearer = None
     self.radius = 0
     self.res_err_data = []
     self.sas_data = sas_data2d
     self.set_data(sas_data2d)
Exemplo n.º 5
0
    def load_frames(self, frames):
        frame_data = []
        # Prepare axis values (arbitrary scale)
        x = self.n_rasters * range(1, self.n_pixels + 1)
        y = [self.n_pixels * [i] for i in range(1, self.n_rasters + 1)]
        y = np.reshape(y, (1, self.n_pixels * self.n_rasters))[0]
        x_bins = x[:self.n_pixels]
        y_bins = y[0::self.n_pixels]

        for frame in frames:
            self.frame = frame
            raw_frame_data = self.load_data()
            data2d = Data2D(data=raw_frame_data, qx_data=x, qy_data=y)
            data2d.x_bins = x_bins
            data2d.y_bins = y_bins
            data2d.Q_unit = ''  # Using arbitrary units
            frame_data.append(data2d)

        return frame_data
Exemplo n.º 6
0
 def convert_image(self, rgb, xmin, xmax, ymin, ymax, zscale):
     """
     Convert image to data2D
     """
     x_len = len(rgb[0])
     y_len = len(rgb)
     x_vals = np.linspace(xmin, xmax, num=x_len)
     y_vals = np.linspace(ymin, ymax, num=y_len)
     # Instantiate data object
     output = Data2D()
     output.filename = os.path.basename(self.title)
     output.id = output.filename
     detector = Detector()
     detector.pixel_size.x = None
     detector.pixel_size.y = None
     # Store the sample to detector distance
     detector.distance = None
     output.detector.append(detector)
     # Initiazed the output data object
     output.data = zscale * self.rgb2gray(rgb)
     output.err_data = np.zeros([x_len, y_len])
     output.mask = np.ones([x_len, y_len], dtype=bool)
     output.xbins = x_len
     output.ybins = y_len
     output.x_bins = x_vals
     output.y_bins = y_vals
     output.qx_data = np.array(x_vals)
     output.qy_data = np.array(y_vals)
     output.xmin = xmin
     output.xmax = xmax
     output.ymin = ymin
     output.ymax = ymax
     output.xaxis('\\rm{Q_{x}}', '\AA^{-1}')
     output.yaxis('\\rm{Q_{y}}', '\AA^{-1}')
     # Store loading process information
     output.meta_data['loader'] = self.title.split('.')[-1] + "Reader"
     output.is_data = True
     output = reader2D_converter(output)
     if self.base is not None:
         data = self.base.create_gui_data(output, self.title)
         self.base.add_data({data.id: data})
Exemplo n.º 7
0
    def read(self, filename=None):
        """
        Open and read the data in a file
        @param file: path of the file
        """

        read_it = False
        for item in self.ext:
            if filename.lower().find(item) >= 0:
                read_it = True

        if read_it:
            try:
                datafile = open(filename, 'r')
            except:
                raise RuntimeError, "danse_reader cannot open %s" % (filename)

            # defaults
            # wavelength in Angstrom
            wavelength = 10.0
            # Distance in meter
            distance = 11.0
            # Pixel number of center in x
            center_x = 65
            # Pixel number of center in y
            center_y = 65
            # Pixel size [mm]
            pixel = 5.0
            # Size in x, in pixels
            size_x = 128
            # Size in y, in pixels
            size_y = 128
            # Format version
            fversion = 1.0

            output = Data2D()
            output.filename = os.path.basename(filename)
            detector = Detector()
            output.detector.append(detector)

            output.data = numpy.zeros([size_x, size_y])
            output.err_data = numpy.zeros([size_x, size_y])

            data_conv_q = None
            data_conv_i = None

            if has_converter == True and output.Q_unit != '1/A':
                data_conv_q = Converter('1/A')
                # Test it
                data_conv_q(1.0, output.Q_unit)

            if has_converter == True and output.I_unit != '1/cm':
                data_conv_i = Converter('1/cm')
                # Test it
                data_conv_i(1.0, output.I_unit)

            read_on = True
            while read_on:
                line = datafile.readline()
                if line.find("DATA:") >= 0:
                    read_on = False
                    break
                toks = line.split(':')
                if toks[0] == "FORMATVERSION":
                    fversion = float(toks[1])
                if toks[0] == "WAVELENGTH":
                    wavelength = float(toks[1])
                elif toks[0] == "DISTANCE":
                    distance = float(toks[1])
                elif toks[0] == "CENTER_X":
                    center_x = float(toks[1])
                elif toks[0] == "CENTER_Y":
                    center_y = float(toks[1])
                elif toks[0] == "PIXELSIZE":
                    pixel = float(toks[1])
                elif toks[0] == "SIZE_X":
                    size_x = int(toks[1])
                elif toks[0] == "SIZE_Y":
                    size_y = int(toks[1])

            # Read the data
            data = []
            error = []
            if fversion == 1.0:
                data_str = datafile.readline()
                data = data_str.split(' ')
            else:
                read_on = True
                while read_on:
                    data_str = datafile.readline()
                    if len(data_str) == 0:
                        read_on = False
                    else:
                        toks = data_str.split()
                        try:
                            val = float(toks[0])
                            err = float(toks[1])
                            if data_conv_i is not None:
                                val = data_conv_i(val, units=output._yunit)
                                err = data_conv_i(err, units=output._yunit)
                            data.append(val)
                            error.append(err)
                        except:
                            logging.info("Skipping line:%s,%s" %
                                         (data_str, sys.exc_value))

            # Initialize
            x_vals = []
            y_vals = []
            ymin = None
            ymax = None
            xmin = None
            xmax = None

            # Qx and Qy vectors
            theta = pixel / distance / 100.0
            stepq = 4.0 * math.pi / wavelength * math.sin(theta / 2.0)
            for i_x in range(size_x):
                theta = (i_x - center_x + 1) * pixel / distance / 100.0
                qx = 4.0 * math.pi / wavelength * math.sin(theta / 2.0)

                if has_converter == True and output.Q_unit != '1/A':
                    qx = data_conv_q(qx, units=output.Q_unit)

                x_vals.append(qx)
                if xmin == None or qx < xmin:
                    xmin = qx
                if xmax == None or qx > xmax:
                    xmax = qx

            ymin = None
            ymax = None
            for i_y in range(size_y):
                theta = (i_y - center_y + 1) * pixel / distance / 100.0
                qy = 4.0 * math.pi / wavelength * math.sin(theta / 2.0)

                if has_converter == True and output.Q_unit != '1/A':
                    qy = data_conv_q(qy, units=output.Q_unit)

                y_vals.append(qy)
                if ymin == None or qy < ymin:
                    ymin = qy
                if ymax == None or qy > ymax:
                    ymax = qy

            # Store the data in the 2D array
            i_x = 0
            i_y = -1

            for i_pt in range(len(data)):
                try:
                    value = float(data[i_pt])
                except:
                    # For version 1.0, the data were still
                    # stored as strings at this point.
                    msg = "Skipping entry (v1.0):%s,%s" % (str(
                        data[i_pt]), sys.exc_value)
                    logging.info(msg)

                # Get bin number
                if math.fmod(i_pt, size_x) == 0:
                    i_x = 0
                    i_y += 1
                else:
                    i_x += 1

                output.data[i_y][i_x] = value
                if fversion > 1.0:
                    output.err_data[i_y][i_x] = error[i_pt]

            # Store all data
            # Store wavelength
            if has_converter == True and output.source.wavelength_unit != 'A':
                conv = Converter('A')
                wavelength = conv(wavelength,
                                  units=output.source.wavelength_unit)
            output.source.wavelength = wavelength

            # Store distance
            if has_converter == True and detector.distance_unit != 'm':
                conv = Converter('m')
                distance = conv(distance, units=detector.distance_unit)
            detector.distance = distance

            # Store pixel size
            if has_converter == True and detector.pixel_size_unit != 'mm':
                conv = Converter('mm')
                pixel = conv(pixel, units=detector.pixel_size_unit)
            detector.pixel_size.x = pixel
            detector.pixel_size.y = pixel

            # Store beam center in distance units
            detector.beam_center.x = center_x * pixel
            detector.beam_center.y = center_y * pixel

            # Store limits of the image (2D array)
            xmin = xmin - stepq / 2.0
            xmax = xmax + stepq / 2.0
            ymin = ymin - stepq / 2.0
            ymax = ymax + stepq / 2.0

            if has_converter == True and output.Q_unit != '1/A':
                xmin = data_conv_q(xmin, units=output.Q_unit)
                xmax = data_conv_q(xmax, units=output.Q_unit)
                ymin = data_conv_q(ymin, units=output.Q_unit)
                ymax = data_conv_q(ymax, units=output.Q_unit)
            output.xmin = xmin
            output.xmax = xmax
            output.ymin = ymin
            output.ymax = ymax

            # Store x and y axis bin centers
            output.x_bins = x_vals
            output.y_bins = y_vals

            # Units
            if data_conv_q is not None:
                output.xaxis("\\rm{Q_{x}}", output.Q_unit)
                output.yaxis("\\rm{Q_{y}}", output.Q_unit)
            else:
                output.xaxis("\\rm{Q_{x}}", 'A^{-1}')
                output.yaxis("\\rm{Q_{y}}", 'A^{-1}')

            if data_conv_i is not None:
                output.zaxis("\\rm{Intensity}", output.I_unit)
            else:
                output.zaxis("\\rm{Intensity}", "cm^{-1}")

            if not fversion >= 1.0:
                msg = "Danse_reader can't read this file %s" % filename
                raise ValueError, msg
            else:
                logging.info("Danse_reader Reading %s \n" % filename)

            # Store loading process information
            output.meta_data['loader'] = self.type_name
            output = reader2D_converter(output)
            return output

        return None
Exemplo n.º 8
0
    def read(self, filename=None):
        """
        Open and read the data in a file

        :param file: path of the file
        """
        try:
            import Image
            import TiffImagePlugin
            Image._initialized = 2
        except:
            msg = "tiff_reader: could not load file. Missing Image module."
            raise RuntimeError(msg)

        # Instantiate data object
        output = Data2D()
        output.filename = os.path.basename(filename)

        # Read in the image
        try:
            im = Image.open(filename)
        except:
            raise RuntimeError("cannot open %s" % (filename))
        data = im.getdata()

        # Initiazed the output data object
        output.data = np.zeros([im.size[0], im.size[1]])
        output.err_data = np.zeros([im.size[0], im.size[1]])
        output.mask = np.ones([im.size[0], im.size[1]], dtype=bool)

        # Initialize
        x_vals = []
        y_vals = []

        # x and y vectors
        for i_x in range(im.size[0]):
            x_vals.append(i_x)

        itot = 0
        for i_y in range(im.size[1]):
            y_vals.append(i_y)

        for val in data:
            try:
                value = float(val)
            except:
                logger.error("tiff_reader: had to skip a non-float point")
                continue

            # Get bin number
            if math.fmod(itot, im.size[0]) == 0:
                i_x = 0
                i_y += 1
            else:
                i_x += 1

            output.data[im.size[1] - 1 - i_y][i_x] = value

            itot += 1

        output.xbins = im.size[0]
        output.ybins = im.size[1]
        output.x_bins = x_vals
        output.y_bins = y_vals
        output.qx_data = np.array(x_vals)
        output.qy_data = np.array(y_vals)
        output.xmin = 0
        output.xmax = im.size[0] - 1
        output.ymin = 0
        output.ymax = im.size[0] - 1

        # Store loading process information
        output.meta_data['loader'] = self.type_name
        output = reader2D_converter(output)
        return output
Exemplo n.º 9
0
    def _get_detector_qxqy_pixels(self):
        """
        Get the pixel positions of the detector in the qx_value-qy_value space
        """

        # update all param values
        self.get_all_instrument_params()

        # wavelength
        wavelength = self.wave.wavelength
        # Gavity correction
        delta_y = self._get_beamcenter_drop()  # in cm

        # detector_pix size
        detector_pix_size = self.detector_pix_size
        # Square or circular pixel
        if len(detector_pix_size) == 1:
            pix_x_size = detector_pix_size[0]
            pix_y_size = detector_pix_size[0]
        # rectangular pixel pixel
        elif len(detector_pix_size) == 2:
            pix_x_size = detector_pix_size[0]
            pix_y_size = detector_pix_size[1]
        else:
            raise ValueError(" Input value format error...")
        # Sample to detector distance = sample slit to detector
        # minus sample offset
        sample2detector_distance = self.sample2detector_distance[0] - \
                                    self.sample2sample_distance[0]
        # detector offset in x-direction
        detector_offset = 0
        try:
            detector_offset = self.sample2detector_distance[1]
        except:
            logger.error(sys.exc_value)

        # detector size in [no of pix_x,no of pix_y]
        detector_pix_nums_x = self.detector_size[0]

        # get pix_y if it exists, otherwse take it from [0]
        try:
            detector_pix_nums_y = self.detector_size[1]
        except:
            detector_pix_nums_y = self.detector_size[0]

        # detector offset in pix number
        offset_x = detector_offset / pix_x_size
        offset_y = delta_y / pix_y_size

        # beam center position in pix number (start from 0)
        center_x, center_y = self._get_beamcenter_position(detector_pix_nums_x,
                                                           detector_pix_nums_y,
                                                           offset_x, offset_y)
        # distance [cm] from the beam center on detector plane
        detector_ind_x = np.arange(detector_pix_nums_x)
        detector_ind_y = np.arange(detector_pix_nums_y)

        # shif 0.5 pixel so that pix position is at the center of the pixel
        detector_ind_x = detector_ind_x + 0.5
        detector_ind_y = detector_ind_y + 0.5

        # the relative postion from the beam center
        detector_ind_x = detector_ind_x - center_x
        detector_ind_y = detector_ind_y - center_y

        # unit correction in cm
        detector_ind_x = detector_ind_x * pix_x_size
        detector_ind_y = detector_ind_y * pix_y_size

        qx_value = np.zeros(len(detector_ind_x))
        qy_value = np.zeros(len(detector_ind_y))
        i = 0

        for indx in detector_ind_x:
            qx_value[i] = self._get_qx(indx, sample2detector_distance, wavelength)
            i += 1
        i = 0
        for indy in detector_ind_y:
            qy_value[i] = self._get_qx(indy, sample2detector_distance, wavelength)
            i += 1

        # qx_value and qy_value values in array
        qx_value = qx_value.repeat(detector_pix_nums_y)
        qx_value = qx_value.reshape(detector_pix_nums_x, detector_pix_nums_y)
        qy_value = qy_value.repeat(detector_pix_nums_x)
        qy_value = qy_value.reshape(detector_pix_nums_y, detector_pix_nums_x)
        qy_value = qy_value.transpose()

        # p min and max values among the center of pixels
        self.qx_min = np.min(qx_value)
        self.qx_max = np.max(qx_value)
        self.qy_min = np.min(qy_value)
        self.qy_max = np.max(qy_value)

        # Appr. min and max values of the detector display limits
        # i.e., edges of the last pixels.
        self.qy_min += self._get_qx(-0.5 * pix_y_size,
                                    sample2detector_distance, wavelength)
        self.qy_max += self._get_qx(0.5 * pix_y_size,
                                    sample2detector_distance, wavelength)
        #if self.qx_min == self.qx_max:
        self.qx_min += self._get_qx(-0.5 * pix_x_size,
                                    sample2detector_distance, wavelength)
        self.qx_max += self._get_qx(0.5 * pix_x_size,
                                    sample2detector_distance, wavelength)

        # min and max values of detecter
        self.detector_qx_min = self.qx_min
        self.detector_qx_max = self.qx_max
        self.detector_qy_min = self.qy_min
        self.detector_qy_max = self.qy_max

        # try to set it as a Data2D otherwise pass (not required for now)
        try:
            from sas.sascalc.dataloader.data_info import Data2D
            output = Data2D()
            inten = np.zeros_like(qx_value)
            output.data = inten
            output.qx_data = qx_value
            output.qy_data = qy_value
        except:
            logger.error(sys.exc_value)

        return output
Exemplo n.º 10
0
    def read(self, filename=None):
        """
            Open and read the data in a file
        
            :param filename: path of the file
        """
        try:
            import nxs
        except:
            msg = "Error reading Nexus file: Nexus package is missing.\n"
            msg += "  Get it from http://http://www.nexusformat.org/"
            raise RuntimeError, msg

        # Instantiate data object
        output = Data2D()
        output.filename = os.path.basename(filename)

        fd = nxs.open(filename, 'rw')

        # Read in the 2D data
        fd.opengroup('mantid_workspace_1')
        fd.opengroup('workspace')
        fd.opendata('values')
        output.data = fd.getdata().copy()
        fd.closedata()

        # Read in the errors
        fd.opendata('errors')
        output.err_data = fd.getdata().copy()
        fd.closedata()

        # Read in the values on each axis
        fd.opendata('axis1')
        output.x_bins = fd.getdata().copy()
        fd.closedata()

        fd.opendata('axis2')
        output.y_bins = fd.getdata().copy()
        fd.closedata()
        fd.closegroup()

        output.xmin = min(output.x_bins)
        output.xmax = max(output.x_bins)
        output.ymin = min(output.y_bins)
        output.ymax = max(output.y_bins)

        output.xaxis("\\rm{Q_{x}}", 'A^{-1}')
        output.yaxis("\\rm{Q_{y}}", 'A^{-1}')
        output.zaxis("\\rm{Intensity}", "cm^{-1}")

        # Meta data
        fd.opendata('title')
        output.title = fd.getdata()
        fd.closedata()

        fd.opengroup('instrument')
        fd.opendata('name')
        output.instrument = fd.getdata()
        fd.closedata()
        fd.closegroup()

        fd.opengroup('logs')
        fd.opengroup('run_number')
        fd.opendata('value')
        output.run = fd.getdata()

        fd.close()

        # Store loading process information
        output.meta_data['loader'] = self.type_name
        return output
Exemplo n.º 11
0
    def load(self):
        """
        Load the data from the file into a Data2D object

        :return: A Data2D instance containing data from the file
        :raises ValueError: Raises a ValueError if the file is incorrectly
            formatted
        """
        with open(self.data_path, 'r') as file_handle:
            file_buffer = file_handle.read()
            all_lines = file_buffer.splitlines()

            # Load num_points line-by-line from lines into a numpy array,
            # starting on line number start_line
            def _load_points(lines, start_line, num_points):
                qs = np.zeros(num_points)
                n = start_line
                filled = 0
                while filled < num_points:
                    row = np.fromstring(lines[n], dtype=np.float32, sep=' ')
                    qs[filled:filled + len(row)] = row
                    filled += len(row)
                    n += 1
                return n, qs

            current_line = 4
            try:
                # Skip n_use_rec lines
                n_use_rec = int(all_lines[current_line].strip()[0])
                current_line += n_use_rec + 1
                # Read qx data
                num_qs = int(all_lines[current_line].strip())
                current_line += 1
                current_line, qx = _load_points(all_lines, current_line,
                                                num_qs)

                # Read qy data
                num_qs = int(all_lines[current_line].strip())
                current_line += 1
                current_line, qy = _load_points(all_lines, current_line,
                                                num_qs)
            except ValueError as e:
                err_msg = "File incorrectly formatted.\n"
                if str(e).find('broadcast') != -1:
                    err_msg += "Incorrect number of q data points provided. "
                    err_msg += "Expected {}.".format(num_qs)
                elif str(e).find('invalid literal') != -1:
                    err_msg += ("Expected integer on line {}. "
                                "Instead got '{}'").format(
                                    current_line + 1, all_lines[current_line])
                else:
                    err_msg += str(e)
                raise ValueError(err_msg)

            # dimensions: [width, height, scale]
            try:
                dimensions = np.fromstring(all_lines[current_line],
                                           dtype=np.float32,
                                           sep=' ')
                if len(dimensions) != 3: raise ValueError()
                width = int(dimensions[0])
                height = int(dimensions[1])
            except ValueError as e:
                err_msg = "File incorrectly formatted.\n"
                err_msg += ("Expected line {} to be of the form: <num_qx> "
                            "<num_qy> <scale>.").format(current_line + 1)
                err_msg += " Instead got '{}'.".format(all_lines[current_line])
                raise ValueError(err_msg)

            if width > len(qx) or height > len(qy):
                err_msg = "File incorrectly formatted.\n"
                err_msg += ("Line {} says to use {}x{} points. "
                            "Only {}x{} provided.").format(
                                current_line + 1, width, height, len(qx),
                                len(qy))
                raise ValueError(err_msg)

            # More qx and/or qy points can be provided than are actually used
            qx = qx[:width]
            qy = qy[:height]

            current_line += 1
            # iflag = 1 => Only intensity data (not dealt with here)
            # iflag = 2 => q axis and intensity data
            # iflag = 3 => q axis, intensity and error data
            try:
                iflag = int(all_lines[current_line].strip()[0])
                if iflag <= 0 or iflag > 3: raise ValueError()
            except:
                err_msg = "File incorrectly formatted.\n"
                iflag = all_lines[current_line].strip()[0]
                err_msg += ("Expected iflag on line {} to be 1, 2 or 3. "
                            "Instead got '{}'.").format(
                                current_line + 1, iflag)
                raise ValueError(err_msg)

            current_line += 1

            try:
                current_line, I = _load_points(all_lines, current_line,
                                               width * height)
                dI = np.zeros(width * height)

                # Load error data if it's provided
                if iflag == 3:
                    _, dI = _load_points(all_lines, current_line,
                                         width * height)
            except Exception as e:
                err_msg = "File incorrectly formatted.\n"
                if str(e).find("list index") != -1:
                    err_msg += ("Incorrect number of data points. Expected {}"
                                " intensity").format(width * height)
                    if iflag == 3:
                        err_msg += " and error"
                    err_msg += " points."
                else:
                    err_msg += str(e)
                raise ValueError(err_msg)

            # Format data for use with Data2D
            qx = list(qx) * height
            qy = np.array([[y] * width for y in qy]).flatten()

            data = Data2D(qx_data=qx, qy_data=qy, data=I, err_data=dI)

        return data
Exemplo n.º 12
0
 def __str__(self):
     """
     print data
     """
     _str = "%s\n" % LoadData2D.__str__(self)
     return _str 
Exemplo n.º 13
0
 def __str__(self):
     """
     print data
     """
     _str = "%s\n" % LoadData2D.__str__(self)
     return _str
Exemplo n.º 14
0
    def read(self, filename=None):
        """ Read file """
        if not os.path.isfile(filename):
            raise ValueError("Specified file %s is not a regular "
                             "file" % filename)

        output = Data2D()

        output.filename = os.path.basename(filename)
        detector = Detector()
        if len(output.detector):
            print(str(output.detector[0]))
        output.detector.append(detector)

        data_conv_q = data_conv_i = None

        if has_converter and output.Q_unit != '1/A':
            data_conv_q = Converter('1/A')
            # Test it
            data_conv_q(1.0, output.Q_unit)

        if has_converter and output.I_unit != '1/cm':
            data_conv_i = Converter('1/cm')
            # Test it
            data_conv_i(1.0, output.I_unit)

        data_row = 0
        wavelength = distance = center_x = center_y = None
        dataStarted = isInfo = isCenter = False

        with open(filename, 'r') as f:
            for line in f:
                data_row += 1
                # Find setup info line
                if isInfo:
                    isInfo = False
                    line_toks = line.split()
                    # Wavelength in Angstrom
                    try:
                        wavelength = float(line_toks[1])
                    except ValueError:
                        msg = "IgorReader: can't read this file, missing wavelength"
                        raise ValueError(msg)
                    # Distance in meters
                    try:
                        distance = float(line_toks[3])
                    except ValueError:
                        msg = "IgorReader: can't read this file, missing distance"
                        raise ValueError(msg)

                    # Distance in meters
                    try:
                        transmission = float(line_toks[4])
                    except:
                        msg = "IgorReader: can't read this file, "
                        msg += "missing transmission"
                        raise ValueError(msg)

                if line.count("LAMBDA"):
                    isInfo = True

                # Find center info line
                if isCenter:
                    isCenter = False
                    line_toks = line.split()

                    # Center in bin number: Must subtract 1 because
                    # the index starts from 1
                    center_x = float(line_toks[0]) - 1
                    center_y = float(line_toks[1]) - 1

                if line.count("BCENT"):
                    isCenter = True

                # Find data start
                if line.count("***"):
                    # now have to continue to blank line
                    dataStarted = True

                    # Check that we have all the info
                    if (wavelength is None or distance is None
                            or center_x is None or center_y is None):
                        msg = "IgorReader:Missing information in data file"
                        raise ValueError(msg)

                if dataStarted:
                    if len(line.rstrip()):
                        continue
                    else:
                        break

        # The data is loaded in row major order (last index changing most
        # rapidly). However, the original data is in column major order (first
        # index changing most rapidly). The swap to column major order is done
        # in reader2D_converter at the end of this method.
        data = np.loadtxt(filename, skiprows=data_row)
        size_x = size_y = int(np.rint(np.sqrt(data.size)))
        output.data = np.reshape(data, (size_x, size_y))
        output.err_data = np.zeros_like(output.data)

        # Det 640 x 640 mm
        # Q = 4 * pi/lambda * sin(theta/2)
        # Bin size is 0.5 cm
        # Removed +1 from theta = (i_x - center_x + 1)*0.5 / distance
        # / 100.0 and
        # Removed +1 from theta = (i_y - center_y + 1)*0.5 /
        # distance / 100.0
        # ToDo: Need  complete check if the following
        # convert process is consistent with fitting.py.

        # calculate qx, qy bin centers of each pixel in the image
        theta = (np.arange(size_x) - center_x) * 0.5 / distance / 100.
        qx = 4 * np.pi / wavelength * np.sin(theta / 2)

        theta = (np.arange(size_y) - center_y) * 0.5 / distance / 100.
        qy = 4 * np.pi / wavelength * np.sin(theta / 2)

        if has_converter and output.Q_unit != '1/A':
            qx = data_conv_q(qx, units=output.Q_unit)
            qy = data_conv_q(qx, units=output.Q_unit)

        xmax = np.max(qx)
        xmin = np.min(qx)
        ymax = np.max(qy)
        ymin = np.min(qy)

        # calculate edge offset in q.
        theta = 0.25 / distance / 100.0
        xstep = 4.0 * np.pi / wavelength * np.sin(theta / 2.0)

        theta = 0.25 / distance / 100.0
        ystep = 4.0 * np.pi / wavelength * np.sin(theta / 2.0)

        # Store all data ######################################
        # Store wavelength
        if has_converter and output.source.wavelength_unit != 'A':
            conv = Converter('A')
            wavelength = conv(wavelength, units=output.source.wavelength_unit)
        output.source.wavelength = wavelength

        # Store distance
        if has_converter and detector.distance_unit != 'm':
            conv = Converter('m')
            distance = conv(distance, units=detector.distance_unit)
        detector.distance = distance

        # Store transmission
        output.sample.transmission = transmission

        # Store pixel size (mm)
        pixel = 5.0
        if has_converter and detector.pixel_size_unit != 'mm':
            conv = Converter('mm')
            pixel = conv(pixel, units=detector.pixel_size_unit)
        detector.pixel_size.x = pixel
        detector.pixel_size.y = pixel

        # Store beam center in distance units
        detector.beam_center.x = center_x * pixel
        detector.beam_center.y = center_y * pixel

        # Store limits of the image (2D array)
        xmin -= xstep / 2.0
        xmax += xstep / 2.0
        ymin -= ystep / 2.0
        ymax += ystep / 2.0
        if has_converter and output.Q_unit != '1/A':
            xmin = data_conv_q(xmin, units=output.Q_unit)
            xmax = data_conv_q(xmax, units=output.Q_unit)
            ymin = data_conv_q(ymin, units=output.Q_unit)
            ymax = data_conv_q(ymax, units=output.Q_unit)
        output.xmin = xmin
        output.xmax = xmax
        output.ymin = ymin
        output.ymax = ymax

        # Store x and y axis bin centers
        output.x_bins = qx.tolist()
        output.y_bins = qy.tolist()

        # Units
        if data_conv_q is not None:
            output.xaxis("\\rm{Q_{x}}", output.Q_unit)
            output.yaxis("\\rm{Q_{y}}", output.Q_unit)
        else:
            output.xaxis("\\rm{Q_{x}}", 'A^{-1}')
            output.yaxis("\\rm{Q_{y}}", 'A^{-1}')

        if data_conv_i is not None:
            output.zaxis("\\rm{Intensity}", output.I_unit)
        else:
            output.zaxis("\\rm{Intensity}", "cm^{-1}")

        # Store loading process information
        output.meta_data['loader'] = self.type_name
        output = reader2D_converter(output)

        return output
Exemplo n.º 15
0
    def read(self, filename=None):
        """ Read file """
        if not os.path.isfile(filename):
            raise ValueError, \
            "Specified file %s is not a regular file" % filename

        # Read file
        f = open(filename, 'r')
        buf = f.read()
        f.close()
        # Instantiate data object
        output = Data2D()
        output.filename = os.path.basename(filename)
        detector = Detector()
        if len(output.detector) > 0:
            print(str(output.detector[0]))
        output.detector.append(detector)
                
        # Get content
        dataStarted = False
        
        ## Defaults
        lines = buf.split('\n')
        x = []
        y = []
        
        wavelength = None
        distance = None
        transmission = None
        
        pixel_x = None
        pixel_y = None
        
        isInfo = False
        isCenter = False

        data_conv_q = None
        data_conv_i = None
        
        # Set units: This is the unit assumed for Q and I in the data file.
        if has_converter == True and output.Q_unit != '1/A':
            data_conv_q = Converter('1/A')
            # Test it
            data_conv_q(1.0, output.Q_unit)
            
        if has_converter == True and output.I_unit != '1/cm':
            data_conv_i = Converter('1/cm')
            # Test it
            data_conv_i(1.0, output.I_unit)
        
              
        # Remove the last lines before the for loop if the lines are empty
        # to calculate the exact number of data points
        count = 0
        while (len(lines[len(lines) - (count + 1)].lstrip().rstrip()) < 1):
            del lines[len(lines) - (count + 1)]
            count = count + 1

        #Read Header and find the dimensions of 2D data
        line_num = 0
        # Old version NIST files: 0
        ver = 0
        for line in lines:
            line_num += 1
            ## Reading the header applies only to IGOR/NIST 2D q_map data files
            # Find setup info line
            if isInfo:
                isInfo = False
                line_toks = line.split()
                # Wavelength in Angstrom
                try:
                    wavelength = float(line_toks[1])
                    # Units
                    if has_converter == True and \
                    output.source.wavelength_unit != 'A':
                        conv = Converter('A')
                        wavelength = conv(wavelength,
                                          units=output.source.wavelength_unit)
                except:
                    #Not required
                    pass
                # Distance in mm
                try:
                    distance = float(line_toks[3])
                    # Units
                    if has_converter == True and detector.distance_unit != 'm':
                        conv = Converter('m')
                        distance = conv(distance, units=detector.distance_unit)
                except:
                    #Not required
                    pass
                
                # Distance in meters
                try:
                    transmission = float(line_toks[4])
                except:
                    #Not required
                    pass
                                            
            if line.count("LAMBDA") > 0:
                isInfo = True
                
            # Find center info line
            if isCenter:
                isCenter = False
                line_toks = line.split()
                # Center in bin number
                center_x = float(line_toks[0])
                center_y = float(line_toks[1])

            if line.count("BCENT") > 0:
                isCenter = True
            # Check version
            if line.count("Data columns") > 0:
                if line.count("err(I)") > 0:
                    ver = 1
            # Find data start
            if line.count("ASCII data") > 0:
                dataStarted = True
                continue

            ## Read and get data.
            if dataStarted == True:
                line_toks = line.split()
                if len(line_toks) == 0:
                    #empty line
                    continue
                # the number of columns must be stayed same 
                col_num = len(line_toks)
                break
        # Make numpy array to remove header lines using index
        lines_array = np.array(lines)

        # index for lines_array
        lines_index = np.arange(len(lines))
        
        # get the data lines
        data_lines = lines_array[lines_index >= (line_num - 1)]
        # Now we get the total number of rows (i.e., # of data points)
        row_num = len(data_lines)
        # make it as list again to control the separators
        data_list = " ".join(data_lines.tolist())
        # split all data to one big list w/" "separator
        data_list = data_list.split()
 
        # Check if the size is consistent with data, otherwise
        #try the tab(\t) separator
        # (this may be removed once get the confidence
        #the former working all cases).
        if len(data_list) != (len(data_lines)) * col_num:
            data_list = "\t".join(data_lines.tolist())
            data_list = data_list.split()

        # Change it(string) into float
        #data_list = map(float,data_list)
        data_list1 = map(check_point, data_list)

        # numpy array form
        data_array = np.array(data_list1)
        # Redimesion based on the row_num and col_num,
        #otherwise raise an error.
        try:
            data_point = data_array.reshape(row_num, col_num).transpose()
        except:
            msg = "red2d_reader: Can't read this file: Not a proper file format"
            raise ValueError, msg
        ## Get the all data: Let's HARDcoding; Todo find better way
        # Defaults
        dqx_data = np.zeros(0)
        dqy_data = np.zeros(0)
        err_data = np.ones(row_num)
        qz_data = np.zeros(row_num)
        mask = np.ones(row_num, dtype=bool)
        # Get from the array
        qx_data = data_point[0]
        qy_data = data_point[1]
        data = data_point[2]
        if ver == 1:
            if col_num > (2 + ver):
                err_data = data_point[(2 + ver)]
        if col_num > (3 + ver):
            qz_data = data_point[(3 + ver)]
        if col_num > (4 + ver):
            dqx_data = data_point[(4 + ver)]
        if col_num > (5 + ver):
            dqy_data = data_point[(5 + ver)]
        #if col_num > (6 + ver): mask[data_point[(6 + ver)] < 1] = False
        q_data = np.sqrt(qx_data*qx_data+qy_data*qy_data+qz_data*qz_data)
           
        # Extra protection(it is needed for some data files): 
        # If all mask elements are False, put all True
        if not mask.any():
            mask[mask == False] = True
  
        # Store limits of the image in q space
        xmin = np.min(qx_data)
        xmax = np.max(qx_data)
        ymin = np.min(qy_data)
        ymax = np.max(qy_data)

        # units
        if has_converter == True and output.Q_unit != '1/A':
            xmin = data_conv_q(xmin, units=output.Q_unit)
            xmax = data_conv_q(xmax, units=output.Q_unit)
            ymin = data_conv_q(ymin, units=output.Q_unit)
            ymax = data_conv_q(ymax, units=output.Q_unit)
            
        ## calculate the range of the qx and qy_data
        x_size = math.fabs(xmax - xmin)
        y_size = math.fabs(ymax - ymin)
        
        # calculate the number of pixels in the each axes
        npix_y = math.floor(math.sqrt(len(data)))
        npix_x = math.floor(len(data) / npix_y)
        
        # calculate the size of bins
        xstep = x_size / (npix_x - 1)
        ystep = y_size / (npix_y - 1)
        
        # store x and y axis bin centers in q space
        x_bins = np.arange(xmin, xmax + xstep, xstep)
        y_bins = np.arange(ymin, ymax + ystep, ystep)
       
        # get the limits of q values
        xmin = xmin - xstep / 2
        xmax = xmax + xstep / 2
        ymin = ymin - ystep / 2
        ymax = ymax + ystep / 2
        
        #Store data in outputs
        #TODO: Check the lengths
        output.data = data
        if (err_data == 1).all():
            output.err_data = np.sqrt(np.abs(data))
            output.err_data[output.err_data == 0.0] = 1.0
        else:
            output.err_data = err_data
            
        output.qx_data = qx_data
        output.qy_data = qy_data
        output.q_data = q_data
        output.mask = mask
        
        output.x_bins = x_bins
        output.y_bins = y_bins
               
        output.xmin = xmin
        output.xmax = xmax
        output.ymin = ymin
        output.ymax = ymax
        
        output.source.wavelength = wavelength
        
        # Store pixel size in mm
        detector.pixel_size.x = pixel_x
        detector.pixel_size.y = pixel_y
        
        # Store the sample to detector distance
        detector.distance = distance
        
        # optional data: if all of dq data == 0, do not pass to output
        if len(dqx_data) == len(qx_data) and dqx_data.any() != 0:
            # if no dqx_data, do not pass dqy_data.
            #(1 axis dq is not supported yet).
            if len(dqy_data) == len(qy_data) and dqy_data.any() != 0:
                # Currently we do not support dq parr, perp.
                # tranfer the comp. to cartesian coord. for newer version.
                if ver != 1:
                    diag = np.sqrt(qx_data * qx_data + qy_data * qy_data)
                    cos_th = qx_data / diag
                    sin_th = qy_data / diag
                    output.dqx_data = np.sqrt((dqx_data * cos_th) * \
                                                 (dqx_data * cos_th) \
                                                 + (dqy_data * sin_th) * \
                                                  (dqy_data * sin_th))
                    output.dqy_data = np.sqrt((dqx_data * sin_th) * \
                                                 (dqx_data * sin_th) \
                                                 + (dqy_data * cos_th) * \
                                                  (dqy_data * cos_th))
                else:
                    output.dqx_data = dqx_data
                    output.dqy_data = dqy_data

        # Units of axes
        if data_conv_q is not None:
            output.xaxis("\\rm{Q_{x}}", output.Q_unit)
            output.yaxis("\\rm{Q_{y}}", output.Q_unit)
        else:
            output.xaxis("\\rm{Q_{x}}", 'A^{-1}')
            output.yaxis("\\rm{Q_{y}}", 'A^{-1}')
        if data_conv_i is not None:
            output.zaxis("\\rm{Intensity}", output.I_unit)
        else:
            output.zaxis("\\rm{Intensity}", "cm^{-1}")
    
        # Store loading process information
        output.meta_data['loader'] = self.type_name

        return output
Exemplo n.º 16
0
    def read(self, filename=None):
        """ Read file """
        if not os.path.isfile(filename):
            raise ValueError, \
            "Specified file %s is not a regular file" % filename
        
        # Read file
        f = open(filename, 'r')
        buf = f.read()
        
        # Instantiate data object
        output = Data2D()
        output.filename = os.path.basename(filename)
        detector = Detector()
        if len(output.detector) > 0:
            print str(output.detector[0])
        output.detector.append(detector)
                
        # Get content
        dataStarted = False
        
        lines = buf.split('\n')
        itot = 0
        x = []
        y = []
        
        ncounts = 0
        
        xmin = None
        xmax = None
        ymin = None
        ymax = None
        
        i_x = 0
        i_y = -1
        i_tot_row = 0
        
        isInfo = False
        isCenter = False
       
        data_conv_q = None
        data_conv_i = None
        
        if has_converter == True and output.Q_unit != '1/A':
            data_conv_q = Converter('1/A')
            # Test it
            data_conv_q(1.0, output.Q_unit)
            
        if has_converter == True and output.I_unit != '1/cm':
            data_conv_i = Converter('1/cm')
            # Test it
            data_conv_i(1.0, output.I_unit)
         
        for line in lines:
            
            # Find setup info line
            if isInfo:
                isInfo = False
                line_toks = line.split()
                # Wavelength in Angstrom
                try:
                    wavelength = float(line_toks[1])
                except:
                    msg = "IgorReader: can't read this file, missing wavelength"
                    raise ValueError, msg
                
            #Find # of bins in a row assuming the detector is square.
            if dataStarted == True:
                try:
                    value = float(line)
                except:
                    # Found a non-float entry, skip it
                    continue
                
                # Get total bin number
                
            i_tot_row += 1
        i_tot_row = math.ceil(math.sqrt(i_tot_row)) - 1
        #print "i_tot", i_tot_row
        size_x = i_tot_row  # 192#128
        size_y = i_tot_row  # 192#128
        output.data = numpy.zeros([size_x, size_y])
        output.err_data = numpy.zeros([size_x, size_y])
     
        #Read Header and 2D data
        for line in lines:
            # Find setup info line
            if isInfo:
                isInfo = False
                line_toks = line.split()
                # Wavelength in Angstrom
                try:
                    wavelength = float(line_toks[1])
                except:
                    msg = "IgorReader: can't read this file, missing wavelength"
                    raise ValueError, msg
                # Distance in meters
                try:
                    distance = float(line_toks[3])
                except:
                    msg = "IgorReader: can't read this file, missing distance"
                    raise ValueError, msg
                
                # Distance in meters
                try:
                    transmission = float(line_toks[4])
                except:
                    msg = "IgorReader: can't read this file, "
                    msg += "missing transmission"
                    raise ValueError, msg
                                            
            if line.count("LAMBDA") > 0:
                isInfo = True
                
            # Find center info line
            if isCenter:
                isCenter = False
                line_toks = line.split()
                
                # Center in bin number: Must substrate 1 because
                #the index starts from 1
                center_x = float(line_toks[0]) - 1
                center_y = float(line_toks[1]) - 1

            if line.count("BCENT") > 0:
                isCenter = True
                
            # Find data start
            if line.count("***")>0:
                dataStarted = True
                
                # Check that we have all the info
                if wavelength == None \
                    or distance == None \
                    or center_x == None \
                    or center_y == None:
                    msg = "IgorReader:Missing information in data file"
                    raise ValueError, msg
                
            if dataStarted == True:
                try:
                    value = float(line)
                except:
                    # Found a non-float entry, skip it
                    continue
                
                # Get bin number
                if math.fmod(itot, i_tot_row) == 0:
                    i_x = 0
                    i_y += 1
                else:
                    i_x += 1
                    
                output.data[i_y][i_x] = value
                ncounts += 1
                
                # Det 640 x 640 mm
                # Q = 4pi/lambda sin(theta/2)
                # Bin size is 0.5 cm 
                #REmoved +1 from theta = (i_x-center_x+1)*0.5 / distance
                # / 100.0 and 
                #REmoved +1 from theta = (i_y-center_y+1)*0.5 /
                # distance / 100.0
                #ToDo: Need  complete check if the following
                # covert process is consistent with fitting.py.
                theta = (i_x - center_x) * 0.5 / distance / 100.0
                qx = 4.0 * math.pi / wavelength * math.sin(theta/2.0)

                if has_converter == True and output.Q_unit != '1/A':
                    qx = data_conv_q(qx, units=output.Q_unit)

                if xmin == None or qx < xmin:
                    xmin = qx
                if xmax == None or qx > xmax:
                    xmax = qx
                
                theta = (i_y - center_y) * 0.5 / distance / 100.0
                qy = 4.0 * math.pi / wavelength * math.sin(theta / 2.0)

                if has_converter == True and output.Q_unit != '1/A':
                    qy = data_conv_q(qy, units=output.Q_unit)
                
                if ymin == None or qy < ymin:
                    ymin = qy
                if ymax == None or qy > ymax:
                    ymax = qy
                
                if not qx in x:
                    x.append(qx)
                if not qy in y:
                    y.append(qy)
                
                itot += 1
                  
                  
        theta = 0.25 / distance / 100.0
        xstep = 4.0 * math.pi / wavelength * math.sin(theta / 2.0)
        
        theta = 0.25 / distance / 100.0
        ystep = 4.0 * math.pi/ wavelength * math.sin(theta / 2.0)
        
        # Store all data ######################################
        # Store wavelength
        if has_converter == True and output.source.wavelength_unit != 'A':
            conv = Converter('A')
            wavelength = conv(wavelength, units=output.source.wavelength_unit)
        output.source.wavelength = wavelength

        # Store distance
        if has_converter == True and detector.distance_unit != 'm':
            conv = Converter('m')
            distance = conv(distance, units=detector.distance_unit)
        detector.distance = distance
  
        # Store transmission
        output.sample.transmission = transmission
        
        # Store pixel size
        pixel = 5.0
        if has_converter == True and detector.pixel_size_unit != 'mm':
            conv = Converter('mm')
            pixel = conv(pixel, units=detector.pixel_size_unit)
        detector.pixel_size.x = pixel
        detector.pixel_size.y = pixel
  
        # Store beam center in distance units
        detector.beam_center.x = center_x * pixel
        detector.beam_center.y = center_y * pixel
        
        # Store limits of the image (2D array)
        xmin = xmin - xstep / 2.0
        xmax = xmax + xstep / 2.0
        ymin = ymin - ystep / 2.0
        ymax = ymax + ystep / 2.0
        if has_converter == True and output.Q_unit != '1/A':
            xmin = data_conv_q(xmin, units=output.Q_unit)
            xmax = data_conv_q(xmax, units=output.Q_unit)
            ymin = data_conv_q(ymin, units=output.Q_unit)
            ymax = data_conv_q(ymax, units=output.Q_unit)
        output.xmin = xmin
        output.xmax = xmax
        output.ymin = ymin
        output.ymax = ymax
        
        # Store x and y axis bin centers
        output.x_bins = x
        output.y_bins = y
        
        # Units
        if data_conv_q is not None:
            output.xaxis("\\rm{Q_{x}}", output.Q_unit)
            output.yaxis("\\rm{Q_{y}}", output.Q_unit)
        else:
            output.xaxis("\\rm{Q_{x}}", 'A^{-1}')
            output.yaxis("\\rm{Q_{y}}", 'A^{-1}')
            
        if data_conv_i is not None:
            output.zaxis("\\rm{Intensity}", output.I_unit)
        else:
            output.zaxis("\\rm{Intensity}", "cm^{-1}")
    
        # Store loading process information
        output.meta_data['loader'] = self.type_name
        output = reader2D_converter(output)

        return output