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

« back to all changes in this revision

Viewing changes to prim/general/src/genxy1.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) 1995-2011 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
.IDENT        genxy1.c
 
30
.LANGUAGE     C
 
31
.AUTHOR       K. Banse   ESO/IPG
 
32
.KEYWORDS     Bulk data frames, Images
 
33
.PURPOSE      a) copy images
 
34
              b) rearrange planes of a cube
 
35
              c) convert descr sequences to a table (e.g. VIMOS data)
 
36
 
 
37
.VERSION      1.00  950921:   Creation 
 
38
 
 
39
 111123         last modif
 
40
---------------------------------------------------------------------*/
 
41
 
 
42
#define MAXSUBS  15
 
43
 
 
44
#include <stdlib.h>
 
45
#include <stdio.h>
 
46
 
 
47
 
 
48
#include   <midas_def.h>
 
49
 
 
50
/*
 
51
 
 
52
*/
 
53
 
 
54
void convert(infile,outfile)
 
55
char    *infile, *outfile;
 
56
 
 
57
{
 
58
char    cbuf[124], instring[124], *ptra;
 
59
char    dscroot[48], fulldscroot[124], *dscsub[MAXSUBS], *helpsub[MAXSUBS];
 
60
char    sfmt[8];
 
61
 
 
62
int  imnoa, tid;
 
63
int  dtype[MAXSUBS], colno[MAXSUBS], csize[MAXSUBS], hlen[MAXSUBS];
 
64
int  mn, mm, maxsubs, nval, null, unit;
 
65
int  n, convdir, bytelm, status;
 
66
int  ec, el, ed, ln1, ln0, delflag;
 
67
int  nrow, nrowmax, dlim[2], single;
 
68
int  ival;
 
69
 
 
70
float rval;
 
71
 
 
72
double dval;
 
73
 
 
74
 
 
75
/*
 
76
 
 
77
Midas command: 
 
78
CONVERT/DESCR_MATRIX image  table  direction 
 
79
              dscroot[,first[,last]]  dscsub1,dscsub2,...  del_flag
 
80
 
 
81
*/
 
82
 
 
83
status = 0;
 
84
single = 0;                     /* set, if 1-dim array instead of matrix */
 
85
(void) strcpy(sfmt,"%s%d.");
 
86
 
 
87
(void) SCKGETC("P3",1,4,&nval,cbuf);    /* which way to go? */
 
88
CGN_LOWSTR(cbuf);
 
89
mm = strcmp(cbuf,"t-ds");
 
90
if (mm != 0)                            /* descriptors -> table */
 
91
   {
 
92
   convdir = 1;
 
93
   (void) SCKRDI("INPUTI",1,1,&nval,&delflag,&unit,&null);  /* delete flag */
 
94
   }
 
95
else                                    /* table -> descriptors */
 
96
   {
 
97
   convdir = 2;
 
98
   delflag = 0;
 
99
   (void) TCTOPN(outfile,F_I_MODE,&tid);
 
100
   (void) TCIGET(tid,&maxsubs,&nrowmax,&mn,&mn,&mn);
 
101
   (void) SCDRDI(tid,"DESCR_MATRIX",1,1,&nval,&single,&unit,&null);
 
102
   }
 
103
 
 
104
 
 
105
 
 
106
/* look for root part of descr and optional index limits */
 
107
 
 
108
if (single == 1)                        /* that implies convdir = 2 ! */
 
109
   {
 
110
   (void) strcpy(sfmt,"%s%d");
 
111
   (void) SCDGETC(tid,"DESCR_ROOT",1,120,&nval,fulldscroot);
 
112
   }
 
113
else
 
114
   {
 
115
   (void) SCKGETC("P4",1,120,&nval,fulldscroot);
 
116
   if ((fulldscroot[0] == '+') || (fulldscroot[0] == '?'))
 
117
      {                                         /* nothing entered */
 
118
      if (convdir == 1)
 
119
         SCETER(67,"missing root of descr...");
 
120
                                        /* get info from table descr. */
 
121
      (void) SCDGETC(tid,"DESCR_ROOT",1,120,&nval,fulldscroot);
 
122
      }
 
123
   }
 
124
 
 
125
 
 
126
/* and process  root[,first[,last]] */
 
127
 
 
128
(void) strcpy(instring,fulldscroot);
 
129
mm = CGN_INDEXC(instring,',');
 
130
if (mm > 0)
 
131
   {
 
132
   instring[mm] = '\0';
 
133
   (void) strcpy(dscroot,instring);             /* copy first string */
 
134
   (void) strcpy(instring,&instring[mm+1]);
 
135
   mm = CGN_INDEXC(instring,',');
 
136
   if (mm > 0)
 
137
      mm = 2;
 
138
   else
 
139
      {
 
140
      mm = 1;
 
141
      dlim[1] = -1;
 
142
      }
 
143
   n =  CGN_CNVT(instring,1,mm,dlim,&rval,&dval);
 
144
   if (n != mm) SCETER(64,"invalid start, end indices...");
 
145
 
 
146
   if (dlim[0] < 1) dlim[0] = 1;
 
147
   if ((mm == 2) && (dlim[1] < dlim[0]))
 
148
      SCETER(65,"invalid start, end indices...");
 
149
   }
 
150
else
 
151
   {
 
152
   (void) strcpy(dscroot,instring);
 
153
   dlim[0] = 1;                         /* index of first descr. */
 
154
   dlim[1] = -1;                        /* index of last descr. */
 
155
   }
 
156
 
 
157
 
 
158
/* allocate virtual space for subDSCs and their help 
 
159
   max 46 chars per subDSC and 72 chars per help string */
 
160
 
 
161
mm = 48 + 74;                   /* use multiples of 4 for nicer handling */
 
162
 
 
163
dscsub[0] = (char *) malloc((size_t) (MAXSUBS * mm));
 
164
if (dscsub[0] == (char *) 0)
 
165
   SCETER(66,"could not allocate virtual memory...");
 
166
helpsub[0] = dscsub[0] + (MAXSUBS * 48);        /* skip to help string part */
 
167
 
 
168
for (n=1; n<MAXSUBS; n++)
 
169
   {
 
170
   dscsub[n] = dscsub[n-1] + 48;
 
171
   helpsub[n] = helpsub[n-1] + 74;
 
172
   }
 
173
 
 
174
 
 
175
/* open the image for access to FITS/descr. header */
 
176
 
 
177
(void) SCFOPN(infile,D_OLD_FORMAT,0,F_IMA_TYPE,&imnoa);
 
178
(void) SCECNT("GET",&ec,&el,&ed);
 
179
ln1 = 1; ln0 = 0;
 
180
 
 
181
 
 
182
/* ........................................
 
183
 
 
184
   Descriptors -> Table
 
185
 
 
186
   ......................................... */
 
187
 
 
188
 
 
189
if (convdir == 1)
 
190
   {
 
191
   maxsubs = 1;                  /* parse individual descr fields (subDCS) */
 
192
   (void) SCKGETC("P5",1,120,&nval,instring);
 
193
   if ((instring[0] == '?') || (instring[0] == '+'))
 
194
      {
 
195
      single = 1;
 
196
      (void) strcpy(dscsub[0],"");              /* points to Nirwana... */
 
197
      (void) strcpy(sfmt,"%s%d");
 
198
      }
 
199
   else
 
200
      {
 
201
      ptra = instring;
 
202
 
 
203
      for (n=0; n<MAXSUBS; n++)
 
204
         {
 
205
         mm = CGN_INDEXC(ptra,',');
 
206
         if (mm > 0)                    
 
207
            {
 
208
            *(ptra+mm) = '\0';
 
209
            (void) strcpy(dscsub[n],ptra);
 
210
            ptra += (mm+1);
 
211
            maxsubs ++;
 
212
            }
 
213
         else
 
214
            {
 
215
            (void) strcpy(dscsub[n],ptra);
 
216
            break;
 
217
            }
 
218
         }
 
219
      }
 
220
 
 
221
 
 
222
   /* move descriptors to binary table */
 
223
 
 
224
   if (single == 0)
 
225
      (void) 
 
226
      printf("use as descr_root:   %s###\nand as descr_fields: ",dscroot);
 
227
   else
 
228
      (void) 
 
229
      printf("use as descr_root:   %s### and no descr_fields ",dscroot);
 
230
 
 
231
   (void) sprintf(instring,sfmt,dscroot,dlim[0]);       /* for first descr. */
 
232
   mm = (int) strlen(instring);
 
233
 
 
234
   for (n=0; n<maxsubs; n++)
 
235
      {
 
236
      (void) strcpy(&instring[mm],dscsub[n]);
 
237
      (void) SCDFND(imnoa,instring,cbuf,&nval,&bytelm);
 
238
      if (cbuf[0] == ' ')
 
239
         SCETER(62,"could not get descr_field type...");
 
240
 
 
241
      csize[n] = 1;
 
242
      if (cbuf[0] == 'I')
 
243
         dtype[n] = 1;
 
244
      else if (cbuf[0] == 'R')
 
245
         dtype[n] = 2;
 
246
      else if (cbuf[0] == 'D')
 
247
         dtype[n] = 4;
 
248
      else                              /* only C (character) left */
 
249
         {
 
250
         dtype[n] = 3;
 
251
         csize[n] = nval * bytelm;              /* size of char. column */
 
252
         }
 
253
   
 
254
      (void) printf("%s, ",dscsub[n]);
 
255
 
 
256
      (void) SCDRDH(imnoa,instring,1,72,&nval,cbuf,&mn);
 
257
      if (mn < 1) 
 
258
         *helpsub[n] = '\0';
 
259
      else
 
260
         {
 
261
         cbuf[nval] = '\0';
 
262
         (void) strcpy(helpsub[n],cbuf);
 
263
         }
 
264
      }
 
265
 
 
266
   if (single == 0)
 
267
      (void) printf("\n=> table will be created with %d columns\n",maxsubs);
 
268
   else
 
269
      {
 
270
      (void) printf("\n=> table will be created with %d column\n",maxsubs);
 
271
      (void) strcpy(dscsub[0],dscroot);
 
272
      }
 
273
 
 
274
 
 
275
   /* create the table with `maxsubs' cols * 200 rows */
 
276
 
 
277
   (void) TCTINI(outfile,F_TRANS,F_O_MODE,maxsubs,200,&tid);
 
278
 
 
279
   for (n=0; n<maxsubs; n++)
 
280
      {
 
281
      if (dtype[n] == 1)
 
282
         status = TCCINI(tid,D_I4_FORMAT,1,"I8"," ",dscsub[n],&colno[n]);
 
283
      else if (dtype[n] == 2)
 
284
         status = TCCINI(tid,D_R4_FORMAT,1,"G12.6"," ",dscsub[n],&colno[n]);
 
285
      else if (dtype[n] == 4)
 
286
         status = TCCINI(tid,D_R8_FORMAT,1,"G12.6"," ",dscsub[n],&colno[n]);
 
287
      else 
 
288
         {
 
289
         mn = (int) strlen(dscsub[n]);
 
290
         nval = ((mn - 1)%8 + 1) * 8;           /* => chunks of 8 chars */
 
291
         (void) sprintf(cbuf,"a%d",nval);
 
292
         status = TCCINI(tid,D_C_FORMAT,csize[n],cbuf," ",dscsub[n],&colno[n]);
 
293
         }
 
294
 
 
295
      if (status != ERR_NORMAL)
 
296
         SCETER(63,"could not create table columns...");
 
297
      }
 
298
 
 
299
   (void) SCDWRC(tid,"DESCR_ROOT",1,fulldscroot,1,120,&unit);
 
300
   (void) SCDWRI(tid,"DESCR_MATRIX",&single,1,1,&unit);
 
301
   for (n=0; n<maxsubs; n++)
 
302
      {
 
303
      (void) sprintf(instring,"DESCR_%4.4d",n);
 
304
      (void) SCDWRC(tid,instring,1,dscsub[n],1,(int)strlen(dscsub[n]),&unit);
 
305
      if (*helpsub[n] != '\0')
 
306
         {
 
307
         hlen[n] = (int) strlen(helpsub[n]);
 
308
         (void) SCDWRH(tid,instring,helpsub[n],1,hlen[n]);
 
309
         }
 
310
      }
 
311
 
 
312
   if (single == 1) (void) strcpy(dscsub[0],"");
 
313
 
 
314
 
 
315
   /* loop through header of file */
 
316
   
 
317
   (void) SCECNT("PUT",&ln1,&ln0,&ln0);         /* now, ignore errors */
 
318
 
 
319
   if (dlim[1] == -1) dlim[1] = 200000;
 
320
   nrow = 1;
 
321
 
 
322
   for (mn=dlim[0]; mn<dlim[1]+1 ; mn++)
 
323
      {
 
324
      (void) sprintf(instring,sfmt,dscroot,mn); /* build DSC name */
 
325
      mm = (int) strlen(instring);
 
326
 
 
327
      for (n=0; n<maxsubs; n++)
 
328
         {
 
329
         (void) strcpy(&instring[mm],dscsub[n]);
 
330
         if (dtype[n] == 1)
 
331
            {
 
332
            status = SCDRDI(imnoa,instring,1,1,&nval,&ival,&unit,&null);
 
333
            if (status == ERR_NORMAL)
 
334
               {
 
335
               (void) TCEWRI(tid,nrow,colno[n],&ival);
 
336
               if (delflag == 1) (void) SCDDEL(imnoa,instring);
 
337
               }
 
338
            }
 
339
         else if (dtype[n] == 2)
 
340
            {
 
341
            status = SCDRDR(imnoa,instring,1,1,&nval,&rval,&unit,&null);
 
342
            if (status == ERR_NORMAL)
 
343
               {
 
344
               (void) TCEWRR(tid,nrow,colno[n],&rval);
 
345
               if (delflag == 1) (void) SCDDEL(imnoa,instring);
 
346
               }
 
347
            }
 
348
         else if (dtype[n] == 4)
 
349
            {
 
350
            status = SCDRDD(imnoa,instring,1,1,&nval,&dval,&unit,&null);
 
351
            if (status == ERR_NORMAL)
 
352
               {
 
353
               (void) TCEWRD(tid,nrow,colno[n],&dval);
 
354
               if (delflag == 1) (void) SCDDEL(imnoa,instring);
 
355
               }
 
356
            }
 
357
         else                           /* only char string left */
 
358
            {
 
359
            status = SCDRDC(imnoa,instring,1,1,80,&nval,cbuf,&unit,&null);
 
360
            if (status == ERR_NORMAL)
 
361
               {
 
362
               cbuf[nval] = '\0';
 
363
               (void) TCEWRC(tid,nrow,colno[n],cbuf);
 
364
               if (delflag == 1) (void) SCDDEL(imnoa,instring);
 
365
               }
 
366
            }
 
367
 
 
368
         }
 
369
 
 
370
      if (status == ERR_DSCNPR)
 
371
         {
 
372
         nrow --;                       /* adjust no. of rows written */
 
373
         (void) sprintf(instring,"%d rows created",nrow);
 
374
         SCTPUT(instring);
 
375
         break;
 
376
         }
 
377
 
 
378
      nrow ++;
 
379
      }
 
380
   }
 
381
 
 
382
 
 
383
/* ........................................
 
384
 
 
385
   Table -> Descriptors
 
386
 
 
387
   ......................................... */
 
388
 
 
389
else
 
390
   {
 
391
   for (n=0; n<maxsubs; n++)            /* find all columns to process */
 
392
      {
 
393
      (void) sprintf(cbuf,"#%d",n+1);
 
394
      (void) TCCSER(tid,cbuf,&colno[n]);
 
395
      if (colno[n] == -1) 
 
396
         SCETER(68,"input column bad...");
 
397
 
 
398
      (void) TCFGET(tid,colno[n],cbuf,&mn,&nval);
 
399
      csize[n] = 1;
 
400
      if (nval == D_C_FORMAT)
 
401
         {
 
402
         dtype[n] = 3;
 
403
         csize[n] = mn;
 
404
         }
 
405
      else if (nval == D_R8_FORMAT)
 
406
         dtype[n] = 4;
 
407
      else if (nval == D_R4_FORMAT)
 
408
         dtype[n] = 2;
 
409
      else 
 
410
         dtype[n] = 1;                  /* all other => integer */
 
411
 
 
412
      (void) sprintf(instring,"DESCR_%4.4d",n);
 
413
      (void) SCDHRC(tid,instring,1,1,46,&nval,dscsub[n],cbuf,72,&unit,&null);
 
414
      *(dscsub[n] + nval) = '\0';       /* strings from descr. have no '\0' */
 
415
      (void) strcpy(helpsub[n],cbuf);
 
416
      hlen[n] = (int) strlen(cbuf);
 
417
 
 
418
/*
 
419
      printf("dscsub[%d] = >%s<, type = %d, size = %d\n",
 
420
             n,dscsub[n],dtype[n],csize[n]);
 
421
      printf("helpsub[%d] = >%s<, hlen = %d\n\n",
 
422
             n,helpsub[n],hlen[n]);
 
423
*/
 
424
      }
 
425
 
 
426
   if (single == 1) (void) strcpy(dscsub[0],"");
 
427
 
 
428
 
 
429
   /* move binary table to descriptors */
 
430
 
 
431
   (void) SCECNT("PUT",&ln1,&ln0,&ln0);         /* now, ignore errors */
 
432
 
 
433
   mn = dlim[0];
 
434
   if (dlim[1] == -1) dlim[1] = nrowmax;
 
435
   nrow = 1;
 
436
 
 
437
  table_loop:
 
438
   (void) sprintf(instring,sfmt,dscroot,mn);    /* build DSC name */
 
439
   mm = (int) strlen(instring);
 
440
 
 
441
   for (n=0; n<maxsubs; n++)
 
442
      {
 
443
      (void) strcpy(&instring[mm],dscsub[n]);
 
444
 
 
445
      if (dtype[n] == 1)
 
446
         {
 
447
         status = TCERDI(tid,nrow,colno[n],&ival,&null);
 
448
         if (status == ERR_NORMAL)
 
449
            (void) xSCDWRI(imnoa,instring,&ival,1,1,&unit);
 
450
         }
 
451
      else if (dtype[n] == 2)
 
452
         {
 
453
         status = TCERDR(tid,nrow,colno[n],&rval,&null);
 
454
         if (status == ERR_NORMAL)
 
455
            (void) xSCDWRR(imnoa,instring,&rval,1,1,&unit);
 
456
         }
 
457
      else if (dtype[n] == 4)
 
458
         {
 
459
         status = TCERDD(tid,nrow,colno[n],&dval,&null);
 
460
         if (status == ERR_NORMAL)
 
461
            (void) xSCDWRD(imnoa,instring,&dval,1,1,&unit);
 
462
         }
 
463
      else                              /* only character left */
 
464
         {
 
465
         status = TCERDC(tid,nrow,colno[n],cbuf,&null);
 
466
         if (status == ERR_NORMAL)
 
467
            (void) xSCDWRC(imnoa,instring,1,cbuf,1,csize[n],&unit);
 
468
         }
 
469
 
 
470
      if (*helpsub[n] != '\0')
 
471
         (void) xSCDWRH(imnoa,instring,helpsub[n],1,hlen[n]);
 
472
      }
 
473
 
 
474
   mn ++; nrow ++;
 
475
   if ((mn > dlim[1]) || (nrow > nrowmax)) 
 
476
      {
 
477
      nrow --;                  /* adjust no. of rows written */
 
478
      (void) sprintf(instring,"%d rows processed",nrow);
 
479
      SCTPUT(instring);
 
480
      }
 
481
   else
 
482
      goto table_loop;
 
483
   }
 
484
 
 
485
 
 
486
(void) SCECNT("PUT",&ec,&el,&ed);               /* reset error flags */
 
487
 
 
488
(void) SCKWRI("OUTPUTI",&nrow,9,1,&unit);      /* save in OUTPUTI(9) */
 
489
 
 
490
(void) TCTCLO(tid);
 
491
(void) SCFCLO(imnoa);
 
492
 
 
493
}
 
