1717requests .get , requests .post , requests .put , requests .delete = authorised_requests ()
1818
1919
20+ def ensure_dcg_exists (transferred_file : Path , environment : MurfeyInstanceEnvironment ):
21+ # Make sure we have a data collection group
22+ source = _get_source (transferred_file , environment = environment )
23+ if not source :
24+ return None
25+ dcg_tag = str (source ).replace (f"/{ environment .visit } " , "" )
26+ url = f"{ str (environment .url .geturl ())} { url_path_for ('workflow.router' , 'register_dc_group' , visit_name = environment .visit , session_id = environment .murfey_session )} "
27+ dcg_data = {
28+ "experiment_type" : "single particle" ,
29+ "experiment_type_id" : 37 ,
30+ "tag" : dcg_tag ,
31+ }
32+ capture_post (url , json = dcg_data )
33+ return dcg_tag
34+
35+
2036class TomographyMetadataContext (Context ):
2137 def __init__ (self , acquisition_software : str , basepath : Path ):
2238 super ().__init__ ("Tomography_metadata" , acquisition_software )
@@ -106,6 +122,7 @@ def post_transfer(
106122
107123 elif transferred_file .name == "SearchMap.xml" and environment :
108124 logger .info ("Tomography session search map xml found" )
125+ dcg_tag = ensure_dcg_exists (transferred_file , environment )
109126 with open (transferred_file , "r" ) as sm_xml :
110127 sm_data = xmltodict .parse (sm_xml .read ())
111128
@@ -174,7 +191,6 @@ def post_transfer(
174191 }
175192
176193 source = _get_source (transferred_file , environment = environment )
177- visitless_source = str (source ).replace (f"/{ environment .visit } " , "" )
178194 image_path = (
179195 _file_transferred_to (
180196 environment , source , transferred_file .parent / "SearchMap.jpg"
@@ -183,11 +199,11 @@ def post_transfer(
183199 else ""
184200 )
185201
186- sm_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomography_router ' , 'register_search_map' , session_id = environment .murfey_session , sm_name = transferred_file .parent .name )} "
202+ sm_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomo_router ' , 'register_search_map' , session_id = environment .murfey_session , sm_name = transferred_file .parent .name )} "
187203 capture_post (
188204 sm_url ,
189205 json = {
190- "tag" : visitless_source ,
206+ "tag" : dcg_tag ,
191207 "x_stage_position" : float (stage_position ["X" ]),
192208 "y_stage_position" : float (stage_position ["Y" ]),
193209 "pixel_size" : sm_pixel_size ,
@@ -201,36 +217,30 @@ def post_transfer(
201217
202218 elif transferred_file .name == "SearchMap.dm" and environment :
203219 logger .info ("Tomography session search map dm found" )
220+ dcg_tag = ensure_dcg_exists (transferred_file , environment )
204221 with open (transferred_file , "r" ) as sm_xml :
205222 sm_data = xmltodict .parse (sm_xml .read ())
206223
207- source = _get_source (transferred_file , environment = environment )
208- visitless_source = str (source ).replace (f"/{ environment .visit } " , "" )
209- if not visitless_source :
210- return
211-
212224 # This bit gets SearchMap size
213225 sm_width = int (sm_data ["TileSetXml" ]["ImageSize" ]["a:width" ])
214226 sm_height = int (sm_data ["TileSetXml" ]["ImageSize" ]["a:height" ])
215227
216- sm_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomography_router ' , 'register_search_map' , session_id = environment .murfey_session , sm_name = transferred_file .parent .name )} "
228+ sm_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomo_router ' , 'register_search_map' , session_id = environment .murfey_session , sm_name = transferred_file .parent .name )} "
217229 capture_post (
218230 sm_url ,
219231 json = {
220- "tag" : visitless_source ,
232+ "tag" : dcg_tag ,
221233 "height" : sm_height ,
222234 "width" : sm_width ,
223235 },
224236 )
225237
226238 elif transferred_file .name == "BatchPositionsList.xml" and environment :
239+ logger .info ("Tomography session batch positions list found" )
240+ dcg_tag = ensure_dcg_exists (transferred_file , environment )
227241 with open (transferred_file ) as xml :
228242 for_parsing = xml .read ()
229243 batch_xml = xmltodict .parse (for_parsing )
230- source = _get_source (transferred_file , environment = environment )
231- visitless_source = str (source ).replace (f"/{ environment .visit } " , "" )
232- if not visitless_source :
233- return
234244
235245 for batch_position in batch_xml ["BatchPositionsList" ]["BatchPositions" ][
236246 "BatchPositionParameters"
@@ -243,11 +253,22 @@ def post_transfer(
243253 batch_stage_location_y = float (
244254 batch_position ["PositionOnTileSet" ]["StagePositionY" ]
245255 )
246- bp_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomography_router' , 'register_batch_position' , session_id = environment .murfey_session , batch_name = batch_name )} "
256+
257+ # Always need search map before batch position
258+ sm_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomo_router' , 'register_search_map' , session_id = environment .murfey_session , sm_name = search_map_name )} "
259+ capture_post (
260+ sm_url ,
261+ json = {
262+ "tag" : dcg_tag ,
263+ },
264+ )
265+
266+ # Then register batch position
267+ bp_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomo_router' , 'register_batch_position' , session_id = environment .murfey_session , batch_name = batch_name )} "
247268 capture_post (
248269 bp_url ,
249270 json = {
250- "tag" : visitless_source ,
271+ "tag" : dcg_tag ,
251272 "x_stage_position" : batch_stage_location_x ,
252273 "y_stage_position" : batch_stage_location_y ,
253274 "x_beamshift" : 0 ,
@@ -268,11 +289,12 @@ def post_transfer(
268289 beamshift_position_x = float (beamshift ["PositionX" ])
269290 beamshift_position_y = float (beamshift ["PositionY" ])
270291
271- bp_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomography_router' , 'register_batch_position' , session_id = environment .murfey_session , batch_name = beamshift_name )} "
292+ # Registration of beamshifted position
293+ bp_url = f"{ str (environment .url .geturl ())} { url_path_for ('session_control.tomo_router' , 'register_batch_position' , session_id = environment .murfey_session , batch_name = beamshift_name )} "
272294 capture_post (
273295 bp_url ,
274296 json = {
275- "tag" : visitless_source ,
297+ "tag" : dcg_tag ,
276298 "x_stage_position" : batch_stage_location_x ,
277299 "y_stage_position" : batch_stage_location_y ,
278300 "x_beamshift" : beamshift_position_x ,
0 commit comments