aboutsummaryrefslogtreecommitdiff
path: root/noao/rv/prepspec.x
blob: e309e692ebb6388ab17dac71af84c71ff2faeea6 (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
include <math.h>
include "rvpackage.h"
include "rvflags.h"
include "rvsample.h"

# PREP_SPEC - Prepare the input spectra prior to a Fourier transform.  Data
# are centered in the transform array according to the number of points covered
# by both spectra.  Apply the DC bias and the cosine bell.

procedure prep_spec (rv, ssp, npts, fftnpts, datanpts, data, tform, ishift, 
    apodize)

pointer	rv				#I RV struct pointer
pointer	ssp				#I Sample struct pointer
int	npts				#I npts in global data array
int	fftnpts				#I Output size of prepared array
int	datanpts			#I npts in original data array
pointer	data				#I pointer to original temp space
pointer	tform				#I Prepared transform work space
int	ishift				#I Shift of spectrum
int	apodize				#I Apodize the data?

pointer sp, tp
int	i, iremain, num
real	dc, rv_avgpix()

define	FORWARD			-1

begin
	call smark (sp)
	call salloc (tp, fftnpts, TY_REAL)

	# Do some error checking.
	if (data == NULL || tform == NULL) 
	    call error (0, "NULL pointer passed to prep_spec()")
	else if (ishift < 0)
	    call error (0, "Negative shift found preparing spectrum.")

	if (DEBUG(rv)) {
	    call d_printf (DBG_FD(rv), "prep_spec:\t")
	    call d_printf (DBG_FD(rv), "ishift=%d np=%d dnp=%d fnp=%d\n")
		call pargi (ishift); call pargi(npts)
		call pargi (datanpts); call pargi(fftnpts)
	}

	# Get things started.
	call aclrr (Memr[tform], fftnpts)
	#call amovr (Memr[data], Memr[tform+ishift], npts)
	call amovr (Memr[data], Memr[tform+ishift], datanpts)

	# Generate size dependent parameters.
	if (SR_COUNT(ssp) != ALL_SPECTRUM && apodize == YES) {
	    #call prep_samples (rv, ssp, Memr[tform+ishift], npts, apodize)
	    call prep_samples (rv, ssp, Memr[tform], npts, apodize)
	} else {
	    num = datanpts
	    if (apodize == YES) {
	        call fft_cosbel (Memr[tform+ishift], num, FORWARD, 
		    RV_APODIZE(rv))
	    }
	}

	# Center the data in the transform arrays.
	if (fftnpts != npts) {
	    call aclrr (Memr[tp], fftnpts)
	    call amovr (Memr[tform], Memr[tp], npts)
	    call aclrr (Memr[tform], fftnpts)
	    iremain = (fftnpts-npts) / 2
	    do i = 1,npts 				# get centered
	        Memr[tform+iremain+i-1] = Memr[tp+i-1]
	} else
	    iremain = 0

	# Now compute the bias to raise zero level to spectrum mean.
	dc = rv_avgpix (Memr[tform], fftnpts)		  # compute DC level
	call asubkr (Memr[tform], dc, Memr[tform], fftnpts) # apply DC bias

	call sfree (sp)
end


# PREP_SAMPLES - Prepare the data by masking according to the samples and
# apodizing each region.

procedure prep_samples (rv, ssp, data, npts, apodize)

pointer	rv					#I RV struct pointer
pointer	ssp					#I Sample struct pointer
real	data[ARB]				#U Data array
int	npts					#I Npts in array
int	apodize					#I Apodize the data?

int	i, j, k, np, left, right

begin
	left = 1
	if (SR_UNITS(ssp) == PIXELS) {
	    #right = nint (SRANGE(ssp,1) - RV_GLOB_W1(rv) + 1)
	    right = SRANGE(ssp,1) - RV_GLOB_W1(rv) + 1
	} else {
	    right = nint ((log10(SRANGE(ssp,1)) - RV_GLOB_W1(rv)) / 
		RV_OWPC(rv)) + 1
	}
	do i = 1, SR_COUNT(ssp)+1 {
	    if (DEBUG(rv)) {
		call d_printf (DBG_FD(rv), "\tl=%g r=%g -- s=%g e=%g\n")
		    call pargi (left) ; call pargi (right)
		    call pargr (SRANGE(ssp,i)) ; call pargR (ERANGE(ssp,i))
	    }

	    k = 1
	    j = left
	    np = right - left + 1
	    while (k <= np) {
	        data[j] = 0.0
	        j = j + 1
		k = k + 1
	    }
	    if (i == SR_COUNT(ssp)+1) 
	    	return

	    if (SR_UNITS(ssp) == PIXELS) {
	        #left = nint ((ERANGE(ssp,i) - RV_GLOB_W1(rv) + 1) + 1)
	        left = (ERANGE(ssp,i) - RV_GLOB_W1(rv) + 1) + 1
	    } else {
	        left = nint ((log10(ERANGE(ssp,i)) - RV_GLOB_W1(rv)) / 
		    RV_OWPC(rv)) + 1
	    }
	    if (apodize == YES)
	        call fft_cosbel (data[right], left-right+1, -1, RV_APODIZE(rv))
	    if (i == SR_COUNT(ssp)) {
		right = npts
	    } else {
		if (SR_UNITS(ssp) == PIXELS) {
	    	    right = SRANGE(ssp,i+1) - RV_GLOB_W1(rv) + 1
		} else {
	    	    right = nint ((log10(SRANGE(ssp,i+1)) - RV_GLOB_W1(rv)) / 
			RV_OWPC(rv)) + 1
		}
	    }
	}
end