~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to prim/general/src/averag.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
  Copyright (C) 1989-2009 European Southern Observatory (ESO)
 
3
 
 
4
  This program is free software; you can redistribute it and/or 
 
5
  modify it under the terms of the GNU General Public License as 
 
6
  published by the Free Software Foundation; either version 2 of 
 
7
  the License, or (at your option) any later version.
 
8
 
 
9
  This program is distributed in the hope that it will be useful,
 
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
  GNU General Public License for more details.
 
13
 
 
14
  You should have received a copy of the GNU General Public 
 
15
  License along with this program; if not, write to the Free 
 
16
  Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, 
 
17
  MA 02139, USA.
 
18
 
 
19
  Correspondence concerning ESO-MIDAS should be addressed as follows:
 
20
        Internet e-mail: midas@eso.org
 
21
        Postal address: European Southern Observatory
 
22
                        Data Management Division 
 
23
                        Karl-Schwarzschild-Strasse 2
 
24
                        D 85748 Garching bei Muenchen 
 
25
                        GERMANY
 
26
===========================================================================*/
 
27
 
 
28
 
 
29
/* ++++++++++++++++++++++++++++++++++++++++++++++++++
 
30
 
 
31
.IDENTIFICATION
 
32
  program AVERAG                        version 1.00    890120
 
33
  K. Banse                              ESO - Garching
 
34
 
 
35
.KEYWORDS
 
36
  bulk data frames, average
 
37
 
 
38
.PURPOSE
 
39
  average a no. of image frames, the result will either be a frame with size
 
40
  equal to common area of all frames involved or size equal to union of all
 
41
  frame areas
 
42
 
 
43
.ALGORITHM
 
44
  extract input frames either from given parameter string or from catalogue,
 
45
  add up all frames + divide in the end
 
46
 
 
47
.INPUT/OUTPUT
 
48
  the following keys are used:
 
49
 
 
50
  ACTION/C/1/2                  (1:1) = M or N, for merging option
 
51
                                (2:2) = I or W, for AVERAGE/IMAGE, /WEIGHTS
 
52
  OUT_A/C/1/60                  result frame
 
53
  P3/C/1/80                     list of frames to be averaged
 
54
 
 
55
.VERSIONS
 
56
 
 
57
 090729         last modif
 
58
-------------------------------------------------- */
 
59
 
 
60
#include <midas_def.h>
 
61
#include <stdio.h>
 
62
#include <stdlib.h>
 
63
 
 
64
#define  MAXIMS  300
 
65
#define  MAXIMSONE  MAXIMS+1
 
66
 
 
67
 
 
68
/*
 
69
 
 
70
*/
 
71
 
 
72
void sortit(rka,ndim)
 
73
 
 
74
/*
 
75
  ndim  = dimension of array rka for sorting
 
76
  but we pass the arrays with 1 element in front, so that the algorithm
 
77
  sorts from [1] -> [ndim]
 
78
  this is the Heapsort algorithm from "Numerical Recipes", page 231
 
79
*/
 
80
  
 
81
float *rka;
 
82
int   ndim;
 
83
 
 
84
{
 
85
register int  m, j, i;
 
86
 
 
87
float  zka;
 
88
  
 
89
 
 
90
m = (ndim>>1) + 1;              /* ndim/2 + 1 */
 
91
  
 
92
for (;;)
 
93
   {
 
94
   if (m > 1)                   /* still hiring */
 
95
      zka = rka[--m];
 
96
   else                 /* in retirement + promotion phase */
 
97
      {
 
98
      zka = rka[ndim];        /* clear a space at end of array rka */
 
99
      rka[ndim] = rka[1];    /* retire the top of the heap into it */
 
100
      if (--ndim == 1)                 /* done with last promotion */
 
101
         {
 
102
         rka[1] = zka;        /* the least competent guy of all... */
 
103
         return;                /* that's it folks  */
 
104
         }
 
105
      }
 
106
     
 
107
/* in hiring as well as in promotion phase */
 
108
/* here set up to shift down zka to its right level */
 
109
 
 
110
   i = m;       
 
111
   j = m << 1;                          /* in FORTRAN: j = m + m */
 
112
  
 
113
 
 
114
   while (j <= ndim)    
 
115
      {
 
116
      if (j < ndim)
 
117
         {
 
118
         if (rka[j] < rka[j+1]) j++;
 
119
         }
 
120
   
 
121
      if (zka < rka[j])                 /* demote zka */
 
122
         {
 
123
         rka[i] = rka[j];
 
124
         i = j;
 
125
         j = j << 1;                    /* in FORTRAN: j = j + j */
 
126
         }
 
127
      else
 
128
         j = ndim + 1;
 
129
 
 
130
      }
 
131
   rka[i] = zka;                /* put zka into its slot */
 
132
   }
 
133
}
 
134
 
 
135
/*
 
136
 
 
137
*/
 
138
 
 
139
void fill(iaux,faux,a,x,z,apix,cpix,npixa,npixc)
 
140
int    *iaux, apix[3][2], *cpix;
 
141
int    npixa, *npixc;
 
142
short int   *x;
 
143
float       *faux, *a, *z;
 