494
/*
 
495
 
 
496
*/
 
497
 
 
498
void mappi(in,map,out,nval,aux,msize,flag,oo)
 
499
float  *in;     /* IN: input buffer */
 
500
float  *map;    /* IN: the map buffer */
 
501
float  *out;    /* OUT: result buffer */
 
502
int    nval;    /* IN: no. of pixels */
 
503
float  *aux;    /* IN: aux[0] = fact, aux[1] = xlo, aux[2] = xhi */
 
504
int    msize;   /* IN: highest index of map */
 
505
int    flag;    /* IN: = 0, do scaling - = 1, take value directly as index */
 
506
float  *oo;     /* OUT: min, max value */
 
507
 
 
508
{
 
509
register int   nr, indx, mindx;
 
510
 
 
511
float   fact, xlo, xhi, mapmax;
 
512
register float  rin, rout, turbo;
 
513
 
 
514
 
 
515
 
 
516
mindx = msize - 1;
 
517
fact = aux[0]; xlo = aux[1]; xhi = aux[2];
 
518
turbo = 0.5 - fact*xlo;
 
519
mapmax = *(map+mindx);
 
520
 
 
521
if (flag == 0)
 
522
   {
 
523
 
 
524
   /* indx = 1 + NINT((r - xlo)*fact); but  first pixel of map has indx = 0
 
525
   => indx = NINT(r*fact - xlo*fact) or (int) (r*fact - xlo*fact + 0.5) */
 
526
 
 
527
   for (nr=0; nr<nval; nr++)
 
528
      {
 
529
      rin = *in++;
 
530
      if (rin <= xlo)
 
531
         rout = *map;
 
532
      else if (rin >= xhi)
 
533
         rout = mapmax;
 
534
      else
 
535
         {
 
536
         indx = (int) (rin*fact + turbo);       
 
537
         rout = *(map+indx);
 
538
         }
 
539
      if (rout < oo[0])
 
540
         oo[0] = rout;
 
541
      else if (rout > oo[1])
 
542
         oo[1] = rout;
 
543
      *out++ = rout;
 
544
      }
 
545
   }
 
546
else
 
547
   {
 
548
 
 
549
   /* indx = NINT(r) */
 
550
 
 
551
   for (nr=0; nr<nval; nr++)
 
552
      {
 
553
      indx = (int) (turbo + (*in++));   
 
554
      if (indx <= 0)
 
555
         rout = *map;
 
556
      else if (indx >= mindx)
 
557
         rout = mapmax;
 
558
      else
 
559
         rout = *(map+indx);
 
560
      if (rout < oo[0])
 
561
         oo[0] = rout;
 
562
      else if (rout > oo[1])
 
563
         oo[1] = rout;
 
564
      *out++ = rout;
 
565
      }
 
566
   }
 
567
 
 
568
}
 
