Beispiel #1
0
void
copy_init(struct conversion_info *ci_ptr)
{
    ci_ptr->frame_buffer = malloc(ci_ptr->frame_nbytes);
    if (ci_ptr->frame_buffer == NULL) {
        message(MSG_FATAL, "Out of memory\n");
        exit(-1);
    }

    /* Create the image, imagemax, and imagemin variables.
     */
    micreate_std_variable(ci_ptr->mnc_fd, MIimagemax, NC_DOUBLE,
                          1, ci_ptr->dim_ids);
    micreate_std_variable(ci_ptr->mnc_fd, MIimagemin, NC_DOUBLE,
                          1, ci_ptr->dim_ids);
    micreate_std_variable(ci_ptr->mnc_fd, MIimage, ci_ptr->minc_type,
                          ci_ptr->dim_count + 1, ci_ptr->dim_ids);

    /* Set up the dimension step and start values.  Because of the microPET
     * data orientation, we set Z and Y to be the inverse of the norm, to 
     * put the animal's nose at the top of the display.
     * TODO: allow this behavior to be controlled on the command line.
     */
    miattputdbl(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_Z]), 
                MIstep, -ci_ptr->dim_steps[DIM_Z]);
    miattputdbl(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_X]), 
                MIstep, ci_ptr->dim_steps[DIM_X]);
    miattputdbl(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_Y]), 
                MIstep, -ci_ptr->dim_steps[DIM_Y]);

    miattputdbl(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_Z]), 
                MIstart, ci_ptr->dim_steps[DIM_Z] * ci_ptr->dim_lengths[DIM_Z]);
    miattputdbl(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_X]), 
                MIstart, 0.0);
    miattputdbl(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_Y]), 
                MIstart, ci_ptr->dim_steps[DIM_Y] * ci_ptr->dim_lengths[DIM_Y]);

    miattputstr(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_Z]), 
                MIunits, "mm");
    miattputstr(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_X]), 
                MIunits, "mm");
    miattputstr(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_Y]), 
                MIunits, "mm");
    miattputstr(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, _dimnames[DIM_T]), 
                MIunits, "s");

    ncendef(ci_ptr->mnc_fd);
}
Beispiel #2
0
MNCAPI int 
minc_save_done(int fd)
{
    miattputstr(fd, ncvarid(fd, MIimage), MIcomplete, MI_TRUE);
    miclose(fd);
    return (MINC_STATUS_OK);
}
/* ----------------------------- MNI Header -----------------------------------
@NAME       : update_history
@INPUT      : mincid - id of output minc file
              arg_string - string giving list of arguments
@OUTPUT     : (nothing)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to update the history global variable in the output 
              minc file
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : August 26, 1993 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
static void update_history(int mincid, char *arg_string)
{
   nc_type datatype;
   int att_length;
   char *string;

   /* Get the history attribute length */
   ncopts=0;
   if ((ncattinq(mincid, NC_GLOBAL, MIhistory, &datatype,
                 &att_length) == MI_ERROR) ||
       (datatype != NC_CHAR))
      att_length = 0;
   att_length += strlen(arg_string) + 1;

   /* Allocate a string and get the old history */
   string = malloc(att_length);
   string[0] = '\0';
   (void) miattgetstr(mincid, NC_GLOBAL, MIhistory, att_length, 
                      string);
   ncopts = NC_OPTS_VAL;

   /* Add the new command and put the new history. */
   (void) strcat(string, arg_string);
   (void) miattputstr(mincid, NC_GLOBAL, MIhistory, string);
   free(string);

}
/* ----------------------------- MNI Header -----------------------------------
@NAME       : setup_image_variables
@INPUT      : inmincid - id of input minc file (MI_ERROR if no file)
              mincid - id of output minc file
              ndims - number of dimensions
              dim - list of dimension ids
@OUTPUT     : (nothing)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to set up image, image-max and image-min variables 
              in the output minc file
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : August 26, 1993 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
static void setup_image_variables(int inmincid, int mincid, 
                                  int ndims, int dim[])
{
   int imgid, maxid, minid;
   static double valid_range[2] = {0.0, 255.0};

   /* Create the image max and min variables (varying over slices) */
   maxid = micreate_std_variable(mincid, MIimagemax, NC_DOUBLE, 1, dim);
   minid = micreate_std_variable(mincid, MIimagemin, NC_DOUBLE, 1, dim);
   if (inmincid != MI_ERROR) {
      (void) micopy_all_atts(inmincid, ncvarid(inmincid, MIimagemax),
                             mincid, maxid);
      (void) micopy_all_atts(inmincid, ncvarid(inmincid, MIimagemin),
                             mincid, minid);
   }

   /* Create the image variable, copy attributes, set the signtype attribute,
      set the valid range attribute and delete valid max/min attributes */
   imgid = micreate_std_variable(mincid, MIimage, NC_BYTE, ndims, dim);
   if (inmincid != MI_ERROR) {
      (void) micopy_all_atts(inmincid, ncvarid(inmincid, MIimage),
                             mincid, imgid);
      ncopts = 0;
      (void) ncattdel(mincid, imgid, MIvalid_max);
      (void) ncattdel(mincid, imgid, MIvalid_min);
      ncopts = NC_OPTS_VAL;
   }
   (void) miattputstr(mincid, imgid, MIsigntype, MI_UNSIGNED);
   (void) miset_valid_range(mincid, imgid, valid_range);

}
Beispiel #5
0
/* ----------------------------- MNI Header -----------------------------------@NAME       : CreateImageVars
@INPUT      : CDF - ID of the MINC file in which to create MIimagemax
                    and MIimagemin variables
              NumDim - *total* number of image dimensions (2,3, or 4)
              DimIDs - ID's of the NumDim image dimensions
	      NCType - type of the image variable
	      Signed - TRUE or FALSE, for the image variable
	      ValidRange - for the image variable
@OUTPUT     : 
@RETURNS    : TRUE on success
              FALSE if any error creating either variable
              (sets ErrMsg on error)
@DESCRIPTION: Create the MIimagemax and MIimagemin variables in a newly
              created MINC file (must be in definition mode!).  The 
              variables will depend on the two lowest (slowest-varying) 
              image dimensions, ie. frames and slices in the full 4-D
              case.  If the file has no frames or no slices (or both),
              that will be handled properly.
@METHOD     : 
@GLOBALS    : ErrMsg
@CALLS      : MINC library
@CREATED    : 93-10-28, Greg Ward: code moved from main()
@MODIFIED   : 93-11-10, GPW: renamed and modified from CreateMinMax
---------------------------------------------------------------------------- */
Boolean CreateImageVars (int CDF, int NumDim, int DimIDs[], 
			 nc_type NCType, Boolean Signed, double ValidRange[])
{
   int  image_id;
   int  max_id, min_id;         /* ID's of the newly-created variables */

#ifdef DEBUG
   printf ("CreateImageVars:\n");
   printf (" Creating MIimage variable with %d dimensions\n", NumDim);
#endif

   image_id = micreate_std_variable (CDF, MIimage, NCType, NumDim, DimIDs);
   (void) miattputstr (CDF, image_id, MIsigntype, MI_SIGN_STR(Signed));
   (void) miattputstr (CDF, image_id, MIcomplete, MI_FALSE);
   
   (void) ncattput (CDF, image_id, MIvalid_range, NC_DOUBLE, 2, ValidRange);

   /*
    * Create the image-max and image-min variables.  They should be
    * dependent on the "non-image" dimensions (ie. time and slices,
    * if they exist), so pass NumDim-2 as the number of
    * dimensions, and DimIDs as the list of dimension ID's -- 
    * micreate_std_variable should then only look at the first one
    * or two dimension IDs in the list.
    */

#ifdef DEBUG
   printf (" creating MIimagemin and MIimagemax with %d dimensions\n",
           NumDim-2);
#endif
   
   max_id = micreate_std_variable (CDF, MIimagemax, NC_DOUBLE,
                                   NumDim-2, DimIDs);
   min_id = micreate_std_variable (CDF, MIimagemin, NC_DOUBLE,
                                   NumDim-2, DimIDs);
   
   if ((max_id == MI_ERROR) || (min_id == MI_ERROR))
   {  
      sprintf (ErrMsg, "Error creating image max/min variables: %s\n",
               NCErrMsg (ncerr, errno));
      return (FALSE);
   }

   return (TRUE);

}     /* CreateImageVars () */
Beispiel #6
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : DoneBloodCDF
@INPUT      : file_CDF -> A handle for the open netCDF file.
@OUTPUT     : none
@RETURNS    : void
@DESCRIPTION: Sets the complete attribute of the blood data root variable
              to true.  This indicates that the file contains complete
	      information.
