Skip to content

Commit

Permalink
Read data_collection_date from master file as start_time (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpks authored Dec 6, 2023
1 parent ddadf7f commit fa49d2e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/nexgen/beamlines/ED_singla_nxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from ..nxs_write.NXmxWriter import EDNXmxFileWriter
from ..nxs_write.write_utils import find_number_of_images
from ..tools.ED_tools import extract_from_SINGLA_master, find_beam_centre
from ..tools.ED_tools import extract_start_time_from_master
from ..tools.ED_tools import extract_exposure_time_from_master
from ..utils import coerce_to_path, find_in_dict, get_iso_timestamp, get_nexus_filename
from .ED_params import ED_coord_system, EDSingla, EDSource

Expand Down Expand Up @@ -100,7 +102,7 @@ def singla_nexus_writer(
if find_in_dict("start_time", params):
start_time = get_iso_timestamp(params["start_time"])
else:
start_time = None
start_time = extract_start_time_from_master(master_file)

# Update source if new info passed
source = EDSource
Expand Down Expand Up @@ -145,6 +147,13 @@ def singla_nexus_writer(
det_axes = EDSingla.det_axes
det_axes[0].start_pos = det_distance

if not exp_time:
logger.warning("Exposure time not set, trying to read it from the master file.")
exp_time = extract_exposure_time_from_master(master_file)

if not exp_time:
raise ValueError("Exposure time not provided. No 'count_time' in the master file.")

# Define detector
detector = Detector(
det_params,
Expand Down
70 changes: 65 additions & 5 deletions src/nexgen/tools/ED_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
import h5py
import hdf5plugin # noqa: F401
import numpy as np
from datetime import datetime
from numpy.typing import ArrayLike
import logging

logger = logging.getLogger("nexgen.EDtools.Singla")
logger.setLevel(logging.DEBUG)


class SinglaMaster:
Expand Down Expand Up @@ -123,7 +128,7 @@ def get_exposure_time(self) -> float:
_loc = [obj for obj in self.walk if "count_time" in obj]
if len(_loc) == 0:
return None
return self.__getitem__(_loc[0])[()]
return float(self.__getitem__(_loc[0])[()])

def get_photon_energy(self) -> float:
_loc = [obj for obj in self.walk if "photon_energy" in obj]
Expand All @@ -143,6 +148,65 @@ def get_software_version(self) -> bytes:
return None
return self.__getitem__(_loc[0])[()]

def get_data_collection_date(self) -> str:
_loc = [obj for obj in self.walk if "data_collection_date" in obj]
if len(_loc) == 0:
return None
else:
collection_date = str(self.__getitem__(_loc[0])[()])[2:21]
collection_date = datetime.strptime(collection_date,
'%Y-%m-%dT%H:%M:%S')
return collection_date


def extract_exposure_time_from_master(master: Path | str) -> float:
"""
Extracts `count_time` from the master file
Args:
master (Path | str): Path to Singla master file.
Returns:
exposure time (float): In the master file this
is recorded as `count_time`.
"""

if SinglaMaster.isDectrisSingla(master) is False:
logger.warning(f"The file {master} is the wrong format.")
return

exposure_time = None

with h5py.File(master, "r") as fh:
singla = SinglaMaster(fh)
exposure_time = singla.get_exposure_time()

return exposure_time


def extract_start_time_from_master(master: Path | str) -> datetime:
"""
Extracts `data_collection_date` from the master file
Args:
master (Path | str): Path to Singla master file.
Returns:
data collection date: Datetime object.
"""

if SinglaMaster.isDectrisSingla(master) is False:
logger.warning(f"The file {master} is the wrong format.")
return

start_time = None

with h5py.File(master, "r") as fh:
singla = SinglaMaster(fh)
start_time = singla.get_data_collection_date()

return start_time


def extract_from_SINGLA_master(master: Path | str) -> Dict[str, Any]:
"""
Expand All @@ -155,10 +219,6 @@ def extract_from_SINGLA_master(master: Path | str) -> Dict[str, Any]:
Returns:
Dict[str, Any]: Dictionary of information relative to the detector.
"""
import logging

logger = logging.getLogger("nexgen.EDtools.Singla")
logger.setLevel(logging.DEBUG)

if SinglaMaster.isDectrisSingla(master) is False:
logger.warning(f"The file {master} is the wrong format.")
Expand Down

0 comments on commit fa49d2e

Please sign in to comment.