1 |
– |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* data.c - routines dealing with interpolated data. |
9 |
– |
* |
10 |
– |
* 6/4/86 |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "standard.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
< |
#include "color.h" |
10 |
> |
#include <time.h> |
11 |
|
|
12 |
+ |
#include "platform.h" |
13 |
+ |
#include "paths.h" |
14 |
+ |
#include "standard.h" |
15 |
+ |
#include "color.h" |
16 |
|
#include "resolu.h" |
17 |
< |
|
17 |
> |
#include "view.h" |
18 |
|
#include "data.h" |
19 |
|
|
20 |
|
/* picture memory usage before warning */ |
21 |
|
#ifndef PSIZWARN |
22 |
< |
#ifdef BIGMEM |
22 |
> |
#ifdef SMLMEM |
23 |
|
#define PSIZWARN 3000000 |
24 |
|
#else |
25 |
< |
#define PSIZWARN 1000000 |
25 |
> |
#define PSIZWARN 50000000 |
26 |
|
#endif |
27 |
|
#endif |
28 |
|
|
29 |
|
#ifndef TABSIZ |
30 |
< |
#define TABSIZ 97 /* table size (prime) */ |
30 |
> |
#define TABSIZ 997 /* table size (prime) */ |
31 |
|
#endif |
32 |
|
|
33 |
|
#define hash(s) (shash(s)%TABSIZ) |
34 |
|
|
35 |
|
|
37 |
– |
extern char *getlibpath(); /* library search path */ |
38 |
– |
|
36 |
|
static DATARRAY *dtab[TABSIZ]; /* data array list */ |
37 |
|
|
38 |
< |
static DATARRAY *ptab[TABSIZ]; /* picture list */ |
38 |
> |
static gethfunc headaspect; |
39 |
|
|
40 |
|
|
41 |
|
DATARRAY * |
42 |
< |
getdata(dname) /* get data array dname */ |
43 |
< |
char *dname; |
42 |
> |
getdata( /* get data array dname */ |
43 |
> |
char *dname |
44 |
> |
) |
45 |
|
{ |
46 |
|
char *dfname; |
47 |
|
FILE *fp; |
48 |
< |
int asize; |
49 |
< |
register int i, j; |
50 |
< |
register DATARRAY *dp; |
48 |
> |
int asize=0; |
49 |
> |
int i, j; |
50 |
> |
DATARRAY *dp; |
51 |
|
/* look for array in list */ |
52 |
|
for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next) |
53 |
|
if (!strcmp(dname, dp->name)) |
54 |
|
return(dp); /* found! */ |
57 |
– |
|
55 |
|
/* |
56 |
|
* If we haven't loaded the data already, we will look |
57 |
|
* for it in the directories specified by the library path. |
72 |
|
* 0 0 ni p0i p1i .. pni |
73 |
|
*/ |
74 |
|
|
75 |
< |
if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) { |
75 |
> |
if ((dfname = getpath(dname, getrlibpath(), R_OK)) == NULL) { |
76 |
|
sprintf(errmsg, "cannot find data file \"%s\"", dname); |
77 |
< |
error(USER, errmsg); |
77 |
> |
error(SYSTEM, errmsg); |
78 |
|
} |
82 |
– |
if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL) |
83 |
– |
goto memerr; |
84 |
– |
|
85 |
– |
dp->name = savestr(dname); |
86 |
– |
|
79 |
|
if ((fp = fopen(dfname, "r")) == NULL) { |
80 |
|
sprintf(errmsg, "cannot open data file \"%s\"", dfname); |
81 |
|
error(SYSTEM, errmsg); |
82 |
|
} |
83 |
|
/* get dimensions */ |
84 |
< |
if (fgetval(fp, 'i', &dp->nd) <= 0) |
84 |
> |
if (fgetval(fp, 'i', &asize) <= 0) |
85 |
|
goto scanerr; |
86 |
< |
if (dp->nd <= 0 || dp->nd > MAXDDIM) { |
86 |
> |
if ((asize <= 0) | (asize > MAXDDIM)) { |
87 |
|
sprintf(errmsg, "bad number of dimensions for \"%s\"", dname); |
88 |
|
error(USER, errmsg); |
89 |
|
} |
90 |
+ |
if ((dp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL) |
91 |
+ |
goto memerr; |
92 |
+ |
dp->name = savestr(dname); |
93 |
+ |
dp->type = DATATY; |
94 |
+ |
dp->nd = asize; |
95 |
|
asize = 1; |
96 |
|
for (i = 0; i < dp->nd; i++) { |
97 |
< |
if (fgetval(fp, 'd', &dp->dim[i].org) <= 0) |
97 |
> |
if (fgetval(fp, DATATY, &dp->dim[i].org) <= 0) |
98 |
|
goto scanerr; |
99 |
< |
if (fgetval(fp, 'd', &dp->dim[i].siz) <= 0) |
99 |
> |
if (fgetval(fp, DATATY, &dp->dim[i].siz) <= 0) |
100 |
|
goto scanerr; |
101 |
|
if (fgetval(fp, 'i', &dp->dim[i].ne) <= 0) |
102 |
|
goto scanerr; |
104 |
|
goto scanerr; |
105 |
|
asize *= dp->dim[i].ne; |
106 |
|
if ((dp->dim[i].siz -= dp->dim[i].org) == 0) { |
107 |
< |
dp->dim[i].p = (double *)malloc(dp->dim[i].ne*sizeof(double)); |
107 |
> |
dp->dim[i].p = (DATATYPE *) |
108 |
> |
malloc(dp->dim[i].ne*sizeof(DATATYPE)); |
109 |
|
if (dp->dim[i].p == NULL) |
110 |
|
goto memerr; |
111 |
|
for (j = 0; j < dp->dim[i].ne; j++) |
112 |
< |
if (fgetval(fp, 'd', &dp->dim[i].p[i]) <= 0) |
112 |
> |
if (fgetval(fp, DATATY, &dp->dim[i].p[j]) <= 0) |
113 |
|
goto scanerr; |
114 |
|
for (j = 1; j < dp->dim[i].ne-1; j++) |
115 |
|
if ((dp->dim[i].p[j-1] < dp->dim[i].p[j]) != |
121 |
|
} else |
122 |
|
dp->dim[i].p = NULL; |
123 |
|
} |
124 |
< |
if ((dp->arr = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL) |
124 |
> |
if ((dp->arr.d = (DATATYPE *)malloc(asize*sizeof(DATATYPE))) == NULL) |
125 |
|
goto memerr; |
126 |
|
|
127 |
|
for (i = 0; i < asize; i++) |
128 |
< |
if (fgetval(fp, DATATY, &dp->arr[i]) <= 0) |
128 |
> |
if (fgetval(fp, DATATY, &dp->arr.d[i]) <= 0) |
129 |
|
goto scanerr; |
130 |
|
fclose(fp); |
131 |
|
i = hash(dname); |
132 |
|
dp->next = dtab[i]; |
133 |
|
return(dtab[i] = dp); |
136 |
– |
|
134 |
|
memerr: |
135 |
|
error(SYSTEM, "out of memory in getdata"); |
136 |
|
scanerr: |
137 |
|
sprintf(errmsg, "%s in data file \"%s\"", |
138 |
|
feof(fp) ? "unexpected EOF" : "bad format", dfname); |
139 |
|
error(USER, errmsg); |
140 |
+ |
return NULL; /* pro forma return */ |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
< |
static |
145 |
< |
headaspect(s, iap) /* check string for aspect ratio */ |
146 |
< |
char *s; |
147 |
< |
double *iap; |
144 |
> |
static int |
145 |
> |
headaspect( /* check string for aspect ratio */ |
146 |
> |
char *s, |
147 |
> |
void *iap |
148 |
> |
) |
149 |
|
{ |
150 |
+ |
char fmt[MAXFMTLEN]; |
151 |
+ |
|
152 |
|
if (isaspect(s)) |
153 |
< |
*iap *= aspectval(s); |
153 |
> |
*(double*)iap *= aspectval(s); |
154 |
> |
else if (formatval(fmt, s) && strcmp(fmt, COLRFMT)) |
155 |
> |
*(double*)iap = 0.0; |
156 |
> |
return(0); |
157 |
|
} |
158 |
|
|
155 |
– |
|
159 |
|
DATARRAY * |
160 |
< |
getpict(pname) /* get picture pname */ |
161 |
< |
char *pname; |
160 |
> |
getpict( /* get picture pname */ |
161 |
> |
char *pname |
162 |
> |
) |
163 |
|
{ |
164 |
|
double inpaspect; |
165 |
|
char *pfname; |
166 |
|
FILE *fp; |
167 |
< |
COLOR *scanin; |
167 |
> |
COLR *scanin; |
168 |
|
int sl, ns; |
169 |
|
RESOLU inpres; |
170 |
< |
FLOAT loc[2]; |
170 |
> |
RREAL loc[2]; |
171 |
|
int y; |
172 |
< |
register int x, i; |
173 |
< |
register DATARRAY *pp; |
172 |
> |
int x, i; |
173 |
> |
DATARRAY *pp; |
174 |
|
/* look for array in list */ |
175 |
< |
for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next) |
175 |
> |
for (pp = dtab[hash(pname)]; pp != NULL; pp = pp->next) |
176 |
|
if (!strcmp(pname, pp->name)) |
177 |
|
return(pp); /* found! */ |
178 |
|
|
179 |
< |
if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) { |
179 |
> |
if ((pfname = getpath(pname, getrlibpath(), R_OK)) == NULL) { |
180 |
|
sprintf(errmsg, "cannot find picture file \"%s\"", pname); |
181 |
< |
error(USER, errmsg); |
181 |
> |
error(SYSTEM, errmsg); |
182 |
|
} |
183 |
< |
if ((pp = (DATARRAY *)calloc(3, sizeof(DATARRAY))) == NULL) |
183 |
> |
if ((pp = (DATARRAY *)malloc(3*sizeof(DATARRAY))) == NULL) |
184 |
|
goto memerr; |
185 |
|
|
186 |
< |
pp[0].name = |
183 |
< |
pp[1].name = |
184 |
< |
pp[2].name = savestr(pname); |
186 |
> |
pp[0].name = savestr(pname); |
187 |
|
|
188 |
< |
if ((fp = fopen(pfname, "r")) == NULL) { |
188 |
> |
if ((fp = fopen(pfname, "rb")) == NULL) { |
189 |
|
sprintf(errmsg, "cannot open picture file \"%s\"", pfname); |
190 |
|
error(SYSTEM, errmsg); |
191 |
|
} |
190 |
– |
#ifdef MSDOS |
191 |
– |
setmode(fileno(fp), O_BINARY); |
192 |
– |
#endif |
192 |
|
/* get dimensions */ |
193 |
|
inpaspect = 1.0; |
194 |
|
getheader(fp, headaspect, &inpaspect); |
195 |
< |
if (!fgetsresolu(&inpres, fp)) |
195 |
> |
if (inpaspect <= FTINY || !fgetsresolu(&inpres, fp)) |
196 |
|
goto readerr; |
197 |
+ |
pp[0].nd = 2; |
198 |
+ |
pp[0].dim[0].ne = inpres.yr; |
199 |
+ |
pp[0].dim[1].ne = inpres.xr; |
200 |
+ |
pp[0].dim[0].org = |
201 |
+ |
pp[0].dim[1].org = 0.0; |
202 |
+ |
if (inpres.xr <= inpres.yr*inpaspect) { |
203 |
+ |
pp[0].dim[0].siz = inpaspect * |
204 |
+ |
(double)inpres.yr/inpres.xr; |
205 |
+ |
pp[0].dim[1].siz = 1.0; |
206 |
+ |
} else { |
207 |
+ |
pp[0].dim[0].siz = 1.0; |
208 |
+ |
pp[0].dim[1].siz = (double)inpres.xr/inpres.yr / |
209 |
+ |
inpaspect; |
210 |
+ |
} |
211 |
+ |
pp[0].dim[0].p = pp[0].dim[1].p = NULL; |
212 |
+ |
sl = scanlen(&inpres); /* allocate array */ |
213 |
+ |
ns = numscans(&inpres); |
214 |
+ |
i = ns*sl*sizeof(COLR); |
215 |
|
#if PSIZWARN |
216 |
< |
/* check memory usage */ |
217 |
< |
i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr; |
218 |
< |
if (i > PSIZWARN) { |
202 |
< |
sprintf(errmsg, "picture file \"%s\" using %d bytes of memory", |
203 |
< |
pname, i); |
216 |
> |
if (i > PSIZWARN) { /* memory warning */ |
217 |
> |
sprintf(errmsg, "picture file \"%s\" using %.1f MB of memory", |
218 |
> |
pname, i*(1.0/(1024*1024))); |
219 |
|
error(WARNING, errmsg); |
220 |
|
} |
221 |
|
#endif |
222 |
< |
for (i = 0; i < 3; i++) { |
223 |
< |
pp[i].nd = 2; |
209 |
< |
pp[i].dim[0].ne = inpres.yr; |
210 |
< |
pp[i].dim[1].ne = inpres.xr; |
211 |
< |
pp[i].dim[0].org = |
212 |
< |
pp[i].dim[1].org = 0.0; |
213 |
< |
if (inpres.xr <= inpres.yr*inpaspect) { |
214 |
< |
pp[i].dim[0].siz = inpaspect * |
215 |
< |
(double)inpres.yr/inpres.xr; |
216 |
< |
pp[i].dim[1].siz = 1.0; |
217 |
< |
} else { |
218 |
< |
pp[i].dim[0].siz = 1.0; |
219 |
< |
pp[i].dim[1].siz = (double)inpres.xr/inpres.yr / |
220 |
< |
inpaspect; |
221 |
< |
} |
222 |
< |
pp[i].dim[0].p = pp[i].dim[1].p = NULL; |
223 |
< |
pp[i].arr = (DATATYPE *) |
224 |
< |
malloc(inpres.xr*inpres.yr*sizeof(DATATYPE)); |
225 |
< |
if (pp[i].arr == NULL) |
226 |
< |
goto memerr; |
227 |
< |
} |
222 |
> |
if ((pp[0].arr.c = (COLR *)malloc(i)) == NULL) |
223 |
> |
goto memerr; |
224 |
|
/* load picture */ |
225 |
< |
sl = scanlen(&inpres); |
230 |
< |
ns = numscans(&inpres); |
231 |
< |
if ((scanin = (COLOR *)malloc(sl*sizeof(COLOR))) == NULL) |
225 |
> |
if ((scanin = (COLR *)malloc(sl*sizeof(COLR))) == NULL) |
226 |
|
goto memerr; |
227 |
|
for (y = 0; y < ns; y++) { |
228 |
< |
if (freadscan(scanin, sl, fp) < 0) |
228 |
> |
if (freadcolrs(scanin, sl, fp) < 0) |
229 |
|
goto readerr; |
230 |
|
for (x = 0; x < sl; x++) { |
231 |
|
pix2loc(loc, &inpres, x, y); |
232 |
|
i = (int)(loc[1]*inpres.yr)*inpres.xr + |
233 |
|
(int)(loc[0]*inpres.xr); |
234 |
< |
pp[0].arr[i] = colval(scanin[x],RED); |
241 |
< |
pp[1].arr[i] = colval(scanin[x],GRN); |
242 |
< |
pp[2].arr[i] = colval(scanin[x],BLU); |
234 |
> |
copycolr(pp[0].arr.c[i], scanin[x]); |
235 |
|
} |
236 |
|
} |
237 |
< |
free((char *)scanin); |
237 |
> |
free(scanin); |
238 |
|
fclose(fp); |
239 |
|
i = hash(pname); |
240 |
< |
pp[0].next = |
241 |
< |
pp[1].next = |
242 |
< |
pp[2].next = ptab[i]; |
243 |
< |
return(ptab[i] = pp); |
244 |
< |
|
240 |
> |
pp[0].next = dtab[i]; /* link into picture list */ |
241 |
> |
pp[1] = pp[0]; |
242 |
> |
pp[2] = pp[0]; |
243 |
> |
pp[0].type = RED; /* differentiate RGB records */ |
244 |
> |
pp[1].type = GRN; |
245 |
> |
pp[2].type = BLU; |
246 |
> |
return(dtab[i] = pp); |
247 |
|
memerr: |
248 |
|
error(SYSTEM, "out of memory in getpict"); |
249 |
|
readerr: |
250 |
|
sprintf(errmsg, "bad picture file \"%s\"", pfname); |
251 |
|
error(USER, errmsg); |
252 |
+ |
return NULL; /* pro forma return */ |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
< |
freedata(dname) /* free memory associated with dname */ |
257 |
< |
char *dname; |
256 |
> |
/* header info type for hyperspectral image */ |
257 |
> |
typedef struct { |
258 |
> |
float wlpart[4]; /* wavelength partitions */ |
259 |
> |
int nc; /* number of components */ |
260 |
> |
double inpaspect; /* pixel aspect ratio */ |
261 |
> |
} SPECINFO; |
262 |
> |
|
263 |
> |
static int |
264 |
> |
specheadline( /* get info for spectral image */ |
265 |
> |
char *s, |
266 |
> |
void *cdp |
267 |
> |
) |
268 |
|
{ |
269 |
+ |
SPECINFO *sip = (SPECINFO *)cdp; |
270 |
+ |
char fmt[MAXFMTLEN]; |
271 |
+ |
|
272 |
+ |
if (isaspect(s)) |
273 |
+ |
sip->inpaspect *= aspectval(s); |
274 |
+ |
else if (isncomp(s)) |
275 |
+ |
sip->nc = ncompval(s); |
276 |
+ |
else if (iswlsplit(s)) |
277 |
+ |
wlsplitval(sip->wlpart, s); |
278 |
+ |
else if (formatval(fmt, s) && strcmp(fmt, SPECFMT)) |
279 |
+ |
return(-1); |
280 |
+ |
return(0); |
281 |
+ |
} |
282 |
+ |
|
283 |
+ |
DATARRAY * |
284 |
+ |
getspec( /* load hyperspectral image as data */ |
285 |
+ |
char *sname |
286 |
+ |
) |
287 |
+ |
{ |
288 |
+ |
SPECINFO si; |
289 |
+ |
char *pfname; |
290 |
+ |
FILE *fp; |
291 |
+ |
int sl, ns; |
292 |
+ |
int y, i; |
293 |
+ |
DATARRAY *pp; |
294 |
+ |
/* look for array in list */ |
295 |
+ |
for (pp = dtab[hash(sname)]; pp != NULL; pp = pp->next) |
296 |
+ |
if (!strcmp(sname, pp->name)) |
297 |
+ |
return(pp); /* found! */ |
298 |
+ |
|
299 |
+ |
if ((pfname = getpath(sname, getrlibpath(), R_OK)) == NULL) { |
300 |
+ |
sprintf(errmsg, "cannot find hyperspectral image \"%s\"", sname); |
301 |
+ |
error(SYSTEM, errmsg); |
302 |
+ |
} |
303 |
+ |
if ((fp = fopen(pfname, "rb")) == NULL) { |
304 |
+ |
sprintf(errmsg, "cannot open hyperspectral image \"%s\"", pfname); |
305 |
+ |
error(SYSTEM, errmsg); |
306 |
+ |
} |
307 |
+ |
si.wlpart[3] = 0; |
308 |
+ |
si.nc = 0; |
309 |
+ |
si.inpaspect = 1.0; |
310 |
+ |
if (getheader(fp, specheadline, &si) < 0 || |
311 |
+ |
(si.nc <= 3) | (si.nc > MAXCSAMP) | (si.wlpart[3] < 1) || |
312 |
+ |
!fscnresolu(&sl, &ns, fp)) |
313 |
+ |
goto readerr; |
314 |
+ |
|
315 |
+ |
if ((pp = (DATARRAY *)malloc(sizeof(DATARRAY))) == NULL) |
316 |
+ |
goto memerr; |
317 |
+ |
|
318 |
+ |
pp->name = savestr(sname); |
319 |
+ |
pp->type = SPECTY; |
320 |
+ |
pp->nd = 3; |
321 |
+ |
pp->dim[0].ne = ns; |
322 |
+ |
pp->dim[1].ne = sl; |
323 |
+ |
pp->dim[0].org = |
324 |
+ |
pp->dim[1].org = 0.0; |
325 |
+ |
if (sl <= ns*si.inpaspect) { |
326 |
+ |
pp->dim[0].siz = si.inpaspect * (double)ns/sl; |
327 |
+ |
pp->dim[1].siz = 1.0; |
328 |
+ |
} else { |
329 |
+ |
pp->dim[0].siz = 1.0; |
330 |
+ |
pp->dim[1].siz = (double)sl/ns / si.inpaspect; |
331 |
+ |
} |
332 |
+ |
pp->dim[2].ne = si.nc; |
333 |
+ |
pp->dim[2].siz = si.wlpart[3] - si.wlpart[0]; |
334 |
+ |
pp->dim[2].org = si.wlpart[0] + 0.5*pp->dim[2].siz/si.nc; |
335 |
+ |
pp->dim[2].siz *= (si.nc - 1.0)/si.nc; |
336 |
+ |
pp->dim[0].p = pp->dim[1].p = pp->dim[2].p = NULL; |
337 |
+ |
i = ns*sl*(si.nc+1); |
338 |
+ |
#if PSIZWARN |
339 |
+ |
if (i > PSIZWARN) { /* memory warning */ |
340 |
+ |
sprintf(errmsg, "hyperspectral image \"%s\" using %.1f MB of memory", |
341 |
+ |
sname, i*(1.0/(1024*1024))); |
342 |
+ |
error(WARNING, errmsg); |
343 |
+ |
} |
344 |
+ |
#endif |
345 |
+ |
if ((pp->arr.s = (uby8 *)malloc(i)) == NULL) |
346 |
+ |
goto memerr; |
347 |
+ |
for (y = 0; y < ns; y++) /* read each scanline */ |
348 |
+ |
if (freadscolrs(pp->arr.s + y*sl*(si.nc+1), si.nc, sl, fp) < 0) |
349 |
+ |
goto readerr; |
350 |
+ |
fclose(fp); |
351 |
+ |
i = hash(sname); /* insert in hash table */ |
352 |
+ |
pp->next = dtab[i]; |
353 |
+ |
return(dtab[i] = pp); |
354 |
+ |
memerr: |
355 |
+ |
error(SYSTEM, "out of memory in getspec"); |
356 |
+ |
readerr: |
357 |
+ |
sprintf(errmsg, "bad hyperspectral image \"%s\"", pfname); |
358 |
+ |
error(USER, errmsg); |
359 |
+ |
return NULL; /* pro forma return */ |
360 |
+ |
} |
361 |
+ |
|
362 |
+ |
|
363 |
+ |
void |
364 |
+ |
freedata( /* release data array reference */ |
365 |
+ |
DATARRAY *dta |
366 |
+ |
) |
367 |
+ |
{ |
368 |
|
DATARRAY head; |
369 |
|
int hval, nents; |
370 |
< |
register DATARRAY *dp, *dpl; |
371 |
< |
register int i; |
370 |
> |
DATARRAY *dpl, *dp; |
371 |
> |
int i; |
372 |
|
|
373 |
< |
if (dname == NULL) { /* free all if NULL */ |
373 |
> |
if (dta == NULL) { /* free all if NULL */ |
374 |
|
hval = 0; nents = TABSIZ; |
375 |
|
} else { |
376 |
< |
hval = hash(dname); nents = 1; |
376 |
> |
hval = hash(dta->name); nents = 1; |
377 |
> |
if (!*dta->name) { /* not a data file? */ |
378 |
> |
dta->next = dtab[hval]; |
379 |
> |
dtab[hval] = dta; /* ...fake position */ |
380 |
> |
} |
381 |
|
} |
382 |
|
while (nents--) { |
383 |
|
head.next = dtab[hval]; |
384 |
|
dpl = &head; |
385 |
|
while ((dp = dpl->next) != NULL) |
386 |
< |
if (dname == NULL || !strcmp(dname, dp->name)) { |
386 |
> |
if ((dta == NULL) | (dta == dp)) { |
387 |
|
dpl->next = dp->next; |
388 |
< |
free((char *)dp->arr); |
388 |
> |
free(dp->arr.p); |
389 |
|
for (i = 0; i < dp->nd; i++) |
390 |
|
if (dp->dim[i].p != NULL) |
391 |
< |
free((char *)dp->dim[i].p); |
391 |
> |
free(dp->dim[i].p); |
392 |
|
freestr(dp->name); |
393 |
< |
free((char *)dp); |
393 |
> |
free(dp); |
394 |
|
} else |
395 |
|
dpl = dp; |
396 |
|
dtab[hval++] = head.next; |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
< |
freepict(pname) /* free memory associated with pname */ |
402 |
< |
char *pname; |
401 |
> |
/* internal call to interpolate data value or vector */ |
402 |
> |
static double |
403 |
> |
data_interp(DATARRAY *dp, double *pt, double coef, DATATYPE *rvec) |
404 |
|
{ |
405 |
< |
DATARRAY head; |
406 |
< |
int hval, nents; |
407 |
< |
register DATARRAY *pp, *ppl; |
405 |
> |
DATARRAY sd; |
406 |
> |
int stride, i; |
407 |
> |
double x, c0, c1, y0, y1; |
408 |
> |
/* set up dimensions for recursion */ |
409 |
> |
if (dp->nd > 1) { |
410 |
> |
sd.name = dp->name; |
411 |
> |
sd.type = dp->type; |
412 |
> |
sd.nd = dp->nd - 1; |
413 |
> |
memcpy(sd.dim, dp->dim+1, sd.nd*sizeof(struct dadim)); |
414 |
> |
stride = sd.dim[i = sd.nd-1].ne + (sd.type==SPECTY); |
415 |
> |
while (i-- > 0) |
416 |
> |
stride *= sd.dim[i].ne; |
417 |
> |
} |
418 |
> |
/* get independent variable */ |
419 |
> |
if (dp->dim[0].p == NULL) { /* evenly spaced points */ |
420 |
> |
x = (pt[0] - dp->dim[0].org)/dp->dim[0].siz; |
421 |
> |
x *= (double)(dp->dim[0].ne - 1); |
422 |
> |
i = x; |
423 |
> |
if (i < 0) |
424 |
> |
i = 0; |
425 |
> |
else if (i > dp->dim[0].ne - 2) |
426 |
> |
i = dp->dim[0].ne - 2; |
427 |
> |
} else { /* unevenly spaced points */ |
428 |
> |
int lower, upper; |
429 |
> |
if (dp->dim[0].siz > 0.0) { |
430 |
> |
lower = 0; |
431 |
> |
upper = dp->dim[0].ne; |
432 |
> |
} else { |
433 |
> |
lower = dp->dim[0].ne; |
434 |
> |
upper = 0; |
435 |
> |
} |
436 |
> |
do { |
437 |
> |
i = (lower + upper) >> 1; |
438 |
> |
if (pt[0] >= dp->dim[0].p[i]) |
439 |
> |
lower = i; |
440 |
> |
else |
441 |
> |
upper = i; |
442 |
> |
} while (i != (lower + upper) >> 1); |
443 |
|
|
444 |
< |
if (pname == NULL) { /* free all if NULL */ |
445 |
< |
hval = 0; nents = TABSIZ; |
444 |
> |
if (i > dp->dim[0].ne - 2) |
445 |
> |
i = dp->dim[0].ne - 2; |
446 |
> |
|
447 |
> |
x = i + (pt[0] - dp->dim[0].p[i]) / |
448 |
> |
(dp->dim[0].p[i+1] - dp->dim[0].p[i]); |
449 |
> |
} |
450 |
> |
/* |
451 |
> |
* Compute interpolation coefficients: |
452 |
> |
* extrapolate as far as one division, then |
453 |
> |
* taper off harmonically to zero. |
454 |
> |
*/ |
455 |
> |
if (x > i+2) { |
456 |
> |
c0 = 1./(i-1 - x); |
457 |
> |
c1 = -2.*c0; |
458 |
> |
} else if (x < i-1) { |
459 |
> |
c1 = 1./(i - x); |
460 |
> |
c0 = -2.*c1; |
461 |
|
} else { |
462 |
< |
hval = hash(pname); nents = 1; |
462 |
> |
c0 = i+1 - x; |
463 |
> |
c1 = x - i; |
464 |
|
} |
465 |
< |
while (nents--) { |
466 |
< |
head.next = ptab[hval]; |
467 |
< |
ppl = &head; |
468 |
< |
while ((pp = ppl->next) != NULL) |
469 |
< |
if (pname == NULL || !strcmp(pname, pp->name)) { |
470 |
< |
ppl->next = pp->next; |
471 |
< |
free((char *)pp[0].arr); |
472 |
< |
free((char *)pp[1].arr); |
473 |
< |
free((char *)pp[2].arr); |
474 |
< |
freestr(pp[0].name); |
475 |
< |
free((char *)pp); |
465 |
> |
c0 *= coef; |
466 |
> |
c1 *= coef; |
467 |
> |
/* check if vector interp */ |
468 |
> |
if ((dp->nd == 2) & (rvec != NULL)) { |
469 |
> |
if (dp->type == DATATY) { |
470 |
> |
sd.arr.d = dp->arr.d + i*stride; |
471 |
> |
for (i = sd.dim[0].ne; i--; ) |
472 |
> |
rvec[i] += c0*sd.arr.d[i] |
473 |
> |
+ c1*sd.arr.d[i+stride]; |
474 |
> |
return(0.); |
475 |
> |
} |
476 |
> |
if (dp->type == SPECTY) { |
477 |
> |
double f; |
478 |
> |
sd.arr.s = dp->arr.s + i*stride; |
479 |
> |
f = ldexp(1.0, (int)sd.arr.s[sd.dim[0].ne] |
480 |
> |
- (COLXS+8)); |
481 |
> |
for (i = sd.dim[0].ne; i--; ) |
482 |
> |
rvec[i] += c0*f*(sd.arr.s[i] + 0.5); |
483 |
> |
sd.arr.s += stride; |
484 |
> |
f = ldexp(1.0, (int)sd.arr.s[sd.dim[0].ne] |
485 |
> |
- (COLXS+8)); |
486 |
> |
for (i = sd.dim[0].ne; i--; ) |
487 |
> |
rvec[i] += c1*f*(sd.arr.s[i] + 0.5); |
488 |
> |
return(0.); |
489 |
> |
} |
490 |
> |
sd.arr.c = dp->arr.c + i*stride; |
491 |
> |
for (i = sd.dim[0].ne; i--; ) |
492 |
> |
rvec[i] += c0*colrval(sd.arr.c[i],sd.type) |
493 |
> |
+ c1*colrval(sd.arr.c[i+stride],sd.type); |
494 |
> |
return(0.); |
495 |
> |
} |
496 |
> |
/* get dependent variable */ |
497 |
> |
if (dp->nd > 1) { |
498 |
> |
if (dp->type == DATATY) { |
499 |
> |
sd.arr.d = dp->arr.d + i*stride; |
500 |
> |
y0 = data_interp(&sd, pt+1, c0, rvec); |
501 |
> |
sd.arr.d += stride; |
502 |
> |
} else if (dp->type == SPECTY) { |
503 |
> |
sd.arr.s = dp->arr.s + i*stride; |
504 |
> |
y0 = data_interp(&sd, pt+1, c0, rvec); |
505 |
> |
sd.arr.s += stride; |
506 |
> |
} else { |
507 |
> |
sd.arr.c = dp->arr.c + i*stride; |
508 |
> |
y0 = data_interp(&sd, pt+1, c0, rvec); |
509 |
> |
sd.arr.c += stride; |
510 |
> |
} |
511 |
> |
y1 = data_interp(&sd, pt+1, c1, rvec); |
512 |
> |
} else { /* end of recursion */ |
513 |
> |
if (dp->type == DATATY) { |
514 |
> |
y0 = dp->arr.d[i]; |
515 |
> |
y1 = dp->arr.d[i+1]; |
516 |
> |
} else if (dp->type == SPECTY) { |
517 |
> |
if (dp->arr.s[dp->dim[0].ne]) { |
518 |
> |
double f = ldexp(1.0, -(COLXS+8) + |
519 |
> |
(int)dp->arr.s[dp->dim[0].ne]); |
520 |
> |
y0 = (dp->arr.s[i] + 0.5)*f; |
521 |
> |
y1 = (dp->arr.s[i+1] + 0.5)*f; |
522 |
|
} else |
523 |
< |
ppl = pp; |
524 |
< |
ptab[hval++] = head.next; |
523 |
> |
y0 = y1 = 0.0; |
524 |
> |
} else { |
525 |
> |
y0 = colrval(dp->arr.c[i],dp->type); |
526 |
> |
y1 = colrval(dp->arr.c[i+1],dp->type); |
527 |
> |
} |
528 |
> |
y0 *= c0; |
529 |
> |
y1 *= c1; |
530 |
|
} |
531 |
+ |
return(y0 + y1); /* coefficients already applied */ |
532 |
|
} |
533 |
|
|
534 |
|
|
535 |
|
double |
536 |
< |
datavalue(dp, pt) /* interpolate data value at a point */ |
537 |
< |
register DATARRAY *dp; |
538 |
< |
double *pt; |
536 |
> |
datavalue( /* interpolate data value at a point */ |
537 |
> |
DATARRAY *dp, |
538 |
> |
double *pt |
539 |
> |
) |
540 |
|
{ |
541 |
+ |
return(data_interp(dp, pt, 1., NULL)); |
542 |
+ |
} |
543 |
+ |
|
544 |
+ |
|
545 |
+ |
/* Interpolate final vector corresponding to last dimension in data array */ |
546 |
+ |
DATARRAY * |
547 |
+ |
datavector(DATARRAY *dp, double *pt) |
548 |
+ |
{ |
549 |
+ |
DATARRAY *newdp; |
550 |
+ |
|
551 |
+ |
if (dp->nd < 2) |
552 |
+ |
error(INTERNAL, "datavector() called with 1-D array"); |
553 |
+ |
/* create vector array */ |
554 |
+ |
newdp = (DATARRAY *)malloc(sizeof(DATARRAY) - |
555 |
+ |
(MAXDDIM-1)*sizeof(struct dadim) + |
556 |
+ |
sizeof(DATATYPE)*dp->dim[dp->nd-1].ne); |
557 |
+ |
if (newdp == NULL) |
558 |
+ |
error(SYSTEM, "out of memory in datavector"); |
559 |
+ |
newdp->next = NULL; |
560 |
+ |
newdp->name = dp->name; |
561 |
+ |
newdp->type = DATATY; |
562 |
+ |
newdp->nd = 1; /* vector data goes here */ |
563 |
+ |
newdp->dim[0] = dp->dim[dp->nd-1]; |
564 |
+ |
newdp->arr.d = (DATATYPE *)(newdp->dim + 1); |
565 |
+ |
memset(newdp->arr.d, 0, sizeof(DATATYPE)*newdp->dim[0].ne); |
566 |
+ |
|
567 |
+ |
(void)data_interp(dp, pt, 1., newdp->arr.d); |
568 |
+ |
|
569 |
+ |
return(newdp); /* will be free'd using free() */ |
570 |
+ |
} |
571 |
+ |
|
572 |
+ |
|
573 |
+ |
#if 0 |
574 |
+ |
double |
575 |
+ |
datavalue( /* interpolate data value at a point */ |
576 |
+ |
DATARRAY *dp, |
577 |
+ |
double *pt |
578 |
+ |
) |
579 |
+ |
{ |
580 |
|
DATARRAY sd; |
581 |
|
int asize; |
582 |
|
int lower, upper; |
583 |
< |
register int i; |
583 |
> |
int i; |
584 |
|
double x, y0, y1; |
585 |
|
/* set up dimensions for recursion */ |
586 |
< |
sd.nd = dp->nd - 1; |
587 |
< |
asize = 1; |
588 |
< |
for (i = 0; i < sd.nd; i++) { |
589 |
< |
sd.dim[i].org = dp->dim[i+1].org; |
590 |
< |
sd.dim[i].siz = dp->dim[i+1].siz; |
591 |
< |
sd.dim[i].p = dp->dim[i+1].p; |
592 |
< |
asize *= sd.dim[i].ne = dp->dim[i+1].ne; |
586 |
> |
if (dp->nd > 1) { |
587 |
> |
sd.name = dp->name; |
588 |
> |
sd.type = dp->type; |
589 |
> |
sd.nd = dp->nd - 1; |
590 |
> |
asize = 1; |
591 |
> |
for (i = 0; i < sd.nd; i++) { |
592 |
> |
sd.dim[i].org = dp->dim[i+1].org; |
593 |
> |
sd.dim[i].siz = dp->dim[i+1].siz; |
594 |
> |
sd.dim[i].p = dp->dim[i+1].p; |
595 |
> |
asize *= (sd.dim[i].ne = dp->dim[i+1].ne) + |
596 |
> |
((sd.type==SPECTY) & (i==sd.nd-1)); |
597 |
> |
} |
598 |
|
} |
599 |
|
/* get independent variable */ |
600 |
|
if (dp->dim[0].p == NULL) { /* evenly spaced points */ |
620 |
|
else |
621 |
|
upper = i; |
622 |
|
} while (i != (lower + upper) >> 1); |
623 |
+ |
|
624 |
|
if (i > dp->dim[0].ne - 2) |
625 |
|
i = dp->dim[0].ne - 2; |
626 |
+ |
|
627 |
|
x = i + (pt[0] - dp->dim[0].p[i]) / |
628 |
|
(dp->dim[0].p[i+1] - dp->dim[0].p[i]); |
629 |
|
} |
630 |
|
/* get dependent variable */ |
631 |
< |
if (dp->nd == 1) { |
632 |
< |
y0 = dp->arr[i]; |
633 |
< |
y1 = dp->arr[i+1]; |
631 |
> |
if (dp->nd > 1) { |
632 |
> |
if (dp->type == DATATY) { |
633 |
> |
sd.arr.d = dp->arr.d + i*asize; |
634 |
> |
y0 = datavalue(&sd, pt+1); |
635 |
> |
sd.arr.d += asize; |
636 |
> |
y1 = datavalue(&sd, pt+1); |
637 |
> |
} else if (dp->type == SPECTY) { |
638 |
> |
sd.arr.s = dp->arr.s + i*asize; |
639 |
> |
y0 = datavalue(&sd, pt+1); |
640 |
> |
sd.arr.s += asize; |
641 |
> |
y1 = datavalue(&sd, pt+1); |
642 |
> |
} else { |
643 |
> |
sd.arr.c = dp->arr.c + i*asize; |
644 |
> |
y0 = datavalue(&sd, pt+1); |
645 |
> |
sd.arr.c += asize; |
646 |
> |
y1 = datavalue(&sd, pt+1); |
647 |
> |
} |
648 |
|
} else { |
649 |
< |
sd.arr = &dp->arr[i*asize]; |
650 |
< |
y0 = datavalue(&sd, pt+1); |
651 |
< |
sd.arr = &dp->arr[(i+1)*asize]; |
652 |
< |
y1 = datavalue(&sd, pt+1); |
649 |
> |
if (dp->type == DATATY) { |
650 |
> |
y0 = dp->arr.d[i]; |
651 |
> |
y1 = dp->arr.d[i+1]; |
652 |
> |
} else if (dp->type == SPECTY) { |
653 |
> |
if (dp->arr.s[dp->dim[0].ne]) { |
654 |
> |
double f = ldexp(1.0, -(COLXS+8) + |
655 |
> |
(int)dp->arr.s[dp->dim[0].ne]); |
656 |
> |
y0 = (dp->arr.s[i] + 0.5)*f; |
657 |
> |
y1 = (dp->arr.s[i+1] + 0.5)*f; |
658 |
> |
} else |
659 |
> |
y0 = y1 = 0.0; |
660 |
> |
} else { |
661 |
> |
y0 = colrval(dp->arr.c[i],dp->type); |
662 |
> |
y1 = colrval(dp->arr.c[i+1],dp->type); |
663 |
> |
} |
664 |
|
} |
665 |
|
/* |
666 |
|
* Extrapolate as far as one division, then |
674 |
|
|
675 |
|
return( y0*((i+1)-x) + y1*(x-i) ); |
676 |
|
} |
677 |
+ |
#endif |