File: icsubcon0.x

package info (click to toggle)
iraf-rvsao 2.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 16,456 kB
  • sloc: ansic: 963; lisp: 651; fortran: 397; makefile: 27
file content (593 lines) | stat: -rw-r--r-- 15,222 bytes parent folder | download
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
# File rvsao/Util/icsubcon.x
# July 16, 2009
# By Doug Mink, Harvard-Smithsonian Center for Astrophysics
# After IRAF RV.CONTINUUM subroutine

include <pkg/gtools.h>
include	<error.h>
include "contin.h"
include	"rvsao.h"
include	"emv.h"


# ICSUBCON - Do the continuum normalization, with optional interaction

procedure icsubcon (n, fdata, wltemp, name, spectype, nsm, fit)

int	n			# Number of pixels to fit
real	fdata[ARB]		# Spectrum to be fit
real	wltemp[ARB]		# Wavelengths for spectrum
char	name[ARB]		# Name of spectrum
int	spectype		# 1=spectrum, 2=template
int	nsm			# Number of times to smooth spectrum
real	fit[ARB]		# Fit array (Returned)

int	i,newrej,nrej,ns,nsp
char	title_line[SZ_LINE]
char	wlab[SZ_LINE]
double	tfit
real	fitmean			# Mean continuum value for scaling
bool	chop
real	cveval()
pointer	spec0
pointer	ic			# ICFIT ptr
pointer	gt			# GTOOLS pointer
pointer	gp			# graphics pointer
pointer	cv			# pointer for curve fitting
pointer	sp
pointer	x, wts, rejpts		# buffers for curve fitting
pointer gopen(), gt_init()
errchk  gopen, gt_init

include "contin.com"
include	"rvsao.com"
include	"emv.com"

begin
	call strcpy ("Wavelength in Angstroms", wlab, SZ_LINE)

# If the order to be fit is zero, return spectrum unchanged
	if (order < 1) {
	    do i = 1, n {
		fit[i] = fdata[i]
		}
	    return
	    }

# Check to see if spectrum is all zeroes
	nsp = 0
	do i = 1, n {
	    if (fdata[i] != 0.0)
		nsp = nsp + 1
	    }

# If spectrum is all zeroes, return it unchanged with fit all zeroes, too
	if (nsp < 1) {
	    do i = 1, n {
		fit[i] = fdata[i]
		}
	    return
	    }

# Set the ICFIT pointer structure.
	call ic_open (ic)
	call ic_pstr (ic, "sample", sample)
	call ic_pstr (ic, "function", confunc)
	call ic_puti (ic, "naverage", naverage)
	call ic_puti (ic, "order", order)
	call ic_puti (ic, "niterate", niterate)
	call ic_putr (ic, "low", lowrej[spectype])
	call ic_putr (ic, "high", hirej[spectype])
	call ic_putr (ic, "grow", grow)
	call ic_pstr (ic, "ylabel", "")

#  Allocate memory for curve fitting.
	call smark (sp)
	call salloc (x, n, TY_REAL)
	call salloc (wts, n, TY_REAL)
	call salloc (rejpts, n, TY_INT)

#  Initlialize WTS array
	call amovkr (1., Memr[wts], n)

#  Avoid fitting zeroes at ends of spectrum
	do i = 1, n {
	    if (fdata[i] == 0.) {
		Memr[wts+i-1] = 0.
#		if (debug) {
#		    call printf ("ICSUBCON: %d = 0\n")
#			call pargi (i)
#		    call flush (STDOUT)
#		    }
		}
	    else
		break
	    }
	do i = n, 1, -1 {
	    if (fdata[i] == 0.) {
		Memr[wts+i-1] = 0.
#		if (debug) {
#		    call printf ("ICSUBCON: %d = 0\n")
#			call pargi (i)
#		    call flush (STDOUT)
#		    }
		}
	    else
		break
	    }
	call amovki (NO, Memi[rejpts], n)

