Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion hexrdgui/hexrd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,9 +1289,16 @@ def load_materials(self, f):
beam_energy = valWUnit('beam', 'energy', self.beam_energy, 'keV')
self.materials = load_materials_hdf5(f, kev=beam_energy)

def save_materials(self, f, path=None):
def save_materials_hdf5(self, f, path=None):
save_materials_hdf5(f, self.materials, path)

def save_material_cif(self, material, directory=None, filename=None):
if directory is None:
directory = HexrdConfig().working_dir
if filename is None:
filename = f"{material.name}.cif"
material.write_cif(f"{directory}/{filename}")

def import_materials(self, file_paths: list[Path | str]):
"""Import materials from a list of files

Expand Down
2 changes: 1 addition & 1 deletion hexrdgui/indexing/fit_grains_results_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def full_path(file_name):
HexrdConfig().working_dir = selected_directory

HexrdConfig().save_indexing_config(full_path('workflow.yml'))
HexrdConfig().save_materials(full_path('materials.h5'))
HexrdConfig().save_materials_hdf5(full_path('materials.h5'))
HexrdConfig().save_instrument_config(
full_path('instrument.hexrd'),
# Remove ROIs, since we are saving the imageseries without them
Expand Down
31 changes: 24 additions & 7 deletions hexrdgui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ def setup_connections(self):
self.on_action_open_materials_triggered)
self.ui.action_save_imageseries.triggered.connect(
self.on_action_save_imageseries_triggered)
self.ui.action_save_materials.triggered.connect(
self.on_action_save_materials_triggered)
self.ui.action_save_materials_hdf5.triggered.connect(
self.on_action_save_materials_hdf5_triggered)
self.ui.action_save_materials_cif.triggered.connect(
self.on_action_save_materials_cif_triggered)
self.ui.action_save_state.triggered.connect(
self.on_action_save_state_triggered)
self.ui.action_open_state.triggered.connect(
Expand Down Expand Up @@ -604,10 +606,14 @@ def images_loaded(self, enabled=True):
def on_action_open_materials_triggered(self):
selected_file, selected_filter = QFileDialog.getOpenFileName(
self.ui, 'Load Materials File', HexrdConfig().working_dir,
'HDF5 files (*.h5 *.hdf5)')
'HDF5 files (*.h5 *.hdf5), CIF (*.cif)')
if not selected_file:
return

if selected_file:
HexrdConfig().working_dir = os.path.dirname(selected_file)
HexrdConfig().working_dir = os.path.dirname(selected_file)
if Path(selected_file).suffix == '.cif':
HexrdConfig().import_material(selected_file)
else:
HexrdConfig().load_materials(selected_file)

def on_action_save_imageseries_triggered(self):
Expand All @@ -618,7 +624,7 @@ def on_action_save_imageseries_triggered(self):

SaveImagesDialog(self.ui).exec()

def on_action_save_materials_triggered(self):
def on_action_save_materials_hdf5_triggered(self):
selected_file, selected_filter = QFileDialog.getSaveFileName(
self.ui, 'Save Materials', HexrdConfig().working_dir,
'HDF5 files (*.h5 *.hdf5)')
Expand All @@ -631,7 +637,18 @@ def on_action_save_materials_triggered(self):
if not any(selected_file.endswith(x) for x in acceptable_exts):
selected_file += '.h5'

return HexrdConfig().save_materials(selected_file)
return HexrdConfig().save_materials_hdf5(selected_file)

def on_action_save_materials_cif_triggered(self):
caption = 'Select directory to save CIF files to'
selected_dir = QFileDialog.getExistingDirectory(
self.ui, caption, dir=HexrdConfig().working_dir)
if not selected_dir:
return

HexrdConfig().working_dir = selected_dir
for material in HexrdConfig().materials.values():
HexrdConfig().save_material_cif(material, selected_dir)

def on_action_export_current_plot_triggered(self):
filters = 'HDF5 files (*.h5 *.hdf5);; NPZ files (*.npz)'
Expand Down
27 changes: 27 additions & 0 deletions hexrdgui/material_list_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def setup_connections(self):

self.ui.import_from_cif.clicked.connect(self.import_from_cif)
self.ui.import_from_defaults.clicked.connect(self.import_from_defaults)
self.ui.export_to_cif.clicked.connect(self.export_to_cif)

HexrdConfig().materials_dict_modified.connect(self.update_editor_items)

Expand Down Expand Up @@ -67,6 +68,32 @@ def item_added(self, new_name):
material.name = new_name
HexrdConfig().add_material(new_name, material)

def export_to_cif(self):
selections = self.editor.selected_items
filename = None
if len(selections) == 1:
caption = 'Save CIF file as'
selected_file, _ = QFileDialog.getSaveFileName(
self.ui, caption, HexrdConfig().working_dir,
'CIF files (*.cif)')
filename = str(Path(selected_file).name)
selected_dir = str(Path(selected_file).parent)
else:
caption = 'Select directory to export CIF files to'
selected_dir = QFileDialog.getExistingDirectory(
self.ui, caption, dir=HexrdConfig().working_dir)

if not selected_dir:
return

HexrdConfig().working_dir = selected_dir
# Export selected materials or all materials if none selected
if not selections:
selections = HexrdConfig().materials.values()
for selected in selections:
material = HexrdConfig().material(selected)
HexrdConfig().save_material_cif(material, selected_dir, filename)

def import_from_cif(self):
selected_file, selected_filter = QFileDialog.getOpenFileName(
self.ui, 'Import Material', HexrdConfig().working_dir,
Expand Down
24 changes: 18 additions & 6 deletions hexrdgui/resources/ui/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@
<addaction name="action_save_config_hexrd"/>
<addaction name="action_save_config_yaml"/>
</widget>
<widget class="QMenu" name="menu_save_materials">
<property name="title">
<string>&amp;Materials</string>
</property>
<addaction name="action_save_materials_hdf5"/>
<addaction name="action_save_materials_cif"/>
</widget>
<addaction name="action_save_imageseries"/>
<addaction name="action_save_state"/>
<addaction name="menu_save_config"/>
<addaction name="action_save_materials"/>
<addaction name="menu_save_materials"/>
</widget>
<widget class="QMenu" name="menu_export">
<property name="title">
Expand Down Expand Up @@ -542,11 +549,6 @@
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Show the saturation percentages of the images when in &amp;quot;Image View&amp;quot; mode. The saturation percentages will appear as white text in the bottom left corners of the images.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</action>
<action name="action_save_materials">
<property name="text">
<string>&amp;Materials</string>
</property>
</action>
<action name="action_run_laue_and_powder_calibration">
<property name="enabled">
<bool>false</bool>
Expand Down Expand Up @@ -902,6 +904,16 @@
<string>Apply Median Filter</string>
</property>
</action>
<action name="action_save_materials_hdf5">
<property name="text">
<string>HDF5</string>
</property>
</action>
<action name="action_save_materials_cif">
<property name="text">
<string>CIF</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
17 changes: 12 additions & 5 deletions hexrdgui/resources/ui/material_list_editor.ui
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,29 @@
<number>6</number>
</property>
<item row="2" column="0">
<widget class="QPushButton" name="import_from_defaults">
<widget class="QPushButton" name="import_from_cif">
<property name="text">
<string>Import from Defaults</string>
<string>Import from CIF</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="import_from_cif">
<item row="3" column="0">
<widget class="QPushButton" name="import_from_defaults">
<property name="text">
<string>Import from CIF</string>
<string>Import from Defaults</string>
</property>
</widget>
</item>
<item row="0" column="0">
<layout class="QVBoxLayout" name="list_editor_layout"/>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="export_to_cif">
<property name="text">
<string>Export to CIF</string>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
Expand Down
2 changes: 1 addition & 1 deletion hexrdgui/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def save(h5_file):
_save_config(h5_file, state)

# Write out the materials
HexrdConfig().save_materials(h5_file, '/materials')
HexrdConfig().save_materials_hdf5(h5_file, '/materials')

# Write out the instrument
HexrdConfig().save_instrument_config(h5_file)
Expand Down
Loading