aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/photcal/fitparams/ftweights.x
blob: 67482867e024376fe60ae40b432b766e249b4b17 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
include	"../lib/parser.h"
include "../lib/preval.h"

# FT_WTUNIFORM -- Set all the weights to 1.0.

procedure ft_wtuniform (otable, wtable)

pointer	otable				# observation table
pointer	wtable				# weight table (output)

int	nobs
int	mct_nrows()

begin
	# Get number of observations.
	nobs = mct_nrows (otable)

	# Clear weight table with ones (uniform weighting), and enter
	# data at the last observation set weight table counters.

	call mct_clearr (wtable, 1.0)
	call mct_putr (wtable, nobs, 1, 1.0)
end


# FT_WTEQNS -- Compute the value of the weights equation for each point. 

procedure ft_wteqns (sym, otable, wtable)

int	sym				# equation symbol
pointer	otable				# observation table
pointer	wtable				# weight table (output)

int	n, nobs
real	wtval, minval, maxval
pointer	varptr, parptr
pointer	wtcode, mincode, maxcode

#bool	clgetb()
int	mct_nrows()
pointer	pr_gsymp(), mct_getrow()
real	pr_eval()

begin
	# Debug ?
	#if (clgetb ("debug.fitcode"))
	    #call eprintf ("ft_wteval (sym=%d) (ot=%d) (wt=%d)\n") {
		#call pargi (sym)
		#call pargi (otable)
		#call pargi (wtable)
	#}

	# Get number of observations.
	nobs = mct_nrows (otable)

	# Clear weight table with ones (uniform weighting), and enter
	# data at the last observation set weight table counters.

	call mct_clearr (wtable, 1.0)
	call mct_putr (wtable, nobs, 1, 1.0)

	# Compute weigths using the weight equation, if there is one defined.
	wtcode = pr_gsymp (sym, PTEQRPNWEIGHT)
	if (wtcode != NULL) {

	    # Get pointer to parameter values for the current equation.
	    parptr = pr_gsymp (sym, PTEQSPARVAL)

	    # Get minimum and maximum equation codes.
	    mincode = pr_gsymp (sym, PTEQRPNWTSMIN)
	    maxcode = pr_gsymp (sym, PTEQRPNWTSMAX)

	    # Iterate over all observations.
	    do n = 1, nobs {

		# Get variable values for the current observation.
		varptr = mct_getrow (otable, n)

		# Compute weight value.
		wtval = pr_eval (wtcode, Memr[varptr], Memr[parptr])
		if (IS_INDEFR(wtval) || wtval < 0.0) {

		    call mct_putr (wtable, n, 1, 0.0)

		} else {

		    # Check value against minimum.
		    if (mincode != NULL) {
		        minval = pr_eval (mincode, Memr[varptr], Memr[parptr])
		        if (! IS_INDEFR(minval) && minval >= 0.0)
		            wtval = min (wtval, minval)
		    }

		    # Check value against maximum.
		    if (maxcode != NULL) {
		        maxval = pr_eval (maxcode, Memr[varptr], Memr[parptr])
		        if (! IS_INDEFR(maxval) && maxval >= 0.0)
		            wtval = max (wtval, maxval)
		    }

		    # Enter value into weight table.
		    call mct_putr (wtable, n, 1, wtval)
		}
	    }
	}

	# Debug ?
	#call dg_dweigths ("from ft_wteval", wtable)
end


# FT_WTPHOTERRS -- Set all the weights to the correct statistical value assuming
# that all the errors are in the photometric indices, that the errors are
# independent, that the equations are linear in the photometric indices,
# and that at least one of the observed or catalog variables in the equation
# has a measured error.

procedure ft_wtphoterrs (sym, otable, wtable)

int	sym				# equation symbol
pointer	otable				# observation table
pointer	wtable				# weight table (output)

double	errval
int	n, i, nobs, nobsvars, ncatvars, nrvars, nfvars, nerrors, symvar, icol
int	itemp
pointer	omap, cmap, sp, rcount, rerrcol, fcount, ferrcol, refcode
real	val
int	mct_nrows(), pr_geti(), pr_gsymi(), pr_gvari(), pr_findmap1()
int	ft_seval()
pointer	pr_gsymp()
#int	mct_ncols()
real	mct_getr()
errchk	pr_gsymi()

