aboutsummaryrefslogtreecommitdiff
path: root/noao/twodspec/apextract/apnearest.x
blob: f3e027c56096a72ebfac64d97c59751575050ddb (plain) (blame)
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
include	<mach.h>
include	"apertures.h"

# AP_NEAREST -- Find the index of the aperture nearest cursor position x.

define	DELTA	0.01		# Tolerance for equidistant apertures

procedure ap_nearest (index, line, aps, naps, x)

int	index			# Index of aperture nearest x
int	line			# Dispersion line
pointer	aps[ARB]		# Aperture pointers
int	naps			# Number of apertures
real	x			# Point nearest aperture

int	i, j, apaxis
char	ch
real	d, delta
pointer	ap

int	fscan(), nscan()
real	ap_cveval()

begin
	if (naps == 0)
	    return

	index = 0
	delta = MAX_REAL

	for (i = 1; i <= naps; i = i + 1) {
	    ap = aps[i]
	    apaxis = AP_AXIS(ap)
	    d = abs (AP_CEN(ap, apaxis)+ap_cveval(AP_CV(ap),real(line))-x)
	    if (d < delta - DELTA) {
		j = 1
		index = i
		delta = d
	    } else if (d < delta + DELTA)
		j = j + 1
	}

	# If there is more than one aperture equally near ask the user.
	if (j > 1) {
	    call printf ("Apertures")
	    for (i = 1; i <= naps; i = i + 1) {
	        ap = aps[i]
	        apaxis = AP_AXIS(ap)
	        d = abs (AP_CEN(ap, apaxis)+ap_cveval(AP_CV(ap),real(line))-x)
	        if (d < delta + DELTA) {
		    call printf (" %d")
			call pargi (AP_ID (ap))
		}
	    }
	    call printf (" are equally near the cursor.\n")
10	    call printf ("Choose an aperture (%d): ")
		call pargi (AP_ID (aps[index]))
	    call flush (STDOUT)
	    if (fscan (STDIN) != EOF) {
		call scanc (ch)
		if (ch == '\n')
		    return

		call reset_scan()
		call gargi (j)
		if (nscan() == 0)
		    goto 10
		for (i=1; (i<=naps)&&(AP_ID(aps[i])!=j); i=i+1)
		    ;
		if (i > naps)
		    goto 10
		index = i
	    }
	}
end