1616from hexrdgui .ui_loader import UiLoader
1717from hexrdgui .utils .conversions import angles_to_cart , cart_to_angles
1818from hexrdgui .utils .dicts import ndarrays_to_lists
19+ from hexrdgui .utils .tth_distortion import apply_tth_distortion_if_needed
1920
2021
2122class HKLPicksTreeViewDialog :
@@ -92,18 +93,28 @@ def export_picks_clicked(self):
9293 return self .export_picks (selected_file )
9394
9495 def export_picks (self , filename ):
96+ return self ._export_dict_to_file (filename , {
97+ 'angular' : self .dictionary ,
98+ 'cartesian' : self .dict_with_cart_coords ,
99+ })
100+
101+ def export_picks_from_overlays (self , filename , overlays ):
102+ # Export picks from overlays using the same export logic as
103+ # the regular dictionary.
104+ return self ._export_dict_to_file (filename , {
105+ 'angular' : overlays_to_tree_format (overlays , polar = True ),
106+ 'cartesian' : overlays_to_tree_format (overlays , polar = False ),
107+ })
108+
109+ def _export_dict_to_file (self , filename : str , export_data : dict ):
95110 filename = Path (filename )
96111
97112 if filename .exists ():
98113 filename .unlink ()
99114
100- # unwrap_dict_to_h5 unfortunately modifies the data
101- # make a deep copy to avoid the modification.
102- export_data = {
103- 'angular' : copy .deepcopy (self .dictionary ),
104- 'cartesian' : self .dict_with_cart_coords ,
105- }
106-
115+ # unwrap_dict_to_h5 unfortunately modifies the data.
116+ # Make a deep copy to avoid the modification.
117+ export_data = copy .deepcopy (export_data )
107118 with h5py .File (filename , 'w' ) as wf :
108119 unwrap_dict_to_h5 (wf , export_data )
109120
@@ -178,7 +189,7 @@ def button_box_visible(self, b):
178189 self .ui .button_box .setVisible (b )
179190
180191
181- def convert_picks (picks , conversion_function , ** kwargs ):
192+ def convert_picks (picks , conversion_function ):
182193 instr = create_hedm_instrument ()
183194 ret = copy .deepcopy (picks )
184195 for name , detectors in ret .items ():
@@ -191,25 +202,43 @@ def convert_picks(picks, conversion_function, **kwargs):
191202 # Avoid the runtime warning
192203 hkls [hkl ] = [np .nan , np .nan ]
193204 else :
194- hkls [hkl ] = conversion_function ([spot ], panel ,
195- ** kwargs )[0 ]
205+ hkls [hkl ] = conversion_function ([spot ], panel )[0 ]
196206 continue
197207
198208 # Must be powder
199209 for hkl , line in hkls .items ():
200210 if len (line ) != 0 :
201- hkls [hkl ] = conversion_function (line , panel , ** kwargs )
211+ hkls [hkl ] = conversion_function (line , panel )
202212
203213 return ret
204214
205215
206216def picks_angles_to_cartesian (picks ):
207- return convert_picks (picks , angles_to_cart )
217+ # Create the conversion function
218+ def func (angs , panel ):
219+ # Reverse the tth distortion first
220+ angs = apply_tth_distortion_if_needed (
221+ angs ,
222+ in_degrees = True ,
223+ reverse = True ,
224+ )
225+ # Now convert to cart
226+ return angles_to_cart (angs , panel )
227+
228+ return convert_picks (picks , func )
208229
209230
210231def picks_cartesian_to_angles (picks ):
211- kwargs = {'eta_period' : HexrdConfig ().polar_res_eta_period }
212- return convert_picks (picks , cart_to_angles , ** kwargs )
232+ # Create the conversion function
233+ eta_period = HexrdConfig ().polar_res_eta_period
234+
235+ def func (xys , panel ):
236+ angs = cart_to_angles (xys , panel , eta_period = eta_period )
237+
238+ # Apply tth distortion now as well
239+ return apply_tth_distortion_if_needed (angs , in_degrees = True )
240+
241+ return convert_picks (picks , func )
213242
214243
215244def generate_picks_results (overlays , polar = True ):
@@ -249,8 +278,8 @@ def generate_picks_results(overlays, polar=True):
249278 return pick_results
250279
251280
252- def overlays_to_tree_format (overlays ):
253- picks = generate_picks_results (overlays )
281+ def overlays_to_tree_format (overlays , polar = True ):
282+ picks = generate_picks_results (overlays , polar = polar )
254283 return picks_to_tree_format (picks )
255284
256285
0 commit comments