aboutsummaryrefslogtreecommitdiff
path: root/noao/rv/continpars.x
blob: 3f64de6270e7af0e011d6d0c75572ca0811d666f (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
include "rvpackage.h"
include "rvflags.h"
include "rvcomdef.h"
include "rvcont.h"

.help continpars
.nf ___________________________________________________________________________
CONTINPARS  - Support routines for the 'continpars' named external pset.  

	This file include routines for opening/closing the contin structure 
as well as command handling.  Command handling is limited to changing the 
parameter values or resetting them to the default values.  Routines included
here are as follows.

	 	        cont_open (rv)
	               cont_close (rv)
	            cont_get_pars (rv, sample, func)
		   cont_parupdate (rv)
		     cont_unlearn (rv)
		        cont_show (rv)
		       cont_colon (rv, cmdstr)
		  cmd_interactive (rv)
		       cmd_sample (rv)
		     cmd_naverage (rv)
		     cmd_function (rv)
		      cmd_replace (rv)
		     cmd_cn_order (rv)
		   cmd_low_reject (rv)
		  cmd_high_reject (rv)
		     cmd_niterate (rv)
		         cmd_grow (rv)

	The 'cmd_' prefix indicates that the routine is called from a colon 
command to either print the current value or set the new value for that
field.  Other routines should be self-explanatory

.endhelp _____________________________________________________________________

# Default values for the CONTPARS pset
define	DEF_INTERACTIVE		NO		# Fit continuum interactively?
define	DEF_TYPE		DIFF		# Type of output(fit|diff|ratio)
define	DEF_SAMPLE		"*"		# Sample of points to use in fit
define	DEF_NAVERAGE		1		# Npts in sample averaging
define	DEF_FUNCTION		CN_SPLINE3	# Fitting function
define	DEF_ORDER		1		# Order of fitting function
define	DEF_REPLACE		NO		# Replace spec w/ fit?
define	DEF_LOW_REJECT		2.		# Low rejection in sigma of fit
define	DEF_HIGH_REJECT		2.		# High rejection in sigma of fit
define	DEF_NITERATE		10		# Number of rejection iterations
define	DEF_GROW		1.		# Rejection growing radius


# CONT_OPEN - Open the Process parameters substructure.  This is used to
# reduce the size of the already over-burdened main RV struct.

procedure cont_open (rv)

pointer	rv					#I RV struct pointer

pointer	cptr

begin
	iferr (call calloc (cptr, SZ_CONT_STRUCT, TY_STRUCT))
	    call error (0, "Error allocating sub-structure RV_CONT.")

	RV_CONT(rv) = cptr

	# Initlialize the values
	call calloc (CON_SAMPLE(rv), 2*SZ_LINE, TY_CHAR)
	call calloc (CON_FUNC(rv), SZ_FNAME, TY_CHAR)
	call cont_get_pars (rv, Memc[CON_SAMPLE(rv)], Memc[CON_FUNC(rv)])
end


# CONT_CLOSE - Close the continpars structure.

procedure cont_close (rv)

pointer	rv					#I RV struct pointer

begin
	call mfree (CON_FUNC(rv), TY_CHAR)
	call mfree (CON_SAMPLE(rv), TY_CHAR)
	call mfree (RV_CONT(rv), TY_STRUCT)
end


# CONT_GET_PARS - Get the continuum fitting parameters from the pset.

procedure cont_get_pars (rv, sample, func)

pointer	rv					#I RV struct pointer
char	sample[SZ_FNAME]			#U Sample points used
char	func[SZ_FNAME]				#U Function name for fit

pointer	pp
pointer	sp, rbf, bp, bp1, bp2

pointer	clopset()
int	strdic(), clgpseti(), btoi()
real	clgpsetr()
bool	clgpsetb(), streq()

begin
	# Get continuum parameters.
	iferr (pp = clopset("continpars"))
	    call error (0, "Error opening `continpars' pset")

	call smark (sp)
	call salloc (bp, SZ_LINE, TY_CHAR)
	call salloc (bp1, SZ_LINE, TY_CHAR)
	call salloc (bp2, SZ_LINE, TY_CHAR)
	call salloc (rbf, SZ_FNAME, TY_CHAR)

	call clgpset (pp, "c_function", func, SZ_LINE)
	if (streq(func,"") || streq(func," "))
	    call error (0,"Continpars.function specified as empty string.")

	call clgpset (pp, "c_sample", sample, SZ_LINE)
	if (streq(sample,"") || streq(sample," "))
	    call strcpy ("*", sample, SZ_FNAME)

	CON_ORDER(rv) = clgpseti (pp, "order")
	CON_NITERATE(rv) = clgpseti (pp, "niterate")
	CON_NAVERAGE(rv) = clgpseti (pp, "naverage")
	CON_GROW(rv) = clgpsetr (pp, "grow")
	CON_LOWREJECT(rv) = clgpsetr (pp, "low_reject")
	CON_HIGHREJECT(rv) = clgpsetr (pp, "high_reject")
	CON_INTERACTIVE(rv) = btoi (clgpsetb(pp, "c_interactive"))
	CON_REPLACE(rv) = btoi (clgpsetb(pp, "replace"))

	CON_CNFUNC(rv) = strdic (func, func, SZ_LINE, CN_INTERP_MODE)
	if (CON_CNFUNC(rv) == 0) 
	    call error (0, "Unknown fitting function type")

	CON_MARKREJ(rv) = YES

	call clcpset (pp)				# Close pset
	call sfree (sp)
end


# CONT_PARUPDATE - Update the pset with the current values of the struct.

procedure cont_parupdate (rv)

pointer	rv					#I RV struct pointer

pointer	sp, b0, b1, b2
pointer	pp, clopset()
bool 	itob()
errchk  clopset

begin
	# Update contin params
	iferr (pp = clopset ("continpars")) {
	    call rv_errmsg ("Error opening `continpars' pset.")
	    return
	}

	call smark (sp)
	call salloc (b0, SZ_LINE, TY_CHAR)
	call salloc (b1, SZ_LINE, TY_CHAR)
	call salloc (b2, SZ_LINE, TY_CHAR)

	call clppseti (pp, "order", CON_ORDER(rv))
	call clppseti (pp, "naverage", CON_NAVERAGE(rv))
	call clppseti (pp, "niterate", CON_NITERATE(rv))

	call clppsetr (pp, "low_reject", CON_LOWREJECT(rv))
	call clppsetr (pp, "high_reject", CON_HIGHREJECT(rv))
	call clppsetr (pp, "grow", CON_GROW(rv))

	call clppsetb (pp, "c_interactive", itob(CON_INTERACTIVE(rv)))
	call clppsetb (pp, "replace", itob(CON_REPLACE(rv)))

	call nam_cninterp (rv, Memc[b1])
	call clppset (pp, "c_function", Memc[b1])

	if (CON_SAMPLE(rv) != NULL)
	    call clppset (pp, "c_sample", Memc[CON_SAMPLE(rv)])

	call clcpset (pp)
	call sfree (sp)
end


# CONT_UNLEARN - Unlearn the pset and replace with the default values.

procedure cont_unlearn (rv)

pointer	rv					#I RV struct pointer

begin
	CON_CNFUNC(rv) = DEF_FUNCTION
	CON_ORDER(rv) = DEF_ORDER
	CON_LOWREJECT(rv) = DEF_LOW_REJECT
	CON_HIGHREJECT(rv) = DEF_HIGH_REJECT
	CON_NITERATE(rv) = DEF_NITERATE
	CON_REPLACE(rv) = DEF_NITERATE
	CON_GROW(rv) = DEF_GROW
	CON_NAVERAGE(rv) = DEF_NAVERAGE
	CON_INTERACTIVE(rv) = DEF_INTERACTIVE

	if (CON_SAMPLE(rv) != NULL)
	    call strcpy (DEF_SAMPLE, Memc[CON_SAMPLE(rv)], SZ_FNAME)
end


# CONT_SHOW - Show the current contin parameters

procedure cont_show (rv, fd)

pointer	rv			#I RV struct pointer
pointer	fd			#I output file descriptor

pointer	sp, str, str1
bool	itob()

begin
	if (fd == NULL)
	    return

	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)
	call salloc (str1,SZ_LINE, TY_CHAR)

	call fprintf (fd, "%21tProcesspars PSET Values\n")
	call fprintf (fd, "%21t-----------------------\n\n")

	# Print the continpars info
	call fprintf (fd, "CONTINUUM parameters:\n")

	call fprintf (fd, "c_interactive%15t= %b\n")
	    call pargb (itob(CON_INTERACTIVE(rv)))
	call fprintf (fd, "c_sample%15t= '%.10s'\n")
	    call pargstr (Memc[CON_SAMPLE(rv)])
	call fprintf (fd, "naverage%15t= %d\n")
	    call pargi (CON_NAVERAGE(rv))
	call fprintf (fd, "c_function%15t= '%.10s'\n")
	    call pargstr (Memc[CON_FUNC(rv)])
	call fprintf (fd, "order%15t= %d\n")
	    call pargi (CON_ORDER(rv))
	call fprintf (fd, "replace%15t= %d\n")
	    call pargb (itob(CON_REPLACE(rv)))
	call fprintf (fd, "low_reject%15t= %g\n")
	    call pargr (CON_LOWREJECT(rv))
	call fprintf (fd, "high_reject%15t= %g\n")
	    call pargr (CON_HIGHREJECT(rv))
	call fprintf (fd, "niterate%15t= %d \n")
	    call pargi (CON_NITERATE(rv))
	call fprintf (fd, "grow%15t= %g\n")
	    call pargr (CON_GROW(rv))

	call fprintf (fd, "\n\n")
	call sfree (sp)
