def run(self):
        nbr_files = self.parent.ui.summary_table.rowCount()
        nbr_col = self.parent.ui.summary_table.columnCount()

        metadata = self.get_metadata()
        data = []
        for _row in np.arange(nbr_files):
            _row_str = []
            for _col in np.arange(nbr_col):
                _row_str.append(
                    str(self.parent.ui.summary_table.item(_row, _col).text()))
            data.append(",".join(_row_str))

        export_file_name = os.path.basename(self.parent.working_dir)
        full_export_file_name = os.path.join(
            self.export_folder,
            export_file_name + "_calibrated_transmission.txt")

        make_ascii_file(metadata=metadata,
                        data=data,
                        output_file_name=full_export_file_name,
                        dim='1d')

        QtGui.QApplication.processEvents()

        # display name of file exported for 10s
        self.parent.ui.statusbar.showMessage(
            "File Created: {}".format(full_export_file_name), 10000)
    def export_table(self, output_folder):
        material = self.handler.material[0]
        lattice = self.handler.lattice[material]
        crystal_structure = self.handler.crystal_structure[material]
        metadata = [
            "# material: {}".format(material),
            "# crystal structure: {}".format(crystal_structure),
            "# lattice: {} Angstroms".format(lattice), "#",
            "# hkl, d(angstroms), BraggEdge"
        ]
        data = []
        bragg_edges = self.bragg_edges[material]
        hkl = self.hkl[material]
        for _index in np.arange(len(bragg_edges)):
            _hkl_str = [str(i) for i in hkl[_index]]
            _hkl = "".join(_hkl_str)
            _bragg_edges = np.float(bragg_edges[_index])
            _d = _bragg_edges / 2.
            _row = "{}, {}, {}".format(_hkl, _d, _bragg_edges)
            data.append(_row)

        output_file_name = os.path.join(
            output_folder, 'bragg_edges_of_{}.txt'.format(material))

        file_handler.make_ascii_file(metadata=metadata,
                                     data=data,
                                     dim='1d',
                                     output_file_name=output_file_name)

        display(HTML('<span style="font-size: 20px; color:blue">File created : ' + \
                     output_file_name + '</span>'))
    def run(self):
        # retrive metadata from dsc file
        folder = self.dsc_folder
        list_dsc_files = glob.glob(folder + '/*.dsc')
        dsc_metadata = file_handler.retrieve_metadata_from_dsc_list_files(
            list_files=list_dsc_files)

        # retrieve tiff files
        tiff_folder = self.tiff_folder
        list_tiff_files = glob.glob(tiff_folder + '/*.tif*')

        folder_name = os.path.basename(os.path.dirname(list_tiff_files[0]))
        ascii_file_name = os.path.join(os.path.abspath(self.output_folder),
                                       folder_name + '_timestamps.txt')

        list_keys = list(dsc_metadata.keys())
        list_keys.sort()

        # create list
        ascii_array = [
            "Filename, acquisition_time(os_format), acquisition_time(user_format), acquisition_duration(s)"
        ]
        for _index, _key in enumerate(list_keys):
            _line = "{}, {}, {}, {}".format(
                os.path.basename(list_tiff_files[_index]),
                dsc_metadata[_key]['os_format'],
                dsc_metadata[_key]['user_format'],
                dsc_metadata[_key]['acquisition_time'])
            ascii_array.append(_line)

        # export file
        file_handler.make_ascii_file(data=ascii_array,
                                     output_file_name=ascii_file_name,
                                     dim='1d')