569
/*
 
570
 
 
571
*/
 
572
 
 
573
int check_fmt(cbuf)
 
574
char  *cbuf;
 
575
 
 
576
{
 
577
int  ibra;
 
578
 
 
579
ibra = *cbuf;
 
580
cbuf ++;
 
581
 
 
582
switch (ibra)
 
583
   {
 
584
   case 'I':
 
585
   case 'i':
 
586
 
 
587
   if (*cbuf == '1')
 
588
      return (D_I1_FORMAT);
 
589
   else if (*cbuf == '2')
 
590
      return (D_I2_FORMAT);
 
591
   else if (*cbuf == '4')
 
592
      return (D_I4_FORMAT);
 
593
   else
 
594
      break;
 
595
 
 
596
 
 
597
   case 'U':
 
598
   case 'u':
 
599
 
 
600
   if ((*cbuf == 'I') || (*cbuf == 'i'))
 
601
      {
 
602
      if (*(++cbuf) == '2')
 
603
         return (D_UI2_FORMAT);
 
604
      }
 
605
 
 
606
 
 
607
   case 'R':
 
608
   case 'r':
 
609
 
 
610
   if (*cbuf == '8')
 
611
      return (D_R8_FORMAT);
 
612
   else
 
613
      return (D_R4_FORMAT);
 
614
   
 
615
 
 
616
   case 'D':
 
617
   case 'd':
 
618
 
 
619
   return (D_R8_FORMAT);
 
620
 
 
621
 
 
622
   default:
 
623
      break;
 
624
   }
 
625
 
 
626
return (-1);                    /* wrong format  */
 
627
}
 
