Coverage for src/susi/utils/timestamps.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2025-06-13 14:15 +0000

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3""" 

4Helper functions for working with timestamps from SUSI HK 

5 

6@author: feller 

7""" 

8 

9import numpy as np 

10from datetime import datetime, timedelta 

11 

12from ..base.header_keys import * 

13 

14# Conversion factor from microsecond [US] to second [S] 

15US2S = 1e-6 

16MS2S = 1e-3 

17 

18 

19class Timestamps: 

20 """ 

21 Helper class for working with timestamps from SUSI HK 

22 """ 

23 

24 @staticmethod 

25 def timestamps_utc(batch): 

26 """ 

27 Get list of timestamps from batch. 

28 The 'Timestamp ms' FITS entries are converted to a list of datetime objects, and 

29 referenced to the 'DATE_OBS' entry of the first file of the batch. 

30 

31 :param batch: FitsBatch object 

32 """ 

33 timestamps = batch.header_field(TIMESTAMP_MS) 

34 timestamps = np.array([int(t) for t in timestamps]) 

35 timestamps -= timestamps[0] 

36 timestamps = [timedelta(milliseconds=int(t)) for t in timestamps] 

37 time_start = datetime.fromisoformat(batch.header_content(DATE_OBS)[0]) 

38 return [time_start + t for t in timestamps] 

39 

40 @staticmethod 

41 def tstmp2datetime(tstmp: int, dtype: str = 's') -> datetime: 

42 """Convert a given timestamp to a datetime object""" 

43 if dtype == 'us': 

44 tstmp = tstmp * US2S 

45 if dtype == 'ms': 

46 tstmp = tstmp * MS2S 

47 return datetime.utcfromtimestamp(tstmp)