Debian Codegraph @

libsrc/st/scfb.c

raw
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: [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 /*+++++++++++++++++++++++ SC interface module SCFB +++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module scfb.c
31 .AUTHOR K. Banse ESO - Garching
32 .KEYWORDS standard interfaces, bulk data files
33 .ENVIRONMENT VMS and UNIX
34 .COMMENTS
35 holds SCFOPN, SCFCLO, SCFIDI, SCFMAP, SCFUNM
36 .REMARKS
37 opening modes are: F_I_MODE, F_O_MODE, F_IO_MODE, F_X_MODE.
38 data types are: D_R4_FORMAT, D_I2_FORMAT, D_I1_FORMAT, D_OLD_FORMAT.
39 filetype no's are: F_IMA_TYPE, F_TBL_TYPE, F_FIT_TYPE.
40
41 .VERSION [1.00] 920212: pulled over from scf.c
42
43 110202
44 ----------------------------------------------------------------------------*/
45
46 #include <stdio.h>
47 #include <fcntl.h>
48 #include <stdlib.h>
49
50 #include <fileexts.h>
51 #include <fitsdef.h>
52 #include <computer.h>
53 #include <fsydef.h>
54 #include <osyparms.h>
55
56
57
58 static struct FCT_STRUCT *fctpntr;
59
60 /*
61
62 */
63
64 int SCFOPN(name,dattype,newopn,filtype,imno)
65
66 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
67 .PURPOSE
68 open a frame + return its file no.
69 .ALGORITHM
70 use CGN_CLEANF + MID_ACCFRM to do the job
71 .RETURNS
72 return status ( 0 = o.k )
73 -----------------------------------------------------------------------*/
74
75 char *name; /* IN: name of data frame */
76 int dattype; /* IN: data types as defined in Module header */
77 int newopn; /* IN: new_open_flag:
78 = 0, open normally
79 = 1, open same file again + get new imno */
80 int filtype; /* IN: filetype, e.g. F_IMA_TYPE, ... */
81 int *imno; /* IN/OUT: file no. of the frame, if `imno' given on
82 input, try to return the same no. ... */
83
84 {
85 int i, status, sta[3], end[3], newsize, iext;
86 int tno, mm, fctnew, namtyp, part_no;
87 static int subcount=0; /* for extracted frames */
88
89 float rbuf;
90
91 double dbuf;
92
93 char temp[200], cbuf[44];
94
95 struct FCB_STRUCT *fcbp;
96
97
98
99
100 /*
101 printf("entering SCFOPN with name = %s\n",name);
102 */
103
104
105 /* check file name */
106
107 status = CGN_CLEANF(name,filtype,temp,FCT_NAME_LEN,&namtyp,&part_no);
108 if (status != 0)
109 {
110 status = ERR_FILNAM; /* invalid syntax */
111 MID_E1(3,name,status,1);
112 return status;
113 }
114
115
116 /* access bulk data frame + get FCB in */
117
118 if (part_no < 0) /* we access a FITS extension */
119 {
120 fctnew = 0;
121 mm = - part_no; /* extension after name[mm] */
122 i = CGN_CNVT(&name[mm+1],1,1,&iext,&rbuf,&dbuf);
123 if (i != 1)
124 {
125 status = ERR_FITEXT;
126 MID_E1(3,temp,status,1);
127 return status;
128 }
129
130 /*
131 printf("SCFOPN: call ACCFITS(%s,..) with extension = %d\n",temp,iext);
132 */
133 status = MID_ACCFITS(temp,newopn,iext,imno);
134 if (status != ERR_NORMAL)
135 {
136 MID_E1(3,temp,status,1);
137 return status;
138 }
139 }
140
141 else
142 {
143 if (newopn > 0) /* open file again for other work */
144 mm = 1;
145 else
146 mm = 0;
147 /*
148 printf("SCFOPN: call ACCFRM(%s,...\n",temp);
149 */
150 status = MID_ACCFRM(temp,mm,imno,&fctnew); /* get file into FCT */
151 if (status != ERR_NORMAL)
152 {
153 /*
154 printf("SCFOPN: call ACCFITS(%s,...\n",temp);
155 */
156 if (status == ERR_VERNOR) /* if Version problem, try FITS */
157 status = MID_ACCFITS(temp,newopn,-1,imno);
158 if (status != ERR_NORMAL)
159 {
160 MID_E1(3,temp,status,1);
161 return status;
162 }
163 else
164 MID_POPERR(); /* pop ACCFRM error from stack again */
165 }
166 }
167
168 fctpntr = FCT.ENTRIES + (*imno);
169 fcbp = fctpntr->FZP;
170
171
172 /* if not F_OLD_TYPE, test if file types match */
173
174 if (filtype != F_OLD_TYPE)
175 {
176 if (fcbp->BDTYPE[0] == 'F')
177 mm = F_FIT_TYPE;
178 else if (fcbp->BDTYPE[0] == 'T')
179 mm = F_TBL_TYPE;
180 else
181 mm = F_IMA_TYPE;
182
183 /* for old FIT files we still have fcbp->BDTYPE[0] = 'I' */
184
185 if ((filtype != mm) && (filtype != F_FIT_TYPE))
186 SCTPUT("Warning: file type of frame + of SCFOPN don't match...!");
187 }
188
189
190 /* store required data type + default access mode */
191
192 if (fctnew == 0) /* file not already open, */
193 { /* so we update DATTYP + ACCESS */
194 fctpntr->ACCESS = 'I';
195 if (dattype == D_OLD_FORMAT)
196 dattype = fctpntr->FORMAT; /* take current data type */
197 fctpntr->DATTYP = dattype;
198 }
199 else
200 {
201 if (fctpntr->DATTYP == D_OLD_FORMAT)
202 {
203 if (dattype == D_OLD_FORMAT) dattype = fctpntr->FORMAT;
204 fctpntr->DATTYP = dattype;
205 }
206
207 else if ((dattype != D_OLD_FORMAT) && (dattype != fctpntr->DATTYP))
208 {
209 if ((fctpntr->FILTYP > 0) && (fctpntr->FITSOUT != 'X'))
210 { /* it's a FITS file and we didn't get data yet */
211 int data_bytes; /* so change data format */
212
213 fctpntr->DATTYP = dattype; /* set to new type */
214 data_bytes = get_byte(dattype); /* no. of bytes needed */
215
216 /* why this? it changes the data format of a FITS file...
217 fcbp->NOBYT = data_bytes;
218 fcbp->DFORMAT = dattype;
219 fcbp->PIXPBL = OUR_BLOCK_SIZE / data_bytes;
220 fcbp->DATAINFO[4] = fcbp->DFORMAT;
221 */
222
223 fctpntr->NOBYTE = fcbp->NOBYT;
224 fctpntr->FORMAT = fcbp->DFORMAT;
225 fctpntr->PIXPBL = fcbp->PIXPBL;
226 }
227 else
228 {
229 status = ERR_INPINV;
230 (void) snprintf(cbuf,(size_t)44," ... old, new dattype = %d, %d",
231 fctpntr->DATTYP,dattype);
232 (void) strcat(temp,cbuf);
233 MID_E1(3,temp,status,1);
234 return status;
235 }
236 }
237 }
238
239
240 /* test for subframe option */
241
242 if (part_no > 0)
243 {
244 status = Fextcor(*imno,&name[part_no],sta,end,&newsize);
245 if (status != ERR_NORMAL)
246 {
247 MID_E1(3,&name[part_no],status,1);
248 return status;
249 }
250
251 (void) snprintf(cbuf,(size_t)44,"MIDas%dX%d.bdf",subcount++,*imno);
252 status = SCFCRE(cbuf,dattype,F_O_MODE,F_IMA_TYPE,newsize,&tno);
253
254 if (status != ERR_NORMAL)
255 {
256 MID_E1(3,cbuf,status,1);
257 return status;
258 }
259
260 fctpntr->LINK[0] = 1; /* declare as father */
261 fctpntr->LINK[1] ++; /* increase count of sons */
262
263 fctpntr = FCT.ENTRIES + tno;
264 fctpntr->LINK[0] = 2; /* declare as son */
265 fctpntr->LINK[1] = *imno; /* and link it to father */
266 if (newopn == -1) fctpntr->LINK[0] = 3; /* only descriptors... */
267
268 status = Fextfr(*imno,sta,end,tno);
269 if (status != ERR_NORMAL)
270 {
271 (void) strcpy(temp,"actual extraction...");
272 MID_E1(3,temp,status,1);
273 return status;
274 }
275
276 fctpntr->ACCESS = 'I'; /* SCFCRE had set it to 'O' before.. */
277 fctpntr->CATALOG[1] = 'N';
278 *imno = tno; /* return imno of son ... */
279 }
280
281 if ((filtype == F_OLD_TYPE) && (fctnew == 0)) /* first time access */
282 {
283 fctpntr = FCT.ENTRIES + (*imno);
284 fctpntr->CATALOG[0] = 'O'; /* avoid going thru TCTCLO in SCSEPI! */
285 }
286
287
288 /* intl FITS table files need special treatment... */
289
290 if (fctpntr->FILTYP > 1)
291 {
292 char *datpntr, origname[FCT_NAME_LEN];
293
294 datpntr = fctpntr->BDADDR[0];
295 if (fctpntr->O_NAMLEN == 0)
296 (void) strcpy(origname,fctpntr->NAME);
297 else /* internal FITS file */
298 {
299 (void) strcpy(origname,fctpntr->NAME+4);
300 mm = fctpntr->O_NAMLEN - 4; /* we just removed leading "FITZ" */
301 origname[mm] = '\0';
302 }
303 status = MID_fitsin(2,origname,-1,"",datpntr,imno,&i);
304 status = TCTFIX(*imno); /* write table control descriptors */
305 fctpntr->ACCESS = 'I'; /* reset */
306 fctpntr->KAUX[2] = 0;
307 }
308
309
310 fctpntr->CR_FLAG = 2; /* mark as just opened */
311 return status;
312 }
313 /*
314
315 */
316
317 int SCFCLO(imno)
318
319 /*++++++++++++++++++++++++++++++++++++++++++++++++++
320 .PURPOSE
321 release a frame
322 .ALGORITHM
323 find frame entry + add frame to catalogue, if FCT_CATALOG[1] = Y,
324 .RETURNS
325 return status ( 0 = o.k )
326 --------------------------------------------------*/
327
328 int imno; /* IN: file no. of data frame */
329
330 {
331 int i, mm, fitsout, fitsupdate, status;
332
333 char cc, temp[200], cbuf[88];
334 char origname[FCT_NAME_LEN], *nampntr;
335
336 struct FCB_STRUCT *fcbp;
337
338 struct FCT_STRUCT *fctpntrz;
339
340 struct LDB_STRUCT *ldbp;
341
342
343
344 /*
345 printf("entering SCFCLO(%d)\n",imno);
346 */
347
348 if ( (imno < 0) || (imno >= FCT.MAXENT) )
349 {
350 status = ERR_INPINV;
351 MID_E2(1,imno,"...",status,1);
352 return status;
353 }
354
355 fctpntr = FCT.ENTRIES + imno;
356 if (fctpntr->NAME[0] == ' ') return ERR_NORMAL; /* nothing to do ... */
357
358
359 if (fctpntr->ACCESS == 'X') /* if scratch file, not much to do */
360 {
361 if (fctpntr->BDADDR[0] != (char *) 0)
362 {
363 free(fctpntr->BDADDR[0]);
364 fctpntr->BDADDR[0] = (char *) 0;
365 free(fctpntr->FZP);
366 }
367 fctpntr->NAME[0] = ' ';
368 fctpntr->LINK[0] = 0;
369 fctpntr->LINK[1] = 0;
370 return ERR_NORMAL;
371 }
372
373 /*
374 printf("SCFCLO: %s with descr_access = %d\n",fctpntr->NAME,fctpntr->KAUX[2]);
375 */
376
377
378 fcbp = fctpntr->FZP;
379 if ( (fctpntr->PROT != 2) && (fctpntr->KAUX[2] == 1) )
380 {
381 (void) cacheLDB(3,fctpntr->IOCHAN,-1,&ldbp); /* write out last LDB */
382 (void) OSY_WVB(fctpntr->IOCHAN,(char *)fcbp,512,1); /* fixed size (512) */
383 }
384 else
385 (void) clearLDB(fctpntr->IOCHAN); /* clear LDB cache */
386
387
388 /* if catalog is enabled, add this entry */
389
390 if (fctpntr->CATALOG[1] != 'N') /* check catalog flag */
391 {
392 int e_c, e_d, e_l, n0=0, n1=1;
393
394 mm = OFF_CATAL;
395 i = KIWORDS[OFF_CATINF+10]; /* length of catalog file name */
396 cc = fctpntr->CATALOG[0];
397
398 if (cc == 'T')
399 mm += i;
400 else if (cc == 'F')
401 mm += (i+i);
402
403 (void) memcpy(cbuf,&KCWORDS[mm],(size_t)i);
404 cbuf[i] = '\0';
405 mm = CGN_INDEXC(cbuf,' '); /* truncate file name */
406 if (mm > 0) cbuf[mm] = '\0';
407
408 SCECNT("GET",&e_c,&e_d,&e_l);
409 SCECNT("PUT",&n1,&n0,&n0); /* disable SC errors */
410
411 status = SCCADD(cbuf,fctpntr->NAME," ");
412
413 SCECNT("PUT",&e_c,&e_d,&e_l);
414 fctpntr->CATALOG[1] = 'N';
415 }
416
417
418 /* if we have subframes, these have to be treated first... */
419
420 if ((fctpntr->LINK[0] == 1) && (fctpntr->LINK[1] > 0))
421 { /* we're the main frame */
422 fctpntrz = FCT.ENTRIES;
423
424 /*
425 printf("for %s LINK[0,1] = %d, %d - we have sons...\n",
426 fctpntr->NAME,fctpntr->LINK[0],fctpntr->LINK[1]);
427 */
428
429 for (i=0; i<FCT.MAXENT; i++) /* look for all the sons */
430 {
431 if ((fctpntrz->NAME[0] != ' ') && /* only filled entries */
432 (fctpntrz->LINK[0] == 2) && (fctpntrz->LINK[1] == imno) &&
433 (fctpntrz->ACCESS == 'O'))
434 {
435 if (fctpntrz->BDADDR[0] != (char *) 0) /* release memory */
436 {
437 if (fctpntrz->DATTYP == fctpntrz->FORMAT)
438 (void) wrdisk(i,fctpntrz->KAUX[0],fctpntrz->KAUX[1],
439 fctpntrz->BDADDR[0]);
440 else
441 (void) conv_dat(2,i,fctpntrz->KAUX[0],fctpntrz->KAUX[1],
442 &mm,fctpntrz->BDADDR);
443 }
444 Finsfr(imno,i);
445 fctpntrz->ACCESS = 'I'; /* clear access mode of son */
446 }
447 fctpntrz ++;
448 }
449 fctpntr->LINK[1] = 0; /* all possible sons processed */
450 }
451
452
453 /* check, if we close the memory based version of an existing FITS file
454 if yes, test if it has been updated/overwritten */
455
456 fitsout = 0;
457 fitsupdate = 0; /* default to update=NO */
458
459
460 /* process a FITS file */
461
462 if (fctpntr->FILTYP > 0)
463 {
464 if (fctpntr->O_NAMLEN == 0)
465 (void) strcpy(origname,fctpntr->NAME);
466 else /* internal FITS file */
467 {
468 (void) strcpy(origname,fctpntr->NAME+4);
469 i = fctpntr->O_NAMLEN - 4; /* we removed leading "FITZ" */
470 origname[i] = '\0';
471 }
472 nampntr = origname;
473 }
474 else
475 nampntr = fctpntr->NAME;
476
477 if (fctpntr->FILTYP > 0)
478 {
479 if (fctpntr->FITSEXT == 0) /* only process, if no FITS extension */
480 {
481 fitsupdate = KIWORDS[OFF_AUX+13]; /* get FITS update flag */
482 if (fitsupdate == 1) /* o.k. - we write a FITS file, so test */
483 { /* if descr and/or data has changed */
484 if ((fctpntr->ACCESS == 'O') &&
485 (fctpntr->BDADDR[0] != (char *) 0))
486 { /* BDADDR[0] only set for images */
487 fctpntr->KAUX[2] = 0; /* avoid only updating descrs */
488 }
489
490 else if (fctpntr->KAUX[2] != 1) /* skip writing a FITS file */
491 goto set_ACCESS;
492
493 status = SCFSAV(imno,1); /* dump descr or descr+data to FITS */
494 if (status != ERR_NORMAL)
495 fitsout = -1; /* indicate FITS trouble */
496 else
497 fitsout = 1; /* rename also tables in SCFCLO */
498 }
499 }
500
501 set_ACCESS:
502 fctpntr->ACCESS = 'I'; /* avoid writing to disk again */
503 }
504
505 else /* check, if we store new frames in FITS format */
506 {
507 if (fctpntr->ACCESS == 'O')
508 fitsout = KIWORDS[OFF_AUX+12]; /* get FITS output flag */
509
510 if (fitsout == 1)
511 {
512 mm = fctpntr->NAMLEN - 4;
513 if (fctpntr->CATALOG[0] == 'T')
514 {
515 if (strncmp(fctpntr->NAME+mm,".tbl",4) == 0)
516 {
517 fitsout = 0;
518 goto write_to_disk;
519 }
520
521 fitsout = -99; /* set flag for TCTCLO ... */
522 }
523 else
524 {
525 if (strncmp(fctpntr->NAME+mm,".bdf",4) == 0)
526 {
527 fitsout = 0;
528 goto write_to_disk;
529 }
530
531 if (fctpntr->BDADDR[0] != (char *) 0) /* for images convert to */
532 { /* FITS format right away */
533 if (fctpntr->DATTYP == fctpntr->FORMAT)
534 status = wrdisk(imno,fctpntr->KAUX[0],fctpntr->KAUX[1],
535 fctpntr->BDADDR[0]);
536 else /* mismatched frame + writing data */
537 status = conv_dat(2,imno,fctpntr->KAUX[0],fctpntr->KAUX[1],
538 &mm,fctpntr->BDADDR);
539 if (status != ERR_NORMAL) goto end_2;
540 }
541
542
543 status = SCFSAV(imno,0); /* dump descr+data to FITS */
544 if (status != ERR_NORMAL) fitsout = -1;
545 }
546
547 fctpntr->ACCESS = 'I'; /* avoid saving anything */
548 }
549 }
550
551
552 write_to_disk:
553 status = ERR_NORMAL;
554 if (fctpntr->BDADDR[0] != (char *) 0) /* release allocated memory */
555 {
556 if (fctpntr->ACCESS == 'O') /* so, no FITS files here... */
557 {
558 if (fctpntr->DATTYP == fctpntr->FORMAT)
559 status = wrdisk(imno,fctpntr->KAUX[0],fctpntr->KAUX[1],
560 fctpntr->BDADDR[0]);
561 else /* mismatched frame + writing data */
562 status = conv_dat(2,imno,fctpntr->KAUX[0],fctpntr->KAUX[1],
563 &mm,fctpntr->BDADDR);
564 }
565
566 free(fctpntr->BDADDR[0]); /* also for the FITS guys */
567 fctpntr->BDADDR[0] = (char *) 0;
568 if (status != ERR_NORMAL) goto end_2;
569 }
570
571
572 /* if we are a subframe of somebody else, we must first update the father */
573
574 if (fctpntr->LINK[0] > 1)
575 { /* we're the subframe */
576 if (fctpntr->ACCESS == 'O')
577 {
578 i = fctpntr->LINK[1];
579 Finsfr(i,imno);
580 fctpntrz = FCT.ENTRIES + i;
581 fctpntrz->LINK[1] --; /* decrement count of sons */
582 fctpntr->ACCESS = 'I';
583 }
584 if (FCT.PARM[2] == 1)
585 { /* delete temp file */
586 status = osfdelete(fctpntr->NAME);
587 if (status != 0)
588 {
589 status = ERR_INPINV;
590 (void) snprintf(temp,(size_t)200,
591 "SCFCLO/osfdelete: %s ",fctpntr->NAME);
592 MID_ERROR("MID",temp,status,0);
593 }
594 else
595 status = ERR_NORMAL;
596 }
597 }
598
599
600 /* close the file on disk*/
601
602 status = OSY_DASSGN(imno,0); /* 2nd par. not used */
603 if (status != ERR_NORMAL) goto end_2; /* skip the rest... */
604
605 if (fitsout != 0) /* some FITS stuff to handle... */
606 {
607 if (fitsout == 1) /* we have to rename temp. FITS file */
608 {
609 status = osfrename("midFITS.mt",nampntr);
610 if (status != 0)
611 {
612 status = ERR_INPINV;
613 (void) snprintf(temp,(size_t)200,
614 "SCFCLO/osfrename: %s ",nampntr);
615 MID_ERROR("MID",temp,status,0);
616 }
617 }
618 else if (fitsout == -1) /* we had a problem writing the FITS file */
619 status = ERR_FILBAD;
620 }
621
622 else if ((fctpntr->COMPRESS != 0) && (KIWORDS[OFF_AUX+20] == 0))
623 { /* compress frame again */
624 char *Nullptr = (char *) 0;
625 /* AUX_MODE(21) for recompressing or not */
626 if (fctpntr->COMPRESS == 1)
627 (void)snprintf(temp,(size_t)200,"compress %s",nampntr);
628 else
629 (void)snprintf(temp,(size_t)200,"gzip %s",nampntr);
630 oshcmd(temp,Nullptr,Nullptr,Nullptr);
631 }
632
633
634 /* now free allocated memory as much as we can */
635
636 if (fctpntr->FILTYP > 0) /* its a FITS file */
637 {
638 BFDEF *bfdefptr;
639 TXDEF *txptr;
640 char *cpntr;
641
642 if (fctpntr->FITSADDR[2] != (char *) 0)
643 {
644 free((char *) fctpntr->FITSADDR[2]);
645 fctpntr->FITSADDR[2] = (char *) 0;
646 }
647 if (fctpntr->FITSADDR[1] != (char *) 0)
648 {
649 free((char *) fctpntr->FITSADDR[1]);
650 fctpntr->FITSADDR[1] = (char *) 0;
651 }
652 if (fctpntr->FITSADDR[0] != (char *) 0)
653 {
654 bfdefptr = (BFDEF *) fctpntr->FITSADDR[0];
655 cpntr = bfdefptr->extd;
656 if (cpntr != (char *) 0)
657 {
658 txptr = (TXDEF *) cpntr;
659 free((char *) txptr->col);
660 free(cpntr);
661 }
662 free((char *) fctpntr->FITSADDR[0]);
663 fctpntr->FITSADDR[0] = (char *) 0;
664 }
665 status = 0;
666 }
667
668
669 end_2:
670 fctpntr->NAME[0] = ' ';
671 fctpntr->NAME[1] = '\0';
672 fctpntr->LINK[0] = 0;
673 fctpntr->LINK[1] = 0;
674 free(fctpntr->FZP);
675
676
677 /* make sure, old_imno is reset in MID_DSCDIR... */
678
679 temp[0] = ' ';
680 MID_DRESET();
681
682 if (status != ERR_NORMAL) MID_E2(1,imno,"...",status,1);
683
684
685 /***************
686
687 OSY_COUNT(&i,&mm);
688 printf("OSY_RVB %d times OSY_WVB %d times\n",i,mm);
689
690 ***************/
691
692
693 if (fitsout == -99) /* we still have to convert a table to FITS */
694 return fitsout;
695 else
696 return status;
697 }
698 /*
699
700 */
701
702 int SCFMAP(imno,iomode,felem,size,actsize,pntr)
703
704 /*++++++++++++++++++++++++++++++++++++++++++++++++++
705 .PURPOSE
706 map a new data frame into memory for writing/reading
707 or just get virtual memory (iomode = 9)
708
709 .ALGORITHM
710 return pointer to first element which is required
711
712 .RETURNS
713 return status ( 0 = o.k )
714
715 .REMARKS
716 parameter 'iomode' may be:
717 0 for input (read only),
718 1 for output (write),
719 2 for in/output (read + write),
720 9 for scratch file = get virtual memory
721
722 --------------------------------------------------------*/
723
724 int imno; /* IN : file no. of data frame */
725 int iomode; /* IN: opening mode, e.g. F_O_MODE, ... */
726 int felem; /* IN : 1st pixel to be accessed in data space */
727 int size; /* IN : number of data values (pixels) to be mapped */
728 int *actsize; /* OUT: actual no. of pixels mapped */
729 char **pntr; /* OUT: pointer to data in memory */
730
731 {
732 char *mypntr, *errpntr;
733
734 int nbytes, nopix;
735 int i, mm, status;
736
737 unsigned int allbytes;
738
739
740
741
742
743 mypntr = (char *) 0;
744 if ( (imno < 0) || (imno >= FCT.MAXENT) ) /* check 'imno' + 'felem' */
745 {
746 status = ERR_INPINV;
747 goto sect_2000;
748 }
749
750 fctpntr = FCT.ENTRIES + imno;
751
752 if ( (fctpntr->NAME[0] == ' ') || (felem <= 0) )
753 {
754 status = ERR_INPINV;
755 goto sect_2000;
756 }
757
758
759 status = ERR_NORMAL;
760 if (size < 1) /* if no size given, map all pixels */
761 nopix = fctpntr->SIZE; /* no. of pixels required by user */
762 else
763 nopix = size;
764
765 *actsize = nopix;
766
767
768 if (fctpntr->BDADDR[0] != (char *) 0)
769 {
770 if (fctpntr->FILTYP > 0) /* its a FITS file */
771 {
772 nbytes = get_byte(fctpntr->DATTYP); /* data format we want */
773 *actsize = size; /* we mapped everything */
774 felem --; /* at the first call...*/
775 mm = felem * nbytes;
776 *pntr = fctpntr->PNTR + mm;
777 return (ERR_NORMAL);
778 }
779
780 if ((felem == fctpntr->KAUX[0]) &&
781 (size == fctpntr->KAUX[1]))
782 {
783 *actsize = size;
784 *pntr = fctpntr->PNTR;
785 return (ERR_NORMAL); /* repeated call */
786 }
787
788 if (fctpntr->ACCESS == 'O') /* write out previous data */
789 {
790 if (fctpntr->DATTYP == fctpntr->FORMAT)
791 status = wrdisk(imno,fctpntr->KAUX[0],fctpntr->KAUX[1],
792 fctpntr->BDADDR[0]);
793 else /* mismatched frame + writing data */
794 status = conv_dat(2,imno,fctpntr->KAUX[0],fctpntr->KAUX[1],
795 &mm,fctpntr->BDADDR);
796 }
797 (void) free(fctpntr->BDADDR[0]); /* and release previous memory */
798 fctpntr->BDADDR[0] = (char *) 0;
799 fctpntr->PNTR = (char *) 0;
800
801 if (status != ERR_NORMAL) return status;
802 }
803
804
805
806
807 /***************************************************
808 iomode = F_X_MODE, just allocate virtual memory
809 ****************************************************/
810
811
812 if (iomode == F_X_MODE)
813 {
814 nbytes = fctpntr->NOBYTE; /* no. of bytes per pixel */
815 allbytes = (unsigned int) (nopix * nbytes);
816 mypntr = malloc((size_t)allbytes); /* ANSI C */
817
818 if (mypntr == (char *) 0)
819 {
820 status = ERR_MEMOUT;
821 goto sect_2000;
822 }
823 fctpntr->BDADDR[0] = mypntr;
824 fctpntr->ACCESS = 'X';
825 }
826
827
828 /************************************************************
829 normal mode - allocate memory + read data in
830 *************************************************************/
831
832
833 else
834 {
835 if (fctpntr->FILTYP > 0) /* its a FITS file */
836 {
837 char dum[2];
838 int nono;
839
840
841 if (nopix < fctpntr-> SIZE)
842 nono = 1; /* we asked for sub_area */
843 else
844 nono = 0; /* we asked for everything */
845
846
847 /* always map everything */
848
849 nbytes = get_byte(fctpntr->DATTYP); /* data format we want */
850 allbytes = (unsigned int) (fctpntr-> SIZE * nbytes);
851 mypntr = malloc((size_t)allbytes);
852 if (mypntr == (char *) 0)
853 {
854 status = ERR_MEMOUT;
855 goto sect_2000;
856 }
857
858 if (fctpntr->FILTYP != 22) /* if it was not mapped as */
859 { /* a table yet... (dummyPUT) */
860 char origname[FCT_NAME_LEN];
861
862 dum[0] = '\0';
863 if (fctpntr->O_NAMLEN == 0)
864 (void) strcpy(origname,fctpntr->NAME);
865 else /* internal FITS file */
866 {
867 (void) strcpy(origname,fctpntr->NAME+4);
868 mm = fctpntr->O_NAMLEN - 4; /* we just removed leading "FITZ" */
869 origname[mm] = '\0';
870 }
871 mm = imno; /* get data as real */
872 status = MID_fitsin(2,origname,-1,dum,mypntr,&mm,&i);
873 /*
874 printf("SCFMAP: called fitsin(%s,..) with mm = %d\n",origname,mm);
875 MID_SHOWFCT2(mm);
876 */
877 }
878
879
880 fctpntr->BDADDR[0] = mypntr;
881 if (iomode != F_I_MODE)
882 fctpntr->ACCESS = 'O';
883 else
884 fctpntr->ACCESS = 'I';
885
886 fctpntr->KAUX[0] = 1;;
887 fctpntr->KAUX[1] = fctpntr->SIZE;
888 fctpntr->PNTR = mypntr;
889
890 if (nono == 1) /* we have to update the pointer */
891 {
892 felem --;
893 mypntr = fctpntr->BDADDR[0] + (felem * nbytes);
894 }
895 *pntr = mypntr;
896 return status;
897 }
898
899 else
900 {
901 if (fctpntr->DATTYP != fctpntr->FORMAT)
902 { /* cannot be F_O_MODE ... */
903 status = conv_dat(1,imno,felem,nopix,&mm,fctpntr->BDADDR);
904 mypntr = fctpntr->BDADDR[0]; /* was set in conv_dat */
905 }
906 else
907 {
908 nbytes = fctpntr->NOBYTE; /* no. of bytes per pixel */
909 allbytes = (unsigned int) (nopix * nbytes);
910 mypntr = malloc((size_t)allbytes);
911
912 if (mypntr == (char *) 0)
913 {
914 status = ERR_MEMOUT;
915 goto sect_2000;
916 }
917 fctpntr->BDADDR[0] = mypntr;
918
919 if (iomode != F_O_MODE)
920 status = rddisk(imno,felem,nopix,&mm,mypntr);
921 }
922
923 if (status != ERR_NORMAL) goto sect_2000;
924
925 if (iomode != F_I_MODE)
926 {
927 fctpntr->ACCESS = 'O';
928 if (fctpntr->PROT == 2)
929 {
930 status = ERR_FILPRO;
931 goto sect_2000;
932 }
933 }
934 }
935 }
936
937 fctpntr->KAUX[0] = felem;
938 fctpntr->KAUX[1] = nopix;
939 fctpntr->PNTR = mypntr;
940 *pntr = mypntr;
941
942 return status;
943
944
945 /* here if problems... */
946
947 sect_2000:
948 errpntr = (char *) malloc((size_t) 44);
949 if (status == ERR_MEMOUT)
950 {
951 nbytes = get_byte(fctpntr->DATTYP); /* data format we want */
952 allbytes = (unsigned int) (nopix * nbytes);
953 (void) snprintf(errpntr,(size_t)44,"file size = %d bytes",(int)allbytes);
954 }
955 else
956 {
957 (void) snprintf(errpntr,(size_t)44,"with iomode = %d",iomode);
958 free(mypntr);
959 }
960 MID_E2(2,imno,errpntr,status,1);
961 free(errpntr);
962 return (status);
963 }
964 /*
965
966 */
967
968 int SCFUNM(imno)
969
970 /*++++++++++++++++++++++++++++++++++++++++++++++++++
971 .PURPOSE
972 release mapped memory of a data frame
973 .ALGORITHM
974 .RETURNS
975 integer return status ( 0 = o.k )
976 --------------------------------------------------------*/
977
978 int imno /* IN: file of data frame */;
979
980 {
981 int mm, status;
982
983
984 if ( (imno < 0) || (imno >= FCT.MAXENT) )
985 status = ERR_INPINV;
986
987 else
988 {
989 fctpntr = FCT.ENTRIES + imno;
990 if (fctpntr->NAME[0] == ' ')
991 status = ERR_INPINV;
992
993
994 /* input o.k. - release allocated memory + update frame if necessary */
995
996 else
997 {
998 status = ERR_NORMAL;
999 if (fctpntr->BDADDR[0] != (char *) 0)
1000 {
1001 if (fctpntr->ACCESS == 'O')
1002 {
1003 if (fctpntr->DATTYP == fctpntr->FORMAT)
1004 status = wrdisk(imno,fctpntr->KAUX[0],fctpntr->KAUX[1],
1005 fctpntr->BDADDR[0]);
1006 else /* mismatched frame + writing data */
1007 status = conv_dat(2,imno,fctpntr->KAUX[0],fctpntr->KAUX[1],
1008 &mm,fctpntr->BDADDR);
1009 }
1010 (void) free(fctpntr->BDADDR[0]);
1011 fctpntr->BDADDR[0] = (char *) 0;
1012 fctpntr->PNTR = (char *) 0;
1013 }
1014 }
1015 }
1016
1017 if (status != ERR_NORMAL)
1018 MID_E2(3,imno,"...",status,1);
1019
1020 return (status);
1021 }
1022
1023