Exemplo n.º 1
0
double getelem(struct data *d,char *par,int image)
{
  double *dbl;
  int cycle,n,id;
  dbl=val(par,&d->p);
  if (arraycheck(par,&d->a)) { /* parameter is arrayed */
    cycle=getcycle(par,&d->a);
    n=nvals(par,&d->p);
    id=(image/cycle)%n;
    return(dbl[id]);
  } else { /* par is not in array string */
    n=nvals(par,&d->p);
    if (n>1) {
      id=image%n; /* assume cycle=1 */
      return(dbl[id]);
    } else {
      return(dbl[0]);
    }
  }
}
Exemplo n.º 2
0
GimpParam *
pygimp_param_from_tuple(PyObject *args, const GimpParamDef *ptype, int nparams)
{
    PyObject *tuple, *item, *r, *g, *b, *x, *y, *w, *h;
    GimpParam *ret;
    int i, j, len;
    gint32 *i32a; gint16 *i16a; guint8 *i8a; gdouble *fa; gchar **sa;

    if (nparams == 0)
	tuple = PyTuple_New(0);
    else if (!PyTuple_Check(args) && nparams == 1)
	tuple = Py_BuildValue("(O)", args);
    else {
	Py_INCREF(args);
	tuple = args;
    }
    if (!PyTuple_Check(tuple)) {
	PyErr_SetString(PyExc_TypeError, "wrong type of parameter");
        Py_DECREF(tuple);
	return NULL;
    }

    if (PyTuple_Size(tuple) != nparams) {
	PyErr_SetString(PyExc_TypeError, "wrong number of parameters");
        Py_DECREF(tuple);
	return NULL;
    }

    ret = g_new(GimpParam, nparams+1);
    for (i = 0; i <= nparams; i++)
	ret[i].type = GIMP_PDB_STATUS;
#define check(expr) if (expr) { \
	    PyErr_SetString(PyExc_TypeError, "wrong parameter type"); \
	    Py_DECREF(tuple); \
	    gimp_destroy_params(ret, nparams); \
	    return NULL; \
	}
