Skip to content

Commit d5ff4ee

Browse files
committed
Introduce workaround to deal with instruments providing starttime for scans
1 parent e8d5b21 commit d5ff4ee

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

mwr_raw2l1/measurement/scan_transform.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from mwr_raw2l1.utils.num_utils import timedelta2s
1010

1111

12-
def scan_endtime_to_time(endtime, n_angles, time_per_angle=17):
12+
def scan_endtime_to_time(endtime, n_angles, time_per_angle=17, from_starttime=False):
1313
"""
1414
RPG and Attex scan files only have one timestamp per scan. This function returns the approximate timestamp for the
1515
observation at each angle
@@ -20,6 +20,8 @@ def scan_endtime_to_time(endtime, n_angles, time_per_angle=17):
2020
n_angles: number of angles per scan.
2121
time_per_angle: total time for scanning one angle incl. integration time and the time for moving the mirror.
2222
Indicated in seconds. The default is 17.
23+
from_starttime: if True, the timestamps will be calculated assuming the provided time is the start time of
24+
the scan, otherwise from the end time. This arise from the change in timestamping operated in HATPRO instruments (TBC)
2325
2426
Returns:
2527
time : :class:`numpy.ndarray` of :class:`datetime.datetime` objects of end times for each observed angle
@@ -33,11 +35,18 @@ def timedelta_method(seconds):
3335
# use ms as timedelta needs int. Will truncate to ms what should also avoid rounding errors in tests
3436
return np.timedelta64(int(seconds*1000), 'ms')
3537

36-
delta = [timedelta_method(n * time_per_angle) for n in reversed(range(n_angles))]
38+
if from_starttime:
39+
delta = [timedelta_method(n * time_per_angle) for n in range(n_angles)]
40+
else:
41+
delta = [timedelta_method(n * time_per_angle) for n in reversed(range(n_angles))]
3742
delta = np.array(delta)
3843

3944
endtime = endtime.reshape(len(endtime), 1) # for letting numpy broadcast along dimension 1
40-
time = endtime - delta # calculate time for each scan position (matrix)
45+
if from_starttime:
46+
time = endtime + delta # calculate time for each scan position (matrix)
47+
else:
48+
time = endtime - delta
49+
4150
time = time.reshape((-1,)) # make one-dimenional vector out of time matrix
4251

4352
return time
@@ -71,7 +80,7 @@ def scantime_from_aux(blb, hkd=None, brt=None):
7180
scan_duration = timedelta2s(time_last_scan_active[-1] - time_last_scan_active[0])
7281
endtime2time_params['time_per_angle'] = scan_duration / n_ele
7382
elif time_scan_active[0] > time_zen_active[0]: # sure to have full scan at beginning of hkd
74-
scan_duration = timedelta2s(blb.time[0].values - time_scan_active[0])
83+
scan_duration = timedelta2s(time_scan_active[-1] - time_scan_active[0])
7584
endtime2time_params['time_per_angle'] = scan_duration / n_ele
7685
else:
7786
logger.warning(
@@ -86,7 +95,14 @@ def scantime_from_aux(blb, hkd=None, brt=None):
8695
else:
8796
logger.warning(
8897
'Cannot infer scan duration as first scan might extend to previous period. Using default values')
89-
98+
99+
# TODO: remove the workaround below once we know for sure if time in .BLB is start or end time of the scans.
100+
if np.abs(timedelta2s(time_scan_active[0] - time_scan[0])) > 50:
101+
logger.info('Assuming that the time in BLB file is the endtime of the scan: TBC')
102+
else:
103+
endtime2time_params = dict(endtime=time_scan, n_angles=n_ele, time_per_angle=endtime2time_params['time_per_angle'], from_starttime=True)
104+
logger.info('Assuming that the time in BLB file is the starttime of the scan: TBC')
105+
90106
return scan_endtime_to_time(**endtime2time_params)
91107

92108

0 commit comments

Comments
 (0)