@@ -106,6 +106,37 @@ def _calc_time(year, month, day, hour, minute, second, usec, zero_is_bad=True):
106106 return dt
107107
108108
109+ def __check_header (infile_obj , eof ):
110+ def find_all (s , c ):
111+ idx = s .find (c )
112+ while idx != - 1 :
113+ yield idx
114+ idx = s .find (c , idx + 1 )
115+
116+ # Save current position
117+ current_pos = infile_obj .tell ()
118+
119+ # Close and reopen file with larger buffer
120+ infile_obj .close ()
121+ int32_max = np .iinfo (np .int32 ).max
122+ if eof >= int32_max :
123+ buffer = int32_max
124+ else :
125+ buffer = eof
126+ fin = open (_abspath (infile_obj .name ), "rb" , buffer )
127+ fin .seek (current_pos , 1 )
128+
129+ # Search for multiple saved headers
130+ pk = fin .peek (1 )
131+ found = [i for i in find_all (pk , b"GETCLOCKSTR" )]
132+ if found :
133+ start_idx = found [0 ] - 11 # assuming next header is 10 bytes
134+ else :
135+ start_idx = 0
136+
137+ return fin , start_idx
138+
139+
109140def _create_index (infile , outfile , init_pos , eof , debug ):
110141 logging = getLogger ()
111142 print ("Indexing {}..." .format (infile ), end = "" )
@@ -156,8 +187,20 @@ def _create_index(infile, outfile, init_pos, eof, debug):
156187 fin .seek (- 10 , 1 )
157188 dat = struct .unpack ("<BBBBIhh" , fin .read (12 ))
158189 else :
190+ # Lost header
159191 if debug :
160192 logging .debug ("Header is not 10 or 12 bytes: %10d\n " % (pos ))
193+ # check for if file was terminated and restarted
194+ # by searching for start of header configuration string
195+ fin , to_skip = __check_header (fin , eof )
196+ if to_skip :
197+ fin .seek (to_skip , 1 )
198+ if debug :
199+ logging .debug (
200+ "Found new header string as position: %10d\n "
201+ % (pos + to_skip )
202+ )
203+ continue
161204 if dat [2 ] in ids :
162205 idk = dat [2 ]
163206 # version, byte offset to actual data, configuration bit mask
0 commit comments