end


# CONT_COLON -- Process the continpars task colon commands.

procedure cont_colon (rv, cmdstr)

pointer	rv				#I pointer to the RV structure
char	cmdstr[SZ_LINE]			#I command string

pointer	sp, cmd, buf
int	strdic()

begin
	call smark (sp)
	call salloc (cmd, SZ_LINE, TY_CHAR)
	call salloc (buf, SZ_LINE, TY_CHAR)

	call sscan (cmdstr)
	call gargwrd (Memc[cmd], SZ_LINE)

	# Unpack the keyword from the string and look it up in the
	# dictionary.  Switch on command and call the appropriate routines.
	switch (strdic(Memc[cmd], Memc[cmd], SZ_FNAME, CONT_KEYWORDS)) {
	case CNT_INTERACTIVE:
	    call cmd_interactive (rv)
	case CNT_SAMPLE:
	    call cmd_sample (rv)
	case CNT_NAVERAGE:
	    call cmd_naverage (rv)
	case CNT_FUNCTION:
	    call cmd_cnfunc (rv)
	case CNT_CN_ORDER:
	    call cmd_cn_order (rv)
	case CNT_REPLACE:
	    call cmd_replace (rv)
	case CNT_LOW_REJECT:
	    call cmd_low_reject (rv)
	case CNT_HIGH_REJECT:
	    call cmd_high_reject (rv)
	case CNT_NITERATE:
	    call cmd_niterate (rv)
	case CNT_GROW:
	    call cmd_grow (rv)
	default:
	    call rv_errmsg ("")
	}

	call sfree (sp)
