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 : FillImage @INPUT : CDF NumDim - number of image dimensions in the child file DimIDs - list of image dimension IDs Value - value with which to fill the image variable @OUTPUT : @RETURNS : TRUE if successful, FALSE on error @DESCRIPTION: Fills the image variable in the child file with a given value. File must be in data mode. @METHOD : @GLOBALS : @CALLS : @CREATED : 95/6/28, Greg Ward @MODIFIED : ---------------------------------------------------------------------------- */ Boolean FillImage (int CDF, int NumDim, int DimIDs[], double Value) { int i; long start[MAX_NC_DIMS], count[MAX_NC_DIMS]; int image_elt = 1; /* # elements in MIimage variable */ int maxmin_elt = 1; /* # elements in MIimage{max,min} vars */ double *values; int var_id; /* * First compute the total number of elements in the image, and * also make a count vector for passing to mivarput(). */ for (i = 0; i < NumDim; i++) { long dimlength; char dimname [MAX_NC_NAME]; ncdiminq (CDF, DimIDs[i], dimname, &dimlength); if (dimlength > 0) { image_elt *= dimlength; if (i < NumDim-2) maxmin_elt *= dimlength; start[i] = 0; count[i] = dimlength; } else { fprintf (stderr, "Image dimension %s has length %d\n", dimname, dimlength); return (FALSE); } } assert (maxmin_elt <= image_elt); /* * Now allocate and fill a big chunk of memory that will hold * image_elt copies of Value. */ values = (double *) malloc (image_elt * sizeof (double)); for (i = 0; i < image_elt; i++) values[i] = Value; /* Put the values into the image variable in the MINC file */ var_id = ncvarid (CDF, MIimage); if (var_id == MI_ERROR) { fprintf (stderr, "Could not find image variable in %s\n", gChildFile); return (FALSE); } if (mivarput (CDF, var_id, start, count, NC_DOUBLE, NULL, values) == MI_ERROR) { fprintf (stderr, "Error writing image values to %s: %s\n", gChildFile, NCErrMsg(ncerr, errno)); return (FALSE); } /* * Now do the same for the image-max and image-min variables. * Since we've just filled image with a single value, we can use * that same value for all elememts of image-max and image-min. * In fact, since maxmin_elt has to be <= image_elt, we can * just reuse the values array that we've just created. */ var_id = ncvarid (CDF, MIimagemax); mivarput (CDF, var_id, start, count, NC_DOUBLE, MI_SIGNED, values); var_id = ncvarid (CDF, MIimagemin); mivarput (CDF, var_id, start, count, NC_DOUBLE, MI_SIGNED, values); free (values); return (TRUE); }
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); }