-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitycalc.ijs
78 lines (65 loc) · 1.48 KB
/
citycalc.ijs
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
NB. cities demo - calculations
NB. following supports DLL demo:
3 : 0 ''
NB. regular system:
if. 3 = 4!:0 <'jpath' do.
FILE=: jpath '~addons/demos/wd/cities.dat'
NB. DLL:
else.
FILE=: (_3 }. BINPATH),'~addons/demos/wd/cities.dat'
end.
)
NB. average earth radius (kilometers, miles):
RADIUS=: 6371.007 3958.760
sin=: 1&o.
cos=: 2&o.
arccos=: _2&o.
NB. =========================================================
cityread=: 3 : 0
dat=. CR -.~ 1!:1 <FILE
dat=. '_' (I. dat='-') } dat
dat=. ] ;._2 dat
hdr=. 3 {."1 dat
msk=. -. hdr e. 'NB.',:' '
dat=. msk # dat
i=. dat i."1 ':'
BNAMES=: i<@{."0 1 dat
ONAMES=: (>BNAMES),.' '
CNAMES=: ;,&LF &.> BNAMES
x=. ". ,' ',.(>:i)}."0 1 dat
pos=. ((#dat),4)$x
LAT=: 1r10800p1 * 60 #. 2{."1 pos
LON=: 1r10800p1 * 60 #. 2}."1 pos
SINLAT=: sin LAT
COSLAT=: cos LAT
empty''
)
NB. =========================================================
citydistance=: 3 : 0
arccos (SINLAT*y{SINLAT)+(COSLAT*y{COSLAT)*cos(y{LON)-LON
)
NB. cityfmt
NB. argument has 3 elements:
NB. ifmiles
NB. ifdistance
NB. city index
NB. result is text to display
cityfmt=: 3 : 0
'm d n'=. y
rad=. m{RADIUS
msk=. n ~: i.#BNAMES
dist=. <.0.5+rad*msk#citydistance n
txt=. (msk#ONAMES),.(":,.dist),"1 ' '
if. d do. txt=. txt /: dist end.
,(72 list txt),.LF
)
NB. cityfmts
NB. as cityfmt, but accepting string argument
NB. e.g. cityfmts 'True False Antwerp'
NB. (required for VB demo)
cityfmts=: 3 : 0
m=. (<'True') = 2{.y=. cutopen y
n=. }:;,&' '&.>2}.y
x=. BNAMES i. <n
,CRLF ,"1~ cityfmt m,x
)