144
 
 
145
{
 
146
int    frmcnt, count;
 
147
int    nn, nin, nini, nout, jump, indx, cntdx;
 
148
int    nx, ny, xfirst, yfirst, xend, yend;
 
149
register int nr;
 
150
 
 
151
register float  rr;
 
152
 
 
153
 
 
154
 
 
155
/* ---------------------------
 
156
 
 
157
the intermediate z-space is structured as follows:
 
158
   
 
159
pix(1) pix(2) up to  pix(FRMCNT)      1              )
 
160
pix(1) pix(2) up to  pix(FRMCNT)      2              )
 
161
. . .                                                ) = 1 line
 
162
pix(1) pix(2) up to  pix(FRMCNT)      NPIXC(1)       )
 
163
  
 
164
STRIP lines of above
 
165
--------------------------- */
 
166
 
 
167
 
 
168
/*   if here for the first time, init the count `pixels'  */
 
169
 
 
170
frmcnt = iaux[6];
 
171
count = iaux[7];
 
172
 
 
173
if (count == 0)
 
174
   {
 
175
   nn = npixc[0] * npixc[1];
 
176
   if ((iaux[5] == 0) && (iaux[2] == 0))
 
177
      nini = frmcnt;                    /* for `nomerge' and `all data' */
 
178
   else
 
179
      nini = 0;
 
180
 
 
181
   for (nr=0; nr<nn; nr++)
 
182
      x[nr] = nini;
 
183
   }
 
184
 
 
185
if (iaux[0] == 0) return;               /* if iaux[0] = 0, not much to do */
 
186
 
 
187
 
 
188
nini = 0;                          /* input frame begins with 1. valid pixel */
 
189
if (iaux[5] == 0)
 
190
   {
 
191
 
 
192
/* --------------------------------- */
 
193
/*  here for the non-merging option  */
 
194
/* --------------------------------- */
 
195
 
 
196
   if (iaux[2] == 0)            /* take all pixels */
 
197
      {
 
198
      indx = count;
 
199
      for (nn=0; nn<npixc[1]; nn++)
 
200
         {
 
201
         nin = nini;
 
202
         for (nr=0; nr<npixc[0]; nr++)
 
203
            {
 
204
            z[indx] = a[nin++];
 
205
            indx += frmcnt;
 
206
            }
 
207
         nini += npixa;                  /* follow with input index */
 
208
         }
 
209
      }
 
210
   else                 /* take only pixels in valid interval */
 
211
      {
 
212
      cntdx = 0;
 
213
      indx = 0;
 
214
      for (nn=0; nn<npixc[1]; nn++)
 
215
         {
 
216
         nin = nini;
 
217
         for (nr=0; nr<npixc[0]; nr++)
 
218
            {
 
219
            rr = a[nin++];
 
220
            if ((rr >= faux[2]) && (rr <= faux[3]))
 
221
               {
 
222
               nout = indx + x[cntdx];
 
223
               z[nout] = rr;
 
224
               x[cntdx] ++;                     /* increment count */
 
225
               }
 
226
            indx += frmcnt;
 
227
            cntdx ++;
 
228
            }
 
229
         nini += npixa;                  /* follow with input index */
 
230
         }
 
231
      }
 
232
   }
 
233
else
 
234
   {
 
235
 
 
236
 
 
237
/* ----------------------------- */
 
238
/*  here for the merging option  */
 
239
/* ----------------------------- */
 
240
 
 
241
   nx = apix[0][1] - apix[0][0];                /*   get overlapping part  */
 
242
   ny = apix[1][1] - apix[1][0];
 
243
   xfirst = cpix[0];
 
244
   yfirst = cpix[1];
 
245
   xend = xfirst + nx;
 
246
   yend = yfirst + ny;
 
247
   jump = frmcnt * npixc[0];
 
248
 
 
249
   cntdx = 0;
 
250
   indx = 0;
 
251
 
 
252
   if (iaux[2] == 0)            /* take all pixels */
 
253
      {
 
254
      for (nn=0; nn<npixc[1]; nn++)
 
255
         {
 
256
         if ((nn >= yfirst) && (nn <= yend)) 
 
257
            {
 
258
            nin = nini;
 
259
            for (nr=0; nr<npixc[0]; nr++)
 
260
               {
 
261
               if ((nr >= xfirst) && (nr <= xend)) 
 
262
                  {
 
263
                  nout = indx + x[cntdx];
 
264
                  z[nout] = a[nin++];
 
265
                  x[cntdx] ++;                  /* increment count */
 
266
                  }
 
267
               indx += frmcnt;
 
268
               cntdx ++;
 
269
               }
 
270
 
 
271
            nini += npixa;                      /* follow with input index */
 
272
            }
 
273
         else
 
274
            {
 
275
            indx += jump;                       /* jump a whole line; */
 
276
            cntdx += npixc[0];
 
277
            }
 
278
         }
 
279
      }
 
280
   else                 /* take only pixels in valid interval */
 
281
      {
 
282
      for (nn=0; nn<npixc[1]; nn++)
 
283
         {
 
284
         if ((nn >= yfirst) && (nn <= yend))
 
285
            {
 
286
            nin = nini;
 
287
            for (nr=0; nr<npixc[0]; nr++)
 
288
               {
 
289
               if ((nr >= xfirst) && (nr <= xend))
 
290
                  {
 
291
                  rr = a[nin++];
 
292
                  if ((rr >= faux[2]) && (rr <= faux[3]))
 
293
                     {
 
294
                     nout = indx + x[cntdx];
 
295
                     z[nout] = rr;
 
296
                     x[cntdx] ++;                     /* increment count */
 
297
                     }
 
298
                  }
 
299
               indx += frmcnt;
 
300
               cntdx ++;
 
301
               }
 
302
 
 
303
            nini += npixa;                   /* follow with input index */
 
304
            }
 
305
         else
 
306
            {
 
307
            indx += jump;                    /* jump a whole line; */
 
308
            cntdx += npixc[0];
 
309
            }
 
310
         }
 
311
      }
 
312
   }
 
313
}
 
314
 
 
315
/*
 
316
 
 
317
*/
 
318
 
 
319
void add(flag,iaux,faux,x,z,c,usrnul,cuts,npixc,nc)
 
320
int   *npixc;
 
321
int   flag, *iaux, *nc;
 
322
short int   *x;
 
323
float       *faux, *z, *c, usrnul, *cuts;
 
