aboutsummaryrefslogtreecommitdiff
path: root/math/iminterp/msifit.x
blob: 27d861ffea650cdfe54b932494ba95632adcf85f (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include "im2interpdef.h"
include <math/iminterp.h>

# MSIFIT -- MSIFIT calculates the coefficients of the interpolant.
# With the exception of the bicubic spline interpolant the coefficients
# are stored as the data points.  The 2D B-spline coefficients are
# calculated using the routines II_SPLINE2D. MSIFIT checks that the
# dimensions of the data array are appropriate for the interpolant selected
# and allocates space for the coefficient array.
# Boundary extension is performed using boundary projection.

procedure msifit (msi, datain, nxpix, nypix, len_datain)

pointer	msi			# pointer to interpolant descriptor structure
real	datain[len_datain,ARB]	# data array
int	nxpix			# number of points in the x dimension
int	nypix			# number of points in the y dimension
int	len_datain			# row length of datain

int	i, j
pointer	fptr, nptr, rptr
pointer	tmp
pointer	rptrf[FNROWS]
pointer	rptrl[LNROWS]

errchk	calloc, mfree

begin
	# check the row length of datain
	if (len_datain < nxpix)
	    call error (0, "MSIFIT: Row length of datain too small.")

	# check that the number of data points in x and y is
	# appropriate for the interpolant type selected and
	# allocate space for the coefficient array allowing
	# sufficient storage for boundary extension

	switch (MSI_TYPE(msi)) {

	case II_BINEAREST:

	    if (nxpix < 1 || nypix < 1) {
		call error (0, "MSIFIT: Too few data points.")
		return
	    } else {
		MSI_NXCOEFF(msi) = nxpix
		MSI_NYCOEFF(msi) = nypix
		MSI_FSTPNT(msi) = 0
		if (MSI_COEFF(msi) != NULL)
		    call mfree (MSI_COEFF(msi), TY_REAL)
		call malloc (MSI_COEFF(msi), nxpix * nypix, TY_REAL)
	    }

	case II_BILINEAR, II_BIDRIZZLE:

	    if (nxpix < 2 || nypix < 2) {
		call error (0, "MSIFIT: Too few data points.")
		return
	    } else {
		MSI_NXCOEFF(msi) = nxpix + 1
		MSI_NYCOEFF(msi) = nypix + 1
		MSI_FSTPNT(msi) = 0
		if (MSI_COEFF(msi) != NULL)
		    call mfree (MSI_COEFF(msi), TY_REAL)
		call malloc (MSI_COEFF(msi),
			     MSI_NXCOEFF(msi) * MSI_NYCOEFF(msi), TY_REAL)
	    }

	case II_BIPOLY3:

	    if (nxpix < 4 || nypix < 4) {
		call error (0, "MSIFIT: Too few data points.")
		return
	    } else {
		MSI_NXCOEFF(msi) = nxpix + 3
		MSI_NYCOEFF(msi) = nypix + 3
		MSI_FSTPNT(msi) = MSI_NXCOEFF(msi) + 1
		if (MSI_COEFF(msi) != NULL)
		    call mfree (MSI_COEFF(msi), TY_REAL)
		call malloc (MSI_COEFF(msi),
			     MSI_NXCOEFF(msi) * MSI_NYCOEFF(msi), TY_REAL)
	    }

	case II_BIPOLY5:

	    if (nxpix < 6 || nypix < 6) {
		call error (0, "MSIFIT: Too few data points.")
		return
	    } else {
		MSI_NXCOEFF(msi) = nxpix + 5
		MSI_NYCOEFF(msi) = nypix + 5
		MSI_FSTPNT(msi) = 2 * MSI_NXCOEFF(msi) + 2
		if (MSI_COEFF(msi) != NULL)
		    call mfree (MSI_COEFF(msi), TY_REAL)
		call malloc (MSI_COEFF(msi),
			     MSI_NXCOEFF(msi) * MSI_NYCOEFF(msi), TY_REAL)
	    }

	case II_BISPLINE3:

	    if (nxpix < 4 || nypix < 4) {
		call error (0, "MSIFIT: Too few data points.")
		return
	    } else {
		MSI_NXCOEFF(msi) = nxpix + 3
		MSI_NYCOEFF(msi) = nypix + 3
		MSI_FSTPNT(msi) = MSI_NXCOEFF(msi) + 1
		if (MSI_COEFF(msi) != NULL)
		    call mfree (MSI_COEFF(msi), TY_REAL)
		call calloc (MSI_COEFF(msi),
			     MSI_NXCOEFF(msi) * MSI_NYCOEFF(msi), TY_REAL)
	    }

	case II_BISINC, II_BILSINC:

	    if (nxpix < 1 || nypix < 1) {
		call error (0, "MSIFIT: Too few data points.")
		return
	    } else {
		MSI_NXCOEFF(msi) = nxpix
		MSI_NYCOEFF(msi) = nypix
		MSI_FSTPNT(msi) = 0
		if (MSI_COEFF(msi) != NULL)
		    call mfree (MSI_COEFF(msi), TY_REAL)
		call calloc (MSI_COEFF(msi), nxpix * nypix, TY_REAL)
	    }

	}

	# index the coefficient pointer so that COEFF(fptr+1) points to the
	# first data point in the coefficient array
	fptr = MSI_COEFF(msi) - 1 + MSI_FSTPNT(msi)

	# load data into coefficient array
	rptr = fptr
	do j = 1, nypix {
	    call amovr (datain[1,j], COEFF(rptr+1), nxpix)
	    rptr = rptr + MSI_NXCOEFF(msi)
	}

	# calculate the coefficients of the interpolant
	# boundary extension is performed using boundary projection

	switch (MSI_TYPE(msi)) {

	case II_BINEAREST, II_BISINC, II_BILSINC:

	    # no end conditions necessary, coefficients stored as data

	case II_BILINEAR, II_BIDRIZZLE:

	    # extend the rows
	    rptr = fptr + nxpix
	    do j = 1, nypix {
		COEFF(rptr+1) = 2. * COEFF(rptr) - COEFF(rptr-1)
		rptr = rptr + MSI_NXCOEFF(msi)
	    }

	    # define the pointers to the last, 2nd last and third last rows
	    rptrl[1] = MSI_COEFF(msi) + (MSI_NYCOEFF(msi) - 1) *
	    	       MSI_NXCOEFF(msi)
	    do i = 2, 3
		rptrl[i] = rptrl[i-1] - MSI_NXCOEFF(msi)

	    # define the last row by extending the columns
	    call awsur (COEFF(rptrl[2]), COEFF(rptrl[3]), COEFF(rptrl[1]),
	    		MSI_NXCOEFF(msi), 2., -1.)
	    
	case II_BIPOLY3:

	    # extend the rows
	    rptr = fptr
	    nptr = fptr + nxpix
	    do j = 1, nypix {
		COEFF(rptr) = 2. * COEFF(rptr+1) - COEFF(rptr+2)
		COEFF(nptr+1) = 2. * COEFF(nptr) - COEFF(nptr-1)
		COEFF(nptr+2) = 2. * COEFF(nptr) - COEFF(nptr-2)
		rptr = rptr + MSI_NXCOEFF(msi)
		nptr = nptr + MSI_NXCOEFF(msi)
	    }

	    # define pointers to first, second and third rows
	    rptrf[1] = MSI_COEFF(msi) 
	    do i = 2, 3
		rptrf[i] = rptrf[i-1] + MSI_NXCOEFF(msi)

	    # extend the columns, first row
	    call awsur (COEFF(rptrf[2]), COEFF(rptrf[3]), COEFF(rptrf[1]),
	    		MSI_NXCOEFF(msi), 2., -1.)
	    
	    # define the pointers to the last to fifth last rows
	    rptrl[1] = MSI_COEFF(msi) + (MSI_NYCOEFF(msi) - 1) *
	    	       MSI_NXCOEFF(msi) 
	    do i = 2, 5
		rptrl[i] = rptrl[i-1] - MSI_NXCOEFF(msi)

	    # extend the columns, define 2nd last row
	    call awsur (COEFF(rptrl[3]), COEFF(rptrl[4]), COEFF(rptrl[2]),
	    		MSI_NXCOEFF(msi), 2., -1.)
	    
	    # extend the columns, define last row
	    call awsur (COEFF(rptrl[3]), COEFF(rptrl[5]), COEFF(rptrl[1]),
	    		MSI_NXCOEFF(msi), 2., -1.)
	    
	case II_BIPOLY5:

	    # extend the rows
	    rptr = fptr
	    nptr = fptr + nxpix
	    do j = 1, nypix {
		COEFF(rptr-1) = 2. * COEFF(rptr+1) - COEFF(rptr+3)
		COEFF(rptr) = 2. * COEFF(rptr+1) - COEFF(rptr+2)
		COEFF(nptr+1) = 2. * COEFF(nptr) - COEFF(nptr-1)
		COEFF(nptr+2) = 2. * COEFF(nptr) - COEFF(nptr-2)
		COEFF(nptr+3) = 2. * COEFF(nptr) - COEFF(nptr-3)
		rptr = rptr + MSI_NXCOEFF(msi)
		nptr = nptr + MSI_NXCOEFF(msi)
	    }

	    # define pointers to first five rows
	    rptrf[1] = MSI_COEFF(msi) 
	    do i = 2, 5
		rptrf[i] = rptrf[i-1] + MSI_NXCOEFF(msi)

	    # extend the columns, define first row
	    call awsur (COEFF(rptrf[3]), COEFF(rptrf[5]), COEFF(rptrf[1]),
	    		MSI_NXCOEFF(msi), 2., -1.)
	    
	    # extend the columns, define second row
	    call awsur (COEFF(rptrf[3]), COEFF(rptrf[4]), COEFF(rptrf[2]),
	    		MSI_NXCOEFF(msi), 2., -1.)
	    
	    # define pointers last seven rows
	    rptrl[1] = MSI_COEFF(msi) + (MSI_NYCOEFF(msi) - 1) *
	    	       MSI_NXCOEFF(msi)
	    do i = 2, 7
		rptrl[i] = rptrl[i-1] - MSI_NXCOEFF(msi) 

	    # extend the columns, last row
	    call awsur (COEFF(rptrl[4]), COEFF(rptrl[7]), COEFF(rptrl[1]),
	    		MSI_NXCOEFF(msi), 2., -1.)

	    # extend the columns, 2nd last row
	    call awsur (COEFF(rptrl[4]), COEFF(rptrl[6]), COEFF(rptrl[2]),
	    		MSI_NXCOEFF(msi), 2., -1.)

	    # extend the columns, 3rd last row
	    call awsur (COEFF(rptrl[4]), COEFF(rptrl[5]), COEFF(rptrl[3]),
	    		MSI_NXCOEFF(msi), 2., -1.)

	case II_BISPLINE3:
		
	    # allocate space for a temporary work arrays
	    call calloc (tmp, MSI_NXCOEFF(msi) * MSI_NYCOEFF(msi), TY_REAL)

	    # the B-spline coefficients are calculated using the
	    # natural end conditions, end coefficents are set to
	    # zero

	    # calculate the univariate B_spline coefficients in x
	    call ii_spline2d (COEFF(MSI_COEFF(msi)), TEMP(tmp),
	    	nxpix, MSI_NYCOEFF(msi), MSI_NXCOEFF(msi), MSI_NYCOEFF(msi))


	    # calculate the univariate B-spline coefficients in y to
	    # results of x interpolation
	    call ii_spline2d (TEMP(tmp), COEFF(MSI_COEFF(msi)),
	        nypix, MSI_NXCOEFF(msi), MSI_NYCOEFF(msi), MSI_NXCOEFF(msi))

	    # deallocate storage for temporary arrays
	    call mfree (tmp, TY_REAL)
	}
end