File: t_velshift.x

package info (click to toggle)
iraf-rvsao 2.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 16,456 kB
  • sloc: ansic: 963; lisp: 651; fortran: 397; makefile: 27
file content (568 lines) | stat: -rw-r--r-- 15,928 bytes parent folder | download
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File Util/t_velset.x
# Modified by Doug Mink from stsdas.playpen.newredshift
# October 1, 1997

include	<imhdr.h>
include	<error.h>

# VELSET -- Changes the redshift of spectra.
#
# The input spectra are given by an image template list. The output is either 
# a matching list of spectra or a directory. The number of input spectra may 
# be either one or match the number of output spectra. Image sections are 
# ignored, since the user wants a exact copy of the input, however with the
# wavelength scale modified.
#
#							Ivo Busko  7/17/89

procedure t_velset()

char	imtlist1[SZ_LINE]		# Input spectra list
char	imtlist2[SZ_LINE]		# Output spect. list/directory
double	newvel				# New redshift
bool	verbose				# Print operations?
bool	velz				# New redshift in Z (yes) or km/sec

char	image1[SZ_PATHNAME]		# Input image name
char	image2[SZ_PATHNAME]		# Output image name
char	dirname1[SZ_PATHNAME]		# Directory name
char	dirname2[SZ_PATHNAME]		# Directory name

int	list1, list2, root_len
double	c0

int	imtopen(), imtgetim(), imtlen()
int	fnldir(), isdirectory()
bool	clgetb()
double	clgetd()

begin
	c0 = 299792.5d0

	# Get task parameters
	call clgstr ("input", imtlist1, SZ_LINE)
	velz	= clgetb ("velz")
	newvel  = clgetd ("newvel")
	if (velz)
	    newvel = c0 * (newvel - 1.d0)
	call clgstr ("output", imtlist2, SZ_LINE)
	verbose = clgetb ("verbose")

	# If the output string is a directory, generate names for
	# the new images accordingly.

	if (isdirectory (imtlist2, dirname2, SZ_PATHNAME) > 0) {
	    list1 = imtopen (imtlist1)
	    while (imtgetim (list1, image1, SZ_PATHNAME) != EOF) {

		# Strip an eventual image section first.  Place the input 
		# image name, without a directory or image section, in string 
		# dirname1.
		call imgimage (image1, image2, SZ_PATHNAME)
		root_len = fnldir (image2, dirname1, SZ_PATHNAME)
		call strcpy (image2[root_len + 1], dirname1, SZ_PATHNAME)

		# Assemble output image name. Strip again image section from
		# input image name.
		call strcpy (dirname2, image2, SZ_PATHNAME)
		call strcat (dirname1, image2, SZ_PATHNAME)
		call imgimage (image1, image1, SZ_PATHNAME)
		call printf ("VELSET: %s -> %s\n")
		    call pargstr (image1)
		    call pargstr (image2)
		call flush (STDOUT)

		# Do it.
		call vels_image (image1, image2, velz, newvel, verbose)
	    }
	    call imtclose (list1)

	} else {

	    # Expand the input and output image lists.
	    list1 = imtopen (imtlist1)
	    list2 = imtopen (imtlist2)
	    if (imtlen (list1) != imtlen (list2)) {
	        call imtclose (list1)
	        call imtclose (list2)
	        call error (0, "Number of input and output images not the same")
	    }

	    # Do each set of input/output images. First strip any sections.
	    while ((imtgetim (list1, image1, SZ_PATHNAME) != EOF) &&
		(imtgetim (list2, image2, SZ_PATHNAME) != EOF)) {
		call imgimage (image1, image1, SZ_PATHNAME)
		call imgimage (image2, image2, SZ_PATHNAME)
		call printf ("VELSET: %s -> %s\n")
		    call pargstr (image1)
		    call pargstr (image2)
		call flush (STDOUT)
		call vels_image (image1, image2, velz, newvel, verbose)
	    }

	    call imtclose (list1)
	    call imtclose (list2)
	}
end