324
 
 
325
{
 
326
int    frmcnt, n, nn, nh, k, mcc, ma, mb, indx, size;
 
327
register int cntdx;
 
328
 
 
329
register float       rval, va, vb;
 
330
float       rbuf[MAXIMSONE];                    /* buffer for sorting */
 
331
static float old = 0.0;
 
332
  
 
333
double      sum;
 
334
 
 
335
 
 
336
 
 
337
mcc = 0;                                /*  init  */
 
338
indx = 0;
 
339
frmcnt = iaux[6];
 
340
size = npixc[0] * npixc[1];
 
341
  
 
342
 
 
343
/*   branch according to FLAG */
 
344
 
 
345
if (flag == 4) goto median;                     /* that is the `worst' part */
 
346
 
 
347
 
 
348
if (flag == 1)
 
349
  
 
350
   /* -------------------------------*/
 
351
   /*   here for the normal average  */
 
352
   /* -------------------------------*/
 
353
   
 
354
   {
 
355
   for (cntdx=0; cntdx<size; cntdx++)           /* loop through count buffer */
 
356
      {
 
357
      nn = x[cntdx];
 
358
  
 
359
      if (nn == 0) 
 
360
         {
 
361
         if (iaux[8] == 1)
 
362
            rval = old;                         /* use last value */
 
363
         else
 
364
            rval = usrnul;
 
365
         mcc ++;                                /* increment null count  */
 
366
         } 
 
367
      else
 
368
         {
 
369
         if (nn > 1) 
 
370
            {
 
371
            sum = z[indx];
 
372
            for (n=indx+1; n<indx+nn; n++)
 
373
                sum += z[n];
 
374
            rval = sum / nn;
 
375
            }
 
376
         else
 
377
            rval = z[indx];
 
378
         }
 
379
 
 
380
      c[cntdx] = rval;
 
381
      old = rval;
 
382
      if (cuts[0] > rval) cuts[0] = rval;       /* update min + max */
 
383
      if (cuts[1] < rval) cuts[1] = rval;
 
384
      indx += frmcnt;
 
385
      }
 
386
   }
 
387
 
 
388
else if (flag == 2)
 
389
  
 
390
   /* -----------------------*/
 
391
   /*  here for the minimum  */
 
392
   /* -----------------------*/
 
393
 
 
394
   {
 
395
   if (iaux[3] > 0)                     /* take average of MIN and the next */
 
396
      {                                 /* `iaux[3]' higher values */
 
397
      for (cntdx=0; cntdx<size; cntdx++)        /* loop through count buffer */
 
398
         {
 
399
         nn = x[cntdx];
 
400
   
 
401
         if (nn == 0)
 
402
            {
 
403
            if (iaux[8] == 1)
 
404
               rval = old;                         /* use last value */
 
405
            else
 
406
               rval = usrnul;
 
407
            mcc ++;                             /* increment null count  */
 
408
            }
 
409
         else
 
410
            {
 
411
            if (nn > 1)
 
412
               {
 
413
               k = 1;                              /* start at index 1 ... */
 
414
               for (n=indx; n<indx+nn; n++)
 
415
                  rbuf[k++] = z[n];
 
416
               sortit(rbuf,nn);                    /* sort array */
 
417
               k = iaux[3] + 1;
 
418
               if (nn > k)
 
419
                  nh = k;
 
420
               else
 
421
                  nh = nn;
 
422
               sum = rbuf[1];             /* avrage over max. iaux[3] values */
 
423
               for (k=2; k<=nh; k++)
 
424
                   sum += rbuf[k];
 
425
               rval = sum / nh;
 
426
               }
 
427
            else
 
428
               rval = z[indx];
 
429
            }
 
430
 
 
431
         c[cntdx] = rval;
 
432
         old = rval;
 
433
         if (cuts[0] > rval) cuts[0] = rval;    /* update min + max */
 
434
         if (cuts[1] < rval) cuts[1] = rval;
 
435
         indx += frmcnt;
 
436
         }
 
437
      }
 
438
 
 
439
   else
 
440
      {
 
441
      for (cntdx=0; cntdx<size; cntdx++)      /* loop through count buffer */
 
442
         {
 
443
         nn = x[cntdx];
 
444
 
 
445
         if (nn == 0)
 
446
            {
 
447
            if (iaux[8] == 1)
 
448
               rval = old;                         /* use last value */
 
449
            else
 
450
               rval = usrnul;
 
451
            mcc ++;                             /* increment null count  */
 
452
            }
 
453
         else
 
454
            {
 
455
            rval = z[indx];
 
456
            if (nn > 1)
 
457
               {
 
458
               for (n=indx+1; n<indx+nn; n++)
 
459
                  if (rval > z[n]) rval = z[n];
 
460
               }
 
461
            }
 
462
 
 
463
         c[cntdx] = rval;
 
464
         old = rval;
 
465
         if (cuts[0] > rval) cuts[0] = rval;    /* update min + max */
 
466
         if (cuts[1] < rval) cuts[1] = rval;
 
467
         indx += frmcnt;
 
468
         }
 
469
      }
 
470
   }
 
471
 
 
472
else if (flag == 3)
 
473
 
 
474
   /* -----------------------*/
 
475
   /*  here for the maximum  */
 
476
   /* -----------------------*/
 
477
 
 
478
   {
 
479
   if (iaux[3] > 0)                     /* take average of MAX and the next */
 
480
      {                                 /* `iaux[3]' lower values */
 
481
      for (cntdx=0; cntdx<size; cntdx++)        /* loop through count buffer */
 
482
         {
 
483
         nn = x[cntdx];
 
484
 
 
485
         if (nn == 0)
 
486
            {
 
487
            if (iaux[8] == 1)
 
488
               rval = old;                         /* use last value */
 
489
            else
 
490
               rval = usrnul;
 
491
            mcc ++;                             /* increment null count  */
 
492
            }
 
493
         else
 
494
            {
 
495
            if (nn > 1)
 
496
               {
 
497
               k = 1;                              /* start at index 1 ... */
 
498
               for (n=indx; n<indx+nn; n++)
 
499
                  rbuf[k++] = z[n];
 
500
               sortit(rbuf,nn);                    /* sort array */
 
501
               k = iaux[3] + 1;
 
502
               if (nn > k)
 
503
                  nh = k;
 
504
               else
 
505
                  nh = nn;
 
506
               sum = rbuf[nn];            /* avrage over max. iaux[3] values */
 
507
               for (k=nn-1; k>nn-nh; k--)
 
508
                   sum += rbuf[k];
 
509
               rval = sum / nh;
 
510
               }
 
511
            else
 
512
               rval = z[indx];
 
513
            }
 
514
 
 
515
         c[cntdx] = rval;
 
516
         old = rval;
 
517
         if (cuts[0] > rval) cuts[0] = rval;    /* update min + max */
 
518
         if (cuts[1] < rval) cuts[1] = rval;
 
519
         indx += frmcnt;
 
520
         }
 
521
      }
 
522
 
 
523
   else
 
524
      {
 
525
      for (cntdx=0; cntdx<size; cntdx++)        /* loop through count buffer */
 
526
         {
 
527
         nn = x[cntdx];
 
528
 
 
529
         if (nn == 0)
 
530
            {
 
531
            if (iaux[8] == 1)
 
532
               rval = old;                         /* use last value */
 
533
            else
 
534
               rval = usrnul;
 
535
            mcc ++;                             /* increment null count  */
 
536
            }
 
537
         else
 
538
            {
 
539
            rval = z[indx];
 
540
            if (nn > 1)
 
541
               {
 
542
               for (n=indx+1; n<indx+nn; n++)
 
543
                  if (rval < z[n]) rval = z[n];
 
544
               }
 
545
            }
 
546
 
 
547
         c[cntdx] = rval;
 
548
         old = rval;
 
549
         if (cuts[0] > rval) cuts[0] = rval;    /* update min + max */
 
550
         if (cuts[1] < rval) cuts[1] = rval;
 
551
         indx += frmcnt;
 
552
         }
 
553
      }
 
554
   }
 
555
 
 
556
*nc = mcc;
 
557
return;
 
558
 
 
559
 
 
560
median:                                                         /*
 
561
-------
 
562
 
 
563
............................................................
 
564
 
 
565
 
 
566
here comes the code for handling the median
 
567
if iaux[1] = 0, use index interval [ iaux[3],iaux[4] ]
 
568
           = 1, use data interval [ faux[0],faux[1] ]
 
569
 
 
570
............................................................
 
571
                                                                */
 
572
 
 
573
if (iaux[1] == 0)
 
574
   {
 
575
   k = iaux[3] + iaux[4];
 
576
   if (k > 0)                     /* take average over index interval  */
 
577
      {                           /* [ MEDIAN-iaux[3] , MEDIAN+iaux[4] ] */
 
578
      for (cntdx=0; cntdx<size; cntdx++)        /* loop through count buffer */
 
579
         {
 
580
         nn = x[cntdx];
 
581
   
 
582
         if (nn == 0)
 
583
            {
 
584
            if (iaux[8] == 1)
 
585
               rval = old;                         /* use last value */
 
586
            else
 
587
               rval = usrnul;
 
588
            mcc ++;                             /* increment null count  */
 
589
            }
 
590
         else
 
591
            {
 
592
            if (nn == 1)
 
593
               rval = z[indx];
 
594
            else if (nn == 2)
 
595
               rval = (z[indx] + z[indx+1]) / 2;
 
596
            else                                /* here we have: nn > 2 */
 
597
               {
 
598
               k = 1;                           /* start at index 1 ... */
 
599
               for (n=indx; n<indx+nn; n++)
 
600
                  rbuf[k++] = z[n];
 
601
               sortit(rbuf,nn);                 /* sort array */
 
602
               nh = (nn+1) / 2;      /* index of median (starting at 1 ...) */
 
603
               ma = nh - iaux[3];
 
604
               if (ma < 1) ma = 1;
 
605
               mb = nh + iaux[4];
 
606
               if (mb > nn) mb = nn;
 
607
               nn = mb - ma + 1;
 
608
               sum = rbuf[ma];
 
609
               for (k=ma+1; k<=mb; k++)
 
610
                   sum += rbuf[k];
 
611
               rval = sum / nn;
 
612
               }
 
613
            }
 
614
 
 
615
         c[cntdx] = rval;
 
616
         old = rval;
 
617
         if (cuts[0] > rval) cuts[0] = rval;       /* update min + max */
 
618
         if (cuts[1] < rval) cuts[1] = rval;
 
619
         indx += frmcnt;
 
620
         }
 
621
      }
 
622
 
 
623
   else
 
624
      {  
 
625
      for (cntdx=0; cntdx<size; cntdx++)        /* loop through count buffer */
 
626
         {
 
627
         nn = x[cntdx];
 
628
 
 
629
         if (nn == 0)
 
630
            {
 
631
            if (iaux[8] == 1)
 
632
               rval = old;                         /* use last value */
 
633
            else
 
634
               rval = usrnul;
 
635
            mcc ++;                             /* increment null count  */
 
636
            }
 
637
         else
 
638
            {
 
639
            if (nn == 1)
 
640
               rval = z[indx];
 
641
            else if (nn == 2)
 
642
               {
 
643
               rbuf[0] = z[indx];
 
644
               rval = z[indx+1];
 
645
               if (rval > rbuf[0]) rval = rbuf[0];      /* take min of them */
 
646
               }
 
647
            else                /* here we have: nn > 2 */
 
648
               {
 
649
               k = 1;                           /* start at index 1 ... */
 
650
               for (n=indx; n<indx+nn; n++)
 
651
                  rbuf[k++] = z[n];
 
652
               sortit(rbuf,nn);                 /* sort array */
 
653
               nh = (nn+1) / 2;      /* index of median (starting at 1 ...) */
 
654
               rval = rbuf[nh];
 
655
               }
 
656
            }
 
657
 
 
658
         c[cntdx] = rval;
 
659
         old = rval;
 
660
         if (cuts[0] > rval) cuts[0] = rval;       /* update min + max */
 
661
         if (cuts[1] < rval) cuts[1] = rval;
 
662
         indx += frmcnt;
 
663
         }
 
664
      }
 
665
   }
 
666
 
 
667
else
 
668
   {                    
 
669
   rval = faux[0] + faux[1];
 
670
   if (rval > 0.0)                     /* take average over data interval  */
 
671
      {                           /* [ MEDIAN-faux[0] , MEDIAN+faux[4] ] */
 
672
      for (cntdx=0; cntdx<size; cntdx++)        /* loop through count buffer */
 
673
         {
 
674
         nn = x[cntdx];
 
675
 
 
676
         if (nn == 0)
 
677
            {
 
678
            if (iaux[8] == 1)
 
679
               rval = old;                         /* use last value */
 
680
            else
 
681
               rval = usrnul;
 
682
            mcc ++;                             /* increment null count  */
 
683
            }
 
684
         else
 
685
            {
 
686
            if (nn == 1)
 
687
               rval = z[indx];
 
688
            else if (nn == 2)
 
689
               rval = (z[indx] + z[indx+1]) / 2;
 
690
            else                                /* here we have: nn > 2 */
 
691
               {
 
692
               k = 1;                           /* start at index 1 ... */
 
693
               for (n=indx; n<indx+nn; n++)
 
694
                  rbuf[k++] = z[n];
 
695
               sortit(rbuf,nn);                 /* sort array */
 
696
               nh = (nn+1) / 2;      /* index of median (starting at 1 ...) */
 
697
               va = rbuf[nh] - faux[0];
 
698
               vb = rbuf[nh] + faux[1];
 
699
               sum = 0.0;
 
700
               nh = 0;
 
701
               for (k=1; k<=nn; k++)
 
702
                  {
 
703
                  rval = rbuf[k];
 
704
                  if (rval > vb) break;         /* array is sorted ... */
 
705
                  if (rval >= va) 
 
706
                     {
 
707
                     sum += rval;
 
708
                     nh ++;
 
709
                     }
 
710
                  }
 
711
               rval = sum / nh;
 
712
               }
 
713
            }
 
714
 
 
715
         c[cntdx] = rval;
 
716
         old = rval;
 
717
         if (cuts[0] > rval) cuts[0] = rval;       /* update min + max */
 
718
         if (cuts[1] < rval) cuts[1] = rval;
 
719
         indx += frmcnt;
 
720
         }
 
721
      }
 
722
 
 
723
   else                                 /* just use median */
 
724
      {
 
725
      for (cntdx=0; cntdx<size; cntdx++)        /* loop through count buffer */
 
726
         {
 
727
         nn = x[cntdx];
 
728
 
 
729
         if (nn == 0)
 
730
            {
 
731
            if (iaux[8] == 1)
 
732
               rval = old;                         /* use last value */
 
733
            else
 
734
               rval = usrnul;
 
735
            mcc ++;                             /* increment null count  */
 
736
            }
 
737
         else
 
738
            {
 
739
            if (nn == 1)
 
740
               rval = z[indx];
 
741
            else if (nn == 2)
 
742
               rval = (z[indx] + z[indx+1]) / 2;
 
743
            else                                /* here we have: nn > 2 */
 
744
               {
 
745
               k = 1;                           /* start at index 1 ... */
 
746
               for (n=indx; n<indx+nn; n++)
 
747
                  rbuf[k++] = z[n];
 
748
               sortit(rbuf,nn);                 /* sort array */
 
749
               nh = (nn+1) / 2;      /* index of median (starting at 1 ...) */
 
750
               rval = rbuf[nh];
 
751
               }
 
752
            }
 
753
 
 
754
         c[cntdx] = rval;
 
755
         old = rval;
 
756
         if (cuts[0] > rval) cuts[0] = rval;       /* update min + max */
 
757
         if (cuts[1] < rval) cuts[1] = rval;
 
758
         indx += frmcnt;
 
759
         }
 
760
      }
 
761
   }
 
762
 
 
763
*nc = mcc;
 
764
}
 
