-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrim_json.py
52 lines (36 loc) · 1.17 KB
/
trim_json.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Trim raw SnapperGPS data to a time interval.
@author: Jonas Beuchert
"""
import json
import glob
import os
import numpy as np
# Path to JSON files (adjust)
directory = "/path/to/directory/"
files = glob.glob(os.path.join(directory, "*.json"))
# Set start & end time
start = np.datetime64('2021-08-22T10:08:00.000')
end = np.datetime64('2021-08-22T10:22:00.000')
# Loop over all files
for file in files:
# Read JSON file
with open(file, "r") as fp:
data = json.load(fp)
# Count snapshots
n = len(data["snapshots"])
# Get all timestamps
dts = [np.datetime64(data["snapshots"][idx]["timestamp"])
for idx in range(n)]
# Get all timestamps in given interval
good = [dt >= start and dt <= end for dt in dts]
good_idx = np.where(good)[0]
# Select corresponding snapshots
new_snapshots = [data["snapshots"][idx]
for idx in good_idx]
data["snapshots"] = new_snapshots
# Save to new JSON file (pre-pend underscore)
base_file_name = os.path.basename(file)
with open(os.path.join(directory, "_" + base_file_name), "w") as fp:
json.dump(data, fp, indent=4)