628
/*
 
629
 
 
630
*/
 
631
 
 
632
void mapima(infile,mapfile,outfile)
 
633
char    *infile, *mapfile, *outfile;
 
634
 
 
635
{
 
636
char    *pntra, *pntrb, *pntrc, cbuf[4];
 
637
 
 
638
int  naxis, npix[6], imnoa, imnob, imnoc, imnow1, imnow2;
 
639
int  felm, mapsize, sizze, msize, clony;
 
640
int  n, null, nval, unit, scflag;
 
641
 
 
642
float   cuts[4], fact, oldoo[2], oo[2], faux[3];
 
643
 
 
644
 
 
645
 
 
646
(void) SCKRDI("MONITPAR",20,1,&nval,&n,&unit,&null);  /* get storage size */
 
647
mapsize = n * n;
 
648
 
 
649
(void) SCKGETC("P5",1,2,&nval,cbuf);
 
650
if ((cbuf[0] == 's') || (cbuf[0] == 'S'))
 
651
   scflag = 0;
 
652
else
 
653
   scflag = 1;                  /* use pixel value directly as index for map */
 
654
 
 
655
imnoa = -1;
 
656
(void) SCFOPN(infile,D_R4_FORMAT,0,F_IMA_TYPE,&imnoa);
 
657
(void) SCDRDI(imnoa,"NAXIS",1,1,&nval,&naxis,&unit,&null);
 
658
if (naxis > 6) SCETER(3,"More than 6 dimensions...");
 
659
 
 
660
(void) SCDRDI(imnoa,"NPIX",1,naxis,&nval,npix,&unit,&null);
 
661
 
 
662
sizze = 1;                                      /*  get total size of frame  */
 
663
for (n=0; n< naxis; n++) sizze *= npix[n];
 
664
 
 
665
(void) SCKRDR("INPUTR",1,2,&nval,cuts,&unit,&null);  /* get mapping interval */
 
666
if (cuts[1] <= cuts[0])
 
667
   {
 
668
   (void) SCDRDR(imnoa,"LHCUTS",3,2,&nval,cuts,&unit,&null);
 
669
   if (cuts[1] <= cuts[0])
 
670
      SCETER(23,"Invalid Map-limits (bad descr. LHCUTS)...");
 
671
   }
 
672
 
 
673
 
 
674
/* create new image and try to clone all descriptors */
 
675
 
 
676
(void) CGN_IBUILD(imnoa,outfile,D_R4_FORMAT,sizze,&imnoc,&clony);
 
677
 
 
678
 
 
679
/* get the complete image "map" */
 
680
 
 
681
imnob = -1;
 
682
(void) SCFOPN(mapfile,D_R4_FORMAT,0,F_IMA_TYPE,&imnob);
 
683
(void) SCDRDI(imnob,"NAXIS",1,1,&nval,&naxis,&unit,&null);
 
684
if (naxis > 6) SCETER(3,"More than 6 dimensions...");
 
685
 
 
686
(void) SCDRDI(imnob,"NPIX",1,naxis,&nval,npix,&unit,&null);
 
687
 
 
688
msize = 1;                                      /*  get total size of map  */
 
689
for (n=0; n< naxis; n++) msize *= npix[n];
 
690
 
 
691
(void) SCFMAP(imnob,F_I_MODE,1,msize,&nval,&pntrb);
 
692
 
 
693
 
 
694
/* compute aux-variables: 
 
695
 
 
696
   (pix - pxlo)/(pxhi - pxlo) = (x - cuts[0])/(cuts[1] - cuts[0])
 
697
   pix = pxlo + (x - cuts[0])*fact
 
698
   with fact = (pxhi - pxlo)/(cuts[1] - cuts[0])   */
 
699
 
 
700
 
 
701
/* allocate virtual memory for in/out frame buffers with right data types */
 
702
 
 
703
if (sizze <= mapsize) mapsize = sizze;
 
704
(void) SCFCRE("work1",D_R4_FORMAT,F_X_MODE,F_IMA_TYPE,mapsize,&imnow1);
 
705
(void) SCFMAP(imnow1,F_X_MODE,1,mapsize,&nval,&pntra);
 
706
(void) SCFCRE("work2",D_R4_FORMAT,F_X_MODE,F_IMA_TYPE,mapsize,&imnow2);
 
707
(void) SCFMAP(imnow2,F_X_MODE,1,mapsize,&nval,&pntrc);
 
708
 
 
709
 
 
710
fact = (msize-1) / (cuts[1] - cuts[0]);
 
711
faux[0] = fact; faux[1] = cuts[0]; faux[2] = cuts[1];
 
712
 
 
713
printf("index-range = [1,%d], map-range = [%f,%f], \n",
 
714
       msize,cuts[0],cuts[1]);
 
715
 
 
716
(void) SCDRDR(imnob,"LHCUTS",3,2,&nval,oldoo,&unit,&null);
 
717
if (oldoo[1] <= oldoo[0]) SCETER(24,"Invalid LHCUTS of mapfile...");
 
718
 
 
719
oo[0] = oldoo[1]; oo[1] = oldoo[0];     /* should be pixels in that interval */
 
720
oldoo[0] = oo[0]; oldoo[1] = oo[1];
 
721
 
 
722
 
 
723
/*  now we do the mapping   */
 
724
 
 
725
felm = 0;
 
726
while (felm < sizze)
 
727
   {
 
728
   n = felm + 1;
 
729
   (void) SCFGET(imnoa,n,mapsize,&nval,pntra);
 
730
   mappi(pntra,pntrb,pntrc,nval,faux,msize,scflag,oo);
 
731
   (void) SCFPUT(imnoc,n,nval,pntrc);
 
732
   felm += nval;
 
733
   if (oo[0] < oldoo[0]) oldoo[0] = oo[0];
 
734
   if (oo[1] > oldoo[1]) oldoo[1] = oo[1];
 
735
   }
 
736
 
 
737
if (clony == 0)
 
738
   (void) SCDCOP(imnoa,imnoc,1," ");   /* copy all descriptors */
 
739
 
 
740
cuts[0] = cuts[1] = 0.0;
 
741
cuts[2] = oldoo[0]; cuts[3] = oldoo[1];
 
742
(void) SCDWRR(imnoc,"LHCUTS",cuts,1,4,&unit);
 
743
}
 
