aboutsummaryrefslogtreecommitdiff
path: root/noao/rv/rvimutil.x
blob: 31b39c5e167b79fcb201fab4e400d71a94cd15e8 (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
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
include <imhdr.h>
include "rvpackage.h"
include "rvflags.h"
include "rvkeywords.h"

# RVIMUTIL.X  - Image utility routines for operations on image list pointers.
# Procedures to position pointers by reading next, previous, or random
# images. Also included are miscellaneous routines for getting image info
# from just a name.


# NEXT_SPEC - Get the next spectrum in the input file list.  Updates the
# image number in the struct and returns an error code if something went wrong.

int procedure next_spec (rv, infile, written)

pointer	rv					#I RV struct pointer
pointer infile					#I File list pointer
bool	written					#U Have results been written?

char	imname[SZ_FNAME]
#int	get_spec(), rv_verify_aps(), imtrgetim(), imtlen()
int	get_spec(), rv_apnum_range(), imtrgetim(), imtlen()
real	clgetr()

begin
        if (RV_IMNUM(rv)+1 <= imtlen(infile)) {
	    RV_IMNUM(rv) = RV_IMNUM(rv) + 1
	    #if (rv_verify_aps(rv,APPARAM(rv),APLIST(rv,1),NUMAPS(rv)) != OK)
	    if (rv_apnum_range(rv,APPARAM(rv)) != OK)
		return (ERR_READ)
	    if (imtrgetim(infile,RV_IMNUM(rv),imname,SZ_FNAME) != EOF) {
		if (get_spec(rv, imname, OBJECT_SPECTRUM) == ERR_READ)
		    return (ERR_READ)
	        written = false
	
		# Now update the data cache flags
		RV_FITDONE(rv) = NO
		RV_NEWXCOR(rv) = YES
		IS_DBLSTAR(rv) = NO
	 	RV_Y1(rv) = INDEF
		RV_Y2(rv) = INDEF
		RV_WINCENPAR(rv) = clgetr ("wincenter")
		call amovkr (0.0, COEFF(rv,1), 4)

	    } else 
	       call rv_errmsg ("Error reading next image name from list.")
        } else {
	    call rv_errmsg ("At end of input list.")
	    RV_NEWXCOR(rv) = NO
	}

	return (OK)
end


# PREV_SPEC - Get the previous spectrum in the input file list.  Updates the
# image number in the struct and returns an error code if something went wrong.

int procedure prev_spec (rv, infile, written)

pointer	rv					#I RV struct pointer
pointer infile					#I File list pointer
bool	written					#U Have results been written?

char	imname[SZ_FNAME]
int	get_spec(), imtrgetim(), rv_apnum_range()
real	clgetr()

begin
        if (RV_IMNUM(rv)-1 >= 1) {
	    RV_IMNUM(rv) = RV_IMNUM(rv) - 1
	    if (rv_apnum_range(rv,APPARAM(rv)) != OK)
		return (ERR_READ)
	    if (imtrgetim(infile,RV_IMNUM(rv),imname,SZ_FNAME) != EOF) {
		if (get_spec(rv, imname, OBJECT_SPECTRUM) == ERR_READ)
		    return (ERR_READ)
	        written = false

		# Now update the data cache flags
		RV_FITDONE(rv) = NO
		RV_NEWXCOR(rv) = YES
		IS_DBLSTAR(rv) = NO
	 	RV_Y1(rv) = INDEF
		RV_Y2(rv) = INDEF
		RV_WINCENPAR(rv) = clgetr ("wincenter")
		call amovkr (0.0, COEFF(rv,1), 4)

	    } else 
	       call rv_errmsg ("Error reading previous image from list.")
        } else {
	    call rv_errmsg ("At beginning of input list.")
	    RV_NEWXCOR(rv) = NO
	}

	return (OK)
end


# NEXT_TEMP - Get the next spectrum in the template file list.  Updates the
# template number in the struct and returns an error code if something went 
# wrong.

int procedure next_temp (rv, rinfile, written)

pointer	rv					#I RV struct pointer
pointer	rinfile					#I Template list pointer
bool	written					#U Have results been written?

int	imtrgetim(), get_spec(), rv_verify_aps()
real	clgetr()

begin
        if (RV_TEMPNUM(rv)+1 <= RV_NTEMPS(rv)) {
	   RV_TEMPNUM(rv) = RV_TEMPNUM(rv) + 1
	    if (rv_verify_aps(rv,APPARAM(rv),APLIST(rv,1),NUMAPS(rv)) != OK)
		return (ERR_READ)
	    if (imtrgetim(rinfile,RV_TEMPNUM(rv),RIMAGE(rv),SZ_FNAME) != EOF){
		if (get_spec(rv,RIMAGE(rv),REFER_SPECTRUM) == ERR_READ)
		    call error (0,"Error reading next template.")
		call rv_imtitle (RIMAGE(rv), TEMPNAME(rv), SZ_FNAME)
	        written = false
		RV_TEMPCODE(rv) = TEMPCODE(rv,RV_TEMPNUM(rv))
		call amovkr (0.0, COEFF(rv,1), 4)
		RV_FITDONE(rv) = NO
		RV_NEWXCOR(rv) = YES
		IS_DBLSTAR(rv) = NO
	 	RV_Y1(rv) = INDEF
		RV_Y2(rv) = INDEF
		RV_WINCENPAR(rv) = clgetr ("wincenter")

	   } else 
	       call rv_errmsg ("Error reading next image name from list.")
        } else { 
	    call rv_errmsg ("At end of template list.")
	    RV_NEWXCOR(rv) = NO
	}

	return (OK)
end


# PREV_TEMP - Get the previous spectrum in the template file list.  Updates the
# template number in the struct and returns an error code if something went 
# wrong.

int procedure prev_temp (rv, rinfile, written)

pointer	rv					#I RV struct pointer
pointer rinfile					#I File list pointer
bool	written					#U Have results been written?

int	imtrgetim(), get_spec(), rv_verify_aps()
real	clgetr()

begin
        if (RV_TEMPNUM(rv)-1 >= 1) {
	   RV_TEMPNUM(rv) = RV_TEMPNUM(rv) - 1
	   if (rv_verify_aps(rv,APPARAM(rv),APLIST(rv,1),NUMAPS(rv)) != OK)
		return (ERR_READ)
	   if (imtrgetim(rinfile,RV_TEMPNUM(rv),RIMAGE(rv),SZ_FNAME) != EOF){
		if (get_spec(rv,RIMAGE(rv),REFER_SPECTRUM) == ERR_READ)
		    call error (0,"Error reading next template.")
		call rv_imtitle (RIMAGE(rv), TEMPNAME(rv), SZ_FNAME)
	        written = false
		RV_TEMPCODE(rv) = TEMPCODE(rv,RV_TEMPNUM(rv))
		call amovkr (0.0, COEFF(rv,1), 4)
		RV_FITDONE(rv) = NO
	        RV_NEWXCOR(rv) = YES
		IS_DBLSTAR(rv) = NO
	 	RV_Y1(rv) = INDEF
		RV_Y2(rv) = INDEF
		RV_WINCENPAR(rv) = clgetr ("wincenter")
        
	   } else 
	       call rv_errmsg ("Error reading previous image from list.")
        } else {
	    call rv_errmsg ("At beginning of template list.")
	    RV_NEWXCOR(rv) = NO
	}

	return (OK)
end


# NEXT_AP - Get the next spectrum in the aperture data list.  Updates the
# aperture number in the struct and returns an error code if something went 
# wrong.  

int procedure next_ap (rv, written)

pointer	rv					#I RV struct pointer
bool	written					#I Have results been written?

int	apnum
int	rv_imdim(), rv_getim()
real	clgetr()
bool	silent

begin
	silent = false
	if (CURAPNUM(rv) == 0) {
	    apnum = 1
	    silent = TRUE
	} else
	    apnum = CURAPNUM(rv)

	if (apnum+1 > NUMAPS(rv)) {
	    if (!silent) {
                call rv_errmsg ("At end of object aperture list.")
		RV_NEWXCOR(rv) = NO
	    }
	    return (OK)
        } else {
	    # Get the next object aperture
	    CURAPNUM(rv) = CURAPNUM(rv) + 1
            RV_APNUM(rv) = APLIST(rv,CURAPNUM(rv))
            if (rv_getim(rv, IMAGE(rv), OBJECT_SPECTRUM, INDEF, INDEF, 
		INDEFI) == ERR_READ) {
		    RV_APNUM(rv) = APLIST(rv,apnum)
		    RV_OAPNUM(rv) = RV_APNUM(rv)
                    return (ERR_READ)
	    }
            written = false

            # Now update the data cache flags
	    call amovkr (0.0, COEFF(rv,1), 4)
	    RV_FITDONE(rv) = NO
	    RV_NEWXCOR(rv) = YES
	    IS_DBLSTAR(rv) = NO
	    RV_Y1(rv) = INDEF
	    RV_Y2(rv) = INDEF
	    RV_WINCENPAR(rv) = clgetr ("wincenter")
	}

	# Now try to get the next template aperture (may be one-dimensional)
	if (rv_imdim(RIMAGE(rv),2) > 1) {
	    if (apnum+1 > NUMAPS(rv)) {
	        call rv_errmsg ("At end of template aperture list.")
		RV_NEWXCOR(rv) = NO
	    } else {
               	if (rv_getim(rv,RIMAGE(rv), REFER_SPECTRUM, INDEF, INDEF,
		    INDEFI) == ERR_READ)
                        return (ERR_READ)
                written = false
	    }
	}

        return (OK)
end


# PREV_AP - Get the previous spectrum in the aperture data list.  Updates the
# aperture number in the struct and returns an error code if something went 
# wrong.  Since all of the aperture have been read from disk, just decrement
# the aperture number and pull the data from the BIN cache.

int procedure prev_ap (rv, written)

pointer	rv					#I RV struct pointer
bool	written					#I Have results been written?

int	apnum
int	rv_imdim(), rv_getim()
real	clgetr()
bool	silent

begin
	silent = false
	if (apnum == (NUMAPS(rv) + 1)) {
	    apnum = NUMAPS(rv)
	    silent = TRUE
	} else
	    apnum = CURAPNUM(rv)

        if (apnum-1 < 1) {
	    if (!silent) {
                call rv_errmsg ("At beginning of object aperture list.")
		RV_NEWXCOR(rv) = NO
	    }
	    return (OK)
        } else {
	    # Get the next object aperture
	    CURAPNUM(rv) = CURAPNUM(rv) - 1
            RV_APNUM(rv) = APLIST(rv,CURAPNUM(rv))
            if (rv_getim(rv, IMAGE(rv), OBJECT_SPECTRUM, INDEF, INDEF,
		INDEFI) == ERR_READ) {
		    RV_APNUM(rv) = APLIST(rv,apnum)
		    RV_OAPNUM(rv) = RV_APNUM(rv)
                    return (ERR_READ)
	    }
            written = false

            # Now update the data cache flags
	    call amovkr (0.0, COEFF(rv,1), 4)
	    RV_FITDONE(rv) = NO
	    RV_NEWXCOR(rv) = YES
	    IS_DBLSTAR(rv) = NO
	    RV_Y1(rv) = INDEF
	    RV_Y2(rv) = INDEF
	    RV_WINCENPAR(rv) = clgetr ("wincenter")
	}

	# Now try to get the next previous aperture (may be one-dimensional)
	if (rv_imdim(RIMAGE(rv),2) > 1) {
            if (rv_getim(rv, RIMAGE(rv), REFER_SPECTRUM, INDEF, INDEF,
		INDEFI) == ERR_READ)
                    return (ERR_READ)
            written = false
	}

	return (OK)
end


# GET_SPEC - Low level routine to do the common drudge work of reading the
# spectrum, plotting the new spec and displaying mask.

int procedure get_spec (rv, imname, spec_type)

pointer	rv					#I RV struct pointer
char	imname[SZ_FNAME]			#I Image name to read
int	spec_type				#I Type of spectrum to read

int	rv_getim()

begin
	# Try to read the data from the image
   	if (rv_getim(rv, imname, spec_type, INDEF, INDEF, INDEFI) == ERR_READ) {
	    RV_NEWXCOR(rv) = NO
	    return (ERR_READ)
	}
	RV_NEWXCOR(rv) = YES
	return (OK)
end


# CONSTRUCT_FILE_NAMES - Construct the log file names from a root

procedure cons_file_names (root, log, meta, verb, maxch)

char	root[maxch]				#I Root file name
char	log[maxch]				#O Text log file name
char	meta[maxch]				#O Metacode file name
char	verb[maxch]				#O Verbose file name
int	maxch					#I Max chars

begin
	call sprintf (log, maxch, "%s.txt")
	    call pargstr (root)
	call sprintf (meta, maxch, "%s.gki")
	    call pargstr (root)
	call sprintf (verb, maxch, "%s.log")
	    call pargstr (root)
end


# RV_IMDIM - Utility to get simply a dimensionality of an image name.

int procedure rv_imdim (image, dim)

char	image[SZ_FNAME]				#I Image name
int	dim					#I Which dimension to get

int	dim_len
pointer	im, sp, bp, immap()
errchk	immap

begin
	call smark (sp)
	call salloc (bp, SZ_FNAME, TY_CHAR)

	iferr (im = immap(image, READ_ONLY, 0)) {
	    call sprintf (Memc[bp], SZ_FNAME,
		 "rv_imdim: Error opening image `%s'.")
		    call pargstr (image)
	    call error (0, Memc[bp])
	}

	if (dim == 1) {
	    dim_len = IM_LEN(im,1)
	} else if (dim == 2) {
	    if (IM_NDIM(im) == 1 || (IM_NDIM(im) == 2 && IM_LEN(im,2) == 1))
		dim_len = 1
	    else
	        dim_len = IM_LEN(im,2)
	} else
	    dim_len = IM_LEN(im,dim)

	call imunmap (im)
	call sfree (sp)
	return (dim_len)
end


# RV_IMTITLE - Procedure to get the IM_TITLE string given a file name

procedure rv_imtitle (image, title, maxchar)

char	image[SZ_FNAME]				#I Image name
char	title[SZ_FNAME]				#O Title
int	maxchar					#I Max chars

pointer	im, sp, bp,  immap()
errchk	immap

begin
	call smark (sp)
	call salloc (bp, SZ_LINE, TY_CHAR)

	iferr (im = immap(image, READ_ONLY, 0)) {
	    call sprintf (Memc[bp], SZ_FNAME,
		 "rv_imtitle: Error opening image `%s'.")
		    call pargstr (image)
	    call error (0, Memc[bp])
	}
        call rv_fill_blanks (IM_TITLE(im), title, maxchar)

	call imunmap (im)
	call sfree (sp)
end


# RV_IMTEMPVEL - Procedure to get the VHELIO string given a file name.

real procedure rv_imtempvel (rv, image)

pointer	rv					#I RV struct pointer
char	image[SZ_FNAME]				#I Image name

pointer	im, sp, bp,  immap()
real	tvel, imgetr()
int	imaccf()
errchk	immap, imaccf, imgetr

begin
	call smark (sp)
	call salloc (bp, SZ_FNAME, TY_CHAR)

	iferr (im = immap(image, READ_ONLY, 0)) {
	    call sprintf (Memc[bp], SZ_FNAME,
		 "rv_imtempvel: Error opening image `%s'.")
		    call pargstr (image)
	    call error (0, Memc[bp])
	}

	# Get the velocity from the reference star image header.  Save the
	# warning for outputting results.
	if (imaccf(im, KW_VHELIO(rv)) == YES)
	    tvel = imgetr (im, KW_VHELIO(rv))
	else
	    tvel = INDEFR

	call imunmap (im)
	call sfree (sp)
	return (tvel)
end