aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/lib/geogmap.gx
blob: e52a129ed52bfc018044cafd190096574d175c54 (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
# 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"

$for (rd)

# GEO_MGFIT -- Fit the surface using interactive graphics.

procedure geo_mgfit$t (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
PIXEL	xref[npts]	#I the x reference coordinates
PIXEL	yref[npts]	#I the y reference coordinates
PIXEL	xin[npts]	#I input x coordinates
PIXEL	yin[npts]	#I input y coordinates
PIXEL	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
PIXEL	xshift, yshift, xscale, yscale, thetax, thetay

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

errchk	geo_fxy$t(), geo_mreject$t(), geo_ftheta$t()
errchk	geo_fmagnify$t(), geo_flinear$t()

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

	# Do initial fit.
	iferr {
	    switch (GM_FIT(fit)) {
	    case GM_ROTATE:
	        call geo_ftheta$t (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Mem$t[xresid], Mem$t[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    case GM_RSCALE:
	        call geo_fmagnify$t (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Mem$t[xresid], Mem$t[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    case GM_RXYSCALE:
	        call geo_flinear$t (fit, sx1, sy1, xref, yref, xin, yin, wts,
	            Mem$t[xresid], Mem$t[yresid], npts, xerrmsg, maxch,
		    yerrmsg, maxch)
		    sx2 = NULL
		    sy2 = NULL
	    default:
	        call geo_fxy$t (fit, sx1, sx2, xref, yref, xin, wts,
		    Mem$t[xresid], npts, YES, xerrmsg, maxch)
	        call geo_fxy$t (fit, sy1, sy2, xref, yref, yin, wts,
		    Mem$t[yresid], npts, NO, yerrmsg, maxch)
	    }
	    if (GM_MAXITER(fit) <= 0 || IS_INDEFD(GM_REJECT(fit)))
	        GM_NREJECT(fit) = 0
	    else
	        call geo_mreject$t (fit, sx1, sy1, sx2, sy2, xref, yref, xin,
		    yin, wts, Mem$t[xresid], Mem$t[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_1graph$t (gd, gt1, fit, gfit, xref, yref, xin, yin, wts,
	    npts)
	if (GG_CONSTXY(gfit) == YES)
	    call geo_conxy$t (gd, fit, sx1, sy1, sx2, sy2)
	call printf ("%s  %s\n")
	    call pargstr (xerrmsg)
	    call pargstr (yerrmsg)

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

	    switch (key) {

	    case 'q':
		call amov$t (Mem$t[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_lcoeff$t (sx1, sy1, xshift, yshift, xscale, yscale,
		        thetax, thetay)
		    call printf ("xshift: %.2f yshift: %.2f ")
		        call parg$t (xshift)
		        call parg$t (yshift)
		    call printf ("xmag: %0.3g ymag: %0.3g ")
		        call parg$t (xscale)
		        call parg$t (yscale)
		    call printf ("xrot: %.2f yrot: %.2f\n")
		        call parg$t (thetax)
		        call parg$t (thetay)
		}

	    case 't':
		if (GG_FITERROR(gfit) == NO && GG_PLOTTYPE(gfit) == FIT)
		    call geo_lxy$t (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_1delete$t (gd, xin, yin, Mem$t[w], wts, npts, wx,
		        wy, delete)
		case XXRESID:
		    call geo_2delete$t (gd, xref, Mem$t[xresid], Mem$t[w], wts,
		        npts, wx, wy, delete)
		case XYRESID:
		    call geo_2delete$t (gd, yref, Mem$t[xresid], Mem$t[w], wts,
		        npts, wx, wy, delete)
		case YXRESID:
		    call geo_2delete$t (gd, xref, Mem$t[yresid], Mem$t[w], wts,
		        npts, wx, wy, delete)
		case YYRESID:
		    call geo_2delete$t (gd, yref, Mem$t[yresid], Mem$t[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_ftheta$t (fit, sx1, sy1, xref, yref, xin,
			        yin, Mem$t[w], Mem$t[xresid], Mem$t[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			case GM_RSCALE:
		            call geo_fmagnify$t (fit, sx1, sy1, xref, yref, xin,
			        yin, Mem$t[w], Mem$t[xresid], Mem$t[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			case GM_RXYSCALE:
		            call geo_flinear$t (fit, sx1, sy1, xref, yref, xin,
			        yin, Mem$t[w], Mem$t[xresid], Mem$t[yresid],
				npts, xerrmsg, maxch, yerrmsg, maxch) 
				sx2 = NULL
				sy2 = NULL
			default:
		            call geo_fxy$t (fit, sx1, sx2, xref, yref, xin,
			        Mem$t[w], Mem$t[xresid], npts, YES,
				xerrmsg, maxch) 
		            call geo_fxy$t (fit, sy1, sy2, xref, yref, yin,
			        Mem$t[w], Mem$t[yresid], npts, NO,
				yerrmsg, maxch) 
			}
		        if (GM_MAXITER(fit) <= 0 || IS_INDEFD(GM_REJECT(fit)))
			    GM_NREJECT(fit) = 0
		        else
			    call geo_mreject$t (fit, sx1, sy1, sx2, sy2, xref,
			        yref, xin, yin, Mem$t[w], Mem$t[xresid],
				Mem$t[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_1graph$t (gd, gt1, fit, gfit, xref, yref, xin, yin,
		        Mem$t[w], npts)
		    if (GG_CONSTXY(gfit) == YES)
			call geo_conxy$t (gd, fit, sx1, sy1, sx2, sy2)
		case XXRESID:
		    call geo_label (XXRESID, gt2, fit)
	    	    call geo_2graph$t (gd, gt2, fit, gfit, xref, Mem$t[xresid],
		        Mem$t[w], npts)
		case XYRESID:
		    call geo_label (XYRESID, gt3, fit)
	    	    call geo_2graph$t (gd, gt3, fit, gfit, yref, Mem$t[xresid],
		         Mem$t[w], npts)
		case YXRESID:
		    call geo_label (YXRESID, gt4, fit)
	    	    call geo_2graph$t (gd, gt4, fit, gfit, xref, Mem$t[yresid],
		         Mem$t[w], npts)
		case YYRESID:
		    call geo_label (YYRESID, gt5, fit)
	    	    call geo_2graph$t (gd, gt5, fit, gfit, yref, Mem$t[yresid],
		         Mem$t[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_lcoeff$t (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
PIXEL   xshift          #O output x shift
PIXEL   yshift          #O output y shift
PIXEL   xscale          #O output x scale
PIXEL   yscale          #O output y scale
PIXEL   xrot            #O rotation of point on x axis
PIXEL   yrot            #O rotation of point on y axis

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

bool    fp_equal$t()
$if (datatype == r)
int     gsgeti()
real    gsgetr()
$else
int     dgsgeti()
double  dgsgetd()
$endif

begin
        # Allocate working space.
        call smark (sp)
$if (datatype == r)
        call salloc (xcoeff, gsgeti (sx, GSNCOEFF), TY_PIXEL)
        call salloc (ycoeff, gsgeti (sy, GSNCOEFF), TY_PIXEL)
$else
        call salloc (xcoeff, dgsgeti (sx, GSNCOEFF), TY_PIXEL)
        call salloc (ycoeff, dgsgeti (sy, GSNCOEFF), TY_PIXEL)
$endif

        # Get coefficients and numbers of coefficients.
$if (datatype == r)
        call gscoeff (sx, Mem$t[xcoeff], nxxcoeff)
        call gscoeff (sy, Mem$t[ycoeff], nyycoeff)
        nxxcoeff = gsgeti (sx, GSNXCOEFF)
        nxycoeff = gsgeti (sx, GSNYCOEFF)
        nyxcoeff = gsgeti (sy, GSNXCOEFF)
        nyycoeff = gsgeti (sy, GSNYCOEFF)
$else
        call dgscoeff (sx, Mem$t[xcoeff], nxxcoeff)
        call dgscoeff (sy, Mem$t[ycoeff], nyycoeff)
        nxxcoeff = dgsgeti (sx, GSNXCOEFF)
        nxycoeff = dgsgeti (sx, GSNYCOEFF)
        nyxcoeff = dgsgeti (sy, GSNXCOEFF)
        nyycoeff = dgsgeti (sy, GSNYCOEFF)
$endif

        # Get the data range.
$if (datatype == r)
        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
        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
$endif
        } else {
            xxrange = PIXEL(1.0)
            xxmaxmin = PIXEL(0.0)
            xyrange = PIXEL(1.0)
            xymaxmin = PIXEL(0.0)
        }

$if (datatype == r)
        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
        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
$endif
        } else {
            yxrange = PIXEL(1.0)
            yxmaxmin = PIXEL(0.0)
            yyrange = PIXEL(1.0)
            yymaxmin = PIXEL(0.0)
        }

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

        # Get the rotation and scaling parameters and correct for normalization.
        if (nxxcoeff > 1)
            a = Mem$t[xcoeff+1] / xxrange
        else
            a = PIXEL(0.0)
        if (nxycoeff > 1)
            b = Mem$t[xcoeff+nxxcoeff] / xyrange
        else
            b = PIXEL(0.0)
        if (nyxcoeff > 1)
            c = Mem$t[ycoeff+1] / yxrange
        else
            c = PIXEL(0.0)
        if (nyycoeff > 1)
            d = Mem$t[ycoeff+nyxcoeff] / yyrange
        else
            d = PIXEL(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_equal$t (a, PIXEL(0.0)) && fp_equal$t (c, PIXEL(0.0)))
            xrot = PIXEL(0.0)
        else
            xrot = RADTODEG (atan2 (-c, a))
        if (xrot < PIXEL(0.0))
            xrot = xrot + PIXEL(360.0)

        if (fp_equal$t (b, PIXEL(0.0)) && fp_equal$t (d, PIXEL(0.0)))
            yrot = PIXEL(0.0)
        else
            yrot = RADTODEG (atan2 (b, d))
        if (yrot < PIXEL(0.0))
            yrot = yrot + PIXEL(360.0)

        call sfree (sp)
end

$endfor