File: t_velset.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 (286 lines) | stat: -rw-r--r-- 8,313 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
# File Util/t_velset.x
# Modified by Doug Mink from stsdas.playpen.newredshift
# July 13, 1994

include	<imhdr.h>
include	<error.h>
include <time.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				# Velocity or shift
bool	verbose				# Print operations?
bool	velz				# New redshift in Z (yes) or km/sec
bool	shift				# Velocity shift (yes) or velocity (no)

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)
	shift = clgetb ("shift")
	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 ("VELSHIFT: %s -> %s\n")
		    call pargstr (image1)
		    call pargstr (image2)
		call flush (STDOUT)

		# Do it.
		call shiftimage (image1, image2, velz, shift, 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 ("VELSHIFT: %s -> %s\n")
		    call pargstr (image1)
		    call pargstr (image2)
		call flush (STDOUT)
		call shiftimage (image1, image2, velz, shift, newvel, verbose)
	    }

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

# SHIFTIMAGE -- 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 shiftimage (image1, image2, velz, shift, velocity, verbose)

char	image1[ARB]	# Input spectrum
char	image2[ARB]	# Output spectrum
bool	velz		# Velocity in Z (yes) or km/sec (no)
bool	shift		# Velocity shift (yes) or velocity (no)
double	velocity	# Velocity or shift 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,newvel
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 ("VELSHIFT: 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.
	
	if (shift)
	    newvel = oldvel + velocity
	else
	    newvel = velocity
	newz = (newvel - hcv) / 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, " VELSHIFT: oldz = %.8f, newz = %.8f ")
		call pargd (oldz)
		call pargd (newz)
	    }
	else {
	    call sprintf (Memc[str], SZ_LINE, " VELSHIFT: 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 VELSHIFT: %s ")
	    call pargstr (image1)
	call imputh (im, "HISTORY", Memc[str])
	call sfree (sp)

	call imunmap (im)
end