aboutsummaryrefslogtreecommitdiff
path: root/noao/rv/splitplot.x
blob: e29564936efd919a14dfef168152ff28a4d6d700 (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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
include	<gset.h>
include "rvpackage.h"
include "rvflags.h"
include "rvplots.h"
include "rvsample.h"

# SPLIT_PLOT - Plot the Fourier transform, power spectrum or spectrum
# normalization to the top or botom half of the screen.

procedure split_plot (rv, gp, where, rinpt, npts, dtype, pltype)

pointer	rv				#I RV struct pointer
pointer	gp				#I Graphics pointer
int	where				#I Where to make plot
real	rinpt[npts]			#I Input array
int	npts				#I No. points in input
int	dtype				#I Type of data being plotted (obj|ref)
int	pltype				#I Type of data plot to draw

pointer	sp, xdata, pldata, title
pointer	xlbl, ylbl
real	x1, x2, y1, y2
int	i, fnpts, pnpts
double	rv_shift2vel()

begin
	if (gp == NULL)
	    return

	call smark (sp)
	call salloc (title, 3*SZ_LINE, TY_CHAR)
	call salloc (xlbl, SZ_FNAME, TY_CHAR)
	call salloc (ylbl, SZ_FNAME, TY_CHAR)
	call salloc (pldata, 2*npts, TY_REAL)
	call salloc (xdata, 2*npts, TY_REAL)

	# Do some misc. initial stuff
	RV_WHERE(rv) = where
	RV_DTYPE(rv) = dtype
	call aclrr (Memr[pldata], 2*npts)
	call amovr (rinpt, Memr[pldata], npts)
	pnpts = RV_IEND(rv) - RV_ISTART(rv) + 1

	switch (pltype) {
	case SPECTRUM_PLOT, PREPARED_PLOT, NORM_PLOT:
	    fnpts = npts
	    call sp_spectrum (rv, title, xlbl, ylbl, x1, x2, fnpts, 
	        Memr[xdata], pltype)

	case FILTER_PLOT:
	    fnpts = npts
	    call sp_spectrum (rv, title, xlbl, ylbl, x1, x2, fnpts, 
		Memr[xdata], pltype)

	case CORRELATION_PLOT:
	    call sp_correlation (rv, where, title, xlbl, ylbl, x1, x2)
	    if (where == BOTTOM)
	        fnpts = npts
	    else {
		x1 = WRKPIXX(rv,1)
		x2 = WRKPIXX(rv,RV_CCFNPTS(rv))
		fnpts = RV_CCFNPTS(rv)
		call strcpy ("", Memc[ylbl], SZ_LINE)
	    }
	    call amovr (WRKPIXX(rv,1), Memr[xdata], fnpts)

	case VCORRELATION_PLOT:
	    call sp_vcorrelation (rv, title, xlbl, ylbl, x1, x2)
	    if (RV_DCFLAG(rv) == -1 && (dtype == SUMMARY_PLOT ||
		dtype == BINARY_PLOT)) {
		    x1 = RV_WINL(rv) - RV_WINDOW(rv)
		    x2 = RV_WINR(rv) + RV_WINDOW(rv)
		    fnpts = RV_CCFNPTS(rv)
		    call amovr (WRKPIXX(rv,1), Memr[xdata], fnpts)
	    } else if (where == BOTTOM) {
		do i = 1, npts 
		    Memr[xdata+i-1] = real (rv_shift2vel(rv,WRKPIXX(rv,i)))
	        fnpts = npts
	    } 

	case FOURIER_PLOT:
	    call sp_fourier (rv, dtype, where, rinpt, pldata, title, 
		xlbl, ylbl, x1, x2, npts, fnpts)
	    fnpts = int (fnpts / RVP_FFT_ZOOM(rv))

	case PS_PLOT:
	    call sp_psplot (rv, dtype, where, rinpt, pldata, title, 
		xlbl, ylbl, x1, x2, npts, fnpts)
	    fnpts = int (fnpts / RVP_FFT_ZOOM(rv))

	case ANTISYM_PLOT:
	    call sp_anplot (rv, title, xlbl, ylbl, x1, x2)
	    fnpts = RV_CCFNPTS(rv)
	    call amovr (WRKPIXX(rv,1), Memr[xdata], fnpts)
	    call amovr (ANTISYM(rv,1), Memr[pldata], fnpts)

	default:
	    call error (0, "split_plot: Illegal plot flag passed.")
	}

	# Set viewports for the plot to the screen
	call sp_set_viewports (rv, gp, where, dtype, pltype, Memc[xlbl])

	# Now label the axes for the various plots
	call sp_label_axes (rv, gp, dtype, pltype, where, x1, x2, y1, y2,
	    Memr[pldata], fnpts, pnpts, Memc[title], Memc[xlbl], Memc[ylbl])

	# lastly, draw the actual vector in the window
	call sp_draw_vector (rv, gp, pltype, where, x1, x2, y1, y2, 
	    Memr[pldata], Memr[xdata], fnpts, pnpts)

	# Now make it pretty
	call sp_annotate (rv, gp, pltype, dtype, fnpts, x1, x2, y1, y2)

	call gflush (gp)
	call sfree (sp)
end


# SP_SET_VIEWPORTS - Set the view ports for the various plit screens

procedure sp_set_viewports (rv, gp, where, dtype, pltype, xlbl)

pointer	rv					#I RV struct pointer
pointer	gp					#I Graphics pointer
int	where					#I Where is plot being drawn
int	dtype					#I Type of data (2ndary flag)
int	pltype					#I Plot type
char	xlbl[SZ_FNAME]				#I X-axis Label

begin
	# Set those darned viewpoints
	switch (where) {
	case TOP:					# Set top viewport
	    call gseti (gp, G_WCS, 1)
	    if (pltype == CORRELATION_PLOT && dtype != ANTISYM_PLOT) {
		call gseti (gp, G_WCS, 3)
	        call gsview (gp, 0.115, 0.95, 0.775, 0.90)
	    } else if (pltype == CORRELATION_PLOT && dtype == ANTISYM_PLOT)
	        call gsview (gp, 0.115, 0.95, 0.65, 0.90)
	    else if (pltype == SPECTRUM_PLOT && dtype == SUMMARY_PLOT)
	        call gsview (gp, 0.115, 0.95, 0.70, 0.90)
	    else
	        call gsview (gp, 0.115, 0.95, 0.51, 0.865)
	    call strcpy ("", xlbl, SZ_FNAME)		# Overwrite axis label

	case MIDDLE:
	    call gseti (gp, G_WCS, 1)
	    call gsview (gp, 0.115, 0.95, 0.475, 0.63)	# In a summary plot

	case BOTTOM:					# Set bottom viewport
	    call gseti (gp, G_WCS, 1)
	    if (pltype == ANTISYM_PLOT)
	        call gsview (gp, 0.115, 0.95, 0.30, 0.55)
	    else if (pltype == CORRELATION_PLOT) {
		call gseti (gp, G_WCS, 2)		# Restore attributes
	        call gsview (gp, 0.115, 0.95, 0.15, 0.725)
	    } else if (pltype == VCORRELATION_PLOT && dtype == SUMMARY_PLOT) {
		call gseti (gp, G_WCS, 2)		# Restore attributes
	        call gsview (gp, 0.115, 0.95, 0.125, 0.465)
	    } else if (pltype == VCORRELATION_PLOT && dtype == BINARY_PLOT) {
		call gseti (gp, G_WCS, 1)
	        call gsview (gp, 0.115, 0.95, 0.125, 0.64)
	    } else {
	        call gsview (gp, 0.115, 0.95, 0.125, 0.50)
	    }

	default:
	    call gclear (gp)
	}

	call gflush (gp)
end


# SP_LABEL_AXES - Draw the axes labels for the requested plots

procedure sp_label_axes (rv, gp, dtype, pltype, where, x1, x2, y1, y2, pldata, 
    fnpts, pnpts, title, xlbl, ylbl)

pointer	rv					#I RV struct pointer
pointer	gp					#I Graphics pointer
int	dtype					#I Type of data (2ndry flag)
int	pltype					#I Type of plot to draw
int	where					#I Where to draw the plot
real	x1, x2, y1, y2				#I Axis endpoints
real	pldata[ARB]				#I Data vector being drawn
int	fnpts, pnpts				#i FFT npts and plot npts
char	title[ARB]				#I Plot title
char	xlbl[SZ_LINE], ylbl[SZ_LINE]		#I Plot labels

real	v1, v2, range
int	istart, npts
double	rv_shift2vel()
real	rv_maxpix(), rv_minpix()

begin
	# Now do the real plotting
	if (where == BOTTOM && (pltype == CORRELATION_PLOT || 
	    pltype == VCORRELATION_PLOT)) {
		istart = max (1, RV_WINCENTER(rv) + 1 - RV_WINDOW(rv))
	        npts = min (RV_WINR(rv) - RV_WINL(rv) + 1, RV_CCFNPTS(rv))
	        y2 = rv_maxpix (pldata[istart], npts)
	        y1 = rv_minpix (pldata[istart], npts)
	} else {
	    y2 = rv_maxpix (pldata, fnpts)
	    y1 = rv_minpix (pldata, fnpts)
	}
	range = abs (y2 - y1)
	if (dtype != BINARY_PLOT) {
	    y2 = y2 + (.15 * range)
	    y1 = y1 - (.12 * range)
	} else
	    y2 = y2 + (.35 * range)
	call gswind (gp, x1, x2, y1, y2)

	if ((pltype == CORRELATION_PLOT || pltype == VCORRELATION_PLOT) && 
	    where == BOTTOM) {

		if (dtype != BINARY_PLOT) {		# Force plot scaling
	    	    if (!IS_INDEF(RV_Y2(rv)))
	        	y2 = RV_Y2(rv)
		    else
			RV_Y2(rv) = y2
	    	    if (!IS_INDEF(RV_Y1(rv)))
	        	y1 = RV_Y1(rv)
		    else
			RV_Y1(rv) = y1
		    call gswind (gp, x1, x2, y1, y2)
		}

		if (dtype == SUMMARY_PLOT || dtype == BINARY_PLOT) {
		    call sp_color_viewport (gp)
	            call glabax (gp, "", xlbl, ylbl) 

		} else if (RV_DCFLAG(rv) == -1) {
		    call sp_color_viewport (gp)
	            call glabax (gp, title, xlbl, ylbl) 

		} else {
		    call gseti (gp, G_WCS, 1)		# Set attributes
		    v1 = real (rv_shift2vel(rv,real(RV_WINL(rv))))
		    v2 = real (rv_shift2vel(rv,real(RV_WINR(rv))))
	            call gsview (gp, 0.115, 0.95, 0.15, 0.725)
		    call gswind (gp, v1, v2, y1, y2)
		    call gseti (gp, G_YDRAWAXES, 0)
		    call gseti (gp, G_XDRAWAXES, 2)
		    call sp_color_viewport (gp)
		    call glabax (gp, "", "", "") 	# Draw top axis

		    call gseti (gp, G_WCS, 2)		# Draw bottom labels
		    call gseti (gp, G_XDRAWAXES, 1)
		    call gseti (gp, G_YDRAWAXES, 3)
	            call glabax (gp, "", xlbl, ylbl) 

		    call gseti (gp, G_WCS, 2)		# Restore attributes
		    call gseti (gp, G_XDRAWAXES, 3)
		    call gseti (gp, G_YDRAWAXES, 3)
		}

	} else if (pltype == CORRELATION_PLOT) {
	    call gseti (gp, G_LABELTICKS, NO)		# Set attributes
	    call gseti (gp, G_DRAWTICKS, NO)

	    if (where==TOP) { 				# Do the label
	        call glabax (gp, title, "", "")
	    } else if (where == MIDDLE) {
		call sp_color_viewport (gp)
		call glabax (gp, "", "", "") 
	    }

	    call gseti (gp, G_LABELTICKS, YES)	# Restore attributes
	    call gseti (gp, G_DRAWTICKS, YES)

	} else if (pltype == FOURIER_PLOT || pltype == PS_PLOT || 
	    pltype == FILTER_PLOT) {
	      if (where==TOP) { 			# Do the label
		call gseti (gp, G_WCS, 1)		# Do the top axis
		call gswind (gp, x1, x2, y1, y2)
	        call gsview (gp, 0.115, 0.95, 0.51, 0.865)
		call gseti (gp, G_XDRAWAXES, 2)
		call gseti (gp, G_XDRAWTICKS, 2)
		call gseti (gp, G_YDRAWAXES, 0)
	        call glabax (gp, "", "", "")

		call gseti (gp, G_WCS, 4)		# Do the plot title
		call gswind (gp, x1, x2, y1, y2)
	        #call gsview (gp, 0.115, 0.95, 0.51, 0.865)
	        call gsview (gp, 0.115, 0.95, 0.51, 0.91)
		call gseti (gp, G_XDRAWAXES, 0)
		call gseti (gp, G_YDRAWAXES, 0)
	        call glabax (gp, title, "", "")

	  	call gseti (gp, G_WCS, 2)		# Remainder of top plot
		call gseti (gp, G_YDRAWAXES, 3)
		call gseti (gp, G_XDRAWAXES, 1)
		call gseti (gp, G_XDRAWTICKS, 0)
		call gseti (gp, G_XLABELTICKS, NO)
		call gswind (gp, x1, x2, y1, y2)
	        call gsview (gp, 0.115, 0.95, 0.51, 0.865)
	        call glabax (gp, "", xlbl, ylbl)

		call gseti (gp, G_XDRAWTICKS, YES)	# Restore reality
		call gseti (gp, G_XLABELTICKS, YES)
		call gseti (gp, G_XDRAWAXES, 2)

	      } else if (where == BOTTOM) {
		call gseti (gp, G_WCS, 1)
		call gswind (gp, x1, x2, y1, y2)
	        call gsview (gp, 0.115, 0.95, 0.125, 0.5)
		call gseti (gp, G_XLABELTICKS, YES)
		call gseti (gp, G_XDRAWAXES, 1)

		call sp_color_viewport (gp)
	        call glabax (gp, "", xlbl, ylbl)

		call gseti (gp, G_XDRAWAXES, 2)		# Draw top boundary
	    	call gseti (gp, G_DRAWTICKS, NO)
		call glabax (gp, "", "", "")

	    	call gseti (gp, G_DRAWTICKS, YES)
	    	call gseti (gp, G_LABELTICKS, YES)
		call gseti (gp, G_XLABELTICKS, 2)
	      }

	} else if (pltype == SPECTRUM_PLOT || pltype == NORM_PLOT ||
	    pltype == PREPARED_PLOT) {

	    if (where == TOP) {
		if (dtype == SUMMARY_PLOT) {
		    call glabax (gp, title, "", ylbl)
		} else {
	    	    call gseti (gp, G_XLABELTICKS, NO)
		    call glabax (gp, title, "", ylbl)
	    	    call gseti (gp, G_XLABELTICKS, YES)
	  	}

	    } else if (where == BOTTOM) {
		call gseti (gp, G_XLABELTICKS, YES)
		call gseti (gp, G_XDRAWAXES, 3)
		call sp_color_viewport (gp)
	        call glabax (gp, "", xlbl, ylbl)
	    }

	} else {
	    call sp_color_viewport (gp)
	    call glabax (gp, title, xlbl, ylbl)
	}

	call gflush (gp)
end


# SP_DRAW_VECTOR - Draw the vector for the requested plot

procedure sp_draw_vector (rv, gp, pltype, where, x1, x2, y1, y2, pldata, xdata,
    fnpts, pnpts)

pointer	rv					#I RV struct pointer
pointer	gp					#I Graphics pointer
int	pltype					#I Type of plot to draw
int	where					#I Where to plot data
real	x1, x2, y1, y2				#I Boundaries of plot
real	pldata[ARB]				#I Vector to plot
real	xdata[ARB]				#I X-Vector to plot
int	fnpts, pnpts				#I FFT npts and plot npts

real	left, right
int	i, npts

begin
	switch (pltype) {
	case CORRELATION_PLOT:
	    if (DBG_QUICK(rv) == NO || (DBG_QUICK(rv) == YES && where==BOTTOM)){
                if (where == BOTTOM) {
                    i = RV_WINCENTER(rv) - RV_WINDOW(rv)
                    npts = 2 * RV_WINDOW(rv) + 1
                    call gpline (gp, xdata[i], pldata[i], npts)
                } else 
                    call gpline (gp, xdata[2], pldata[2], fnpts-2)
		call gflush (gp)
	    }

	    left = RV_WINL(rv)
	    right = RV_WINR(rv)
	    switch (where) {
	    case TOP:
		call gseti (gp, G_PLTYPE, GL_DASHED)
		call gseti (gp, G_PLCOLOR, C_RED)
		call gline (gp, left, y1, left, y2)
		call gline (gp, right, y1, right, y2)
		call gseti (gp, G_PLTYPE, GL_SOLID)
		call gseti (gp, G_PLCOLOR, C_FOREGROUND)
	    	call gline (gp, x1, 0.0, x2, 0.0) 		# Zero level
	    case MIDDLE:
		# Actual plot window
		call gseti (gp, G_PLTYPE, GL_DASHED)
		#call gseti (gp, G_PLCOLOR, C_RED)
		call gline (gp, real(left-RV_WINDOW(rv)), y1, 
		    		real(left-RV_WINDOW(rv)), y2)
		call gline (gp, real(right+RV_WINDOW(rv)), y1, 
		    		real(right+RV_WINDOW(rv)), y2)
		# Parameter plot window
		call gseti (gp, G_PLTYPE, GL_DOTTED)
		call gseti (gp, G_PLCOLOR, C_RED)
		call gline (gp, left, y1, left, y2)
		call gline (gp, right, y1, right, y2)
		call gseti (gp, G_PLTYPE, GL_SOLID)
		call gseti (gp, G_PLCOLOR, C_FOREGROUND)
	    	call gline (gp, x1, 0.0, x2, 0.0) 		# Zero level
	    case BOTTOM:
	        call gseti (gp, G_WCS, 2)
		if (RV_FITDONE(rv) == YES) {
		    if (IS_DBLSTAR(rv) == NO) {
	                call gpmark (gp, xdata[RV_ISTART(rv)], 
			    pldata[RV_ISTART(rv)], pnpts, 4, 2., 2.)
	 		call rv_draw_fit (rv, gp, NO)
			call gseti (gp, G_PLCOLOR, C_GREEN)
	 	        call rv_draw_background (rv, gp)
			call gseti (gp, G_PLCOLOR, C_FOREGROUND)
		    } else {
			i = DBL_I1(rv)
	                call gpmark (gp, xdata[i], pldata[i], DBL_NFITP(rv), 
			    4, 2., 2.)
	 		call rv_plt_deblend (rv, gp, NO)
		    }
		}
	    }
	    RV_X1(rv) = x1
	    RV_X2(rv) = x2

	case VCORRELATION_PLOT:
	    call gpline (gp, xdata, pldata, fnpts)
	    if (where == BOTTOM && RV_FITDONE(rv) == YES) {
		if (IS_DBLSTAR(rv) == NO) {
	            call gpmark (gp, xdata[RV_ISTART(rv)], 
			pldata[RV_ISTART(rv)], pnpts, 4, 2., 2.)
		    if (RV_DCFLAG(rv) != -1)
		        call rv_draw_fit (rv, gp, YES)
		    else
		        call rv_draw_fit (rv, gp, NO)
	 	    call rv_draw_background (rv, gp)
		} else {
		    i = DBL_I1(rv)
	            call gpmark (gp, xdata[i], pldata[i], DBL_NFITP(rv), 
		        4, 2., 2.)
	 	    call rv_plt_deblend (rv, gp, YES)
		}
	    }

	case CONVOLUTION_PLOT, ANTISYM_PLOT, FILTER_PLOT, PREPARED_PLOT:
	    if (DBG_QUICK(rv) == NO && RV_DTYPE(rv) != SUMMARY_PLOT)
	        call gpline (gp, xdata, pldata, fnpts)

	case SPECTRUM_PLOT, NORM_PLOT:
	    if (DBG_QUICK(rv) == NO && RV_DTYPE(rv) != SUMMARY_PLOT) {
		if (RV_DTYPE(rv) == OBJECT_SPECTRUM)
	            call gpline (gp, xdata, pldata, RV_NPTS(rv))
		else
	            call gpline (gp, xdata, pldata, RV_RNPTS(rv))
	    } else if (RV_DTYPE(rv) == SUMMARY_PLOT) {
	        call gpline (gp, xdata, pldata, RV_NPTS(rv))
	        #call gvline (gp, pldata, RV_NPTS(rv), x1, x2)
	    }
	default:
	    call gvline (gp, pldata, fnpts, x1, x2)
	}
	RV_GTYPE(rv) = pltype
	call gflush (gp)
end


# SP_ANNOTATE - Annotate the split plot to clarify what's what.

procedure sp_annotate (rv, gp, pltype, dtype, fnpts, x1, x2, y1, y2)

pointer	rv						#I RV struct pointer
pointer	gp						#I Graphics pointer
int	pltype, dtype					#I Plot and data types
int	fnpts						#I Npts in fft plot
real	x1, x2, y1, y2					#I Plot boundaries

double	dex()
real	l, r
int	i

begin
	call gseti (gp, G_TXCOLOR, RV_TXTCOLOR(rv))
	switch (pltype) {
	case FOURIER_PLOT, PS_PLOT:
	    if (dtype == OBJECT_SPECTRUM) {
	        if (RV_FILTER(rv) == OBJ_ONLY || RV_FILTER(rv) == BOTH) {
		    call gseti (gp, G_WCS, 2)
	            call gsview (gp, 0.115, 0.95, 0.51, 0.865)
		    call gswind (gp, x1, x2, y1, y2)
	            call fft_fltoverlay (rv, gp, int(fnpts*RVP_FFT_ZOOM(rv))*2, 
			y2)
		}
	    } else if (dtype == REFER_SPECTRUM) {
	        if (RV_FILTER(rv) == TEMP_ONLY || RV_FILTER(rv) == BOTH) {
		    call gseti (gp, G_WCS, 2)
	            call gsview (gp, 0.115, 0.95, 0.125, 0.5)
		    call gswind (gp, x1, x2, y1, y2)
	            call fft_fltoverlay (rv, gp, int(fnpts*RVP_FFT_ZOOM(rv))*2, 
			y2)
		}
	    }
	    if (RV_WHERE(rv) == TOP) {
	        call gctran (gp, 0.73, 0.8, x1, y1, 0, 2)
		if (pltype == FOURIER_PLOT)
	            call gtext (gp, x1, y1, "Object FFT", "")
		else
	            call gtext (gp, x1, y1, "Object PS", "")
	        call gctran (gp, 0.73, 0.77, x1, y1, 0, 1)
	    } else if (RV_WHERE(rv) == BOTTOM) {
	        call gctran (gp, 0.73, 0.43, x1, y1, 0, 1)
		if (pltype == FOURIER_PLOT)
	            call gtext (gp, x1, y1, "Template FFT", "") 
		else
	            call gtext (gp, x1, y1, "Template PS", "")
	        call gctran (gp, 0.73, 0.4, x1, y1, 0, 1)
	    }
	    if (RV_FILTER(rv) == BOTH || RV_FILTER(rv) == OBJ_ONLY) {
	        if (RVP_WHEN(rv) == BEFORE)
	            call gtext (gp, x1, y1, "Before Filter", "")
	        else
	            call gtext (gp, x1, y1, "After Filter", "")
	    }

	case NORM_PLOT, SPECTRUM_PLOT, PREPARED_PLOT, FILTER_PLOT:
	    if (RV_WHERE(rv) == TOP && dtype != SUMMARY_PLOT) {
	        call gctran (gp, 0.7, 0.55, x1, y1, 0, 1)
		switch (pltype) {
		case NORM_PLOT:
		    if (dtype == OBJECT_SPECTRUM)
	                call gtext (gp, x1, y1, "Norm. Object", "")
		    else
	                call gtext (gp, x1, y1, "Norm. Template", "")
		case SPECTRUM_PLOT:
		    if (dtype == OBJECT_SPECTRUM)
	               call gtext (gp, x1, y1, "Orig. Object", "")
		    else
	               call gtext (gp, x1, y1, "Orig. Template", "")
		case PREPARED_PLOT:
	            call gtext (gp, x1, y1, "Prepared Object", "")
		case FILTER_PLOT:
	            call gtext (gp, x1, y1, "Filtered Object", "")
		}
	    } else if (RV_WHERE(rv) == BOTTOM) {
	        call gctran (gp, 0.7, 0.175, x1, y1, 0, 1)
		switch (pltype) {
		case NORM_PLOT:
		    if (dtype == OBJECT_SPECTRUM)
	                call gtext (gp, x1, y1, "Norm. Object", "")
		    else
	                call gtext (gp, x1, y1, "Norm. Template", "")
		case SPECTRUM_PLOT:
		    if (dtype == OBJECT_SPECTRUM)
	               call gtext (gp, x1, y1, "Orig. Object", "")
		    else
	               call gtext (gp, x1, y1, "Orig. Template", "")
		case PREPARED_PLOT: 
		    call gtext (gp, x1, y1, "Prepared Temp.", "")
		case FILTER_PLOT:
		    if (dtype == OBJECT_SPECTRUM)
	                call gtext (gp, x1, y1, "Filtered Object", "")
		    else if (dtype == REFER_SPECTRUM)
	                call gtext (gp, x1, y1, "Filtered Temp.", "")
		}
	    }
	    if (dtype != SUMMARY_PLOT) {
		if (pltype != PREPARED_PLOT) {
		    if (RV_WHERE(rv) == TOP)
	 	        call rv_mark_regions (RV_OSAMPLE(rv), gp)
		    else
	 	        call rv_mark_regions (RV_RSAMPLE(rv), gp)
		}
	    } else if (dtype == SUMMARY_PLOT && RV_WHERE(rv) == TOP) {
		if (SR_COUNT(RV_OSAMPLE(rv)) != ALL_SPECTRUM) {
		    call gseti (gp, G_PLCOLOR, C_GREEN)
		    do i = 1, SR_COUNT(RV_OSAMPLE(rv)) {
            		l = SRANGE(RV_OSAMPLE(rv),i)
            		r = ERANGE(RV_OSAMPLE(rv),i)
            		if (RV_PIXCORR(rv) == NO && RV_DCFLAG(rv) != -1 &&
            		    SR_UNITS(RV_OSAMPLE(rv)) == PIXELS) {
                    		l = real (dex(RV_OW0(rv)+(l-1)*RV_OWPC(rv)))
                    		r = real (dex(RV_OW0(rv)+(r-1)*RV_OWPC(rv)))
            		}
	 	        call mark_range (gp, l, r)
		    }
		    call gseti (gp, G_PLCOLOR, C_FOREGROUND)
		}
	    }
	
	case ANTISYM_PLOT:
	    # Write the text
	    call gsview (gp, 0.05, 0.97, 0.30, 0.9)
	    call gswind (gp, 0.08, 0.97, 0.30, 0.9)
	    call gseti (gp, G_TXCOLOR, C_FOREGROUND)
	    call gtext (gp, 0.075, 0.45, "Correlation\000", "p=d")
	}
	call gseti (gp, G_TXCOLOR, C_FOREGROUND)
end


# SP_COLOR_VIEWPORT - Fill the specified viewport with the current background
# color.  For color terminals such as XGterm the background color is only
# drawn in the first graph on the screen.

procedure sp_color_viewport (gp)

pointer	gp					#I graphics pointer

real	x1, x2, y1, y2, xv[5], yv[5]

begin
	# Get the current viewport boundaries.
	call ggwind (gp, x1, x2, y1, y2)

	# Fill the polygon vector and color the area.
        xv[1] = x1;  yv[1] = y1
        xv[2] = x2;  yv[2] = y1
        xv[3] = x2;  yv[3] = y2
        xv[4] = x1;  yv[4] = y2
        xv[5] = x1;  yv[5] = y1
        call gseti (gp, G_FACOLOR, 0)
        call gfill (gp, xv, yv, 4, GF_SOLID)
	call gflush (gp)
end


# SP_SPECTRUM -  Set window boundaries and title for a spectrum plot

procedure sp_spectrum (rv, title, x_lbl, y_lbl, x1, x2, fnpts, xdata, pltype)

pointer	rv					#I RV struct pointer
pointer	title					#O Plot title pointer
pointer	x_lbl					#O Plot x label pointer
pointer	y_lbl					#O Plot y label pointer
real	x1, x2					#O Endpoints
int	fnpts					#I Npts to plot
real	xdata[fnpts]				#O X-axis data
int	pltype					#I Plot type

int	i, fft_pow2()

begin
	call get_plot_title (rv, title, fnpts)
	call strcpy ("Intensity", Memc[y_lbl], SZ_FNAME)
	if (RV_DCFLAG(rv) == -1 || pltype == PREPARED_PLOT) {
	    call strcpy ("Pixel", Memc[x_lbl], SZ_FNAME)
	    x1 = 1.
	    if (pltype == PREPARED_PLOT) {
                i = int ((RV_GLOB_W2(rv) - RV_GLOB_W1(rv)) / RV_OWPC(rv) + 1)
	        x2 = fft_pow2 (i)
		if (RV_RW0(rv) > RV_OW2(rv) || RV_OW0(rv) > RV_RW2(rv))
	    	    x2 = x2 * 2
	    } else
		x2 = real (fnpts)
	    for (i=int(x2); i>=1; i=i-1)
	  	xdata[i] = real[i]
	} else {
	    call strcpy ("Wavelength", Memc[x_lbl], SZ_FNAME)
	    if (RV_DTYPE(rv) == SUMMARY_PLOT) {
	        x1 = 10. ** (RV_OW0(rv))
	        x2 = 10. ** (RV_OW2(rv))
	    } else {
	        x1 = 10. ** (RV_GLOB_W1(rv))
	        x2 = 10. ** (RV_GLOB_W2(rv))
	    }

	    if (pltype == FILTER_PLOT || 
		pltype == NORM_PLOT || 
		pltype == SPECTRUM_PLOT) {
	            if (RV_DTYPE(rv) == REFER_SPECTRUM) {
	                do i = 1, fnpts
	                    xdata[i] = 10. ** (RV_RW0(rv) + (i-1) * RV_RWPC(rv))
		    } else {
	                do i = 1, fnpts
		            xdata[i] = 10. ** (RV_OW0(rv) + (i-1) * RV_OWPC(rv))
		    }
	    } else if (RV_WHERE(rv) == TOP) {
	        do i = 1, fnpts
		    xdata[i] = 10. ** (RV_OW0(rv) + (i-1) * RV_OWPC(rv))
	    } else {
	        do i = 1, fnpts
		    xdata[i] = 10. ** (RV_RW0(rv) + (i-1) * RV_RWPC(rv))
	    }
	}
end


# SP_VCORRELATION - Set window boundaries and titles for a velocity CCF
# plot.

procedure sp_vcorrelation (rv, title, x_lbl, y_lbl, x1, x2)

pointer	rv					#I RV struct pointer
pointer	title					#O Plot title pointer
pointer	x_lbl					#O Plot x label pointer
pointer	y_lbl					#O Plot y label pointer
real	x1, x2					#O Endpoints
double	rv_shift2vel()
real	min(), max()

begin
	if (RV_DCFLAG(rv) == -1) {
	    call strcpy ("Pixel Correlation - No velocities will be computed",
		Memc[title], SZ_LINE)
	    x1 = max (WRKPIXX(rv,1), real(RV_WINL(rv)-RV_WINDOW(rv)))
	    x2 = min (WRKPIXX(rv,RV_CCFNPTS(rv)), 
		real(RV_WINR(rv)+RV_WINDOW(rv)))
	    call strcpy ("Pixel Shift", Memc[x_lbl], SZ_FNAME)
	} else {
	    call sprintf (Memc[title], SZ_LINE,
	        "Correlation function  Template = '%s'")
		    call pargstr (TEMPNAME(rv))
	    if (RV_DTYPE(rv) == SUMMARY_PLOT) {
	        x1 = max (WRKPIXX(rv,1), real(RV_WINL(rv)-RV_WINDOW(rv)))
	        x2 = min (WRKPIXX(rv,RV_CCFNPTS(rv)), 
		    real(RV_WINR(rv)+RV_WINDOW(rv)))
	    } else {
	        x1 = max (WRKPIXX(rv,1), real(RV_WINL(rv)))
	        x2 = min (WRKPIXX(rv,RV_CCFNPTS(rv)), real(RV_WINR(rv)))
	    }
	    x1 = real (rv_shift2vel(rv,x1))
	    x2 = real (rv_shift2vel(rv,x2))
	    call strcpy ("Relative Velocity (Km/sec)", Memc[x_lbl], SZ_FNAME)
	}
	call strcpy ("Correlation", Memc[y_lbl], SZ_FNAME)
end


# SP_CORRELATION - Set window boundaries and titles for a CCF plot.

procedure sp_correlation (rv, where, title, x_lbl, y_lbl, x1, x2)

pointer	rv					#I RV struct pointer
int	where					#I Where is the plot located?
pointer	title					#O Plot title pointer
pointer	x_lbl					#O Plot x label pointer
pointer	y_lbl					#O Plot y label pointer
real	x1, x2					#O Endpoints

real	min(), max()

begin
	if (where == BOTTOM) {
	    if (RV_DCFLAG(rv) == -1) {
	        call strcpy (
		  "Pixel Correlation - No velocities will be computed",
		    Memc[title], SZ_LINE)
	    } else {
	        call sprintf (Memc[title], SZ_LINE,
	            "Correlation function  Template = '%s'")
		        call pargstr (TEMPNAME(rv))
	    }
	} else
	    call get_anplot_title (rv, title)

	call strcpy ("Correlation", Memc[y_lbl], SZ_FNAME)
	call strcpy ("Pixel Shift", Memc[x_lbl], SZ_FNAME)
	x1 = max (real(RV_WINL(rv)), WRKPIXX(rv,1)-1)
	x2 = min (real(RV_WINR(rv)), WRKPIXX(rv,RV_CCFNPTS(rv))+1)
end


# SP_ANPLOT - Set window boundaries and title for an antisymmetric noise
# plot.

procedure sp_anplot (rv, title, x_lbl, y_lbl, x1, x2)
 
pointer rv                                      #I RV struct pointer
pointer title                                   #O Plot title pointer
pointer x_lbl                                   #O Plot x label pointer
pointer y_lbl                                   #O Plot y label pointer
real    x1, x2                                  #O Endpoints
 
begin
        call sprintf (Memc[title], SZ_LINE,
            "Antisymmetric Noise Component of CCF")
        call strcpy ("", Memc[y_lbl], SZ_FNAME)
        call strcpy ("Lag", Memc[x_lbl], SZ_FNAME)
        x1 = WRKPIXX(rv,1)
        x2 = WRKPIXX(rv,RV_CCFNPTS(rv))
end
 
 
# SP_FOURIER - Set window boundaries and title for an FFT plot.

procedure sp_fourier (rv, dtype, where, rinpt, pldata, title, x_lbl, y_lbl, 
    x1, x2, npts, fnpts)

pointer	rv					#I RV struct pointer
int	dtype					#I Data type to plot
int	where					#I Where to plot the data
real	rinpt[npts]				#I Input plot array
pointer	pldata					#O Output plot array
pointer	title					#O Plot title pointer
pointer	x_lbl					#O Plot x label pointer
pointer	y_lbl					#O Plot y label pointer
real	x1, x2					#O Endpoints
int	npts					#I Npts in data
int	fnpts					#I Npts in fft

begin
	call get_fft (rv, rinpt, npts, Memr[pldata], fnpts)
	fnpts = max (RV_FFTNPTS(rv), fnpts) / 2
	if (where != BOTTOM)
	    call get_anplot_title (rv, title)
	else
	    call strcpy ("", Memc[title], SZ_FNAME)

	if (RVP_LOG_SCALE(rv) == YES)
	    call strcpy ("log(|G(k)|)", Memc[y_lbl], SZ_FNAME)
	else
	    call strcpy ("|G(k)|", Memc[y_lbl], SZ_FNAME)

	switch (where) {
	case TOP:
	    call strcpy ("Frequency", Memc[x_lbl], SZ_FNAME)
	    x1 = 0.0
	    x2 = (real (fnpts) / RVP_FFT_ZOOM(rv)) / (2. * real (fnpts))
	case BOTTOM:
	    call strcpy ("Wavenumber", Memc[x_lbl], SZ_FNAME)
	    x1 = 1.
	    x2 = real (fnpts) / RVP_FFT_ZOOM(rv)
	}
end


# SP_PSPLOT - Set window boundaries and title for a power spectrum plot.

procedure sp_psplot (rv, dtype, where, rinpt, pldata, title, x_lbl, y_lbl, 
    x1, x2, npts, fnpts)

pointer	rv					#I RV struct pointer
int	dtype					#I Data type to plot
int	where					#I Where to plot the data
real	rinpt[npts]				#I Input plot array
pointer	pldata					#O Output plot array
pointer	title					#O Plot title pointer
pointer	x_lbl					#O Plot x label pointer
pointer	y_lbl					#O Plot y label pointer
real	x1, x2					#O Endpoints
int	npts					#I Npts in data
int	fnpts					#O Npts to plot


begin
	call get_pspec (rv, rinpt, npts, Memr[pldata], fnpts)
	fnpts = max (RV_FFTNPTS(rv), fnpts) / 2
	call get_anplot_title (rv, title)

	if (RVP_LOG_SCALE(rv) == YES)
	    call strcpy ("log(|Power|)", Memc[y_lbl], SZ_FNAME)
	else
	    call strcpy ("Power", Memc[y_lbl], SZ_FNAME)

	switch (where) {
	case TOP:
	    call strcpy ("Frequency", Memc[x_lbl], SZ_FNAME)
	    x1 = 0.0
	    x2 = (real (fnpts) / RVP_FFT_ZOOM(rv)) / (2. * real (fnpts))
	case BOTTOM:
	    call strcpy ("Wavenumber", Memc[x_lbl], SZ_FNAME)
	    x1 = 1.
	    x2 = real (fnpts) / RVP_FFT_ZOOM(rv)
	}
end