#define arraycheck(expr, ar) if (expr) { \
	    PyErr_SetString(PyExc_TypeError, "subscript of wrong type"); \
	    Py_DECREF(tuple); \
	    gimp_destroy_params(ret, nparams); \
	    g_free(ar); \
	    return NULL; \
	}
    for (i = 1; i <= nparams; i++) {
	item = PyTuple_GetItem(tuple, i-1);
	switch (ptype[i-1].type) {
	case GIMP_PDB_INT32:
	    check((x = PyNumber_Int(item)) == NULL);
	    ret[i].data.d_int32 = (gint32)PyInt_AsLong(x);
	    Py_DECREF(x);
	    break;
	case GIMP_PDB_INT16:
	    check((x = PyNumber_Int(item)) == NULL);
	    ret[i].data.d_int16 = (gint16)PyInt_AsLong(x);
	    Py_DECREF(x);
	    break;
	case GIMP_PDB_INT8:
	    check((x = PyNumber_Int(item)) == NULL);
	    ret[i].data.d_int8 = (guint8)PyInt_AsLong(x);
	    Py_DECREF(x);
	    break;
	case GIMP_PDB_FLOAT:
	    check((x = PyNumber_Float(item)) == NULL);
	    ret[i].data.d_float = PyFloat_AsDouble(x);
	    Py_DECREF(x);
	    break;
	case GIMP_PDB_STRING:
	    if (item == Py_None) {
		ret[i].data.d_string = NULL;
		break;
	    }
	    check((x = PyObject_Str(item)) == NULL);
	    ret[i].data.d_string = g_strdup(PyString_AsString(x));
	    Py_DECREF(x);
	    break;
	case GIMP_PDB_INT32ARRAY:
	    check(!PySequence_Check(item));
	    len = PySequence_Length(item);
	    i32a = g_new(gint32, len);
	    for (j = 0; j < len; j++) {
		x = PySequence_GetItem(item, j);
		arraycheck((y=PyNumber_Int(x))==NULL,
			   i32a);
		i32a[j] = PyInt_AsLong(y);
		Py_DECREF(y);
	    }
	    ret[i].data.d_int32array = i32a;
	    break;
	case GIMP_PDB_INT16ARRAY:
	    check(!PySequence_Check(item));
	    len = PySequence_Length(item);
	    i16a = g_new(gint16, len);
	    for (j = 0; j < len; j++) {
		x = PySequence_GetItem(item, j);
		arraycheck((y=PyNumber_Int(x))==NULL,
			   i16a);
		i16a[j] = PyInt_AsLong(y);
		Py_DECREF(y);
	    }
	    ret[i].data.d_int16array = i16a;
	    break;
	case GIMP_PDB_INT8ARRAY:
	    check(!PySequence_Check(item));
	    len = PySequence_Length(item);
	    i8a = g_new(guint8, len);
	    for (j = 0; j < len; j++) {
		x = PySequence_GetItem(item, j);
		arraycheck((y=PyNumber_Int(x))==NULL,
			   i8a);
		i8a[j] = PyInt_AsLong(y);
		Py_DECREF(y);
	    }
	    ret[i].data.d_int8array = i8a;
	    break;
	case GIMP_PDB_FLOATARRAY:
	    check(!PySequence_Check(item));
	    len = PySequence_Length(item);
	    fa = g_new(gdouble, len);
	    for (j = 0; j < len; j++) {
		x = PySequence_GetItem(item, j);
		arraycheck((y=PyNumber_Float(x))==NULL,
			   fa);
		fa[j] = PyFloat_AsDouble(y);
		Py_DECREF(y);
	    }
	    ret[i].data.d_floatarray = fa;
	    break;
	case GIMP_PDB_STRINGARRAY:
	    check(!PySequence_Check(item));
	    len = PySequence_Length(item);
	    sa = g_new(gchar *, len);
	    for (j = 0; j < len; j++) {
		x = PySequence_GetItem(item, j);
		if (x == Py_None) {
		    sa[j] = NULL;
		    continue;
		}
		arraycheck((y=PyObject_Str(x))==NULL,
			   sa);
		sa[j] = g_strdup(PyString_AsString(y));
		Py_DECREF(y);
	    }
	    ret[i].data.d_stringarray = sa;
	    break;
	case GIMP_PDB_COLOR:
	    {
		GimpRGB *rgb, tmprgb;

		if (!pygimp_rgb_check(item)) {
		    check(!PySequence_Check(item) ||
			  PySequence_Length(item) < 3);
		    r = PySequence_GetItem(item, 0);
		    g = PySequence_GetItem(item, 1);
		    b = PySequence_GetItem(item, 2);
		    check(!PyInt_Check(r) || !PyInt_Check(g) ||
			  !PyInt_Check(b));
		    gimp_rgba_set_uchar(&tmprgb, PyInt_AsLong(r),
					PyInt_AsLong(g), PyInt_AsLong(b), 255);
		    rgb = &tmprgb;
		} else {
		    rgb = pyg_boxed_get(item, GimpRGB);
		}
		ret[i].data.d_color = *rgb;
	    }
	    break;
	case GIMP_PDB_REGION:
	    check(!PySequence_Check(item) ||
		  PySequence_Length(item) < 4);
	    x = PySequence_GetItem(item, 0);
	    y = PySequence_GetItem(item, 1);
	    w = PySequence_GetItem(item, 2);
	    h = PySequence_GetItem(item, 3);
	    check(!PyInt_Check(x) || !PyInt_Check(y) ||
		  !PyInt_Check(w) || !PyInt_Check(h));
	    ret[i].data.d_region.x = PyInt_AsLong(x);
	    ret[i].data.d_region.y = PyInt_AsLong(y);
	    ret[i].data.d_region.width = PyInt_AsLong(w);
	    ret[i].data.d_region.height = PyInt_AsLong(h);
	    break;
	case GIMP_PDB_DISPLAY:
	    check(!pygimp_display_check(item));
	    ret[i].data.d_display = ((PyGimpDisplay *)item)->ID;
	    break;
	case GIMP_PDB_IMAGE:
	    if (item == Py_None) {
		ret[i].data.d_image = -1;
		break;
	    }
	    check(!pygimp_image_check(item));
	    ret[i].data.d_image = ((PyGimpImage *)item)->ID;
	    break;
	case GIMP_PDB_LAYER:
	    if (item == Py_None) {
		ret[i].data.d_layer = -1;
		break;
	    }
	    check(!pygimp_layer_check(item));
	    ret[i].data.d_layer = ((PyGimpLayer *)item)->ID;
	    break;
	case GIMP_PDB_CHANNEL:
	    if (item == Py_None) {
		ret[i].data.d_channel = -1;
		break;
	    }
	    check(!pygimp_channel_check(item));
	    ret[i].data.d_channel = ((PyGimpChannel *)item)->ID;
	    break;
	case GIMP_PDB_DRAWABLE:
	    if (item == Py_None) {
		ret[i].data.d_channel = -1;
		break;
	    }
	    check(!pygimp_drawable_check(item));
	    ret[i].data.d_channel = ((PyGimpDrawable *)item)->ID;
	    break;
	case GIMP_PDB_SELECTION:
	    check(!pygimp_layer_check(item));
	    ret[i].data.d_selection = ((PyGimpLayer *)item)->ID;
	    break;
	case GIMP_PDB_BOUNDARY:
	    check(!PyInt_Check(item));
	    ret[i].data.d_boundary = PyInt_AsLong(item);
	    break;
	case GIMP_PDB_VECTORS:
	    check(!pygimp_vectors_check(item));
	    ret[i].data.d_vectors = ((PyGimpVectors *)item)->ID;
	    break;
	case GIMP_PDB_PARASITE:
	    /* can't do anything, since size of GimpParasite is not known */
	    break;
	case GIMP_PDB_STATUS:
	    check(!PyInt_Check(item));
	    ret[i].data.d_status = PyInt_AsLong(item);
	    break;
	case GIMP_PDB_END:
	    break;
	}
#undef check
#undef arraycheck
	ret[i].type = ptype[i-1].type;
    }

    Py_DECREF(tuple);
    return ret;
}
Exemplo n.º 3
0
void gen3Dfdfhdr(struct data *d,int image,int slab,int echo,int receiver,int type,int precision)
{

  char str[100];
  int cycle,n,id;
  double pro,ppe,pss,psi,phi,theta;
  double cospsi,cosphi,costheta;
  double sinpsi,sinphi,sintheta;
  double or0,or1,or2,or3,or4,or5,or6,or7,or8;
  double value;
  int dim1,dim2,dim3,ns,nr;
  int ne;
  int i,j,add;
  int align=0,hdrlen,pad_cnt;
  int *intval;
  double *dblval;
  char **strval;
  int datamode;

  /* Check that type is valid */
  if (!validtype(type) || (type == VJ)) {
    fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__);
    fprintf(stderr,"  Invalid 6th argument %s(*,*,*,*,*,'type',*)\n",__FUNCTION__);
    fflush(stderr);
    return;
  }

  datamode=FID;
  /* If FT has been done flag it as IMAGE */
  if ((d->dimstatus[0] & FFT) || (d->dimstatus[1] & FFT) || (d->dimstatus[2] & FFT)) datamode=IMAGE;

  /* Allocate for header */
  if ((d->fdfhdr = (char *)malloc(FDFHDRLEN*sizeof(char))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__);

  /* Data dimensions */
  dim1=d->np/2; dim2=d->nv; dim3=d->nv2; nr=d->nr;

  /* Number of echoes */
  ne=(int)*val("ne",&d->p);
  if (ne < 1) ne=1; /* Set ne to 1 if 'ne' does not exist */

  /* Number of slices (slabs) */
  ns=nvals("pss",&d->p);
  if (ns < 1) ns=1; /* Set ns to 1 if 'ns' does not exist */

  /* Allow for compressed multi-echo loop and multiple slabs */
/*
  image=(volindex-IMAGEOFFSET)/(ne*ns);
  slab=((volindex-IMAGEOFFSET)/ne)%ns;
  echo=(volindex-IMAGEOFFSET)%ne;
*/

  /* Set up for orientation */
  pro=getelem(d,"pro",image);
  ppe=getelem(d,"ppe",image);
  pss=sliceposition(d,slab); /* position in 2nd phase encode dimension */
  psi=getelem(d,"psi",image);
  phi=getelem(d,"phi",image);
  theta=getelem(d,"theta",image);

  /* Create header */
  sprintf(d->fdfhdr,"#!/usr/local/fdf/startup\n");
  strcat(d->fdfhdr,"float  rank = 3;\n");
  strcat(d->fdfhdr,"char  *spatial_rank = \"3dfov\";\n");
  switch(precision) {
    case FLT32: /* 32 bit float */
      strcat(d->fdfhdr,"char  *storage = \"float\";\n");
      strcat(d->fdfhdr,"float  bits = 32;\n");
      break;
    case DBL64: /* 64 bit double */
      strcat(d->fdfhdr,"char  *storage = \"double\";\n");
      strcat(d->fdfhdr,"float  bits = 64;\n");
      break;
    default:
      strcat(d->fdfhdr,"char  *storage = \"not supported\";\n");
      strcat(d->fdfhdr,"float  bits = ?;\n");
      break;
  } /* end precision switch */
  switch(type) {
    case MG: /* Magnitude */
      strcat(d->fdfhdr,"char  *type = \"absval\";\n");
      break;
    case RE: /* Real */
      strcat(d->fdfhdr,"char  *type = \"real\";\n");
      break;
    case IM: /* Imaginary */
      strcat(d->fdfhdr,"char  *type = \"imag\";\n");
      break;
    case PH: /* Phase */
      strcat(d->fdfhdr,"char  *type = \"phase\";\n");
      break;
    case MK: /* Mask */
      strcat(d->fdfhdr,"char  *type = \"mask\";\n");
      break;
    case RMK: /* Reverse mask of magnitude */
      strcat(d->fdfhdr,"char  *type = \"absval\";\n");
      break;
    case SM: /* Sensitivity map */
      strcat(d->fdfhdr,"char  *type = \"smap\";\n");
      break;
    case GF: /* Geometry factor */
      strcat(d->fdfhdr,"char  *type = \"gmap\";\n");
      break;
    case RS: /* Relative SNR */
      strcat(d->fdfhdr,"char  *type = \"rsnrmap\";\n");
      break;
    default:
      break;
  } /* end type switch */
  sprintf(str,"float  matrix[] = {%d, %d, %d};\n",dim1,dim2,dim3);
  strcat(d->fdfhdr,str);
  switch(datamode) {
    case IMAGE: /* Image */
      strcat(d->fdfhdr,"char  *abscissa[] = {\"cm\", \"cm\", \"cm\"};\n");
      break;
    case FID: /* FID */
/*
      strcat(d->fdfhdr,"char  *abscissa[] = {\"s\", \"s\", \"s\"};\n");
*/
      /* We must define as for image space to get a good display in VnmrJ */
      strcat(d->fdfhdr,"char  *abscissa[] = {\"cm\", \"cm\", \"cm\"};\n");
      break;
    default:
      strcat(d->fdfhdr,"char  *abscissa[] = {\"cm\", \"cm\", \"cm\"};\n");
      break;
  } /* end datamode switch */
  switch(type) {
    case PH: /* Phase */
      strcat(d->fdfhdr,"char  *ordinate[] = { \"radians\" };\n");
      break;
    case MK: /* Mask */
      strcat(d->fdfhdr,"char  *ordinate[] = { \"mask\" };\n");
      break;
    default:
      strcat(d->fdfhdr,"char  *ordinate[] = { \"intensity\" };\n");
      break;
  } /* end type switch */
  switch(datamode) {
    case IMAGE: /* Image */
      sprintf(str,"float  span[] = {%.6f, %.6f, %.6f};\n",*val("lro",&d->p),*val("lpe",&d->p),*val("lpe2",&d->p));
      strcat(d->fdfhdr,str);
      sprintf(str,"float  origin[] = {%.6f,%.6f,%.6f};\n",-pro-*val("lro",&d->p)/2,ppe-*val("lpe",&d->p)/2,pss-*val("lpe2",&d->p)/2);
      strcat(d->fdfhdr,str);
      break;
    case FID: /* FID */
/*
      sprintf(str,"float  span[] = {%.6f, %.6f, %.6f};\n",dim1/(*val("sw",&d->p)),dim2/(*val("sw1",&d->p)),dim3/(*val("sw2",&d->p)));
      strcat(d->fdfhdr,str);
      sprintf(str,"float  origin[] = {%.6f, %.6f, %.6f};\n",0.0,0.0,0.0);
      strcat(d->fdfhdr,str);
*/
      /* We must define as for image space to get a good display in VnmrJ */
      sprintf(str,"float  span[] = {%.6f, %.6f, %.6f};\n",*val("lro",&d->p),*val("lpe",&d->p),*val("lpe2",&d->p));
      strcat(d->fdfhdr,str);
      sprintf(str,"float  origin[] = {%.6f,%.6f,%.6f};\n",-pro-*val("lro",&d->p)/2,ppe-*val("lpe",&d->p)/2,pss-*val("lpe2",&d->p)/2);
      strcat(d->fdfhdr,str);
      break;
    default:
      sprintf(str,"float  span[] = {%.6f, %.6f, %.6f};\n",*val("lro",&d->p),*val("lpe",&d->p),*val("lpe2",&d->p));
      strcat(d->fdfhdr,str);
      sprintf(str,"float  origin[] = {%.6f,%.6f,%.6f};\n",-pro-*val("lro",&d->p)/2,ppe-*val("lpe",&d->p)/2,pss-*val("lpe2",&d->p)/2);
      strcat(d->fdfhdr,str);
      break;
  } /* end datamode switch */
  sprintf(str,"char  *nucleus[] = {\"%s\",\"%s\"};\n",*sval("tn",&d->p),*sval("dn",&d->p));
  strcat(d->fdfhdr,str);
  sprintf(str,"float  nucfreq[] = {%.6f,%.6f};\n",*val("sfrq",&d->p),*val("dfrq",&d->p));
  strcat(d->fdfhdr,str);
  switch(datamode) {
    case IMAGE: /* Image */
      sprintf(str,"float  location[] = {%.6f,%.6f,%.6f};\n",-pro,ppe,pss);
      strcat(d->fdfhdr,str);
      sprintf(str,"float  roi[] = {%.6f,%.6f,%.6f};\n",*val("lro",&d->p),*val("lpe",&d->p),*val("lpe2",&d->p));
      strcat(d->fdfhdr,str);
      break;
    case FID: /* FID */
      sprintf(str,"float  location[] = {%.6f,%.6f,%.6f};\n",-pro,ppe,pss);
      strcat(d->fdfhdr,str);
      sprintf(str,"float  roi[] = {%.6f,%.6f,%.6f};\n",dim1/(*val("sw",&d->p)),dim2/(*val("sw1",&d->p)),dim3/(*val("sw2",&d->p)));
      strcat(d->fdfhdr,str);
      break;
    default:
      sprintf(str,"float  location[] = {%.6f,%.6f,%.6f};\n",-pro,ppe,pss);
      strcat(d->fdfhdr,str);
      sprintf(str,"float  roi[] = {%.6f,%.6f,%.6f};\n",*val("lro",&d->p),*val("lpe",&d->p),*val("lpe2",&d->p));
      strcat(d->fdfhdr,str);
      break;
  } /* end datamode switch */
  sprintf(str,"float  gap = %.6f;\n",*val("gap",&d->p));
  strcat(d->fdfhdr,str);
  sprintf(str,"char  *file = \"%s\";\n",d->file);
  strcat(d->fdfhdr,str);
  sprintf(str,"int    slab_no = %d;\n",slab+1);
  strcat(d->fdfhdr,str);
  sprintf(str,"int    slabs = %d;\n",ns);
  strcat(d->fdfhdr,str);
  sprintf(str,"int    echo_no = %d;\n",echo+1);
  strcat(d->fdfhdr,str);
  sprintf(str,"int    echoes = %d;\n",ne);
  strcat(d->fdfhdr,str);

  if (ne < 2)
    value=getelem(d,"te",image);
  else { /* a multi echo expt */
    /* The TE array should hold the echo time of each echo */
    if (nvals("TE",&d->p) == *val("ne",&d->p)) {
      dblval=val("TE",&d->p);
      value=dblval[echo]/1000.0;
    } else {
      value=1.0; /* Just set a silly value */
    }
  }
  sprintf(str,"float  TE = %.3f;\n",1000.0*value);
  strcat(d->fdfhdr,str);
  sprintf(str,"float  te = %.6f;\n",value);
  strcat(d->fdfhdr,str);

  value=getelem(d,"tr",image);
  sprintf(str,"float  TR = %.3f;\n",1000.0*value);
  strcat(d->fdfhdr,str);
  sprintf(str,"float  tr = %.6f;\n",value);
  strcat(d->fdfhdr,str);

  sprintf(str,"int    ro_size = %d;\n",(int)*val("np",&d->p)/2);
  strcat(d->fdfhdr,str);
  sprintf(str,"int    pe_size = %d;\n",(int)*val("nv",&d->p));
  strcat(d->fdfhdr,str);
  sprintf(str,"int    pe2_size = %d;\n",(int)*val("nv2",&d->p));
  strcat(d->fdfhdr,str);
  sprintf(str,"char  *sequence = \"%s\";\n",*sval("seqfil",&d->p));
  strcat(d->fdfhdr,str);
  sprintf(str,"char  *studyid = \"%s\";\n",*sval("studyid_",&d->p));
  strcat(d->fdfhdr,str);
/*
  sprintf(str,"char  *position1 = \"%s\";\n","");
  strcat(d->fdfhdr,str);
  sprintf(str,"char  *position2 = \"%s\";\n","");
  strcat(d->fdfhdr,str);
*/

  value=getelem(d,"ti",image);
  sprintf(str,"float  TI = %.3f;\n",1000.0*value);
  strcat(d->fdfhdr,str);
  sprintf(str,"float  ti = %.6f;\n",value);
  strcat(d->fdfhdr,str);

  sprintf(str,"int    array_index = %d;\n",image+1-IMAGEOFFSET);
  strcat(d->fdfhdr,str);

  /* The array_dim is the number of *image???*.fdf = (for 3D) # volumes divided by # echoes*slices */
  value=(double)d->nvols/(ne*ns);
  /* But if there are reference volumes they must be accounted for */
  /* Check for image parameter array, since that is used to signify reference scans */
  if (arraycheck("image",&d->a)) {
    /* count # image=1 values */
    for (i=0;i<nvals("image",&d->p);i++) if (getelem(d,"image",i)<1) value--;
  }
  sprintf(str,"float  array_dim = %.4f;\n",value);
  strcat(d->fdfhdr,str);
/*
  sprintf(str,"float  image = 1.0;\n");
  strcat(d->fdfhdr,str);
*/

  /* The following assumes that fid data is always stored bigendian ..
     .. if we must reverse byte order to interpret then CPU is lilendian */
  if (reverse_byte_order) {
    sprintf(str,"int    bigendian = 0;\n");
    strcat(d->fdfhdr,str);
  }

  /* Image scaling */
  value=*val("aipScale",&d->p);
  if (FP_EQ(value,0.0)) value=1.0;
  sprintf(str,"float  imagescale = %.9f;\n",value);
  strcat(d->fdfhdr,str);

  sprintf(str,"float  psi = %.4f;\n",psi);
  strcat(d->fdfhdr,str);
  sprintf(str,"float  phi = %.4f;\n",phi);
  strcat(d->fdfhdr,str);
  sprintf(str,"float  theta = %.4f;\n",theta);
  strcat(d->fdfhdr,str);

  /* Generate direction cosine matrix from "Euler" angles just as recon_all */
  cospsi=cos(DEG2RAD*psi);
  sinpsi=sin(DEG2RAD*psi);
  cosphi=cos(DEG2RAD*phi);
  sinphi=sin(DEG2RAD*phi);
  costheta=cos(DEG2RAD*theta);
  sintheta=sin(DEG2RAD*theta);

  /* For 2D ...
  or0=-1*cosphi*cospsi - sinphi*costheta*sinpsi;
  or1=-1*cosphi*sinpsi + sinphi*costheta*cospsi;
  or2=-1*sinphi*sintheta;
  or3=-1*sinphi*cospsi + cosphi*costheta*sinpsi;
  or4=-1*sinphi*sinpsi - cosphi*costheta*cospsi;
  or5=cosphi*sintheta;
  or6=-1*sintheta*sinpsi;
  or7=sintheta*cospsi;
  or8=costheta;
  */
  /* For 3D ... */
  or0=-1*sinphi*sinpsi - cosphi*costheta*cospsi; /* the 2D or4  */
  or1=sinphi*cospsi - cosphi*costheta*sinpsi;    /* the 2D -or3 */
  or2=cosphi*sintheta;                           /* the 2D or5  */
  or3=cosphi*sinpsi - sinphi*costheta*cospsi;    /* the 2D -or1 */
  or4=-1*cosphi*cospsi - sinphi*costheta*sinpsi; /* the 2D or0  */
  or5=sinphi*sintheta;                           /* the 2D -or2 */
  or6=sintheta*cospsi;                           /* the 2D or7  */
  or7=sintheta*sinpsi;                           /* the 2D -or6  */
  or8=costheta;                                  /* the 2D or8  */

  sprintf(str,"float  orientation[] = {%.4f,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f};\n",
    or0,or1,or2,or3,or4,or5,or6,or7,or8);
  strcat(d->fdfhdr,str);

  sprintf(str,"char  *array_name = \"none\";\n");
  strcat(d->fdfhdr,str);

  /* Add arrayed parameters */
  if (d->a.npars>0) {
    for (i=0;i<*d->a.nvals;i++) {
      if (addpar2hdr(&d->a,i)) { /* If not already included by default */
        cycle=(int)d->a.d[0][i];
        n=nvals(d->a.s[0][i],&d->p);
        id=(image/cycle)%n;
        switch (ptype(d->a.s[0][i],&d->p)) {
          case 0:
            strcat(d->fdfhdr,"int    ");
            intval=ival(d->a.s[0][i],&d->p);
            sprintf(str,"%s = %d;\n",d->a.s[0][i],intval[id]);
            strcat(d->fdfhdr,str);
            break;
          case 1:
            strcat(d->fdfhdr,"float  ");
            dblval=val(d->a.s[0][i],&d->p);
            sprintf(str,"%s = %.6f;\n",d->a.s[0][i],dblval[id]);
            strcat(d->fdfhdr,str);
            break;
          case 2:
            strcat(d->fdfhdr,"char  *");
            strval=sval(d->a.s[0][i],&d->p);
            sprintf(str,"%s = \"%s\";\n",d->a.s[0][i],strval[id]);
            strcat(d->fdfhdr,str);
            break;
        }
      }
    }
  }

  /* Add sviblist parameters */
  if (d->s.npars>0) {
    for (i=0;i<*d->s.nvals;i++) {
      if (addpar2hdr(&d->s,i)) { /* If not already included by default */
        add = 1;
        if (d->a.npars>0) { /* Don't include arrayed parameters */
          for (j=0;j<*d->a.nvals;j++)
            if (!strcmp(d->a.s[0][j],d->s.s[0][i])) add--;
        }
        if (add) {
          /* NB The parameter may be arrayed with setprotect('par','on',256) */
          n=nvals(d->s.s[0][i],&d->p);
          if (n>0) {
            id=image%n; /* Assume 'cycle' is 1 - no mechanism exists to suggest otherwise */
            switch (ptype(d->s.s[0][i],&d->p)) {
              case 0: /* Integer */
                strcat(d->fdfhdr,"int    ");
                intval=ival(d->s.s[0][i],&d->p);
                sprintf(str,"%s = %d;\n",d->s.s[0][i],intval[id]);
                strcat(d->fdfhdr,str);
                break;
              case 1: /* Real */
                strcat(d->fdfhdr,"float  ");
                dblval=val(d->s.s[0][i],&d->p);
                sprintf(str,"%s = %.6f;\n",d->s.s[0][i],dblval[id]);
                strcat(d->fdfhdr,str);
                break;
              case 2: /* String */
                strcat(d->fdfhdr,"char  *");
                strval=sval(d->s.s[0][i],&d->p);
                sprintf(str,"%s = \"%s\";\n",d->s.s[0][i],strval[id]);
                strcat(d->fdfhdr,str);
                break;
            }
          }
        }
      }
    }
  }

/* For 2D strcat(d->fdfhdr,"int checksum = 1291708713;\n"); is used for some unknown reason */
  strcat(d->fdfhdr,"int checksum = 0;\n");
  strcat(d->fdfhdr,"\f\n");

  /* Add padding */
  switch(precision) {
    case FLT32: /* 32 bit float */
      align = sizeof(float);
      break;
    case DBL64: /* 64 bit double */
      align = sizeof(double);
      break;
    default:
      break;
  } /* end precision switch */
  hdrlen=strlen(d->fdfhdr);
  hdrlen++; /* allow for NULL terminator */
  pad_cnt=hdrlen%align;
  pad_cnt=(align-pad_cnt)%align;
  for(i=0;i<pad_cnt;i++) strcat(d->fdfhdr,"\n");

}