2020-09-26 22:57:36 +02:00
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
Export data that is entered directly into Dougal
|
|
|
|
|
|
as opposed to being read from external sources.
|
|
|
|
|
|
|
|
|
|
|
|
This data will be read back in when the database
|
|
|
|
|
|
is recreated for an existing survey.
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
from glob import glob
|
|
|
|
|
|
import pathlib
|
|
|
|
|
|
import string
|
|
|
|
|
|
import configuration
|
|
|
|
|
|
import requests
|
|
|
|
|
|
import json
|
|
|
|
|
|
#from datastore import Datastore
|
|
|
|
|
|
|
|
|
|
|
|
def sane_name(filename):
|
|
|
|
|
|
allowed_chars = string.ascii_letters + string.digits + " _-#+&^%$!();:.,"
|
|
|
|
|
|
return ''.join([c for c in filename if c in allowed_chars])
|
|
|
|
|
|
|
|
|
|
|
|
def write_file (filename, payload):
|
|
|
|
|
|
print("Writing to", filename)
|
|
|
|
|
|
tmpname = filename.parent / (filename.name + ".tmp")
|
|
|
|
|
|
filename.parent.mkdir(parents=True, exist_ok=True)
|
2020-09-29 17:45:45 +02:00
|
|
|
|
with open(tmpname, "w", encoding="utf8") as fd:
|
|
|
|
|
|
json.dump(payload, fd, indent=4, ensure_ascii=False)
|
2020-09-26 22:57:36 +02:00
|
|
|
|
os.rename(tmpname, filename)
|
|
|
|
|
|
|
|
|
|
|
|
def seis_data (survey):
|
|
|
|
|
|
try:
|
2020-09-30 15:38:35 +02:00
|
|
|
|
pathPrefix = survey["sse"]["path"]
|
2020-09-26 22:57:36 +02:00
|
|
|
|
except KeyError:
|
|
|
|
|
|
print("Survey does not define an export path for human data")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if not pathlib.Path(pathPrefix).exists():
|
|
|
|
|
|
print(pathPrefix)
|
|
|
|
|
|
raise ValueError("Export path does not exist")
|
2022-04-29 14:48:21 +02:00
|
|
|
|
|
2020-09-26 22:57:36 +02:00
|
|
|
|
print(f"Requesting sequences for {survey['id']}")
|
|
|
|
|
|
url = f"http://localhost:3000/api/project/{survey['id']}/sequence"
|
|
|
|
|
|
r = requests.get(url)
|
|
|
|
|
|
print(r.status_code, url)
|
|
|
|
|
|
for sequence in r.json():
|
|
|
|
|
|
if sequence['status'] not in ["final", "ntbp"]:
|
|
|
|
|
|
continue
|
2022-04-29 14:48:21 +02:00
|
|
|
|
|
2020-09-30 15:38:35 +02:00
|
|
|
|
filename = pathlib.Path(pathPrefix, "sequence{:0>3d}.json".format(sequence['sequence']))
|
2020-09-26 22:57:36 +02:00
|
|
|
|
if filename.exists():
|
|
|
|
|
|
print(f"Skipping export for sequence {sequence['sequence']} – file already exists")
|
|
|
|
|
|
continue
|
2022-04-29 14:48:21 +02:00
|
|
|
|
|
2020-09-26 22:57:36 +02:00
|
|
|
|
print(f"Processing sequence {sequence['sequence']}")
|
2020-10-04 03:51:30 +02:00
|
|
|
|
url = f"http://localhost:3000/api/project/{survey['id']}/event?sequence={sequence['sequence']}&missing=t"
|
2020-09-26 22:57:36 +02:00
|
|
|
|
headers = { "Accept": "application/vnd.seis+json" }
|
|
|
|
|
|
r = requests.get(url, headers=headers)
|
|
|
|
|
|
if r.status_code == requests.codes.ok:
|
|
|
|
|
|
write_file(filename, r.json())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
print("Reading configuration")
|
|
|
|
|
|
surveys = configuration.surveys()
|
|
|
|
|
|
|
|
|
|
|
|
print("Reading surveys")
|
|
|
|
|
|
for survey in surveys:
|
|
|
|
|
|
print(f'Survey: {survey["id"]} ({survey["schema"]})')
|
|
|
|
|
|
seis_data(survey)
|
|
|
|
|
|
|
|
|
|
|
|
print("Done")
|