# VELS_IMAGE -- Copy a spectrum, changing the wavelength scale accordingly
# to the value of newz relative to the VELOCITY parameter. 
#
# If the input and output image names are equal, just 
# issue a warning message. Keywords that describe the wavelength axis are 
# looked for in the header, in the following order: first, ONEDSPEC keywords 
# W0 and WPC; second, CD keywords CRVALn and CDn_1, and at last FITS keywords 
# CRVALn and CDELTn. Here n is the value of parameter AXIS, unless the ONEDSPEC 
# header keyword DISPAXIS is found, in which case it takes precedence over the 
# parameter. Logarithmic wavelength scale is treated correctly either if the 
# LOG task parameter is set to yes or if the ONEDSPEC header keyword DC-FLAG is
# found in the header with value 1. HISTORY records are appended to the 
# header.

procedure vels_image (image1, image2, velz, newvel, verbose)

char	image1[ARB]	# Input spectrum
char	image2[ARB]	# Output spectrum
bool	velz		# Velocity in Z (yes) or km/sec (no)
double	newvel		# New redshift in km/sec
bool	verbose		# Print the operation

pointer	im, sp, str
int	dispaxis, dcflag
double	corre, crval2, cdelt2, crval1, cdelt1, c0, oldvel, oldz, newz
double	hcv
double	imgetd()

pointer immap()
int	imgeti()
int	imaccf()
bool	streq()

begin
	if (streq (image1, image2))
	    call error (0, "Same input and output images.")

	# Check keywords on input image header
	im = immap (image1, READ_ONLY, 0)

	# First check which one is the dispersion axis.
	if (imaccf (im, "DISPAXIS") == YES)
	    dispaxis = imgeti (im, "DISPAXIS")
	else {
	    call printf ("VELSIMAGE: No DISPAXIS for %s\n")
		call pargstr (image1)
            call imunmap (im)
	    return
	    }

	# Next check image dimensionality
	if (dispaxis < 1 || dispaxis > IM_NDIM(im)) {
	    call eprintf ("VELSIMAGE: non-existent axis %d for %s.\n")
		call pargi (dispaxis)
	        call pargstr (image1)
	    call imunmap (im)
	    return
	    }

	# Check if wavelength scale is linear or logarithmic.
	# If no indication is found on the image, assume linear.
	if (imaccf (im, "DC-FLAG") == YES)
	    dcflag = imgeti (im, "DC-FLAG")
	else
	    dcflag = 0
	if (dcflag == -1) {
	    call eprintf ("%s : not calibrated to wavelength.\n")
	        call pargstr (image1)
	    call imunmap (im)
	    return
	    }
	else if (dcflag == 0) {
	    call eprintf ("%s : not log wavelength.\n")
	        call pargstr (image1)
	    call imunmap (im)
	    return
	    }

	# Convert velocity from km/sec to Z = 1 + v/C
	c0 = 299792.5d0
	if (imaccf (im, "VELOCITY") == YES) {
	    oldvel = imgetd (im, "VELOCITY")
	    if (imaccf (im,"BCV") == YES)
		hcv = imgetd (im, "BCV")
	    else if (imaccf (im,"HCV") == YES)
		hcv = imgetd (im, "HCV")
	    else
		hcv = 0.d0
	    oldz = (oldvel - hcv) / c0
	    if (verbose) {
		call printf ("VELSET: Old velocity is %f + %f = %f/n")
		    call pargd (oldvel)
		    call pargd (hcv)
		    call pargd (oldz)
		}
	    }

	call imunmap (im)

	# Create and open the new image.
