Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
interp.c
Go to the documentation of this file.
1 /*
2 Copyright 2021 Arizona Board of Regents on behalf of the University of Arizona.
3 
4 For commercial uses, to obtain a license to sell and or sublicense copies of
5 the Software please contact the University of Arizona at Tech Launch Arizona:
6 lewish@tla.arizona.edu.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction for non-commercial use educational and
11 research purposes, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, and to permit persons to whom the Software
13 is furnished to do so, subject to the following conditions:
14 
15 The entire above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 
26 1. Additional Required Provisions
27 
28 1.1. Arbitration. The parties agree that if a dispute arises between them
29 concerning this Agreement, the parties may be required to submit the matter to
30 arbitration pursuant to Arizona law.
31 
32 1.2. Applicable Law and Venue. This Agreement shall be interpreted pursuant
33 to the laws of the State of Arizona. Any arbitration or litigation between the
34 Parties shall be conducted in Pima County, ARIZONA, and LICENSEE hereby
35 submits to venue and jurisdiction in Pima County, ARIZONA.
36 
37 1.3. Non-Discrimination. The Parties agree to be bound by state and federal
38 laws and regulations governing equal opportunity and non-discrimination and
39 immigration.
40 
41 1.4. Appropriation of Funds. The Parties recognize that performance by
42 ARIZONA may depend upon appropriation of funds by the State Legislature of
43 ARIZONA. If the Legislature fails to appropriate the necessary funds, or if
44 ARIZONA’S appropriation is reduced during the fiscal year, ARIZONA may cancel
45 this Agreement without further duty or obligation. ARIZONA will notify
46 LICENSEE as soon as reasonably possible after it knows of the loss of funds.
47 
48 1.5. Conflict of Interest. This Agreement is subject to the provisions of
49 A.R.S. 38-511 and other conflict of interest regulations. Within three years
50 of the EFFECTIVE DATE, ARIZONA may cancel this Agreement if any person
51 significantly involved in initiating, negotiating, drafting, securing, or
52 creating this Agreement for or on behalf of ARIZONA becomes an employee or
53 consultant in any capacity of LICENSEE with respect to the subject matter of
54 this Agreement.
55 
56  */
57 
58 
59 #include "extern.h"
60 
61 /*
62  2d array interpolation routine
63  swiped from Darrel, converted to 'C' by Jeff
64 */
65 
66 
67 /*
68  version 1.0 mpifr cyber edition 22 MAY 1977.
69  see nod 2 manual page m 2.1
70  C.G Haslam mar 1969.
71  This routine interpolates the amplitude z at the point y which lies
72  between the points b and c. y=0.0 corresponds to b and y=1.0
73  corresponds to c. a,b,c,d are the amplitudes at 4 points spaced at
74  unit intervals.
75 */
76 
77 
78 
79 double yintpn(a, b, c, d, y)
80 double a, b, c, d, y;
81 {
82  double e, f, g;
83 
84  if(y == 0.0)
85  {
86  return((double)b);
87  }
88 
89  e = b-a; /* exact copy case */
90  f = b-c;
91  g = e + f + f + d - c;
92  g = g*y- e - f - g;
93  g = g*y - a + c;
94 
95  return( (double)(0.5*g*y + b));
96 }
97 
98 /*
99  interp interpolates a 2-d array array into parray.
100  uses lots of memory; PC gymnastics deleted
101 */
102 int interp(array, nx, ny, parray, npx, npy )
103 double *array;
104 int nx, ny;
105 double *parray;
106 int npx, npy;
107 {
108  double *one, *two, *three, *four, *save, *done;
109  double *xone, *xtwo, *xthree, *xfour;
110  int k, i, iy;
111  double y, dy, lasty;
112  double oversampy, oversampx, oversamp;
113 
114  one = (double *)malloc( (unsigned)(sizeof(double) * npx));
115  two = (double *)malloc( (unsigned)(sizeof(double) * npx));
116  three = (double *)malloc( (unsigned)(sizeof(double) * npx));
117  four = (double *)malloc( (unsigned)(sizeof(double) * npx));
118 
119  if(!one || !two || !three || !four)
120  {
121  st_fail("no memory for interpolation");
122  return(1);
123  }
124 
125  xone = &array[0];
126  xtwo = &array[nx];
127  xthree = &array[nx*2];
128 
129  oversampy = (npy-1.0)/(ny-1.0);
130  oversampx = (npx-1.0)/(nx-1.0);
131  oversamp = oversampx < oversampy ? oversampx:oversampy;
132 
133 /*
134  npx = 1 + (int)rint(oversamp * ((double)nx - 1.0));
135  npy = 1 + (int)rint(oversamp * ((double)ny - 1.0));
136  */
137 
138  /* check that we need to interpolate */
139  if(fabs(oversamp - 1.0) < 0.001 )
140  {
141  st_report("interpolation not needed - oversamp is %f", oversamp );
142  return(0);
143  }
144 
145  xinterp( xone, nx, two, npx );
146  xinterp( xtwo, nx, three, npx );
147  xinterp( xthree, nx, four, npx );
148 
149  lasty = 0;
150  /* create first row by extrapolation */
151  for( i=0; i< npx; i++ )
152  {
153  one[i] = two[i] - three[i] + two[i];
154  }
155 
156  for(k=0;k<npy;k++) /* for each new row, interpolate in y */
157  {
158  y = (double)k/oversampy;
159  iy = (int)y;
160 
161  if(iy != lasty) /* check to see if it is time to move the pointers */
162  {
163  save = one;
164  one = two;
165  two = three;
166  three = four;
167  four = save;
168 
169  if(iy+2 < ny) /* interpolate along x */
170  {
171  xfour = &array[(iy+2)*nx];
172  xinterp( xfour, nx, four, npx );
173  }
174  else /* otherwize extrapolate */
175  {
176  for(i=0;i<npx;i++)
177  {
178  four[i] = three[i] - two[i] + three[i];
179  }
180  }
181 
182  lasty = iy;
183  }
184 
185  done = &parray[npx*k];
186  dy = y - (double)iy;
187 
188  for(i=0;i<npx;i++)
189  {
190  done[i] = yintpn( one[i], two[i], three[i], four[i], dy );
191  }
192  }
193 
194  free((char *)one);
195  free((char *)two);
196  free((char *)three);
197  free((char *)four);
198 
199  return(0);
200 }
201 
202 /*
203  subroutine xinterp interpolates the input array x1 onto the output
204  array x2, with nx2 points, oversampling the input array by oversamp
205 */
206 int xinterp(x1, nx1, x2, nx2)
207 double *x1;
208 int nx1;
209 double *x2;
210 int nx2;
211 {
212  double oversamp, x, a, b, c, d, dx;
213  int jx, ix, inx;
214 
215  oversamp = (double)(nx2-1)/(double)(nx1-1);
216 
217  for(jx=0;jx<nx2;jx++) /* jx is address of interpolated array */
218  {
219  x = jx/oversamp;
220  inx = (int)rint(x);
221 
222  if(fabs(x-(double)inx) < 0.0001)
223  {
224  x2[jx]=x1[inx];
225  }
226  else
227  {
228  ix = (int)x;
229  dx = x-ix;
230  b = x1[ix];
231  c = x1[ix+1];
232 
233  if(ix > 0)
234  {
235  a = x1[ix-1];
236  }
237  else
238  {
239  a = b -c +b; /* linear extrapolation for first point */
240  }
241 
242  if(ix+2 < nx1)
243  {
244  d = x1[ix+2];
245  }
246  else
247  {
248  d = c -b +c; /* linear extrapolation for last point */
249  }
250 
251  x2[jx] = yintpn(a, b, c, d, dx);
252  }
253  }
254 
255  return(0);
256 }
int interp(double *array, int nx, int ny, double *parray, int npx, int npy)
Definition: interp.c:102
int xinterp(double *x1, int nx1, double *x2, int nx2)
Definition: interp.c:206
double yintpn(double a, double b, double c, double d, double y)
Definition: interp.c:79
#define dy(i)
void st_fail()
void st_report()
#define dx(i)