begin
	# Get number of observations.
	nobs = mct_nrows (otable)

	# Clear weight table with ones (uniform weighting), and enter
	# data at the last observation set weight table counters.

	call mct_clearr (wtable, 1.0)
	call mct_putr (wtable, nobs, 1, 1.0)

	# Map the column numbers for the observed and catalog variables.
	call pr_obsmap (omap, nobsvars)
	call pr_catmap (cmap, ncatvars)

	# Get the number of reference and fit equation variables.
	nrvars = pr_gsymi (sym, PTEQNRCAT) + pr_gsymi (sym, PTEQNROBS) 
	nfvars = pr_gsymi (sym, PTEQNFCAT) + pr_gsymi (sym, PTEQNFOBS) 

	# Allocate working space.
	call smark (sp)
	call salloc (rerrcol, nobsvars + ncatvars, TY_INT)
	call amovi (NULL, Memi[rerrcol], nobsvars + ncatvars)
	call salloc (rcount, nobsvars + ncatvars, TY_INT)
	call aclri (Memi[rcount], nobsvars + ncatvars)
	call salloc (ferrcol, nobsvars + ncatvars, TY_INT)
	call amovi (NULL, Memi[ferrcol], nobsvars + ncatvars)
	call salloc (fcount, nobsvars + ncatvars, TY_INT)
	call aclri (Memi[fcount], nobsvars + ncatvars)

	# Initialize.
	nerrors = 0

	# Compute the positions of the reference equation variable errors in
	# the observations table.

	do i = 1, nrvars {
	    Memi[rcount+i-1] = pr_gvari (sym, i, PTEQREFCNT)
	    symvar = pr_gvari (sym, i, PTEQREFVAR)
	    icol = pr_gsymi (symvar, PINPERRCOL)
	    if (IS_INDEFI(icol))
		Memi[rerrcol+i-1] = NULL
	    else {
		if (pr_gsymi (symvar, PSYMTYPE) == PTY_CATVAR)
		    Memi[rerrcol+i-1] = pr_findmap1 (cmap, icol) + nobsvars
		else
		    Memi[rerrcol+i-1] = pr_findmap1 (omap, icol)
		nerrors = nerrors + 1
	    }
	}

	# Evaluate the contribution of the set equations to the errors
	# in the reference equation.

	if (pr_geti (NSETEQS) > 0) {
	    refcode = pr_gsymp (sym, PTEQRPNREF)
	    itemp = ft_seval (refcode, cmap, omap, nobsvars,
	        Memi[rerrcol], Memi[rcount], nrvars)
	    nrvars = nrvars + itemp 
	    nerrors = nerrors + itemp
	}

	# Compute the positions of the fit equation variable errors in the
	# observations table.

	do i = 1, nfvars {
	    Memi[fcount+i-1] = pr_gvari (sym, i, PTEQFITCNT)
	    symvar = pr_gvari (sym, i, PTEQFITVAR)
	    icol = pr_gsymi (symvar, PINPERRCOL)
	    if (IS_INDEFI(icol))
		Memi[ferrcol+i-1] = NULL
	    else {
		if (pr_gsymi (symvar, PSYMTYPE) == PTY_CATVAR)
		    Memi[ferrcol+i-1] = pr_findmap1 (cmap, icol) + nobsvars
		else
		    Memi[ferrcol+i-1] = pr_findmap1 (omap, icol)
		nerrors = nerrors + 1
	    }
	}

	# Evaluate the contribution of the set equations to the errors
	# in the fit equation.

	if (pr_geti (NSETEQS) > 0) {
	    refcode = pr_gsymp (sym, PTEQRPNFIT)
	    itemp = ft_seval (refcode, cmap, omap, nobsvars,
	        Memi[ferrcol], Memi[fcount], nfvars)
	    nfvars = nfvars + itemp
	    nerrors = nerrors + itemp
	}

	# Loop over the table rows.
	if (nerrors > 0) {
	    do n = 1, nobs {

	        errval = 0.0d0

	        # Add contributions from the reference equation variables.
	        do i = 1, nrvars {
		    if (Memi[rerrcol+i-1] == NULL)
		        next
		    val = mct_getr (otable, n, Memi[rerrcol+i-1])
		    if (IS_INDEFR(val) || val < 0.0)
			next
		    errval = errval + real (Memi[rcount+i-1]) * (val * val)
	        }

	        # Add contributions from the fitting equation variables.
	        do i = 1, nfvars {
		    if (Memi[ferrcol+i-1] == NULL)
		        next
		    val = mct_getr (otable, n, Memi[ferrcol+i-1])
		    if (IS_INDEFR(val) || val < 0.0)
			next
		    errval = errval + real (Memi[fcount+i-1]) * (val * val)
	        }

	        # Check for negative and zero error values and enter value
		# into weight table.
	        if (errval <= 0.0d0)
	            call mct_putr (wtable, n, 1, 0.0)
		else
	            call mct_putr (wtable, n, 1, real (1.0d0 / errval))
	    }
	}

	call pr_unmap (cmap)
	call pr_unmap (omap)

	call sfree (sp)