#	call imcopy (image1, image2)
	call vels_imcopy (image1, image2)
	im = immap (image2, READ_WRITE, 28800)

	if (imaccf (im,"VELOCITY") != YES) {
	    call imaddd (im, "VELOCITY", newvel)
	    call imunmap (im)
	    return
	    }

	# Update keywords with new values corrected by the redshift
	# difference between oldz and newz.
	
	newz = newvel / c0
	corre = (1.d0 + newz) / (1.d0 + oldz)

	call vels_get (im, dispaxis, crval1, cdelt1)

	if (dcflag == 1) {
	    crval2 = crval1 + log10 (corre)
	    cdelt2 = cdelt1
	    }
	else {
	    crval2 = crval1 * corre
	    cdelt2 = cdelt1 * corre
	    }

	call vels_put (im, dispaxis, crval2, cdelt2)

	# If verbose print the operation.
	if (verbose) {
	    call eprintf ("%s %32t->%40t %s\n")
	        call pargstr (image1)
	        call pargstr (image2)
	    call eprintf (" origin = %.8g %40t  origin = %.8g\n")
	        call pargd (crval1)
	        call pargd (crval2)
	    call eprintf (" step   = %.8g %40t  step   = %.8g\n")
	        call pargd (cdelt1)
	        call pargd (cdelt2)
	    }

	# Update velocity in image header
	call imputd (im, "VELOCITY", newvel)
	if (imaccf (im,"BCV") == YES)
	    call imputd (im, "BCV", 0.d0)
	if (imaccf (im,"HCV") == YES)
	    call imputd (im, "HCV", 0.d0)

	# Update image HISTORY.
	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)
	if (velz) {
	    call sprintf (Memc[str], SZ_LINE, " VELSET: oldz = %.8f, newz = %.8f ")
		call pargd (oldz)
		call pargd (newz)
	    }
	else {
	    call sprintf (Memc[str], SZ_LINE, " VELSET: old vel = %.3f, new vel = %.3f ")
		call pargd (oldvel)
		call pargd (newvel)
	    }
	call vels_timelog (Memc[str], SZ_LINE)
	call imputh (im, "HISTORY", Memc[str])
	call sprintf (Memc[str], SZ_LINE, "input file to VELSET: %s ")
	    call pargstr (image1)
	call imputh (im, "HISTORY", Memc[str])
	call sfree (sp)

	call imunmap (im)
end

# VELS_GET -- Get header parameters which describe the wavelength axis.

procedure vels_get (im, axis, crval, cdelt)

pointer	im			#i: image pointer
int	axis			#i: dispersion axis
double	crval			#o: reference wavelength
double	cdelt			#o: wavelength step

char	keyword[SZ_FNAME]
double	imgetd()
int	imaccf()

begin
	# FITS or IRAF keyword
	# Wavelength scale zero is common for ST and FITS formats.
	call sprintf (keyword, SZ_FNAME, "CRVAL%1d")
	    call pargi (axis)
	if (imaccf (im, keyword) == YES)
	    crval = imgetd (im, keyword)

	# STSDAS keyword
	call sprintf (keyword, SZ_FNAME, "CD%1d_1")
	    call pargi (axis)
	if (imaccf (im, keyword) == YES)
	    cdelt = imgetd (im, keyword)

	# FITS keyword
	call sprintf (keyword, SZ_FNAME, "CDELT%1d")
	    call pargi (axis)
	if (imaccf (im, keyword) == YES)
	    cdelt = imgetd (im, keyword)

	# Old IRAF keywords
	if (imaccf (im, "W0") == YES) {
	    crval = imgetd (im, "W0")
	    cdelt = imgetd (im, "WPC")
	    }
end

# VELS_PUT -- Put header parameters which describe the wavelength axis.

procedure vels_put (im, axis, crval, cdelt)

pointer	im			#i: image pointer
int	axis			#i: dispersion axis
double	crval			#i: reference wavelength
double	cdelt			#i: wavelength step

int	imaccf()
char	keyword[SZ_FNAME]

begin
	# FITS or IRAF keyword
	# Wavelength scale zero is common for ST and FITS formats.
	call sprintf (keyword, SZ_FNAME, "CRVAL%1d")
	    call pargi (axis)
	if (imaccf (im, keyword) == YES)
	    call imputd (im, keyword, crval)

	# STSDAS keyword
	call sprintf (keyword, SZ_FNAME, "CD%1d_1")
	    call pargi (axis)
	if (imaccf (im, keyword) == YES)
	    call imputd (im, keyword, cdelt)

	# FITS keyword
	call sprintf (keyword, SZ_FNAME, "CDELT%1d")
	    call pargi (axis)
	if (imaccf (im, keyword) == YES)
	    call imputd (im, keyword, cdelt)

	# Old IRAF keywords
	if (imaccf (im, "W0") == YES) {
	    call imputd (im, "W0", crval)
	    call imputd (im, "WPC", cdelt)
	    }