예제 #4
0
    def output_profiles(self):

        # make name of output folder
        output_folder = os.path.join(os.path.abspath(self.output_folder_ui.selected),
                                     os.path.basename(os.path.dirname(self.list_file_name[0]) + '_profiles'))
        output_folder = os.path.abspath(output_folder)
        if os.path.isdir(output_folder):
            shutil.rmtree(output_folder)
        os.mkdir(output_folder)

        [roi_left, roi_top, roi_width, roi_height] = self.roi

        self.list_data_files_short = [os.path.basename(_file) for _file in self.df['#filename']]
        time_0 = self.__get_time_stamp(self.list_data_files[0])

        w = widgets.IntProgress()
        w.max = len(self.profile_1d)
        display(w)

        for _index, _profile in enumerate(self.profile_1d):
            _file_name = self.list_data_files[_index]
            _time_stamp = self.__get_time_stamp(_file_name)

            metadata = []
            metadata.append("#Image: {}".format(_file_name))
            metadata.append(
                "#ROI selected (y0, x0, height, width): ({}, {}, {}, {})".format(roi_top,
                                                                                 roi_left,
                                                                                 roi_height,
                                                                                 roi_width))
            metadata.append("#Rebin in y direction: {}".format(self.rebin))

            _time_stamp_str = datetime.datetime.fromtimestamp(_time_stamp).strftime("%Y-%m-%d %H:%M:%S")
            metadata.append("#Time Stamp: {}".format(_time_stamp_str))

            _delta_time = _time_stamp - time_0
            metadata.append("#Delta Time (s): {}".format(_delta_time))
            metadata.append("#")

            metadata.append("#Label: pixel_index, counts")

            data = []
            for _index_data, value in enumerate(self.profile_1d[_index]):
                data.append("{}, {}".format(self.rebin_range[_index_data], value))

            _base_file_name = os.path.basename(_file_name)
            [base, _] = os.path.splitext(_base_file_name)

            output_file_name = os.path.join(output_folder, base + '.txt')
            make_ascii_file(metadata=metadata,
                            data=data,
                            output_file_name=output_file_name,
                            dim='1d')

            w.value = _index+1

        w.close()

        display(HTML('<span style="font-size: 20px; color:blue">Files created in folder ' +
                     output_folder + '</span>'))
예제 #5
0
    def configuration(self):
        # bring file dialog to locate where the file will be saved
        base_folder = Path(self.parent.working_dir)
        directory = str(base_folder.parent)
        _export_folder = QFileDialog.getExistingDirectory(
            self.parent,
            directory=directory,
            caption="Select Output Folder",
            options=QFileDialog.ShowDirsOnly)

        if _export_folder:
            data, metadata = self.get_data_metadata_from_selection_tab()

            # collect initial selection size (x0, y0, width, height)
            o_get = Get(parent=self.parent)
            [x0, y0, x1, y1, width, height] = o_get.selection_roi_dimension()

            name_of_ascii_file = ExportHandler.makeup_name_of_profile_ascii_file(
                base_name=str(base_folder.name),
                export_folder=_export_folder,
                x0=x0,
                y0=y0,
                width=width,
                height=height)

            make_ascii_file(metadata=metadata,
                            data=data,
                            output_file_name=name_of_ascii_file,
                            dim='1d')

            self.parent.ui.statusbar.showMessage(
                "{} has been created!".format(name_of_ascii_file),
                10000)  # 10s
            self.parent.ui.statusbar.setStyleSheet("color: green")
예제 #6
0
    def export(self, output_folder=''):
        if output_folder:
            for _index, _file in enumerate(self.list_images):

                [input_image_base_name,
                 ext] = os.path.splitext(os.path.basename(_file))
                output_file_name = os.path.join(
                    output_folder, input_image_base_name +
                    '_profile_c_x{}_y{}_angle_{}_to_{}.txt'.format(
                        self.center['x0'], self.center['y0'],
                        self.angle_range['from'], self.angle_range['to']))
                output_file_name = os.path.abspath(output_file_name)

                text = []
                text.append("# source image: {}".format(_file))
                text.append("# center [x0, y0]: [{},{}]".format(
                    self.center['x0'], self.center['y0']))
                text.append(
                    "# angular range from {}degrees to {}degrees".format(
                        self.angle_range['from'], self.angle_range['to']))
                text.append('')
                text.append('#pixel_from_center, Average_counts')
                data = list(
                    zip(np.arange(len(self.profile_data[_index])),
                        self.profile_data[_index]))

                file_handler.make_ascii_file(metadata=text,
                                             data=data,
                                             output_file_name=output_file_name)

                # display(HTML('<span style="font-size: 20px; color:blue">File created: ' + output_file_name + '</span>'))

        self.parent_ui.ui.statusbar.showMessage(
            "Profiles Exported in {}!".format(output_folder), 10000)
        self.parent_ui.ui.statusbar.setStyleSheet("color: green")