end


# FT_SEVAL -- Locate any catalog and observations variables that are
# defined in up to two levels of set equations.

int procedure ft_seval (code, cmap, omap, nobsvars, errcol, ecount, nerrors)

pointer	code		# the equation code
pointer	cmap		# pointer to the catalog variable column map
pointer	omap		# pointer to the observations variable column map
int	nobsvars	# the number of observations variables
int	errcol[ARB]	# the input/output error columns
int	ecount[ARB]	# the input/output error column counts
int	nerrors		# the previous number of errors

int	ip, ins, newerr
pointer	setcode
int	pr_gsym(), ft_scoeval()
pointer	pr_gsymp()

begin
	ip = 0
	ins = Memi[code+ip]

	newerr = 0
	while (ins != PEV_EOC) {
	    switch (ins) {
	    case PEV_SETEQ:
		ip = ip + 1
		setcode = pr_gsymp (pr_gsym (Memi[code+ip], PTY_SETEQ),
		    PSEQRPNEQ)
		newerr = newerr + ft_scoeval (setcode, cmap, omap, nobsvars,
		    errcol, ecount, nerrors + newerr)
	    case PEV_NUMBER, PEV_CATVAR, PEV_OBSVAR, PEV_PARAM:
		ip = ip + 1
	    case PEV_EXTEQ, PEV_TRNEQ:
		ip = ip + 1
	    default:
		;
	    }

	    ip = ip + 1
	    ins = Memi[code+ip]
	}

	return (newerr)
end


# FT_SCOEVAL -- Locate any catalog and observations variables as well as any
# set equations that are defined in a previous set equation.

int procedure ft_scoeval (code, cmap, omap, nobsvars, errcol, ecount, nerrors)

pointer	code		# the equation code
pointer	cmap		# pointer to the catalog variable column map
pointer	omap		# pointer to the observations variable column map
int	nobsvars	# the number of observations variables
int	errcol[ARB]	# the input/output error columns
int	ecount[ARB]	# the input/output error column counts
int	nerrors		# the previous number of errors

bool	new
int	i, ip, ins, newerr, ecol
pointer	sym, setcode
int	pr_gsym(), pr_gsymi(), pr_findmap1(), ft_coeval()
pointer	pr_gsymp()