end

# VELS_TIMELOG -- Prepend a time stamp to the given string.
#
# For the purpose of a history logging prepend a short time stamp to the
# given string.  Note that the input string is modified.

procedure vels_timelog (str, max_char)

char	str[ARB]		# String to be time stamped
int	max_char		# Maximum characters in string

pointer	sp, time, temp

begin
	call smark (sp)
	call salloc (time, SZ_LINE, TY_CHAR)
	call salloc (temp, max_char, TY_CHAR)

	call logtime (Memc[time], SZ_LINE)
	call sprintf (Memc[temp], max_char, "%s %s")
	    call pargstr (Memc[time])
	    call pargstr (str)
	call strcpy (Memc[temp], str, max_char)
	call sfree (sp)
end


# VELS_IMCOPY -- Copy an image.  Use sequential routines to permit copying
# images of any dimension.  Perform pixel i/o in the datatype of the image,
# to avoid unnecessary type conversion.
#
# This routine is basicaly task images.imcopy whith verbose option and
# output image section handling removed.

procedure vels_imcopy (image1, image2)

char	image1[ARB]			# Input image
char	image2[ARB]			# Output image

int	npix, junk
pointer	buf1, buf2, im1, im2
pointer	sp, imtemp
long	v1[IM_MAXDIM], v2[IM_MAXDIM]

int	imgnls(), imgnll(), imgnlr(), imgnld(), imgnlx()
int	impnls(), impnll(), impnlr(), impnld(), impnlx()
pointer	immap()

begin
	call smark (sp)
	call salloc (imtemp, SZ_PATHNAME, TY_CHAR)

	# Map the input image.
	im1 = immap (image1, READ_ONLY, 0)

	# Get a temporary output image name and map it as a copy of the 
	# input image.
	# Copy the input image to the temporary output image and unmap
	# the images.  Release the temporary image name.

	call xt_mkimtemp (image1, image2, Memc[imtemp], SZ_PATHNAME)
	im2 = immap (image2, NEW_COPY, im1)

	# Setup start vector for sequential reads and writes.

	call amovkl (long(1), v1, IM_MAXDIM)
	call amovkl (long(1), v2, IM_MAXDIM)

	# Copy the image.

	npix = IM_LEN(im1, 1)
	switch (IM_PIXTYPE(im1)) {
	case TY_SHORT:
	    while (imgnls (im1, buf1, v1) != EOF) {
		junk = impnls (im2, buf2, v2)
		call amovs (Mems[buf1], Mems[buf2], npix)
	    }
	case TY_USHORT, TY_INT, TY_LONG:
	    while (imgnll (im1, buf1, v1) != EOF) {
		junk = impnll (im2, buf2, v2)
		call amovl (Meml[buf1], Meml[buf2], npix)
	    }
	case TY_REAL:
	    while (imgnlr (im1, buf1, v1) != EOF) {
		junk = impnlr (im2, buf2, v2)
		call amovr (Memr[buf1], Memr[buf2], npix)
	    }
	case TY_DOUBLE:
	    while (imgnld (im1, buf1, v1) != EOF) {
		junk = impnld (im2, buf2, v2)
		call amovd (Memd[buf1], Memd[buf2], npix)
	    }
	case TY_COMPLEX:
	    while (imgnlx (im1, buf1, v1) != EOF) {
	        junk = impnlx (im2, buf2, v2)
		call amovx (Memx[buf1], Memx[buf2], npix)
	    }
	default:
	    call error (1, "unknown pixel datatype")
	}

	# Unmap the images.

	call imunmap (im2)
	call imunmap (im1)
	call xt_delimtemp (image2, Memc[imtemp])
	call sfree (sp)