end


# CMD_INTERACTIVE - Set/Show the interactive continuum subtraction flag.

procedure cmd_interactive (rv)

pointer	rv						#I RV struct pointer

int	nscan(), btoi()
bool	bval, itob()

begin
	call gargb (bval)
	if (nscan() == 2) {
	    CON_INTERACTIVE(rv) = btoi (bval)
	} else {
	    call printf ("continpars.c_interactive = %b")
		call pargb (itob(CON_INTERACTIVE(rv)))
	}
end


# CMD_SAMPLE - Set/Show the sample regions for continuum fitting.

procedure cmd_sample (rv)

pointer	rv

pointer	sp, buf
bool	streq()

begin
	call smark (sp)
	call salloc (buf, SZ_LINE, TY_CHAR)

	call gargstr (Memc[buf], SZ_FNAME)
	if (Memc[buf] != EOS) {
	    if (streq(Memc[buf],"") || streq(Memc[buf]," "))
	        call error (0, "continpars.c_sample specified as empty string.")
	    call strcpy (Memc[buf+1], Memc[CON_SAMPLE(rv)], SZ_LINE)
	} else {
	    call printf ("continpars.c_sample = '%s'")
	        call pargstr (Memc[CON_SAMPLE(rv)])
	}

	call sfree (sp)
