2424except ImportError :
2525 Image = None
2626
27+ try :
28+ from smartem_backend .api_client import SmartEMAPIClient
29+ from smartem_common .schemas import (
30+ AcquisitionData as SmartEMAcquisitionData ,
31+ GridData as SmartEMGridData ,
32+ MicrographData as SmartEMMicrographData ,
33+ MicrographManifest as SmartEMMicrographManifest ,
34+ )
35+
36+ SMARTEM_ACTIVE = True
37+ except ImportError :
38+ SMARTEM_ACTIVE = False
39+
2740import murfey .server .prometheus as prom
2841from murfey .server import _transport_object
2942from murfey .server .api .auth import (
7891
7992logger = getLogger ("murfey.server.api.workflow" )
8093
94+
8195router = APIRouter (
8296 prefix = "/workflow" ,
8397 dependencies = [Depends (validate_instrument_token )],
@@ -92,6 +106,8 @@ class DCGroupParameters(BaseModel):
92106 atlas : str = ""
93107 sample : Optional [int ] = None
94108 atlas_pixel_size : float = 0
109+ create_smartem_grid : bool = False
110+ acquisition_uuid : Optional [str ] = None
95111
96112
97113@router .post (
@@ -110,6 +126,35 @@ def register_dc_group(
110126 db .exec (select (Session ).where (Session .id == session_id )).one ().instrument_name
111127 )
112128 logger .info (f"Registering data collection group on microscope { instrument_name } " )
129+ smartem_grid_uuid = None
130+ if (
131+ dcg_params .create_smartem_grid
132+ and SMARTEM_ACTIVE
133+ and dcg_params .acquisition_uuid
134+ ):
135+ machine_config = get_machine_config (instrument_name = instrument_name )[
136+ instrument_name
137+ ]
138+ if machine_config .smartem_api_url :
139+ try :
140+ smartem_client = SmartEMAPIClient (
141+ base_url = machine_config .smartem_api_url , logger = logger
142+ )
143+ grid_data = SmartEMGridData (
144+ data_dir = Path (dcg_params .tag ),
145+ atlas_dir = Path (dcg_params .atlas ) if dcg_params .atlas else None ,
146+ acquisition_data = SmartEMAcquisitionData (
147+ uuid = dcg_params .acquisition_uuid ,
148+ name = f"{ visit_name } -sample-{ dcg_params .sample } "
149+ if dcg_params .sample
150+ else f"{ visit_name } -sample-unknown" ,
151+ ),
152+ )
153+ smartem_grid_uuid = smartem_client .create_acquisition_grid (
154+ grid_data
155+ ).uuid
156+ except Exception :
157+ logger .warning ("Failed to register SmartEM grid" , exc_info = True )
113158 if (
114159 dcg_murfey := db .exec (
115160 select (DataCollectionGroup )
@@ -135,6 +180,8 @@ def register_dc_group(
135180 dcg_instance .atlas_pixel_size = (
136181 dcg_params .atlas_pixel_size or dcg_instance .atlas_pixel_size
137182 )
183+ if smartem_grid_uuid :
184+ dcg_instance .smartem_grid_uuid = smartem_grid_uuid
138185
139186 if _transport_object :
140187 if dcg_instance .atlas_id is not None :
@@ -217,6 +264,11 @@ def register_dc_group(
217264 "proposal_code" : ispyb_proposal_code ,
218265 "proposal_number" : ispyb_proposal_number ,
219266 "visit_number" : ispyb_visit_number ,
267+ ** (
268+ {"smartem_grid_uuid" : smartem_grid_uuid }
269+ if smartem_grid_uuid
270+ else {}
271+ ),
220272 },
221273 )
222274 return dcg_params
@@ -491,6 +543,58 @@ async def request_spa_preprocessing(
491543 )
492544 db .add (movie )
493545 db .commit ()
546+
547+ if (
548+ SMARTEM_ACTIVE
549+ and machine_config .smartem_api_url
550+ and foil_hole_id is not None
551+ ):
552+ try :
553+ fh_with_gs = db .exec (
554+ select (FoilHole , GridSquare )
555+ .where (FoilHole .id == foil_hole_id )
556+ .where (GridSquare .id == FoilHole .grid_square_id )
557+ ).one_or_none ()
558+ if fh_with_gs is not None :
559+ fh , gs = fh_with_gs
560+ if fh .smartem_uuid :
561+ smartem_client = SmartEMAPIClient (
562+ base_url = machine_config .smartem_api_url , logger = logger
563+ )
564+ movie_path = Path (proc_file .path )
565+ micrograph_manifest = SmartEMMicrographManifest (
566+ unique_id = movie_path .stem ,
567+ acquisition_datetime = datetime .now (),
568+ defocus = None ,
569+ detector_name = "" ,
570+ energy_filter = True ,
571+ phase_plate = False ,
572+ image_size_x = None ,
573+ image_size_y = None ,
574+ binning_x = 1 ,
575+ binning_y = 1 ,
576+ )
577+ micrograph_data = SmartEMMicrographData (
578+ id = movie_path .stem ,
579+ gridsquare_id = str (gs .name ),
580+ foilhole_uuid = fh .smartem_uuid ,
581+ foilhole_id = str (fh .name ),
582+ location_id = str (murfey_ids [0 ]),
583+ high_res_path = movie_path ,
584+ manifest_file = movie_path ,
585+ manifest = micrograph_manifest ,
586+ )
587+ response = smartem_client .create_foilhole_micrograph (
588+ micrograph_data
589+ )
590+ movie .smartem_uuid = response .uuid
591+ db .add (movie )
592+ db .commit ()
593+ except Exception :
594+ logger .warning (
595+ "Failed to register micrograph with smartem" , exc_info = True
596+ )
597+
494598 db .close ()
495599
496600 if not mrc_out .parent .exists ():
0 commit comments