diff --git a/src/cristallina/utils.py b/src/cristallina/utils.py index 072f617..cffc0c9 100644 --- a/src/cristallina/utils.py +++ b/src/cristallina/utils.py @@ -5,6 +5,9 @@ import re from pathlib import Path from collections import defaultdict +import requests +import datetime + import logging logger = logging.getLogger() @@ -663,3 +666,18 @@ def FWHM_to_sigma(FWHM): """FWHM to gaussian sigma""" sigma = FWHM / 2.355 return sigma + + +def pulseid_to_timestamp(pulse_id): + """ Converts pulse id to datetime using the data-api server. + """ + + r = requests.get(f"https://data-api.psi.ch/api/4/map/pulse/sf-databuffer/{pulse_id}") + try: + ns_timestamp = r.json() + except JSONDecodeError as e: + raise ValueError(f"Cannot convert pulse id {pulse_id} to timestamp.") + + return datetime.datetime.fromtimestamp(ns_timestamp/1E9) + +