aboutsummaryrefslogtreecommitdiff
path: root/pkg/plot/t_implot.x
blob: fbf8f00a037b4e174f1c5f24042186b8658fc5fb (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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<error.h>
include	<ctype.h>
include	<imhdr.h>
include	<mach.h>
include	<gset.h>
include	<mwset.h>

define	SEGLEN		10
define	SZ_PLOTTITLE	512
define	KEYSFILE	"lib$scr/implot.key"
define	MAX_COLORS	8


# IMPLOT -- Image plot program.  An interactive, cursor driven program for
# plotting lines and columns of images.  This is an early version of the program
# lacking averaging and interaction with the image display cursor.
#
#	implot (image [,line])
#
# Keystrokes:
#
#	?		help
#	a		plot the average of a range of lines of columns
#	c		plot column at position of cursor
#	e		expand plot by marking corners of new window
#	j		move down
#	k		move up
#	l		plot line at position of cursor
#	m		previous image
#	n		next image
#	p		measure profile (mark region and baseline with 2 pos)
#	o		overplot next vector
#	r		redraw
#	s		print statistics on a region
#	/		scroll status line
#
#
# In addition to the above keystrokes, the following ':' escapes are recognized
# by the program:
#
#	:a N		set number of lines or columns to average
#	:c M [N]	plot column M or avg of M to N
#	:f format	set label format
#	:i imagename	open a new image for input
#	:l M [N]	plot line M or avg of M to N
#	:o		overplot
#	:log+,log-	enable, disable log scaling in Y
#	:step N		set step size for j,k
#	:solid		overplot with solid, not dashed, lines
#	:mono		disable coloring of overplotted vectors
#	:x x1 x2	fix plot window in X (no args to unfix)
#	:y y1 y2	fix plot window in Y (no args to unfix)
#	:w wcstype	change world coordinate type

procedure t_implot()

int	list
char	image[SZ_FNAME]
char	wcstype[SZ_FNAME]
char	xlabel[SZ_FNAME]
char	format[SZ_FNAME]
char	fmt[SZ_FNAME]
char	command[SZ_FNAME]
char	device[SZ_FNAME]

int	xnticks, ynticks
bool	overplot, lineplot, logscale, erase, rescale[2], p_rescale[2]
int	key, wcs, ip, i1, i2, n, linetype, color, nltypes, linestep, navg
int	npix, nlines, ncols, line, col, shift, step, p_navg, sline, nim, index
real	x, y, px, py, qx, qy, x1, x2, y1, y2
real	median, mean, sigma, sum
pointer	im, mw, ct, gp, xold, yold, xnew, ynew, sl, ptr

real	asumr(), amedr(), plt_iformatr()
int	clgeti(), clgcur(), ctoi(), ctor(), ggeti(), imtlen(), imaccess()
pointer	gopen(), immap(), mw_openim(), mw_sctran(), sl_getstr()
pointer	imtopenp(), imtrgetim()
errchk	mw_sctran

define	line_ 91
define	col_ 92
define	replotline_ 93
define	replotcol_ 94
define	nextim_ 95
define	quit_ 96
string	bell "\007"
string	again "again:"

begin
	list = imtopenp ("image")
	call clgstr ("device", device, SZ_FNAME)
	gp = gopen (device, NEW_FILE, STDGRAPH)
	call clgstr ("wcs", wcstype, SZ_FNAME)

	if (clgeti ("$nargs") > 1)
	    line = clgeti ("line")
	else
	    line = INDEFI

	p_rescale[1] = true
	rescale[1]   = true
	p_rescale[2] = true
	rescale[2]   = true

	logscale  = false
	overplot  = false
	lineplot  = true
	erase     = false
	xnticks	  = 5
	ynticks	  = 5
	format[1] = EOS

	linestep  = 1
	linetype  = 1
	color     = 1
	nltypes   = ggeti (gp, "lt")
	p_navg	  = 1
	navg	  = 1
	step	  = clgeti ("step")

	# Loop through the images.  Currently this loop is not actually
	# used and instead the 'm' and 'n' keys explicitly change the
	# image.  The 'q' key exits the loop regardless of the position
	# of the list.

	nim = imtlen (list)
	index = 1
	while (imtrgetim (list, index, image, SZ_FNAME) != EOF) {
	    iferr {
		im = NULL; mw = NULL; sl = NULL

		if (imaccess (image, READ_ONLY) == YES) {
		    ptr = immap (image, READ_ONLY, 0); 
		    im = ptr
	   
		} else {
		    call eprintf ("Error opening image '%s'")
			call pargstr (image)
		    goto nextim_
		}

		ptr = mw_openim (im); mw = ptr
		call mw_seti (mw, MW_USEAXMAP, NO)

		ct = mw_sctran (mw, "logical", wcstype, 0)

		ncols = IM_LEN(im,1)
		if (IM_NDIM(im) <= 0)
		    call error (1, "image has no pixels")
		else if (IM_NDIM(im) > 1)
		    nlines = IM_LEN(im,2)
		else
		    nlines = 1

		if (IS_INDEFI(line))
		    line = max(1, min(nlines, (nlines + 1) / 2))

		if (IS_INDEFI(step) || step < 1)
		    step = max (1, nlines / 10)

		npix = max (ncols, nlines)
		call malloc (xold, npix, TY_REAL)
		call malloc (yold, npix, TY_REAL)
		call malloc (xnew, npix, TY_REAL)
		call malloc (ynew, npix, TY_REAL)

		if (!overplot)
		    call gclear (gp)
		call imp_getvector (im, mw, ct, wcstype, xnew, ynew, xlabel,
		    fmt, line, navg, lineplot)
		if (format[1] == '%')
		    call strcpy (format, fmt, SZ_FNAME)
		npix = ncols

		call gsets (gp, G_XTICKFORMAT, fmt)
		if (xnticks >= 0)
		    call gseti (gp, G_XNMAJOR, xnticks)
		if (ynticks >= 0)
		    call gseti (gp, G_YNMAJOR, ynticks)
		call gseti (gp, G_NMINOR, 0)
		call gseti (gp, G_PLTYPE, 1)
		call gseti (gp, G_PLCOLOR, 1)

		call imp_plotvector (gp, im, Memr[xnew], Memr[ynew], ncols,
		    nlines, real(line), navg, lineplot, rescale, image, xlabel)
		overplot = false

		call sl_init (sl, 1)
		while (clgcur ("coords", x, y, wcs, key, command, SZ_FNAME) !=
		    EOF) {
		    if (key == 'q')
quit_			break

		    switch (key) {
		    case 'a':
			# Plot the average over a range of lines or columns
			# marked interactively with the cursor.

			x1 = x; y1 = y
			call printf (again)
			if (clgcur ("gcur", x2, y2, wcs, key, command,
			    SZ_FNAME) == EOF)
			    next

			if (abs(x2-x1) > abs(y2-y1)) {
			    # Range is in X.

			    navg = abs (x2 - x1) + 1
			    if (lineplot) {
				col = nint (min (x1, x2))
				goto col_
			    } else {
				line = nint (min (x1, x2))
				goto line_
			    }

			} else {
			    # Range is in Y.

			    if (lineplot) {
				call imp_tran (gp, x1, y1, x1, y1, Memr[xnew],
				    ncols, nlines)
				call imp_tran (gp, x2, y2, x2, y2, Memr[xnew],
				    ncols, nlines)
				navg = abs (y2 - y1) + 1
				line = nint (min (y1, y2))
				goto line_
			    } else {
				call imp_tran (gp, x1, y1, x1, y1, Memr[xnew],
				    nlines, ncols)
				call imp_tran (gp, x2, y2, x2, y2, Memr[xnew],
				    nlines, ncols)
				navg = abs (y2 - y1) + 1
				col = nint (min (y1, y2))
				goto col_
			    }
			}

		    case 'j', 'k':
			# Move viewport into image up (k) or down (j).  This
			# is done by erasing the old data vector and drawing
			# a new one.

			erase = true
			navg = p_navg
			overplot = true
			call amovr (Memr[xnew], Memr[xold], npix)
			call amovr (Memr[ynew], Memr[yold], npix)

			shift = step
			if (key == 'j')
			    shift = -shift
			    
			if (lineplot) {
			    line = line + shift
			    goto line_
			} else {
			    col  = col  + shift
			    goto col_
			}

		    case 'l':
			# Plot a line.
			if (lineplot) {
			    call imp_tran (gp, x, y, px, py, Memr[xnew], ncols,
				nlines)
			    line = max(1, min(nlines, nint(py)))
			} else {
			    call imp_tran (gp, x, y, px, py, Memr[xnew], nlines,
				ncols)
			    line = max(1, min(nlines, nint(px)))
			}
			navg = p_navg
			line = line - (navg - 1) / 2
line_
			lineplot = true
			line = max(1, min(nlines, line))
			call imp_getvector (im, mw, ct, wcstype, xnew, ynew,
			    xlabel, fmt, line, navg, lineplot)
			if (format[1] == '%')
			    call strcpy (format, fmt, SZ_FNAME)
			npix = ncols
replotline_
			if (overplot) {
			    if (erase) {
				# Erase old vector and replace it with new
				# vector.

				call imp_redraw (gp, Memr[xold], Memr[yold],
				    Memr[xnew], Memr[ynew], npix)
				erase = false

			    } else {
				# Overplot new vector. 

				linetype = linetype + linestep
				if (linetype > nltypes)
				    linetype = 1
				call gseti (gp, G_PLTYPE, linetype)

				color = color + 1
				if (color > MAX_COLORS)
				    color = 1
				call gseti (gp, G_PLCOLOR, color)

				call gpline (gp, Memr[xnew], Memr[ynew], ncols)
			    }

			    call imp_markpos (gp, line, nlines)
			    overplot = false

			} else {
			    call gclear (gp)
			    call gsets (gp, G_XTICKFORMAT, fmt)
			    if (logscale)
				call gseti (gp, G_YTRAN, GW_LOG)
			    call gseti (gp, G_NMINOR, 0)
			    if (xnticks >= 0)
				call gseti (gp, G_XNMAJOR, xnticks)
			    if (ynticks >= 0)
				call gseti (gp, G_YNMAJOR, ynticks)
			    linetype = 1
			    color = 1
			    call imp_plotvector (gp, im, Memr[xnew], Memr[ynew],
				ncols, nlines, real(line), navg, lineplot,
				rescale, image, xlabel)
			    rescale[1] = p_rescale[1]
			    rescale[2] = p_rescale[2]
			}

		    case 'm', 'n':
			if (key == 'm') {
			    if (index > 1)
				index = index - 1
			    else
				next
			} else if (key == 'n') {
nextim_		    	if (index < nim)
				index = index + 1
			    else
				next
			}

			if (imtrgetim (list, index, command, SZ_FNAME) == EOF)
			    break

			# Open a different image.
			call mw_close (mw)
			call imunmap (im)

			iferr (im = immap (command, READ_ONLY, 0)) {
			    call erract (EA_WARN)
			    im = immap (image, READ_ONLY, 0)
			    mw = mw_openim (im)
			    call mw_seti (mw, MW_USEAXMAP, NO)
			    ct = mw_sctran (mw, "logical", wcstype, 0)
			    next
			}

			if (IM_NDIM(im) <= 0) {
			    call eprintf ("image has no pixels\n")
			    im = immap (image, READ_ONLY, 0)
			    mw = mw_openim (im)
			    call mw_seti (mw, MW_USEAXMAP, NO)
			    ct = mw_sctran (mw, "logical", wcstype, 0)
			    next

			} else {
			    mw = mw_openim (im)
			    call mw_seti (mw, MW_USEAXMAP, NO)
			    ct = mw_sctran (mw, "logical", wcstype, 0)

			    ncols = IM_LEN(im,1)
			    if (IM_NDIM(im) > 1)
				nlines = IM_LEN(im,2)
			    else {
				lineplot = true
				nlines = 1
			    }

			    npix   = max (ncols, nlines)
			    call strcpy (command, image, SZ_FNAME)
			    call realloc (xold, npix, TY_REAL)
			    call realloc (yold, npix, TY_REAL)
			    call realloc (xnew, npix, TY_REAL)
			    call realloc (ynew, npix, TY_REAL)

			    if (lineplot)
				goto line_
			    else
				goto col_
			}

		    case 'c':
			# Plot a column.
			if (lineplot) {
			    call imp_tran (gp, x, y, px, py, Memr[xnew], ncols,
				nlines)
			    col = max(1, min(ncols, nint(px)))
			} else {
			    call imp_tran (gp, x, y, px, py, Memr[xnew], nlines,
				ncols)
			    col = max(1, min(ncols, nint(py)))
			}
			navg = p_navg
			col = col - (navg - 1) / 2
col_
			if (nlines == 1) {
			    call printf (bell)
			    next
			}
			lineplot = false
			col = max(1, min(ncols, col))
			call imp_getvector (im, mw, ct, wcstype, xnew, ynew,
			    xlabel, fmt, col, navg, lineplot)
			if (format[1] == '%')
			    call strcpy (format, fmt, SZ_FNAME)
			npix = nlines
replotcol_
			if (overplot) {
			    if (erase) {
				# Erase old vector and replace it with new
				# vector.

				call imp_redraw (gp, Memr[xold], Memr[yold],
				    Memr[xnew], Memr[ynew], npix)
				erase = false

			    } else {
				linetype = linetype + linestep
				if (linetype > nltypes)
				    linetype = 1
				call gseti (gp, G_PLTYPE, linetype)

				color = color + 1
				if (color > MAX_COLORS)
				    color = 1
				call gseti (gp, G_PLCOLOR, color)

				call gpline (gp, Memr[xnew], Memr[ynew], nlines)
			    }

			    call imp_markpos (gp, col, ncols)
			    overplot = false

			} else {
			    call gclear (gp)
			    call gsets (gp, G_XTICKFORMAT, fmt)
			    if (logscale)
				call gseti (gp, G_YTRAN, GW_LOG)
			    call gseti (gp, G_NMINOR, 0)
			    if (xnticks >= 0)
				call gseti (gp, G_XNMAJOR, xnticks)
			    if (ynticks >= 0)
				call gseti (gp, G_YNMAJOR, ynticks)
			    linetype = 1
			    color = 1
			    call imp_plotvector (gp, im, Memr[xnew], Memr[ynew],
				nlines, ncols, real(col), navg, lineplot,
				rescale, image, xlabel)
			    rescale[1] = p_rescale[1]
			    rescale[2] = p_rescale[2]
			}

		    case 'e':
			# Expand plot by marking corners of new window.  We are
			# called with the coords of the lower left corner.

			x1 = x; y1 = y
			call printf (again)
			if (clgcur ("gcur", x2, y2, wcs, key, command,
			    SZ_FNAME) == EOF)
			    next

			rescale[1] = false
			rescale[2] = false
			p_rescale[1] = true
			p_rescale[2] = true

			# If the cursor moved only in X, with negligible range
			# in Y, expand only in X.  Do the comparisons in NDC
			# space to avoid scaling problems.

			call gctran (gp, x1, y1, px, py, wcs, 0)
			call gctran (gp, x2, y2, qx, qy, wcs, 0)

			if (abs (py - qy) < .01) {
			    y1 = INDEF;  y2 = INDEF
			    rescale[2] = true
			}
			call imp_swind (x1, x2, y1, y2)

			if (lineplot)
			    goto replotline_
			else
			    goto replotcol_

		    case 'o':
			overplot = true

		    case 'r':
			if (lineplot)
			    goto replotline_
			else
			    goto replotcol_

		    case 'p':
			# Profile analysis.
			x1 = x
			y1 = y
			call printf (again)
			if (clgcur ("gcur", x2, y2, wcs, key, command,
			    SZ_FNAME) == EOF)
			    next

			call imp_profile (gp, Memr[xnew], Memr[ynew], npix,
			    x1, y1, x2, y2, sl, sline)
			call printf (Memc[sl_getstr(sl,sline)])

		    case 's':
			# Statistics.
			x1 = x
			call printf (again)
			if (clgcur ("gcur", x2, y, wcs, key, command,
			    SZ_FNAME) == EOF)
			    next

			i1 = max(1, min(npix, nint(x1)))
			i2 = max(1, min(npix, nint(x2)))
			if (i1 > i2) {
			    n = i1
			    i1 = i2
			    i2 = n
			} else if (i1 == i2)
			    i2 = i1 + 1

			n = i2 - i1 + 1
			call aavgr (Memr[ynew+i1-1], n, mean, sigma)
			median = amedr (Memr[ynew+i1-1], n)
			sum = asumr (Memr[ynew+i1-1], n)

			call sl_init (sl, 1)
			call sprintf (Memc[sl_getstr(sl,1)], SZ_LINE,
			    "median=%g, mean=%g, rms=%g, sum=%g, npix=%d\n")
			    call pargr (median)
			    call pargr (mean)
			    call pargr (sigma)
			    call pargr (sum)
			    call pargi (n)
			sline = 1
			call printf (Memc[sl_getstr(sl,sline)])

		    case ' ':
			# Print cursor coordinates.
			call sl_init (sl, 1)
			if (lineplot) {
			    call imp_tran (gp, x, y, px, py, Memr[xnew], ncols,
				nlines)
			    col = px
			    call plt_wcscoord (im, mw, ct, wcstype, format, col,
				line, Memr[ynew+col-1], Memc[sl_getstr(sl,1)],
				SZ_LINE)
			} else {
			    call imp_tran (gp, x, y, px, py, Memr[xnew], nlines,
				ncols)
			    line = px
			    call plt_wcscoord (im, mw, ct, wcstype, format, col,
				line, Memr[ynew+line-1], Memc[sl_getstr(sl,1)],
				SZ_LINE)
			}
			sline = 1
			call printf (Memc[sl_getstr(sl,sline)])

		    case '?':
			# Print command summary.
			call gpagefile (gp, KEYSFILE, "implot cursor commands")

		    case ':':
			# Command mode.
			for (ip=1;  IS_WHITE (command[ip]);  ip=ip+1)
			    ;
			if (command[ip] == 'o') {
			    overplot = true
			    ip = ip + 1
			}

			switch (command[ip]) {
			case 'a':
			    # Set number of lines or columns to average.
			    ip = ip + 1
			    if (ctoi (command, ip, p_navg) <= 0) {
				call printf (bell)
				p_navg = 1
			    }

			case 'i':
			    # Open a different image.
			    call mw_close (mw)
			    call imunmap (im)
			    ip = ip + 1
			    while (IS_WHITE (command[ip]))
				ip = ip + 1

			    iferr (im = immap (command[ip], READ_ONLY, 0)) {
				call erract (EA_WARN)
				im = immap (image, READ_ONLY, 0)
				mw = mw_openim (im)
				call mw_seti (mw, MW_USEAXMAP, NO)
				ct = mw_sctran (mw, "logical", wcstype, 0)

			    } else if (IM_NDIM(im) <= 0) {
				call eprintf ("image has no pixels\n")
				im = immap (image, READ_ONLY, 0)
				mw = mw_openim (im)
				call mw_seti (mw, MW_USEAXMAP, NO)
				ct = mw_sctran (mw, "logical", wcstype, 0)

			    } else {
				mw = mw_openim (im)
				call mw_seti (mw, MW_USEAXMAP, NO)
				ct = mw_sctran (mw, "logical", wcstype, 0)

				ncols = IM_LEN(im,1)
				if (IM_NDIM(im) > 1)
				    nlines = IM_LEN(im,2)
				else
				    nlines = 1

				npix   = max (ncols, nlines)
				call strcpy (command[ip], image, SZ_FNAME)
				call realloc (xold, npix, TY_REAL)
				call realloc (yold, npix, TY_REAL)
				call realloc (xnew, npix, TY_REAL)
				call realloc (ynew, npix, TY_REAL)
			    }

			case 'w':
			    # Change wcs type.
			    call mw_ctfree (ct)
			    ip = ip + 1
			    while (IS_WHITE (command[ip]))
				ip = ip + 1

			    iferr {
				ct = mw_sctran (mw, "logical", command[ip], 0)
				call strcpy (command[ip], wcstype, SZ_FNAME)
			    } then {
				call erract (EA_WARN)
				ct = mw_sctran (mw, "logical", wcstype, 0)
			    } else {
				# Only replot if WCS command succeeds,
				# otherwise the error message is lost.
				if (lineplot)
				    goto line_
				else
				    goto col_
			    }

			case 'f':
			    # Change label format.
			    ip = ip + 1
			    while (IS_WHITE (command[ip]))
				ip = ip + 1
			    if (command[ip] == '%') {
				call strcpy (command[ip], format, SZ_FNAME)
				call strcpy (format, fmt, SZ_FNAME)
				if (lineplot)
				    goto replotline_
				else
				    goto replotcol_
			    } else if (format[1] == '%') {
				call strcpy (command[ip], format, SZ_FNAME)
				if (lineplot)
				    goto line_
				else
				    goto col_
			    }

			case 'l':
			    if (command[ip+1] != 'o') {
				# Plot a line.
				ip = ip + 1
				if (ctoi (command, ip, i1) <= 0) {
				    call printf (bell)
				    next
				} else if (ctoi (command, ip, i2) <= 0) {
				    line = max(1, min(nlines, i1))
				    navg = p_navg
				    line = line - (navg - 1) / 2
				    goto line_
				} else {
				    i1 = max(1, min(nlines, i1))
				    i2 = max(1, min(nlines, i2))
				    line = min (i1, i2)
				    navg = max (1, abs (i2 - i1) + 1)
				    goto line_
				}
			    } else {
				# Enable/disable log scaling.
				while (IS_ALPHA(command[ip]))
				    ip = ip + 1
				logscale = (command[ip] == '+')
			    }

			case 'c':
			    # Plot a column.
			    ip = ip + 1
			    if (ctoi (command, ip, i1) <= 0) {
				call printf (bell)
				next
			    } else if (ctoi (command, ip, i2) <= 0) {
				col = max(1, min(ncols, i1))
				navg = p_navg
				col = col - (navg - 1) / 2
				goto col_
			    } else {
				i1 = max(1, min(ncols, i1))
				i2 = max(1, min(ncols, i2))
				col  = min (i1, i2)
				navg = max (1, abs (i2 - i1) + 1)
				goto col_
			    }

			case 's':
			    if (command[ip+1] == 'o') {
				# Use only linetype=1 (solid).
				linetype = 1
				linestep = 0
				color = 1
			    } else {
				# Set step size.
				while (IS_ALPHA (command[ip]))
				    ip = ip + 1
				if (ctoi (command, ip, step) <= 0) {
				    call printf (bell)
				    step = 1
				}
			    }
			
			case 'x':
			    # Fix window in X and replot vector.  If no args
			    # are given, unfix the window.

			    ip = ip + 1
			    if (ctor (command, ip, x1) <= 0) {
				rescale[1]   = true
				p_rescale[1] = true
			    } else if (ctor (command, ip, x2) <= 0) {
				call printf (bell)
			    } else {
				x1 = plt_iformatr (x1, fmt)
				x2 = plt_iformatr (x2, fmt)
				call imp_swind (x1, x2, INDEF, INDEF)
				rescale[1]   = false
				p_rescale[1] = false
			    }

			    if (lineplot)
				goto replotline_
			    else
				goto replotcol_
			
			case 'y':
			    # Fix window in Y and replot vector.  If no args
			    # are given, unfix the window.

			    ip = ip + 1
			    if (ctor (command, ip, y1) <= 0) {
				rescale[2]   = true
				p_rescale[2] = true
			    } else if (ctor (command, ip, y2) <= 0) {
				call printf (bell)
			    } else {
				y1 = plt_iformatr (y1, fmt)
				y2 = plt_iformatr (y2, fmt)
				call imp_swind (INDEF, INDEF, y1, y2)
				p_rescale[2] = false
				rescale[2]   = false
			    }

			    if (lineplot)
				goto replotline_
			    else
				goto replotcol_

			case 'n':
			    ip = ip + 1
			    if (command[ip] == 'x') {
				while (IS_ALPHA(command[ip]))
				    ip = ip + 1
				if (ctoi (command, ip, xnticks) <= 0)
				    xnticks = -1
			    } else if (command[ip] == 'y') {
				while (IS_ALPHA(command[ip]))
				    ip = ip + 1
				if (ctoi (command, ip, ynticks) <= 0)
				    ynticks = -1
			    } else
				call printf (bell)

			default:
			    call printf (bell)
			}

		    case '/':
			# Scroll or rewrite the status line.

			sline = sline + 1
			call printf (Memc[sl_getstr(sl,sline)])

		    default:
			call printf (bell)
		    }
		}
	    } then
		call erract (EA_WARN)

	    call mfree (xnew, TY_REAL)
	    call mfree (ynew, TY_REAL)
	    call mfree (xold, TY_REAL)
	    call mfree (yold, TY_REAL)
	    if (sl != NULL)
		call sl_free (sl)

	    if (mw != NULL)
		call mw_close (mw)
	    if (im != NULL)
		call imunmap (im)

	    if (key == 'q')
		break
	}

	call gclose (gp)
	call imtclose (list)
end


# IMP_GETVECTOR -- Get a data vector, i.e., line or column or average of
# lines and columns.

procedure imp_getvector (im, mw, ct, wcstype, x, y, xlabel, format, linecol,
	navg, lineplot)

pointer	im			# image descriptor
pointer	mw			# mwcs descriptor
pointer	ct			# coordinate descriptor
char	wcstype[ARB]		# WCS type
pointer	x, y			# output vector
char	xlabel[SZ_FNAME]	# WCS label
char	format[SZ_FNAME]	# WCS format
int	linecol			# line or column number
int	navg			# number of lines or columns to be averaged
bool	lineplot		# true if line is to be extracted

real	norm
pointer	sp, axvals, buf, off
int	x1, x2, y1, y2
int	nx, ny, width, i, ndim
real	asumr()
pointer	imgl2r(), imgs2r(), imgl1r(), imgs1r()
errchk	imgl2r, imgs2r, imgl1r, imgs1r, plt_wcs

begin
	call smark (sp)
	call salloc (axvals, IM_NDIM(im), TY_REAL)

	call strcpy (wcstype, xlabel, SZ_FNAME)

	ndim = IM_NDIM(im)
	nx = IM_LEN(im,1)
	if (ndim > 1)
	    ny = IM_LEN(im,2)
	else
	    ny = 1
	call amovkr (1., Memr[axvals], ndim)

	if (lineplot) {
	    # Extract a line vector.

	    x1 = 1
	    x2 = nx
	    y1 = max(1, min (ny, linecol))
	    y2 = max(1, min (ny, linecol + navg - 1))

	    Memr[axvals+1] = (y1 + y2) / 2.
	    call plt_wcs (im, mw, ct, 1, Memr[axvals], real(x1), real(x2),
		Memr[x], nx, xlabel, format, SZ_FNAME)

	    if (ndim == 1)
		call amovr (Memr[imgl1r(im)], Memr[y], nx)

	    else {
		# Compute sum.
		call aclrr (Memr[y], nx)
		do i = y1, y2
		    call aaddr (Memr[imgl2r(im,i)], Memr[y], Memr[y], nx)

		# Normalize.
		width = y2 - y1 + 1
		if (width > 1)
		    call amulkr (Memr[y], 1. / width, Memr[y], nx)
	    }

	} else {
	    # Extract a column vector.

	    x1 = max(1, min(nx, linecol))
	    x2 = max(1, min(nx, linecol + navg - 1))
	    y1 = 1
	    y2 = ny

	    Memr[axvals] = (x1 + x2) / 2.
	    call plt_wcs (im, mw, ct, 2, Memr[axvals], real(y1), real(y2),
		Memr[x], ny, xlabel, format, SZ_LINE)

	    width = x2 - x1 + 1
	    norm  = 1.0 / real(width)

	    if (width > 1) {
		call aclrr (Memr[y], ny)
		do i = y1, y2 {
		    if (ndim == 1) {
			buf = imgs1r (im, x1, x2)
			off = buf
		    } else if (nx > 1024) {
			buf = imgs2r (im, x1, x2, i, i)
			off = buf
		    } else {
			buf = imgl2r (im, i)
			off = buf + x1 - 1
		    }
		    Memr[y+i-1] = asumr (Memr[off], width) * norm
		}
	    } else {
		buf = imgs2r (im, x1, x2, y1, y2)
		call amovr (Memr[buf], Memr[y], ny)
	    }
	}

	call sfree (sp)
end


# IMP_PLOTVECTOR -- Plot a line or column vector.

procedure imp_plotvector (gp, im, xv, yv, nx, ny, y, navg, lineplot, rescale,
	image, xlabel)

pointer	gp			# graphics descriptor
pointer	im			# image descriptor
real	xv[ARB]			# coordinate vector
real	yv[ARB]			# data  vector
int	nx			# number of pixels in vector
int	ny			# number of pixels on plot-Y axis
real	y			# position on plot Y-axis
int	navg			# number of lines or columns averaged
bool	lineplot		# are we plotting a line or a column
bool	rescale[2]		# rescale plot
char	image[ARB]		# image name
char	xlabel[ARB]		# X label

real	junkr
int	i, i1, i2, npix, maxch
pointer	sp, ip, plot_title, op
bool	fp_equalr()

real	x1, x2, y1, y2
common	/implcom/ x1, x2, y1, y2

begin
	call smark (sp)
	call salloc (plot_title, SZ_PLOTTITLE, TY_CHAR)

	# Format the plot title, starting with the system banner.
	call sysid (Memc[plot_title], SZ_PLOTTITLE)
	for (op=plot_title;  Memc[op] != '\n' && Memc[op] != EOS;  op=op+1)
	    ;
	Memc[op] = '\n'
	op = op + 1
	maxch = SZ_PLOTTITLE - (op - plot_title)
	# Format the remainder of the plot title.

	if (IM_LEN(im,2) <= 1) {
	    # Plot of a one-dim image.
	    call strcpy (IM_TITLE(im), Memc[op], maxch)

	} else if (navg > 1) {
	    call sprintf (Memc[op], maxch, "Average of %s %d to %d of %s\n%s")
		if (lineplot) {
		    call pargstr ("lines")
		    npix = IM_LEN(im,2)
		} else {
		    call pargstr ("columns")
		    npix = IM_LEN(im,1)
		}

		i1 = max (1, min (npix, nint (y)))
		i2 = max (1, min (npix, nint (y) + navg - 1))
		call pargi (i1)
		call pargi (i2)
		call pargstr (image)
		call pargstr (IM_TITLE(im))

	} else {
	    call sprintf (Memc[op], maxch, "%s %d of %s\n%s")
		if (lineplot)
		    call pargstr ("Line")
		else
		    call pargstr ("Column")
		call pargi (nint(y))
		call pargstr (image)
		call pargstr (IM_TITLE(im))
	}


	# Delete trailing newline and any whitespace from image title string.
	# Trailing whitespace causes plot title to not be drawn centered on
	# plot.

	for (ip=plot_title;  Memc[ip] != EOS;  ip=ip+1)
	    ;
	ip = ip - 1
	if (ip > plot_title && Memc[ip] == '\n')
	    ip = ip - 1
	while (ip > plot_title && IS_WHITE(Memc[ip]))
	    ip = ip - 1
	Memc[ip+1] = EOS

	# Autoscale the plot in X and or Y if so indicated.
	if (rescale[1])
	    call gascale (gp, xv, nx, 1)
	else
	    call gswind (gp, x1, x2, INDEF, INDEF)

	call ggwind (gp, x1, x2, junkr, junkr)
	junkr = min (x1, x2)
	for (i1=1; i1<nx && xv[i1] < junkr; i1=i1+1)
	    ;
	junkr = max (x1, x2)
	for (i2=nx; i2>1 && xv[i2] > junkr; i2=i2-1)
	    ;
	if (i2 < i1) {
	    i = i1
	    i1 = i2
	    i2 = i
	}
	npix = max (1, i2 - i1 + 1)

	if (rescale[2]) {
	    if (npix < 2)
		call gascale (gp, yv[i1], nx, 2)
	    else
		call gascale (gp, yv[i1], npix, 2)
	} else
	    call gswind (gp, INDEF, INDEF, y1, y2)

	call ggwind (gp, x1, x2, y1, y2)

	# If the image is two dimensional plot the position within the image
	# of the plotted vector on the plot-Y axis (which may refer to either
	# X or Y on the image).

	if (IM_LEN(im,2) > 1) {
	    # Draw all but right axes.
	    if (fp_equalr (y1, y2)) {
		y1 = 0.99 * y1
		y2 = 1.01 * y2
		call gswind (gp, INDEF, INDEF, y1, y2)
	    }
	    call gseti (gp, G_YDRAWAXES, 1)
	    call glabax (gp, Memc[plot_title], xlabel, "")

	    # Draw right axis (pixel scale)
	    call ggwind (gp, x1, x2, y1, y2)
	    call gswind (gp, 1., real (nx), 1., real (ny))
	    call gseti (gp, G_XDRAWAXES, 0)
	    call gseti (gp, G_YDRAWAXES, 2)
	    call glabax (gp, "", "", "")
	    call gswind (gp, x1, x2, y1, y2)

	    # Mark position on Y axis.
	    if (abs(y) > .001)
		call imp_markpos (gp, nint(y), ny)
	} else {
	    call glabax (gp, Memc[plot_title], xlabel, "")
	    call ggwind (gp, x1, x2, y1, y2)
	}

	# Draw data vector.
	call gpline (gp, xv, yv, nx)

	call sfree (sp)
end


# IMP_SWIND -- Set all or part of the plotting window if autoscaling is not
# desired.

procedure imp_swind (n_x1, n_x2, n_y1, n_y2)

real	n_x1, n_x2		# range of world coords in X
real	n_y1, n_y2		# range of world coords in Y

real	x1, x2, y1, y2
common	/implcom/ x1, x2, y1, y2

begin
	if (!IS_INDEF(n_x1))
	    x1 = n_x1
	if (!IS_INDEF(n_x2))
	    x2 = n_x2
	if (!IS_INDEF(n_y1))
	    y1 = n_y1
	if (!IS_INDEF(n_y2))
	    y2 = n_y2
end


# IMP_REDRAW -- Erase the old vector and draw a new one in its place.

procedure imp_redraw (gp, xold, yold, xnew, ynew, npix)

pointer	gp			# graphics descriptor
real	xold[ARB], yold[ARB]	# old data vector
real	xnew[ARB], ynew[ARB]	# new data vector
int	npix			# length of the data vectors

int	i, n

begin
	# Erase the old vector and redraw the new in its place, in segments
	# of length SEGLEN.  These segments must overlap by one pixel to
	# produce a continuous output polyline.

	do i = 1, npix, SEGLEN {
	    n  = min (SEGLEN + 1, npix - i + 1)

	    # Erase next segment of old vector.
	    call gseti (gp, G_PLTYPE, 0)
	    call gpline (gp, xold[i], yold[i], n)

	    # Plot same segment of new vector.
	    call gseti (gp, G_PLTYPE, 1)
	    call gseti (gp, G_PLCOLOR, 1)
	    call gpline (gp, xnew[i], ynew[i], n)
	}
end


# IMP_MARKPOS -- Mark the line or column number on the right axis of the plot.

procedure imp_markpos (gp, line, nlines)

pointer	gp			# graphics descriptor
int	line			# line or column
int	nlines			# number of lines or columns
real	y, x1, x2, y1, y2

begin
	if (nlines < 2)
	    return

	call ggwind (gp, x1, x2, y1, y2)
	y = (y2 - y1) / (nlines - 1) * (line - 1) + y1
	call gmark (gp, x2, y, GM_PLUS, 3., 4.)
end


# IMP_TRAN -- Transform cursor coordinate to line and column in image.

procedure imp_tran (gp, x, y, px, py, xvec, nx, ny)

pointer	gp			# graphics descriptor
real	x, y			# cursor coordinate
real	px, py			# image coordinate
real	xvec[nx]		# x vector
int	nx, ny			# number of columns and lines

int	i
real	x1, x2, y1, y2, diff, diffmin
bool	fp_equalr()

begin
	call ggwind (gp, x1, x2, y1, y2)
	if (fp_equalr (y1, y2))
	    py = nint (ny / 2.)
	else
	    py = nint ((ny - 1) / (y2 - y1) * (y - y1) + 1)

	px = 1
	diffmin = abs (x - xvec[1])
	do i = 2, nx {
	    diff = abs (x - xvec[i])
	    if (diff < diffmin) {
		px = i
		diffmin = diff
	    }
	}
end