begin
	ip = 0
	ins = Memi[code+ip]

	newerr = 0
	while (ins != PEV_EOC) {
	    switch (ins) {
	    case PEV_SETEQ:
		ip = ip + 1
		setcode = pr_gsymp (pr_gsym (Memi[code+ip], PTY_SETEQ),
		    PSEQRPNEQ)
		newerr = newerr + ft_coeval (setcode, cmap, omap, nobsvars,
		    errcol, ecount, nerrors + newerr)
	    case PEV_CATVAR:
		ip = ip + 1
		sym = pr_gsym (Memi[code+ip] - nobsvars, PTY_CATVAR)
		ecol = pr_gsymi (sym, PINPERRCOL)
		if (! IS_INDEFI(ecol))
		    ecol = pr_findmap1 (cmap, ecol) + nobsvars
		else
		    ecol = NULL
		if (ecol != NULL) {
		    new = true
		    do i = 1, nerrors + newerr {
			if (ecol != errcol[i])
			    next
			new = false
			ecount[i] = ecount[i] + 1 
		    }
		    if (new) {
			newerr = newerr + 1
			errcol[nerrors+newerr] = ecol
			ecount[nerrors+newerr] = 1 
		    }
		}
	    case PEV_OBSVAR:
		ip = ip + 1
		sym = pr_gsym (Memi[code+ip], PTY_OBSVAR)
		ecol = pr_gsymi (sym, PINPERRCOL)
		if (! IS_INDEFI(ecol))
		    ecol = pr_findmap1 (omap, ecol)
		else
		    ecol = NULL
		if (ecol != NULL) {
		    new = true
		    do i = 1, nerrors + newerr {
			if (ecol != errcol[i])
			    next
			new = false
			ecount[i] = ecount[i] + 1 
		    }
		    if (new) {
			newerr = newerr + 1
			errcol[nerrors+newerr] = ecol
			ecount[nerrors+newerr] = 1 
		    }
		}
	    case PEV_NUMBER, PEV_PARAM:
		ip = ip + 1
	    case PEV_EXTEQ, PEV_TRNEQ:
		ip = ip + 1
	    default:
		;
	    }

	    ip = ip + 1
	    ins = Memi[code+ip]
	}

	return (newerr)
end


# FT_COEVAL -- Locate any catalog and observations variables that are defined i
# a previous set equation.

int procedure ft_coeval (code, cmap, omap, nobsvars, errcol, ecount, nerrors)

pointer	code		# the equation code
pointer	cmap		# pointer to the catalog variable column map
pointer	omap		# pointer to the observations variable column map
int	nobsvars	# the number of observations variables
int	errcol[ARB]	# the input/output error columns
int	ecount[ARB]	# the input/output error column counts
int	nerrors		# the previous number of errors

bool	new
int	i, ip, ins, newerr, ecol
pointer	sym
int	pr_gsym(), pr_gsymi(), pr_findmap1()

begin
	ip = 0
	ins = Memi[code+ip]

	newerr = 0
	while (ins != PEV_EOC) {
	    switch (ins) {
	    case PEV_SETEQ:
		ip = ip + 1
	    case PEV_CATVAR:
		ip = ip + 1
		sym = pr_gsym (Memi[code+ip] - nobsvars, PTY_CATVAR)
		ecol = pr_gsymi (sym, PINPERRCOL)
		if (! IS_INDEFI(ecol))
		    ecol = pr_findmap1 (cmap, ecol) + nobsvars
		else
		    ecol = NULL
		if (ecol != NULL) {
		    new = true
		    do i = 1, nerrors + newerr {
			if (ecol != errcol[i])
			    next
			new = false
			ecount[i] = ecount[i] + 1 
		    }
		    if (new) {
			newerr = newerr + 1
			errcol[nerrors+newerr] = ecol
			ecount[nerrors+newerr] = 1 
		    }
		}
	    case PEV_OBSVAR:
		ip = ip + 1
		sym = pr_gsym (Memi[code+ip], PTY_OBSVAR)
		ecol = pr_gsymi (sym, PINPERRCOL)
		if (! IS_INDEFI(ecol))
		    ecol = pr_findmap1 (omap, ecol)
		else
		    ecol = NULL
		if (ecol != NULL) {
		    new = true
		    do i = 1, nerrors + newerr {
			if (ecol != errcol[i])
			    next
			new = false
			ecount[i] = ecount[i] + 1 
		    }
		    if (new) {
			newerr = newerr + 1
			errcol[nerrors+newerr] = ecol
			ecount[nerrors+newerr] = 1 
		    }
		}
	    case PEV_NUMBER, PEV_PARAM:
		ip = ip + 1
	    case PEV_EXTEQ, PEV_TRNEQ:
		ip = ip + 1
	    default:
		;
	    }

	    ip = ip + 1
	    ins = Memi[code+ip]
	}

	return (newerr)
end