#  Initlialize X array
	do i = 1, n {
	    Memr[x+i-1] = real (i)
#	    if (debug) {
#		call printf ("ICSUBCON: %d=%f: %f (%f)\n")
#		    call pargi (i)
#		    call pargr (Memr[x+i-1])
#		    call pargr (fdata[i])
#		    call pargr (Memr[wts+i-1])
#		call flush (STDOUT)
#		}
	    }

	if (spectype == 1)
	    chop = schop
	else if (spectype == 2)
	    chop = tchop

#  Update icfit struct
	call ic_putr (ic, "xmin", 1.)
	call ic_putr (ic, "xmax", real(n))

#	if (debug) {
#	    call printf ("ICSUBCON: %d points being fit from spectrum %d\n")
#		call pargi (n)
#		call pargi (spectype)
#	    call flush (STDOUT)
#	    }

#  If the interactive flag is set then use icg_fit to set the fitting
#  parameters.  Only done if task is run interactively as well.

	if (interact) {
	    iferr {
		gp = gopen ("stdgraph", NEW_FILE, STDGRAPH)
		gt = gt_init ()
		call gt_sets (gt, GTTYPE, "line")
		} then
		    call error (0, "Error opening `stdgraph'.")

	    call icg_fit (ic,gp,"cursor",gt,cv,Memr[x],fdata,Memr[wts],n)
	
	# Now recover any parameters that were changed
	    call recover_icfit_pars (ic, spectype)
	    call gt_free (gt)
	    call gclose (gp)
	    }

#  Do the fit non-interactively using ic_fit.
	else {
	    call ic_fit (ic,cv,Memr[x],fdata,Memr[wts],n,YES,YES,YES,YES)
	    }

#  Save original spectrum if plotting and chopping emission lines
	spec0 = NULL
	if (pltcon && chop) {
	    call salloc (spec0,n,TY_REAL)
	    do i = 1, n {
		Memr[spec0+i-1] = fdata[i]
		}
	    }

#  Find deviant points
	if (chop && (abrej[spectype] > 0. || emrej[spectype] > 0.)) {
	    nrej = 0
	    newrej = 0
	    call ic_deviantr (cv, Memr[x],fdata,Memr[wts], Memi[rejpts],
			      n,abrej[spectype],emrej[spectype],grow,NO,
			      nrej,newrej)

#  Replace points out of bounds with continuum
	    do i = 1, n {
		if (Memi[rejpts+i-1] == YES)
		    fdata[i] = cveval (cv, Memr[x+i-1])
		}

# If plot enabled, plot the lines that were removed
	    if (pltcon && newrej > 0) {
		do i = 1, n {
		    Memr[spec0+i-1] = Memr[spec0+i-1] - fdata[i]
		    }
		call strcpy (name, title_line, SZ_LINE)
		if (emrej[spectype] > abrej[spectype])
		    call strcat (" absorption lines", title_line, SZ_LINE)
		else
		    call strcat (" emission lines", title_line, SZ_LINE)
		ns = nsm
		call plotspec (n,Memr[spec0],title_line,wltemp, wlab, ns)
		}
	    }

#  Now remove the fit continuum
	call cvvector (cv, Memr[x], fit, n)
	if (conproc == DIVCONT || conproc == ZEROCONT) {
	    tfit = 0.d0
	    do i = 1, n {
		tfit = tfit + fit[i]
		}
	    fitmean = tfit / double (n)
	    do i = 1, n {
		if (Memr[wts+i-1] > 0.) {
		    if (fit[i] <= 0.0)
			fdata[i] = 1.0
		    else
			fdata[i] = fdata[i] / fit[i]
		    }
		}
#	    call adivr (fdata, fit, fdata, n)
	    if (conproc == ZEROCONT)
		call asubkr (fdata, 1.0, fdata, n)
	    do i = 1, n {
		fdata[i] = fdata[i] * fitmean
		}
	    }
	else if (conproc == SUBCONT) {
	    do i = 1, n {
		if (Memr[wts+i-1] > 0.)
		    fdata[i] = fdata[i] - fit[i]
		}
#	    call asubr (fdata, fit, fdata, n)
	    }
	else if (conproc == CONTFIT) {
	    do i = 1, n {
		fdata[i] = fit[i]
		}
	    }

