aboutsummaryrefslogtreecommitdiff
path: root/noao/onedspec/t_sapertures.x
blob: 4aa0c16fd805bca4372b99c672b2836966a73d6b (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
include	<error.h>
include	<imhdr.h>
include	<smw.h>

define	LEN_SAP		52		# Length of structure
define	LEN_SAPTITLE	79		# Length of title

define	AP	Memi[$1]		# Aperture number
define	BEAM	Memi[$1+1]		# Beam number
define	DTYPE	Memi[$1+2]		# Dispersion type
define	W1	Memd[P2D($1+4)]		# Starting wavelength
define	DW	Memd[P2D($1+6)]		# Wavelength per pixel
define	Z	Memd[P2D($1+8)]		# Doppler factor
define	APLOW	Memr[P2R($1+10)]	# Low aperture
define	APHIGH	Memr[P2R($1+11)]	# High aperture
define	TITLE	Memc[P2C($1+12)]	# Title
 
 
# T_SAPERTURES -- Set aperture beam numbers and titles.
 
procedure t_sapertures()
 
int	list			# Input list
bool	wcsreset		# Reset WCS?
bool	verbose			# Verbose?
pointer	saps			# Pointer to array of aperture structures
 
int	imtopenp(), imtgetim()
bool	clgetb()
pointer	sp, input, ranges, tmp, im, mw, rng_open(), immap(), smw_openim()
errchk	sap_gids, immap, smw_openim
 
begin
	call smark (sp)
	call salloc (input, SZ_FNAME, TY_CHAR)

	list = imtopenp ("input")
	wcsreset = clgetb ("wcsreset")
	verbose = clgetb ("verbose")
	call clgstr ("apertures", Memc[input], SZ_FNAME)
	iferr (ranges = rng_open (Memc[input], INDEF, INDEF, INDEF))
	    call error (0, "Bad aperture list")

	call sap_gids (saps, wcsreset, verbose)
	
	while (imtgetim (list, Memc[input], SZ_FNAME) != EOF) {
	    im = NULL
	    mw = NULL
	    iferr {
	        tmp = immap (Memc[input], READ_WRITE, 0); im = tmp
		tmp = smw_openim (im); mw = tmp
		if (SMW_FORMAT(mw) != SMW_ES && SMW_FORMAT(mw) != SMW_MS)
		    call error (1, "Wrong spectrum format")
		call sap_ms (im, mw, Memc[input], ranges, saps, verbose)
	    } then
		call erract (EA_WARN)

	    if (mw != NULL) {
		call smw_saveim (mw, im)
		call smw_close (mw)
	    }
	    if (im != NULL)
		call imunmap (im)
	}

	call rng_close (ranges)
	call imtclose (list)
	call sap_fids (saps)
	call sfree (sp)
end


# SAP_MS -- Set aperture information

procedure sap_ms (im, mw, input, ranges, saps, verbose)

pointer	im			# IMIO pointer
pointer	mw			# SMW pointer
char	input[ARB]		# Image name
pointer	ranges			# Aperture range list
pointer	saps			# Pointer to array of aperture structures
bool	verbose			# Verbose?

int	i, naps, ap, beam, dtype, nw, obeam, odtype
double	w1, dw, z, ow1, odw, oz
real	aplow[2], aphigh[2], oaplow[2], oaphigh[2]
bool	newtitle, streq(), rng_elementi()
pointer	sp, title, coeff, sap

begin
	call smark (sp)
	call salloc (title, SZ_LINE, TY_CHAR)
	coeff = NULL
 
	# Go through each spectrum and change the selected apertures.
	naps = -1
	do i = 1, SMW_NSPEC(mw) {
	    # Get aperture info
	    iferr (call smw_gwattrs (mw, i, 1, ap, beam, dtype, w1, dw, nw, z,
		aplow, aphigh, coeff))
		break

	    # Check if aperture is to be changed
	    if (!rng_elementi (ranges, ap))
		next

	    # Check for aperture info
	    for (sap = saps; Memi[sap] != NULL; sap = sap + 1)
		if (ap == AP(Memi[sap]))
		    break
	    if (Memi[sap] == NULL) {
		for (sap = saps; Memi[sap] != NULL; sap = sap + 1)
		    if (IS_INDEFI (AP(Memi[sap])))
			break
	    }
	    if (Memi[sap] == NULL)
		next

	    # Get aperture title
	    call smw_gapid (mw, i, 1, Memc[title], SZ_LINE)

	    # Set new aperture values
	    sap = Memi[sap]
	    obeam = BEAM(sap)
	    odtype = DTYPE(sap)
	    ow1 = W1(sap)
	    odw = DW(sap)
	    oz = Z(sap)
	    oaplow[1] = APLOW(sap)
	    oaphigh[1] = APHIGH(sap)
	    oaplow[2] = INDEF
	    oaphigh[2] = INDEF

	    if (IS_INDEFI (obeam))
		obeam = beam
	    if (IS_INDEFI (odtype))
		odtype = dtype
	    else
		odtype = max (-1, min (1, odtype))
	    if (IS_INDEFD (ow1))
		ow1 = w1
	    if (IS_INDEFD (odw))
		odw = dw
	    if (IS_INDEFD (oz))
		oz = z
	    if (IS_INDEF (oaplow[1]))
		oaplow[1] = aplow[1]
	    if (IS_INDEF (oaphigh[1]))
		oaphigh[1] = aphigh[1]
	    if (streq (TITLE(sap), "INDEF") || TITLE(sap) == EOS)
		newtitle = false
	    else
		newtitle = !streq (TITLE(sap), Memc[title])

	    if (dtype == 2 && odtype != 2)
		Memc[coeff] = EOS

	    # Make change if needed
	    if (obeam!=beam || odtype!=dtype || ow1!=w1 || odw !=dw || oz!=z ||
		oaplow[1]!=aplow[1] || oaphigh[1]!=aphigh[1] || newtitle) {
		call smw_swattrs (mw, i, 1, ap, obeam, odtype, ow1, odw, nw,
		    oz, oaplow, oaphigh, Memc[coeff])
		if (newtitle)
		    call smw_sapid (mw, i, 1, TITLE(sap))
		naps = naps + 1

		# Make record
		if (verbose) {
		    if (naps == 0) {
			call printf ("%s:\n")
			    call pargstr (input)
			naps = naps + 1
		    }
		    call printf ("  Aperture %d:\n")
			call pargi (ap)
		    if (obeam != beam) {
			call printf ("    beam %d --> %d\n")
			    call pargi (beam)
			    call pargi (obeam)
		    }
		    if (odtype != dtype) {
			call printf ("    dtype %d --> %d\n")
			    call pargi (dtype)
			    call pargi (odtype)
		    }
		    if (ow1 != w1) {
			call printf ("    w1 %g --> %g\n")
			    call pargd (w1)
			    call pargd (ow1)
		    }
		    if (odw != dw) {
			call printf ("    dw %g --> %g\n")
			    call pargd (dw)
			    call pargd (odw)
		    }
		    if (oz != z) {
			call printf ("    z %g --> %g\n")
			    call pargd (z)
			    call pargd (oz)
		    }
		    if (oaplow[1] != aplow[1]) {
			call printf ("    aplow %g --> %g\n")
			    call pargr (aplow[1])
			    call pargr (oaplow[1])
		    }
		    if (oaphigh[1] != aphigh[1]) {
			call printf ("    aphigh %g --> %g\n")
			    call pargr (aphigh[1])
			    call pargr (oaphigh[1])
		    }
		    if (newtitle) {
			call printf ("    apid %s --> %s\n")
			    call pargstr (Memc[title])
			    call pargstr (TITLE(sap))
		    }
		}
	    }
	}

	call mfree (coeff, TY_CHAR)
	call sfree (sp)
end


# SA_GIDS -- Get user aperture ID's.

procedure sap_gids (saps, wcsreset, verbose)

pointer	saps			# Pointer to array of aperture structures
bool	wcsreset		# Reset WCS?
bool	verbose			# Verbose (negative beam warning)?
pointer	sap

int	naps, ap, beam, fd, nalloc
double	ra, dec
pointer	sp, str, key, im, list

real	clgetr()
double	clgetd()
int	nowhite(), open(), fscan(), nscan(), clgeti()
pointer	immap(), imofnlu(), imgnfn()
errchk	open

begin

	# If resetting ignore the APIDTABLE and the task parameters.
	if (wcsreset) {
	    call malloc (saps, 2, TY_POINTER)
	    call malloc (Memi[saps], LEN_SAP, TY_STRUCT)
	    Memi[saps+1] = NULL

	    sap = Memi[saps]
	    AP(sap) = INDEFI
	    BEAM(sap) = INDEFI
	    DTYPE(sap) = -1
	    W1(sap) = 1.
	    DW(sap) = 1.
	    Z(sap) = 0.
	    APLOW(sap) = INDEF
	    APHIGH(sap) = INDEF
	    TITLE(sap) = EOS
	    return
	}

	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)
	call salloc (key, SZ_FNAME, TY_CHAR)
	call clgstr ("apidtable", Memc[str], SZ_LINE)

	# Set parameters from an APIDTABLE if given.
	naps = 0
	if (nowhite (Memc[str], Memc[str], SZ_LINE) > 0) {
	    iferr {
		# Read aperture information from an image.
		ifnoerr (im = immap (Memc[str], READ_ONLY, 0)) {
		    list = imofnlu (im, "SLFIB[0-9]*")
		    while (imgnfn (list, Memc[key], SZ_FNAME) != EOF) {
			call imgstr (im, Memc[key], Memc[str], SZ_LINE)
			call sscan (Memc[str])
			call gargi (ap)
			call gargi (beam)
			if (nscan() < 2)
			    next
			if (!IS_INDEFI(beam) && beam < 0 && verbose) {
			    call eprintf (
			    "Negative beam number for aperture %d ignored.\n") 
				call pargi (ap)
			    beam = INDEFI
			}
			if (naps == 0) {
			    nalloc = 50
			    call malloc (saps, nalloc, TY_POINTER)
			} else if (naps == nalloc) {
			    nalloc = nalloc + 50
			    call realloc (saps, nalloc, TY_POINTER)
			}
			call malloc (Memi[saps+naps], LEN_SAP, TY_STRUCT)

			sap = Memi[saps+naps]
			AP(sap) = ap
			BEAM(sap) = beam
			call gargd (ra)
			call gargd (dec)
                        if (nscan() != 4) {
                            call reset_scan ()
                            call gargi (ap)
                            call gargi (beam)
			    call gargstr (TITLE(sap), LEN_SAPTITLE)
                        } else {
                            Memc[str] = EOS
                            call gargstr (Memc[str], SZ_LINE)
                            call xt_stripwhite (Memc[str])
                            if (Memc[str] == EOS) {
                                call sprintf (TITLE(sap), LEN_SAPTITLE,
                                    "(%.2h %.2h)")
                                    call pargd (ra)
                                    call pargd (dec)
                            } else {
                                call sprintf (TITLE(sap), LEN_SAPTITLE,
                                    "%s (%.2h %.2h)")
                                    call pargstr (Memc[str])
                                    call pargd (ra)
                                    call pargd (dec)
                            }
                        }
			DTYPE(sap) = INDEFI
			W1(sap) = INDEFD
			DW(sap) = INDEFD
			Z(sap) = INDEFD
			APLOW(sap) = INDEF
			APHIGH(sap) = INDEF
			call xt_stripwhite (TITLE(sap))
			naps = naps + 1
		    }	
		    call imcfnl (list)
		    call imunmap (im)

		# Read aperture information from a file.
		} else {
		    fd = open (Memc[str], READ_ONLY, TEXT_FILE)
		    while (fscan (fd) != EOF) {
			call gargi (ap)
			call gargi (beam)
			if (nscan() < 2)
			    next
			if (!IS_INDEFI(beam) && beam < 0 && verbose) {
			    call eprintf (
			    "Negative beam number for aperture %d ignored.\n") 
				call pargi (ap)
			    beam = INDEFI
			}
			if (naps == 0) {
			    nalloc = 50
			    call malloc (saps, nalloc, TY_POINTER)
			} else if (naps == nalloc) {
			    nalloc = nalloc + 50
			    call realloc (saps, nalloc, TY_POINTER)
			}
			call malloc (Memi[saps+naps], LEN_SAP, TY_STRUCT)

			sap = Memi[saps+naps]
			AP(sap) = ap
			BEAM(sap) = beam
			call gargi (DTYPE(sap))
			call gargd (W1(sap))
			call gargd (DW(sap))
			call gargd (Z(sap))
			call gargr (APLOW(sap))
			call gargr (APHIGH(sap))
			call gargstr (TITLE(sap), LEN_SAPTITLE)
			if (nscan() < 9) {
			    call reset_scan()
			    call gargi (AP(sap))
			    call gargi (BEAM(sap))
			    if (!IS_INDEFI(BEAM(sap)) && BEAM(sap) < 0)
				BEAM(sap) = INDEFI
			    call gargstr (TITLE(sap), LEN_SAPTITLE)
			    DTYPE(sap) = INDEFI
			    W1(sap) = INDEFD
			    DW(sap) = INDEFD
			    Z(sap) = INDEFD
			    APLOW(sap) = INDEF
			    APHIGH(sap) = INDEF
			}
			call xt_stripwhite (TITLE(sap))
			naps = naps + 1
		    }	
		    call close (fd)
		}
	    } then
		call erract (EA_WARN)
	}

	# Set remaining default parameters and the list terminator.
	call realloc (saps, naps+2, TY_INT)
	call malloc (Memi[saps+naps], LEN_SAP, TY_STRUCT)
	Memi[saps+naps+1] = NULL

	sap = Memi[saps+naps]
	AP(sap) = INDEFI
	BEAM(sap) = clgeti ("beam")
	if (!IS_INDEFI(BEAM(sap)) && BEAM(sap) < 0 && verbose) {
	    call eprintf (
		"Negative default beam number ignored.\n") 
	    BEAM(sap) = INDEFI
	}
	DTYPE(sap) = clgeti ("dtype")
	W1(sap) = clgetd ("w1")
	DW(sap) = clgetd ("dw")
	Z(sap) = clgetd ("z")
	APLOW(sap) = clgetr ("aplow")
	APHIGH(sap) = clgetr ("aphigh")
	call clgstr ("title", TITLE(sap), LEN_SAPTITLE)

	call sfree (sp)
end


procedure sap_fids (saps)

pointer	saps			# Pointer to array of aperture structures
pointer	sap

begin
	for (sap=saps; Memi[sap] != NULL; sap = sap + 1)
	    call mfree (Memi[sap], TY_STRUCT)
	call mfree (saps, TY_POINTER)
end