예제 #7
0
    def export_ascii(self, output_folder):
        list_files = self.list_of_files
        projection = self.create_projection()
        output_ascii_file_name = ListMetadata.create_output_ascii_name(
            list_files, output_folder)

        o_metadata_selected = oncat.GetProjection(instrument=self.instrument,
                                                  facility=self.facility,
                                                  list_files=list_files,
                                                  oncat=self.oncat_session,
                                                  projection=projection,
                                                  with_progressbar=True)
        metadata_selected = o_metadata_selected.datafiles

        name_metadata = self.create_metadata_name_row()
        value_metadata = self.create_metadata_value_rows(
            list_files, metadata_selected)
        make_ascii_file(metadata=name_metadata,
                        data=value_metadata,
                        output_file_name=output_ascii_file_name,
                        dim='1d')
        print("Done!")
        display(
            HTML('<span style="font-size: 20px; color:Green">File ' +
                 output_ascii_file_name +
                 ' has been created with success!</span>'))
예제 #8
0
    def run(self):
        metadata = self._create_metadata()
        data = self._format_data()

        # create output file name
        _output_file_name = self._create_output_file_name()
        make_ascii_file(metadata=metadata,
                        data=data,
                        output_file_name=_output_file_name,
                        dim='1d')
예제 #9
0
	def export(self):

		output_folder = self.list_output_folders_ui.selected

		parent_folder = self.list_images[0].split(os.path.sep)[-2]
		metadata_name = 'metadata#{}'.format(self.key)
		output_file_name = os.path.join(output_folder, "{}_{}.txt".format(parent_folder, metadata_name))
		file_handler.make_ascii_file(metadata=['#Metadata: ' + self.key], 
									 data=self.export_txt,
									 dim='1d',
									 output_file_name=output_file_name)
    def run(self):
        _nbr_profiles = self.parent.ui.tableWidget.rowCount()
        for _profile_index in np.arange(_nbr_profiles):
            _output_file_name = self._create_output_file_name(
                profile_index=_profile_index)
            metadata = self._create_metadata(profile_index=_profile_index)
            data = self._create_data(profile_index=_profile_index)
            make_ascii_file(metadata=metadata,
                            data=data,
                            output_file_name=_output_file_name,
                            dim='1d')

            display(HTML("Exported Profile file {}".format(_output_file_name)))
    def create_new_timestamp_file(self, timestamp_file='', offset=0, new_timestamp_filename=''):
        timestamp_array = self.load_timestamp_file(timestamp_file)
        time_axis = timestamp_array[:,0]
        new_counts_axis = np.roll(np.array(timestamp_array[:,1]), -offset)

        delta_time = time_axis[1] - time_axis[0]

        new_time_axis = np.arange(len(new_counts_axis)) * delta_time

        # bring back axis together
        combined_array = np.stack((new_time_axis, new_counts_axis)).T
        # print("new timesamp_filename is {}".format(new_timestamp_filename))
        make_ascii_file(data=combined_array, output_file_name=new_timestamp_filename,sep='\t')
    def export_data(self, output_folder):
        input_folder = os.path.dirname(
            self.o_norm.data['sample']['file_name'][0])
        ob_folder = os.path.dirname(self.o_norm.data['ob']['file_name'][0])
        output_file_name = self.make_output_file_name(
            output_folder=output_folder, input_folder=input_folder)

        lambda_array = self.lambda_array
        counts_vs_file_index = self.counts_vs_file_index
        tof_array = self.tof_array

        metadata = ["# input folder: {}".format(input_folder)]
        metadata.append("# ob folder: {}".format(ob_folder))

        list_roi = self.list_roi
        if len(list_roi) == 0:
            metadata.append("# Entire sample selected")
        else:
            for index, key in enumerate(list_roi.keys()):
                roi = list_roi[key]
                _x0 = roi['x0']
                _y0 = roi['y0']
                _x1 = roi['x1']
                _y1 = roi['y1']
                metadata.append("# ROI {}: x0={}, y0={}, x1={}, y1={}".format(
                    index, _x0, _y0, _x1, _y1))
        metadata.append("#")
        metadata.append("# tof (micros), lambda (Angstroms), Average counts")

        data = []
        for _t, _l, _c in zip(tof_array, lambda_array, counts_vs_file_index):
            data.append("{}, {}, {}".format(_t, _l, _c))

        file_handler.make_ascii_file(metadata=metadata,
                                     data=data,
                                     output_file_name=output_file_name,
                                     dim='1d')

        if os.path.exists(output_file_name):
            display(
                HTML('<span style="font-size: 20px; color:blue">Ascii file ' +
                     output_file_name + ' has been ' + 'created  </span>'))
        else:
            display(
                HTML(
                    '<span style="font-size: 20px; color:red">Error exporting Ascii file '
                    + output_file_name + '</span>'))
    def copy_and_renamed_fits_files(self, output_dir='./', original_list_of_files=[], prefix='test'):
        current_working_dir = str(Path(original_list_of_files[0]).parent)
        make_or_reset_folder(output_dir)
        log_file = str(Path(output_dir).joinpath("renaming_log.txt"))

        renaming_log_file = ["Renaming schema of folder {}".format(current_working_dir),
                             "old name -> new name", ""]
        for index, _file  in enumerate(original_list_of_files):

            old_name = Path(_file).name
            new_name = Path(output_dir).joinpath(prefix + "_{:05d}.fits".format(index))
            new_short_name = Path(new_name).name
            renaming_log_file.append("{} -> {}".format(old_name, new_short_name))

            # renamed here
            copyfile(_file, new_name)

        make_ascii_file(metadata=renaming_log_file, data=[], output_file_name=log_file)
