aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/lib/geogmapi.x
blob: 9dc6361064a7b220b6eaa8319c96c4f4e25f368c (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <error.h>
include <math.h>
include <math/gsurfit.h>
include	<gset.h>
include "geomap.h"
include "geogmap.h"

define	GHELPFILE 	"images$lib/geomap.key"
define	CHELPFILE 	"images$lib/coomap.key"



# GEO_MGFIT -- Fit the surface using interactive graphics.

procedure geo_mgfitr (gd, fit, sx1, sy1, sx2, sy2, xref, yref, xin,
        yin, wts, npts, xerrmsg, yerrmsg, maxch)

pointer	gd		#I graphics file descriptor
pointer	fit		#I pointer to the fit structure
pointer	sx1		#I pointer to the linear x surface fit
pointer	sy1		#I pointer to the linear y surface fit
pointer	sx2		#I pointer to higher order x surface fit
pointer	sy2		#I pointer to higher order y surface fit
real	xref[npts]	#I the x reference coordinates
real	yref[npts]	#I the y reference coordinates
real	xin[npts]	#I input x coordinates
real	yin[npts]	#I input y coordinates
real	wts[npts]	#I array of weights
int	npts		#I number of data points
char	xerrmsg[ARB]	#O the output x fit error message 
char	yerrmsg[ARB]	#O the output x fit error message 
int	maxch		#I the size of the error messages

char	errstr[SZ_LINE]
int	newgraph, delete, wcs, key, errcode
pointer	sp, w, gfit, xresid, yresid, cmd
pointer	gt1, gt2, gt3, gt4, gt5
real	wx, wy
real	xshift, yshift, xscale, yscale, thetax, thetay

int	clgcur(), errget()
pointer	gt_init()

errchk	geo_fxyr(), geo_mrejectr(), geo_fthetar()
errchk	geo_fmagnifyr(), geo_flinearr()

begin
	# Initialize gfit structure and working space.
	call smark (sp)
	call salloc (gfit, LEN_GEOGRAPH, TY_STRUCT)
	call salloc (xresid, npts, TY_REAL)
	call salloc (yresid, npts, TY_REAL)
	call salloc (w, npts, TY_REAL)
	call salloc (cmd, SZ_LINE, TY_CHAR)

	# Do initial fit.
	iferr {
	    switch (GM_FIT(fit)) {
	    case GM_ROTATE:
	        call geo_fthetar (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Memr[xresid], Memr[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    case GM_RSCALE:
	        call geo_fmagnifyr (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Memr[xresid], Memr[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    case GM_RXYSCALE:
	        call geo_flinearr (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Memr[xresid], Memr[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    default:
	        call geo_fxyr (fit, sx1, sx2, xref, yref, xin, wts,
		    Memr[xresid], npts, YES, xerrmsg, maxch)
	        call geo_fxyr (fit, sy1, sy2, xref, yref, yin, wts,
		    Memr[yresid], npts, NO, yerrmsg, maxch)
	    }
	    if (GM_MAXITER(fit) <= 0 || IS_INDEFD(GM_REJECT(fit)))
	        GM_NREJECT(fit) = 0
	    else
	        call geo_mrejectr (fit, sx1, sy1, sx2, sy2, xref, yref, xin,
		    yin, wts, Memr[xresid], Memr[yresid], npts, xerrmsg,
		    maxch, yerrmsg, maxch)
	} then {
	    call sfree (sp)
	    if (GM_PROJECTION(fit) == GM_NONE)
	        call error (2, "Too few points for X and Y fits.")
	    else
	        call error (2, "Too few points for XI and ETA fits.")
	}

	GG_NEWFUNCTION(gfit) = NO
	GG_FITERROR(gfit) = NO
	errcode = OK

	# Set up plotting defaults.
	GG_PLOTTYPE(gfit) = FIT
	GG_OVERPLOT(gfit) = NO
	GG_CONSTXY(gfit) = YES
	newgraph = NO

	# Allocate graphics tools.
	gt1 = gt_init ()
	gt2 = gt_init ()
	gt3 = gt_init ()
	gt4 = gt_init ()
	gt5 = gt_init ()

	# Set the plot title and x and y axis labels.
	call geo_gtset (FIT, gt1, fit)
	call geo_gtset (XXRESID, gt2, fit)
	call geo_gtset (XYRESID, gt3, fit)
	call geo_gtset (YXRESID, gt4, fit)
	call geo_gtset (YYRESID, gt5, fit)

	# Make the first plot.
	call gclear (gd)
	call geo_label (FIT, gt1, fit)
	call geo_1graphr (gd, gt1, fit, gfit, xref, yref, xin, yin, wts,
	    npts)
	if (GG_CONSTXY(gfit) == YES)
	    call geo_conxyr (gd, fit, sx1, sy1, sx2, sy2)
	call printf ("%s  %s\n")
	    call pargstr (xerrmsg)
	    call pargstr (yerrmsg)

	# Read the cursor commands.
	call amovr (wts, Memr[w], npts)
	while (clgcur ("cursor", wx, wy, wcs, key, Memc[cmd], SZ_LINE) != EOF) {

	    switch (key) {

	    case 'q':
		call amovr (Memr[w], wts, npts)
		break

	    case '?':
		if (GM_PROJECTION(fit) == GM_NONE)
		    call gpagefile (gd, GHELPFILE, "")
		else
		    call gpagefile (gd, CHELPFILE, "")

	    case ':':
		call geo_colon (gd, fit, gfit, Memc[cmd], newgraph)
		switch (GG_PLOTTYPE(gfit)) {
		case FIT:
		    call gt_colon (Memc[cmd], gd, gt1, newgraph)
		case XXRESID:
		    call gt_colon (Memc[cmd], gd, gt2, newgraph)
		case XYRESID:
		    call gt_colon (Memc[cmd], gd, gt3, newgraph)
		case YXRESID:
		    call gt_colon (Memc[cmd], gd, gt4, newgraph)
		case YYRESID:
		    call gt_colon (Memc[cmd], gd, gt5, newgraph)
		}

	    case 'l':
		if (GG_FITERROR(gfit) == NO) {
		    call geo_lcoeffr (sx1, sy1, xshift, yshift, xscale, yscale,
		        thetax, thetay)
		    call printf ("xshift: %.2f yshift: %.2f ")
		        call pargr (xshift)
		        call pargr (yshift)
		    call printf ("xmag: %0.3g ymag: %0.3g ")
		        call pargr (xscale)
		        call pargr (yscale)
		    call printf ("xrot: %.2f yrot: %.2f\n")
		        call pargr (thetax)
		        call pargr (thetay)
		}

	    case 't':
		if (GG_FITERROR(gfit) == NO && GG_PLOTTYPE(gfit) == FIT)
		    call geo_lxyr (gd, fit, sx1, sy1, sx2, sy2, xref, yref,
		        xin, yin, npts, wx, wy)

	    case 'c':
		if (GG_CONSTXY(gfit) == YES)
		    GG_CONSTXY(gfit) = NO
		else if (GG_CONSTXY(gfit) == NO)
		    GG_CONSTXY(gfit) = YES

	    case 'd', 'u':
		if (key == 'd')
		    delete = YES
		else
		    delete = NO

		switch (GG_PLOTTYPE(gfit)) {
		case FIT:
		    call geo_1deleter (gd, xin, yin, Memr[w], wts, npts, wx,
		        wy, delete)
		case XXRESID:
		    call geo_2deleter (gd, xref, Memr[xresid], Memr[w], wts,
		        npts, wx, wy, delete)
		case XYRESID:
		    call geo_2deleter (gd, yref, Memr[xresid], Memr[w], wts,
		        npts, wx, wy, delete)
		case YXRESID:
		    call geo_2deleter (gd, xref, Memr[yresid], Memr[w], wts,
		        npts, wx, wy, delete)
		case YYRESID:
		    call geo_2deleter (gd, yref, Memr[yresid], Memr[w], wts,
		        npts, wx, wy, delete)
		}

		GG_NEWFUNCTION(gfit) = YES

	    case 'g':
		if (GG_PLOTTYPE(gfit) != FIT)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = FIT

	    case 'x':
		if (GG_PLOTTYPE(gfit) != XXRESID)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = XXRESID

	    case 'r':
		if (GG_PLOTTYPE(gfit) != XYRESID)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = XYRESID

	    case 'y':
		if (GG_PLOTTYPE(gfit) != YXRESID)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = YXRESID

	    case 's':
		if (GG_PLOTTYPE(gfit) != YYRESID)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = YYRESID

	    case 'f':
		# do fit
		if (GG_NEWFUNCTION(gfit) == YES) {
		    iferr {
			switch (GM_FIT(fit)) {
			case GM_ROTATE:
		            call geo_fthetar (fit, sx1, sy1, xref, yref, xin,
			        yin, Memr[w], Memr[xresid], Memr[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			case GM_RSCALE:
		            call geo_fmagnifyr (fit, sx1, sy1, xref, yref, xin,
			        yin, Memr[w], Memr[xresid], Memr[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			case GM_RXYSCALE:
		            call geo_flinearr (fit, sx1, sy1, xref, yref, xin,
			        yin, Memr[w], Memr[xresid], Memr[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			default:
		            call geo_fxyr (fit, sx1, sx2, xref, yref, xin,
			        Memr[w], Memr[xresid], npts, YES,
				xerrmsg, maxch) 
		            call geo_fxyr (fit, sy1, sy2, xref, yref, yin,
			        Memr[w], Memr[yresid], npts, NO,
				yerrmsg, maxch) 
			}
		        if (GM_MAXITER(fit) <= 0 || IS_INDEFD(GM_REJECT(fit)))
			    GM_NREJECT(fit) = 0
		        else
			    call geo_mrejectr (fit, sx1, sy1, sx2, sy2, xref,
			        yref, xin, yin, Memr[w], Memr[xresid],
				Memr[yresid], npts, xerrmsg, maxch,
				yerrmsg, maxch)
			GG_NEWFUNCTION(gfit) = NO
			GG_FITERROR(gfit) = NO
			errcode = OK
		    } then {
	    		errcode = errget (errstr, SZ_LINE)
	    		call printf ("%s\n")
			    call pargstr (errstr)
			GG_FITERROR(gfit) = YES
		    }
		}

		# plot new graph
		if (GG_FITERROR(gfit) == YES)
		    newgraph = NO
		else
		    newgraph = YES

	    case 'o':
		GG_OVERPLOT(gfit) = YES

	    default:
		call printf ("\07")

	    }

	    if (newgraph == YES) {
		switch (GG_PLOTTYPE(gfit)) {
		case FIT:
		    call geo_label (FIT, gt1, fit)
	    	    call geo_1graphr (gd, gt1, fit, gfit, xref, yref, xin, yin,
		        Memr[w], npts)
		    if (GG_CONSTXY(gfit) == YES)
			call geo_conxyr (gd, fit, sx1, sy1, sx2, sy2)
		case XXRESID:
		    call geo_label (XXRESID, gt2, fit)
	    	    call geo_2graphr (gd, gt2, fit, gfit, xref, Memr[xresid],
		        Memr[w], npts)
		case XYRESID:
		    call geo_label (XYRESID, gt3, fit)
	    	    call geo_2graphr (gd, gt3, fit, gfit, yref, Memr[xresid],
		         Memr[w], npts)
		case YXRESID:
		    call geo_label (YXRESID, gt4, fit)
	    	    call geo_2graphr (gd, gt4, fit, gfit, xref, Memr[yresid],
		         Memr[w], npts)
		case YYRESID:
		    call geo_label (YYRESID, gt5, fit)
	    	    call geo_2graphr (gd, gt5, fit, gfit, yref, Memr[yresid],
		         Memr[w], npts)
		}
	        call printf ("%s  %s\n")
	    	    call pargstr (xerrmsg)
	    	    call pargstr (yerrmsg)
		newgraph = NO
	    }
	}

	# Free space.
	call gt_free (gt1)
	call gt_free (gt2)
	call gt_free (gt3)
	call gt_free (gt4)
	call gt_free (gt5)
	call sfree (sp)

	# Call an error if appropriate.
	if (errcode > 0)
	    call error (2, errstr)
end

# GEO_LCOEFF -- Print the coefficents of the linear portion of the
# fit, xshift, yshift, xexpansion, yexpansion, x and y rotations.

procedure geo_lcoeffr (sx, sy, xshift, yshift, xscale, yscale, xrot, yrot)

pointer sx              #I pointer to the x surface fit
pointer sy              #I pointer to the y surface fit
real   xshift          #O output x shift
real   yshift          #O output y shift
real   xscale          #O output x scale
real   yscale          #O output y scale
real   xrot            #O rotation of point on x axis
real   yrot            #O rotation of point on y axis

int     nxxcoeff, nxycoeff, nyxcoeff, nyycoeff
pointer sp, xcoeff, ycoeff
real   xxrange, xyrange, xxmaxmin, xymaxmin
real   yxrange, yyrange, yxmaxmin, yymaxmin
real   a, b, c, d

bool    fp_equalr()
int     gsgeti()
real    gsgetr()

begin
        # Allocate working space.
        call smark (sp)
        call salloc (xcoeff, gsgeti (sx, GSNCOEFF), TY_REAL)
        call salloc (ycoeff, gsgeti (sy, GSNCOEFF), TY_REAL)

        # Get coefficients and numbers of coefficients.
        call gscoeff (sx, Memr[xcoeff], nxxcoeff)
        call gscoeff (sy, Memr[ycoeff], nyycoeff)
        nxxcoeff = gsgeti (sx, GSNXCOEFF)
        nxycoeff = gsgeti (sx, GSNYCOEFF)
        nyxcoeff = gsgeti (sy, GSNXCOEFF)
        nyycoeff = gsgeti (sy, GSNYCOEFF)

        # Get the data range.
        if (gsgeti (sx, GSTYPE) != GS_POLYNOMIAL) {
            xxrange = (gsgetr (sx, GSXMAX) - gsgetr (sx, GSXMIN)) / 2.0
            xxmaxmin = - (gsgetr (sx, GSXMAX) + gsgetr (sx, GSXMIN)) / 2.0
            xyrange = (gsgetr (sx, GSYMAX) - gsgetr (sx, GSYMIN)) / 2.0
            xymaxmin = - (gsgetr (sx, GSYMAX) + gsgetr (sx, GSYMIN)) / 2.0
        } else {
            xxrange = real(1.0)
            xxmaxmin = real(0.0)
            xyrange = real(1.0)
            xymaxmin = real(0.0)
        }

        if (gsgeti (sy, GSTYPE) != GS_POLYNOMIAL) {
            yxrange = (gsgetr (sy, GSXMAX) - gsgetr (sy, GSXMIN)) / 2.0
            yxmaxmin = - (gsgetr (sy, GSXMAX) + gsgetr (sy, GSXMIN)) / 2.0
            yyrange = (gsgetr (sy, GSYMAX) - gsgetr (sy, GSYMIN)) / 2.0
            yymaxmin = - (gsgetr (sy, GSYMAX) + gsgetr (sy, GSYMIN)) / 2.0
        } else {
            yxrange = real(1.0)
            yxmaxmin = real(0.0)
            yyrange = real(1.0)
            yymaxmin = real(0.0)
        }

        # Get the shifts.
        xshift = Memr[xcoeff] + Memr[xcoeff+1] * xxmaxmin / xxrange +
            Memr[xcoeff+2] * xymaxmin / xyrange
        yshift = Memr[ycoeff] + Memr[ycoeff+1] * yxmaxmin / yxrange +
            Memr[ycoeff+2] * yymaxmin / yyrange

        # Get the rotation and scaling parameters and correct for normalization.
        if (nxxcoeff > 1)
            a = Memr[xcoeff+1] / xxrange
        else
            a = real(0.0)
        if (nxycoeff > 1)
            b = Memr[xcoeff+nxxcoeff] / xyrange
        else
            b = real(0.0)
        if (nyxcoeff > 1)
            c = Memr[ycoeff+1] / yxrange
        else
            c = real(0.0)
        if (nyycoeff > 1)
            d = Memr[ycoeff+nyxcoeff] / yyrange
        else
            d = real(0.0)

        # Get the magnification factors.
        xscale = sqrt (a * a + c * c)
        yscale = sqrt (b * b + d * d)

        # Get the x and y axes rotation factors.
        if (fp_equalr (a, real(0.0)) && fp_equalr (c, real(0.0)))
            xrot = real(0.0)
        else
            xrot = RADTODEG (atan2 (-c, a))
        if (xrot < real(0.0))
            xrot = xrot + real(360.0)

        if (fp_equalr (b, real(0.0)) && fp_equalr (d, real(0.0)))
            yrot = real(0.0)
        else
            yrot = RADTODEG (atan2 (b, d))
        if (yrot < real(0.0))
            yrot = yrot + real(360.0)

        call sfree (sp)
end



# GEO_MGFIT -- Fit the surface using interactive graphics.

procedure geo_mgfitd (gd, fit, sx1, sy1, sx2, sy2, xref, yref, xin,
        yin, wts, npts, xerrmsg, yerrmsg, maxch)

pointer	gd		#I graphics file descriptor
pointer	fit		#I pointer to the fit structure
pointer	sx1		#I pointer to the linear x surface fit
pointer	sy1		#I pointer to the linear y surface fit
pointer	sx2		#I pointer to higher order x surface fit
pointer	sy2		#I pointer to higher order y surface fit
double	xref[npts]	#I the x reference coordinates
double	yref[npts]	#I the y reference coordinates
double	xin[npts]	#I input x coordinates
double	yin[npts]	#I input y coordinates
double	wts[npts]	#I array of weights
int	npts		#I number of data points
char	xerrmsg[ARB]	#O the output x fit error message 
char	yerrmsg[ARB]	#O the output x fit error message 
int	maxch		#I the size of the error messages

char	errstr[SZ_LINE]
int	newgraph, delete, wcs, key, errcode
pointer	sp, w, gfit, xresid, yresid, cmd
pointer	gt1, gt2, gt3, gt4, gt5
real	wx, wy
double	xshift, yshift, xscale, yscale, thetax, thetay

int	clgcur(), errget()
pointer	gt_init()

errchk	geo_fxyd(), geo_mrejectd(), geo_fthetad()
errchk	geo_fmagnifyd(), geo_flineard()

begin
	# Initialize gfit structure and working space.
	call smark (sp)
	call salloc (gfit, LEN_GEOGRAPH, TY_STRUCT)
	call salloc (xresid, npts, TY_DOUBLE)
	call salloc (yresid, npts, TY_DOUBLE)
	call salloc (w, npts, TY_DOUBLE)
	call salloc (cmd, SZ_LINE, TY_CHAR)

	# Do initial fit.
	iferr {
	    switch (GM_FIT(fit)) {
	    case GM_ROTATE:
	        call geo_fthetad (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Memd[xresid], Memd[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    case GM_RSCALE:
	        call geo_fmagnifyd (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Memd[xresid], Memd[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    case GM_RXYSCALE:
	        call geo_flineard (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Memd[xresid], Memd[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    default:
	        call geo_fxyd (fit, sx1, sx2, xref, yref, xin, wts,
		    Memd[xresid], npts, YES, xerrmsg, maxch)
	        call geo_fxyd (fit, sy1, sy2, xref, yref, yin, wts,
		    Memd[yresid], npts, NO, yerrmsg, maxch)
	    }
	    if (GM_MAXITER(fit) <= 0 || IS_INDEFD(GM_REJECT(fit)))
	        GM_NREJECT(fit) = 0
	    else
	        call geo_mrejectd (fit, sx1, sy1, sx2, sy2, xref, yref, xin,
		    yin, wts, Memd[xresid], Memd[yresid], npts, xerrmsg,
		    maxch, yerrmsg, maxch)
	} then {
	    call sfree (sp)
	    if (GM_PROJECTION(fit) == GM_NONE)
	        call error (2, "Too few points for X and Y fits.")
	    else
	        call error (2, "Too few points for XI and ETA fits.")
	}

	GG_NEWFUNCTION(gfit) = NO
	GG_FITERROR(gfit) = NO
	errcode = OK

	# Set up plotting defaults.
	GG_PLOTTYPE(gfit) = FIT
	GG_OVERPLOT(gfit) = NO
	GG_CONSTXY(gfit) = YES
	newgraph = NO

	# Allocate graphics tools.
	gt1 = gt_init ()
	gt2 = gt_init ()
	gt3 = gt_init ()
	gt4 = gt_init ()
	gt5 = gt_init ()

	# Set the plot title and x and y axis labels.
	call geo_gtset (FIT, gt1, fit)
	call geo_gtset (XXRESID, gt2, fit)
	call geo_gtset (XYRESID, gt3, fit)
	call geo_gtset (YXRESID, gt4, fit)
	call geo_gtset (YYRESID, gt5, fit)

	# Make the first plot.
	call gclear (gd)
	call geo_label (FIT, gt1, fit)
	call geo_1graphd (gd, gt1, fit, gfit, xref, yref, xin, yin, wts,
	    npts)
	if (GG_CONSTXY(gfit) == YES)
	    call geo_conxyd (gd, fit, sx1, sy1, sx2, sy2)
	call printf ("%s  %s\n")
	    call pargstr (xerrmsg)
	    call pargstr (yerrmsg)

	# Read the cursor commands.
	call amovd (wts, Memd[w], npts)
	while (clgcur ("cursor", wx, wy, wcs, key, Memc[cmd], SZ_LINE) != EOF) {

	    switch (key) {

	    case 'q':
		call amovd (Memd[w], wts, npts)
		break

	    case '?':
		if (GM_PROJECTION(fit) == GM_NONE)
		    call gpagefile (gd, GHELPFILE, "")
		else
		    call gpagefile (gd, CHELPFILE, "")

	    case ':':
		call geo_colon (gd, fit, gfit, Memc[cmd], newgraph)
		switch (GG_PLOTTYPE(gfit)) {
		case FIT:
		    call gt_colon (Memc[cmd], gd, gt1, newgraph)
		case XXRESID:
		    call gt_colon (Memc[cmd], gd, gt2, newgraph)
		case XYRESID:
		    call gt_colon (Memc[cmd], gd, gt3, newgraph)
		case YXRESID:
		    call gt_colon (Memc[cmd], gd, gt4, newgraph)
		case YYRESID:
		    call gt_colon (Memc[cmd], gd, gt5, newgraph)
		}

	    case 'l':
		if (GG_FITERROR(gfit) == NO) {
		    call geo_lcoeffd (sx1, sy1, xshift, yshift, xscale, yscale,
		        thetax, thetay)
		    call printf ("xshift: %.2f yshift: %.2f ")
		        call pargd (xshift)
		        call pargd (yshift)
		    call printf ("xmag: %0.3g ymag: %0.3g ")
		        call pargd (xscale)
		        call pargd (yscale)
		    call printf ("xrot: %.2f yrot: %.2f\n")
		        call pargd (thetax)
		        call pargd (thetay)
		}

	    case 't':
		if (GG_FITERROR(gfit) == NO && GG_PLOTTYPE(gfit) == FIT)
		    call geo_lxyd (gd, fit, sx1, sy1, sx2, sy2, xref, yref,
		        xin, yin, npts, wx, wy)

	    case 'c':
		if (GG_CONSTXY(gfit) == YES)
		    GG_CONSTXY(gfit) = NO
		else if (GG_CONSTXY(gfit) == NO)
		    GG_CONSTXY(gfit) = YES

	    case 'd', 'u':
		if (key == 'd')
		    delete = YES
		else
		    delete = NO

		switch (GG_PLOTTYPE(gfit)) {
		case FIT:
		    call geo_1deleted (gd, xin, yin, Memd[w], wts, npts, wx,
		        wy, delete)
		case XXRESID:
		    call geo_2deleted (gd, xref, Memd[xresid], Memd[w], wts,
		        npts, wx, wy, delete)
		case XYRESID:
		    call geo_2deleted (gd, yref, Memd[xresid], Memd[w], wts,
		        npts, wx, wy, delete)
		case YXRESID:
		    call geo_2deleted (gd, xref, Memd[yresid], Memd[w], wts,
		        npts, wx, wy, delete)
		case YYRESID:
		    call geo_2deleted (gd, yref, Memd[yresid], Memd[w], wts,
		        npts, wx, wy, delete)
		}

		GG_NEWFUNCTION(gfit) = YES

	    case 'g':
		if (GG_PLOTTYPE(gfit) != FIT)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = FIT

	    case 'x':
		if (GG_PLOTTYPE(gfit) != XXRESID)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = XXRESID

	    case 'r':
		if (GG_PLOTTYPE(gfit) != XYRESID)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = XYRESID

	    case 'y':
		if (GG_PLOTTYPE(gfit) != YXRESID)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = YXRESID

	    case 's':
		if (GG_PLOTTYPE(gfit) != YYRESID)
		    newgraph = YES
		GG_PLOTTYPE(gfit) = YYRESID

	    case 'f':
		# do fit
		if (GG_NEWFUNCTION(gfit) == YES) {
		    iferr {
			switch (GM_FIT(fit)) {
			case GM_ROTATE:
		            call geo_fthetad (fit, sx1, sy1, xref, yref, xin,
			        yin, Memd[w], Memd[xresid], Memd[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			case GM_RSCALE:
		            call geo_fmagnifyd (fit, sx1, sy1, xref, yref, xin,
			        yin, Memd[w], Memd[xresid], Memd[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			case GM_RXYSCALE:
		            call geo_flineard (fit, sx1, sy1, xref, yref, xin,
			        yin, Memd[w], Memd[xresid], Memd[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			default:
		            call geo_fxyd (fit, sx1, sx2, xref, yref, xin,
			        Memd[w], Memd[xresid], npts, YES,
				xerrmsg, maxch) 
		            call geo_fxyd (fit, sy1, sy2, xref, yref, yin,
			        Memd[w], Memd[yresid], npts, NO,
				yerrmsg, maxch) 
			}
		        if (GM_MAXITER(fit) <= 0 || IS_INDEFD(GM_REJECT(fit)))
			    GM_NREJECT(fit) = 0
		        else
			    call geo_mrejectd (fit, sx1, sy1, sx2, sy2, xref,
			        yref, xin, yin, Memd[w], Memd[xresid],
				Memd[yresid], npts, xerrmsg, maxch,
				yerrmsg, maxch)
			GG_NEWFUNCTION(gfit) = NO
			GG_FITERROR(gfit) = NO
			errcode = OK
		    } then {
	    		errcode = errget (errstr, SZ_LINE)
	    		call printf ("%s\n")
			    call pargstr (errstr)
			GG_FITERROR(gfit) = YES
		    }
		}

		# plot new graph
		if (GG_FITERROR(gfit) == YES)
		    newgraph = NO
		else
		    newgraph = YES

	    case 'o':
		GG_OVERPLOT(gfit) = YES

	    default:
		call printf ("\07")

	    }

	    if (newgraph == YES) {
		switch (GG_PLOTTYPE(gfit)) {
		case FIT:
		    call geo_label (FIT, gt1, fit)
	    	    call geo_1graphd (gd, gt1, fit, gfit, xref, yref, xin, yin,
		        Memd[w], npts)
		    if (GG_CONSTXY(gfit) == YES)
			call geo_conxyd (gd, fit, sx1, sy1, sx2, sy2)
		case XXRESID:
		    call geo_label (XXRESID, gt2, fit)
	    	    call geo_2graphd (gd, gt2, fit, gfit, xref, Memd[xresid],
		        Memd[w], npts)
		case XYRESID:
		    call geo_label (XYRESID, gt3, fit)
	    	    call geo_2graphd (gd, gt3, fit, gfit, yref, Memd[xresid],
		         Memd[w], npts)
		case YXRESID:
		    call geo_label (YXRESID, gt4, fit)
	    	    call geo_2graphd (gd, gt4, fit, gfit, xref, Memd[yresid],
		         Memd[w], npts)
		case YYRESID:
		    call geo_label (YYRESID, gt5, fit)
	    	    call geo_2graphd (gd, gt5, fit, gfit, yref, Memd[yresid],
		         Memd[w], npts)
		}
	        call printf ("%s  %s\n")
	    	    call pargstr (xerrmsg)
	    	    call pargstr (yerrmsg)
		newgraph = NO
	    }
	}

	# Free space.
	call gt_free (gt1)
	call gt_free (gt2)
	call gt_free (gt3)
	call gt_free (gt4)
	call gt_free (gt5)
	call sfree (sp)

	# Call an error if appropriate.
	if (errcode > 0)
	    call error (2, errstr)
end

# GEO_LCOEFF -- Print the coefficents of the linear portion of the
# fit, xshift, yshift, xexpansion, yexpansion, x and y rotations.

procedure geo_lcoeffd (sx, sy, xshift, yshift, xscale, yscale, xrot, yrot)

pointer sx              #I pointer to the x surface fit
pointer sy              #I pointer to the y surface fit
double   xshift          #O output x shift
double   yshift          #O output y shift
double   xscale          #O output x scale
double   yscale          #O output y scale
double   xrot            #O rotation of point on x axis
double   yrot            #O rotation of point on y axis

int     nxxcoeff, nxycoeff, nyxcoeff, nyycoeff
pointer sp, xcoeff, ycoeff
double   xxrange, xyrange, xxmaxmin, xymaxmin
double   yxrange, yyrange, yxmaxmin, yymaxmin
double   a, b, c, d

bool    fp_equald()
int     dgsgeti()
double  dgsgetd()

begin
        # Allocate working space.
        call smark (sp)
        call salloc (xcoeff, dgsgeti (sx, GSNCOEFF), TY_DOUBLE)
        call salloc (ycoeff, dgsgeti (sy, GSNCOEFF), TY_DOUBLE)

        # Get coefficients and numbers of coefficients.
        call dgscoeff (sx, Memd[xcoeff], nxxcoeff)
        call dgscoeff (sy, Memd[ycoeff], nyycoeff)
        nxxcoeff = dgsgeti (sx, GSNXCOEFF)
        nxycoeff = dgsgeti (sx, GSNYCOEFF)
        nyxcoeff = dgsgeti (sy, GSNXCOEFF)
        nyycoeff = dgsgeti (sy, GSNYCOEFF)

        # Get the data range.
        if (dgsgeti (sx, GSTYPE) != GS_POLYNOMIAL) {
            xxrange = (dgsgetd (sx, GSXMAX) - dgsgetd (sx, GSXMIN)) / 2.0d0
            xxmaxmin = - (dgsgetd (sx, GSXMAX) + dgsgetd (sx, GSXMIN)) / 2.0d0
            xyrange = (dgsgetd (sx, GSYMAX) - dgsgetd (sx, GSYMIN)) / 2.0d0
            xymaxmin = - (dgsgetd (sx, GSYMAX) + dgsgetd (sx, GSYMIN)) / 2.0d0
        } else {
            xxrange = double(1.0)
            xxmaxmin = double(0.0)
            xyrange = double(1.0)
            xymaxmin = double(0.0)
        }

        if (dgsgeti (sy, GSTYPE) != GS_POLYNOMIAL) {
            yxrange = (dgsgetd (sy, GSXMAX) - dgsgetd (sy, GSXMIN)) / 2.0d0
            yxmaxmin = - (dgsgetd (sy, GSXMAX) + dgsgetd (sy, GSXMIN)) / 2.0d0
            yyrange = (dgsgetd (sy, GSYMAX) - dgsgetd (sy, GSYMIN)) / 2.0d0
            yymaxmin = - (dgsgetd (sy, GSYMAX) + dgsgetd (sy, GSYMIN)) / 2.0d0
        } else {
            yxrange = double(1.0)
            yxmaxmin = double(0.0)
            yyrange = double(1.0)
            yymaxmin = double(0.0)
        }

        # Get the shifts.
        xshift = Memd[xcoeff] + Memd[xcoeff+1] * xxmaxmin / xxrange +
            Memd[xcoeff+2] * xymaxmin / xyrange
        yshift = Memd[ycoeff] + Memd[ycoeff+1] * yxmaxmin / yxrange +
            Memd[ycoeff+2] * yymaxmin / yyrange

        # Get the rotation and scaling parameters and correct for normalization.
        if (nxxcoeff > 1)
            a = Memd[xcoeff+1] / xxrange
        else
            a = double(0.0)
        if (nxycoeff > 1)
            b = Memd[xcoeff+nxxcoeff] / xyrange
        else
            b = double(0.0)
        if (nyxcoeff > 1)
            c = Memd[ycoeff+1] / yxrange
        else
            c = double(0.0)
        if (nyycoeff > 1)
            d = Memd[ycoeff+nyxcoeff] / yyrange
        else
            d = double(0.0)

        # Get the magnification factors.
        xscale = sqrt (a * a + c * c)
        yscale = sqrt (b * b + d * d)

        # Get the x and y axes rotation factors.
        if (fp_equald (a, double(0.0)) && fp_equald (c, double(0.0)))
            xrot = double(0.0)
        else
            xrot = RADTODEG (atan2 (-c, a))
        if (xrot < double(0.0))
            xrot = xrot + double(360.0)

        if (fp_equald (b, double(0.0)) && fp_equald (d, double(0.0)))
            yrot = double(0.0)
        else
            yrot = RADTODEG (atan2 (b, d))
        if (yrot < double(0.0))
            yrot = yrot + double(360.0)

        call sfree (sp)
end