2023-01-29 17:03:50 +00:00
|
|
|
import copy
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
import troggle.settings as settings
|
|
|
|
from troggle.core.models.caves import Cave, Entrance
|
|
|
|
from troggle.core.models.logbooks import QM
|
2023-09-06 20:58:14 +01:00
|
|
|
from troggle.core.models.survex import SurvexBlock, SurvexFile, SurvexPersonRole, SurvexStation
|
2023-01-30 23:04:11 +00:00
|
|
|
from troggle.core.models.wallets import Wallet
|
2023-01-29 17:03:50 +00:00
|
|
|
from troggle.core.models.troggle import DataIssue, Expedition
|
|
|
|
from troggle.core.utils import chaosmonkey, get_process_memory
|
|
|
|
from troggle.parsers.logbooks import GetCaveLookup
|
|
|
|
from troggle.parsers.people import GetPersonExpeditionNameLookup, known_foreigner
|
|
|
|
from troggle.parsers.survex import stash_data_issue, store_data_issues, ROOTBLOCK
|
|
|
|
|
|
|
|
"""Uses the imported data to find the locations of the survey stations labelled as
|
|
|
|
entrances
|
|
|
|
"""
|
|
|
|
|
|
|
|
todo = """
|
|
|
|
- Pending a complete revision of how we handle GPS coordinates of entrances.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class MapLocations(object):
|
2023-10-07 00:26:52 +01:00
|
|
|
"""Class used only for identifying the entrance locations
|
2023-10-07 17:41:25 +01:00
|
|
|
Formerly used to put locations on a prospecting map
|
|
|
|
|
|
|
|
We don't need these map locations any more ?!
|
|
|
|
They would only be used in addition to entrances going onto a map display"""
|
2023-01-29 17:03:50 +00:00
|
|
|
|
2023-10-10 23:03:28 +01:00
|
|
|
fp = [
|
2023-01-29 17:03:50 +00:00
|
|
|
("laser.0_7", "BNase", "Reference", "Bräuning Nase laser point"),
|
|
|
|
("226-96", "BZkn", "Reference", "Bräuning Zinken trig point"),
|
|
|
|
("vd1", "VD1", "Reference", "VD1 survey point"),
|
|
|
|
("laser.kt114_96", "HSK", "Reference", "Hinterer Schwarzmooskogel trig point"),
|
|
|
|
("2000", "Nipple", "Reference", "Nipple (Weiße Warze)"),
|
|
|
|
("3000", "VSK", "Reference", "Vorderer Schwarzmooskogel summit"),
|
|
|
|
("topcamp", "OTC", "Reference", "Old Top Camp"),
|
|
|
|
("laser.0", "LSR0", "Reference", "Laser Point 0"),
|
|
|
|
("laser.0_1", "LSR1", "Reference", "Laser Point 0/1"),
|
|
|
|
("laser.0_3", "LSR3", "Reference", "Laser Point 0/3"),
|
|
|
|
("laser.0_5", "LSR5", "Reference", "Laser Point 0/5"),
|
|
|
|
("225-96", "BAlm", "Reference", "Bräuning Alm trig point"),
|
2023-10-07 00:26:52 +01:00
|
|
|
] # 12 fixed points
|
2023-10-10 23:03:28 +01:00
|
|
|
|
|
|
|
p = []
|
2023-01-29 17:03:50 +00:00
|
|
|
|
|
|
|
def points(self):
|
|
|
|
for ent in Entrance.objects.all():
|
2023-10-11 22:58:20 +01:00
|
|
|
for st, ent_type in {ent.other_station: "other", ent.tag_station: "tag"}.items():
|
2023-10-07 00:26:52 +01:00
|
|
|
if st != "":
|
2023-10-10 23:03:28 +01:00
|
|
|
self.p.append((st, str(ent), ent.needs_surface_work(), ent))
|
2023-10-07 00:26:52 +01:00
|
|
|
store_data_issues()
|
2023-10-17 22:19:17 +01:00
|
|
|
message = f" - {len(self.p)} Survey stations found on Entrance objects - not yet validated against survex .pos file."
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
|
|
|
return self.p
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-10-17 22:19:17 +01:00
|
|
|
return f"{len(self.p)} ent locations"
|
2023-01-29 17:03:50 +00:00
|
|
|
|
|
|
|
|
2023-10-07 00:26:52 +01:00
|
|
|
def validate_entrance_stations(ent=None):
|
2023-10-17 22:19:17 +01:00
|
|
|
"""Now that we have the located positions, we can check if the Entrances had correct stations
|
2023-10-07 00:26:52 +01:00
|
|
|
"""
|
|
|
|
bads = 0
|
|
|
|
good = 0
|
2023-10-10 23:03:28 +01:00
|
|
|
url="/caves" # fallback
|
2023-10-07 00:26:52 +01:00
|
|
|
|
2023-10-17 22:19:17 +01:00
|
|
|
def station_lower_case(station):
|
2023-10-10 23:03:28 +01:00
|
|
|
nonlocal url
|
2023-10-07 00:26:52 +01:00
|
|
|
so = SurvexStation.objects.filter(name=station.lower())
|
|
|
|
if so.count() == 1:
|
|
|
|
message = f"X - Entrance {ent} station '{station}' should be '{station.lower()}'"
|
|
|
|
stash_data_issue(parser="positions", message=message, url=url)
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
def validate_ent(ent):
|
2023-10-12 14:13:28 +01:00
|
|
|
"""For each of the two station strings in an Entrance object,
|
2023-10-07 00:26:52 +01:00
|
|
|
validate each string as referring to a valid SurvexStation object.
|
|
|
|
But our list of created SurvexStation objects is created by taking a list of strings and using them
|
|
|
|
to select from lines in a .pos file - so this is unnecessarily indirect.
|
|
|
|
"""
|
|
|
|
nonlocal bads
|
|
|
|
nonlocal good
|
|
|
|
# {% url "editentrance" ent.entrance.url_parent cave.slug ent.entrance.slug %}
|
|
|
|
# e.g. url = f"/1623/101/1623-101:{ent}_entrance_edit"
|
|
|
|
cavelist = ent.cavelist()
|
|
|
|
if len(cavelist) == 1:
|
|
|
|
cave = cavelist[0]
|
|
|
|
url = f"/{cave.url}"
|
|
|
|
elif len(cavelist) > 1:
|
2023-10-12 14:13:28 +01:00
|
|
|
cave = cavelist[-1] # set to last in list
|
2023-10-07 00:26:52 +01:00
|
|
|
url = f"/{cave.url}"
|
|
|
|
else:
|
2023-10-12 14:13:28 +01:00
|
|
|
print(f"BUGGER bad cave '{cavelist}' on Entrance object {ent} ")
|
2023-10-07 00:26:52 +01:00
|
|
|
url="/caves"
|
2023-10-11 22:58:20 +01:00
|
|
|
for st, ent_type in {ent.other_station: "other", ent.tag_station: "tag"}.items():
|
2023-10-07 00:26:52 +01:00
|
|
|
if st == "":
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
so = SurvexStation.objects.filter(name=st)
|
|
|
|
except:
|
|
|
|
message = f" ! - Entrance {ent} has invalid '{ent_type}' station '{st}'. EXCEPTION."
|
|
|
|
stash_data_issue(parser="positions", message=message, url=url)
|
|
|
|
print(message)
|
|
|
|
bads +=1
|
|
|
|
continue
|
2023-10-10 23:03:28 +01:00
|
|
|
|
|
|
|
if so.count() == 1:
|
|
|
|
good +=1
|
|
|
|
# print(f"OK - Entrance {ent} '{ent_type}' station '{st}'")
|
|
|
|
continue
|
|
|
|
if so.count() != 0:
|
|
|
|
message =f"{so.count()} found for Entrance {ent} '{ent_type}' station '{st}' {so}"
|
|
|
|
else:
|
|
|
|
message = f" ! - Entrance {ent} has invalid '{ent_type}' station '{st}'"
|
|
|
|
if st == ent.best_station():
|
|
|
|
message = message + " - AND THIS IS THE 'BEST' ONE"
|
|
|
|
else:
|
|
|
|
message = message + " - not the 'best'"
|
|
|
|
stash_data_issue(parser="positions", message=message, url=url)
|
|
|
|
print(message)
|
|
|
|
bads +=1
|
2023-10-17 22:19:17 +01:00
|
|
|
station_lower_case(st)
|
2023-10-10 23:03:28 +01:00
|
|
|
continue
|
|
|
|
|
2023-10-07 00:26:52 +01:00
|
|
|
|
|
|
|
if ent:
|
|
|
|
return validate_ent(ent)
|
|
|
|
|
|
|
|
|
|
|
|
for ent in Entrance.objects.all():
|
|
|
|
validate_ent(ent)
|
|
|
|
|
2023-10-15 16:43:15 +01:00
|
|
|
print(f" - {good} valid SurvexStations of all types found on Entrances.")
|
|
|
|
print(f" - {bads} bad SurvexStations of all types found on Entrances.")
|
2023-10-07 00:26:52 +01:00
|
|
|
return True # not necessarily.. but unused return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
poslineregex = re.compile(r"^\(\s*([+-]?\d*\.\d*),\s*([+-]?\d*\.\d*),\s*([+-]?\d*\.\d*)\s*\)\s*([^\s]+)$")
|
2023-01-29 17:03:50 +00:00
|
|
|
|
|
|
|
def LoadPositions():
|
|
|
|
"""First load the survex stations for entrances and fixed points (about 600) into the database.
|
2023-09-30 18:35:40 +01:00
|
|
|
Run 'cavern' to produce a complete .3d file, then run 'survexport -pos' to produce a table of
|
|
|
|
all survey point positions in UTM cooridnates. Then lookup each of the 600 positions by name to
|
|
|
|
see if we have it in the database and if we do, then save the UTM x/y/z coordinates.
|
|
|
|
This gives us coordinates of the entrances.
|
2023-01-29 17:03:50 +00:00
|
|
|
If we don't have it in the database, print an error message and discard it.
|
|
|
|
"""
|
|
|
|
svx_t = 0
|
|
|
|
d3d_t = 0
|
2023-07-25 22:14:46 +01:00
|
|
|
|
|
|
|
DataIssue.objects.filter(parser="positions").delete()
|
2023-10-07 00:26:52 +01:00
|
|
|
SurvexStation.objects.all().delete()
|
2023-07-25 22:14:46 +01:00
|
|
|
|
2023-01-29 17:03:50 +00:00
|
|
|
|
|
|
|
def runcavern3d():
|
|
|
|
outputdir = Path(str(f"{topdata}.svx")).parent
|
|
|
|
|
|
|
|
# print(" - Regenerating stale cavern .log and .3d for '{}'\n days old: {:.1f} {:.1f} {:.1f}".
|
|
|
|
# format(topdata, (svx_t - d3d_t)/(24*3600), (cav_t - d3d_t)/(24*3600), (now - d3d_t)/(24*3600)))
|
|
|
|
|
|
|
|
file3d = Path(f"{topdata}.3d")
|
|
|
|
try:
|
|
|
|
sp = subprocess.run(
|
|
|
|
[settings.CAVERN, "--log", f"--output={outputdir}", f"{topdata}.svx"],
|
|
|
|
capture_output=True,
|
|
|
|
check=False,
|
|
|
|
text=True,
|
|
|
|
) # check=False means exception not raised
|
|
|
|
if sp.returncode != 0:
|
|
|
|
message = f" ! Error: cavern: creating {file3d} in runcavern3()"
|
2023-07-25 22:14:46 +01:00
|
|
|
stash_data_issue(parser="positions", message=message)
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
|
|
|
|
|
|
|
# find the errors in the 1623.log file
|
|
|
|
sp = subprocess.run(
|
|
|
|
["grep", "error:", f"{topdata}.log"], capture_output=True, check=False, text=True
|
|
|
|
) # check=False means exception not raised
|
|
|
|
message = f" ! Error: cavern: {sp.stdout} creating {file3d} "
|
2023-07-25 22:14:46 +01:00
|
|
|
stash_data_issue(parser="positions", message=message)
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
|
|
|
|
|
|
|
except:
|
|
|
|
message = f" ! CalledProcessError 'cavern' in runcavern3() at {topdata}."
|
2023-07-25 22:14:46 +01:00
|
|
|
stash_data_issue(parser="positions", message=message)
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
|
|
|
|
|
|
|
if file3d.is_file():
|
|
|
|
message = f" ! CalledProcessError. File permissions {file3d.stat().st_mode} on {str(file3d)}"
|
2023-07-25 22:14:46 +01:00
|
|
|
stash_data_issue(parser="positions", message=message)
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
|
|
|
|
|
|
|
if file3d.is_file(): # might be an old one though
|
|
|
|
try:
|
|
|
|
# print(" - Regenerating {} {}.3d in {}".format(settings.SURVEXPORT, topdata, settings.SURVEX_DATA))
|
|
|
|
sp = subprocess.run(
|
|
|
|
[settings.SURVEXPORT, "--pos", f"{file3d}"],
|
|
|
|
cwd=settings.SURVEX_DATA,
|
|
|
|
capture_output=True,
|
|
|
|
check=False,
|
|
|
|
text=True,
|
|
|
|
)
|
|
|
|
if sp.returncode != 0:
|
|
|
|
print(
|
|
|
|
f" ! Error: survexport creating {topdata}.pos in runcavern3().\n\n"
|
|
|
|
+ str(sp.stdout)
|
|
|
|
+ "\n\nreturn code: "
|
|
|
|
+ str(sp.returncode)
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
message = f" ! CalledProcessError 'survexport' in runcavern3() at {file3d}."
|
2023-07-25 22:14:46 +01:00
|
|
|
stash_data_issue(parser="positions", message=message)
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
|
|
|
else:
|
|
|
|
message = f" ! Failed to find {file3d} so aborting generation of new .pos, using old one if present"
|
2023-07-25 22:14:46 +01:00
|
|
|
stash_data_issue(parser="positions", message=message)
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
|
|
|
|
|
|
|
topdata = os.fspath(Path(settings.SURVEX_DATA) / settings.SURVEX_TOPNAME)
|
|
|
|
print(f" - Generating a list of Pos from {topdata}.svx and then loading...")
|
|
|
|
|
|
|
|
found = 0
|
|
|
|
print("\n") # extra line because cavern overwrites the text buffer somehow
|
|
|
|
# cavern defaults to using same cwd as supplied input file
|
|
|
|
|
|
|
|
completed_process = subprocess.run(["which", f"{settings.CAVERN}"], capture_output=True, check=True, text=True)
|
|
|
|
cav_t = os.path.getmtime(completed_process.stdout.strip())
|
|
|
|
|
|
|
|
svxpath = topdata + ".svx"
|
|
|
|
d3dpath = topdata + ".3d"
|
|
|
|
pospath = topdata + ".pos"
|
|
|
|
|
|
|
|
svx_t = os.path.getmtime(svxpath)
|
|
|
|
|
|
|
|
if os.path.isfile(d3dpath):
|
|
|
|
# always fails to find log file if a double directory, e.g. caves-1623/B4/B4/B4.svx Why ?
|
|
|
|
d3d_t = os.path.getmtime(d3dpath)
|
|
|
|
|
|
|
|
now = time.time()
|
|
|
|
if not os.path.isfile(pospath):
|
|
|
|
runcavern3d()
|
|
|
|
if not os.path.isfile(d3dpath):
|
|
|
|
runcavern3d()
|
|
|
|
elif d3d_t - svx_t > 0: # stale, 3d older than svx file
|
|
|
|
runcavern3d()
|
2023-10-07 00:26:52 +01:00
|
|
|
elif now - d3d_t > 24 * 60 * 60: # >1 days old, re-run anyway
|
2023-01-29 17:03:50 +00:00
|
|
|
runcavern3d()
|
|
|
|
elif cav_t - d3d_t > 0: # new version of cavern
|
|
|
|
runcavern3d()
|
|
|
|
|
|
|
|
mappoints = {}
|
2023-10-17 22:19:17 +01:00
|
|
|
found_points = {}
|
|
|
|
pts = MapLocations().points()
|
|
|
|
for pt in pts:
|
2023-10-10 23:03:28 +01:00
|
|
|
svxid, number, point_type, ent = pt
|
|
|
|
#((st, str(ent), ent.needs_surface_work(), ent))
|
2023-10-17 22:19:17 +01:00
|
|
|
if svxid in mappoints:
|
2023-10-17 22:42:50 +01:00
|
|
|
print(f" = seen this svxid {svxid} for {ent} already on entrance {mappoints[svxid]}")
|
2023-10-17 22:19:17 +01:00
|
|
|
else:
|
|
|
|
mappoints[svxid] = ent
|
2023-07-25 22:14:46 +01:00
|
|
|
if svxid =="1":
|
2023-10-17 22:19:17 +01:00
|
|
|
print(f"BOGUS {pt}") # this is now checked for when importing the entrance stations in parsers/caves.py
|
2023-01-29 17:03:50 +00:00
|
|
|
|
|
|
|
if not Path(pospath).is_file():
|
|
|
|
message = f" ! Failed to find {pospath} so aborting generation of entrance locations. "
|
2023-10-07 00:26:52 +01:00
|
|
|
stash_data_issue(parser="positions", message=message, url=f"/entrance_data/{pospath}_edit")
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
|
|
|
return
|
|
|
|
|
|
|
|
posfile = open(pospath)
|
|
|
|
posfile.readline() # Drop header
|
2023-07-25 16:56:13 +01:00
|
|
|
|
|
|
|
sbdict = {}
|
|
|
|
dups = 0
|
|
|
|
lineno = 1 # we dropped the header
|
2023-01-29 17:03:50 +00:00
|
|
|
for line in posfile.readlines():
|
2023-07-25 16:56:13 +01:00
|
|
|
lineno += 1
|
2023-01-29 17:03:50 +00:00
|
|
|
r = poslineregex.match(line)
|
|
|
|
if r:
|
2023-07-25 16:56:13 +01:00
|
|
|
x, y, z, sbid = r.groups() # renamed id to sbid so as to not confuse with Django internal .id
|
|
|
|
if sbid in sbdict:
|
|
|
|
dups += 1
|
|
|
|
message = f" ! DUPLICATE SurvexBlock identifier in .pos file '{sbid}'\n{sbs[sbid]}\n{lineno} / {line}"
|
|
|
|
print(message)
|
2023-07-25 22:14:46 +01:00
|
|
|
stash_data_issue(parser="positions", message=message)
|
2023-07-25 16:56:13 +01:00
|
|
|
else:
|
|
|
|
sbdict[sbid] = lineno
|
|
|
|
|
2023-10-07 00:26:52 +01:00
|
|
|
|
2023-01-29 17:03:50 +00:00
|
|
|
for sid in mappoints:
|
2023-10-07 00:26:52 +01:00
|
|
|
if sbid.endswith(sid) or sbid.endswith(sid.lower()):
|
2023-07-25 16:56:13 +01:00
|
|
|
blockpath = "." + sbid[: -len(sid)].strip(".") # only the most recent one that is mappoints
|
2023-10-17 22:19:17 +01:00
|
|
|
if sid in found_points:
|
|
|
|
found_points[sid] += 1
|
|
|
|
else:
|
|
|
|
found_points[sid] = 1
|
|
|
|
|
2023-01-29 17:03:50 +00:00
|
|
|
try:
|
2023-07-25 16:56:13 +01:00
|
|
|
ss = SurvexStation(name=sbid)
|
2023-01-29 17:03:50 +00:00
|
|
|
ss.x = float(x)
|
|
|
|
ss.y = float(y)
|
|
|
|
ss.z = float(z)
|
2023-10-10 23:03:28 +01:00
|
|
|
ss.entrance = mappoints[sid]
|
2023-01-29 17:03:50 +00:00
|
|
|
ss.save()
|
|
|
|
found += 1
|
|
|
|
except:
|
2023-07-25 16:56:13 +01:00
|
|
|
message = f" ! {lineno} FAIL to create SurvexStation Entrance point {blockpath} {sid}"
|
2023-01-29 17:03:50 +00:00
|
|
|
print(message)
|
2023-07-25 22:14:46 +01:00
|
|
|
stash_data_issue(parser="positions", message=message)
|
|
|
|
store_data_issues()
|
2023-01-29 17:03:50 +00:00
|
|
|
raise
|
2023-10-07 00:26:52 +01:00
|
|
|
validate_entrance_stations() # do not need to use db here really
|
|
|
|
positions_filename = Path(pospath).name
|
2023-10-17 22:19:17 +01:00
|
|
|
print(f" - {found} distinct SurvexStation entrance stations identified in {lineno:,} lines in {positions_filename}.")
|
2023-10-07 00:26:52 +01:00
|
|
|
if dups > 0:
|
|
|
|
print(f" - {dups} Duplicated SurvexStation entrances found")
|
2023-10-17 22:19:17 +01:00
|
|
|
|
|
|
|
# for p in mappoints:
|
|
|
|
# if p not in found_points:
|
|
|
|
# print(f"Valid point {p} NOT found in {positions_filename}")
|
|
|
|
# print(f" - {len(mappoints)} mappoints, {len(found_points)} found_points")
|
|
|
|
# for sid in found_points:
|
|
|
|
# if found_points[sid] > 1:
|
|
|
|
# print(f" - {sid} - {found_points[sid]}")
|
2023-01-29 17:03:50 +00:00
|
|
|
store_data_issues()
|