11from __future__ import annotations
22
33import asyncio
4+ import logging
45import os
56from enum import Enum
67from pathlib import Path
78from typing import Dict , Tuple
89
910from murfey .util import secure_path
1011
12+ logger = logging .getLogger ("murfey.server.gain" )
13+
1114
1215class Camera (Enum ):
1316 K3_FLIPX = 1
@@ -30,8 +33,10 @@ async def prepare_gain(
3033 tag : str = "" ,
3134) -> Tuple [Path | None , Path | None ]:
3235 if not all (executables .get (s ) for s in ("dm2mrc" , "clip" , "newstack" )):
36+ logger .error ("No executables were provided to prepare the gain reference with" )
3337 return None , None
3438 if camera == Camera .FALCON :
39+ logger .info ("Gain reference preparation not needed for Falcon detector" )
3540 return None , None
3641 if gain_path .suffix == ".dm4" :
3742 gain_out = (
@@ -63,6 +68,10 @@ async def prepare_gain(
6368 )
6469 stdout , stderr = await dm4_proc .communicate ()
6570 if dm4_proc .returncode :
71+ logger .error (
72+ "Error encountered while trying to process the gain reference with 'dm2mrc': \n "
73+ f"{ stderr .decode ('utf-8' ).strip ()} "
74+ )
6675 return None , None
6776 clip_proc = await asyncio .create_subprocess_shell (
6877 f"{ executables ['clip' ]} { flip } { secure_path (gain_path_mrc )} { secure_path (gain_path_superres ) if rescale else secure_path (gain_out )} " ,
@@ -71,6 +80,10 @@ async def prepare_gain(
7180 )
7281 stdout , stderr = await clip_proc .communicate ()
7382 if clip_proc .returncode :
83+ logger .error (
84+ "Error encountered while trying to process the gain reference with 'clip': \n "
85+ f"{ stderr .decode ('utf-8' ).strip ()} "
86+ )
7487 return None , None
7588 if rescale :
7689 newstack_proc = await asyncio .create_subprocess_shell (
@@ -80,6 +93,10 @@ async def prepare_gain(
8093 )
8194 await newstack_proc .communicate ()
8295 if newstack_proc .returncode :
96+ logger .error (
97+ "Error encountered while trying to process the gain reference with 'newstack': \n "
98+ f"{ stderr .decode ('utf-8' ).strip ()} "
99+ )
83100 return None , None
84101 if rescale :
85102 secure_path (gain_out_superres ).symlink_to (secure_path (gain_path_superres ))
@@ -91,6 +108,9 @@ async def prepare_eer_gain(
91108 gain_path : Path , executables : Dict [str , str ], env : Dict [str , str ], tag : str = ""
92109) -> Tuple [Path | None , Path | None ]:
93110 if not executables .get ("tif2mrc" ):
111+ logger .error (
112+ "No executables were provided to prepare the EER gain reference with"
113+ )
94114 return None , None
95115 gain_out = (
96116 gain_path .parent / f"gain_{ tag } .mrc" if tag else gain_path .parent / "gain.mrc"
@@ -100,7 +120,11 @@ async def prepare_eer_gain(
100120 mrc_convert = await asyncio .create_subprocess_shell (
101121 f"{ executables ['tif2mrc' ]} { secure_path (gain_path )} { secure_path (gain_out )} "
102122 )
103- await mrc_convert .communicate ()
123+ stdout , stderr = await mrc_convert .communicate ()
104124 if mrc_convert .returncode :
125+ logger .error (
126+ "Error encountered while trying to process the EER gain reference: \n "
127+ f"{ stderr .decode ('utf-8' ).strip ()} "
128+ )
105129 return None , None
106130 return gain_out , None
0 commit comments