예제 #14
0
    def extract_all_in_one(self, output_folder):

        display(
            HTML(
                '<span style="font-size: 20px; color:blue">Work in progress ... </span>'
            ))

        self.output_folder_ui.shortcut_buttons.close()
        output_file_name = Extract.create_output_file_name(
            output_folder=output_folder, nbr_nexus=len(self.list_nexus))
        full_list_selected = self.full_list_selected

        # get list of path
        list_entry_path = []
        for top_key in full_list_selected.keys():
            top_path = copy.deepcopy(self.list_parameters[top_key]['path'])
            for inside_key in full_list_selected[top_key]:
                full_path = copy.deepcopy(top_path)
                full_path.append(inside_key)
                # metadata_tag = "# {} -> {}".format(top_key, inside_key)
                list_entry_path.append(full_path)

        list_nexus = self.list_nexus
        metadata = []
        label_of_columns = []
        for _index, _nexus in enumerate(list_nexus):
            _line = [_nexus]

            if _index == 0:
                label_of_columns.append("nexus name")
            reduction_log_dict = Extract.get_entry_value(
                nexus_file_name=_nexus, list_entry_path=list_entry_path)
            for _key in reduction_log_dict.keys():
                _value = reduction_log_dict[_key]
                # print(f"-> _key:{_key}: {_value}")
                # print(f"--> type(_value): {type(_value)}")

                if type(_value) is str:
                    if _index == 0:
                        label_of_columns.append(_key)
                    _line.append(_value)
                elif type(_value) is list:
                    if _index == 0:
                        label_of_columns.append(_key)
                    array_value = np.array(_value)
                    _line.append("{}".format(np.mean(array_value)))
                elif type(_value) is dict:
                    for _inside_key in _value.keys():
                        if _index == 0:
                            label_of_columns.append("{}/{}".format(
                                _key, _inside_key))
                        _line.append("{}".format(np.mean(_value[_inside_key])))

            metadata.append(", ".join(_line))

        label_of_columns = ", ".join(label_of_columns)

        final_metadata = [label_of_columns]
        for _meta in metadata:
            final_metadata.append(_meta)

        make_ascii_file(metadata=final_metadata,
                        output_file_name=output_file_name)

        clear_output(wait=False)
        display(
            HTML(
                '<span style="font-size: 20px; color:blue">The following ASCII file has been created: '
                + '</span><span style="font-size: 20px; color:green">' +
                output_file_name + '</span>'))
