-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzip_proc.py
More file actions
371 lines (299 loc) · 10.2 KB
/
zip_proc.py
File metadata and controls
371 lines (299 loc) · 10.2 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""
Script to process AIS data from http://web.ais.dk/aisdata/.
We extract csv files from zip files, convert csv files to parquet and then
resample them by 15m, 30m and 1h.
We resample by the `# Timestamp` and `MMSI` columns (time & unique id).
Final data is stored on tresorit; see link in e-mail.
The script is run on the SODAS server.
"""
import datetime
import os
import sys
import warnings
import zipfile
from pathlib import Path
import click
import polars as pl
import tabulate
from loguru import logger
KU_ID = os.getenv("KUID")
fp_ais = Path(f"/home/{KU_ID}/main-compute/ais-proc")
fp_sdir = Path(f"/home/{KU_ID}/ukraine-sdir/AIS_DK")
fp_exp = Path(f"/home/{KU_ID}/ukraine-sdir/explore-ais")
fp_test = fp_exp.joinpath("tests")
if not Path.home().name == "jsr-p": # Only log on server
logger.remove()
logger.add(
sys.stdout,
level="INFO",
)
logger.add(
sink=fp_ais / "log.log",
rotation="50 MB",
backtrace=True,
diagnose=True,
serialize=False,
level="INFO",
)
# Suppress specific DeprecationWarning from polars
warnings.filterwarnings(
"ignore", category=DeprecationWarning, message=".*old streaming engine.*"
)
years = [
"2021",
"2022",
"2023",
"2024",
"2025",
]
cats = [
"IMO", # IMO number of the vessel
"Callsign", # Callsign of the vessel
"Name", # Name of the vessel
"Destination", # Destination of the vessel
]
nums = [
"ROT",
"Heading",
"SOG",
"COG",
"A",
"B",
"C",
"D",
"Width",
"Length",
"Draught",
]
def proc_ais(df: pl.DataFrame):
return df.with_columns(
pl.col("# Timestamp").str.to_datetime(
# format="%Y-%m-%d %H:%M:%S"
format="%d/%m/%Y %H:%M:%S"
),
pl.col("ETA").str.to_datetime(format="%d/%m/%Y %H:%M:%S"),
).with_columns(
pl.col(nums).cast(pl.Float64),
# NOTE: If categorical we'll get some problems when concatenating
pl.col(cats).cast(pl.Utf8),
pl.col("MMSI").cast(pl.Utf8),
)
def sink_csv(csv_path: Path, pq_path: Path):
pl.scan_csv(csv_path).sink_parquet(pq_path)
logger.info(f"Sinked {csv_path} to {pq_path}")
def proc_zip(zip_path: Path, output_dir: Path):
"""
Extracts the CSV files from a ZIP archive and sinks them as Parquet files.
"""
with zipfile.ZipFile(zip_path, "r") as z:
names = z.namelist()
logger.info(f"Extracting {len(names)} files from {zip_path}")
for filename in names:
if filename.endswith(".csv"):
csv_out = Path.joinpath(output_dir, filename)
pqfile = csv_out.with_suffix(".parquet")
if pqfile.exists():
logger.info(f"File {pqfile} already exists")
continue
logger.info(f"Extracting {filename}...")
z.extract(filename, output_dir)
logger.info(f"Extracted {filename} to {output_dir}")
sink_csv(csv_out, pqfile)
csv_out.unlink()
logger.info(f"Deleted {filename}")
def extract_and_sink(zip_path: Path, output_dir: Path):
try:
proc_zip(zip_path, output_dir)
except zipfile.BadZipfile as ex:
logger.info(f"Error: {ex}")
error_file = fp_ais / "errors.csv"
with open(error_file, "a") as f:
f.write(f"{zip_path.name}\n")
else:
logger.info(f"Done processing {zip_path}")
def file_in_mb(f: Path) -> float:
return os.path.getsize(f) / (1024 * 1024)
def file_in_gb(f: Path) -> float:
return os.path.getsize(f) / (1024 * 1024 * 1024)
def get_zip_files(year: int | str) -> list[Path]:
return sorted(fp_sdir.glob(f"{year}/*.zip"), key=lambda f: f.name)
def current_time():
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def proc_zip_files(zip_files: list[Path], out_dir: Path):
out_dir.mkdir(parents=True, exist_ok=True)
for i, file in enumerate(zip_files):
logger.info(f": Processing zip-file: {file.name}")
extract_and_sink(file, out_dir)
logger.info(f"Done processing {file.name} ({i + 1}/{len(zip_files)})")
def proc_zip_files_year(year: int):
out_dir = fp_ais.joinpath("data", f"{year}")
out_dir.mkdir(parents=True, exist_ok=True)
zip_files = get_zip_files(f"{year}")
print(f"Processing files for {year=}")
proc_zip_files(zip_files, out_dir)
logger.info(f"Done processing all files for {year}")
@click.group()
def cli():
pass
@cli.command()
def list_zips():
for year in years:
print(f"Year: {year}")
zip_files = get_zip_files(year)
for f in zip_files:
print(f" {f.name} - {file_in_mb(f):.2f} MB")
@cli.command()
def inspect_sizes():
tots = []
for year in years:
print(f"Year: {year}")
fp_pq = fp_exp.joinpath(year)
files = sorted(fp_pq.glob("*.parquet"), key=lambda f: f.name)
sizes = []
for f in files:
size = file_in_mb(f)
print(f" {f.name} - {size:.2f} MB")
sizes.append(size)
zip_size = sum(file_in_mb(f) for f in get_zip_files(year))
tots.append((year, sum(sizes) / 1024, zip_size / 1024))
print(" Total sizes ".center(40, "="))
print(tabulate.tabulate(tots, headers=["Year", "Size (GB)", "Zip size (GB)"]))
@cli.command()
@click.argument("year", type=int)
def proc_year(year: int):
"""
Process all zip files for a given year.
"""
proc_zip_files_year(year)
@cli.command()
def proc_errd():
"""Proc zip files that err'd before"""
fp_extra = Path(f"/home/{KU_ID}/ukraine-sdir/extra-zips")
# 2024
proc_zip_files(
[
# fp_extra / "aisdk-2024-01.zip",
fp_extra
/ "2024-05-26.zip",
],
out_dir=fp_ais.joinpath("data", "2024"),
)
# 2023
proc_zip_files(
[
fp_extra / "2023-02.zip",
],
out_dir=fp_ais.joinpath("data", "2023"),
)
# 2021
proc_zip_files(
[
fp_sdir.joinpath("2021") / "aisdk-2021-08.zip",
fp_extra / "2021-09.zip",
fp_extra / "2021-10.zip",
fp_extra / "2021-12.zip",
fp_extra / "aisdk-2021-11.zip",
],
out_dir=fp_ais.joinpath("data", "2021"),
)
@cli.command()
def proc_errdcsvs():
"""Proc faulty csv files"""
fp_csvs = Path(f"/home/{KU_ID}/ukraine-sdir/extra-csvs")
# 2024 files
files = [Path("aisdk-2024-02-03.csv")]
for file in files:
f_csv = fp_csvs / file
f_pq = fp_ais.joinpath("data", "2024") / file.with_suffix(".parquet")
logger.info(f"Processing errd csv `{f_csv}`")
sink_csv(csv_path=f_csv, pq_path=f_pq)
def resample_df(df: pl.DataFrame, every: str) -> pl.DataFrame:
return df.group_by_dynamic(
"# Timestamp",
every=every,
closed="left",
group_by="MMSI",
include_boundaries=False,
).agg(pl.all().backward_fill().first())
def rs_df(file: Path, every: str) -> pl.DataFrame:
"""Resample dataframe by backwards-filling nans.
The first observation is kept for each group (id, time).
"""
return (
pl.scan_parquet(file)
.pipe(proc_ais)
.pipe(resample_df, every=every)
# collect
.collect()
)
def resample_files(files: list[Path], every: str, fp_out: Path):
for f in files:
print(f"Processing {f}")
stem = f.stem
file_out = fp_out / f"{stem}-{every}.parquet"
try: # Just write it directly to avoid memory issues
if not file_out.exists():
rs_df(f, every=every).write_parquet(file_out)
else:
print(f"File {file_out} already exists")
except Exception as ex:
logger.info(f"Failed processing {f} {every=}")
logger.exception(ex)
else:
logger.info(f"Done processing {f} {every=} to {file_out}")
@cli.command()
@click.argument("year", type=str)
def resample_year(year: str):
"""
Resample data to 15m intervals for a given year.
Later on we resample to 30m and 1h.
"""
every = "15m" # Can resample to 30m and 1h from 15 m then
year = f"{year}"
fp_pq = fp_ais.joinpath("data", year)
fp_out = fp_ais.joinpath("data", "proc", year)
fp_out.mkdir(parents=True, exist_ok=True)
files = sorted(fp_pq.glob("aisdk*.parquet"), key=lambda f: f.name)
logger.info(f"Processing {len(files)} files from {fp_pq}; {year=}")
resample_files(files, every="15m", fp_out=fp_out)
logger.info(f"Done processing all files for {year=} for {every=}")
@cli.command()
def rs_extra():
"""Resample missing ones"""
# One missing for 2024
fp_data = fp_ais.joinpath("data", "2024")
fp_out = fp_ais.joinpath("data", "proc", "2024")
resample_files([fp_data / "aisdk-2024-05-26.parquet"], every="15m", fp_out=fp_out)
@cli.command()
@click.argument("year", type=str)
def resample_final(year: str):
"""
Resample all data for a given year into 15m, 30m and 1h intervals.
These are the datasets provided for the data sprint.
"""
fp_dsprint = fp_ais.joinpath("data", "proc", "data-sprint")
fp_dsprint.mkdir(parents=True, exist_ok=True)
fp = fp_ais.joinpath("data", "proc", year)
logger.info(f"Loading all files for {year}")
df = pl.read_parquet([f for f in fp.glob("*.parquet")])
logger.info(f"Loaded all files; {df.shape[0]} rows in total.")
df30m = df.sort("# Timestamp", "MMSI").pipe(resample_df, every="30m")
df1h = df30m.pipe(resample_df, every="1h")
df.write_parquet(fp_dsprint.joinpath(f"aisdk-{year}-15m.parquet"))
df30m.write_parquet(fp_dsprint.joinpath(f"aisdk-{year}-30m.parquet"))
df1h.write_parquet(fp_dsprint.joinpath(f"aisdk-{year}-1h.parquet"))
print(df.shape, df30m.shape, df1h.shape, sep="\n")
logger.info(f"Done resampling {year}")
@cli.command()
def inspect_final():
fp_pq = fp_ais.joinpath("data", "proc", "data-sprint")
files = sorted(fp_pq.glob("*.parquet"), key=lambda f: f.name)
sizes = []
for f in files:
size = file_in_mb(f)
shape = pl.read_parquet(f).shape
print(f" {f.name} - {size:.2f} MB; {shape}")
sizes.append(size)
print(f" Total: {sum(sizes):.2f} MB")
if __name__ == "__main__":
cli()