void rdhda_c(int thandle,Const char *keyword,char *value,Const char *defval,int len) /** rdhda -- Read a string-valued header variable. */ /*& mjs */ /*: header-i/o */ /*+ FORTRAN call sequence: subroutine rdhda(tno,keyword,value,default) integer tno character keyword*(*) character value*(*),default*(*) Read a string valued header variable. Input: tno The file handle of the data set. keyword The name of the header variable. default The default value to return, if the header variable is not found. Output: value The value of the header variable. This will be the default value, if the variable is missing from the header. */ /*-- */ /*----------------------------------------------------------------------*/ { int item; char s[ITEM_HDR_SIZE]; int iostat,dodef,length=0; /* Firstly assume the variable is missing. Try to get it. If successful read it. */ dodef = TRUE; haccess_c(thandle,&item,keyword,"read",&iostat); if(! iostat) { length = min( hsize_c(item) - ITEM_HDR_SIZE, len - 1); if(length > 0) { hreadb_c(item,s,0,ITEM_HDR_SIZE,&iostat); check(iostat); if(!memcmp(s,char_item,ITEM_HDR_SIZE)){ hreadb_c(item,value,ITEM_HDR_SIZE,length,&iostat); check(iostat); dodef = FALSE; } } hdaccess_c(item,&iostat); check(iostat); } if( dodef ) { length = min(strlen(defval),len-1); memcpy(value,defval,length); } *(value+length) = 0; }
void scrread_c(int handle,float *buffer,int offset,int length) /**scrread -- Read real data from a scratch file. */ /*:scratch-i/o */ /*+ FORTRAN call sequence: subroutine scrread(tno,buf,offset,length) integer tno,offset,length real buf(length) This reads real data from the scratch file. Input: tno The handle of the scratch file. offset The offset (measured in reals) into the scratch file to read. The first real has offset 0. length The number of reals to read. Output: buf The returned data. */ /*-- */ /*----------------------------------------------------------------------*/ { int iostat; hreadb_c(handle,(char *)buffer, (off64_t)sizeof(float)*offset,sizeof(float)*length,&iostat); if(iostat){ bug_c( 'w',"Error reading from scratch file"); bugno_c('f',iostat); } }
void rdhdl_c(int thandle,Const char *keyword,int8 *value,int8 defval) /** rdhdl -- Read an integer*8-valued header variable. */ /*& mjs */ /*: header-i/o */ /*+ FORTRAN call sequence: subroutine rdhdl(tno,keyword,value,default) integer tno character keyword*(*) integer*8 value,default Read an integer*8 valued header variable. Only supported on some compilers. See comments in wrhdl Input: tno The file handle of the data set. keyword The name of the header variable. default The default value to return, if the header variable is not found. Output: value The value of the header variable. This will be the default value, if the variable is missing from the header. */ /*-- */ /*----------------------------------------------------------------------*/ { int item; char s[ITEM_HDR_SIZE]; int iostat,length,offset,itemp; /* Firstly assume the variable is missing. Try to get it. If successful read it. */ *value = defval; haccess_c(thandle,&item,keyword,"read",&iostat); if(iostat)return; length = hsize_c(item); if(length >= 0){ /* Determine the type of the value, and convert it to double precision. */ hreadb_c(item,s,0,ITEM_HDR_SIZE,&iostat); check(iostat); iostat = 0; if( !memcmp(s,int8_item, ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE, H_INT8_SIZE); if(offset + H_INT8_SIZE == length) hreadl_c(item,value,offset,H_INT8_SIZE,&iostat); } else if ( !memcmp(s,int_item, ITEM_HDR_SIZE)){ /* this is to cover old style MIR3 files that were using int4's */ offset = mroundup(ITEM_HDR_SIZE, H_INT_SIZE); if(offset + H_INT_SIZE == length) { hreadi_c(item,&itemp,offset,H_INT_SIZE,&iostat); *value = itemp; } } else bugv_c('f',"rdhdl_c: item %s not an int8 or small enough int4",keyword); check(iostat); } hdaccess_c(item,&iostat); check(iostat); }
/* hread supports reading of header items of all types using the various * hread_c calls. Reads one item per call. */ PyObject * WRAP_hread(PyObject *self, PyObject *args) { int item_hdl, offset, iostat; char *type; PyObject *val, *rv; int in; short sh; long lg; float fl; double db; float cx[2]; char st[2]; if (!PyArg_ParseTuple(args, "iis", &item_hdl, &offset, &type)) return NULL; try { switch (type[0]) { case 'a': hreadb_c(item_hdl, st, offset, H_BYTE_SIZE, &iostat); CHK_IO(iostat); return Py_BuildValue("si", st, H_BYTE_SIZE); case 'i': hreadi_c(item_hdl, &in, offset, H_INT_SIZE, &iostat); CHK_IO(iostat); return Py_BuildValue("ii", in, H_INT_SIZE); case 'j': hreadj_c(item_hdl, &sh, offset, H_INT2_SIZE, &iostat); CHK_IO(iostat); return Py_BuildValue("ii", sh, H_INT2_SIZE); case 'l': hreadl_c(item_hdl, &lg, offset, H_INT8_SIZE, &iostat); CHK_IO(iostat); return Py_BuildValue("li", lg, H_INT8_SIZE); case 'r': hreadr_c(item_hdl, &fl, offset, H_REAL_SIZE, &iostat); CHK_IO(iostat); return Py_BuildValue("fi", fl, H_REAL_SIZE); case 'd': hreadd_c(item_hdl, &db, offset, H_DBLE_SIZE, &iostat); CHK_IO(iostat); return Py_BuildValue("fi", db, H_DBLE_SIZE); case 'c': hreadc_c(item_hdl, cx, offset, H_CMPLX_SIZE, &iostat); CHK_IO(iostat); val = PyComplex_FromDoubles((double) cx[0], (double) cx[1]); rv = Py_BuildValue("Oi", val, H_CMPLX_SIZE); Py_DECREF(val); return rv; default: PyErr_Format(PyExc_ValueError, "unknown item type: %c",type[0]); return NULL; } } catch (MiriadError &e) { PyErr_Format(PyExc_RuntimeError, e.get_message()); return NULL; } }
void rdhdc_c(int thandle,Const char *keyword,float *value,Const float *defval) /** rdhdc -- Read a complex-valued header variable. */ /*& mjs */ /*: header-i/o */ /*+ FORTRAN call sequence: subroutine rdhdc(tno,keyword,value,default) integer tno character keyword*(*) complex value,default Read a complex valued header variable. Input: tno The file handle of the data set. keyword The name of the header variable. default The default value to return, if the header variable is not found. Output: value The value of the header variable. This will be the default value, if the variable is missing from the header. */ /*-- */ /*----------------------------------------------------------------------*/ { int item; char s[ITEM_HDR_SIZE]; int iostat,length,offset; /* Firstly assume the variable is missing. Try to get it. If successful read it. */ *value = *defval; *(value+1) = *(defval+1); haccess_c(thandle,&item,keyword,"read",&iostat); if(iostat)return; offset = mroundup(ITEM_HDR_SIZE,H_CMPLX_SIZE); length = hsize_c(item) - offset; if(length == H_CMPLX_SIZE){ hreadb_c(item,s,0,ITEM_HDR_SIZE,&iostat); check(iostat); iostat = 0; if(!memcmp(s,cmplx_item, ITEM_HDR_SIZE)){ hreadc_c(item,value,offset,H_CMPLX_SIZE,&iostat); } check(iostat); } hdaccess_c(item,&iostat); check(iostat); }
/* hread_init surmises the type of a header item from the first few bytes */ PyObject * WRAP_hread_init(PyObject *self, PyObject *args) { int item_hdl, offset, iostat, code; char s[ITEM_HDR_SIZE]; if (!PyArg_ParseTuple(args, "i", &item_hdl)) return NULL; try { hreadb_c(item_hdl,s,0,ITEM_HDR_SIZE,&iostat); CHK_IO(iostat); code = FIRSTINT(s); if (code == FIRSTINT(char_item)) { offset = mroundup(ITEM_HDR_SIZE,H_BYTE_SIZE); return Py_BuildValue("si", "a", offset); } else if (code == FIRSTINT(binary_item)) { return Py_BuildValue("si", "b", ITEM_HDR_SIZE); } else if (code == FIRSTINT(int_item)) { offset = mroundup(ITEM_HDR_SIZE,H_INT_SIZE); return Py_BuildValue("si", "i", offset); } else if (code == FIRSTINT(int2_item)) { offset = mroundup(ITEM_HDR_SIZE,H_INT2_SIZE); return Py_BuildValue("si", "j", offset); } else if (code == FIRSTINT(int8_item)) { offset = mroundup(ITEM_HDR_SIZE,H_INT8_SIZE); return Py_BuildValue("si", "l", offset); } else if (code == FIRSTINT(real_item)) { offset = mroundup(ITEM_HDR_SIZE,H_REAL_SIZE); return Py_BuildValue("si", "r", offset); } else if (code == FIRSTINT(dble_item)) { offset = mroundup(ITEM_HDR_SIZE,H_DBLE_SIZE); return Py_BuildValue("si", "d", offset); } else if (code == FIRSTINT(cmplx_item)) { offset = mroundup(ITEM_HDR_SIZE,H_CMPLX_SIZE); return Py_BuildValue("si", "c", offset); } PyErr_Format(PyExc_RuntimeError, "unknown item type"); return NULL; } catch (MiriadError &e) { PyErr_Format(PyExc_RuntimeError, e.get_message()); return NULL; } }
void hdcopy_c(int tin,int tout,Const char *keyword) /** hdcopy -- Copy a headfer variable from one data set to another. */ /*& mjs */ /*: header-i/o */ /*+ FORTRAN call sequence: subroutine hdcopy(tin,tout,keyword) integer tin,tout character keyword*(*) Copy a header item from one data set to another. Input: tin File handle of the input data set. tout File handle of the output data set. keyword Name of the header variable to be copied. */ /*-- */ /*----------------------------------------------------------------------*/ { char buf[MAXSIZE]; int item_in,item_out; int length,offset,iostat,size; haccess_c(tin,&item_in,keyword,"read",&iostat); if(iostat)return; haccess_c(tout,&item_out,keyword,"write",&iostat); check(iostat); size = hsize_c(item_in); offset = 0; while(offset < size){ length = min(size - offset, sizeof(buf)); hreadb_c(item_in,buf,offset,length,&iostat); check(iostat); hwriteb_c(item_out,buf,offset,length,&iostat); check(iostat); offset += length; } hdaccess_c(item_in,&iostat); check(iostat); hdaccess_c(item_out,&iostat); check(iostat); }
void xyopen_c(int *thandle,Const char *name,Const char *status,int naxis,int *axes) /**xyopen -- Open an image file. */ /*:image-i/o */ /*+ FORTRAN call sequence: subroutine xyopen(tno,name,status,naxis,axes) integer tno,naxis,axes(naxis) character name*(*),status*(*) This opens an image file. For an old file, determine the size of each axe. For a new file, it writes out this info. Input: name The name of the file to be opened. status Either 'old', 'new' or 'append'. naxis The maximum number of axes that the calling program can handle. For an 'old' file, if the data file has fewer than naxis axes, the higher dimensions are treated as having only one element. If the data file has more than naxis axes, and the higher dimensions are more than 1 element deep, xyopen bombs out. Input or Output: axes This is input for status='new' and output for status='old'. It gives the number of elements along each axis. Output: tno The handle of the output file. */ /*----------------------------------------------------------------------*/ { int iostat,length,access,tno,i,ndim,npix,temp; char *stat,*mode,naxes[16],s[ITEM_HDR_SIZE]; if(!strcmp("old",status)) { access = OLD; mode = "read"; stat = "old";} else if(!strcmp("append",status)){ access = OLD; mode = "append";stat = "old";} else if(!strcmp("new",status)) { access = NEW; mode = "write"; stat = "new";} else ERROR('f',(message,"Unrecognised status when opening %s, in XYOPEN",name)); /* Access the image data. */ hopen_c(&tno,name,stat,&iostat); CHECK(iostat,(message,"Error opening %s, in XYOPEN",name)); haccess_c(tno,&(images[tno].image),"image",mode,&iostat); CHECK(iostat,(message,"Error accessing pixel data of %s, in XYOPEN",name)); /*----------------------------------------------------------------------*/ /* */ /* Handle an old image. Get number of axes, and then the length */ /* of each axis. Also compute and check that the size of the */ /* image file looks OK. */ /* */ /*----------------------------------------------------------------------*/ if(access == OLD){ rdhdi_c(tno,"naxis",&ndim,0); if(ndim <= 0 || ndim > MAXNAX) ERROR('f',(message,"Bad number of dimensions for %s in XYOPEN",name)); Strcpy(naxes,"naxis0"); length = strlen(naxes) - 1; npix = 1; for(i=0; i<max(ndim,naxis); i++){ naxes[length] ++; if(i < ndim) rdhdi_c(tno,naxes,&temp,0); else temp = 1; if(temp <= 0) ERROR('f',(message,"Bad image dimension for %s, in XYOPEN",name)); npix = npix * temp; if(i < naxis) axes[i] = temp; else if(temp > 1) ERROR('f',(message,"Too many dimensions for %s, in XYOPEN",name)); } /* Check the file size if OK and that it starts with the "real_item" sequence. */ if(hsize_c(images[tno].image) < H_REAL_SIZE*npix+ITEM_HDR_SIZE) ERROR('f',(message,"Pixel data for %s appears too small, in XYOPEN",name)); hreadb_c(images[tno].image,s,0,ITEM_HDR_SIZE,&iostat); CHECK(iostat,(message,"Error reading pixel data label for %s, in XYOPEN",name)); if( memcmp(s,real_item,ITEM_HDR_SIZE) ) ERROR('f',(message,"Bad pixel data label for %s, in XYOPEN",name)); /*----------------------------------------------------------------------*/ /* */ /* A new image. Write out all the axes infomation, and initialise */ /* the file with the "binary item" sequence. */ /* */ /*----------------------------------------------------------------------*/ } else { wrhdi_c(tno,"naxis",naxis); Strcpy(naxes,"naxis0"); length = strlen(naxes) - 1; for(i=0; i < naxis; i++){ naxes[length] ++; wrhdi_c(tno,naxes,axes[i]); } hwriteb_c(images[tno].image,real_item,0,ITEM_HDR_SIZE,&iostat); CHECK(iostat,(message,"Error writing pixel data label for %s, in XYOPEN",name)); } /* Common to old and new. Copy the dimension info to the local description. */ images[tno].offset = 0; images[tno].naxis = naxis; for(i=0; i < naxis; i++) images[tno].axes[i] = axes[i]; for(i = naxis; i < MAXNAX; i++) images[tno].axes[i] = 1; images[tno].mask = NULL; images[tno].image_exists = TRUE; images[tno].mask_exists = TRUE; *thandle = tno; }
void hdprobe_c(int tno,Const char *keyword,char *descr,size_t length,char *type,int *n) /** hdprobe -- Determine characteristics of a header variable. */ /*& mjs */ /*: header-i/o */ /*+ FORTRAN call sequence: subroutine hdprobe(tno,keyword,descr,type,n) integer tno character keyword*(*),descr*(*),type*(*) integer n Determine characteristics of a particular header variable. Inputs: tno Handle of the data set. keyword Name of the header variable to probe. Outputs: descr A formatted version of the item. For single numerics or short strings, this is the ascii encoding of the value. For large items, this is some message describing the item. type One of: 'nonexistent' 'integer*2' 'integer*8' 'integer' 'real' 'double' 'complex' 'character' 'text' 'binary' n Number of elements in the item. Zero implies an error. One implies that "descr" is the ascii encoding of the value. */ /*-- */ /*----------------------------------------------------------------------*/ { int item; char s[ITEM_HDR_SIZE],buf[MAXSIZE]; float rtemp,ctemp[2]; int iostat,unknown,size,i,itemp,offset,bufit; double dtemp; int2 jtemp; int8 ltemp; haccess_c(tno,&item,keyword,"read",&iostat); *n = 0; bufit = 0; Strcpy(type,"nonexistent"); if(iostat)return; size = hsize_c(item); unknown = FALSE; if(size <= ITEM_HDR_SIZE){ unknown = TRUE; size -= ITEM_HDR_SIZE; } else { hreadb_c(item,s,0,ITEM_HDR_SIZE,&iostat); check(iostat); if(!memcmp(s,real_item,ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_REAL_SIZE); size -= offset; Strcpy(type,"real"); *n = size / H_REAL_SIZE; if(size % H_REAL_SIZE) unknown = TRUE; else if(size == H_REAL_SIZE){ hreadr_c(item,&rtemp,offset,H_REAL_SIZE,&iostat); check(iostat); Sprintf(buf,"%-14.7g",rtemp); bufit = 1; } } else if(!memcmp(s,int_item,ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_INT_SIZE); size -= offset; Strcpy(type,"integer"); *n = size / H_INT_SIZE; if(size % H_INT_SIZE) unknown = TRUE; else if(size == H_INT_SIZE){ hreadi_c(item,&itemp,offset,H_INT_SIZE,&iostat); check(iostat); Sprintf(buf,"%d",itemp); bufit = 1; } } else if(!memcmp(s,int2_item,ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_INT2_SIZE); size -= offset; Strcpy(type,"integer*2"); *n = size / H_INT2_SIZE; if(size % H_INT2_SIZE) unknown = TRUE; else if(size == H_INT2_SIZE){ hreadj_c(item,&jtemp,offset,H_INT2_SIZE,&iostat); check(iostat); Sprintf(buf,"%d",jtemp); bufit = 1; } } else if(!memcmp(s,int8_item,ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_INT8_SIZE); size -= offset; Strcpy(type,"integer*8"); *n = size / H_INT8_SIZE; if(size % H_INT8_SIZE) unknown = TRUE; else if(size == H_INT8_SIZE){ hreadl_c(item,<emp,offset,H_INT8_SIZE,&iostat); check(iostat); Sprintf(buf,"%lld",ltemp); bufit = 1; } } else if(!memcmp(s,dble_item,ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_DBLE_SIZE); size -= offset; Strcpy(type,"double"); *n = size / H_DBLE_SIZE; if(size % H_DBLE_SIZE) unknown = TRUE; else if(size == H_DBLE_SIZE){ hreadd_c(item,&dtemp,offset,H_DBLE_SIZE,&iostat); check(iostat); Sprintf(buf,"%-20.10g",dtemp); bufit = 1; } } else if(!memcmp(s,cmplx_item,ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_CMPLX_SIZE); size -= offset; Strcpy(type,"complex"); *n = size / H_CMPLX_SIZE; if(size % H_CMPLX_SIZE) unknown = TRUE; else if(size == H_CMPLX_SIZE){ hreadr_c(item,ctemp,offset,H_CMPLX_SIZE,&iostat); check(iostat); Sprintf(buf,"(%-14.7g,%-14.7g)",ctemp[0],ctemp[1]); bufit = 1; } } else if(!memcmp(s,char_item,ITEM_HDR_SIZE)){ offset = ITEM_HDR_SIZE; size -= offset; size = min(size,MAXSIZE-1); *n = 1; Strcpy(type,"character"); hreadb_c(item,buf,ITEM_HDR_SIZE,size,&iostat); check(iostat); *(buf+size) = 0; bufit = 1; } else if(!memcmp(s,binary_item,ITEM_HDR_SIZE)){ *n = size; Strcpy(type,"binary"); } else{ Strcpy(type,"text"); *n = size + ITEM_HDR_SIZE; for(i=0; i < ITEM_HDR_SIZE; i++) if(!isspace(*(s+i)) && !isprint(*(s+i)))unknown = TRUE; } } hdaccess_c(item,&iostat); check(iostat); if(unknown){ Strcpy(type,"unknown"); *n = size + ITEM_HDR_SIZE; } else if(bufit){ if(strlen(buf) > length - 1) bugv_c('f',"Descr buffer overflow in hdprobe for %s",keyword); strcpy(descr,buf); } }
void rdhdd_c(int thandle,Const char *keyword,double *value,double defval) /** rdhdd -- Read a double precision-valued header variable. */ /*& mjs */ /*: header-i/o */ /*+ FORTRAN call sequence: subroutine rdhdd(tno,keyword,value,default) integer tno character keyword*(*) double precision value,default Read a double precision valued header variable. Input: tno The file handle of the data set. keyword The name of the header variable. default The default value to return, if the header variable is not found. Output: value The value of the header variable. This will be the default value, if the variable is missing from the header. */ /*-- */ /*----------------------------------------------------------------------*/ { int item; char s[ITEM_HDR_SIZE]; int iostat,length,itemp,offset; float rtemp; /* Firstly assume the variable is missing. Try to get it. If successful read it. */ *value = defval; haccess_c(thandle,&item,keyword,"read",&iostat); if(iostat)return; length = hsize_c(item); if(length >= 0){ /* Determine the type of the value, and convert it to double precision. */ hreadb_c(item,s,0,ITEM_HDR_SIZE,&iostat); check(iostat); iostat = 0; if( !memcmp(s,int_item, ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_INT_SIZE); if(offset + H_INT_SIZE == length){ hreadi_c(item,&itemp,offset,H_INT_SIZE,&iostat); *value = itemp; } } else if(!memcmp(s,real_item,ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_REAL_SIZE); if(offset + H_REAL_SIZE == length){ hreadr_c(item,&rtemp,offset,H_REAL_SIZE,&iostat); *value = rtemp; } } else if(!memcmp(s,dble_item,ITEM_HDR_SIZE)){ offset = mroundup(ITEM_HDR_SIZE,H_DBLE_SIZE); if(offset + H_DBLE_SIZE == length){ hreadd_c(item,value, offset,H_DBLE_SIZE,&iostat); } } #if 0 /* can't do this: some routines, e.g. imhead, actually depend * on it falling through. Sick, but true */ else bugv_c('f',"rdhdd_c: keyword %s not covered here",keyword); #endif check(iostat); } hdaccess_c(item,&iostat); check(iostat); }