aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/lib/pttables/ptchoose.x
blob: 18a351958d83945659d36f12c4300e39d0370389 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
include "../ptkeysdef.h"

# PT_CHOOSE -- Determine which fields are to be selected.

int procedure pt_choose (key, fields)

pointer	key		# pointer to key structure
char	fields[ARB]	# fields to be evaluated

int	max_nkeys, nkeys, index, elems, nelems, element, len
pointer	list, sp, kname, uname, fname, aranges, ranges, rangeset
pointer	nop, uop, fop
int	pt_gnfn(), pt_ranges(), strdic(), decode_ranges(), get_next_number()
int	pt_kstati()
pointer	pt_ofnl()
real	asumi()

begin
	# Allocate buffer space
	call smark (sp)
	call salloc (kname, KY_SZPAR, TY_CHAR)
	call salloc (uname, KY_SZPAR, TY_CHAR)
	call salloc (fname, KY_SZPAR, TY_CHAR)
	call salloc (aranges, SZ_FNAME, TY_CHAR)
	call salloc (ranges, SZ_FNAME, TY_CHAR)
	call salloc (rangeset, 3 * KY_MAXNRANGES + 1, TY_INT)

	# Allocate space for the select buffers. Space equal to the number
	# of keys in the database is allocated. Allowance must be made for
	# array subsripts.

	max_nkeys = int (asumi (Memi[KY_NELEMS(key)], KY_NKEYS(key))) + 1

	if (KY_SELECT(key) != NULL)
	    call mfree (KY_SELECT(key), TY_INT)
	call malloc (KY_SELECT(key), max_nkeys, TY_INT)
	if (KY_ELEM_SELECT(key) != NULL)
	    call mfree (KY_ELEM_SELECT(key), TY_INT)
	call malloc (KY_ELEM_SELECT(key), max_nkeys, TY_INT)
	if (KY_LEN_SELECT(key) != NULL)
	    call mfree (KY_LEN_SELECT(key), TY_INT)
	call malloc (KY_LEN_SELECT(key), max_nkeys, TY_INT)

	if (KY_NAME_SELECT(key) != NULL)
	    call mfree (KY_NAME_SELECT(key), TY_CHAR)
	call malloc (KY_NAME_SELECT(key), max_nkeys * KY_SZPAR, TY_CHAR)
	if (KY_UNIT_SELECT(key) != NULL)
	    call mfree (KY_UNIT_SELECT(key), TY_CHAR)
	call malloc (KY_UNIT_SELECT(key), max_nkeys * KY_SZPAR, TY_CHAR)
	if (KY_FMT_SELECT(key) != NULL)
	    call mfree (KY_FMT_SELECT(key), TY_CHAR)
	call malloc (KY_FMT_SELECT(key), max_nkeys * KY_SZPAR, TY_CHAR)

	nop = KY_NAME_SELECT(key)
	uop = KY_UNIT_SELECT(key)
	fop = KY_FMT_SELECT(key)
	nkeys = 0

	# Loop through the fields list.
	list = pt_ofnl (key, fields)
	while (pt_gnfn (list, Memc[kname], Memc[aranges], KY_SZPAR) != EOF) {

	    # Find the field name and the ranges.
	    index = strdic (Memc[kname], Memc[kname], KY_SZPAR,
	        Memc[KY_WORDS(key)])
	    if (pt_ranges (Memc[aranges], Memc[ranges], element,
	        SZ_FNAME) == ERR)
		call error (0, "Cannot decode apphot range string")
	    if (index == 0)
		next

	    # Get the length, format and the units strings.
	    nelems = pt_kstati (key, Memc[kname], KY_NUMELEMS)
	    len = pt_kstati (key, Memc[kname], KY_LENGTH)
	    call pt_kstats (key, Memc[kname], KY_UNITSTR, Memc[uname],
	        KY_SZPAR)
	    call pt_kstats (key, Memc[kname], KY_FMTSTR, Memc[fname],
	        KY_SZPAR)

	    # Load the fields.
	    if (nelems == 1) {

	        Memi[KY_SELECT(key)+nkeys] = index
	        Memi[KY_ELEM_SELECT(key)+nkeys] = 1
		Memi[KY_LEN_SELECT(key)+nkeys] = len

		call sprintf (Memc[nop], len, "%*.*s")
		    call pargi (-len)
		    call pargi (len)
		    call pargstr (Memc[kname])
		nop = nop + len

		call sprintf (Memc[uop], len, "%*.*s")
		    call pargi (-len)
		    call pargi (len)
		    call pargstr (Memc[uname])
		uop = uop + len

		call sprintf (Memc[fop], len, "%*.*s")
		    call pargi (-len)
		    call pargi (len)
		    call pargstr (Memc[fname])
		fop = fop + len

	        nkeys = nkeys + 1

	    } else {

		if (Memc[ranges] == EOS) {
		    call sprintf (Memc[ranges], SZ_FNAME, "1-%d")
			call pargi (nelems)
		}
		if (decode_ranges (Memc[ranges], Memi[rangeset], KY_MAXNRANGES,
		    elems) == ERR)
		    call error (0, "Cannot decode ranges string")

		elems = 0
		while (get_next_number (Memi[rangeset], elems) != EOF) {

		    if (elems < 1 || elems > nelems)
			break
		    Memi[KY_SELECT(key)+nkeys] = index
		    Memi[KY_ELEM_SELECT(key)+nkeys] = elems
		    Memi[KY_LEN_SELECT(key)+nkeys] = len

		    call sprintf (Memc[nop], max_nkeys * KY_SZPAR, "%s%*.*d")
		        call pargstr (Memc[kname])
		        call pargi (-len)
			call pargi (len)
			call pargi (elems)
		    nop = nop + len

		    call sprintf (Memc[uop], len, "%*.*s")
		        call pargi (-len)
			call pargi (len)
		        call pargstr (Memc[uname])
		    uop = uop + len

		    call sprintf (Memc[fop], len, "%*.*s")
		        call pargi (-len)
			call pargi (len)
		        call pargstr (Memc[fname])
		    fop = fop + len

		    nkeys = nkeys + 1
		}
	    }
	}

	# Reallocate the select buffer space.
	KY_NSELECT(key) = nkeys
	call realloc (KY_SELECT(key), KY_NSELECT(key), TY_INT)
	call realloc (KY_ELEM_SELECT(key), KY_NSELECT(key), TY_INT)
	call realloc (KY_LEN_SELECT(key), KY_NSELECT(key), TY_INT)
	call realloc (KY_NAME_SELECT(key), KY_NSELECT(key) * KY_SZPAR, TY_CHAR)
	call realloc (KY_UNIT_SELECT(key), KY_NSELECT(key) * KY_SZPAR, TY_CHAR)
	call realloc (KY_FMT_SELECT(key), KY_NSELECT(key) * KY_SZPAR, TY_CHAR)

	# Free list storage space.
	call pt_cfnl (list)
	call sfree (sp)

	return (nkeys)
end