aboutsummaryrefslogtreecommitdiff
path: root/noao/onedspec/scombine/icombine.x
blob: 5650d3ababb0e6bb3e08e84a1ee231a463680445 (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
include	<mach.h>
include	<smw.h>
include "icombine.h"


# IC_COMBINE -- Combine images.

procedure ic_combiner (sh, shout, d, id, n, m, lflag, scales, zeros, wts,
	nimages, npts)

pointer	sh[nimages]		# Input spectra
pointer	shout			# Output spectrum
pointer	d[nimages]		# Data pointers
pointer	id[nimages]		# Image index ID pointers
int	n[npts]			# Number of good pixels
pointer	m[nimages]		# Mask pointers
int	lflag[nimages]		# Line flags
real	scales[nimages]		# Scale factors
real	zeros[nimages]		# Zero offset factors
real	wts[nimages]		# Combining weights
int	nimages			# Number of input images
int	npts			# Number of points per output line

int	i, ctor()
real	r
pointer	sp, nm
errchk	ic_scale

include	"icombine.com"

begin
	call smark (sp)

	# Rebin spectra and set mask arrays
	call scb_rebin (sh, shout, lflag, nimages, npts)

	# Set scale and weights and log
	call ic_scale (sh, shout, lflag, scales, zeros, wts, nimages)

	# Set combine parameters
	switch (combine) {
	case AVERAGE:
	    if (dowts)
		keepids = true
	    else
		keepids = false
	case MEDIAN:
	    dowts = false
	    keepids = false
	case SUM:
	    keepids = false
	    reject = NONE
	    grow = 0
	}
	docombine = true

	# Set rejection algorithm specific parameters
	switch (reject) {
	case CCDCLIP, CRREJECT:
	    call salloc (nm, 3*nimages, TY_REAL)
	    i = 1
	    if (ctor (Memc[rdnoise], i, r) > 0) {
		do i = 1, nimages
		    Memr[nm+3*(i-1)] = r
	    } else {
		do i = 1, nimages
		    Memr[nm+3*(i-1)] = RA(sh[i])
	    }
	    i = 1
	    if (ctor (Memc[gain], i, r) > 0) {
		do i = 1, nimages {
		    Memr[nm+3*(i-1)+1] = r
		    Memr[nm+3*(i-1)] = (Memr[nm+3*(i-1)] / r) ** 2
		}
	    } else {
		do i = 1, nimages {
		    r = DEC(sh[i])
		    Memr[nm+3*(i-1)+1] = r
		    Memr[nm+3*(i-1)] = (Memr[nm+3*(i-1)] / r) ** 2
		}
	    }
	    i = 1
	    if (ctor (Memc[snoise], i, r) > 0) {
		do i = 1, nimages
		    Memr[nm+3*(i-1)+2] = r
	    } else {
		do i = 1, nimages {
		    r = UT(sh[i])
		    Memr[nm+3*(i-1)+2] = r
		}
	    }
	    if (!keepids) {
		if (doscale1 || grow > 0)
		    keepids = true
		else {
		    do i = 2, nimages {
			if (Memr[nm+3*(i-1)] != Memr[nm] ||
			    Memr[nm+3*(i-1)+1] != Memr[nm+1] ||
			    Memr[nm+3*(i-1)+2] != Memr[nm+2]) {
			    keepids = true
			    break
			}
		    }
		}
	    }
	    if (reject == CRREJECT)
		lsigma = MAX_REAL
	case MINMAX:
	    mclip = false
	    if (grow > 0)
		keepids = true
	case PCLIP:
	    mclip = true
	    if (grow > 0)
		keepids = true
	case AVSIGCLIP, SIGCLIP:
	    if (doscale1 || grow > 0)
		keepids = true
	case NONE:
	    mclip = false
	    grow = 0
	}

	if (keepids) {
	    do i = 1, nimages
		call salloc (id[i], npts, TY_INT)
	}

	call ic_gdatar (sh, d, id, n, m, lflag, scales, zeros, nimages, npts)

	switch (reject) {
	case CCDCLIP, CRREJECT:
	    if (mclip)
		call ic_mccdclipr (d, id, n, scales, zeros, Memr[nm],
		    nimages, npts, Memr[SY(shout)])
	    else
		call ic_accdclipr (d, id, n, scales, zeros, Memr[nm],
		    nimages, npts, Memr[SY(shout)])
	case MINMAX:
	    call ic_mmr (d, id, n, npts)
	case PCLIP:
	    call ic_pclipr (d, id, n, nimages, npts, Memr[SY(shout)])
	case SIGCLIP:
	    if (mclip)
		call ic_msigclipr (d, id, n, scales, zeros, nimages, npts,
		    Memr[SY(shout)])
	    else
		call ic_asigclipr (d, id, n, scales, zeros, nimages, npts,
		    Memr[SY(shout)])
	case AVSIGCLIP:
	    if (mclip)
		call ic_mavsigclipr (d, id, n, scales, zeros, nimages,
		    npts, Memr[SY(shout)])
	    else
		call ic_aavsigclipr (d, id, n, scales, zeros, nimages,
		    npts, Memr[SY(shout)])
	}

	if (grow > 0)
	    call ic_growr (d, id, n, nimages, npts, Memr[SY(shout)])

	if (docombine) {
	    switch (combine) {
	    case AVERAGE:
		call ic_averager (d, id, n, wts, npts, Memr[SY(shout)])
	    case MEDIAN:
		call ic_medianr (d, n, npts, Memr[SY(shout)])
	    case SUM:
		call ic_sumr (d, n, npts, Memr[SY(shout)])
	    }
	}

	call sfree (sp)
end