end


# VELS_CONVERT -- Copy an image, translating in wavelength.  Use sequential
# routines to permit copying images of any dimension.  Perform pixel i/o in
# the datatype of the image, to avoid unnecessary type conversion.

# This routine is basicaly task images.imcopy with verbose option and
# output image section handling removed.

procedure vels_convert (image1, image2, inwl1, indwl, outwl1, outdwl)

char	image1[ARB]	# Input image
char	image2[ARB]	# Output image
double	inwl1		# Input starting wavelength
double	indwl		# Input delta wavelength
double	outwl1		# Output starting wavelength
double	outdwl		# Output delta wavelength

int	npix, junk
pointer	buf1, buf2, im1, im2
pointer	sp, imtemp, tbuf
long	v1[IM_MAXDIM], v2[IM_MAXDIM]

int	imgnls(), imgnll(), imgnlr(), imgnld(), imgnlx()
int	impnls(), impnll(), impnlr(), impnld(), impnlx()
pointer	immap()

begin
	call smark (sp)
	call salloc (imtemp, SZ_PATHNAME, TY_CHAR)

	# Map the input image.
	im1 = immap (image1, READ_ONLY, 0)

	# Get a temporary output image name and map it as a copy of the 
	# input image.
	# Copy the input image to the temporary output image and unmap
	# the images.  Release the temporary image name.

	call xt_mkimtemp (image1, image2, Memc[imtemp], SZ_PATHNAME)
	im2 = immap (image2, NEW_COPY, im1)

	# Setup start vector for sequential reads and writes.

	call amovkl (long(1), v1, IM_MAXDIM)
	call amovkl (long(1), v2, IM_MAXDIM)

	# Copy the image.

	npix = IM_LEN(im1, 1)
	switch (IM_PIXTYPE(im1)) {
	case TY_SHORT:
	    call salloc (tbuf, TY_SHORT, npix)
	    while (imgnls (im1, buf1, v1) != EOF) {
		junk = impnls (im2, buf2, v2)
		call amovs (Mems[buf1], Mems[buf2], npix)
	    }
	case TY_USHORT, TY_INT, TY_LONG:
	    call salloc (tbuf, TY_LONG, npix)
	    while (imgnll (im1, buf1, v1) != EOF) {
		junk = impnll (im2, buf2, v2)
		call amovl (Meml[buf1], Meml[buf2], npix)
	    }
	case TY_REAL:
	    call salloc (tbuf, TY_REAL, npix)
	    while (imgnlr (im1, buf1, v1) != EOF) {
		junk = impnlr (im2, buf2, v2)
		call amovr (Memr[buf1], Memr[buf2], npix)
	    }
	case TY_DOUBLE:
	    call salloc (tbuf, TY_DOUBLE, npix)
	    while (imgnld (im1, buf1, v1) != EOF) {
		junk = impnld (im2, buf2, v2)
		call amovd (Memd[buf1], Memd[buf2], npix)
	    }
	case TY_COMPLEX:
	    while (imgnlx (im1, buf1, v1) != EOF) {
	        junk = impnlx (im2, buf2, v2)
		call amovx (Memx[buf1], Memx[buf2], npix)
	    }
	default:
	    call error (1, "unknown pixel datatype")
	}

	# Unmap the images.

	call imunmap (im2)
	call imunmap (im1)
	call xt_delimtemp (image2, Memc[imtemp])
	call sfree (sp)
end
# Apr 15 1994	Modify STSDAS playpen.newredshift
# Apr 15 1994	Change multiple types of WCS parameters at once
# Apr 15 1994	Change parameters to use only header velocity and axis
# Apr 15 1994	Modify STSDAS playpen.newredshift
# Apr 15 1994	Change multiple types of WCS parameters at once
# Apr 15 1994	Change parameters to use only header velocity and axis
# Apr 20 1994	Fix IMACCF calls
# Apr 22 1994	Fix sprintf str

# Mar 28 1995	Free all stack pointers

# OCt  1 1997	Always print 4-digit date in time stamp