/* ----------------------------- MNI Header ----------------------------------- @NAME : minc_convenience.c @DESCRIPTION: File of convenience functions following the minc standard. @METHOD : Routines included in this file : public : miget_datatype miget_default_range miget_valid_range miset_valid_range miget_image_range mivar_exists miattput_pointer miattget_pointer miadd_child micreate_std_variable micreate_group_variable miget_version miappend_history micreate_ident private : MI_create_dim_variable MI_create_dimwidth_variable MI_create_image_variable MI_create_imaxmin_variable MI_verify_maxmin_dims MI_create_root_variable MI_create_simple_variable MI_add_stdgroup MI_is_in_list @CREATED : July 27, 1992. (Peter Neelin, Montreal Neurological Institute) @MODIFIED : * $Log: minc_convenience.c,v $ * Revision 6.18 2004/12/14 23:53:46 bert * Get rid of compilation warnings * * Revision 6.17 2004/12/03 21:52:35 bert * Minor changes for Windows build * * Revision 6.16 2004/10/15 13:46:15 bert * Minor changes for Windows compatibility * * Revision 6.15 2004/08/26 16:14:21 bert * Fix up miappend_history() again * * Revision 6.14 2004/06/04 18:15:46 bert * Added micreate_ident() * * Revision 6.13 2004/04/27 15:49:17 bert * Use new logging, gettext preparation * * Revision 6.12 2004/03/24 20:53:48 bert * Increase att_length by one in miappend_history() in order to read the entire attribute * * Revision 6.11 2004/02/02 18:22:46 bert * Added miget_version() and miappend_history() * * Revision 6.10 2001/12/06 14:09:07 neelin * Corrected return from mivar_exists to use minc macro MI_RETURN so that * ncopts is properly restored. * * Revision 6.9 2001/11/13 14:15:18 neelin * Added functions miget_image_range and mivar_exists * * Revision 6.8 2001/10/17 14:32:20 neelin * Modified miset_valid_range to write out valid_range as double in all * cases except float. Unfortunately, writing out values in a type that * matched the type of the image variable caused problems with programs * linked against old minc libraries. * * Revision 6.7 2001/09/18 15:44:27 neelin * When output type is NC_BYTE, valid_range attribute should have type NC_SHORT. * * Revision 6.6 2001/08/20 13:19:14 neelin * Added function miattget_with_sign to allow the caller to specify the sign * of the input attribute since this information is ambiguous. This is * necessary for the valid_range attribute which should have the same sign * as the image data. Modified miget_valid_range to make use of this function. * * Revision 6.5 2001/08/16 19:24:11 neelin * Fixes to the code handling valid_range values. * * Revision 6.4 2001/08/16 16:41:32 neelin * Added library functions to handle reading of datatype, sign and valid range, * plus writing of valid range and setting of default ranges. These functions * properly handle differences between valid_range type and image type. Such * difference can cause valid data to appear as invalid when double to float * conversion causes rounding in the wrong direction (out of range). * Modified voxel_loop, volume_io and programs to use these functions. * * Revision 6.3 2001/08/16 13:32:18 neelin * Partial fix for valid_range of different type from image (problems * arising from double to float conversion/rounding). NOT COMPLETE. * * Revision 6.2 2001/04/17 18:40:13 neelin * Modifications to work with NetCDF 3.x * In particular, changed NC_LONG to NC_INT (and corresponding longs to ints). * Changed NC_UNSPECIFIED to NC_NAT. * A few fixes to the configure script. * * Revision 6.1 1999/10/19 14:45:09 neelin * Fixed Log subsitutions for CVS * * Revision 6.0 1997/09/12 13:24:54 neelin * Release of minc version 0.6 * * Revision 5.0 1997/08/21 13:25:53 neelin * Release of minc version 0.5 * * Revision 4.0 1997/05/07 20:07:52 neelin * Release of minc version 0.4 * * Revision 3.0 1995/05/15 19:33:12 neelin * Release of minc version 0.3 * * Revision 2.1 1995/02/08 19:01:06 neelin * Moved private function declarations from minc_routines.h to appropriate file. * * Revision 2.0 1994/09/28 10:38:02 neelin * Release of minc version 0.2 * * Revision 1.18 94/09/28 10:37:12 neelin * Pre-release * * Revision 1.17 93/08/11 12:06:19 neelin * Added RCS logging in source. * @COPYRIGHT : Copyright 1993 Peter Neelin, McConnell Brain Imaging Centre, Montreal Neurological Institute, McGill University. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies. The author and McGill University make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. ---------------------------------------------------------------------------- */ #ifndef lint static char rcsid[] = "$Header: /software/source/minc/libsrc/minc_convenience.c,v 6.18 2004/12/14 23:53:46 bert Exp $ MINC (MNI)"; #endif #include "minc_private.h" #include "type_limits.h" #include "minc_varlists.h" #include #if HAVE_UNISTD_H #include /* for getpid() */ #endif /* HAVE_UNISTD_H */ /* Private functions */ PRIVATE int MI_create_dim_variable(int cdfid, char *name, nc_type datatype, int ndims); PRIVATE int MI_create_dimwidth_variable(int cdfid, char *name, nc_type datatype, int ndims); PRIVATE int MI_create_image_variable(int cdfid, char *name, nc_type datatype, int ndims, int dim[]); PRIVATE int MI_create_imaxmin_variable(int cdfid, char *name, nc_type datatype, int ndims, int dim[]); PRIVATE int MI_verify_maxmin_dims(int cdfid, int image_ndims, int image_dim[], int maxmin_ndims, int maxmin_dim[]); PRIVATE int MI_create_root_variable(int cdfid, char *name); PRIVATE int MI_create_simple_variable(int cdfid, char *name); PRIVATE int MI_add_stdgroup(int cdfid, int varid); PRIVATE int MI_is_in_list(char *string, char *list[]); /* ----------------------------- MNI Header ----------------------------------- @NAME : miget_datatype @INPUT : cdfid - cdf file id imgid - image variable id @OUTPUT : datatype is_signed - TRUE if type is signed @RETURNS : MI_ERROR when an error occurs. @DESCRIPTION: Gets the datatype and sign of the image variable. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines. @CREATED : August 15, 2001 @MODIFIED : ---------------------------------------------------------------------------- */ MNCAPI int miget_datatype(int cdfid, int imgid, nc_type *datatype, int *is_signed) { int old_ncopts; int use_default_sign; char attstr[MI_MAX_ATTSTR_LEN]; MI_SAVE_ROUTINE_NAME("miget_datatype"); /* Get the type information for the variable */ if (ncvarinq(cdfid, imgid, NULL, datatype, NULL, NULL, NULL) == MI_ERROR) MI_RETURN(MI_ERROR); /* Save the ncopts value */ old_ncopts = ncopts; ncopts = 0; /* Get the sign information */ if ((miattgetstr(cdfid, imgid, MIsigntype, MI_MAX_ATTSTR_LEN, attstr) != NULL)) { use_default_sign = FALSE; if (strcmp(attstr, MI_SIGNED) == 0) *is_signed = TRUE; else if (strcmp(attstr, MI_UNSIGNED) == 0) *is_signed = FALSE; else use_default_sign = TRUE; } else { use_default_sign = TRUE; } /* Set a default sign if needed */ if (use_default_sign) { if (*datatype == NC_BYTE) *is_signed = FALSE; else *is_signed = TRUE; } /* Restore ncopts */ ncopts = old_ncopts; MI_RETURN(MI_NOERROR); } /* ----------------------------- MNI Header ----------------------------------- @NAME : miget_default_range @INPUT : datatype is_signed - TRUE if type is signed @OUTPUT : default_range - array containing default range for variable @RETURNS : MI_NOERROR @DESCRIPTION: Gets the default range for a data type. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines. @CREATED : August 15, 2001 @MODIFIED : ---------------------------------------------------------------------------- */ MNCAPI int miget_default_range(nc_type datatype, int is_signed, double default_range[]) { MI_SAVE_ROUTINE_NAME("miget_default_range"); switch (datatype) { case NC_INT: default_range[0] = (is_signed) ? INT_MIN : 0; default_range[1] = (is_signed) ? INT_MAX : UINT_MAX; break; case NC_SHORT: default_range[0] = (is_signed) ? SHRT_MIN : 0; default_range[1] = (is_signed) ? SHRT_MAX : USHRT_MAX; break; case NC_BYTE: default_range[0] = (is_signed) ? SCHAR_MIN : 0; default_range[1] = (is_signed) ? SCHAR_MAX : UCHAR_MAX; break; case NC_FLOAT: default_range[0] = -FLT_MAX; default_range[1] = FLT_MAX; break; case NC_DOUBLE: default_range[0] = -DBL_MAX; default_range[1] = DBL_MAX; break; default: default_range[0]= MI_DEFAULT_MIN; default_range[1]= MI_DEFAULT_MAX; break; } MI_RETURN(MI_NOERROR); } /* ----------------------------- MNI Header ----------------------------------- @NAME : miget_valid_range @INPUT : cdfid - cdf file id imgid - image variable id @OUTPUT : valid_range - array containing valid min and max of image @RETURNS : MI_ERROR when an error occurs. @DESCRIPTION: Gets the valid range for an image variable. Ensures that the values are cast to the appropriate type to ensure that they are correctly truncated. This is particularly important for float images. If the range cannot be found, then use the default values. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines. @CREATED : August 15, 2001 @MODIFIED : ---------------------------------------------------------------------------- */ MNCAPI int miget_valid_range(int cdfid, int imgid, double valid_range[]) { int old_ncopts; int status; int length; nc_type datatype; int is_signed; char *att_sign; double temp; MI_SAVE_ROUTINE_NAME("miget_valid_range"); /* Get the type information for the variable */ if (miget_datatype(cdfid, imgid, &datatype, &is_signed) == MI_ERROR) MI_RETURN(MI_ERROR); /* Save the ncopts value */ old_ncopts = ncopts; ncopts = 0; /* Get the sign string for the attribute */ if (is_signed) att_sign = MI_SIGNED; else att_sign = MI_UNSIGNED; /* Get valid range */ status=miattget_with_sign(cdfid, imgid, MIvalid_range, att_sign, NC_DOUBLE, NULL, 2, valid_range, &length); /* If not there, look for the max and min */ if ((status==MI_ERROR) || (length!=2)) { /* Get the default range for the type */ (void) miget_default_range(datatype, is_signed, valid_range); /* Try to read the valid max */ (void) miattget_with_sign(cdfid, imgid, MIvalid_max, att_sign, NC_DOUBLE, NULL, 1, &valid_range[1], NULL); /* Try to read the valid min */ (void) miattget_with_sign(cdfid, imgid, MIvalid_min, att_sign, NC_DOUBLE, NULL, 1, &valid_range[0], NULL); } /* Restore the ncopts value */ ncopts = old_ncopts; /* Make sure that the first element is the minimum */ if (valid_range[1] < valid_range[0]) { temp = valid_range[0]; valid_range[0] = valid_range[1]; valid_range[1] = temp; } /* Cast to the appropriate type and back to make sure that things are rounded/truncated properly. This is only really needed for floats */ switch (datatype) { case NC_INT: case NC_SHORT: case NC_BYTE: if (is_signed) { valid_range[0] = (int) valid_range[0]; valid_range[1] = (int) valid_range[1]; } else { valid_range[0] = (unsigned int) valid_range[0]; valid_range[1] = (unsigned int) valid_range[1]; } break; case NC_FLOAT: valid_range[0] = (float) valid_range[0]; valid_range[1] = (float) valid_range[1]; break; } MI_RETURN(MI_NOERROR); } /* ----------------------------- MNI Header ----------------------------------- @NAME : miset_valid_range @INPUT : cdfid - cdf file id imgid - image variable id valid_range - array containing valid min and max of image @OUTPUT : (none) @RETURNS : MI_ERROR when an error occurs. @DESCRIPTION: Sets the valid range for an image variable. Ensures that the attribute types match the image variable type. This is particularly important for float images because of potential rounding when going from double to float. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines. @CREATED : August 15, 2001 @MODIFIED : ---------------------------------------------------------------------------- */ MNCAPI int miset_valid_range(int cdfid, int imgid, double valid_range[]) { nc_type datatype; int is_signed; int status; char *attname; float fval[2]; MI_SAVE_ROUTINE_NAME("miset_valid_range"); /* Get the type information for the variable */ if (miget_datatype(cdfid, imgid, &datatype, &is_signed) == MI_ERROR) MI_RETURN(MI_ERROR); /* Cast to the appropriate type and save. Originally, it was thought that casting to the type of the image variable would be a good idea because NetCDF says that it should. Unfortunately, this breaks compatibility in some cases with programs linked with old minc libraries, so we only cast for floats to avoid rounding problems (cast from double to float can put values out of range) - for everything else double is used. */ attname = MIvalid_range; switch (datatype) { case NC_FLOAT: fval[0] = valid_range[0]; fval[1] = valid_range[1]; status = ncattput(cdfid, imgid, attname, datatype, 2, fval); break; default: status = ncattput(cdfid, imgid, attname, NC_DOUBLE, 2, valid_range); break; } MI_RETURN(status); } /* ----------------------------- MNI Header ----------------------------------- @NAME : miget_image_range @INPUT : cdfid - cdf file id @OUTPUT : image_range - array containing min and max of image-min/max for the entire file @RETURNS : MI_ERROR when an error occurs. @DESCRIPTION: Gets the image range for a file - that is, the maximum image-max value and the minimum image-min value. For float images, ensures that values are cast to float to ensure that they are correctly truncated. If the range cannot be found, then use the default values for int images and valid_range for floating-point images. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines. @CREATED : October 19, 2001 @MODIFIED : ---------------------------------------------------------------------------- */ MNCAPI int miget_image_range(int cdfid, double image_range[]) { int oldncopts; /* For saving value of ncopt */ int vid[2]; /* Variable ids for min and max */ int imgid; /* Image variable id */ nc_type datatype; /* Type of image variable */ int is_signed; /* Indicates if image variable is signed */ int is_float, no_range_found; /* Flags */ int imm; /* For looping over min and max */ int ndims; /* Number of dimensions of variable */ int idim; /* For looping over dimensions */ int dim[MAX_VAR_DIMS]; /* Dimension ids of variable */ long ientry; /* For stepping through values */ long size; /* Size of min and max variables */ long start[MAX_VAR_DIMS]; /* Start of variable */ long count[MAX_VAR_DIMS]; /* Dimension sizes */ double *buffer; /* Pointer to buffer for min/max values */ MI_SAVE_ROUTINE_NAME("miget_image_range"); /* Set default values for image_range */ image_range[0] = MI_DEFAULT_MIN; image_range[1] = MI_DEFAULT_MAX; /* Get the image-min/max variable ids */ oldncopts=ncopts; ncopts=0; vid[0] = ncvarid(cdfid, MIimagemin); vid[1] = ncvarid(cdfid, MIimagemax); ncopts = oldncopts; /* Get the type information for the image variable */ if ( ((imgid = ncvarid(cdfid, MIimage)) == MI_ERROR) || (miget_datatype(cdfid, imgid, &datatype, &is_signed) == MI_ERROR) ) MI_RETURN(MI_ERROR); /* No max/min variables, so use valid_range values for floats if it is set and defaults otherwise */ if ((vid[0] == MI_ERROR) || (vid[1] == MI_ERROR)) { /* Check for a floating-point type - if it is, try to get the valid_range. If the valid_range was set to full range for the type, then that means that the valid range was probably not set (and if it was, it was not particularly reasonable). */ is_float = (datatype == NC_FLOAT || datatype == NC_DOUBLE); no_range_found = FALSE; if (is_float) { if (miget_valid_range(cdfid, imgid, image_range) == MI_ERROR) MI_RETURN(MI_ERROR); no_range_found = (datatype == NC_FLOAT && image_range[1] == FLT_MAX) || (datatype == NC_DOUBLE && image_range[1] == DBL_MAX); } /* If it is not a float, or if the valid range was not set, then use the default. */ if (!is_float || no_range_found) { image_range[0] = MI_DEFAULT_MIN; image_range[1] = MI_DEFAULT_MAX; } } /* If the variables are there then get the max and min and fastest varying dimension */ else { /* Set initial values */ image_range[0] = DBL_MAX; image_range[1] = -DBL_MAX; /* Loop over min and max */ for (imm=0; imm<2; imm++) { /* Get dimension list */ MI_CHK_ERR(ncvarinq(cdfid, vid[imm], NULL, NULL, &ndims, dim, NULL)) /* Loop through dimensions, getting dimension sizes and total min/max variable size */ size=1; /* Size of MIimagemin/max variable */ for (idim=0; idim0) { if (ncattget(cdfid, parent_varid, MIchildren, child_list) == MI_ERROR) { FREE(child_list); milog_message(MI_MSG_READATTR, MIchildren); MI_RETURN(MI_ERROR); } if (child_list[child_list_size-1] == '\0') child_list_size--; /* Copy the child list element separator (only if there are other elements in the list */ (void) strcpy(&child_list[child_list_size], MI_CHILD_SEPARATOR); child_list_size += strlen(MI_CHILD_SEPARATOR); } /* Get pointer to name of new child */ new_child = &child_list[child_list_size]; /* Add the new child name to the list */ if (ncvarinq(cdfid, child_varid, new_child, NULL, NULL, NULL, NULL) == MI_ERROR) { FREE(child_list); MI_RETURN_ERROR(MI_ERROR); } /* Check for multiple copies of child */ if (strstr(child_list, new_child) != new_child) { child_list_size -= strlen(MI_CHILD_SEPARATOR); child_list[child_list_size] = '\0'; } /* Put the attribute MIchildren */ if (miattputstr(cdfid, parent_varid, MIchildren, child_list) == MI_ERROR) { FREE(child_list); MI_RETURN_ERROR(MI_ERROR); } /* Get the parent variable name */ if (ncvarinq(cdfid, parent_varid, child_list, NULL, NULL, NULL, NULL) == MI_ERROR) { FREE(child_list); MI_RETURN_ERROR(MI_ERROR); } /* Put the attribute MIparent */ if (miattputstr(cdfid, child_varid, MIparent, child_list) == MI_ERROR) { FREE(child_list); MI_RETURN_ERROR(MI_ERROR); } FREE(child_list); MI_RETURN(MI_NOERROR); } /* ----------------------------- MNI Header ----------------------------------- @NAME : micreate_std_variable @INPUT : cdfid - cdf file id name - name of standard variable to create datatype - type of data to store (see ncvardef) ndims - number of dimensions of variable (see ncvardef) dim - vector of variable dimensions (see ncvardef) @OUTPUT : (none) @RETURNS : id of created variable, or MI_ERROR if an error occurs @DESCRIPTION: Creates a standard MINC variable by calling ncvardef and then sets default attributes. The standard variables are identified by name, so an unrecognised name produces an error. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines @CREATED : August 5, 1992 @MODIFIED : ---------------------------------------------------------------------------- */ MNCAPI int micreate_std_variable(int cdfid, char *name, nc_type datatype, int ndims, int dim[]) { int varid; /* Created variable id */ MI_SAVE_ROUTINE_NAME("micreate_std_variable"); /* Check to see if it is a standard dimension */ if (MI_is_in_list(name, dimvarlist)) { MI_CHK_ERR(varid=MI_create_dim_variable(cdfid, name, datatype, ndims)) } /* Check for a dimension width */ else if (MI_is_in_list(name, dimwidthlist)) { MI_CHK_ERR(varid=MI_create_dimwidth_variable(cdfid, name, datatype, ndims)) } /* Check for a standard variable or group */ else if (MI_is_in_list(name, varlist)) { if (STRINGS_EQUAL(name, MIimage)) MI_CHK_ERR(varid=MI_create_image_variable(cdfid, name, datatype, ndims, dim)) else if ((STRINGS_EQUAL(name, MIimagemax)) || (STRINGS_EQUAL(name, MIimagemin))) MI_CHK_ERR(varid=MI_create_imaxmin_variable(cdfid, name, datatype, ndims, dim)) else if (STRINGS_EQUAL(name, MIrootvariable)) MI_CHK_ERR(varid=MI_create_root_variable(cdfid, name)) else if (STRINGS_EQUAL(name, MIpatient)) MI_CHK_ERR(varid=MI_create_simple_variable(cdfid, name)) else if (STRINGS_EQUAL(name, MIstudy)) MI_CHK_ERR(varid=MI_create_simple_variable(cdfid, name)) else if (STRINGS_EQUAL(name, MIacquisition)) MI_CHK_ERR(varid=MI_create_simple_variable(cdfid, name)) else { milog_message(MI_MSG_VARNOTSTD, name); MI_RETURN(MI_ERROR); } } /* If not in any list, then return an error */ else { milog_message(MI_MSG_VARNOTSTD, name); MI_RETURN(MI_ERROR); } MI_RETURN(varid); } /* ----------------------------- MNI Header ----------------------------------- @NAME : MI_create_dim_variable @INPUT : cdfid - cdf file id name - name of standard variable to create datatype - type of data to store ndims - number of dimensions - must be 0 or 1 @OUTPUT : (none) @RETURNS : id of created variable, or MI_ERROR if an error occurs @DESCRIPTION: Creates a standard MINC dimension variable by calling ncvardef and then sets default attributes. The standard variables are identified by name, so an unrecognised name produces an error. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines @CREATED : August 5, 1992 @MODIFIED : ---------------------------------------------------------------------------- */ PRIVATE int MI_create_dim_variable(int cdfid, char *name, nc_type datatype, int ndims) { int dimid; /* Dimension id (for dimensions variables) */ int varid; /* Created variable id */ MI_SAVE_ROUTINE_NAME("MI_create_dim_variable"); /* Check for MIvector_dimension - no associated variable */ if (STRINGS_EQUAL(name, MIvector_dimension)) { milog_message(MI_MSG_VARNOTSTD, name); MI_RETURN(MI_ERROR); } /* Check for ndims being 0 or 1 */ if (ndims>1) { milog_message(MI_MSG_TOOMANYDIMS, 1); MI_RETURN(MI_ERROR); } /* Look for dimension and create the variable */ MI_CHK_ERR(dimid=ncdimid(cdfid, name)) MI_CHK_ERR(varid=ncvardef(cdfid, name, datatype, ndims, &dimid)) /* Standard attributes */ MI_CHK_ERR(miattputstr(cdfid, varid, MIvarid, MI_STDVAR)) MI_CHK_ERR(miattputstr(cdfid, varid, MIvartype, MI_DIMENSION)) MI_CHK_ERR(miattputstr(cdfid, varid, MIversion, MI_CURRENT_VERSION)) /* Add comments for spatial dimensions */ if (STRINGS_EQUAL(name, MIxspace)) {MI_CHK_ERR(miattputstr(cdfid, varid, MIcomments, _("X increases from patient left to right")))} else if (STRINGS_EQUAL(name, MIyspace)) {MI_CHK_ERR(miattputstr(cdfid, varid, MIcomments, _("Y increases from patient posterior to anterior")))} else if (STRINGS_EQUAL(name, MIzspace)) {MI_CHK_ERR(miattputstr(cdfid, varid, MIcomments, _("Z increases from patient inferior to superior")))} /* Dimension attributes */ if (ndims==0) { MI_CHK_ERR(miattputstr(cdfid, varid, MIspacing, MI_REGULAR)) } else { MI_CHK_ERR(miattputstr(cdfid, varid, MIspacing, MI_IRREGULAR)) } if (STRINGS_EQUAL(name, MItime)) MI_CHK_ERR(miattputstr(cdfid, varid, MIalignment, MI_START)) else MI_CHK_ERR(miattputstr(cdfid, varid, MIalignment, MI_CENTRE)) MI_RETURN(varid); } /* ----------------------------- MNI Header ----------------------------------- @NAME : MI_create_dimwidth_variable @INPUT : cdfid - cdf file id name - name of standard variable to create datatype - type of data to store ndims - number of dimensions - must be 0 or 1 @OUTPUT : (none) @RETURNS : id of created variable, or MI_ERROR if an error occurs @DESCRIPTION: Creates a standard MINC dimension width variable by calling ncvardef and then sets default attributes. The standard variables are identified by name, so an unrecognised name produces an error. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines @CREATED : August 5, 1992 @MODIFIED : ---------------------------------------------------------------------------- */ PRIVATE int MI_create_dimwidth_variable(int cdfid, char *name, nc_type datatype, int ndims) { int dimid; /* Dimension id (for dimensions variables) */ int varid; /* Created variable id */ char string[MAX_NC_NAME]; /* String for dimension name */ char *str; MI_SAVE_ROUTINE_NAME("MI_create_dimwidth_variable"); /* Look for dimension name in name (remove width suffix) */ if ((str=strstr(strcpy(string, name),MI_WIDTH_SUFFIX)) == NULL) { milog_message(MI_MSG_DIMWIDTH); MI_RETURN(MI_ERROR); } *str='\0'; /* Check for ndims being 0 or 1 */ if (ndims>1) { milog_message(MI_MSG_TOOMANYDIMS, 1); MI_RETURN(MI_ERROR); } /* Look for the dimension */ MI_CHK_ERR(dimid=ncdimid(cdfid, string)) /* Create the variable and set defaults */ MI_CHK_ERR(varid=ncvardef(cdfid, name, datatype, ndims, &dimid)) MI_CHK_ERR(miattputstr(cdfid, varid, MIvarid, MI_STDVAR)) MI_CHK_ERR(miattputstr(cdfid, varid, MIvartype, MI_DIM_WIDTH)) MI_CHK_ERR(miattputstr(cdfid, varid, MIversion, MI_CURRENT_VERSION)) if (ndims==0) { MI_CHK_ERR(miattputstr(cdfid, varid, MIspacing, MI_REGULAR)) } else { MI_CHK_ERR(miattputstr(cdfid, varid, MIspacing, MI_IRREGULAR)) } MI_CHK_ERR(miattputstr(cdfid, varid, MIfiltertype, MI_SQUARE)) MI_RETURN(varid); } /* ----------------------------- MNI Header ----------------------------------- @NAME : MI_create_image_variable @INPUT : cdfid - cdf file id name - name of standard variable to create datatype - type of data to store (see ncvardef) ndims - number of dimensions of variable (see ncvardef) dim - vector of variable dimensions (see ncvardef) @OUTPUT : (none) @RETURNS : id of created variable, or MI_ERROR if an error occurs @DESCRIPTION: Creates a standard MINC image variable by calling ncvardef and then sets default attributes. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines @CREATED : August 6, 1992 @MODIFIED : ---------------------------------------------------------------------------- */ PRIVATE int MI_create_image_variable(int cdfid, char *name, nc_type datatype, int ndims, int dim[]) { int varid; /* Created variable id */ int max_varid; /* Variable id for dimensional attribute */ int min_varid; /* Variable id for dimensional attribute */ int maxmin_ndims; /* Number of dimensions in max/min variable */ int maxmin_dim[MAX_VAR_DIMS]; /* Dimensions of max/min variable */ int oldncopts; /* For saving and restoring ncopts */ MI_SAVE_ROUTINE_NAME("MI_create_image_variable"); /* Look to see if MIimagemax or MIimagemin exist for dimension checking and pointers */ oldncopts=ncopts; ncopts=0; max_varid=ncvarid(cdfid, MIimagemax); min_varid=ncvarid(cdfid, MIimagemin); ncopts=oldncopts; if (max_varid != MI_ERROR) { /* Get MIimagemax dimensions */ MI_CHK_ERR(ncvarinq(cdfid, max_varid, NULL, NULL, &maxmin_ndims, maxmin_dim, NULL)) MI_CHK_ERR(MI_verify_maxmin_dims(cdfid, ndims, dim, maxmin_ndims, maxmin_dim)) } if (min_varid != MI_ERROR) { /* Get MIimagemin dimensions */ MI_CHK_ERR(ncvarinq(cdfid, min_varid, NULL, NULL, &maxmin_ndims, maxmin_dim, NULL)) MI_CHK_ERR(MI_verify_maxmin_dims(cdfid, ndims, dim, maxmin_ndims, maxmin_dim)) } /* Create the variable */ MI_CHK_ERR(varid=ncvardef(cdfid, name, datatype, ndims, dim)) /* Standard attributes */ MI_CHK_ERR(MI_add_stdgroup(cdfid, varid)) /* Create pointers to MIimagemax and MIimagemin if they exist */ if (max_varid!=MI_ERROR) MI_CHK_ERR(miattput_pointer(cdfid, varid, MIimagemax, max_varid)) if (min_varid!=MI_ERROR) MI_CHK_ERR(miattput_pointer(cdfid, varid, MIimagemin, min_varid)) MI_RETURN(varid); } /* ----------------------------- MNI Header ----------------------------------- @NAME : MI_create_imaxmin_variable @INPUT : cdfid - cdf file id name - name of standard variable to create datatype - type of data to store (see ncvardef) ndims - number of dimensions of variable (see ncvardef) dim - vector of variable dimensions (see ncvardef) @OUTPUT : (none) @RETURNS : id of created variable, or MI_ERROR if an error occurs @DESCRIPTION: Creates a standard MINC image maximum or minimum dimensional attribute variable by calling ncvardef and then sets default attributes. If MIimage exists, then dimensions are checked (MIimagemax and MIimagemin cannot vary over the first two dimensions of MIimage (or first three if the first is MIvector_dimension)), and a pointer attribute is added to MIimage. @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines @CREATED : August 6, 1992 @MODIFIED : ---------------------------------------------------------------------------- */ PRIVATE int MI_create_imaxmin_variable(int cdfid, char *name, nc_type datatype, int ndims, int dim[]) { int varid; /* Created variable id */ int image_varid; /* Variable id for image */ int image_ndims; /* Number of image dimensions */ int image_dim[MAX_VAR_DIMS]; /* Image dimensions */ void *fillp; /* Pointer to fill value */ int oldncopts; /* For saving and restoring ncopts */ int index; static char fill_b[]={0,1}; static short fill_s[]={0,1}; static int fill_i[]={0,1}; static float fill_f[]={0.0,1.0}; static double fill_d[]={0.0,1.0}; MI_SAVE_ROUTINE_NAME("MI_create_imaxmin_variable"); /* Look to see if MIimage exists for dimension checking and pointers */ oldncopts=ncopts; ncopts=0; image_varid=ncvarid(cdfid, MIimage); ncopts=oldncopts; if (image_varid != MI_ERROR) { /* Get image dimensions */ MI_CHK_ERR(ncvarinq(cdfid, image_varid, NULL, NULL, &image_ndims, image_dim, NULL)) MI_CHK_ERR(MI_verify_maxmin_dims(cdfid, image_ndims, image_dim, ndims, dim)) } /* Create the variable */ MI_CHK_ERR(varid=ncvardef(cdfid, name, datatype, ndims, dim)) /* Standard attributes */ MI_CHK_ERR(miattputstr(cdfid, varid, MIvarid, MI_STDVAR)) MI_CHK_ERR(miattputstr(cdfid, varid, MIvartype, MI_VARATT)) MI_CHK_ERR(miattputstr(cdfid, varid, MIversion, MI_CURRENT_VERSION)) /* Attribute for setting default values to something reasonable */ index = STRINGS_EQUAL(name, MIimagemax) ? 1 : 0; fillp = ((datatype==NC_BYTE) ? (void *) &fill_b[index] : (datatype==NC_SHORT) ? (void *) &fill_s[index] : (datatype==NC_INT) ? (void *) &fill_i[index] : (datatype==NC_FLOAT) ? (void *) &fill_f[index] : (datatype==NC_DOUBLE) ? (void *) &fill_d[index] : (void *) NULL); if (fillp != NULL) { MI_CHK_ERR(ncattput(cdfid, varid, MI_FillValue, datatype, 1, fillp)) } /* Create pointer from MIimage to max or min if MIimage exists */ if (image_varid != MI_ERROR) MI_CHK_ERR(miattput_pointer(cdfid, image_varid, name, varid)) MI_RETURN(varid); } /* ----------------------------- MNI Header ----------------------------------- @NAME : MI_verify_maxmin_dims @INPUT : cdfid - cdf file id image_ndims - number of MIimage dimensions image_dim - image dimensions maxmin_ndims - number of MIimagemax or MIimagemin dimensions maxmin_dim - max/min dimensions @OUTPUT : (none) @RETURNS : MI_ERROR if dimensions don't agree @DESCRIPTION: Verifies that MIimage dimensions and MIimagemax/MIimagemin dimensions agree. MIimagemax/MIimagemin cannot vary over the two fastest varying (last) dimensions of MIimage - three fastest dimensions if MIvector_dimension is the fastest varying dimension of MIimage (this maintains the image nature of MIimage and its dimensional attributes MIimagemax and MIimagemin). @METHOD : @GLOBALS : @CALLS : NetCDF and MINC routines @CREATED : August 7, 1992 @MODIFIED : ---------------------------------------------------------------------------- */ PRIVATE int MI_verify_maxmin_dims(int cdfid, int image_ndims, int image_dim[], int maxmin_ndims, int maxmin_dim[]) { char dimname[MAX_NC_NAME]; int i,j; int nbaddims = 2; /* Number of dimension over which max/min should not vary */ MI_SAVE_ROUTINE_NAME("MI_verify_maxmin_dims"); /* Check to see if last dimension is MIvectordimension */ MI_CHK_ERR(ncdiminq(cdfid, image_dim[image_ndims-1], dimname, NULL)) if (STRINGS_EQUAL(dimname, MIvector_dimension)) nbaddims++; /* Loop through illegal image dimensions (last nbaddims) checking dimensions against maxmin_dim */ for (i=MAX(0,image_ndims-nbaddims); i