744
 
 
745
/*
 
746
 
 
747
*/
 
748
 
 
749
void copyii(infile,outfile)
 
750
char    *infile, *outfile;
 
751
 
 
752
{
 
753
char    cbuf[12], dely[8];
 
754
char    *pntra, *pntrb;
 
755
 
 
756
int  naxis, npix[6], imnoa, imnob, imnow1, imnow2;
 
757
int  felm, mapsize, sizze, clony;
 
758
int  info[5], null, nval, unit;
 
759
int  n, fmtin, fmtout, uflow, oflow;
 
760
 
 
761
float   cuts[4];
 
762
 
 
763
 
 
764
(void) SCKGETC("P3",1,10,&nval,cbuf);                   /* get output format */
 
765
(void) SCKGETC("P4",1,7,&nval,dely);                    /* get delete flag   */
 
766
 
 
767
(void) SCKRDI("MONITPAR",20,1,&nval,&n,&unit,&null);  /* get mapping size */
 
768
mapsize = n * n;
 
769
 
 
770
(void) SCFINF(infile,2,info);                   /* get source format  */
 
771
fmtin =  info[1];
 
772
fmtout = check_fmt(cbuf);
 
773
if (fmtout < 0) SCETER(2,"invalid data format...");
 
774
 
 
775
imnoa = -1;
 
776
(void) SCFOPN(infile,info[1],0,F_IMA_TYPE,&imnoa);
 
777
(void) SCDRDI(imnoa,"NAXIS",1,1,&nval,&naxis,&unit,&null);
 
778
if (naxis > 6) SCETER(3,"More than 6 dimensions...");
 
779
 
 
780
(void) SCDRDI(imnoa,"NPIX",1,naxis,&nval,npix,&unit,&null);
 
781
(void) SCDRDR(imnoa,"LHCUTS",1,4,&nval,cuts,&unit,&null);
 
782
 
 
783
uflow = 0; oflow = 0;
 
784
sizze = 1;                                      /*  get total size of frame  */
 
785
for (n=0; n< naxis; n++)
 
786
   sizze *= npix[n];
 
787
 
 
788
/* (void) SCFCRE(outfile,fmtout,F_O_MODE,F_IMA_TYPE,size,&imnob); */
 
789
(void) CGN_IBUILD(imnoa,outfile,fmtout,sizze,&imnob,&clony);
 
790
 
 
791
 
 
792
/* allocate virtual memory for in/out frame buffers with right data types */
 
793
 
 
794
if (sizze <= mapsize) mapsize = sizze;
 
795
(void) SCFCRE("work1",fmtin,F_X_MODE,F_IMA_TYPE,mapsize,&imnow1);
 
796
(void) SCFMAP(imnow1,F_X_MODE,1,mapsize,&nval,&pntra);
 
797
 
 
798
if (fmtin != fmtout)
 
799
   {
 
800
   (void) SCFCRE("work2",fmtout,F_X_MODE,F_IMA_TYPE,mapsize,&imnow2);
 
801
   (void) SCFMAP(imnow2,F_X_MODE,1,mapsize,&nval,&pntrb);
 
802
   }
 
803
else
 
804
   pntrb = pntra;
 
805
 
 
806
 
 
807
/*  now we do the conversion   */
 
808
 
 
809
felm = 0;
 
810
while (felm < sizze)
 
811
   {
 
812
   n = felm + 1;
 
813
   (void) SCFGET(imnoa,n,mapsize,&nval,pntra);
 
814
   if (fmtin != fmtout) 
 
815
      conv_pix(pntrb,pntra,fmtout,fmtin,nval);
 
816
   (void) SCFPUT(imnob,n,nval,pntrb);
 
817
   felm += nval;
 
818
   }
 
819
 
 
820
 
 
821
/*  handle over, underflow  */
 
822
 
 
823
if (fmtout == D_I1_FORMAT)
 
824
   {
 
825
   if (cuts[2] < 0) uflow = 1;
 
826
   if (cuts[3] > 255) oflow = 1;
 
827
   }
 
828
else if (fmtout == D_I2_FORMAT)
 
829
   {
 
830
   if (cuts[2] < -32768) uflow = 1;
 
831
   if (cuts[3] > 32767) oflow = 1;
 
832
   }
 
833
else if (fmtout == D_UI2_FORMAT)
 
834
   {
 
835
   if (cuts[2] < 0) uflow = 1;
 
836
   if (cuts[3] > 65534) oflow = 1;
 
837
   }
 
838
if (uflow == 1)
 
839
   SCTPUT("Warning: Format conversion results in underflow...");
 
840
if (oflow == 1)
 
841
   SCTPUT("Warning: Format conversion results in overflow...");
 
842
 
 
843
 
 
844
if (clony == 0)
 
845
   (void) SCDCOP(imnoa,imnob,1,cbuf);   /* copy all descriptors */
 
846
 
 
847
n = CGN_INDEXC(dely,',');               /* test history_update_flag */
 
848
if (n > 0)
 
849
   dely[1] = dely[n+1];
 
850
else
 
851
   dely[1] = 'u';
 
852
 
 
853
if ((dely[1] == 'U') || (dely[1] == 'u')) 
 
854
   (void) CGN_DSCUPD(imnob,imnob," ");          /* update history */
 
855
 
 
856
 
 
857
 
 
858
/* if delete flag set, delete the input frame  */
 
859
 
 
860
if ((dely[0] == 'D') || (dely[0] == 'd')) (void) SCFDEL(infile);
 
861
}
 