# If plot enabled, show the object spectrum with continuum removed
	if (pltcon) {
	    call strcpy (name, title_line, SZ_LINE)
	    if (conproc == NOCONT)
		call strcat (" continuum NOT removed", title_line, SZ_LINE)
	    else if (conproc == CONTFIT)
		call strcat (" continuum", title_line, SZ_LINE)
	    else
		call strcat (" continuum removed", title_line, SZ_LINE)
	    ns = nsm
	    call plotspec (n,fdata,title_line,wltemp, wlab, ns)
	    }

	call ic_closer (ic)
	call cvfree (cv)
	call sfree (sp)
	return
end


# ICSUBCONY - Do the continuum normalization, with optional interaction
#             Set range of counts/flux in graph

procedure icsubcony (n, fdata, wltemp, name, spectype, nsm, fit, ymin, ymax)

int	n			# Number of pixels to fit
real	fdata[ARB]		# Spectrum to be fit
real	wltemp[ARB]		# Wavelengths for spectrum
char	name[ARB]		# Name of spectrum
int	spectype		# 1=spectrum, 2=template
int	nsm			# Number of times to smooth spectrum
real	fit[ARB]		# Fit array (Returned)
real	ymin, ymax		# Minimum, maximum flux/count values to plot

int	i,newrej,nrej,ns,nsp
char	title_line[SZ_LINE]
char	wlab[SZ_LINE]
double	tfit
real	fitmean			# Mean continuum value for scaling
bool	chop
real	cveval()
pointer	spec0
pointer	ic			# ICFIT ptr
pointer	gt			# GTOOLS pointer
pointer	gp			# graphics pointer
pointer	cv			# pointer for curve fitting
pointer	sp
pointer	x, wts, rejpts		# buffers for curve fitting
pointer gopen(), gt_init()
errchk  gopen, gt_init

include "contin.com"
include	"rvsao.com"
include	"emv.com"

begin
	call strcpy ("Wavelength in Angstroms", wlab, SZ_LINE)

# If the order to be fit is zero, return spectrum unchanged
	if (order < 1) {
	    do i = 1, n {
		fit[i] = fdata[i]
		}
	    return
	    }

# Check to see if spectrum is all zeroes
	nsp = 0
	do i = 1, n {
	    if (fdata[i] != 0.0)
		nsp = nsp + 1
	    }

# If spectrum is all zeroes, return it unchanged with fit all zeroes, too
	if (nsp < 1) {
	    do i = 1, n {
		fit[i] = fdata[i]
		}
	    return
	    }

# Set the ICFIT pointer structure.
	call ic_open (ic)
	call ic_pstr (ic, "sample", sample)
	call ic_pstr (ic, "function", confunc)
	call ic_puti (ic, "naverage", naverage)
	call ic_puti (ic, "order", order)
	call ic_puti (ic, "niterate", niterate)
	call ic_putr (ic, "low", lowrej[spectype])
	call ic_putr (ic, "high", hirej[spectype])
	call ic_putr (ic, "grow", grow)
	call ic_pstr (ic, "ylabel", "")

#  Allocate memory for curve fitting.
	call smark (sp)
	call salloc (x, n, TY_REAL)
	call salloc (wts, n, TY_REAL)
	call salloc (rejpts, n, TY_INT)

#  Initlialize WTS array
	call amovkr (1., Memr[wts], n)

#  Avoid fitting zeroes at ends of spectrum
	do i = 1, n {
	    if (fdata[i] == 0.) {
		Memr[wts+i-1] = 0.
#		if (debug) {
#		    call printf ("ICSUBCON: %d = 0\n")
#			call pargi (i)
#		    call flush (STDOUT)
#		    }
		}
	    else
		break
	    }
	do i = n, 1, -1 {
	    if (fdata[i] == 0.) {
		Memr[wts+i-1] = 0.
#		if (debug) {
#		    call printf ("ICSUBCON: %d = 0\n")
#			call pargi (i)
#		    call flush (STDOUT)
#		    }
		}
	    else
		break
	    }
	call amovki (NO, Memi[rejpts], n)

