/* @(#)midfcta.c 17.1.1.1 (ESO-DMD) 01/25/02 17:36:22 */ /*=========================================================================== Copyright (C) 1995 European Southern Observatory (ESO) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. Correspondence concerning ESO-MIDAS should be addressed as follows: Internet e-mail: midas@eso.org Postal address: European Southern Observatory Data Management Division Karl-Schwarzschild-Strasse 2 D 85748 Garching bei Muenchen GERMANY ===========================================================================*/ /*+++++++++++++++++++++ Module MIDFCTA +++++++++++++++++++++++++++++++++++++++ .LANGUAGE C .IDENTIFICATION Module MIDFCTA.C .AUTHOR Klaus Banse .KEYWORDS Midas utility routines. .ENVIRONMENT VMS and UNIX .COMMENTS holds MID_INITFR, MID_FCTIN .VERSION [1.00] 920312: pulled over from midfct.c 011130 last modif ------------------------------------------------------------------------*/ #include #include #include #include #include #define BIT_0 0x1 #define DISK_REC 512 #define FILE_MASK 0 /* CG. 920529: Set to 0. osfcreate() makes default */ /* */ int MID_INITFR(name,dattype,filtype,sizze,entrx) /*++++++++++++++++++++++++++++++++++++++++++++++++++ .KEYWORDS frame control table, initialisation .PURPOSE create + initialize a new frame .ALGORITHM create a new bulk data frame set up the 1. virtual block as frame control block, FCB reserve the next 10 blocks as LDB's for the standard descriptors data starts from virtual block 12 on .RETURNS status: I*4 return status -----------------------------------------------------*/ char *name; /* IN : translated frame name with type */ int dattype; /* IN : type of data, I*1, I*2, I*4, R*4 */ int filtype; /* IN : type of frame */ int sizze; /* IN : no. of pixels in frame if < 0, we only want to create the file header */ int *entrx; /* OUT: FCT entry number (= index) */ { int n, mm, status; int kldb[2], nblock, alq, ext_size, data_bytes, iochan, nvb; int savftype, dscdsiz, myentry; long int longtime, oshtime(); register int nr; struct FCB_STRUCT *fcbp; struct FCT_STRUCT *fctpntr; static int swapshort = SWAPSHORT; static int swapint = SWAPINT; static int floatfmt = FLOATFMT; static char Midvers[] = "VERS_100"; /* as of 010301 */ static char oMidvers[] = "VERS_010"; /* with old descr. format */ static char ooMidvers[] = "VERS_008"; /* with old, old d.f. */ /* calculate no. of blocks needed for data */ data_bytes = get_byte(dattype); if (sizze < 1) /* that means F_H_MODE ... */ { savftype = filtype; filtype = -99; sizze = 0; nblock = 0; /* no data yet */ } else { savftype = -1; mm = DISK_REC - 1; if (filtype < 11) nblock = (data_bytes * sizze) + mm; else /* intl FITS files */ nblock = data_bytes + mm; nblock /= DISK_REC; } /* calculate size of descr. directory and no. of LDBs needed for it */ if (DSC_FLAG == 'Y') { mm = (FCT.PARM[0] + 1) * YENTRY_SIZE; /* total size: with dscdir */ ext_size = 2500; } else { mm = (FCT.PARM[0] + 1) * (2*ENTRY_SIZE); /* total size: with dscdir */ ext_size = 1500; } nvb = ((mm - 1)/ext_size) + 1; /* no. of extensions needed */ dscdsiz = (nvb * ext_size) + 12; /* size of descr_dir and its atom header (which is = 3 ints) */ kldb[0] = ((dscdsiz - 1)/LDB_NDSCRB) + 1; /* no. of LDBs needed for dscdir */ /* calculate initial no. of LDBs needed for descr. data */ n = (FCT.PARM[0]*12) + FCT.PARM[1]; /* 12 bytes atom header per descr */ kldb[1] = ((n - 1)/LDB_NDSCRB) + 1; /* get no. of LDBs */ nvb = kldb[0] + kldb[1]; alq = 1 + (4*nvb) + nblock; /* 1 FCB + nvb*LDB's + data */ /* 1 LDB = 4 single blocks */ /* check, that no such file is already in FCT + create frame */ n = MID_FINDFR(name); if (n >= 0) return (-99); /* tell SCFCRE that file should be closed */ /* printf("INITDS: dscdsiz = %d, ext_size = %d, nvb = %d, alq = %d\n",dscdsiz,ext_size,nvb,alq); */ /* create file on disk */ myentry = *entrx; if (filtype > 10) /* is it a memory file? */ { status = MID_VMEM(1,alq,&iochan); /* allocate memory for file */ if (status != ERR_NORMAL) goto end_of_it; /* do what MID_ACCFRM would do */ myentry = MID_CREFRE(name,0); /* create one more entry */ if (myentry < 0) { status = ERR_FILNAM; goto end_of_it; } fctpntr = FCT.ENTRIES + myentry; fctpntr->COMPRESS = 0; fctpntr->IOCHAN = iochan; #if vms fctpntr->FILEIDA = iochan; fctpntr->FILEIDB = iochan; #else fctpntr->FILEID = iochan; #endif fctpntr->CATALOG[1] = 'N'; fcbp = fctpntr->FZP; CGN_FILL((char * )fcbp,' ',512); /* init FCB to ' ' */ nvb = alq; goto type_switch; } else { #if vms FSY_CREBDF(name,(int)strlen(name),alq,&nvb,&status); /* close it again */ if ( !(status & BIT_0) ) { MID_ERROR("FSY","MID_INITFR: ",status,0); status = ERR_FRMNAC; goto end_of_it; } #else mm = osfcreate(name,(alq*BLOCK_SIZE),FILE_MASK); if (mm == -1) { MID_ERROR("FSY","MID_INITFR: ",mm,0); /* mm just a dummy here */ status = ERR_FRMNAC; goto end_of_it; } nvb = mm / BLOCK_SIZE; #endif } /* use MID_ACCFRM to open file again + get FCB in */ status = MID_ACCFRM(name,2,&myentry,&n); if (status != ERR_NORMAL) goto end_of_it; fctpntr = FCT.ENTRIES + myentry; iochan = fctpntr->IOCHAN; fcbp = fctpntr->FZP; CGN_FILL((char * )fcbp,' ',512); /* init FCB to ' ' */ /* fill FCB.BDTYPE with type of frame */ type_switch: fcbp->FITSINFO[0] = 0; fcbp->FITSINFO[1] = 0; fctpntr->ACCESS = 'O'; /* set to write access */ switch(filtype) { case F_IMA_TYPE: (void)strcpy(fcbp->BDTYPE,"IMAGE "); fcbp->NDVAL = sizze; break; case F_TBL_TYPE: (void)strcpy(fcbp->BDTYPE,"TABLE "); fcbp->NDVAL = sizze; break; case F_FIT_TYPE: (void)strcpy(fcbp->BDTYPE,"FITFILE"); fcbp->NDVAL = sizze; break; case F_FIMA_TYPE: /* FITS files */ (void)strcpy(fcbp->BDTYPE,"IMAGE-F"); fcbp->NDVAL = 1; fcbp->FITSINFO[0] = sizze; fctpntr->ACCESS = 'I'; /* not really a new file */ break; case F_FTBL_TYPE: (void)strcpy(fcbp->BDTYPE,"TABLE-F"); fcbp->NDVAL = 1; fcbp->FITSINFO[0] = sizze; fctpntr->ACCESS = 'I'; /* not really a new file */ break; case F_FFIT_TYPE: (void)strcpy(fcbp->BDTYPE,"FITFI-F"); fcbp->NDVAL = 1; fcbp->FITSINFO[0] = sizze; fctpntr->ACCESS = 'I'; /* not really a new file */ break; case -99: if (savftype == F_IMA_TYPE) (void)strcpy(fcbp->BDTYPE,"IMAGE "); else (void)strcpy(fcbp->BDTYPE,"TABLE "); fcbp->NDVAL = 0; break; default: status = ERR_INPINV; goto end_of_it; } /* fill frame control block */ for (nr=22; nr<28; nr++) fcbp->CREATE[nr] = ' '; OSY_ASCTIM(fcbp->CREATE); /* get current time (26 chars.) */ longtime = oshtime(); (void)memcpy(fcbp->CRETIM,&longtime,sizeof(long)); /* get time also as secs */ fcbp->OLDVERS = 0.0; if (DSC_FLAG == 'Y') { fcbp->DSCFLAG = 'Y'; /* indicate new descr. stuff */ (void)strncpy(fcbp->VERSION,Midvers,8); } else if (DSC_FLAG == 'Z') { fcbp->DSCFLAG = 'Z'; /* indicate old descr. stuff */ (void)strncpy(fcbp->VERSION,oMidvers,8); } else { fcbp->DSCFLAG = 'N'; /* indicate very old descr. stuff */ (void)strncpy(fcbp->VERSION,ooMidvers,8); } fcbp->PROT = 0; fcbp->INCARN = 1; fcbp->INLDB[0] = kldb[0]; fcbp->INLDB[1] = kldb[1]; fcbp->LEXJMP = ' '; fcbp->DIRUSE = 0; /* just to init the addr. */ fcbp->DIRNOELEM = 0; /* just to init the addr. */ fcbp->DBEGIN = dscdsiz; /* inital size of descr. directory */ fcbp->MAINSEG[0] = data_bytes; fcbp->MAINSEG[1] = dattype; /* no. of data values (pixels) per page/block */ fcbp->MAINSEG[2] = DISK_REC / data_bytes; fcbp->MAINSEG[4] = alq; if (sizze == 0) /* no data (yet) */ fcbp->MAINSEG[3] = -1; /* no start block of data (yet) */ else fcbp->MAINSEG[3] = 1 + (4*(fcbp->INLDB[0]+fcbp->INLDB[1])) + 1; /* FCB + 4*LDBs + 1 */ fcbp->PTRLDB = 2; /* point to first LDB (= VBN 2) */ fcbp->LEXBDF = alq; /* logical file extension */ fcbp->PEXBDF = nvb; /* physical file extension (cluster factor of disk) */ fcbp->NEXT = 0; /* we only have 1 FCB */ /* fill updated values into FCT */ fctpntr->SIZE = sizze; fctpntr->NOBYTE = fcbp->MAINSEG[0]; fctpntr->FORMAT = fctpntr->DATTYP = fcbp->MAINSEG[1]; fctpntr->PIXPBL = fcbp->MAINSEG[2]; fctpntr->STBLOK = fcbp->MAINSEG[3]; fctpntr->CATALOG[0] = fcbp->BDTYPE[0]; status = ERR_INSBAD; if (swapshort == 12) fcbp->SWPSHORT = '='; else if (swapshort == 21) fcbp->SWPSHORT = 's'; else goto end_of_it; if (swapint == 1234) fcbp->SWPINT = '='; else if (swapint == 4321) fcbp->SWPINT = 's'; else if (swapint == 2143) fcbp->SWPINT = 'h'; else if (swapint == 3412) fcbp->SWPINT = 'w'; else goto end_of_it; if (floatfmt == IEEEFLOAT) fcbp->FLOTFMT = '='; else if (floatfmt == VAXFLOAT) fcbp->FLOTFMT = 'V'; else if (floatfmt == VAXGFLOAT) fcbp->FLOTFMT = 'G'; else if (floatfmt == HPFLOAT) fcbp->FLOTFMT = 'H'; else goto end_of_it; *entrx = myentry; /* set the 6 basic info values for easy reading of data files */ fcbp->DATAINFO[0] = 0; /* has to be updated later */ fcbp->DATAINFO[1] = 0; fcbp->DATAINFO[2] = 0; fcbp->DATAINFO[3] = 0; fcbp->DATAINFO[4] = fcbp->MAINSEG[1]; fcbp->DATAINFO[5] = (fcbp->MAINSEG[3]-1)*DISK_REC; /* fill up reserved areas with 0's + set up descr. directory */ for (nr=0; nr<12; nr++) { /* only clear first 12 elements */ fcbp->RESERV0[nr] = fcbp->RESERV1[nr] = ' '; fcbp->RESERV2[nr] = fcbp->RESERV3[nr] = ' '; } status = MID_INITDS(fcbp,iochan); if (status == ERR_NORMAL) status = OSY_WVB(fctpntr->IOCHAN,(char *)fcbp,BLOCK_SIZE,1); if (status == ERR_NORMAL) return status; /* prepare error messages for higher level routines */ end_of_it: MID_ERROR("MIDAS","MID_INITFR:",status,0); return status; } /* */ int MID_FCTIN(fctnos) /*++++++++++++++++++++++++++++++++++++++++++++++++++ .KEYWORDS frame control table .PURPOSE create the FCT structures for `fctsize' entries .ALGORITHM use malloc + free .RETURNS --------------------------------------------------*/ int fctnos; /* IN : no. of entries for extension \ if -1, create new structure */ { int mm, fctsize; unsigned int allbytes; register int nr, mr; char *cptr; static char *oldpntr; struct FCT_STRUCT *fctpntr, *fctpntro; /* do not forget to synchronize with $MID_INCLUDE/fctext.h !!! */ if (fctnos == -1) { fctnos = FCT_MAXENT; /* use initial value */ fctsize = (sizeof(struct FCT_STRUCT) * fctnos) + 4; allbytes = fctsize; cptr = malloc(allbytes); if (cptr == NULL) { (void)printf("could not allocate %d bytes for FCT table\n\r",fctsize); ospexit(0); } FCT.ENTRIES = (struct FCT_STRUCT *) cptr; fctpntr = FCT.ENTRIES; /* clear FCT entries */ for (nr=0; nrNAME[0] = ' '; fctpntr->NAME[1] = '\0'; fctpntr ++; } } else { mm = FCT.MAXENT; /* save old stuff for the moment */ fctpntro = FCT.ENTRIES; fctsize = (sizeof(struct FCT_STRUCT) * fctnos) + 4; allbytes = fctsize; cptr = malloc(allbytes); if (cptr == NULL) { (void)printf("could not allocate %d bytes for FCT table\n\r",fctsize); ospexit(0); } FCT.ENTRIES = (struct FCT_STRUCT *) cptr; fctpntr = FCT.ENTRIES; /* clear FCT entries */ for (nr=0; nrNAME[0] = ' '; fctpntr ++; } /* now copy old entries over */ fctpntr = FCT.ENTRIES; /* reset pointer to begin */ for (nr=0; nrNAME[0] != ' ') { fctpntr->NAMLEN = fctpntro->NAMLEN; fctpntr->IOCHAN = fctpntro->IOCHAN; #if vms fctpntr->FILEIDA = fctpntro->FILEIDA; fctpntr->FILEIDB = fctpntro->FILEIDB; #else fctpntr->FILEID = fctpntro->FILEID; #endif for (mr=0; mr<4; mr++) fctpntr->KAUX[mr] = fctpntro->KAUX[mr]; fctpntr->SIZE = fctpntro->SIZE; fctpntr->PROT = fctpntro->PROT; fctpntr->COMPRESS = fctpntro->COMPRESS; fctpntr->NOBYTE = fctpntro->NOBYTE; fctpntr->FORMAT = fctpntro->FORMAT; fctpntr->PIXPBL = fctpntro->PIXPBL; fctpntr->STBLOK = fctpntro->STBLOK; fctpntr->DATTYP = fctpntro->DATTYP; fctpntr->FILTYP = fctpntro->FILTYP; fctpntr->FITSEXT = fctpntro->FITSEXT; fctpntr->LINK[0] = fctpntro->LINK[0]; fctpntr->LINK[1] = fctpntro->LINK[1]; (void)strcpy(fctpntr->NAME,fctpntro->NAME); (void)strcpy(fctpntr->PATH,fctpntro->PATH); #if vms (void)strcpy(fctpntr->DEVICE,fctpntro->DEVICE); #endif for (mr=0; mr<4; mr++) fctpntr->BDADDR[mr] = fctpntro->BDADDR[mr]; fctpntr->PNTR = fctpntro->PNTR; for (mr=0; mr<3; mr++) fctpntr->FITSADDR[mr] = fctpntro->FITSADDR[mr]; fctpntr->CATALOG[0] = fctpntro->CATALOG[0]; fctpntr->CATALOG[1] = fctpntro->CATALOG[1]; fctpntr->ACCESS = fctpntro->ACCESS; fctpntr->FZP = fctpntro->FZP; } fctpntr ++; fctpntro ++; } free(oldpntr); } FCT.MAXENT = fctnos; oldpntr = cptr; /* save old char. pointer */ return 0; }