static void initLineWindow(lineWindow *lw, lineObj *line, int size)
{ 
  lw->pos = -1;
  lw->lineIsRing = MS_FALSE;
  lw->size = size;
  lw->line = line;
  lw->index = floor(lw->size/2); /* index of current position in points array */
  lw->points = (pointObj**)msSmallMalloc(sizeof(pointObj*)*size);
  
  if ( (line->numpoints >= 2) &&
       ((FP_EQ(line->point[0].x,
               line->point[line->numpoints-1].x)) &&
        (FP_EQ(line->point[0].y,
               line->point[line->numpoints-1].y))) ) {
    lw->lineIsRing = 1; 
  }
}
Exemple #2
0
void wcomb3Dfdf(char *filename,struct data *d,int image,int slab,int echo,int type,int precision)
{
  FILE *f_out;
  float *floatdata;
  double *doubledata;
  char null[1];
  double re,im,M,scale;
  int dim1,dim2,dim3,nr;
  int i,j,k,l;
  int ix1;
  int datamode;

  /* Check that type is valid */
  if ((type != MG) && (type != RMK)) {
    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;

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

  /* Generate fdf header if its the first processing block of a volume */
  if (d->block == 0) { /* The first processing block of a volume */
    gen3Dfdfhdr(d,image+IMAGEOFFSET,slab,echo,0,MG,precision);
    /* Set NULL terminator for fdf header */
    *null=(char)0;
    /* Write header */
    if ((f_out = fopen(filename, "w")) == NULL) {
      fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__);
      fprintf(stderr,"  Unable to write to %s\n",filename);
      fflush(stderr);
      free(d->fdfhdr); /* Wipe fdf header */
      return;
    }
    fprintf(f_out,"%s",d->fdfhdr);
    fwrite(null,sizeof(char),1,f_out);
    free(d->fdfhdr); /* Wipe fdf header */
  } else { /* Open file for appending */
    if ((f_out = fopen(filename, "a")) == NULL) {
      fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__);
      fprintf(stderr,"  Unable to append to %s\n",filename);
      fflush(stderr);
      return;
    }
  }

  /* Image scaling */
  scale=*val("aipScale",&d->p);
  if (FP_EQ(scale,0.0)) scale=1.0;

  /* Allocate memory and write fdf header and image */
  switch(precision) {
    case FLT32: /* 32 bit float */
      if ((floatdata = (float *)malloc(dim2*dim1*sizeof(float))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__);
      switch(type) {
        case MG: /* Magnitude */
          for(l=0;l<dim3;l++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                M=0.0;
                for (k=0;k<nr;k++) {
                  re=fabs(d->data[k][l][ix1][0]);
                  im=fabs(d->data[k][l][ix1][1]);
                  M+=(re*re+im*im);
                }
                floatdata[ix1] = (float)sqrt(M);
                switch(datamode) {
                  case IMAGE: floatdata[ix1] *= scale; break;
                  default: break;
                }
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
          break;
        case RMK: /* Reverse mask of magnitude */
          for(l=0;l<dim3;l++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                if (!d->mask[l][ix1]) {
                  M=0.0;
                  for (k=0;k<nr;k++) {
                    re=fabs(d->data[k][l][ix1][0]);
                    im=fabs(d->data[k][l][ix1][1]);
                    M+=(re*re+im*im);
                  }
                  floatdata[ix1] = (float)sqrt(M);
                  switch(datamode) {
                    case IMAGE: floatdata[ix1] *= scale; break;
                    default: break;
                  }
                } else {
                  floatdata[ix1] = 0.0;
                }
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
          break;
      } /* end type switch */
      fclose(f_out);
      free(floatdata);
      break;
    case DBL64: /* 64 bit double */
      if ((doubledata = (double *)malloc(dim2*dim1*sizeof(double))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__);
      switch(type) {
        case MG: /* Magnitude */
          for(l=0;l<dim3;l++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                M=0.0;
                for (k=0;k<nr;k++) {
                  re=fabs(d->data[k][l][ix1][0]);
                  im=fabs(d->data[k][l][ix1][1]);
                  M+=(re*re+im*im);
                }
                doubledata[ix1] = sqrt(M);
                switch(datamode) {
                  case IMAGE: doubledata[ix1] *= scale; break;
                  default: break;
                }
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
        case RMK: /* Reverse mask of magnitude */
          for(l=0;l<dim3;l++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                if (!d->mask[l][ix1]) {
                  M=0.0;
                  for (k=0;k<nr;k++) {
                    re=fabs(d->data[k][l][ix1][0]);
                    im=fabs(d->data[k][l][ix1][1]);
                    M+=(re*re+im*im);
                  }
                  doubledata[ix1] = sqrt(M);
                  switch(datamode) {
                    case IMAGE: doubledata[ix1] *= scale; break;
                    default: break;
                  }
                } else {
                  doubledata[ix1] = 0.0;
                }
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
      } /* end type switch */
      fclose(f_out);
      free(doubledata);
      break;
    default:
      fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__);
      fprintf(stderr,"  Invalid 7th argument %s(*,*,*,*,*,*,'precision')\n",__FUNCTION__);
      fflush(stderr);
      break;
  } /* end precision switch */

}
Exemple #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");

}
Exemple #4
0
void w3Dfdf(char *filename,struct data *d,int image,int slab,int echo,int receiver,int type,int precision)
{
  FILE *f_out;
  float *floatdata;
  double *doubledata;
  char null[1];
  double re,im,M,scale;
  int dim1,dim2,dim3;
  int i,j,k;
  int ix1;
  int datamode;

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

  /* Data dimensions */
  dim1=d->np/2; dim2=d->nv; dim3=d->endpos-d->startpos;

  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;

  /* Generate fdf header if its the first processing block of a volume */
  if (d->block == 0) { /* The first processing block of a volume */
    gen3Dfdfhdr(d,image+IMAGEOFFSET,slab,echo,receiver,type,precision);
    /* Set NULL terminator for fdf header */
    *null=(char)0;
    /* Write header */
    if ((f_out = fopen(filename, "w")) == NULL) {
      fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__);
      fprintf(stderr,"  Unable to write to %s\n",filename);
      fflush(stderr);
      free(d->fdfhdr); /* Wipe fdf header */
      return;
    }
    fprintf(f_out,"%s",d->fdfhdr);
    fwrite(null,sizeof(char),1,f_out);
    free(d->fdfhdr); /* Wipe fdf header */
  } else { /* Open file for appending */
    if ((f_out = fopen(filename, "a")) == NULL) {
      fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__);
      fprintf(stderr,"  Unable to append to %s\n",filename);
      fflush(stderr);
      return;
    }
  }

  /* For Sensitivity Map, Geometry Factor and Relative SNR we just output
     the magnitude. For type 'MG' output is scaled by aipScale so we use
     type 'SM' which is not scaled */
  switch(type) {
    case GF: type = SM; break; /* Geometry Factor */
    case RS: type = SM; break; /* Relative SNR */
  }

  /* Image scaling */
  scale=*val("aipScale",&d->p);
  if (FP_EQ(scale,0.0)) scale=1.0;

  /* Allocate memory and write fdf header and image */
  switch(precision) {
    case FLT32: /* 32 bit float */
      if ((floatdata = (float *)malloc(dim2*dim1*sizeof(float))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__);
      switch(type) {
        case MG: /* Magnitude */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                re=fabs(d->data[receiver][k][ix1][0]);
                im=fabs(d->data[receiver][k][ix1][1]);
                M=sqrt(re*re+im*im);
                floatdata[ix1] = (float)M;
                switch(datamode) {
                  case IMAGE: floatdata[ix1] *= scale; break;
                  default: break;
                }
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
          break;
        case PH: /* Phase */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                re=d->data[receiver][k][ix1][0];
                im=d->data[receiver][k][ix1][1];
                /* atan2 returns values (in radians) between +PI and -PI */
                M=atan2(im,re);
                floatdata[ix1] = (float)M;
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
          break;
        case RE: /* Real */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                floatdata[ix1] = (float)d->data[receiver][k][ix1][0];
                switch(datamode) {
                  case IMAGE: floatdata[ix1] *= scale; break;
                  default: break;
                }
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
          break;
        case IM: /* Imaginary */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                floatdata[ix1] = (float)d->data[receiver][k][ix1][1];
                switch(datamode) {
                  case IMAGE: floatdata[ix1] *= scale; break;
                  default: break;
                }
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
          break;
        case MK: /* Mask */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                floatdata[ix1] = (float)d->mask[k][ix1];
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
          break;
        case RMK: /* Reverse Mask of Magnitude */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                if (!d->mask[k][ix1]) {
                  re=fabs(d->data[receiver][k][ix1][0]);
                  im=fabs(d->data[receiver][k][ix1][1]);
                  M=sqrt(re*re+im*im);
                  floatdata[ix1] = (float)M;
                } else {
                  floatdata[ix1] = 0.0;
                }
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
          break;
        case SM: /* Sensitivity Map */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                re=fabs(d->data[receiver][k][ix1][0]);
                im=fabs(d->data[receiver][k][ix1][1]);
                M=sqrt(re*re+im*im);
                floatdata[ix1] = (float)M;
              }
            }
            fwrite(floatdata,sizeof(float),dim1*dim2,f_out);
          }
        default:
          break;
      } /* end type switch */
      fclose(f_out);
      free(floatdata);
      break;
    case DBL64: /* 64 bit double */
      if ((doubledata = (double *)malloc(dim2*dim1*sizeof(double))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__);
      switch(type) {
        case MG: /* Magnitude */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                re=fabs(d->data[receiver][k][ix1][0]);
                im=fabs(d->data[receiver][k][ix1][1]);
                M=sqrt(re*re+im*im);
                doubledata[ix1] = M;
                switch(datamode) {
                  case IMAGE: doubledata[ix1] *= scale; break;
                  default: break;
                }
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
        case PH: /* Phase */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                re=d->data[receiver][k][ix1][0];
                im=d->data[receiver][k][ix1][1];
                /* atan2 returns values (in radians) between +PI and -PI */
                M=atan2(im,re);
                doubledata[ix1] = M;
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
        case RE: /* Real */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                doubledata[ix1] = d->data[receiver][k][ix1][0];
                switch(datamode) {
                  case IMAGE: doubledata[ix1] *= scale; break;
                  default: break;
                }
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
        case IM: /* Imaginary */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                doubledata[ix1] = d->data[receiver][k][ix1][1];
                switch(datamode) {
                  case IMAGE: doubledata[ix1] *= scale; break;
                  default: break;
                }
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
        case MK: /* Mask */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                doubledata[ix1] = (double)d->mask[k][ix1];
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
        case RMK: /* Reverse Mask of Magnitude */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                if (!d->mask[k][ix1]) {
                  re=fabs(d->data[receiver][k][ix1][0]);
                  im=fabs(d->data[receiver][k][ix1][1]);
                  M=sqrt(re*re+im*im);
                  doubledata[ix1] = M;
                } else {
                  doubledata[ix1] = 0.0;
                }
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
        case SM: /* Sensitivity map */
          for (k=0;k<dim3;k++) {
            for(i=0;i<dim2;i++) {
              for (j=0;j<dim1;j++) {
                ix1=i*dim1+j;
                re=fabs(d->data[receiver][k][ix1][0]);
                im=fabs(d->data[receiver][k][ix1][1]);
                M=sqrt(re*re+im*im);
                doubledata[ix1] = M;
              }
            }
            fwrite(doubledata,sizeof(double),dim1*dim2,f_out);
          }
          break;
        default:
          break;
      } /* end type switch */
      fclose(f_out);
      free(doubledata);
      break;
    default:
      fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__);
      fprintf(stderr,"  Invalid 8th argument %s(*,*,*,*,*,*,*,'precision')\n",__FUNCTION__);
      fflush(stderr);
      break;
  } /* end precision switch */
}