#  Initlialize X array
	do i = 1, n {
	    Memr[x+i-1] = real (i)
#	    if (debug) {
#		call printf ("ICSUBCON: %d=%f: %f (%f)\n")
#		    call pargi (i)
#		    call pargr (Memr[x+i-1])
#		    call pargr (fdata[i])
#		    call pargr (Memr[wts+i-1])
#		call flush (STDOUT)
#		}
	    }

	if (spectype == 1)
	    chop = schop
	else if (spectype == 2)
	    chop = tchop

#  Update icfit struct
	call ic_putr (ic, "xmin", 1.)
	call ic_putr (ic, "xmax", real(n))

#	if (debug) {
#	    call printf ("ICSUBCON: %d points being fit from spectrum %d\n")
#		call pargi (n)
#		call pargi (spectype)
#	    call flush (STDOUT)
#	    }

#  If the interactive flag is set then use icg_fit to set the fitting
#  parameters.  Only done if task is run interactively as well.

	if (interact) {
	    iferr {
		gp = gopen ("stdgraph", NEW_FILE, STDGRAPH)
		gt = gt_init ()
		call gt_sets (gt, GTTYPE, "line")
		} then
		    call error (0, "Error opening `stdgraph'.")

	    call icg_fit (ic,gp,"cursor",gt,cv,Memr[x],fdata,Memr[wts],n)
	
	# Now recover any parameters that were changed
	    call recover_icfit_pars (ic, spectype)
	    call gt_free (gt)
	    call gclose (gp)
	    }

#  Do the fit non-interactively using ic_fit.
	else {
	    call ic_fit (ic,cv,Memr[x],fdata,Memr[wts],n,YES,YES,YES,YES)
	    }

#  Save original spectrum if plotting and chopping emission lines
	spec0 = NULL
	if (pltcon && chop) {
	    call salloc (spec0,n,TY_REAL)
	    do i = 1, n {
		Memr[spec0+i-1] = fdata[i]
		}
	    }

#  Find deviant points
	if (chop && (abrej[spectype] > 0. || emrej[spectype] > 0.)) {
	    nrej = 0
	    newrej = 0
	    call ic_deviantr (cv, Memr[x],fdata,Memr[wts], Memi[rejpts],
			      n,abrej[spectype],emrej[spectype],grow,NO,
			      nrej,newrej)

#  Replace points out of bounds with continuum
	    do i = 1, n {
		if (Memi[rejpts+i-1] == YES)
		    fdata[i] = cveval (cv, Memr[x+i-1])
		}

# If plot enabled, plot the lines that were removed
	    if (pltcon && newrej > 0) {
		do i = 1, n {
		    Memr[spec0+i-1] = Memr[spec0+i-1] - fdata[i]
		    }
		call strcpy (name, title_line, SZ_LINE)
		if (emrej[spectype] > abrej[spectype])
		    call strcat (" absorption lines", title_line, SZ_LINE)
		else
		    call strcat (" emission lines", title_line, SZ_LINE)
		ns = nsm
		call plotsum (n,Memr[spec0],title_line,wltemp, wlab, ns, ymin, ymax)
		}
	    }

#  Now remove the fit continuum
	call cvvector (cv, Memr[x], fit, n)
	if (conproc == DIVCONT || conproc == ZEROCONT) {
	    tfit = 0.d0
	    do i = 1, n {
		tfit = tfit + fit[i]
		}
	    fitmean = tfit / double (n)
	    do i = 1, n {
		if (Memr[wts+i-1] > 0.) {
		    if (fit[i] <= 0.0)
			fdata[i] = 1.0
		    else
			fdata[i] = fdata[i] / fit[i]
		    }
		}
#	    call adivr (fdata, fit, fdata, n)
	    if (conproc == ZEROCONT)
		call asubkr (fdata, 1.0, fdata, n)
	    do i = 1, n {
		fdata[i] = fdata[i] * fitmean
		}
	    }
	else if (conproc == SUBCONT) {
	    do i = 1, n {
		if (Memr[wts+i-1] > 0.)
		    fdata[i] = fdata[i] - fit[i]
		}
#	    call asubr (fdata, fit, fdata, n)
	    }
	else if (conproc == CONTFIT) {
	    do i = 1, n {
		fdata[i] = fit[i]
		}
	    }

