aboutsummaryrefslogtreecommitdiff
path: root/noao/imred/dtoi/hdicfit/hdicgfit.x
blob: b50ed03ca5b266980818cc87ce4aea0f14c0c923 (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
include	<error.h>
include	<pkg/gtools.h>
include	<mach.h>
include	<gset.h>
include	"hdicfit.h"

define	HELP		"noao$lib/scr/hdicgfit.key"
define	PROMPT		"hdicfit options"
define	EB_WTS		10
define	EB_SDEV		11

# ICG_FIT -- Interactive curve fitting with graphics.  This is the main
# entry point for the interactive graphics part of the icfit package.

procedure icg_fitd (ic, gp, cursor, gt, cv, density, loge, owts, osdev, npts)

pointer	ic			# ICFIT pointer
pointer	gp			# GIO pointer
char	cursor[ARB]		# GIO cursor input
pointer	gt			# GTOOLS pointer
pointer	cv			# CURFIT pointer
double	density[npts]		# Original density, not fog subtracted
double	loge[npts]		# Original log exposure values
double	owts[npts]		# Original weights array
double	osdev[npts]		# Original standard deviation array
int	npts			# Number of points

real	wx, wy, wwy, oldx, oldy
int	wcs, key, nptso
char	cmd[SZ_LINE]

int	i, newgraph, axes[2], linetype
double	x1, newwt
pointer	userwts, x, y, wts, den, whydel, sdev, ebw

int	clgcur(), stridxs(), scan(), nscan()
int	icg_nearestd(), gstati()
double	dcveval()
errchk	ic_fitd, malloc
define	redraw_ 91

begin
	# Allocate memory for the fit and a copy of the weights.
	# The weights are copied because they are changed when points are
	# deleted.  The input x is the untransformed density, and is used
	# to generate other types of transforms.  Points can be added to
	# the sample, so the y array and weights array can change as well.
	# The original number of points is also remembered.

	call malloc (userwts, npts, TY_DOUBLE)
	call malloc (x,       npts, TY_DOUBLE)
	call malloc (y,       npts, TY_DOUBLE)
	call malloc (wts,     npts, TY_DOUBLE)
	call malloc (den,     npts, TY_DOUBLE)
	call malloc (whydel,  npts, TY_INT)
	call malloc (sdev,    npts, TY_DOUBLE)
	call malloc (ebw,     npts, TY_DOUBLE)

	call amovd (owts,     Memd[userwts], npts)
	call amovd (owts,     Memd[wts],     npts)
	call amovd (loge,     Memd[y],       npts)
	call amovd (density,  Memd[den],     npts)
	call amovki (NDELETE, Memi[whydel],  npts)
	call amovd (osdev,    Memd[sdev],    npts)
	nptso = npts

	# Initialize
	IC_OVERPLOT(ic) = NO
	IC_NEWX(ic) = YES
	IC_NEWY(ic) = YES
	IC_NEWWTS(ic) = YES
	IC_NEWFUNCTION(ic) = YES
	IC_NEWTRANSFORM(ic) = YES
	IC_UPDATE(ic) = YES
	IC_EBARS(ic) = EB_SDEV

	# Read cursor commands.

	key = 'f'
	axes[1] = IC_AXES(ic, IC_GKEY(ic), 1)
	axes[2] = IC_AXES(ic, IC_GKEY(ic), 2)

	repeat {
	    switch (key) {
	    case '?': # Print help text.
		call gpagefile (gp, HELP, PROMPT)

	    case 'q': # Terminate cursor loop
		break

	    case ':': # List or set parameters
		if (stridxs ("/", cmd) == 1)
	            call gt_colon (cmd, gp, gt, newgraph)
		else
		    call icg_colond (ic, cmd, gp, gt, cv, Memd[x], 
			Memd[y], Memd[wts], npts)

		if (IC_RESET(ic) == YES) {
		    npts = nptso
		    call amovd (owts, Memd[userwts], npts)
		    call amovd (owts, Memd[wts], npts)
		    call amovd (loge, Memd[y], npts)
		    call amovd (density, Memd[den], npts)
		    call amovki (NDELETE, Memi[whydel], npts)
		    call amovd (osdev, Memd[sdev], npts)
		    call hdic_init (density, npts, 0.0)
		}

		# See if user wants to quit without updating
		if (IC_UPDATE(ic) == NO) {
		    call mfree (x, TY_DOUBLE)
		    call mfree (y, TY_DOUBLE)
		    call mfree (wts, TY_DOUBLE)
		    call mfree (userwts, TY_DOUBLE)
		    call mfree (den, TY_DOUBLE)
		    call mfree (sdev, TY_DOUBLE)
		    return
		}

	    case 'a': # Add data points to the sample.  This is only possible 
		      # from an HD curve plot.

		if ((IC_AXES (ic, IC_GKEY(ic), 1) == 'y') &&
		    (IC_AXES (ic, IC_GKEY(ic), 2) == 'u')) {

		    # Query for weight after plotting current location
		    # call gt_plot (gp, gt, wx, wy, 1)
		    call gmark (gp, wx, wy, GM_CIRCLE, 2.0, 2.0)
		    newwt = 1.0D0
		    call printf ("Enter weight of new point (%g): ")
			call pargd (newwt)
		    call flush (STDOUT)
		    if (scan() != EOF) {
		        call pargd (x1)
		        if (nscan() == 1) {
			    if (!IS_INDEFD (x1)) {
			        newwt = x1
			    }
		        }
		    }

		} else {
		    call eprintf ("Points can be added only from an HD Curve\n")
		    next
		}

		# Add fog into "density above fog" value read from cursor
		wwy = wy + IC_FOG (ic)
		if (wwy < 0.0) {
		    call eprintf (
			"New density (%g) is below fog and will not be added\n")
			    call pargr (wwy)
		    next
		}

		# Add point into sample
		call eprintf ("New Point: density above fog = %.4f, log ")
		    call pargr (wwy)
		call eprintf ("exposure = %.4f, weight = %.4f\n")
		    call pargr (wx)
		    call pargd (newwt)

		call hdic_addpoint (ic, wwy, wx, newwt, den, y, wts, userwts, 
		    x, whydel, sdev, npts)

		call realloc (ebw, npts, TY_DOUBLE)

		call hdic_transform (ic, Memd[den], Memd[userwts], Memd[x], 
		    Memd[wts], Memi[whydel], npts)

	    case 'c': # Print the positions of data points.
		i = icg_nearestd (ic, gp, gt, cv, Memd[x], Memd[y], npts, 
		    wx, wy)

	    	if (i != 0) {
		    call printf ("den= %7.4g x= %7.4g  exp= %7.4g  fit= %7.4g")
			call pargd (Memd[den+i-1])
			call pargd (Memd[x+i-1])
			call pargd (Memd[y+i-1])
			call pargd (dcveval (cv, Memd[x+i-1]))
		}

	    case 'd': # Delete data points.
		call icg_deleted (ic, gp, gt, cv, Memd[x], Memd[y], Memd[wts], 
		    npts, wx, wy)

	    case 'f': # Fit the function and reset the flags.
		iferr {
		    # Copy new transformed vector, if necessary
		    if (IC_NEWTRANSFORM(ic) == YES || IC_NEWFOG(ic) == YES) 
			call hdic_transform (ic, Memd[den], Memd[userwts], 
			    Memd[x], Memd[wts], Memi[whydel], npts)

		    call ic_fitd (ic, cv, Memd[x], Memd[y], Memd[wts], npts, 
			IC_NEWX(ic), IC_NEWY(ic), IC_NEWWTS(ic), 
			    IC_NEWFUNCTION(ic))

		    IC_NEWX(ic) = NO
		    IC_NEWY(ic) = NO
		    IC_NEWWTS(ic) = NO
		    IC_NEWFUNCTION(ic) = NO
		    IC_NEWTRANSFORM(ic) = NO
		    IC_FITERROR(ic) = NO
		    IC_NEWFOG(ic) = NO
		    newgraph = YES
		} then {
		    IC_FITERROR(ic) = YES
		    call erract (EA_WARN)
		    newgraph = NO
		}

	    case 'g':	# Set graph axes types.
		call printf ("Graph key to be defined: ")
		call flush (STDOUT)
		if (scan() == EOF)
		    goto redraw_
		call gargc (cmd[1])

		switch (cmd[1]) {
		case '\n':
		case 'h', 'i', 'j', 'k', 'l':
		    switch (cmd[1]) {
		    case 'h':
		        key = 1
		    case 'i':
		        key = 2
		    case 'j':
		        key = 3
		    case 'k':
		        key = 4
		    case 'l':
		        key = 5
		    }

		    call printf ("Set graph axes types (%c, %c): ")
		        call pargc (IC_AXES(ic, key, 1))
		        call pargc (IC_AXES(ic, key, 2))
		    call flush (STDOUT)
		    if (scan() == EOF)
		        goto redraw_
		    call gargc (cmd[1])

		    switch (cmd[1]) {
		    case '\n':
		    default:
		        call gargc (cmd[2])
		        call gargc (cmd[2])
		        if (cmd[2] != '\n') {
			    IC_AXES(ic, key, 1) = cmd[1]
			    IC_AXES(ic, key, 2) = cmd[2]
		        }
		    }
		default:
		    call printf ("Not a graph key")
		}

	    case 'h':
		if (IC_GKEY(ic) != 1) {
		    IC_GKEY(ic) = 1
		    newgraph = YES
		}

	    case 'i':
		if (IC_GKEY(ic) != 2) {
		    IC_GKEY(ic) = 2
		    newgraph = YES
		}

	    case 'j':
		if (IC_GKEY(ic) != 3) {
		    IC_GKEY(ic) = 3
		    newgraph = YES
		}

	    case 'k':
		if (IC_GKEY(ic) != 4) {
		    IC_GKEY(ic) = 4
		    newgraph = YES
		}

	    case 'l':
		if (IC_GKEY(ic) != 5) {
		    IC_GKEY(ic) = 5
		    newgraph = YES
		}

	    case 'o': # Set overplot flag
		IC_OVERPLOT(ic) = YES

	    case 'r': # Redraw the graph
		newgraph = YES

	    case 'u': # Undelete data points.
		call icg_undeleted (ic, gp, gt, cv, Memd[x], Memd[y], 
		    Memd[wts], Memd[userwts], npts, wx, wy)

	    case 'w':  # Window graph
		call gt_window (gt, gp, cursor, newgraph)

	    case 'x': # Reset the value of the x point.
		i = icg_nearestd (ic, gp, gt, cv, Memd[x], Memd[y], npts, wx, 
		    wy)

	    	if (i != 0) {
		    call printf ("Enter new x (%g): ")
			call pargd (Memd[x+i-1])
		    call flush (STDOUT)
		    if (scan() != EOF) {
		        call gargd (x1)
		        if (nscan() == 1) {
			    if (!IS_INDEF (x1)) {
				oldx = Memd[x+i-1]
				oldy = Memd[y+i-1]
			        Memd[x+i-1] = x1
				call hd_redraw (gp, oldx, oldy, x1, oldy)
			        IC_NEWX(ic) = YES
			    }
			}
		    }
		}

	    case 'y': # Reset the value of the y point.
		i = icg_nearestd (ic, gp, gt, cv, Memd[x], Memd[y], npts, wx, 
		    wy)

	    	if (i != 0) {
		    call printf ("Enter new y (%g): ")
			call pargd (Memd[y+i-1])
		    call flush (STDOUT)
		    if (scan() != EOF) {
		        call gargd (x1)
		        if (nscan() == 1) {
			    if (!IS_INDEF (x1)) {
				oldx = Memd[x+i-1]
				oldy = Memd[y+i-1]
			        Memd[y+i-1] = x1
				call hd_redraw (gp, oldx, oldy, oldx, x1)
			        IC_NEWY(ic) = YES
			    }
			}
		    }
		}

	    case 'z': # Reset the weight value of the nearest point
		i = icg_nearestd (ic, gp, gt, cv, Memd[x], Memd[y], npts, wx, 
		    wy)

	    	if (i != 0) {
		    call printf ("Enter new weight (%g): ")
			call pargd (Memd[wts+i-1])
		    call flush (STDOUT)
		    if (scan() != EOF) {
		        call gargd (x1)
		        if (nscan() == 1) {
			    if (!IS_INDEF (x1)) {
			        Memd[wts+i-1] = x1
			        IC_NEWWTS(ic) = YES
			    }
			}
		    }
		}

	    default: # Let the user decide on any other keys.
		call icg_user (ic, gp, gt, cv, wx, wy, wcs, key, cmd)
	    }

	    # Redraw the graph if necessary.
redraw_	    if (newgraph == YES) {
		if (IC_AXES(ic, IC_GKEY(ic), 1) != axes[1]) {
		    axes[1] = IC_AXES(ic, IC_GKEY(ic), 1)
		    call gt_setr (gt, GTXMIN, INDEF)
		    call gt_setr (gt, GTXMAX, INDEF)
		}
		if (IC_AXES(ic, IC_GKEY(ic), 2) != axes[2]) {
		    axes[2] = IC_AXES(ic, IC_GKEY(ic), 2)
		    call gt_setr (gt, GTYMIN, INDEF)
		    call gt_setr (gt, GTYMAX, INDEF)
		}

		# Overplot with a different line type
		if (IC_OVERPLOT(ic) == YES)
		    linetype = min ((gstati (gp, G_PLTYPE) + 1), 4)
		else
		    linetype = GL_SOLID
		call gseti (gp, G_PLTYPE, linetype)

		call hdic_ebw (ic, Memd[den], Memd[x], Memd[sdev], Memd[ebw], 
		    npts)

	    	call icg_graphd (ic, gp, gt, cv, Memd[x], Memd[y], Memd[wts], 
		    Memd[ebw], npts)

		newgraph = NO
	    }
	} until (clgcur (cursor, wx, wy, wcs, key, cmd, SZ_LINE) == EOF)

	call mfree (x, TY_DOUBLE)
	call mfree (y, TY_DOUBLE)
	call mfree (wts, TY_DOUBLE)
	call mfree (userwts, TY_DOUBLE)
	call mfree (den, TY_DOUBLE)
end