aboutsummaryrefslogtreecommitdiff
path: root/noao/onedspec/dispcor/dispcor.x
blob: 7f2c32a80f22f9403f586a3a328b28ffb3668583 (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
include	<math/iminterp.h>

# DISPCOR -- Dispersion correct input spectrum to output spectrum.
# This procedure uses the MWCS forward and inverse transformations
# and interpolate the input data, conserving flux if desired.  Image
# interpolation uses the image interpolation package and flux conservation
# integrates the interpolation function across the output pixel.  This
# procedure does some CLIO to get the interpolation function and to
# query whether to conserve flux.

procedure dispcor (cti, linei, cto, lineo, in, npts, out, nw, flux)

pointer	cti			#I MWCS input inverse transformation
int	linei			#I Spectrum line
pointer	cto			#I MWCS output forward transformation
int	lineo			#I Spectrum line
real	in[npts]		#I Input spectrum
int	npts			#I Number of input pixels
real	out[nw]			#O Output spectrum
int	nw			#I Number of output pixels
bool	flux			#I Conserve flux

char	interp[10]
bool	ofb_a, ofb_b
int	i, j, ia, ib, clgwrd()
real	a, b, sum, asieval(), asigrl()
double	x, xmin, xmax, w, y1, y2, smw_c1trand()
pointer	asi, temp

begin
	# Get the image buffers fit the interpolation function to the
	# input spectrum.  Extend the interpolation by one pixel at each end.

	call malloc (temp, npts+2, TY_REAL)
	call amovr (in, Memr[temp+1], npts)
	Memr[temp] = in[1]
	Memr[temp+npts+1] = in[npts]

	call asiinit (asi, clgwrd ("interp", interp, 10, II_FUNCTIONS))
	call asifit (asi, Memr[temp], npts+2)

	call mfree (temp, TY_REAL)

	# Determine edges of output pixels in input spectrum and integrate
	# using ASIGRL.  If not flux conserving take the average.

	xmin = 0.5
	xmax = npts + 0.5

	x = 0.5
	if (IS_INDEFI(lineo))
	    w = smw_c1trand (cto, x)
	else {
	    y1 = lineo
	    call smw_c2trand (cto, x, y1, w, y2)
	}
	if (IS_INDEFI(linei))
	    x = smw_c1trand (cti, w)
	else {
	    #y2 = linei
	    call smw_c2trand (cti, w, y2, x, y1)
	}
	ofb_b = (x < xmin || x > xmax)
	b = max (xmin, min (xmax, x)) + 1
	do i = 1, nw {
	    ofb_a = ofb_b
	    a = b
	    x = i + 0.5
	    if (IS_INDEFI(lineo))
		w = smw_c1trand (cto, x)
	    else {
		y1 = lineo
		call smw_c2trand (cto, x, y1, w, y2)
	    }
	    if (IS_INDEFI(linei))
		x = smw_c1trand (cti, w)
	    else {
		#y2 = linei
		call smw_c2trand (cti, w, y2, x, y1)
	    }
	    ofb_b = (x < xmin || x > xmax)
	    b = max (xmin, min (xmax, x)) + 1
	    if (ofb_a && ofb_b)
		out[i] = 0.
	    else if (a <= b) {
		ia = nint (a + 0.5)
		ib = nint (b - 0.5)
		if (abs (a+0.5-ia) < 0.00001 && abs (b-0.5-ib) < 0.00001) {
		    sum = 0.
		    do j = ia, ib
			sum = sum + asieval (asi, real(j))
		    out[i] = sum
		} else
		    out[i] = asigrl (asi, a, b)
		if (!flux)
		    out[i] = out[i] / max (b - a, 1e-4)
	    } else {
		ib = nint (b + 0.5)
		ia = nint (a - 0.5)
		if (abs (a-0.5-ia) < 0.00001 && abs (b+0.5-ib) < 0.00001) {
		    sum = 0.
		    do j = ib, ia
			sum = sum + asieval (asi, real(j))
		    out[i] = sum
		} else
		    out[i] = asigrl (asi, b, a)
		if (!flux)
		    out[i] = out[i] / max (a - b, 1e-4)
	    }
	}

	call asifree (asi)
end


# DISPCORA -- Dispersion correct input spectrum to output spectrum.
# This procedure uses the MWCS forward and inverse transformations
# and interpolate the input data, conserving flux if desired.  Image
# interpolation uses the image interpolation package and flux conservation
# integrates the interpolation function across the output pixel.  This
# procedure does some CLIO to get the interpolation function and to
# query whether to conserve flux.
# 
# This differs from DISPCOR by the "blank" argument.

procedure dispcora (cti, linei, cto, lineo, in, npts, out, nw, flux, blank)

pointer	cti			#I MWCS input inverse transformation
int	linei			#I Spectrum line
pointer	cto			#I MWCS output forward transformation
int	lineo			#I Spectrum line
real	in[npts]		#I Input spectrum
int	npts			#I Number of input pixels
real	out[nw]			#O Output spectrum
int	nw			#I Number of output pixels
bool	flux			#I Conserve flux
real	blank			#I Out of bounds value (INDEF to leave unchanged

char	interp[10]
bool	ofb_a, ofb_b
int	i, j, ia, ib, clgwrd()
real	a, b, sum, asieval(), asigrl()
double	x, xmin, xmax, w, y1, y2, smw_c1trand()
pointer	asi, temp

begin
	# Get the image buffers fit the interpolation function to the
	# input spectrum.  Extend the interpolation by one pixel at each end.

	call malloc (temp, npts+2, TY_REAL)
	call amovr (in, Memr[temp+1], npts)
	Memr[temp] = in[1]
	Memr[temp+npts+1] = in[npts]

	call asiinit (asi, clgwrd ("interp", interp, 10, II_FUNCTIONS))
	call asifit (asi, Memr[temp], npts+2)

	call mfree (temp, TY_REAL)

	# Determine edges of output pixels in input spectrum and integrate
	# using ASIGRL.  If not flux conserving take the average.

	xmin = 0.5
	xmax = npts + 0.5

	x = 0.5
	if (IS_INDEFI(lineo))
	    w = smw_c1trand (cto, x)
	else {
	    y1 = lineo
	    call smw_c2trand (cto, x, y1, w, y2)
	}
	if (IS_INDEFI(linei))
	    x = smw_c1trand (cti, w)
	else {
	    #y2 = linei
	    call smw_c2trand (cti, w, y2, x, y1)
	}
	ofb_b = (x < xmin || x > xmax)
	b = max (xmin, min (xmax, x)) + 1
	do i = 1, nw {
	    ofb_a = ofb_b
	    a = b
	    x = i + 0.5
	    if (IS_INDEFI(lineo))
		w = smw_c1trand (cto, x)
	    else {
		y1 = lineo
		call smw_c2trand (cto, x, y1, w, y2)
	    }
	    if (IS_INDEFI(linei))
		x = smw_c1trand (cti, w)
	    else {
		#y2 = linei
		call smw_c2trand (cti, w, y2, x, y1)
	    }
	    ofb_b = (x < xmin || x > xmax)
	    b = max (xmin, min (xmax, x)) + 1
	    if (ofb_a && ofb_b) {
		if (!IS_INDEFR(blank))
		    out[i] = blank
	    } else if (a == b) {
		if (!IS_INDEFR(blank))
		    out[i] = blank
	    } else if (a < b) {
		ia = nint (a + 0.5)
		ib = nint (b - 0.5)
		if (abs (a+0.5-ia) < 0.00001 && abs (b-0.5-ib) < 0.00001) {
		    sum = 0.
		    do j = ia, ib
			sum = sum + asieval (asi, real(j))
		    out[i] = sum
		} else
		    out[i] = asigrl (asi, a, b)
		if (!flux)
		    out[i] = out[i] / max (b - a, 1e-4)
	    } else {
		ib = nint (b + 0.5)
		ia = nint (a - 0.5)
		if (abs (a-0.5-ia) < 0.00001 && abs (b+0.5-ib) < 0.00001) {
		    sum = 0.
		    do j = ib, ia
			sum = sum + asieval (asi, real(j))
		    out[i] = sum
		} else
		    out[i] = asigrl (asi, b, a)
		if (!flux)
		    out[i] = out[i] / max (a - b, 1e-4)
	    }
	}

	call asifree (asi)
end