void cppar(char *par1,struct pars *p1,char *par2,struct pars *p2) { int i; int ix; struct pars p3; /* Find the index of par1 in the p1 set otherwise return */ ix=parindex(par1,p1); if (ix<0) return; /* Search for par2 in the p2 set to modify an exisiting parameter */ for (i=0;i<p2->npars;i++) { if (!strcmp(p2->name[i],par2)) { /* There is a match */ /* Free existing values */ switch(p2->type[i]) { case 0: free(p2->i[i]); break; case 1: free(p2->d[i]); break; case 2: free(p2->s[i]); break; } /* end p->type switch */ /* Copy values from p1 to p2 */ copyvalues(p1,ix,p2,i); return; } } /* If we have not already returned then add the parameter */ /* Copy the current p2 set to p3 */ p3.npars = p2->npars; if (p3.npars > 0) mallocnpars(&p3); for (i=0;i<p3.npars;i++) { if ((p3.name[i] = (char *)malloc((strlen(p2->name[i])+1)*sizeof(char))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); strcpy(p3.name[i],p2->name[i]); /* Copy values from p2 to p3 */ copyvalues(p2,i,&p3,i); } /* Clear the current p2 set */ clearpars(p2); /* Restore the p2 set but allocate for an extra parameter */ p2->npars = p3.npars+1; mallocnpars(p2); for (i=0;i<p3.npars;i++) { if ((p2->name[i] = (char *)malloc((strlen(p3.name[i])+1)*sizeof(char))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); strcpy(p2->name[i],p3.name[i]); /* Copy values back from p3 to p2 */ copyvalues(&p3,i,p2,i); } /* Clear the p3 set */ clearpars(&p3); /* Now add the parameter */ if ((p2->name[i] = (char *)malloc((strlen(par2)+1)*sizeof(char))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); strcpy(p2->name[i],par2); /* Copy values from p1 to p2 */ copyvalues(p1,ix,p2,i); }
void sliceorder2D(struct data *d) { int index; int ns,nr; double *pss; fftw_complex ***slice; int i,j,k; #ifdef DEBUG struct timeval tp; double t1,t2; char function[20]; strcpy(function,"sliceorder2D"); /* Set function name */ fprintf(stdout,"\n%s: %s()\n",SOURCEFILE,function); gettimeofday(&tp, NULL); t1=(double)tp.tv_sec+(1.e-6)*tp.tv_usec; fflush(stdout); #endif /* Index of pss in pars struct, return if pss is not found */ index=parindex("pss",&d->p); if (index < 0) return; /* Number of slices and receivers */ ns=d->ns; nr=d->nr; /* Take a copy of the starting slice order */ if ((pss = (double *)malloc(ns*sizeof(double))) == NULL) nomem(); for (i=0;i<ns;i++) pss[i]=d->p.d[index][i]; /* Sort slice positions into ascending order */ qsort(d->p.d[index],ns,sizeof(d->p.d[index][0]),doublecmp); /* Allocate memory for some data pointers to the slices */ if ((slice = (fftw_complex ***)fftw_malloc(nr*sizeof(fftw_complex **))) == NULL) nomem(); for (i=0;i<nr;i++) { /* loop over receiver blocks */ if ((slice[i] = (fftw_complex **)fftw_malloc(ns*sizeof(fftw_complex *))) == NULL) nomem(); for (j=0;j<ns;j++) /* loop over slices */ if ((slice[i][j] = (fftw_complex *)fftw_malloc(sizeof(fftw_complex))) == NULL) nomem(); } /* Set new pointers to slices in ascending order */ for (i=0;i<ns;i++) { for (j=0;j<ns;j++) { if (d->p.d[index][i] == pss[j]) { for (k=0;k<nr;k++) slice[k][i]=d->data[k][j]; break; } } } /* Reset original data pointers in ascending order */ for (i=0;i<nr;i++) for (j=0;j<ns;j++) d->data[i][j]=slice[i][j]; fftw_free(slice); #ifdef DEBUG gettimeofday(&tp, NULL); t2=(double)tp.tv_sec+(1.e-6)*tp.tv_usec; fprintf(stdout," Took %f secs\n",t2-t1); fprintf(stdout," pss\n"); fprintf(stdout," original\tnew\n"); for (i=0;i<ns;i++) fprintf(stdout," %f\t%f\n",pss[i],d->p.d[index][i]); fflush(stdout); #endif free(pss); }
void settep(struct data *d, int mode) { int dim1,dim2,dim3,nr; int **max; int i,j,k,l,n; int ix; int nseg,nnav,etl,altread,echo,oddcount,evencount; int steadyStates,flipLine; double re,im,M2,maxM,oddmax,evenmax,sw,tepadjust; char tepfile[MAXPATHLEN]; FILE *f_out; #ifdef DEBUG struct timeval tp; double t1,t2; int rtn; fprintf(stdout,"\n%s: %s()\n",__FILE__,__FUNCTION__); fprintf(stdout," Attempting to calculate correction for tep ...\n"); rtn=gettimeofday(&tp, NULL); t1=(double)tp.tv_sec+(1.e-6)*tp.tv_usec; fflush(stdout); #endif /* Set data dimensions */ dim1=d->np/2; dim2=d->nv; dim3=d->endpos-d->startpos; nr=d->nr; /* Allow for arbitrary number of acquired steady state echoes (default 1) */ steadyStates = (int)*val("acquiredSteadyStates",&d->p); if (parindex("acquiredSteadyStates",&d->p) < 0) steadyStates = 1; /* Allow for sequences with readout polarity set constant for the first acquired EPI echo, i.e. the set polarity depends on # navigators and # acquired steady states */ flipLine = (int)*val("flipLine",&d->p); if (flipLine != 1) flipLine = 0; switch(mode) { case STD: /* Use maximum magnitude */ /* If noise data does not exist set it to zero */ if (!d->noise.data) zeronoise(d); /* If noise data is zero sample the noise */ if (d->noise.zero) getnoise(d,STD); /* Set some defaults */ zeromax(d); if ((max = (int **)malloc(nr*sizeof(int *))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); for (i=0;i<nr;i++) if ((max[i] = (int *)malloc(dim2*sizeof(int))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); /* Get coordinates of maxima for odd and even echoes */ for (i=0;i<nr;i++) { for (j=0;j<dim3;j++) { for (k=0;k<dim2;k++) { max[i][k]=-1; maxM=0.0; for (l=0;l<dim1;l++) { re=fabs(d->data[i][j][k*dim1+l][0]); im=fabs(d->data[i][j][k*dim1+l][1]); M2=re*re+im*im; if ((M2 > maxM) && (M2 > 4*d->noise.M2[i])) { maxM=M2; max[i][k]=l; } } } } } oddcount=0; oddmax=0.0; evencount=0; evenmax=0.0; nseg=(int)*val("nseg",&d->p); nnav=(int)*val("nnav",&d->p); etl=(int)*val("etl",&d->p); altread=spar(d,"altread","y"); /* alternating read gradient for alternate segments ... */ if (nseg<2) altread=FALSE; /* ... only if nseg>1 ... */ if (nseg%2==0) altread=FALSE; /* ... and only if nseg is odd */ for (n=0;n<nseg;n++) { ix=n*(nnav+etl); for (i=0;i<nr;i++) { echo=0; if (altread && n%2) echo=1; for (k=0;k<nnav;k++) { if (max[i][k] > -1) { if (echo%2==flipLine) { oddmax += max[i][ix+k]; oddcount++; } else { evenmax += max[i][ix+k]; evencount++; } } echo++; } /* Skip the steady state readout gradients */ echo += steadyStates; for (k=nnav;k<etl;k++) { if (max[i][k] > -1) { if (echo%2==flipLine) { oddmax += max[i][ix+k]; oddcount++; } else { evenmax += max[i][ix+k]; evencount++; } } echo++; } } } /* end nseg loop */ oddmax /=oddcount; evenmax /=evencount; for (i=0;i<nr;i++) free(max[i]); free(max); sw=*val("sw",&d->p); tepadjust=0.5e6*(oddmax-evenmax)/sw; strcpy(tepfile,vnmrj_path); strcat(tepfile,"tepadjust"); if ((f_out = fopen(tepfile, "w")) == NULL) { fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__); fprintf(stderr," Unable to write to %s\n",tepfile); fflush(stderr); return; } fprintf(f_out,"tepadjust %f",tepadjust); fclose(f_out); #ifdef DEBUG rtn=gettimeofday(&tp, NULL); t2=(double)tp.tv_sec+(1.e-6)*tp.tv_usec; fprintf(stdout,"\n%s: %s()\n",__FILE__,__FUNCTION__); fprintf(stdout," Magnitude data suggests add %f us to value of tep\n",tepadjust); fprintf(stdout," Result written to file %s\n",tepfile); fflush(stdout); #endif break; default: break; } }