@METHOD     : none
@GLOBALS    : none
@CALLS      : netCDF library
              MINC library
@CREATED    : June 4, 1993 by MW
@MODIFIED   :
---------------------------------------------------------------------------- */
void DoneBloodCDF (int file_CDF)
{
    int parent_id;

    ncredef (file_CDF);
    parent_id = ncvarid (file_CDF, MIbloodroot);
    (void) miattputstr (file_CDF, parent_id, MIbloodcomplete, "true_");
    ncendef (file_CDF);
}
Beispiel #7
0
int main()
{
   int cdf, cdf2;
   int img;
   int dim[MAX_VAR_DIMS];
   int dim2[MAX_VAR_DIMS];
   long start[MAX_VAR_DIMS];
   long count[MAX_VAR_DIMS];
   double image[256*256];
   int i, j, k, ioff;
   char filename[256];
   char filename2[256];

   set_ncopts(NC_VERBOSE|NC_FATAL);
   snprintf(filename, sizeof(filename), "test_minc-%d.mnc", getpid());
   snprintf(filename2, sizeof(filename2), "test_minc2-%d.mnc", getpid());
   cdf=micreate(filename, NC_CLOBBER);
   count[2]=5;
   count[1]=3;
   count[0]=7;
   dim[2]=ncdimdef(cdf, MIzspace, count[2]);
   dim[1]=ncdimdef(cdf, MIxspace, count[1]);
   dim[0]=ncdimdef(cdf, MIyspace, count[0]);
   dim2[0]=ncdimdef(cdf, MItime, NC_UNLIMITED);
   dim2[1]=dim[0];
   dim2[2]=dim[1];
   img=ncvardef(cdf, MIimage, NC_SHORT, 3, dim);
   (void) ncvardef(cdf, "testvar", NC_FLOAT, 2, dim2);
   (void) miattputstr(cdf, img, MIsigntype, MI_SIGNED);
   for (j=0; j<count[0]; j++) {
      for (i=0; i<count[1]; i++) {
         ioff=(j*count[1]+i)*count[2];
         for (k=0; k<count[2]; k++)
            image[ioff+k]=ioff+k+10;
      }
   }
   cdf2=micreate(filename2,NC_CLOBBER);
   (void) ncdimdef(cdf2, "junkdim", NC_UNLIMITED);
   (void) micopy_all_var_defs(cdf, cdf2, 1, &img);
   (void) ncendef(cdf2);
   (void) ncendef(cdf);
   (void) miset_coords(3,0L,start);
   (void) mivarput(cdf, img, start, count, NC_DOUBLE, NULL, image);

   (void) micopy_all_var_values(cdf, cdf2, 1, &img);
   (void) miclose(cdf2);
   (void) miclose(cdf);
   unlink(filename);
   unlink(filename2);
   return(0);
}
Beispiel #8
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : WriteImages
@INPUT      : TempFile - where the images come from
              Image - where they're going
              Slices - vector containing list of slices to write
              Frames - vector containing list of frames to write
              NumSlices - the number of elements of Slices[] actually used
              NumFrames - the number of elements of Frames[] actually used
