static int micopy(int old_fd, int new_fd) { /* Copy all variable definitions (and global attributes). */ micopy_all_var_defs(old_fd, new_fd, 0, NULL); ncendef(new_fd); micopy_all_var_values(old_fd, new_fd, 0, NULL); return MI_NOERROR; }
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); }
/* ----------------------------- MNI Header ----------------------------------- @NAME : CopyOthers @INPUT : ParentCDF - CDF ID of the parent file ChildCDF - CDF ID of the child file NumExclude - number of variables to exclude from the copy Exclude - list of variable ID's to be excluded TimeStamp - line to add to the history attribute @OUTPUT : @RETURNS : TRUE if successfull FALSE if any of micopy_all_var_defs(), ncendef(), or mi_copy_all_var_values() indicate failure @DESCRIPTION: Copies the definitions and values of all variables except those in the exclusion list. Also calls UpdateHistory() to update the history line. The child file should be in definition mode when CopyOthers() is called; it will be ncendef()'d (put in update mode) before variable values are copied, and left that way on exit. @METHOD : @GLOBALS : @CALLS : UpdateHistory @CREATED : fall 1993, Greg Ward @MODIFIED : ---------------------------------------------------------------------------- */ Boolean CopyOthers (int ParentCDF, int ChildCDF, int NumExclude, int Exclude[], char *TimeStamp) { #ifdef DEBUG printf ("CopyOthers:\n"); printf (" copying variable definitions...\n"); #endif if (micopy_all_var_defs(ParentCDF, ChildCDF, NumExclude, Exclude) == MI_ERROR) { sprintf (ErrMsg, "Error copying variable definitions: %s", NCErrMsg (ncerr, errno)); ncclose (ChildCDF); return (FALSE); } #ifdef DEBUG printf (" updating history...\n"); #endif UpdateHistory (ChildCDF, TimeStamp); #ifdef DEBUG printf (" ncendef'ing and copying variable values...\n"); #endif if (ncendef (ChildCDF) == MI_ERROR) { sprintf (ErrMsg, "Error updating file (ncendef): %s", NCErrMsg (ncerr, errno)); ncclose (ChildCDF); return (FALSE); } if (micopy_all_var_values(ParentCDF, ChildCDF, NumExclude, Exclude) == MI_ERROR) { sprintf (ErrMsg, "Error copying variable values: %s", NCErrMsg (ncerr, errno)); ncclose (ChildCDF); return (FALSE); } return (TRUE); } /* CopyOthers () */
/* ----------------------------- MNI Header ----------------------------------- @NAME : setup_variables @INPUT : inmincid - id of input minc file (MI_ERROR if no file) mincid - id of output minc file volume_info - volume information arg_string - string giving argument list @OUTPUT : (nothing) @RETURNS : (nothing) @DESCRIPTION: Routine to set up variables in the output minc file @METHOD : @GLOBALS : @CALLS : @CREATED : August 26, 1993 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ static void setup_variables(int inmincid, int mincid, Volume_Info *volume_info, char *arg_string) { int dim[MAX_VAR_DIMS], ndims, idim, varid; int excluded_vars[10], nexcluded; /* Create the dimensions */ ndims = NUMBER_OF_DIMENSIONS; dim[0] = ncdimdef(mincid, volume_info->dimension_names[0], volume_info->nslices); dim[1] = ncdimdef(mincid, volume_info->dimension_names[1], volume_info->nrows); dim[2] = ncdimdef(mincid, volume_info->dimension_names[2], volume_info->ncolumns); /* If an input file is provided, copy all header info from that file except image, image-max, image-min */ if (inmincid != MI_ERROR) { /* Look for the image variable and the image-max/min variables so that we can exclude them from the copy */ nexcluded = 0; excluded_vars[nexcluded] = ncvarid(inmincid, MIimage); if (excluded_vars[nexcluded] != MI_ERROR) nexcluded++; excluded_vars[nexcluded] = ncvarid(inmincid, MIimagemax); if (excluded_vars[nexcluded] != MI_ERROR) nexcluded++; excluded_vars[nexcluded] = ncvarid(inmincid, MIimagemin); if (excluded_vars[nexcluded] != MI_ERROR) nexcluded++; /* Copy the variable definitions */ (void) micopy_all_var_defs(inmincid, mincid, nexcluded, excluded_vars); } /* Set up the dimension variables. If the variable doesn't exist, create it (either no input file or variable did not exist in it). If the dimensions are not standard, then no variable is created. */ for (idim=0; idim < ndims; idim++) { ncopts = 0; varid = ncvarid(mincid, volume_info->dimension_names[idim]); if (varid == MI_ERROR) { varid = micreate_std_variable(mincid, volume_info->dimension_names[idim], NC_INT, 0, NULL); } ncopts = NC_OPTS_VAL; if (varid != MI_ERROR) { (void) miattputdbl(mincid, varid, MIstep, volume_info->step[idim]); (void) miattputdbl(mincid, varid, MIstart, volume_info->start[idim]); } } /* Create the image, image-max and image-min variables */ setup_image_variables(inmincid, mincid, ndims, dim); /* Add the time stamp to the history */ update_history(mincid, arg_string); /* Put the file in data mode */ (void) ncendef(mincid); /* Copy over variable values */ if (inmincid != MI_ERROR) { (void) micopy_all_var_values(inmincid, mincid, nexcluded, excluded_vars); } }