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