defdbl a - z
dr = 180 / 3.1415926536
palette 6 , 6
F$="SITES.DAT"
fx$=dir$(f$)
if fx$<>f$ then
	sound 400,1:sound 500,1
	print:print:print
	color 28
	print "Site database missing";
	color 2
	print "  SITES.DAT must be in the default directory"
	end
end if
cmnd$ = command$
if cmnd$ = "" then cls else print : print
color 14
print "QUICK SITE FINDER";
color 6
print tab(35); "G4JNT  Dec 1994"
print

do
	color 7
	if cmnd$ > "" then
		wantedname$ = cmnd$
		print wantedname$
	else
		input; "Sitename   ",wantedname$
	end if
	wantedname$=ucase$(wantedname$)
	if wantedname$ = "" then exit loop
	found% = 0
	open "i" , 1 , "sites.dat"
	do
		line input #1 , readline$
		posn1%=instr(readline$,"~")
		currentname$ = left$(ucase$(readline$) , posn1% - 1)
		wh% = instr(currentname$ , wantedname$)
		if wh% > 0 then gosub ThisSite
	loop until eof(1)
	close 1
	if dir$("auxsites.dat") > "" then
	open "i" , 1 , "auxsites.dat"
		do
			line input #1 , readline$
			if readline$ = "" then iterate loop
			posn1%=instr(readline$,"~")
			currentname$ = left$(ucase$(readline$) , posn1% - 1)
			wh% = instr(currentname$ , wantedname$)
			if wh% > 0 then gosub ThisSite
		loop until eof(1)
	end if
	if not found% then
		sound 300 , 1
		color 5
		print tab(30);"Not stored"
	else
		print
	end if
	close 1
loop until cmnd$ > ""
end

ThisSite:
   
	posn2%=instr(posn1%+7,readline$,"~")
	popsite%=0
	if posn2%>0 then
		flags$=ucase$(mid$(readline$,posn2%+1,3))
		if instr(flags$,"P")>0 then popsite%=-1 else popsite%=0
	end if
	storedsite$=left$(readline$,posn1%-1)
	location$=ucase$(mid$(readline$,posn1%+1,8))
	if instr(location$,"~")>0 then location$=left$(location$,6)  ' 6 Digit loc stored
	q1$="AA00AA  "
	q2$="RR99XX99"
	q3$="HA000000"
	q4$="YZ999999"
	notloc%=0
	notngr%=0
	for chk%=1 to len(location$)
		a$=mid$(location$,chk%,1)
		if a$ < mid$(q1$,chk%,1) or a$ > mid$(q2$,chk%,1) then notloc%=-1
		if a$ < mid$(q3$,chk%,1) or a$ > mid$(q4$,chk%,1) then notngr%=-1
	next chk%
	if not notloc% then locator$=location$ : gosub DecodeLocator
	if not notngr% then ngr$=location$ : gosub DecodeNgr
	color 11
	if eof(1) or (notloc% and notngr%) then
		sound 300,.5
		storedsite$ = "No data"
		locator$ = ""
		ngr$=""
		color 7
	end if
	found% = -1
	locate csrlin , 1
	print using "\                                      \  \      \  \      \"; storedsite$ , Locator$ , ngr$
return

DecodeLocator:
	'        Enter with Locator$, exit with Lat/Long + NGR$
	IF LEN(locator$)<8 or right$(locator$,2)="  " THEN LC8%=0 else LC8%=-1
	LAT=10*(ASC(MID$(locator$,2,1))-65)+(ASC(MID$(locator$,4,1))-48)
	LAT=LAT+(ASC(MID$(locator$,6,1))-65)/24-90+1.25/60
	IF LC8% THEN LAT=LAT+(ASC(MID$(locator$,8,1))-48)*.25/60-1.125/60
	LONG=20*(ASC(MID$(locator$,1,1))-65)+2*(ASC(MID$(locator$,3,1))-48)
	LONG=LONG+(ASC(MID$(locator$,5,1))-65)/12-180+2.5/60
	IF LC8% THEN LONG=LONG+(ASC(MID$(locator$,7,1))-48)*.5/60-2.25/60
	LATR=LAT/DR
	LONGR=LONG/DR
	if long > -6 and long< 2 and lat >48 and lat < 60 then
		Y=SQR(1-.00667054#*SIN(LATR)*SIN(LATR))
		N=-100+111.0793*(LAT-49)
		N=N-31.947*SIN((LAT-49)/DR)*COS(LATR+49/DR)+.971*(2+LONG)^2*SIN(LATR)*COS(LATR)/Y
		E=400+111.2651*(2+LONG)*COS(LATR)/Y
		E=E-.0056*(2+LONG)^3*(COS(LATR))^3*((TAN(LATR))^2-1)
		C1%=83-5*((N-.5)\500)+((E-.5)\500)
		IF C1%=73 THEN C1%=72
		EH%=((E MOD 500)-.5)\100
		NH%=((N MOD 500)-.5)\100
		C2%=(4-NH%)*5+EH%+65
		IF C2%>72 THEN C2%=C2%+1
		C35%=(E*10) MOD 1000
		C35$=RIGHT$(STR$(C35%),1-(C35%>9)-(C35%>99))
		while LEN(C35$)<3
			C35$="0"+C35$
		wend
		C68%=(N*10) MOD 1000
		C68$=RIGHT$(STR$(C68%),1-(C68%>9)-(C68%>99))
		while LEN(C68$)<3
			C68$="0"+C68$
		wend
		if lc8% then
			NGR$=CHR$(C1%)+CHR$(C2%)+C35$+C68$
		else
			ngr$="["+chr$(C1%)+chr$(C2%)+left$(C35$,2)+left$(C68$,2)+"]"
		end if
		else
			ngr$="   --   "
		end if
	return

DecodeNgr:
	N=VAL(MID$(NGR$,6,3))/10
	E=VAL(MID$(NGR$,3,3))/10
	T1%=ASC(MID$(NGR$,1,1))-65:IF T1%>8 THEN T1%=T1%-1
	T2%=ASC(MID$(NGR$,2,1))-65:IF T2%>8 THEN T2%=T2%-1
	E=500* (T1% MOD 5) + 100*(T2% MOD 5)-1000+E
	N=1900-500*(T1% \5)-100*(T2%\5)+N

	T1=(N+5548.79)/6371.28
	T2=2*ATN(EXP((E-400)/6389.7))
	LONGR=ATN(-COS(T2)/(COS(T1)*SIN(T2)))-2/DR
	LA=SIN(T1)*SIN(T2)
	LATR=ATN(LA/SQR(1-LA*LA))
	LAT=LATR*DR
	LONG=LONGR*DR

	LOCATOR$="":V=LAT/180+.5:H=LONG/360+.5:locator$=""
	T=65:F=18:GOSUB NextChr
	T=48:F=10:GOSUB NextChr
	T=65:F=24:GOSUB NextChr
	T=48:F=10:GOSUB NextChr
RETURN

NextChr:
	V=F*(V-INT(V)):H=F*(H-INT(H))
	LOCATOR$=LOCATOR$+CHR$(INT(T+H))+CHR$(INT(T+V))
RETURN
