Debian Codegraph @

libsrc/st/midfcta.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2010 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: [email protected]
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 /*+++++++++++++++++++++ Module midfcta +++++++++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION midfcta.c
31 .AUTHOR Klaus Banse
32 .KEYWORDS Midas utility routines.
33 .ENVIRONMENT VMS and UNIX
34 .COMMENTS
35 holds MID_INITFR, MID_FCTIN
36
37 .VERSION
38
39 100222 last modif
40 ------------------------------------------------------------------------*/
41
42 #include <fileexts.h>
43 #include <osyparms.h>
44 #include <computer.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <osparms.h>
48
49
50 #define BIT_0 0x1
51
52 #define FILE_MASK 0 /* CG. 920529: Set to 0. osfcreate() makes default */
53 /*
54
55 */
56
57 int MID_INITFR(name,dattype,filtype,sizze,optio,entrx)
58
59 /*++++++++++++++++++++++++++++++++++++++++++++++++++
60 .KEYWORDS
61 frame control table, initialisation
62 .PURPOSE
63 create + initialize a new frame
64 .ALGORITHM
65 create a new bulk data frame
66 set up the 1. virtual block as frame control block, FCB
67 reserve the next 44 blocks as LDB's for the standard descriptors
68 data starts from virtual block 46 on (first block = #1)
69 .RETURNS
70 status: I*4 return status
71 -----------------------------------------------------*/
72
73 char *name; /* IN : translated frame name with type */
74 int dattype; /* IN : type of data, I*1, I*2, I*4, R*4 */
75 int filtype; /* IN : type of frame */
76 size_t sizze; /* IN : no. of pixels in frame
77 if < 0, we only want to create the file header */
78 int *optio; /* IN : optio[0] = 1 for cloning, else 0
79 optio[1] = imno of source image (if optio[0] = 1) */
80 int *entrx; /* OUT: FCT entry number (= index) */
81
82 {
83 int n, mm, status, iochan;
84 int savftype, dscdsiz, myentry, source_entry;
85 int dummy_data;
86
87 unsigned int nvb;
88 unsigned int ext_size, data_bytes;
89 register int nr;
90
91 long int *lptr, oshtime();
92
93 size_t kldb[2], nrldb, data_start;
94 size_t alq, nblock, reqbytes;
95 size_t *sptr;
96
97 static int swapshort = SWAPSHORT;
98 static int swapint = SWAPINT;
99 static int floatfmt = FLOATFMT;
100
101 struct FCB_STRUCT *fcbp, *source_fcbp;
102
103 struct FCT_STRUCT *fctpntr, *source_fctpntr;
104
105 static char Midvers[] = "VERS_120"; /* as of 090401 */
106
107
108
109
110
111 /* calculate no. of blocks needed for data */
112
113 data_bytes = get_byte(dattype);
114 dummy_data = 0;
115
116 if (sizze < 1) /* that means F_H_MODE ... */
117 {
118 savftype = filtype;
119 filtype = -99;
120 sizze = 0; /* just to underline... */
121 nblock = 0; /* no data yet */
122 }
123
124 else
125 {
126 nblock = (size_t) data_bytes * sizze; /* check for overflow... */
127 if (unlikely(nblock > KIWORDS[OFF_AUX+19])) /* AUX_MODE(20) = max int */
128 {
129 status = ERR_OUTLIM;
130 goto end_of_it;
131 }
132
133 savftype = -1;
134 mm = OUR_BLOCK_SIZE - 1;
135
136 if (filtype < 11) /* binary Midas format */
137 nblock += mm;
138 else /* intl FITS files */
139 nblock = data_bytes + mm;
140
141 nblock /= OUR_BLOCK_SIZE;
142 }
143
144
145 /* calculate size of descr. directory and no. of LDBs needed for it */
146
147 mm = (FCT.PARM[0] + 1) * YENTRY_SIZE; /* total size: with dscdir */
148 ext_size = 6000;
149
150 nvb = ((mm - 1)/ext_size) + 1; /* no. of extensions needed */
151 dscdsiz = (nvb * ext_size) + 12; /* size of descr_dir and
152 its atom header (which is = 3 ints) */
153 kldb[0] = ((dscdsiz - 1)/LDB_DATA) + 1; /* LDBs needed for dscdir */
154 n = (FCT.PARM[0]*12) + FCT.PARM[1]; /* 12 bytes atom header per descr */
155 kldb[1] = ((n - 1)/LDB_DATA) + 1; /* LDBs needed for dsc data */
156
157
158 /* calculate initial no. of LDBs needed for descr. data */
159
160 if (*optio == 0)
161 {
162 source_entry = -1;
163 source_fctpntr = NULL;
164 source_fcbp = NULL;
165 data_start = 0;
166 nrldb = kldb[0] + kldb[1];
167 }
168
169 else /* cloning possible from VERSION_105 on */
170 {
171 source_entry = *(optio+1); /* get imno of source frame */
172 source_fctpntr = FCT.ENTRIES + source_entry;
173 source_fcbp = source_fctpntr->FZP;
174
175
176 nvb = source_fcbp->LEXBDF;
177 data_start = source_fcbp->D1BLOCK;
178
179 /* check, if descriptors already extended over data block */
180
181 if (nvb > data_start)
182 {
183 if ((data_bytes != get_byte(source_fctpntr->FORMAT)) ||
184 (sizze != source_fctpntr->SIZE))
185 { /* need same no. of data blocks */
186 status = ERR_NOCLON; /* for cloning */
187 goto end_of_it;
188 }
189
190 if (source_fcbp->FITSINF1 > 0) /* internal FITS file, no data yet */
191 {
192 n = source_fcbp->LEXBDF - 2;
193 kldb[0] = n/4;
194 kldb[1] = 0;
195 dummy_data = 1;
196 data_start = source_fcbp->LEXBDF + 1;
197 }
198 else
199 {
200 kldb[0] = source_fcbp->INLDB[0];
201 kldb[1] = source_fcbp->INLDB[1];
202 }
203 }
204
205 nrldb = source_fcbp->NOLDB; /* use same no. of LDBs */
206 }
207
208 alq = 1 + (4*nrldb) + dummy_data + nblock; /* 1 FCB + nrldb LDBs + data */
209 /* an LDB = 4 blocks */
210
211 /*
212 printf("MID_INITFR: *optio = %d, sizze = %d, kldb[0,1] = %d, %d\n",
213 *optio,sizze,kldb[0],kldb[1]);
214 printf("INITFR: nrldb = %d, dummy_data = %d, nblock = %d\n",
215 nrldb,dummy_data,nblock);
216 */
217
218
219 /* check, that no such file is already in FCT + create frame */
220
221 n = MID_FINDFR(name);
222 if (n >= 0) return (-99); /* tell SCFCRE that file should be closed */
223
224 /*
225 printf("INITFR: dscdsiz = %d, ext_size = %d, noext = %d, alq = %ld\n",
226 dscdsiz,ext_size,nvb,alq);
227 */
228
229 /* create file on disk */
230
231 myentry = *entrx;
232
233
234 if (filtype > 10) /* is it a memory file? */
235 {
236 status = MID_VMEM(1,(int) alq,&iochan); /* allocate memory for file */
237 if (status != ERR_NORMAL) goto end_of_it;
238
239 /* do what MID_ACCFRM would do */
240
241 myentry = MID_CREFRE(name,0); /* create one more entry */
242
243 if (myentry < 0)
244 {
245 status = ERR_FILNAM;
246 goto end_of_it;
247 }
248
249 fctpntr = FCT.ENTRIES + myentry;
250 fctpntr->COMPRESS = 0;
251 fctpntr->IOCHAN = iochan;
252 #if vms
253 fctpntr->FILEIDA = iochan;
254 fctpntr->FILEIDB = iochan;
255 #else
256 fctpntr->FILEID = iochan;
257 #endif
258 fctpntr->CATALOG[1] = 'N';
259
260 fcbp = fctpntr->FZP;
261 memset((void * )fcbp,32,(size_t)512); /* init FCB to ' ' */
262
263 nvb = (int) alq; /* now, nvb = total no. of blocks */
264 goto type_switch;
265 }
266
267 else
268 {
269
270 #if vms
271
272 FSY_CREBDF(name,(int)strlen(name),(int) alq,&nvb,&status);
273 if ( !(status & BIT_0) )
274 {
275 MID_ERROR("FSY","MID_INITFR: ",status,0);
276 status = ERR_FRMNAC;
277 goto end_of_it;
278 }
279
280 #else
281
282 reqbytes = alq * (size_t) OUR_BLOCK_SIZE;
283 mm = osfcreate(name,reqbytes,FILE_MASK);
284 if (mm != 1)
285 {
286 MID_ERROR("FSY","MID_INITFR: ",mm,0);
287 status = ERR_FRMNAC;
288 goto end_of_it;
289 }
290
291 nvb = (unsigned int) alq;
292
293 #endif
294 }
295
296
297 /* use MID_ACCFRM to open file again + get FCB in */
298
299 status = MID_ACCFRM(name,2,&myentry,&n);
300 if (status != ERR_NORMAL) goto end_of_it;
301
302 fctpntr = FCT.ENTRIES + myentry;
303 iochan = fctpntr->IOCHAN;
304 fcbp = fctpntr->FZP;
305 memset((void * )fcbp,32,(size_t)512); /* init FCB to ' ' */
306
307
308 /* fill FCB.BDTYPE with type of frame */
309
310 type_switch:
311 fcbp->FITSINF1 = 0;
312 fcbp->FITSINF2 = 0;
313 fcbp->NOLDB = nrldb; /* save no. of LDBs */
314 fctpntr->ACCESS = 'O'; /* set to write access */
315 sptr = (size_t *) fcbp->XNDVAL;
316 *sptr = sizze; /* fill FCB.XNDVAL */
317 fcbp->NDVAL = (unsigned int) sizze; /* and FCB.NDVAL as well */
318
319 switch(filtype)
320 {
321 case F_IMA_TYPE:
322 (void)strcpy(fcbp->BDTYPE,"IMAGE ");
323 break;
324
325 case F_TBL_TYPE:
326 (void)strcpy(fcbp->BDTYPE,"TABLE ");
327 break;
328
329 case F_FIT_TYPE:
330 (void)strcpy(fcbp->BDTYPE,"FITFILE");
331 break;
332
333 case F_FIMA_TYPE: /* FITS files */
334 (void)strcpy(fcbp->BDTYPE,"IMAGE-F");
335 fcbp->NDVAL = 1;
336 *sptr = (size_t) 1;
337 fcbp->FITSINF1 = (unsigned int) sizze;
338 sptr = (size_t *) fcbp->XFITSINF1;
339 *sptr = sizze; /* fill FCB.XFITSINF1 */
340 fctpntr->ACCESS = 'I'; /* not really a new file */
341 break;
342
343 case F_FTBL_TYPE:
344 (void)strcpy(fcbp->BDTYPE,"TABLE-F");
345 fcbp->NDVAL = 1;
346 *sptr = (size_t) 1;
347 fcbp->FITSINF1 = (unsigned int) sizze;
348 sptr = (size_t *) fcbp->XFITSINF1;
349 *sptr = sizze; /* fill FCB.XFITSINF1 */
350 fctpntr->ACCESS = 'I'; /* not really a new file */
351 break;
352
353 case F_FFIT_TYPE:
354 (void)strcpy(fcbp->BDTYPE,"FITFI-F");
355 fcbp->NDVAL = 1;
356 *sptr = (size_t) 1;
357 fcbp->FITSINF1 = (unsigned int) sizze;
358 sptr = (size_t *) fcbp->XFITSINF1;
359 *sptr = sizze; /* fill FCB.XFITSINF1 */
360 fctpntr->ACCESS = 'I'; /* not really a new file */
361 break;
362
363 case -99:
364 if (savftype == F_IMA_TYPE)
365 (void)strcpy(fcbp->BDTYPE,"IMAGE ");
366 else
367 (void)strcpy(fcbp->BDTYPE,"TABLE ");
368 fcbp->NDVAL = 0;
369 *sptr = (size_t) 0;
370 break;
371
372 default:
373 status = ERR_INPINV;
374 goto end_of_it;
375 }
376
377
378 /* fill frame control block
379
380 for (nr=22; nr<28; nr++) fcbp->CREATE[nr] = ' '; */
381
382 OSY_ASCTIM(fcbp->CREATE); /* get current time (26 chars.) */
383 lptr = (long int *) fcbp->CRETIM;
384 *lptr = oshtime();
385
386 fcbp->DSCFLAG = 'Y'; /* indicate new descr. stuff */
387 (void)strncpy(fcbp->VERSION,Midvers,8);
388
389 fcbp->PROT = 0;
390 fcbp->INCARN = 1;
391 fcbp->INLDB[0] = kldb[0];
392 fcbp->INLDB[1] = kldb[1];
393 fcbp->DBEGIN = dscdsiz; /* inital size of descr. directory */
394 fcbp->NOBYT = data_bytes;
395 fcbp->DFORMAT = dattype;
396 /* no. of data values (pixels) per page/block */
397 fcbp->PIXPBL = OUR_BLOCK_SIZE / data_bytes;
398 fcbp->DLBLOCK = (int) alq;
399
400 if (sizze == (size_t)0) /* no data (yet) */
401 fcbp->D1BLOCK = -1; /* no start block of data (yet) */
402 else
403 {
404 if (source_entry == -1)
405 fcbp->D1BLOCK = /* 1st block after LDB area */
406 1 + (4*(fcbp->INLDB[0]+fcbp->INLDB[1])) + 1;
407 else
408 fcbp->D1BLOCK = data_start;
409 }
410
411 fcbp->PTRLDB = 2; /* point to first LDB (= VBN 2) */
412 if (source_entry == -1)
413 fcbp->LEXBDF = (unsigned int) alq; /* log. file ext. -> last data block*/
414 else
415 fcbp->LEXBDF = source_fcbp->LEXBDF;
416
417 /* physical file extension (cluster fact.of disk) */
418
419 fcbp->PEXBDF = (unsigned int) nvb;
420 fcbp->NEXT = 0; /* we only have 1 FCB */
421
422
423 /* fill updated values into FCT */
424
425 fctpntr->SIZE = sizze;
426 fctpntr->NOBYTE = fcbp->NOBYT;
427 fctpntr->FORMAT = fctpntr->DATTYP = fcbp->DFORMAT;
428 fctpntr->PIXPBL = fcbp->PIXPBL;
429 fctpntr->STBLOK = fcbp->D1BLOCK;
430 fctpntr->CATALOG[0] = fcbp->BDTYPE[0];
431
432 if (swapshort == 12)
433 fcbp->SWPSHORT = '=';
434 else if (swapshort == 21)
435 fcbp->SWPSHORT = 's';
436 else
437 {
438 status = ERR_INSBAD;
439 goto end_of_it; /* do this check only once */
440 }
441
442 if (swapint == 1234)
443 fcbp->SWPINT = '=';
444 else if (swapint == 4321)
445 fcbp->SWPINT = 's';
446 else if (swapint == 2143)
447 fcbp->SWPINT = 'h';
448 else /* if (swapint == 3412) */
449 fcbp->SWPINT = 'w';
450
451 if (floatfmt == IEEEFLOAT)
452 fcbp->FLOTFMT = '=';
453
454 #if vms
455 else if (floatfmt == VAXFLOAT)
456 fcbp->FLOTFMT = 'V';
457 else if (floatfmt == VAXGFLOAT)
458 fcbp->FLOTFMT = 'G';
459 #endif
460
461 else /* if (floatfmt == HPFLOAT) */
462 fcbp->FLOTFMT = 'H';
463
464
465 *entrx = myentry;
466
467
468 /* set the 6 basic info values for easy reading of data files */
469
470 for (nr=0; nr<4; nr++) fcbp->DATAINFO[nr] = 0;
471 fcbp->DATAINFO[4] = fcbp->DFORMAT;
472 fcbp->DATAINFO[5] = (fcbp->D1BLOCK - 1) * OUR_BLOCK_SIZE;
473
474 if (source_entry == (-1))
475 status = MID_INITDS(fcbp,iochan);
476 else
477 status = MID_CLONEDS(fcbp,iochan,source_fctpntr);
478
479 if (status == ERR_NORMAL)
480 status = OSY_WVB(fctpntr->IOCHAN,(char *)fcbp,OUR_BLOCK_SIZE,1);
481
482 if (status == ERR_NORMAL) return status;
483
484
485 /* prepare error messages for higher level routines */
486
487 end_of_it:
488 MID_ERROR("MIDAS","MID_INITFR:",status,0);
489 return status;
490 }
491 /*
492
493 */
494
495 int MID_TDCLON(source_entry,dattype,sizze)
496 /*++++++++++++++++++++++++++++++++++++++++++++++++++
497 .KEYWORDS
498 internal descriptor structure
499 .PURPOSE
500 test, if descr. cloning is possible or not
501 .RETURN
502 = 0, if no
503 = 1, if cloning of descriptors is possible
504 --------------------------------------------------*/
505
506 int source_entry; /* IN: entry of source image */
507 int dattype; /* IN: data format for new image */
508 int sizze; /* IN: no. of pixels of new image */
509
510 {
511 int data_bytes;
512
513 struct FCB_STRUCT *source_fcbp;
514
515 struct FCT_STRUCT *source_fctpntr;
516
517
518
519
520
521 source_fctpntr = FCT.ENTRIES + source_entry;
522 source_fcbp = source_fctpntr->FZP;
523
524
525 /* if descriptors already wrapped around data,
526 the no. of data blocks must be the same for the new image as for the
527 source image => the original data format must be the same */
528
529
530 if (source_fcbp->LEXBDF > source_fcbp->D1BLOCK)
531 {
532 data_bytes = get_byte(dattype); /* bytes per pixel of new frame */
533 if ((data_bytes != get_byte(source_fctpntr->FORMAT)) ||
534 (sizze != source_fctpntr->SIZE))
535 return 0; /* no descr. cloning possible */
536 }
537
538 return 1; /* Yes. we can clone the descriptors */
539 }
540
541 /* keep diagnostic printout for later usage...
542 printf("TDCLON: source logext, datstart = %d,
543 %d\n",source_fcbp->LEXBDF,
544 source_fcbp->D1BLOCK);
545
546 printf(" source size, b/p = %d, %d
547 new size, b/p = %d, %d\n",
548 source_fctpntr->SIZE,
549 get_byte(source_fctpntr->FORMAT),sizze,
550 get_byte(dattype));
551 */
552 /*
553
554 */
555
556 int MID_FCTIN(fctnos)
557 /*++++++++++++++++++++++++++++++++++++++++++++++++++
558 .KEYWORDS
559 frame control table
560 .PURPOSE
561 create the FCT structures for `fctsize' entries
562 .ALGORITHM
563 use malloc + free
564 .RETURNS
565 --------------------------------------------------*/
566
567 int fctnos; /* IN : no. of entries for extension \
568 if -1, create new structure */
569
570 {
571 int mm, fctsize;
572 register int nr, mr;
573
574 char *cptr;
575 static char *oldpntr;
576
577 struct FCT_STRUCT *fctpntr, *fctpntro;
578
579
580
581
582 /* do not forget to synchronize with $MID_INCLUDE/fctext.h !!! */
583
584 if (fctnos == -1)
585 {
586 fctnos = FCT_MAXENT; /* use initial value */
587 fctsize = (int) (sizeof(struct FCT_STRUCT) * fctnos) + 4;
588 cptr = malloc((size_t)fctsize);
589 if (cptr == NULL)
590 {
591 (void)printf("could not allocate %d bytes for FCT table\n\r",fctsize);
592 ospexit(0);
593 }
594
595 FCT.ENTRIES = (struct FCT_STRUCT *) cptr;
596
597 fctpntr = FCT.ENTRIES; /* clear FCT entries */
598 for (nr=0; nr<fctnos; nr++)
599 {
600 fctpntr->NAME[0] = ' ';
601 fctpntr->NAME[1] = '\0';
602 fctpntr ++;
603 }
604 }
605
606 else
607 {
608 mm = FCT.MAXENT; /* save old stuff for the moment */
609 fctpntro = FCT.ENTRIES;
610
611 fctsize = (int) (sizeof(struct FCT_STRUCT) * fctnos) + 4;
612 cptr = malloc((size_t)fctsize);
613 if (cptr == NULL)
614 {
615 (void)printf("could not allocate %d bytes for FCT table\n\r",fctsize);
616 ospexit(0);
617 }
618
619 FCT.ENTRIES = (struct FCT_STRUCT *) cptr;
620
621 fctpntr = FCT.ENTRIES; /* clear FCT entries */
622 for (nr=0; nr<fctnos; nr++)
623 {
624 fctpntr->NAME[0] = ' ';
625 fctpntr ++;
626 }
627
628
629 /* now copy old entries over */
630
631 fctpntr = FCT.ENTRIES; /* reset pointer to begin */
632 for (nr=0; nr<mm; nr++)
633 {
634 if (fctpntro->NAME[0] != ' ')
635 {
636 fctpntr->NAMLEN = fctpntro->NAMLEN;
637 fctpntr->IOCHAN = fctpntro->IOCHAN;
638 #if vms
639 fctpntr->FILEIDA = fctpntro->FILEIDA;
640 fctpntr->FILEIDB = fctpntro->FILEIDB;
641 #else
642 fctpntr->FILEID = fctpntro->FILEID;
643 #endif
644 for (mr=0; mr<4; mr++)
645 fctpntr->KAUX[mr] = fctpntro->KAUX[mr];
646 fctpntr->SIZE = fctpntro->SIZE;
647 fctpntr->PROT = fctpntro->PROT;
648 fctpntr->COMPRESS = fctpntro->COMPRESS;
649 fctpntr->NOBYTE = fctpntro->NOBYTE;
650 fctpntr->FORMAT = fctpntro->FORMAT;
651 fctpntr->PIXPBL = fctpntro->PIXPBL;
652 fctpntr->STBLOK = fctpntro->STBLOK;
653 fctpntr->DATTYP = fctpntro->DATTYP;
654 fctpntr->FILTYP = fctpntro->FILTYP;
655 fctpntr->FITSEXT = fctpntro->FITSEXT;
656 fctpntr->LINK[0] = fctpntro->LINK[0];
657 fctpntr->LINK[1] = fctpntro->LINK[1];
658 fctpntr->CR_FLAG = fctpntro->CR_FLAG;
659 fctpntr->O_NAMLEN = fctpntro->O_NAMLEN;
660 (void)strcpy(fctpntr->NAME,fctpntro->NAME);
661 #if vms
662 (void)strcpy(fctpntr->DEVICE,fctpntro->DEVICE);
663 #endif
664 for (mr=0; mr<4; mr++)
665 fctpntr->BDADDR[mr] = fctpntro->BDADDR[mr];
666 fctpntr->PNTR = fctpntro->PNTR;
667 for (mr=0; mr<3; mr++)
668 fctpntr->FITSADDR[mr] = fctpntro->FITSADDR[mr];
669 fctpntr->CATALOG[0] = fctpntro->CATALOG[0];
670 fctpntr->CATALOG[1] = fctpntro->CATALOG[1];
671 fctpntr->ACCESS = fctpntro->ACCESS;
672 fctpntr->FZP = fctpntro->FZP;
673 }
674 fctpntr ++;
675 fctpntro ++;
676 }
677
678 free(oldpntr);
679 }
680
681
682 FCT.MAXENT = fctnos;
683 oldpntr = cptr; /* save old char. pointer */
684 return 0;
685 }