862
 
 
863
/*
 
864
 
 
865
*/
 
866
 
 
867
void transcube(infile,outfile)
 
868
char  *infile, *outfile;
 
869
 
 
870
{
 
871
char   cbuf[12], temp[128];
 
872
char   *pntra;
 
873
 
 
874
static char workfile[] = "middummxyz.bdf";
 
875
 
 
876
int  nobytes, mapsize, unit, null, fmtin;
 
877
int  naxis, npix[3], newpix[3], imnoa, imnob, imnow, size;
 
878
int  info[5], planedir, nval, mm[3];
 
879
register int  nr, mr;
 
880
 
 
881
double  start[3], step[3], nstart[3], nstep[3];
 
882
 
 
883
 
 
884
(void) SCFINF(infile,2,info);                   /* get source format  */
 
885
nobytes = info[0];                              /* bytes per pixel */
 
886
fmtin =  info[1];
 
887
planedir = 0;
 
888
 
 
889
(void) SCFOPN(infile,fmtin,0,F_IMA_TYPE,&imnoa);
 
890
(void) SCDRDI(imnoa,"NAXIS",1,1,&nval,&naxis,&unit,&null);
 
891
if (naxis != 3)
 
892
   SCETER(3,"Input image has not 3 dimensions...");
 
893
(void) SCDRDI(imnoa,"NPIX",1,naxis,&nval,npix,&unit,&null);
 
894
(void) SCDRDD(imnoa,"START",1,naxis,&nval,start,&unit,&null);
 
895
(void) SCDRDD(imnoa,"STEP",1,naxis,&nval,step,&unit,&null);
 
896
 
 
897
 
 
898
/* create working frame (may be renamed to output frame) */
 
899
 
 
900
size = npix[0] * npix[1] * npix[2];             /*  get total size of frame */
 
901
(void) SCFCRE(workfile,fmtin,F_O_MODE,F_IMA_TYPE,size,&imnob);
 
902
 
 
903
 
 
904
(void) SCKGETC("P3",1,4,&nval,cbuf);                    /* get plane specs */
 
905
CGN_UPSTR(cbuf);
 
906
 
 
907
if ( (strncmp(cbuf,"ZY",2) == 0) || (strncmp(cbuf,"YZ",2) == 0) )
 
908
   {
 
909
   mm[0] = 2;
 
910
   mm[1] = 1;
 
911
   mm[2] = 0;
 
912
   planedir = Z_Y_PLANE;
 
913
   if (cbuf[0] == 'Y')
 
914
      {
 
915
      SCTPUT(
 
916
"Warning: YZ-plane is handled as ZY-plane, i.e. rows are in z-direction...");
 
917
      }
 
918
   }
 
919
else if ( (strncmp(cbuf,"ZX",2) == 0) || (strncmp(cbuf,"XZ",2) == 0) )
 
920
   {
 
921
   mm[0] = 0;
 
922
   mm[1] = 2;
 
923
   mm[2] = 1;
 
924
   planedir = X_Z_PLANE;
 
925
   if (cbuf[0] == 'Z')
 
926
      {
 
927
      SCTPUT(
 
928
"Warning: ZX-plane is handled as XZ-plane, i.e. rows are in x-direction...");
 
929
      }
 
930
   }
 
931
else
 
932
   SCETER(3,"Invalid plane specification...");
 
933
 
 
934
for (nr=0; nr<3; nr++)
 
935
   {
 
936
   newpix[nr] = npix[mm[nr]];
 
937
   nstart[nr] = start[mm[nr]];
 
938
   nstep[nr] = step[mm[nr]];
 
939
   }
 
940
 
 
941
(void) SCDCOP(imnoa,imnob,1,cbuf);              /* copy all descriptors */
 
942
(void) SCDWRI(imnob,"NPIX",newpix,1,naxis,&unit);       /* update NPIX */
 
943
(void) SCDWRD(imnob,"START",nstart,1,naxis,&unit);      /* update START */
 
944
(void) SCDWRD(imnob,"STEP",nstep,1,naxis,&unit);        /* update STEP */
 
945
 
 
946
 
 
947
/* allocate virtual memory for buffer with right data types */
 
948
 
 
949
mapsize = newpix[0] * newpix[1];
 
950
(void) SCFCRE("work1",fmtin,F_X_MODE,F_IMA_TYPE,mapsize,&imnow);
 
951
(void) SCFMAP(imnow,F_X_MODE,1,mapsize,&nval,&pntra);
 
952
 
 
953
 
 
954
/* loop through YZ- or XZ_planes and store them as XY_plane into
 
955
   workframe                                                     */
 
956
 
 
957
for (nr=0; nr<newpix[2]; nr++)
 
958
   {
 
959
   mr = nr + 1;
 
960
   (void) SCPGET(imnoa,planedir,mr,pntra);
 
961
   (void) SCPPUT(imnob,X_Y_PLANE,mr,pntra);
 
962
   }
 
963
 
 
964
CGN_DSCUPD(imnoa,imnob," ");            /* update history */
 
965
 
 
966
 
 
967
/* now test if result should replace input frame or go to a new frame */
 
968
 
 
969
if (outfile[0] == '+')
 
970
   CGN_CLEANF(infile,0,temp,120,&nval,&null);
 
971
else
 
972
   CGN_CLEANF(outfile,0,temp,120,&nval,&null);
 
973
(void) SCFRNM(workfile,temp);
 
974
 
 
975
}
 
