@@ -189,15 +189,14 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
189189 n_declared = total_packets
190190 n_observed_file = length(packet_starti )
191191
192- # validate packet headers (i.e., nr. of appearances of MDTCPACK in files)
193- # in case that empty packets have been added at the end, only use valid packets
194- n_use = min(total_packets , length(packet_starti ))
192+ # Use all packets available in the file (even if empty packets have been added at the end)
193+ n_use = n_observed_file
195194 packet_starti = packet_starti [seq_len(n_use )]
196195 packet_endi = packet_endi [seq_len(n_use )]
197196
198197 # Apply chunking AFTER truncation
199198 i_from = max(1 , start )
200- i_to = if (is.null( end )) n_use else min( end , n_use )
199+ i_to = n_use
201200 idx = seq.int(i_from , i_to )
202201 n_rows = length(idx )
203202
@@ -257,7 +256,8 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
257256 gap_with_previous_block_secs = numeric (n_rows ),
258257 start_time_adjustment_secs = numeric (n_rows ),
259258 declared_packets = rep(n_declared , n_rows ),
260- observed_packets = rep(n_observed_file , n_rows )
259+ observed_packets = rep(n_observed_file , n_rows ),
260+ acc_recorded = TRUE
261261 )
262262
263263 # log corrupt packets
@@ -299,7 +299,6 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
299299 seek(con , packet_starti [i ] + 31 , " start" ) # Move to correct position
300300 readBin(con , what = " integer" , size = 4 , endian = " little" )
301301 }, numeric (1 ))
302-
303302 # Process timestamps and calculate sampling frequency
304303 packets_dur_s = packets_t1 - packets_t0
305304 sf_acc_observed = acc_count / packets_dur_s
@@ -312,6 +311,20 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
312311 QClog [, " frequency_set" ] = sf
313312 QClog [, " frequency_observed" ] = sf_acc_observed
314313
314+ # At the moment the function requires that accelerometer data has been
315+ # recorded to work, as accelerometer timestamps are used as reference time
316+ if (sum(acc_count ) == 0 ) {
317+ QClog [, " acc_recording" ] = FALSE
318+ return (list (
319+ QClog = QClog ,
320+ data = matrix (nrow = 0 , ncol = 3 ),
321+ header = list (sf = sf ,
322+ acc_dynrange = acc_dynrange ,
323+ starttime = starttime_posix ),
324+ lastchunk = TRUE
325+ ))
326+ }
327+
315328 # build data structure lists
316329 # acc
317330 acc_starts = packet_starti + 36 ; acc_stops = acc_starts + 6 * acc_count - 1
@@ -408,7 +421,11 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
408421 } else {
409422 # if the first packet is corrupted, then impute by first non-corrupted temperature observed
410423 first_valid_temp = which(! 1 : length(temp_count ) %in% corrupt_packets )[1 ]
411- prev_temp = temp_readings [[first_valid_temp ]][1 ,]
424+ if (is.na(first_valid_temp )) {
425+ prev_temp = NA
426+ } else {
427+ prev_temp = temp_readings [[first_valid_temp ]][1 ,]
428+ }
412429 }
413430 # next index
414431 next_index = ifelse(any(! is.na(x [(i + 1 ): length(x )])),
@@ -422,15 +439,17 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
422439 } else {
423440 next_temp = NA
424441 }
425-
426442 if (! is.na(prev_temp [1 ]) & ! is.na(next_temp [1 ])) {
427443 mean_temp = (prev_temp + next_temp ) / 2
428- } else if (is.na(prev_temp [1 ])) {
444+ } else if (is.na(prev_temp [1 ]) & ! is.na( next_temp [ 1 ]) ) {
429445 mean_temp = next_temp
430- } else if (is.na(next_temp [1 ])) {
446+ } else if (! is.na( prev_temp [ 1 ]) & is.na(next_temp [1 ])) {
431447 # if the last packet is corrupted, then impute by last temperature observed
432448 mean_temp = prev_temp
433- }
449+ } else {
450+ # safe conduct, if both are NA, then leave it without imputation
451+ mean_temp = temp_readings [[i ]]
452+ }
434453
435454 temp_readings [[i ]] = matrix (mean_temp , nrow = nrow(temp_readings [[i ]]),
436455 ncol = 2 , byrow = T )
0 commit comments