aboutsummaryrefslogtreecommitdiff
path: root/noao/rv/rvcursor.x
blob: 844c72e13ea80a70f58267938cab3c7707c2b0d4 (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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
include <gset.h>
include <pkg/gtools.h>
include <fset.h>
include "rvpackage.h"
include "rvflags.h"
include "rvcont.h"
include "rvplots.h"
include "rvfilter.h"

# RV_CURSOR - Get the next command from the user in the input cursor loop
# and perform the requested function.

procedure rv_cursor (rv, infile, rinfile)

pointer	rv				#I RV struct pointer
pointer	infile				#I Object input list pointer
pointer	rinfile				#I Template input list pointer

pointer	sp, cmd, buf, fname
pointer	gp, gt
int	wcs, key, nans, i
int	stat, npts, tmp_int
real	x, y, x1, x2, dx, max
char	ans[2*SZ_LINE,4]

pointer open(), gopen()
int 	rv_colon(), rv_data_check(), scan()
int	clgcur(), fstati(), fft_cursor(), spc_cursor()
int	next_spec(), next_temp(), next_ap()
int	prev_spec(), prev_temp(), prev_ap()
real	rv_maxpix()
bool	written, streq()
errchk	open, gt_init

define	LOG_PROMPT		"Log of Current Results: "
define	exit_			99
define	unknown_		98
define	redo_xcor_		97
define	replot_			96
define	refit_			95

begin
	call smark (sp)				# Allocate some space
	call salloc (cmd, SZ_LINE, TY_CHAR)
	call salloc (buf, SZ_LINE, TY_CHAR)
	call salloc (fname, SZ_FNAME, TY_CHAR)

	# Nab some pointers
	gp = RV_GP(rv)
	gt = RV_GT(rv)

	call op_debug (rv)			# Open debugger
	if (DBG_KEYSTROKE(rv) != 'x')
	    key = DBG_KEYSTROKE(rv)
	else
	    key = 'x'
	RV_NEWGRAPH(rv) = NO
	written = false
	RV_NEWXCOR(rv) = YES

	repeat {

	    RV_CMD(rv) = key
	    switch (key) { 			# Switch on the keystroke
	    case '?':				
	    	# List options
		call gpagefile (gp, XC_HELP, "FXCOR Task Options: ")

	    case ':':				
	    	# Colon command
	    	if (rv_colon(rv,Memc[cmd],written,infile,rinfile,NULL) == QUIT)
	    	    break
		if (RV_NEWXCOR(rv) == YES)
		    goto redo_xcor_
		if (RV_NEWGRAPH(rv) == YES)
		    goto replot_
		if (RV_FITDONE(rv) == NO) {
		    RV_ERRCODE(rv) = OK
		    call rv_batch_xcor (rv, RV_TEMPNUM(rv), RV_APNUM(rv), NO, 
			NO, YES)
	 	}

	    case '.':
		# Accelerator for doing continuum normalizations.
		call cmd_cont (rv)
		if (RV_NEWXCOR(rv) == YES) {
		    key = 'x'
		    goto redo_xcor_
 		} else
		    goto replot_

	    case '-':
		# Subtract a blend from the CCF.
		if (RV_FITDONE(rv) == YES) {
	            x1 = WRKPIXX(rv,1)
		    x2 = WRKPIXX(rv,RV_CCFNPTS(rv))
		    dx = 1.
		    call gctran (gp, x, y, x, y, wcs, 2)
		    call subblend (rv, gp, WRKPIXY(rv,1), x1, x2, dx, x, y)
		    goto replot_
		} else {
		    call rv_errmsg (
			"No deblended fit done yet.  Use 'd' to fit.")
		}

	    case '+':
		# Toggle status line output for ccf mode.
		RV_STATLINE(rv) = RV_STATLINE(rv) + 1
		call rv_writeln (rv, STDOUT)

	    case ',':
		# Generic Test Command (hidden from users).

	    case 'a':
		# Do a summary plot of the antisymmetric noise of the CCF.
		if (IS_DBLSTAR(rv) == YES) {
		    call rv_errmsg (
			"Antisymmetric plot invalid for a deblended fit.")
		} else if (RV_FITFUNC(rv) == CENTER1D) {
	    	    call rv_errmsg (
			"Antisymmetric plot unavailable for center1d fit.")
		} else if (RV_FITFUNC(rv) == SINC &&
		    IS_INDEF(RV_BACKGROUND(rv))) {
	    	       call rv_errmsg (
		       "Must set background for sinc fit antisym. computation.")
		} else {
		    call rv_anplot (rv, RV_GP(rv))
		    call rv_pause ("Hit any key to return to the correlation.")
		    goto replot_
		}

	    case 'b':
		# Set "background" for FWHM calculation.
		i = RV_WINCENTER(rv) - RV_WINDOW(rv)
		max = rv_maxpix (WRKPIXY(rv,i), 2*RV_WINDOW(rv))
		call gctran (gp, x, y, x, y, wcs, 2)
		if (y >= max) {
		    call rv_errmsg ("Cannot set background above CCF peak.")
	 	} else {
		    if (RV_FITFUNC(rv) == GAUSSIAN || 
			RV_FITFUNC(rv) == LORENTZIAN ||
			RV_FITFUNC(rv) == SINC) {
		            call reset_errcom (rv)
		            call rv_erase_fit (rv, false)
	                    RV_BACKGROUND(rv) = y
		            RV_ERRCODE(rv) = OK
			    IS_DBLSTAR(rv) = NO
		            call rv_batch_xcor (rv, RV_TEMPNUM(rv), 
				RV_APNUM(rv), NO, NO, NO)
				#RV_APNUM(rv), NO, NO, YES)
		    } else {
			call rv_errmsg ("Cannot set background for a %s fit.")
			if (IS_DBLSTAR(rv) == YES)
			    call pargstr ("deblended")
			else if (RV_FITFUNC(rv) == PARABOLA)
			    call pargstr ("parabolic")
			else
			    call pargstr ("center1d")
		    }
		}

	    case 'c':				
		# Read cursor poistion
		call rv_rdcursor (rv, gp, x, y, wcs)

	    case 'd':				
		# Fit the peak with up to four Gaussians.
		call reset_errcom (rv)
	        x1 = WRKPIXX(rv,1)
		x2 = WRKPIXX(rv,RV_CCFNPTS(rv))
		dx = 1.
		call gctran (gp, x, y, x, y, wcs, 2)
		call deblend (rv, gp, x1, x2, dx, x, y, WRKPIXY(rv,1), ans, 
		    nans)

	    case 'e':
		# Show the summary plot after the fit.
	        call rv_eplot (rv, gp)
		call rv_pause ("Hit any key to return to the correlation.")
		goto replot_

	    case 'f':
		# Enter the Fourier Mode command stream.
		stat = fft_cursor(rv)
		if (RV_NEWXCOR(rv) == YES) {
		    key = 'x'
		    goto redo_xcor_
		} else
		    goto replot_

	    case 'g':
	    	# Fit the correlation plot based on the X cursor values
		call reset_errcom (rv)
		call gctran (gp, x, y, x, y, wcs, 2)
		call rv_xfit (rv, x, YES)

	    case 'I':
		# Fatal interrupt.
	        call fatal (0, "Interrupt")

	    case 'j':
		# Residual plot of fit.
		call rv_plot (rv, RESIDUAL_PLOT)

	    case 'l':
	    	# Page the logfile. Pointer is closed to be VMS compatable.
	    	if (RV_TXFD(rv) != NULL) {
		    if (fstati(RV_TXFD(rv),F_FILESIZE) == 0) 
			call rv_errmsg ("Nothing yet written to logfile.")
		    else {
		        call sprintf (Memc[buf], SZ_FNAME, "%s.txt")
			    call pargstr (SPOOL(rv))
		        call flush (RV_TXFD(rv))
		        call close (RV_TXFD(rv))
		        call gpagefile (gp, Memc[buf], LOG_PROMPT)
		        RV_TXFD(rv) = open (Memc[buf], APPEND, TEXT_FILE)
		    }
	    	} else
		    call rv_errmsg ("No output file specified.")

	    case 'm':				
		# Plot polymarkers of the actual CCF points.
		call gseti (gp, G_WCS, 2)
		i = RV_WINCENTER(rv) - RV_WINDOW(rv)
		npts = 2 * RV_WINDOW(rv)
		call gpmark (gp,WRKPIXX(rv,i), WRKPIXY(rv,i), npts, 4, 2.0, 2.0)
		call gflush (gp)

	    case 'n':
		# Go to the next (template --> aperture --> object) after 
		# saving results.
		if (RV_TEMPNUM(rv) < RV_NTEMPS(rv) ||
		    CURAPNUM(rv) < NUMAPS(rv) ||
		    RV_IMNUM(rv) < RV_NOBJS(rv))
		    	call rv_do_save (rv, written)

		# Now do the "next" operation as specified. The logic sequence
		# is complicated but handles all cases.
		if (RV_TEMPNUM(rv) < RV_NTEMPS(rv)) {
		    if (next_temp(rv, rinfile, written) == ERR_READ)
			call rv_errmsg ("Error reading next template.")
		    else
			goto redo_xcor_
		} else {
		    # Get the next aperture
		    if (CURAPNUM(rv) < NUMAPS(rv)) {
		        if (RV_NTEMPS(rv) > 1) {	# Reset templates
		            RV_TEMPNUM(rv) = 0
		            if (next_temp(rv, rinfile, written) == ERR_READ)
			        call rv_errmsg ("Error reading next template.")
		        }
			if (next_ap(rv, written) == ERR_READ)
			    call rv_errmsg ("Errror reading next aperture.")
		    	else
			    goto redo_xcor_
		    } else {
			# get the next object
			if (RV_IMNUM(rv) < RV_NOBJS(rv)) {
			    if (NUMAPS(rv) > 1) {
			        CURAPNUM(rv) = 0	# Reset apertures
			        if (next_ap(rv, written) == ERR_READ) {
			            call rv_errmsg (
					"Errror reading next aperture.")
				}
			    }
		            if (RV_NTEMPS(rv) > 1) {
		                RV_TEMPNUM(rv) = 0	# Reset templates
		                if (next_temp(rv, rinfile, written) ==ERR_READ){
			            call rv_errmsg (
					"Error reading next template.")
				}
			    }
			    if (next_spec(rv, infile, written) == ERR_READ)
				call rv_errmsg ("Error reading next object.")
		    	    else
				goto redo_xcor_
			} else {
			    call rv_errmsg ("No more spectra to process.")
			}
		    }
		}

	    case 'o':
		# Fit or refit the object spectrum continuum for subtraction.
		tmp_int = CON_INTERACTIVE(rv)
		CON_INTERACTIVE(rv) = YES
		call do_continuum (rv, OBJECT_SPECTRUM)
		CON_INTERACTIVE(rv) = tmp_int
		RV_FITDONE(rv) = NO
		RV_NEWXCOR(rv) = YES
		IS_DBLSTAR(rv) = NO
		goto redo_xcor_

	    case 'p':
		# Go to the previous (template --> aperture --> object) after 
		# saving results.
		if (RV_TEMPNUM(rv) > 1 || CURAPNUM(rv) > 1 || RV_IMNUM(rv) > 1)
		    call rv_do_save (rv, written)

		# Now do the "previous" operation as specified. The logic 
		# sequence is complicated but handles all cases.
		if (RV_TEMPNUM(rv) > 1 && RV_NTEMPS(rv) > 1) {
		    if (prev_temp(rv, rinfile, written) == ERR_READ)
			call rv_errmsg ("Error reading previous template.")
		    else
			goto redo_xcor_
		} else {
		    # Do previous aperture
		    if (CURAPNUM(rv) > 1 && NUMAPS(rv) > 1) {
		        if (RV_NTEMPS(rv) > 1 && RV_TEMPNUM(rv) > 1) {
		            RV_TEMPNUM(rv) = RV_NTEMPS(rv) + 1 # Reset templates
		            if (prev_temp(rv, rinfile, written) == ERR_READ) {
			        call rv_errmsg (
				    "Error reading previous template.")
			    }
		        }
			if (prev_ap(rv, written) == ERR_READ)
			    call rv_errmsg ("Errror reading previous aperture.")
			else
		            goto redo_xcor_
		    } else {
			# Do previous object image
			if (RV_NOBJS(rv) > 1 && RV_IMNUM(rv) > 1) {
			    if (NUMAPS(rv) > 1) {
			        CURAPNUM(rv) = NUMAPS(rv) + 1
			        if (prev_ap(rv, written) == ERR_READ) {
			            call rv_errmsg (
				        "Errror reading previous aperture.")
			        }
			    }
			    if (RV_NTEMPS(rv) > 1) {
			        RV_TEMPNUM(rv) = RV_NTEMPS(rv) + 1
		                if (prev_temp(rv, rinfile, written) ==ERR_READ){
			            call rv_errmsg (
				        "Error reading previous template.")
			        }
			    }
			    if (prev_spec(rv, infile, written) == ERR_READ)
				call rv_errmsg("Error reading previous object.")
			    else
		                goto redo_xcor_
			} else {
			    call rv_errmsg ("At the start of the input list.")
			}
		    }
		}
		
	    case 'q': 				
	    	# Quit, possibly saving results before hand.
		if (!written || RV_UPDATE(rv) == YES) {
		    if (RV_AUTOWRITE(rv) == YES && !streq(SPOOL(rv),""))
		        call cmd_write (rv, written)
		    else
		        call rv_query_save (rv, written, QUIT)
		}
		break

	    case 'r':				
	    	# Replot the data.
replot_	    	call rv_plot (rv, CORRELATION_PLOT)
	 	if (IS_DBLSTAR(rv) == NO)
		    call rv_writeln (rv, STDOUT)

	    case 's':				
	    	# Examine object and template spectra.
		stat = spc_cursor (rv)
		if (RV_NEWXCOR(rv) == YES) {
		    key = 'x'
		    goto redo_xcor_
 		} else
		    goto replot_

	    case 't': 
		# Fit or refit the template spectrum continuum for subtraction.
		tmp_int = CON_INTERACTIVE(rv)
		CON_INTERACTIVE(rv) = YES
		call do_continuum (rv, REFER_SPECTRUM)
		CON_INTERACTIVE(rv) = tmp_int
		RV_FITDONE(rv) = NO
		RV_NEWXCOR(rv) = YES
		IS_DBLSTAR(rv) = NO
		goto redo_xcor_

	    case 'v':
		# Suspend graphics and show verbose fit/xcor output.
		call rv_verbose_fit (rv, STDOUT)

	    case 'w':
		# Write current correlation results to log file.
		if (!written || RV_UPDATE(rv) == YES) {
	            if (RV_TXFD(rv) == NULL) {
		        call strcpy ("\0", Memc[fname], SZ_FNAME)
		        while (Memc[fname] == '\0') {
	    	            call printf ("Root output filename: ")
	    	            call flush (STDOUT)
	    	            stat = scan()
		                call gargstr (Memc[fname], SZ_FNAME)
		        }
		        call init_files (rv, DEVICE(rv), Memc[fname], true)
        	        RV_MGP(rv) = gopen ("stdvdm", APPEND, RV_GRFD(rv))
			written = false
	            }
		    call cmd_write (rv, written)
		    written = true
		} else
		    call rv_query_save (rv, written, NULL)

	    case 'x':				
	    	# Re-do the correlation and check params.
		RV_NEWXCOR(rv) = YES
redo_xcor_	RV_ERRCODE(rv) = OK
		if (rv_data_check(rv) == OK && RV_NEWXCOR(rv) == YES) {
		    call rv_batch_xcor (rv, RV_TEMPNUM(rv), RV_APNUM(rv), YES, 
			YES, YES)
		} else 
		    goto replot_

	    case 'y':
		# Same as 'g' but get endpoints from Y value.
		i = RV_WINCENTER(rv) - RV_WINDOW(rv)
		max = rv_maxpix (WRKPIXY(rv,i), 2*RV_WINDOW(rv))
		call gctran (gp, x, y, x, y, wcs, 2)
		if (y >= max) {
		    call rv_errmsg ("Cannot set cursor above CCF peak.")
	 	} else {
		    call reset_errcom (rv)
		    RV_FITHGHT(rv) = y
		    call rv_yfit (rv, y, YES)
	 	}

	    case 'z':
		# Zoom in on a new peak in the entire CCF.
		RV_ERRCODE(rv) = OK
		call rv_zplot (rv, gp, x, y, wcs)

	    case '\n', '\r', ' ':
		# No-op.

	    default:
	    	# Unknown command
unknown_	call rv_errmsg ("Type '?' for a list of commands.")
	    }

	    RV_NEWXCOR(rv) = NO
	    RV_NEWGRAPH(rv) = NO

	} until (clgcur("cursor",x,y,wcs,key,Memc[cmd],SZ_LINE) == EOF)

exit_	call sfree (sp)
end


# RV_DO_SAVE - Check that a write is called for, and prompt for a filename
# if necessary.

procedure rv_do_save (rv, written)

pointer	rv				#I RV struct pointer
bool	written				#U Data write flag

begin
	if (RV_AUTOWRITE(rv) == YES && (!written || RV_UPDATE(rv) == YES))
  	    call cmd_write (rv, written)
	else if (!written || RV_UPDATE(rv) == YES)
	    call rv_query_save (rv, written, MOVE)
end


# RV_MODE_PROMPT - For a given sub-mode, prompt the user appropriately, giving
# also the parent mode.

procedure rv_mode_prompt (rv)

pointer	rv				#I RV struct pointer

begin
	# Switch on the legal modes (i.e. make it simple)
	switch (RV_MODES(rv)) {
	case 1:
	    # No-op
	case 12:
	    call printf ("ccf.fft mode: ") 	# CCF -> FFT
	case 13:
	    call printf ("ccf.spec mode: ") 	# CCF -> SPEC
	case 123:
	    call printf ("ccf.fft.spec mode: ") # CCF -> FFT -> SPEC
	case 132:
	    call printf ("ccf.spec.fft mode: ") # CCF -> SPEC -> FFT
	default:
	}
	call flush (STDOUT)
end


# RV_PARENT - Utility to return the parent mode of the current mode.

int procedure rv_parent (rv)

pointer	rv				#I RV struct pointer

begin
	# Switch on the legal modes (i.e. make it simple)
	switch (RV_MODES(rv)) {
	case 123:
	    return (FFT_MODE)
	case 132:
	    return (SPEC_MODE)
	default:
	    return (INDEFI)
	}
end


# RV_QUERY_SAVE - Query the user to save the results, and possibly a file
# name.

procedure rv_query_save (rv, written, type)

pointer	rv				#I RV struct pointer
bool	written				#I Results written flag
int	type				#I Type of prompt

pointer	sp, resp, gopen()
int	stat, scan()
bool	answer, streq()

data	answer	/true/

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

	# Prompt the user.
	if (type == QUIT) {
	    call printf ("Save results before quitting? (%b) ")
		call pargb (answer)
	} else if (type == MOVE) {
	    call printf ("Save results before moving? (%b) ")
		call pargb (answer)
	} else {
	    call printf ("Results already written.  Write again? (%b) ")
		call pargb (answer)
	}
	call flush (STDOUT)

	stat = scan()
	    call gargstr (Memc[resp], SZ_FNAME)
	if (Memc[resp] == 'n' || Memc[resp] == 'N')
	    answer = false
	else if (Memc[resp] == 'y' || Memc[resp] == 'Y')
	    answer = true

	if (answer) {
	    if (RV_TXFD(rv) == NULL) {
		call strcpy ("\0", Memc[resp], SZ_FNAME)
		while (Memc[resp] == '\0' && !streq(Memc[resp],"\"\"")) {
	    	    call printf ("Root output filename: ")
	    	    call flush (STDOUT)
	    	    stat = scan()
		        call gargstr (Memc[resp], SZ_FNAME)
		}
		if (!streq(Memc[resp],"\"\"")) {
		    call init_files (rv, DEVICE(rv), Memc[resp], true)
        	    RV_MGP(rv) = gopen ("stdvdm", APPEND, RV_GRFD(rv))
		}
	    }
	    if (!streq(Memc[resp],"\"\"")) {
	        written = false
	        call cmd_write (rv, written)
	        answer = true
	    } else {
		call printf ("Results not saved.\n")
	        call sfree (sp)
		return
	    }

	} else {
	    answer = false
	    call sfree (sp)
	    return
	}

	call sfree (sp)
end


# RV_RDCURSOR - Read and print out the current cursor position.  This routine
# figures out where the user is pointing and print out in the correct WCS.

procedure rv_rdcursor (rv, gp, x, y, wcs)

pointer	rv				#I RV struct pointer
pointer	gp				#I Graphics pointer
real	x				#I X cursor position
real	y				#I Y cursor position
int	wcs				#I WCS of cursor position

real	x1, y1, vel
double	rv_shift2vel()

begin
	# Check for boundary coordinates
	call gctran (gp, x, y, x1, y1, wcs, 0)
	if (y1 < 0.725)					# in bottom ccf plot
	    call gctran (gp, x, y, x1, y1, wcs, 2)
	else
	    call gctran (gp, x, y, x1, y1, wcs, 3)

	if (RV_DCFLAG(rv) == -1) {
	    call printf ("Cursor:  lag = %.3f  y = %.3f\n")
	        call pargr (x1)
	        call pargr (y1)
	} else {
	    vel = real (rv_shift2vel(rv,x1))
	    call printf ("Cursor:  lag = %.3f  velocity = %.3f  y = %.3f\n")
	        call pargr (x1)
	        call pargr (vel)
	        call pargr (y1)
	}
end