765
 
 
766
/*
 
767
 
 
768
*/
 
769
 
 
770
int main()
 
771
 
 
772
{
 
773
int    uni, mm;
 
774
int    imnoc, imnox, imnol[MAXIMS];
 
775
int    felm, sizec, chunk[MAXIMS], chunkc;
 
776
int    naxis[MAXIMS], npix[MAXIMS][3], naxisc, npixc[3];
 
777
int    iseq, iav, nulo, zpix[3];
 
778
int    apix[3][2], cpix[3];
 
779
int    iaux[10];
 
780
int    stat, nolin, begin;
 
781
int    stripe, optio;
 
782
int    frmcnt, nulcnt, kk, m, dim3, floff;
 
783
int    debufl=0, planesize=0;
 
784
short int   *xpntr;
 
785
register int nr;
 
786
 
 
787
char     *wpntr, *tmpntr;
 
788
char     line[84], action[4];
 
789
char     frame[84], framec[84], catfil[84];
 
790
char     cunit[64], ident[72], output[84];
 
791
static char  error1[] = "operands do not match in stepsize... ";
 
792
static char  error2[] = "operands do not overlap... ";
 
793
static char  error3[] = "stepsizes of different sign... ";
 
794
static char  error4[] = "catalog empty... ";
 
795
static char  mesalloc[] = "could not allocate virtual memory...";
 
796
 
 
797
double   step[MAXIMS][3], start[MAXIMS][3];
 
798
double   stepc[3], ostep[3], endc[3];
 
799
double   startc[3], ostart[3], oldend[3];
 
800
double   aostep;
 
801
 
 
802
float    *pntr[MAXIMS], *pntrc, *zpntr;
 
803
float    dif, eps[3], cuts[4];
 
804
float    usrnul;
 
805
float    faux[4];
 
806
 
 
807
 
 
808
/* set up MIDAS environment + enable automatic error abort */
 
809
 
 
810
SCSPRO("averag");
 
811
 
 
812
pntrc = zpntr = (float *) 0;
 
813
xpntr = (short int *) 0;
 
814
 
 
815
for (nr=0; nr<3; nr++)
 
816
   {
 
817
   apix[nr][0] = 0;
 
818
   apix[nr][1] = 0;
 
819
   cpix[nr] = 0;
 
820
   startc[nr] = 0.0;
 
821
   stepc[nr] = 1.0;
 
822
   endc[nr] = 0.0;
 
823
   npixc[nr] = 1;
 
824
   zpix[nr] = 1;
 
825
   ostart[nr] = 0.0;
 
826
   oldend[nr] = 0.0;
 
827
   ostep[nr] = 1.0;
 
828
   npix[0][nr] = 1;
 
829
   start[0][nr] = 0.0;
 
830
   step[0][nr] = 1.0;
 
831
   }
 
832
 
 
833
 
 
834
/* get result frame, input frame list, action flag */
 
835
 
 
836
(void) SCKGETC("OUT_A",1,80,&iav,framec);
 
837
(void) SCKGETC("P3",1,80,&iav,line);
 
838
(void) SCKGETC("ACTION",1,2,&iav,action);
 
839
CGN_UPSTR(action);                      /* convert to upper case */
 
840
nulcnt = 0;
 
841
(void) SCKGETC("MID$SPEC",1,5,&iav,output);
 
842
output[5] = '\0';
 
843
if ( strcmp(output,"DEBUG") == 0) debufl = 1;           /* set debug flag */
 
844
 
 
845
 
 
846
/* ---------------------------
 
847
 
 
848
the auxiliary arrays iaux + faux contain the following:
 
849
 
 
850
iaux[0] = 1, input data is inside result space
 
851
          0, only initialize counter pixels if necessary
 
852
iaux[1] = INDEX/DATA flag - not used here
 
853
iaux[2] = 0, take all data
 
854
        = 1, take only data in interval [ faux[2],faux[3] ]
 
855
iaux[3,4] = average limits - not used here
 
856
iaux[5] = 0, `nomerge' option
 
857
        = 1, `merge' option
 
858
iaux[6] = frame count
 
859
iaux[7] = no. of current frame
 
860
iaux[8] = 0, use NULL value
 
861
        = 1, use last pixel as NULL value
 
862
 
 
863
faux[0,1] = average limits if iaux[1] == 1 - not used here
 
864
faux[2,3] = valid_data_interval if iaux[2] = 1
 
865
 
 
866
--------------------------- */
 
867
 
 
868
for (nr=0; nr<10; nr++) iaux[nr] = 0;
 
869
 
 
870
 
 
871
/* get Null value, average option+limits, valid_data_interval */
 
872
 
 
873
(void) SCKGETC("P5",1,40,&iav,output);
 
874
if ((output[0] == '+') && (output[1] == '\0'))
 
875
   iaux[8] = 1;                                 /* use `last' value as Null */
 
876
else
 
877
   {
 
878
   iav = CGN_CNVT(output,2,1,npixc,&usrnul,&aostep);
 
879
   if (iav < 1)
 
880
      SCETER(19,"invalid `null value' ...");
 
881
   }
 
882
 
 
883
optio = 1;
 
884
(void) SCKGETC("P6",1,60,&iav,output);
 
885
CGN_UPCOPY(frame,output,2);             /* upper case -> frame */
 
886
 
 
887
if (frame[0] == 'M')
 
888
   {
 
889
   if (frame[1] == 'I')
 
890
      optio = 2;
 
891
   else if (frame[1] == 'A')
 
892
      optio = 3;
 
893
   else if (frame[1] == 'E')
 
894
      optio = 4;
 
895
   }
 
896
 
 
897
if (optio != 1)
 
898
   {    
 
899
   begin = CGN_INDEXC(output,',')+1;
 
900
   if ((begin > 1) && (output[begin] != '\0'))
 
901
      {
 
902
      (void) strcpy(output,&output[begin]);
 
903
      if (optio < 4)                    /* must be MIN,n or MAX,n */
 
904
         {
 
905
         kk = CGN_CNVT(output,1,1,npixc,&dif,&aostep);
 
906
         if (kk <= 0)
 
907
            SCETER(8,"invalid syntax in option_string ...");
 
908
         iaux[3] =  npixc[0];
 
909
         }
 
910
      else
 
911
         {                                              /* MED,r,q,DATA */
 
912
         iav = CGN_CNVT(output,2,2,npixc,faux,&aostep);
 
913
         if (iav < 1)
 
914
            SCETER(8,"invalid syntax in `option_string' ...");
 
915
         if (iav == 1) faux[1] = faux[0];
 
916
         iaux[3] = faux[0];
 
917
         iaux[4] = faux[1];
 
918
                                                /* skip max. two numbers */
 
919
         begin = 0;
 
920
         m = (int) strlen(output);                      /*          or: MAX,n */
 
921
         kk = CGN_EXTRSS(output,m,',',&begin,cunit,40);
 
922
         if (iav == 2)
 
923
            kk = CGN_EXTRSS(output,m,',',&begin,cunit,40);
 
924
 
 
925
         if ((output[begin] == 'D') || (output[begin] == 'd'))
 
926
            iaux[1] = 1;                   /* show that it's DATA method */
 
927
         }
 
928
      if ((iaux[3] < 0) || (iaux[4] < 0))
 
929
         SCETER(9,"negative `average limits' ...");
 
930
      }
 
931
   }
 
932
 
 
933
 
 
934
/*  now let's look if we have a valid data interval */
 
935
 
 
936
(void) SCKGETC("P7",1,60,&iav,output);
 
937
if (output[0] != '+')
 
938
   {
 
939
   kk = CGN_CNVT(output,2,2,npixc,&faux[2],&aostep);
 
940
   if (kk < 2)
 
941
      SCETER(10,"invalid syntax in `valid_data_interval' ...");
 
942
   if (faux[2] > faux[3])
 
943
      SCETER(11,"invalid `valid_data_interval' ...");
 
944
   iaux[2] = 1;                 /* indicate we have a valid_data_interval */
 
945
   }
 
946
 
 
947
 
 
948
/*  test, if we have list of frames or catalog */
 
949
 
 
950
kk = CGN_INDEXS(line,".cat");
 
951
if (kk <= 0) kk = CGN_INDEXS(line,".CAT");
 
952
 
 
953
 
 
954
/*   here we handle input from a catalog - get name of first image file 
 
955
     in catalog                                                         */
 
956
 
 
957
if (kk > 0) 
 
958
   {
 
959
   if ((int)strlen(line) > 63)
 
960
      SCETER(3,"catalog name too long...");
 
961
   else
 
962
      (void) strcpy(catfil,line);
 
963
 
 
964
   iseq = 0;
 
965
   for (frmcnt=0; frmcnt<MAXIMS; frmcnt++)          /* max. MAXIMS frames */
 
966
      {
 
967
      (void) SCCGET(catfil,0,frame,output,&iseq);
 
968
      if (frame[0] == ' ') break;               /*  indicates the end ... */
 
969
    
 
970
      imnol[frmcnt] = 0;
 
971
      (void) SCFOPN(frame,D_R4_FORMAT,0,F_IMA_TYPE,&imnol[frmcnt]);
 
972
      }
 
973
   sprintf(output,"%d images from catalog to be processed",frmcnt);
 
974
   SCTPUT(output);
 
975
   }
 
976
 
 
977
 
 
978
/*  here we handle input from single input line - pull out operands */
 
979
 
 
980
else
 
981
   {
 
982
   begin = 0;
 
983
   m = (int) strlen(line);
 
984
   for (frmcnt=0; frmcnt<MAXIMS; frmcnt++)
 
985
      {
 
986
      kk = CGN_EXTRSS(line,m,',',&begin,output,60);
 
987
      if (kk <= 0) break;
 
988
       
 
989
      CGN_FRAME(output,F_IMA_TYPE,frame,0);     /* convert frames  */
 
990
      imnol[frmcnt] = 0;
 
991
      stat = SCFOPN(frame,D_R4_FORMAT,0,F_IMA_TYPE,&imnol[frmcnt]);
 
992
      }
 
993
   }
 
994
 
 
995
if (frmcnt <= 0) SCETER(4,error4);       /* there must be at least 1 image  */
 
996
 
 
997
 
 
998
/*  --------------------------------------------*/
 
999
/*   get initial area from 1. frame             */
 
1000
/*  --------------------------------------------*/
 
1001
 
 
1002
(void) SCDRDI(imnol[0],"NAXIS",1,1,&iav,&naxis[0],&uni,&nulo);
 
1003
(void) SCDRDI(imnol[0],"NPIX",1,3,&iav,&npix[0][0],&uni,&nulo);
 
1004
(void) SCDRDD(imnol[0],"START",1,3,&iav,&start[0][0],&uni,&nulo);
 
1005
(void) SCDRDD(imnol[0],"STEP",1,3,&iav,&step[0][0],&uni,&nulo);
 
1006
(void) SCDGETC(imnol[0],"CUNIT",1,64,&iav,cunit);
 
1007
 
 
1008
 
 
1009
for (nr=0; nr<3; nr++)
 
1010
   {
 
1011
   ostart[nr] = start[0][nr];
 
1012
   ostep[nr] = step[0][nr];
 
1013
   aostep = ostep[nr];
 
1014
   if (aostep < 0.0) aostep = - aostep;
 
1015
   eps[nr] = 0.0001 * aostep;                   /* take 0.01% of stepsize */
 
1016
   oldend[nr] = ostart[nr] + (npix[0][nr]-1)*ostep[nr];
 
1017
   }
 
1018
cuts[0] = 999999.0;                                     /* cuts[0] > cuts[1] */
 
1019
cuts[1] = -999999.0;
 
1020
 
 
1021
 
 
1022
/*   now loop through the other input frames  */
 
1023
  
 
1024
for (nr=1; nr< frmcnt; nr++)
 
1025
   {
 
1026
   for (m=0; m<3; m++)                          /* init values... */
 
1027
      {
 
1028
      start[nr][m] = 0.0;
 
1029
      step[nr][m] = 1.0;
 
1030
      npix[nr][m] = 1;
 
1031
      }
 
1032
  
 
1033
   (void) SCDRDI(imnol[nr],"NAXIS",1,1,&iav,&naxis[nr],&uni,&nulo);
 
1034
   (void) SCDRDI(imnol[nr],"NPIX",1,3,&iav,&npix[nr][0],&uni,&nulo);
 
1035
   (void) SCDRDD(imnol[nr],"START",1,3,&iav,&start[nr][0],&uni,&nulo);
 
1036
   (void) SCDRDD(imnol[nr],"STEP",1,3,&iav,&step[nr][0],&uni,&nulo);
 
1037
 
 
1038
 
 
1039
/*  stepsizes should have same sign and not differ too much...  */
 
1040
 
 
1041
   for (m=0; m<3; m++)
 
1042
      {
 
1043
      if ((ostep[m]*step[nr][m]) <= 0.) SCETER(1,error3);
 
1044
      aostep = step[nr][m] - ostep[m];
 
1045
      if (aostep < 0.) aostep = -aostep;
 
1046
      
 
1047
      if (aostep > eps[m]) SCETER(5,error1);
 
1048
      }
 
1049
 
 
1050
 
 
1051
/*   get intersection or union of image areas  */
 
1052
 
 
1053
   if (action[0] != 'M') 
 
1054
      {
 
1055
      for (m=0; m<3; m++)
 
1056
         {
 
1057
         dif = start[nr][m] + (npix[nr][m]-1)*step[nr][m];
 
1058
         if (ostep[m] > 0.) 
 
1059
            {
 
1060
            if (ostart[m] < start[nr][m]) ostart[m] = start[nr][m];   /* MAX */
 
1061
            if (oldend[m] > dif) oldend[m] = dif;                     /* MIN */
 
1062
            }
 
1063
         else
 
1064
            {
 
1065
            if (ostart[m] > start[nr][m]) ostart[m] = start[nr][m];   /* MIN */
 
1066
            if (oldend[m] < dif) oldend[m] = dif;                     /* MAX */
 
1067
            }
 
1068
         }
 
1069
      }
 
1070
   else
 
1071
      {
 
1072
      for (m=0; m<3; m++)
 
1073
         {
 
1074
         dif = start[nr][m] + (npix[nr][m]-1)*step[nr][m];
 
1075
         if (ostep[m] < 0.) 
 
1076
            {
 
1077
            if (ostart[m] < start[nr][m]) ostart[m] = start[nr][m];   /* MAX */
 
1078
            if (oldend[m] > dif) oldend[m] = dif;                     /* MIN */
 
1079
            }
 
1080
         else
 
1081
            {
 
1082
            if (ostart[m] > start[nr][m]) ostart[m] = start[nr][m];   /* MIN */
 
1083
            if (oldend[m] < dif) oldend[m] = dif;                     /* MAX */
 
1084
            }
 
1085
         }
 
1086
      iaux[5] = 1;
 
1087
      }
 
1088
 
 
1089
   }
 
1090
 
 
1091
 
 
1092
/*  test, if something is left...  */
 
1093
 
 
1094
for (m=0; m<3; m++)
 
1095
   {
 
1096
   if (ostep[m]*(oldend[m]-ostart[m]) < 0.) SCETER(2,error2);
 
1097
   }
 
1098
 
 
1099
 
 
1100
/*   create new result frame with dimension as intersection 
 
1101
     or union of input frames                                   */
 
1102
 
 
1103
naxisc = naxis[0];
 
1104
if (action[0] == 'M') 
 
1105
   {
 
1106
   for (nr=1; nr<frmcnt; nr++)
 
1107
      {
 
1108
      if (naxisc < naxis[nr]) naxisc = naxis[nr];               /* MAX */
 
1109
      }
 
1110
   }
 
1111
else
 
1112
   {
 
1113
   for (nr=1; nr<frmcnt; nr++)
 
1114
      {
 
1115
      if (naxisc > naxis[nr]) naxisc = naxis[nr];               /* MIN */
 
1116
      }
 
1117
   }
 
1118
 
 
1119
 
 
1120
/*  check, if input from a cube */
 
1121
 
 
1122
if (naxisc > 2) 
 
1123
   {
 
1124
   dim3 = npix[0][2];
 
1125
   naxisc = 2;
 
1126
   }
 
1127
else
 
1128
   dim3 = 0;
 
1129
 
 
1130
 
 
1131
/*  set up standard stuff for result frame        */
 
1132
 
 
1133
sizec = 1;
 
1134
for (nr=0; nr<naxisc; nr++)
 
1135
   {
 
1136
   startc[nr] = ostart[nr];
 
1137
   stepc[nr] = ostep[nr];
 
1138
   npixc[nr] = CGN_NINT((oldend[nr]-ostart[nr]) / stepc[nr]) + 1;
 
1139
   sizec = sizec * npixc[nr];
 
1140
   }
 
1141
 
 
1142
imnoc = 0;
 
1143
(void) SCFCRE(framec,D_R4_FORMAT,F_O_MODE,F_IMA_TYPE,sizec,&imnoc);
 
1144
(void) SCDWRI(imnoc,"NAXIS",&naxisc,1,1,&uni);
 
1145
(void) SCDWRI(imnoc,"NPIX",npixc,1,naxisc,&uni);
 
1146
(void) SCDWRD(imnoc,"START",startc,1,naxisc,&uni);
 
1147
(void) SCDWRD(imnoc,"STEP",stepc,1,naxisc,&uni);
 
1148
(void) strcpy(ident,"average frame ");
 
1149
(void) SCDWRC(imnoc,"IDENT",1,ident,1,72,&uni);
 
1150
mm = (naxisc+1) * 16;
 
1151
(void) SCDWRC(imnoc,"CUNIT",1,cunit,1,mm,&uni);
 
1152
 
 
1153
  
 
1154
/* in case of debugging save the counts in MIDAS image averdumy.dum  */
 
1155
 
 
1156
if (debufl == 1)
 
1157
   {
 
1158
   imnox = 0;
 
1159
   (void) strcpy(frame,"averdumy.dum");
 
1160
   (void) SCFCRE(frame,D_I2_FORMAT,F_O_MODE,F_IMA_TYPE,sizec,&imnox);
 
1161
   (void) SCDWRI(imnox,"NAXIS",&naxisc,1,1,&uni);
 
1162
   (void) SCDWRI(imnox,"NPIX",npixc,1,naxisc,&uni);
 
1163
   (void) SCDWRD(imnox,"START",startc,1,naxisc,&uni);
 
1164
   (void) SCDWRD(imnox,"STEP",stepc,1,naxisc,&uni);
 
1165
   (void) strcpy(ident,"valid pixel counts ");
 
1166
   (void) SCDWRC(imnox,"IDENT",1,ident,1,72,&uni);
 
1167
   (void) SCDWRC(imnox,"CUNIT",1,cunit,1,mm,&uni);
 
1168
   }
 
1169
 
 
1170
 
 
1171
/* see, if keyword MONITPAR(20) holds amount of virtual memory available */
 
1172
 
 
1173
(void) SCKRDI("MONITPAR",20,1,&iav,&chunkc,&uni,&nulo);
 
1174
mm = (chunkc * chunkc) / 5;              /* `chunkc' = x-dim of square image
 
1175
                                            which can be fully mapped */
 
1176
if (mm < npixc[0])
 
1177
   stripe = 1;                           /* at least a single line */
 
1178
else
 
1179
   stripe = mm / npixc[0];
 
1180
 
 
1181
 
 
1182
/*  get virtual memory buffers */
 
1183
 
 
1184
if (npixc[1] < stripe) stripe = npixc[1];
 
1185
chunkc = stripe * npixc[0];                 /* size of 1 result strip */
 
1186
kk = chunkc * sizeof(float);
 
1187
wpntr = malloc((size_t)kk);
 
1188
if (wpntr == (char *) 0)
 
1189
   SCETER(66,mesalloc);
 
1190
else
 
1191
   pntrc = (float *) wpntr;
 
1192
 
 
1193
if (dim3 > 1)
 
1194
   {
 
1195
   if (npix[0][1] < stripe) 
 
1196
      iav = npix[0][1];                         /* iav = y-dim  */
 
1197
   else
 
1198
      iav = stripe;
 
1199
 
 
1200
   chunk[0] = iav * npix[0][0];         /* always stripe lines  */
 
1201
   for (nr=0; nr<dim3; nr++)
 
1202
      {                                 /* all chunks like chunk[0] */
 
1203
      chunk[nr] = chunk[0];
 
1204
      kk = chunk[nr] * sizeof(float);
 
1205
      wpntr = malloc((size_t)kk);
 
1206
      if (wpntr == (char *) 0)
 
1207
         SCETER(66,mesalloc);
 
1208
      else
 
1209
         pntr[nr] = (float *) wpntr;
 
1210
      imnol[nr] = imnol[0];
 
1211
      for (m=0; m<naxisc; m++)
 
1212
         {
 
1213
         npix[nr][m] = npix[0][m];
 
1214
         start[nr][m] = start[0][m];
 
1215
         step[nr][m] = step[0][m];
 
1216
         }
 
1217
      }
 
1218
   }
 
1219
else
 
1220
   {
 
1221
   for (nr=0; nr<frmcnt; nr++)
 
1222
      {
 
1223
      if (npix[nr][1] < stripe) 
 
1224
         iav = npix[nr][1];                             /* iav = y-dim  */
 
1225
      else
 
1226
         iav = stripe;
 
1227
 
 
1228
      chunk[nr] = iav * npix[nr][0];            /* always stripe lines */
 
1229
      kk = chunk[nr] * sizeof(float);
 
1230
      wpntr = malloc((size_t)kk);
 
1231
      if (wpntr == (char *) 0)
 
1232
         SCETER(66,mesalloc);
 
1233
      else
 
1234
         pntr[nr] = (float *) wpntr;
 
1235
      }
 
1236
   }
 
1237
 
 
1238
 
 
1239
/*  now map chunk for z-direction and count buffer  */
 
1240
 
 
1241
if (dim3 > 1)
 
1242
   {
 
1243
   planesize = npixc[0] * npixc[1];
 
1244
   frmcnt = dim3;
 
1245
   }
 
1246
 
 
1247
iaux[6] = frmcnt;
 
1248
m = frmcnt * chunkc;
 
1249
kk = m * sizeof(float);
 
1250
wpntr = malloc((size_t)kk);
 
1251
if (wpntr == (char *) 0)
 
1252
   SCETER(66,mesalloc);
 
1253
else
 
1254
   zpntr = (float *) wpntr;
 
1255
 
 
1256
kk = chunkc * sizeof(short int);
 
1257
wpntr = malloc((size_t)kk);
 
1258
if (wpntr == (char *) 0)
 
1259
   SCETER(66,mesalloc);
 
1260
else
 
1261
   xpntr = (short int *) wpntr;
 
1262
  
 
1263
 
 
1264
/*   here the main loops over all chunks
 
1265
     first fill the cube chunk,  work on it + store result           */
 
1266
 
 
1267
endc[0] = startc[0] + (npixc[0]-1)*stepc[0];
 
1268
endc[1] = startc[1] + (stripe-1)*stepc[1];
 
1269
zpix[0] = npixc[0];
 
1270
  
 
1271
 
 
1272
for (nolin=0; nolin<npixc[1]; nolin+=stripe)
 
1273
   {
 
1274
   if ((nolin+stripe) > npixc[1])               /* adjust chunk size */
 
1275
      {
 
1276
      stripe = npixc[1] - nolin;
 
1277
      chunkc = stripe * npixc[0];
 
1278
      for (nr=0; nr<frmcnt; nr++)
 
1279
         chunk[nr] = stripe * npix[nr][0];
 
1280
      }
 
1281
   zpix[1] = stripe;
 
1282
 
 
1283
   floff = 0;                           /* for data cube input */
 
1284
   for (nr=0; nr<frmcnt; nr++)
 
1285
      {
 
1286
 
 
1287
      /*  convert start + end of overlap region into pixel no.'s   */
 
1288
 
 
1289
      for (m=0; m<naxisc; m++)
 
1290
         {
 
1291
         dif = (start[nr][m]-startc[m]) / stepc[m];
 
1292
         if (dif < 0.1) 
 
1293
            cpix[m] = 0;
 
1294
         else
 
1295
            {
 
1296
            cpix[m] = CGN_NINT(dif);                    /* offset in output */
 
1297
            if (cpix[m] >= zpix[m]) 
 
1298
               {
 
1299
               iaux[0] = 0;
 
1300
               goto sect_5360;
 
1301
               }
 
1302
            }
 
1303
 
 
1304
         dif = (startc[m]-start[nr][m]) / step[nr][m];
 
1305
         if (dif < 0.1)                                 /* offset in input */
 
1306
            apix[m][0] = 0;
 
1307
         else
 
1308
            {
 
1309
            apix[m][0] = CGN_NINT(dif);
 
1310
            if (apix[m][0] >= npix[nr][m]) 
 
1311
               {
 
1312
               iaux[0] = 0;
 
1313
               goto sect_5360;
 
1314
               }
 
1315
            }
 
1316
         
 
1317
         dif = (endc[m]-start[nr][m]) / step[nr][m];
 
1318
         apix[m][1] = CGN_NINT(dif);
 
1319
         if (apix[m][1] >= npix[nr][m]) apix[m][1] = npix[nr][m] - 1;
 
1320
         }
 
1321
  
 
1322
      iaux[0] = 1;
 
1323
                                  /* remember, SCFGET/SCFPUT begins with 1!  */
 
1324
      felm = floff + (apix[1][0] * npix[nr][0]) + apix[0][0] + 1;
 
1325
      tmpntr = (char *) pntr[nr];
 
1326
      stat = SCFGET(imnol[nr],felm,chunk[nr],&iav,tmpntr);
 
1327
 
 
1328
 
 
1329
sect_5360:                                              /* fill z-buffer */
 
1330
      iaux[7] = nr;
 
1331
      fill(iaux,faux,pntr[nr],xpntr,zpntr,apix,cpix,npix[nr][0],zpix);
 
1332
      floff += planesize;
 
1333
      }
 
1334
  
 
1335
 
 
1336
   /*   now do the calculus  */
 
1337
 
 
1338
   add(optio,iaux,faux,xpntr,zpntr,pntrc,usrnul,cuts,zpix,&kk);
 
1339
 
 
1340
  
 
1341
   /*  and write results to disk */
 
1342
 
 
1343
   felm = nolin*npixc[0] + 1;
 
1344
   tmpntr = (char *) pntrc;
 
1345
   (void) SCFPUT(imnoc,felm,chunkc,tmpntr);
 
1346
   nulcnt += kk;                                /* update null count */
 
1347
   if (debufl == 1)
 
1348
      {
 
1349
      tmpntr = (char *) xpntr;
 
1350
      (void) SCFPUT(imnox,felm,chunkc,tmpntr);  /* if debug, update on disk */
 
1351
      }
 
1352
  
 
1353
 
 
1354
   /*  follow chunks with start + end value  */
 
1355
 
 
1356
   aostep = stripe*stepc[1];
 
1357
   startc[1] += aostep;
 
1358
   endc[1] += aostep;
 
1359
   }
 
1360
 
 
1361
  
 
1362
/*  update descriptors + cuts of result frame  */
 
1363
 
 
1364
frame[0] = ' ';
 
1365
frame[1] = '\0';
 
1366
CGN_DSCUPD(imnol[0],imnoc,frame);
 
1367
 
 
1368
cuts[2] = cuts[0];
 
1369
cuts[3] = cuts[1];
 
1370
(void) SCDWRR(imnoc,"LHCUTS",cuts,1,4,&uni);
 
1371
 
 
1372
cuts[0] = nulcnt;                       /* update key NULL */
 
1373
if (iaux[8] == 0)
 
1374
   {
 
1375
   cuts[1] = usrnul;
 
1376
   mm = 2;
 
1377
   }
 
1378
else
 
1379
   mm = 1;
 
1380
(void) SCKWRR("NULL",cuts,1,mm,&uni);
 
1381
if (nulcnt > 0) 
 
1382
   {
 
1383
   if (iaux[8] == 0)
 
1384
      (void) sprintf(output,
 
1385
      "%d undefined pixels, set to `null value' (= %12.6f)",nulcnt,usrnul);
 
1386
   else
 
1387
      (void) sprintf(output,
 
1388
      "%d undefined pixels, set to `previous pixel'",nulcnt);
 
1389
   SCTPUT(output);
 
1390
   }
 
1391
  
 
1392
SCSEPI();
 
1393
return(0);
 
1394
}