Inserting Timestamps into Old Agilent Files

· ☕ 3 min read · ✍️ Bence

An important part of iolite is the time ordering of data. This allows iolite to accurately model time-dependent phenomena such as sensitivity drift, mass fractionation and baseline variation. Unfortunately, some older Agilent files may not include a timestamp. This may not be a problem if you need to load just a single file: there is always a record within the file of when each datapoint was collected relative to the file start time. However, iolite still needs to know when the file started. You could just add a dummy line like the one below as the third line in the file so that iolite can load the file, and all the internal timing will be consistent:

Acquired : 2020-01-11 12:48:10 using Batch FakeTimeStamp.b

However, if you have multiple files from the same analytical session, you need a more accurate timestamp so that iolite can import the files in their correct time order. One approach used here is to make use of the file’s metadata. Every file stores the date that it was created. If this timestamp reflects roughly the time the sample was run, we can use this as our timestamp. I’ve written a short script below that you can use to insert the ‘Acquired …’ line from above, with the file’s creation date as the timestamp.

There are a couple of things to note though: sometimes a file’s creation date is reset, either by copying it, or sometimes by moving it between operating systems (e.g. sending it by email). In this case, the file creation times are very recent, or they are all within a few seconds of one another, so they’re easy to spot. If the file creation times look right, you can use the approach below.

The script assumes that you’ve got a folder containing the .csv files (the ‘input’ folder). It will also ask you where you want it to write out the edited copies (the ‘output’ folder). The output folder needs to exist before you run the script.

NOTE: Any files in the output folder with the same name will be overwritten

If there are already files in your output folder and they have the same name as the file being edited, the earlier file will be overwritten. I suggest creating a new output folder for each input folder.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from iolite.QtGui import QFileDialog
import csv
import pathlib
import time
import glob

in_folder_path = QFileDialog.getExistingDirectory(None, "Select INPUT Directory")
out_folder_path = QFileDialog.getExistingDirectory(None, "Select OUTPUT Directory")
print(in_folder_path)
print(out_folder_path)

for csv_file in glob.glob(in_folder_path+"/*.csv"):
    print(f"Editing {csv_file}")
    file = pathlib.Path(csv_file)
    file_name = file.parts[-1]
    out_file_name = file_name.replace('.', '_edit.')
    out_file_path = out_folder_path + "/" + out_file_name

    file_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(file.stat().st_mtime))
    print(f"Inserting this timestamp: {file_time_str}")
    toAdd = ["Acquired      : {} using Batch FakeTimeStamp.b".format(file_time_str)]

    with open(csv_file, "r") as infile:
        reader = list(csv.reader(infile))
        reader.insert(2, toAdd)

    with open(out_file_path, "w", newline='') as outfile:
        writer = csv.writer(outfile)
        for line in reader:
            writer.writerow(line)

If you have any questions, comments or suggestions about this Note, you can discuss it here


iolite team
WRITTEN BY
Bence
iolite developer


What's on this Page