2020-08-08 23:59:13 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Import preplot data.
|
|
|
|
|
|
|
|
|
|
For each preplot file defined in each survey, check for new
|
|
|
|
|
or modified preplots and (re-)import them into the database.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from glob import glob
|
2021-05-07 13:50:32 +02:00
|
|
|
import os
|
2020-08-29 13:14:20 +02:00
|
|
|
import sys
|
2021-05-07 13:50:32 +02:00
|
|
|
import time
|
2020-08-08 23:59:13 +02:00
|
|
|
import configuration
|
|
|
|
|
import preplots
|
|
|
|
|
from datastore import Datastore
|
|
|
|
|
|
2024-05-04 17:32:30 +02:00
|
|
|
def preplots_sorter (preplot):
|
|
|
|
|
rank = {
|
|
|
|
|
"x-sl+csv": 10
|
|
|
|
|
}
|
|
|
|
|
return rank.get(preplot.get("type"), 0)
|
|
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
print("Connecting to database")
|
|
|
|
|
db = Datastore()
|
2023-08-30 14:19:56 +02:00
|
|
|
surveys = db.surveys()
|
|
|
|
|
|
|
|
|
|
print("Reading configuration")
|
|
|
|
|
file_min_age = configuration.read().get('imports', {}).get('file_min_age', 10)
|
2020-08-08 23:59:13 +02:00
|
|
|
|
|
|
|
|
print("Reading surveys")
|
|
|
|
|
for survey in surveys:
|
|
|
|
|
print(f'Survey: {survey["id"]} ({survey["schema"]})')
|
|
|
|
|
db.set_survey(survey["schema"])
|
2024-05-04 17:32:30 +02:00
|
|
|
|
|
|
|
|
# We sort the preplots so that ancillary line info always comes last,
|
|
|
|
|
# after the actual line + point data has been imported
|
|
|
|
|
for file in sorted(survey["preplots"], key=preplots_sorter):
|
2023-08-30 14:56:09 +02:00
|
|
|
realpath = configuration.translate_path(file["path"])
|
|
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
print(f"Preplot: {file['path']}")
|
|
|
|
|
if not db.file_in_db(file["path"]):
|
2022-04-29 14:48:21 +02:00
|
|
|
|
2023-08-30 14:56:09 +02:00
|
|
|
age = time.time() - os.path.getmtime(realpath)
|
2021-05-07 13:50:32 +02:00
|
|
|
if age < file_min_age:
|
|
|
|
|
print("Skipping file because too new", file["path"])
|
|
|
|
|
continue
|
2022-04-29 14:48:21 +02:00
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
print("Importing")
|
2020-08-29 13:14:20 +02:00
|
|
|
try:
|
2023-08-30 14:56:09 +02:00
|
|
|
preplot = preplots.from_file(file, realpath)
|
2020-08-29 13:14:20 +02:00
|
|
|
except FileNotFoundError:
|
|
|
|
|
print(f"File does not exist: {file['path']}", file=sys.stderr)
|
|
|
|
|
continue
|
|
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
if type(preplot) is list:
|
|
|
|
|
print("Saving to DB")
|
2024-05-04 17:32:30 +02:00
|
|
|
if file.get("type") == "x-sl+csv":
|
|
|
|
|
db.save_preplot_line_info(preplot, file["path"], file)
|
|
|
|
|
else:
|
|
|
|
|
db.save_preplots(preplot, file["path"], file["class"], survey["epsg"], file)
|
2020-08-08 23:59:13 +02:00
|
|
|
elif type(preplot) is str:
|
|
|
|
|
print(preplot)
|
|
|
|
|
else:
|
|
|
|
|
print("Already in DB")
|
|
|
|
|
|
|
|
|
|
print("Done")
|