end


# CMD_NAVERAGE - Set/Show the number of points to average in the fit.

procedure cmd_naverage (rv)

pointer	rv						#I RV struct pointer

int	ival, nscan()

begin
	call gargi (ival)
	if (nscan() == 2) {
	    CON_NAVERAGE(rv) = ival
	} else {
	    call printf ("continpars.naverage = %d")
		call pargi (CON_NAVERAGE(rv))
	}
end


# CMD_CNFUNC - Set/Show the fitting function used.

procedure cmd_cnfunc (rv)

pointer	rv						#I RV struct pointer

pointer	sp, buf, bp
int	cod_cninterp()

begin
	call smark (sp)
	call salloc (buf, SZ_LINE, TY_CHAR)
	call salloc (bp, SZ_LINE, TY_CHAR)

	call gargstr (Memc[buf], SZ_FNAME)
	if (Memc[buf] != EOS) {
	    CON_CNFUNC(rv) = cod_cninterp (Memc[buf+1])
	    
	} else {
	    call nam_cninterp (rv, Memc[bp])
	    call printf ("continpars.c_function = '%s'")
		call pargstr (Memc[bp])
	}

	call sfree (sp)
end


# CMD_CN_ORDER - Set/Show the order of the function fit.

procedure cmd_cn_order (rv)

pointer	rv						#I RV struct pointer

int	ival, nscan()

begin
	call gargi (ival)
	if (nscan() == 2) {
	    CON_ORDER(rv) = ival
	} else {
	    call printf ("continpars.order = %d")
		call pargi (CON_ORDER(rv))
	}
end


# CMD_REPLACE - Set/Show the replace continuum subtraction flag.

procedure cmd_replace (rv)

pointer rv                                              #I RV struct pointer

int     nscan(), btoi()
bool    bval, itob()

begin
        call gargb (bval)
        if (nscan() == 2) {
            CON_REPLACE(rv) = btoi (bval)
        } else {
            call printf ("continpars.replace = %b")
                call pargb (itob(CON_REPLACE(rv)))
        }
end


# CMD_LOW_REJECT - Set/Show the lower sigma rejection limit.

procedure cmd_low_reject (rv)

pointer	rv						#I RV struct pointer

real	rval
int	nscan()

begin
	call gargr (rval)
	if (nscan() == 2) {
	    CON_LOWREJECT(rv) = rval
	} else {
	    call printf ("continpars.low_reject = %g")
		call pargr (CON_LOWREJECT(rv))
	}
end


# CMD_HIGH_REJECT - Set/Show the upper sigma rejection limit.

procedure cmd_high_reject (rv)

pointer	rv						#I RV struct pointer

real	rval
int	nscan()

begin
	call gargr (rval)
	if (nscan() == 2) {
	    CON_HIGHREJECT(rv) = rval
	} else {
	    call printf ("continpars.high_reject = %g")
		call pargr (CON_HIGHREJECT(rv))
	}
end


# CMD_NITERATE - Set/Show the number of iterations in the fit.

procedure cmd_niterate (rv)

pointer	rv						#I RV struct pointer

int	ival, nscan()

begin
	call gargi (ival)
	if (nscan() == 2) {
	    CON_NITERATE(rv) = ival
	} else {
	    call printf ("continpars.niterate = %d")
		call pargi (CON_NITERATE(rv))
	}
end


# CMD_GROW - Set/Show the rejection growing radius.

procedure cmd_grow (rv)

pointer	rv						#I RV struct pointer

real	rval
int	nscan()

begin
	call gargr (rval)
	if (nscan() == 2) {
	    CON_GROW(rv) = rval
	} else {
	    call printf ("continpars.grow = %g")
		call pargr (CON_GROW(rv))
	}
end