@OUTPUT     : 
@RETURNS    : an error code as defined in mierrors.h
              ERR_NONE = all went well
              ERR_IN_TEMP = ran out of data reading the temp file
              ERR_OUT_MINC = some problem writing to MINC file
                (this should not happen!!!)
              also sets ErrMsg in the event of an error
@DESCRIPTION: Reads images sequentially from TempFile, and writes them into
              the image variable specified by *Image at the slice/frame
              locations specified by Slices[] and Frames[].  Smart enough
              to handle files with no time dimension, but assumes there
              is always a z dimension.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : 93-6-3, Greg Ward
@MODIFIED   : 
---------------------------------------------------------------------------- */
int WriteImages (FILE *TempFile,
                 ImageInfoRec *Image,
                 long Slices[], 
                 long Frames[],
                 long NumSlices,
                 long NumFrames)
{
   int      slice, frame;
   long     Start [MAX_NC_DIMS], Count [MAX_NC_DIMS];
   double   *Buffer;
   Boolean  DoFrames;
   Boolean  DoSlices;
   Boolean  Success;
   int      RetVal;

   Buffer = (double *) calloc (Image->ImageSize, sizeof (double));

   /*
    * First ensure that we will always read an *entire* image, but only
    * one slice/frame at a time (no matter how many slices/frames we
    * may be reading)
    */

   Start [Image->HeightDim] = 0; Count [Image->HeightDim] = Image->Height;
   Start [Image->WidthDim] = 0;  Count [Image->WidthDim] = Image->Width;

   /*
    * Handle files with missing frames or slices.  See the function
    * ReadImages in the file mireadimages.c for a detailed explanation.
    */

   if (NumFrames > 0)
   {
      Count [Image->FrameDim] = 1;
      DoFrames = TRUE;
   }
   else
   {
      DoFrames = FALSE;
      NumFrames = 1;
   }

   /* Exact same code as for frames, but changed to slices */

   if (NumSlices > 0)
   {
      Count [Image->SliceDim] = 1;
      DoSlices = TRUE;
   }
   else
   {
      DoSlices = FALSE;
      NumSlices = 1;
   }

#ifdef DEBUG
   printf ("Ready to start writing.\n");
   printf ("NumFrames = %ld, DoFrames = %d\n", NumFrames, (int) DoFrames);
   printf ("NumSlices = %ld, DoSlices = %d\n", NumSlices, (int) DoSlices);
#endif

  
   for (slice = 0; slice < NumSlices; slice++)
   {
      if (DoSlices)
      {
         Start [Image->SliceDim] = Slices [slice];
      }

      /* 
       * Loop through all frames, reading/writing one image each time.
       * Note that NumFrames will be one even if DoFrames is false; 
       * so this loop WILL always execute, but it and the functions it calls
       * (particularly PutMaxMin) act slightly differently depending on
       * the value of DoFrames,
       */

      for (frame = 0; frame < NumFrames; frame++)
      {
         Success = ReadNextImage (Buffer, Image->ImageSize, TempFile);
         if (!Success)
         {
            sprintf (ErrMsg, 
                    "Error reading from temporary file at slice %d, frame %d", 
                    slice, frame);
            return (ERR_IN_TEMP);
         }

         PutMaxMin (Image, Buffer,
                    Slices [slice], Frames [frame],
                    DoSlices, DoFrames);

         if (DoFrames)
         {
            Start [Image->FrameDim] = Frames [frame];
         }

         RetVal = miicv_put (Image->ICV, Start, Count, Buffer);
         if (RetVal == MI_ERROR)
         {
            sprintf (ErrMsg, "INTERNAL BUG: Fail on miicv_put: Error code %d",
                     ncerr);
            return (ERR_OUT_MINC);
         }
      }     /* for frame */
   }     /* for slice */

   /*
    * Use the MIcomplete attribute to signal that we are done writing
    */
   miattputstr (Image->CDF, Image->ID, MIcomplete, MI_TRUE);
   free (Buffer);
   return (ERR_NONE);

}     /* WriteImages */
Beispiel #9
0
int main(int argc, char **argv)
{
   int cdf;
   int img, img2;
   int dim[MAX_VAR_DIMS];
   long start[MAX_VAR_DIMS];
   long count[MAX_VAR_DIMS];
   double image[256*256];
   int i, itype, jtype;
   int cflag = 0;
   char filename[256];

#if MINC2
   if (argc == 2 && !strcmp(argv[1], "-2")) {
       cflag = MI2_CREATE_V2;
   }
#endif /* MINC2 */

   snprintf(filename, sizeof(filename), "test_minc_types-%d.mnc", getpid());

   ncopts=NC_VERBOSE|NC_FATAL;
   for (itype=0; itype<ntypes; itype++) {
      for (jtype=0; jtype<ntypes; jtype++) {
         cdf=micreate(filename, NC_CLOBBER | cflag);
         count[2]=256;
         count[1]=20;
         count[0]=7;
         dim[2]=ncdimdef(cdf, MIzspace, count[2]);
         dim[1]=ncdimdef(cdf, MIxspace, count[1]);
         dim[0]=ncdimdef(cdf, MIyspace, count[0]);
         img=ncvardef(cdf, MIimage, types[itype].type, 1, dim);
         (void) miattputstr(cdf, img, MIsigntype, types[itype].sign);
         img2=ncvardef(cdf, "image2", types[jtype].type, 1, dim);
         (void) miattputstr(cdf, img2, MIsigntype, types[jtype].sign);
         image[0]=10.0;
         image[1]=2.0*(-FLT_MAX);
         image[2]=2.0*FLT_MAX;
         image[3]=3.2;
         image[4]=3.7;
         image[5]=(-3.2);
         image[6]=(-3.7);
#if 0
   for (j=0; j<count[0]; j++) {
      for (i=0; i<count[1]; i++) {
         ioff=(j*count[1]+i)*count[2];
         for (k=0; k<count[2]; k++)
            image[ioff+k]=ioff+k+10;
      }
   }
#endif
         (void) ncendef(cdf);
         (void) miset_coords(3,0L,start);
         (void) mivarput(cdf, img, start, count, NC_DOUBLE, NULL, image);

         (void) mivarget(cdf, img, start, count, types[jtype].type,
                         types[jtype].sign, image);
         (void) mivarput(cdf, img2, start, count, types[jtype].type,
                         types[jtype].sign, image);

         (void) mivarget(cdf, img2, start, count, NC_DOUBLE, NULL, image);
         (void) printf("Image 1 : %s %s\n",types[itype].sign,
                       types[itype].ctype);
         (void) printf("Image 2 : %s %s\n",types[jtype].sign,
                       types[jtype].ctype);
         for (i=0;i<count[0];i++)
            (void) printf("   image[%d] = %g\n",(int) i, image[i]);
         (void) miclose(cdf);
      }
   }
   unlink(filename);
   return(0);
}
Beispiel #10
0
MNCAPI int
minc_save_start(char *path,     /* Path to the file */
                int filetype,   /* Date type as stored in the file */
                long ct,        /* Total length of time axis, in voxels */
                long cz,        /* Total length of Z axis, in voxels */
                long cy,        /* Total length of Y axis, in voxels */
                long cx,        /* Total length of X axis, in voxels */
                double dt,      /* Sample width along time axis, in seconds */
                double dz,      /* Sample width along Z axis, in mm */
                double dy,      /* Sample width along Y axis, in mm */
                double dx,      /* Sample width along X axis, in mm */
                void *infoptr,  /* Opaque file structure information */
                const char *history) /* New history information */
{
    int fd;                     /* MINC file descriptor */
    int dim_id[MI_S_NDIMS];     /* netCDF dimension ID array */
    int var_ndims;              /* Number of dimensions per variable */
    int var_dims[MI_S_NDIMS];   /* Dimension ID's per variable */
    int i, j;                   /* Generic loop counters */
    int old_ncopts;             /* For supressing fatal error messages */
    struct file_info *p_file;   /* For accessing the file structure */
    struct var_info *p_var;
    struct att_info *p_att;
    int var_id;                 /* netCDF ID for variable */
    char *signstr;
    nc_type nctype;

    old_ncopts =get_ncopts();
    set_ncopts(0);

    fd = micreate(path, NC_CLOBBER);

    set_ncopts(old_ncopts);
    
    if (fd < 0) {
        return (MINC_STATUS_ERROR);
    }

    if (ct > 0) {
        dim_id[MI_S_T] = ncdimdef(fd, MItime, ct);
        micreate_std_variable(fd, MItime, NC_INT, 0, NULL);
        if (dt > 0.0) {
            miattputdbl(fd, ncvarid(fd, MItime), MIstep, dt);
        }
    }
    else {
        dim_id[MI_S_T] = -1;
    }

    if (cz > 0) {
        dim_id[MI_S_Z] = ncdimdef(fd, MIzspace, cz);
        micreate_std_variable(fd, MIzspace, NC_INT, 0, NULL);
        if (dz > 0.0) {
            miattputdbl(fd, ncvarid(fd, MIzspace), MIstep, dz);
        }
    }
    else {
        dim_id[MI_S_Z] = -1;
    }

    if (cy > 0) {
        dim_id[MI_S_Y] = ncdimdef(fd, MIyspace, cy);
        micreate_std_variable(fd, MIyspace, NC_INT, 0, NULL);
        if (dy > 0.0) {
            miattputdbl(fd, ncvarid(fd, MIyspace), MIstep, dy);
        }
    }
    else {
        return (MINC_STATUS_ERROR); /* Must define Y */
    }

    if (cx > 0) {
        dim_id[MI_S_X] = ncdimdef(fd, MIxspace, cx);
        micreate_std_variable(fd, MIxspace, NC_INT, 0, NULL);
        if (dx > 0.0) {
            miattputdbl(fd, ncvarid(fd, MIxspace), MIstep, dx);
        }
    }
    else {
        return (MINC_STATUS_ERROR); /* Must define X */
    }
    

    /* The var_dims[] array is the array of actual dimension ID's to
     * be used in defining the image variables.  Here I set it up by
     * copying all valid dimension ID's from the dim_id[] array.
     */
    var_ndims = 0;
    for (i = 0; i < MI_S_NDIMS; i++) {
        if (dim_id[i] >= 0) {
            var_dims[var_ndims] = dim_id[i];
            var_ndims++;
        }
    }

    minc_simple_to_nc_type(filetype, &nctype, &signstr);

    /* Create the image variable with the standard
     * dimension order, and the same type as the template
     * file.
     */
    micreate_std_variable(fd, MIimage, nctype, var_ndims, var_dims);
    micreate_std_variable(fd, MIimagemin, NC_DOUBLE, 1, var_dims);
    micreate_std_variable(fd, MIimagemax, NC_DOUBLE, 1, var_dims);

    /* Copy information from the infoptr to the output. 
     */
    if ((p_file = infoptr) != NULL) {
        old_ncopts =get_ncopts();
        set_ncopts(0);

        for (i = 0; i < p_file->file_natts; i++) {
            p_att = &p_file->file_atts[i];
            if (strcmp(p_att->att_name, "ident") != 0) {
                ncattput(fd, NC_GLOBAL, p_att->att_name, p_att->att_type, 
                         p_att->att_len, p_att->att_val);
            }
        }

        for (i = 0; i < p_file->file_nvars; i++) {
            p_var = &p_file->file_vars[i];

            if ((var_id = ncvarid(fd, p_var->var_name)) < 0) {
                var_id = ncvardef(fd, p_var->var_name, p_var->var_type, 
                                  p_var->var_ndims, p_var->var_dims);
            }
            
            for (j = 0; j < p_var->var_natts; j++) {
                p_att = &p_var->var_atts[j];
                ncattput(fd, var_id, p_att->att_name, p_att->att_type,
                         p_att->att_len, p_att->att_val);
            }
        }
        
        set_ncopts(old_ncopts);
    }

    miattputstr(fd, ncvarid(fd, MIimage), MIcomplete, MI_FALSE);
    miattputstr(fd, ncvarid(fd, MIimage), MIsigntype, signstr);

    miappend_history(fd, history);
    ncendef(fd);
    return fd;
}
Beispiel #11
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : CreateBloodCDF
@INPUT      :
@OUTPUT     :
@RETURNS    :
@DESCRIPTION:
@METHOD     :
@GLOBALS    :
@CALLS      :
@CREATED    :
@MODIFIED   :
---------------------------------------------------------------------------- */
int CreateBloodCDF (char name[], header *cnt_header)
{
    int file_CDF;
    int dim_id[1];
    int parent_id;
    int sample_start_id;
    int sample_stop_id;
    int sample_length_id;
    int count_start_id;
    int count_length_id;
    int counts_id;
    int empty_weight_id;
    int full_weight_id;
    int corrected_activity_id;
    int activity_id;

    file_CDF = nccreate (name, NC_CLOBBER);
    if (file_CDF == MI_ERROR)
    {
        return (file_CDF);
    }


    /* Create the dimension */

    dim_id[0] = ncdimdef (file_CDF, "sample", (long)cnt_header->num_samples);

    /* Create the variables */

    sample_start_id  = ncvardef (file_CDF, MIsamplestart, NC_DOUBLE, 1, dim_id);
    sample_stop_id   = ncvardef (file_CDF, MIsamplestop, NC_DOUBLE, 1, dim_id);
    sample_length_id = ncvardef (file_CDF, MIsamplelength, NC_DOUBLE, 1, dim_id);
    count_start_id   = ncvardef (file_CDF, MIcountstart, NC_DOUBLE, 1, dim_id);
    count_length_id  = ncvardef (file_CDF, MIcountlength, NC_DOUBLE, 1, dim_id);
    counts_id        = ncvardef (file_CDF, MIcounts, NC_DOUBLE, 1, dim_id);
    empty_weight_id  = ncvardef (file_CDF, MIemptyweight, NC_DOUBLE, 1, dim_id);
    full_weight_id   = ncvardef (file_CDF, MIfullweight, NC_DOUBLE, 1, dim_id);
    corrected_activity_id = ncvardef (file_CDF, MIcorrectedactivity, NC_DOUBLE,
                                      1, dim_id);
    activity_id      = ncvardef (file_CDF, MIactivity, NC_DOUBLE, 1, dim_id);

    /* Create variable attributes */

    miattputstr (file_CDF, sample_start_id, "units", "seconds");
    miattputstr (file_CDF, sample_stop_id, "units", "seconds");
    miattputstr (file_CDF, sample_length_id, "units", "seconds");
    miattputstr (file_CDF, count_start_id, "units", "seconds");
    miattputstr (file_CDF, count_length_id, "units", "seconds");
    miattputstr (file_CDF, counts_id, "units", "counts");
    miattputstr (file_CDF, empty_weight_id, "units", "grams");
    miattputstr (file_CDF, full_weight_id, "units", "grams");
    miattputstr (file_CDF, corrected_activity_id, "units", "Bq/gram");
    miattputstr (file_CDF, activity_id, "units", "Bq");

    /* Make a root for the blood analysis info */

    parent_id = ncvardef (file_CDF, MIbloodroot, NC_LONG, 0, NULL);
    (void) miattputstr (file_CDF, parent_id, MIbloodname,
                        cnt_header->patient_name);
    (void) ncattput (file_CDF, parent_id, MIbloodrunnumber, NC_LONG, 1,
                     &(cnt_header->run_number));
    (void) miattputstr (file_CDF, parent_id, MIbloodstarttime,
                        cnt_header->start_time);
    (void) miattputstr (file_CDF, parent_id, MIblooddate, cnt_header->date);
    (void) miattputstr (file_CDF, parent_id, MIbloodisotope,
                        cnt_header->isotope);
    (void) miattputstr (file_CDF, parent_id, MIbloodstudytype,
                        cnt_header->study_type);
    (void) miattputint (file_CDF, parent_id, MIbloodbackground,
                        cnt_header->background);
    (void) miattputstr (file_CDF, parent_id, MIbloodcomplete, "false");

    /* Set up the hierarchy */

    (void) miadd_child (file_CDF, parent_id, sample_start_id);
    (void) miadd_child (file_CDF, parent_id, sample_stop_id);
    (void) miadd_child (file_CDF, parent_id, sample_length_id);
    (void) miadd_child (file_CDF, parent_id, count_start_id);
    (void) miadd_child (file_CDF, parent_id, count_length_id);
    (void) miadd_child (file_CDF, parent_id, counts_id);
    (void) miadd_child (file_CDF, parent_id, empty_weight_id);
    (void) miadd_child (file_CDF, parent_id, full_weight_id);
    (void) miadd_child (file_CDF, parent_id, corrected_activity_id);
    (void) miadd_child (file_CDF, parent_id, activity_id);

    /* End definition mode */

    ncendef (file_CDF);

    return (file_CDF);
}
Beispiel #12
0
int
upet_to_minc(char *hdr_fname, char *img_fname, char *out_fname, 
             char *prog_name)
{
    char *line_ptr;
    char line_buf[1024];
    char *val_ptr;
    int in_header;
    double dbl_tmp;
    int int_tmp;
    struct conversion_info ci;
    struct keywd_entry *ke_ptr;
    int is_known;
    char *argv_tmp[5];
    char *out_history;

    ci.hdr_fp = fopen(hdr_fname, "r"); /* Text file */
    if (ci.hdr_fp == NULL) {
        perror(hdr_fname);
        return (-1);
    }

    ci.img_fp = fopen(img_fname, "rb"); /* Binary file */
    if (ci.img_fp == NULL) {
        perror(img_fname);
        return (-1);
    }

    ci.mnc_fd = micreate(out_fname, NC_NOCLOBBER);
    if (ci.mnc_fd < 0) {
        perror(out_fname);
        return (-1);
    }

    ci.frame_zero = -1;     /* Initial frame is -1 until set. */

    /* Define the basic MINC group variables.
     */
    micreate_group_variable(ci.mnc_fd, MIstudy);
    micreate_group_variable(ci.mnc_fd, MIacquisition);
    micreate_group_variable(ci.mnc_fd, MIpatient);
    ncvardef(ci.mnc_fd, "micropet", NC_SHORT, 0, NULL);

    /* Fake the history here */
    argv_tmp[0] = prog_name;
    argv_tmp[1] = VERSIONSTR;
    argv_tmp[2] = hdr_fname;
    argv_tmp[3] = img_fname;
    argv_tmp[4] = out_fname;

    out_history = time_stamp(5, argv_tmp);

    miattputstr(ci.mnc_fd, NC_GLOBAL, MIhistory, out_history);
    free(out_history);
        
    in_header = 1;

    ci.frame_nbytes = 1;
    ci.frame_nvoxels = 1;

    /* When we read voxels, we need COMBINED_SCALE_FACTOR() to have a sane
     * value for all modalities. Set defaults for these in case the modality
     * does not define one of these factors. For example, a CT (modality 2)
     * will not define isotope_branching_fraction or calibration_factor.
     */

    ci.scale_factor = 1.0;
    ci.calibration_factor = 1.0;
    ci.isotope_branching_fraction = 1.0;

    /* Collect the headers */
    while (fgets(line_buf, sizeof(line_buf), ci.hdr_fp) != NULL) {
        if (line_buf[0] == '#') /*  */
            continue;
        line_ptr = line_buf;
        while (!isspace(*line_ptr)) {
            line_ptr++;
        }
        *line_ptr++ = '\0';
        val_ptr = line_ptr;
        while (*line_ptr != '\n' && *line_ptr != '\r' && *line_ptr != '\0') {
            line_ptr++;
        }
        *line_ptr = '\0';
            
        is_known = 0;

        if (in_header) {
            if (*val_ptr != '\0') {
                /* Save the raw attribute into the file */
                ncattput(ci.mnc_fd, ncvarid(ci.mnc_fd, "micropet"),
                         line_buf, NC_CHAR, strlen(val_ptr), val_ptr);
            }

            for (ke_ptr = vol_atts; ke_ptr->upet_kwd != NULL; ke_ptr++) {
                if (!strcmp(ke_ptr->upet_kwd, line_buf)) {
                    
                    is_known = 1;

                    if (ke_ptr->func != NULL) {
                        (*ke_ptr->func)(&ci, val_ptr, 
                                        ke_ptr->mnc_var,
                                        ke_ptr->mnc_att);
                    }
                    else if (ke_ptr->mnc_var != NULL &&
                             ke_ptr->mnc_att != NULL) {

                        /* Interpret based upon type */
                        switch (ke_ptr->upet_type) {
                        case UPET_TYPE_INT:
                            int_tmp = atoi(val_ptr);
                            miattputint(ci.mnc_fd, 
                                        ncvarid(ci.mnc_fd, ke_ptr->mnc_var),
                                        ke_ptr->mnc_att,
                                        int_tmp);
                            break;

                        case UPET_TYPE_REAL:
                            dbl_tmp = atof(val_ptr);
                            miattputdbl(ci.mnc_fd, 
                                        ncvarid(ci.mnc_fd, ke_ptr->mnc_var),
                                        ke_ptr->mnc_att,
                                        dbl_tmp);
                            break;

                        case UPET_TYPE_STR:
                            miattputstr(ci.mnc_fd, 
                                        ncvarid(ci.mnc_fd, ke_ptr->mnc_var),
                                        ke_ptr->mnc_att,
                                        val_ptr);
                            break;

                        }
                        
                    }
                    break;
                }
            }
        }
        else {
            /* Not in the header any longer 
             */
            for (ke_ptr = frm_atts; ke_ptr->upet_kwd != NULL; ke_ptr++) {
                if (!strcmp(ke_ptr->upet_kwd, line_buf)) {
                    
                    is_known = 1;
                    
                    if (ke_ptr->func != NULL) {
                        (*ke_ptr->func)(&ci, val_ptr, 
                                        ke_ptr->mnc_var,
                                        ke_ptr->mnc_att);
                    }
                    break;
                }
            }
        }

        if (!is_known) {
            if (!strcmp(line_buf, "end_of_header")) {
                if (in_header) {
                    in_header = 0;

                    copy_init(&ci);

                }
                else {
                    copy_frame(&ci);
                }
            }
            else {
                message(MSG_WARNING, "Unrecognized keyword %s\n", line_buf);
            }
        }
    }

    fclose(ci.hdr_fp);
    fclose(ci.img_fp);
    miclose(ci.mnc_fd);
    return (0);
}