# If plot enabled, show the object spectrum with continuum removed
	if (pltcon) {
	    call strcpy (name, title_line, SZ_LINE)
	    if (conproc == NOCONT)
		call strcat (" continuum NOT removed", title_line, SZ_LINE)
	    else if (conproc == CONTFIT)
		call strcat (" continuum", title_line, SZ_LINE)
	    else
		call strcat (" continuum removed", title_line, SZ_LINE)
	    ns = nsm
	    call plotsum (n,fdata,title_line,wltemp, wlab, ns, ymin, ymax)
	    }

	call ic_closer (ic)
	call cvfree (cv)
	call sfree (sp)
	return
end


# RECOVER_ICFIT_PARS - Since the ICFIT parameters may have been changed in
# an interactive operation, we need to get the new values from the ICFIT
# structure.

procedure recover_icfit_pars (ic,intype)

pointer	ic		# ICFIT pointer
int	intype		# 1=spectrum, 2=template

int	strdic(), ic_geti()
real	ic_getr()
include "contin.com"

begin
	naverage = ic_geti (ic, "naverage")
	order = ic_geti (ic, "order")
	niterate = ic_geti (ic, "niterate")
	lowrej[intype] = ic_getr (ic, "low")
	hirej[intype] = ic_getr (ic, "high")
	grow = ic_getr (ic, "grow")

	call ic_gstr (ic, "sample", sample, SZ_LINE)
	call ic_gstr (ic, "function", confunc, SZ_LINE)
	function = strdic (confunc, confunc, SZ_LINE, CN_INTERP_MODE)
	if (function == 0)
	    call error (0, "Unknown fitting function type")
	return
end
# Nov 18 1991	Use malloc instead of salloc
# Nov 20 1991	Free buffers before allocating them
# Dec 16 1991	Plot spectrum and emission lines removed
#		Fix bug by filling in rejected points
#		Chop emission lines only if chop is set
# Dec 17 1991	Don't plot emission lines if none have been removed

# Oct 22 1992	Always free spec0 before returning

# Apr 13 1994	Cleanup code after ftnchek
# Apr 26 1994	Clean up code
# May  3 1994	Add smoothed vector argument to PLOTSPEC call
# Jun 15 1994	Add smoothing argument to PLOTSPEC call
# Aug  3 1994	Use separate template and spectrum emission line chopping flags
# Aug  3 1994	Change common and header from fquot to rvsao

# Mar 13 1995	Allow line chopping in absorption as well as emission
# Mar 15 1995	Add argument for name of spectrum
# May 15 1995	Add template-driven option to divide, not subtract, continuum
# Oct 16 1995	Try salloc instead of malloc

# Dec 13 1996	Remove zeroes and negative numbers from continuum if dividing
# Dec 16 1996	Subtract one after dividing continuum

# May  6 1997	Add smoothing parameter as argument
# Dec 22 1997	Add units to wavelength graph label

# Aug 16 1999	Comment out subtraction of 1 when dividing out continuum
# Aug 18 1999	Add ZEROCONT for continuum divided - 1
# Aug 18 1999	Use int conproc instead of bool divcon

# Jan 25 2000	Add option to not remove continuum

# Apr 26 2001	Drop zeroed pixels at spectrum ends from continuum fit

# May  7 2004	Add option to return continuum fit in place of spectrum

# Apr 25 2006	Return fit and spectrum as all zeroes if spectrum is all zeroes
# Jul  7 2006	If dividing out continuum and it is zero or negative, set to 1
# Sep 18 2006	Add ICSUBCONY to plot continuum with flux/count limits

# Jul 16 2009	Renormalize by mean continuum if dividing out spectrum
# Jul 16 2009	Return spectrum unchanged if order is zero