aboutsummaryrefslogtreecommitdiff
path: root/noao/twodspec/longslit/transform/igsfit/igsfit.x
blob: 14e8e51e48fcc05296b784e1ef6c5b939d321aed (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
include	<mach.h>
include	<pkg/gtools.h>
include <pkg/igsfit.h>

define	HELP	"noao$lib/scr/igsfit.key"
define	PROMPT	"fitcoords surface fitting options"


# IGS_FIT1 -- Fit z = f(x, y)

procedure igs_fit1 (sf, gp, gplog, gt, axis, pts, npts, labels, interactive)

pointer	sf			# GSURFIT pointer
pointer	gp			# GIO pointer
pointer	gplog			# GIO pointer for plot log
pointer	gt			# GTOOLS pointer
int	axis[2]			# Axis definitions
real	pts[npts, ARB]		# Data
int	npts			# Number of pts points
char	labels[SZ_LINE, ARB]	# Identification labels
int	interactive		# Interactive?

extern	igs_solve1()

begin
	call igs_fit (sf, gp, gplog, gt, axis, pts, npts, labels, interactive,
	    igs_solve1)
end


# IGS_FIT2 -- Fit z = x + f(y)

procedure igs_fit2 (sf, gp, gplog, gt, axis, pts, npts, labels, interactive)

pointer	sf			# GSURFIT pointer
pointer	gp			# GIO pointer
pointer	gplog			# GIO pointer for plot log
pointer	gt			# GTOOLS pointer
int	axis[2]			# Axis definitions
real	pts[npts, ARB]		# Data
int	npts			# Number of pts points
char	labels[SZ_LINE, ARB]	# Identification labels
int	interactive		# Interactive?

extern	igs_solve2()

begin
	call igs_fit (sf, gp, gplog, gt, axis, pts, npts, labels, interactive,
	    igs_solve2)
end


# IGS_FIT3 -- Fit z = y + f(x)

procedure igs_fit3 (sf, gp, gplog, gt, axis, pts, npts, labels, interactive)

pointer	sf			# GSURFIT pointer
pointer	gp			# GIO pointer
pointer	gplog			# GIO pointer for plot log
pointer	gt			# GTOOLS pointer
int	axis[2]			# Axis definitions
real	pts[npts, ARB]		# Data
int	npts			# Number of pts points
char	labels[SZ_LINE, ARB]	# Identification labels
int	interactive		# Interactive?

extern	igs_solve3()

begin
	call igs_fit (sf, gp, gplog, gt, axis, pts, npts, labels, interactive,
	    igs_solve3)
end


# IGS_FIT -- Interactive surface fitting.

procedure igs_fit (sf, gp, gplog, gt, axis, pts, npts, labels, interactive, igs_solve)

pointer	sf			# GSURFIT pointer
pointer	gp			# GIO pointer
pointer	gplog			# GIO pointer for plot log
pointer	gt			# GTOOLS pointer
int	axis[2]			# Axis definitions
real	pts[npts, ARB]		# Data
int	npts			# Number of pts points
char	labels[SZ_LINE, ARB]	# Identification labels
int	interactive		# Interactive?
extern	igs_solve()		# Surface solution routine

int	i, newgraph, ztype, dtype, refpt, refpt1
real	zval, zval1
pointer	wts

real	wx, wy
int	wcs, key
char	cmd[SZ_LINE]

int	clgcur(), gt_gcur(), igs_nearest(), igs_nearestd(), igs_nearestu()
errchk	igs_solve

include	"igsfit.com"

begin
	# Compute a solution and set the residuals.

	call igs_solve (sf, pts[1,X], pts[1,Y], pts[1,Z], pts[1,W], npts)
	call xgsvector (sf, pts[1,X], pts[1,Y], pts[1,S], npts)
	call asubr (pts[1,Z], pts[1,S], pts[1,R], npts)
	call aavgr (pts[1,R], npts, mean, rms)
	call igs_params (gt)

	# Return if not interactive.

	ztype = INDEFI
	if ((gp == NULL) || (interactive == NO))
	    goto 30

	call malloc (wts, npts, TY_REAL)
	call amovr (pts[1,W],  Memr[wts], npts)

	call igs_graph (gp, gt, ztype, refpt, axis, pts, npts, labels)
	newgraph = NO

	# Read cursor commands.

10	while (gt_gcur ("cursor", wx, wy, wcs, key, cmd, SZ_LINE) != EOF) {
	    switch (key) {
	    case '?':
		# Print help text.

		call gpagefile (gp, HELP, PROMPT)

	    case ':':
		# List or set parameters

		if (cmd[1] == '/')
		    call gt_colon (cmd, gp, gt, newgraph)
		else
		    call igs_colon (cmd, gp, sf)

	    # Set abscissa

	    case 'x':
		call printf ("Select abscissa (x, y, z, s, r): ")
		if (clgcur ("cursor", wx, wy, wcs, key, cmd, SZ_LINE) == EOF)
		    goto 10
		call printf ("\n")

		switch (key) {
		case 'x':
		    i = X
		case 'y':
		    i = Y
		case 'z':
		    i = Z
		case 's':
		    i = S
		case 'r':
		    i = R
		default:
		    call printf ("\07\n")
		    goto 10
		}

		if (axis[1] != i) {
		    axis[1] = i
		    call gt_setr (gt, GTXMIN, INDEF)
		    call gt_setr (gt, GTXMAX, INDEF)
		}

	    # Set ordinate

	    case 'y':
		call printf ("Select ordinate (x, y, z, s, r): ")
		if(clgcur ("cursor", wx, wy, wcs, key, cmd, SZ_LINE) == EOF)
		    goto 10
		call printf ("\n")

		switch (key) {
		case 'x':
		    i = X
		case 'y':
		    i = Y
		case 'z':
		    i = Z
		case 's':
		    i = S
		case 'r':
		    i = R
		default:
		    call printf ("\07\n")
		    goto 10
		}

		if (axis[2] != i) {
		    axis[2] = i
		    call gt_setr (gt, GTYMIN, INDEF)
		    call gt_setr (gt, GTYMAX, INDEF)
		}

	    case 'r':
		newgraph = YES

	    case 'z':
		if (IS_INDEFI (ztype)) {
		    refpt = igs_nearest (gp, ztype, refpt, axis, pts, npts, wx,
			wy, wcs)

		    call printf ("Zoom type (x, y, z): ")
		    if (clgcur ("cursor",wx,wy,wcs,key,cmd,SZ_LINE) == EOF)
			goto 10
		    call printf ("\n")

		    switch (key) {
		    case 'x':
		        ztype = X
		    case 'y':
		        ztype = Y
		    case 'z':
		        ztype = Z
		    default:
		        call printf ("\07\n")
		        goto 10
		    }

		    newgraph = YES
		}

	    case 'p':
		if (!IS_INDEFI (ztype)) {
		    ztype = INDEFI
		    newgraph = YES
		}

	    case 'l':
		if (!IS_INDEFI (ztype)) {
		    refpt1 = 0
		    zval = pts[refpt, ztype]
		    zval1 = -MAX_REAL
		    do i = 1, npts {
			if ((pts[i,ztype] < zval) && (pts[i,ztype] > zval1)) {
			    refpt1 = i
			    zval1 = pts[refpt1,ztype]
			}
		    }

		    if (refpt1 != 0) {
			refpt = refpt1
		        newgraph = YES
		    }
		}

	    case 'n':
		if (!IS_INDEFI (ztype)) {
		    refpt1 = 0
		    zval = pts[refpt, ztype]
		    zval1 = MAX_REAL
		    do i = 1, npts {
			if ((pts[i,ztype] > zval) && (pts[i,ztype] < zval1)) {
			    refpt1 = i
			    zval1 = pts[refpt1,ztype]
			}
		    }

		    if (refpt1 != 0) {
			refpt = refpt1
		        newgraph = YES
		    }
		}

	    case 'c':
		# cursor read
		i = igs_nearest (gp, ztype, refpt, axis, pts, npts, wx, wy, wcs)
		call printf ("%g %g %g %g %g %g\n")
		    call pargr (pts[i, X])
		    call pargr (pts[i, Y])
		    call pargr (pts[i, Z])
		    call pargr (pts[i, W])
		    call pargr (pts[i, S])
		    call pargr (pts[i, R])

	    case 'd':
		i = igs_nearestd (gp, ztype, refpt, axis, pts, npts, wx, wy,
		    wcs)
		if (i == 0)
		    goto 10

	        call gscur (gp, real (pts[i,axis[1]]), real (pts[i,axis[2]]))

		call printf ( "Delete 'p'oint or constant 'x', 'y', or 'z': ")
		if (clgcur ("cursor", wx, wy, wcs, key, cmd, SZ_LINE) == EOF)
		    goto 10
		call printf ("\n")

		switch (key) {
		case 'p':
		    dtype = 0
		case 'x':
		    dtype = X
		case 'y':
		    dtype = Y
		case 'z':
		    dtype = Z
		default:
		    call printf ("\07\n")
		    goto 10
		}

		call igs_delete (gp, gt, ztype, i, axis, pts, npts, dtype)

	    case 'u':
		i = igs_nearestu (gp, ztype, refpt, axis, pts, npts, wx, wy,
		    wcs)
		if (i == 0)
		    goto 10

	        call gscur (gp, real (pts[i,axis[1]]), real (pts[i,axis[2]]))

		call printf ( "Undelete 'p'oint or constant 'x', 'y', or 'z': ")
		if (clgcur ("cursor", wx, wy, wcs, key, cmd, SZ_LINE) == EOF)
		    goto 10
		call printf ("\n")

		switch (key) {
		case 'p':
		    dtype = 0
		case 'x':
		    dtype = X
		case 'y':
		    dtype = Y
		case 'z':
		    dtype = Z
		default:
		    call printf ("\07\n")
		    goto 10
		}

		call igs_undelete (gp, gt, ztype, i, axis, pts, Memr[wts],
		    npts, dtype)

	    case 'f':
		#call printf ("Fitting ...")
		#call flush (STDOUT)
		call igs_solve (sf,pts[1,X],pts[1,Y],pts[1,Z],pts[1,W],npts)
		call xgsvector (sf, pts[1,X], pts[1,Y], pts[1,S], npts)
		call asubr (pts[1,Z], pts[1,S], pts[1,R], npts)
		call aavgr (pts[1,R], npts, mean, rms)
		call igs_params (gt)
		newgraph = YES

	    case 'w':
		call gt_window (gt, gp, "cursor", newgraph)

	    case 'I':
		call fatal (0, "Interrupt")

	    default:
		# Ring the bell.

		call printf ("\07\n")
	    }

	    if (newgraph == YES) {
		call igs_graph (gp, gt, ztype, refpt, axis, pts, npts, labels)
		newgraph = NO
	    }
	}

	call mfree (wts, TY_REAL)

30	call igs_graph (gplog, gt, ztype, refpt, axis, pts, npts, labels)

end