976
 
 
977
/*
 
978
 
 
979
*/
 
980
 
 
981
int main()
 
982
 
 
983
{
 
984
char   infile[124], outfile[124], mapfile[124], cbuf[12];
 
985
 
 
986
int    nval;
 
987
 
 
988
 
 
989
(void) SCSPRO("genxy1");            
 
990
 
 
991
(void) SCKGETC("IN_A",1,120,&nval,infile);              /* get input frame   */
 
992
(void) SCKGETC("OUT_A",1,120,&nval,outfile);            /* get output frame  */
 
993
 
 
994
(void) SCKGETC("ACTION",1,3,&nval,cbuf);                /* get action flag */
 
995
 
 
996
if (strcmp(cbuf,"TRA") == 0) 
 
997
   transcube(infile,outfile);
 
998
 
 
999
else if (strcmp(cbuf,"COP") == 0) 
 
1000
   copyii(infile,outfile);
 
1001
 
 
1002
else if (strcmp(cbuf,"CON") == 0) 
 
1003
   convert(infile,outfile);
 
1004
 
 
1005
else if (strcmp(cbuf,"MAP") == 0) 
 
1006
   {
 
1007
   (void) SCKGETC("IN_B",1,120,&nval,mapfile);
 
1008
   mapima(infile,mapfile,outfile);
 
1009
   }
 
1010
 
 
1011
(void) SCSEPI();
 
1012
return(0);
 
1013
 
 
1014
}