예제 #15
0
    def export_data_of_selected_rows(self):

        base_folder = Path(self.parent.working_dir)
        directory = str(base_folder.parent)
        _export_folder = QFileDialog.getExistingDirectory(
            self.parent, directory=directory, caption="Select Output Folder")

        if _export_folder:
            o_table = TableHandler(
                table_ui=self.parent.ui.march_dollase_result_table)
            list_of_rows_selected = o_table.get_rows_of_table_selected()

            str_list_of_rows_selected = [
                str(_row) for _row in list_of_rows_selected
            ]
            str_rows = "_".join(str_list_of_rows_selected)
            output_file_name = os.path.join(
                str(_export_folder),
                "march_dollase_result_fitting_row{}.txt".format(str_rows))

            fitting_input_dictionary = self.parent.fitting_input_dictionary

            o_get = Get(parent=self.parent)
            xaxis_index = o_get.x_axis_data(x_axis_selected='index')
            xaxis_tof = o_get.x_axis_data(x_axis_selected='tof')
            xaxis_lambda = o_get.x_axis_data(x_axis_selected='lambda')

            # metadata
            metadata = ["#Marche Dollase Result of Fitting"]
            is_advance_mode = self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked(
            )
            metadata.append(
                "#Using advanced fitting mode: {}".format(is_advance_mode))

            data_label = "#image index, TOF(micros), Lambda(Angstroms)"
            temp_data = []
            for _row in list_of_rows_selected:
                _entry = fitting_input_dictionary['rois'][_row]['fitting'][
                    'march_dollase']
                _top_entry = fitting_input_dictionary['rois'][_row]
                _line = "#-> row {}: x0: {}, y0:{}, width:{}, height:{}, " \
                        "d_spacing:{}, sigma:{}, alpha:{}, a1:{}, " \
                        "a2:{}".format(_row,
                                                   _top_entry['x0'],
                                                   _top_entry['y0'],
                                                   _top_entry['width'],
                                       _top_entry['height'],
                                                   _entry['d_spacing'],
                                                   _entry['sigma'],
                                                   _entry['alpha'],
                                                   _entry['a1'],
                                                   _entry['a2'])
                if is_advance_mode:
                    _line += ", a5:{}, a6:{}".format(_entry['a5'],
                                                     _entry['a6'])
                metadata.append(_line)
                data_label += ", row {}".format(_row)

                temp_data.append(
                    o_get.y_axis_data_of_selected_row(row_selected=_row))

            metadata.append("#")
            metadata.append(data_label)

            # data
            data = []
            self.parent.debug_data = temp_data

            for _index in np.arange(len(xaxis_index)):
                str_data = "{}, {}, {}".format(xaxis_index[_index],
                                               xaxis_tof[_index],
                                               xaxis_lambda[_index])
                for _col_index in np.arange(len(list_of_rows_selected)):
                    str_data += ", {}".format(temp_data[_col_index][_index])
                data.append(str_data)

            make_ascii_file(metadata=metadata,
                            data=data,
                            output_file_name=output_file_name,
                            dim='1d')

            message = "{} has been created!".format(output_file_name)
            self.parent.ui.statusbar.showMessage(message, 10000)  # 10s