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
|
include <error.h>
include <imhdr.h>
include <smw.h>
define EXTN_LOOKUP 10 # Interp index for de-extinction
define VLIGHT 2.997925e5 # Speed of light, Km/sec
# T_DOPCOR -- Apply doppler correction to spectra.
procedure t_dopcor ()
int inlist # List of input spectra
int outlist # List of output spectra
double z # Doppler redshift or velocity
bool isvel # Is redshift parameter a velocity?
bool add # Add to existing correction?
bool dcor # Apply dispersion correction?
bool fcor # Apply flux correction?
real ffac # Flux correction factor (power of 1+z)
pointer aps # Apertures
bool verbose # Verbose?
real fcval
bool wc, fc, aplow[2], aphigh[2]
int i, j, ap, beam, nw, dtype
double w1, dw, zold, znew, zvel
pointer ptr, in, out, mw, sh, inbuf, outbuf
pointer sp, input, output, vkey, apstr, key, log, coeff
real clgetr()
double imgetd()
bool clgetb(), streq(), rng_elementi()
int imtopenp(), imtgetim(), ctod()
pointer rng_open(), immap(), smw_openim(), imgl3r(), impl3r()
errchk immap, imgetd, imgstr,imgl3r, impl3r
errchk smw_openim, shdr_open, smw_gwattrs
begin
call smark (sp)
call salloc (input, SZ_FNAME, TY_CHAR)
call salloc (output, SZ_FNAME, TY_CHAR)
call salloc (vkey, SZ_FNAME, TY_CHAR)
call salloc (apstr, SZ_FNAME, TY_CHAR)
call salloc (key, SZ_FNAME, TY_CHAR)
call salloc (log, SZ_LINE, TY_CHAR)
coeff = NULL
# Parameters
inlist = imtopenp ("input")
outlist = imtopenp ("output")
call clgstr ("redshift", Memc[vkey], SZ_FNAME)
isvel = clgetb ("isvelocity")
add = clgetb ("add")
dcor = clgetb ("dispersion")
fcor = clgetb ("flux")
if (fcor)
ffac = clgetr ("factor")
else
ffac = 0.
call clgstr ("apertures", Memc[apstr], SZ_FNAME)
verbose = clgetb ("verbose")
# Parameter checks
if (!dcor && !fcor)
call error (1, "No correction specified")
iferr (aps = rng_open (Memc[apstr], INDEF, INDEF, INDEF))
call error (1, "Bad aperture list")
if (Memc[apstr] == EOS)
call strcpy ("all", Memc[apstr], SZ_LINE)
# Loop over input images.
while (imtgetim (inlist, Memc[input], SZ_FNAME) != EOF) {
if (imtgetim (outlist, Memc[output], SZ_FNAME) == EOF)
call strcpy (Memc[input], Memc[output], SZ_FNAME)
iferr {
in = NULL
out = NULL
mw = NULL
sh = NULL
# Map and check input image.
if (streq (Memc[input], Memc[output]))
ptr = immap (Memc[input], READ_WRITE, 0)
else
ptr = immap (Memc[input], READ_ONLY, 0)
in = ptr
ptr = smw_openim (in); mw = ptr
call shdr_open (in, mw, 1, 1, INDEFI, SHHDR, sh)
if (DC(sh) == DCNO) {
call sprintf (Memc[output], SZ_LINE,
"[%s] has no dispersion function")
call pargstr (Memc[input])
call error (1, Memc[output])
}
# Map output image.
if (streq (Memc[input], Memc[output]))
ptr = in
else
ptr = immap (Memc[output], NEW_COPY, in)
out = ptr
# Set velocity and flux correction
i = 1
if (Memc[vkey] == '-' || Memc[vkey] == '+') {
if (ctod (Memc[vkey+1], i, z) == 0) {
z = imgetd (in, Memc[vkey+1])
if (Memc[vkey] == '-') {
if (isvel)
z = -z
else
z = 1 / (1 + z) - 1
}
} else if (Memc[vkey] == '-')
z = -z
} else {
if (ctod (Memc[vkey], i, z) == 0)
z = imgetd (in, Memc[vkey])
}
zvel = z
if (isvel) {
z = z / VLIGHT
if (abs (z) >= 1.)
call error (1, "Impossible velocity")
z = sqrt ((1 + z) / (1 - z)) - 1
}
if (z <= -1.)
call error (1, "Impossible redshift")
if (fcor) {
fcval = (1 + z) ** ffac
if (in != out && IM_PIXTYPE(out) != TY_DOUBLE)
IM_PIXTYPE(out) = TY_REAL
}
# Go through spectrum and apply corrections.
switch (SMW_FORMAT(mw)) {
case SMW_ND:
if (dcor) {
call smw_gwattrs (mw, 1, 1, ap, beam, dtype,
w1, dw, nw, zold, aplow, aphigh, coeff)
if (add)
znew = (1+z) * (1+zold) - 1
else
znew = z
call smw_swattrs (mw, 1, 1, ap, beam, dtype,
w1, dw, nw, znew, aplow, aphigh, Memc[coeff])
}
if (fcor || in != out) {
do j = 1, IM_LEN(in,3) {
do i = 1, IM_LEN(in,2) {
inbuf = imgl3r (in, i, j)
outbuf = impl3r (out, i, j)
if (fcor)
call amulkr (Memr[inbuf], fcval,
Memr[outbuf], IM_LEN(in,1))
else
call amovr (Memr[inbuf], Memr[outbuf],
IM_LEN(in,1))
}
}
}
case SMW_ES, SMW_MS:
do i = 1, IM_LEN(in,2) {
call shdr_open (in, mw, i, 1, INDEFI, SHHDR, sh)
if (rng_elementi (aps, AP(sh))) {
wc = dcor
fc = fcor
} else {
wc = false
fc = false
}
if (wc) {
call smw_gwattrs (mw, i, 1, ap, beam, dtype,
w1, dw, nw, zold, aplow, aphigh, coeff)
if (add)
znew = (1+z) * (1+zold) - 1
else
znew = z
call smw_swattrs (mw, i, 1, ap, beam, dtype, w1,
dw, nw, znew, aplow, aphigh, Memc[coeff])
if (mw != MW(sh)) {
MW(sh) = NULL
call shdr_close (sh)
}
}
# Correct fluxes
# Note that if the operation is in-place we can skip
# this step if there is no corrections. Otherwise we
# still have to copy the data even if there is no
# correction.
if (fc || in != out) {
do j = 1, IM_LEN(in,3) {
call shdr_open (in, mw, i, j, INDEFI,
SHDATA, sh)
outbuf = impl3r (out, i, j)
if (fc)
call amulkr (Memr[SY(sh)], fcval,
Memr[outbuf], SN(sh))
else
call amovr (Memr[SY(sh)], Memr[outbuf],
SN(sh))
if (IM_LEN(out,1) > SN(sh))
call amovkr (Memr[outbuf+SN(sh)-1],
Memr[outbuf+SN(sh)],
IM_LEN(out,1)-SN(sh))
}
}
}
}
# Document header.
do i = 1, 98 {
call sprintf (Memc[key], SZ_FNAME, "DOPCOR%02d")
call pargi (i)
iferr (call imgstr (out, Memc[key], Memc[log], SZ_LINE))
break
}
if (fcor) {
call sprintf (Memc[log], SZ_LINE, "%8g %g %s")
if (isvel)
call pargd (zvel)
else
call pargd (z)
call pargr (ffac)
call pargstr (Memc[apstr])
} else {
call sprintf (Memc[log], SZ_LINE, "%8g %s")
if (isvel)
call pargd (zvel)
else
call pargd (z)
call pargstr (Memc[apstr])
}
call imastr (out, Memc[key], Memc[log])
# Verbose output
if (verbose) {
call printf ("%s: Doppler correction -")
call pargstr (Memc[output])
if (SMW_FORMAT(mw) != SMW_ND) {
call printf (" apertures=%s,")
call pargstr (Memc[apstr])
}
if (isvel) {
call printf (" velocity=%8g,")
call pargd (zvel)
}
call printf (" redshift=%8g, flux factor=%g\n")
call pargd (z)
call pargr (ffac)
if (add && zold != 0.) {
call printf (" Correction added: %g + %g = %g\n")
call pargd (zold)
call pargd (z)
call pargd (znew)
}
call flush (STDOUT)
}
} then {
call erract (EA_WARN)
if (out != NULL && out != in) {
call imunmap (out)
call imdelete (Memc[output])
}
}
if (mw != NULL && out != NULL)
call smw_saveim (mw, out)
if (sh != NULL)
call shdr_close (sh)
if (mw != NULL)
call smw_close (mw)
if (out != NULL && out != in)
call imunmap (out)
if (in != NULL)
call imunmap (in)
}
call rng_close (aps)
call imtclose (inlist)
call imtclose (outlist)
call mfree (coeff, TY_CHAR)
call sfree (sp)
end
|