int define_variable_name_variable(int exoid, const char *VARIABLE, long dimension, const char *label) { char errmsg[MAX_ERR_LENGTH]; int dims[2]; int variable; dims[0] = dimension; dims[1] = ncdimid(exoid, DIM_STR); /* Checked earlier, so known to exist */ if ((variable = ncvardef (exoid, VARIABLE, NC_CHAR, 2, dims)) == -1) { if (ncerr == NC_ENAMEINUSE) { exerrval = ncerr; sprintf(errmsg, "Error: %s variable names are already defined in file id %d", label, exoid); ex_err("ex_put_all_var_param",errmsg,exerrval); } else { exerrval = ncerr; sprintf(errmsg, "Error: failed to define %s variable names in file id %d", label, exoid); ex_err("ex_put_all_var_param",errmsg,exerrval); } } return variable; }
int ex_get_map_param (int exoid, int *num_node_maps, int *num_elem_maps) { int dimid; long lnum_node_maps, lnum_elem_maps; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* node maps are optional */ if ((dimid = ncdimid (exoid, DIM_NUM_NM)) == -1) *num_node_maps = 0; else { if (ncdiminq (exoid, dimid, (char *) 0, &lnum_node_maps) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of node maps in file id %d", exoid); ex_err("ex_get_map_param",errmsg,exerrval); return (EX_FATAL); } *num_node_maps = lnum_node_maps; } /* element maps are optional */ if ((dimid = ncdimid (exoid, DIM_NUM_EM)) == -1) *num_elem_maps = 0; else { if (ncdiminq (exoid, dimid, (char *) 0, &lnum_elem_maps) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of element maps in file id %d", exoid); ex_err("ex_get_map_param",errmsg,exerrval); return (EX_FATAL); } *num_elem_maps = lnum_elem_maps; } return (EX_NOERR); }
MNCAPI int MI2dimid(int fd, const char *dimnm) { if (MI2_ISH5OBJ(fd)) { return (hdf_dimid(fd, dimnm)); } else { return (ncdimid(fd, dimnm)); } }
/* * Return the ID of a netCDF dimension, given the name of the dimension. */ static int c_ncdid ( int ncid, /* netCDF ID */ const char *dimname,/* dimension name */ int *rcode /* returned error code */ ) { int dimid; if ((dimid = ncdimid (ncid, dimname)) == -1) *rcode = ncerr; else { dimid++; *rcode = 0; } return dimid; }
int ex_inquire (int exoid, int req_info, int *ret_int, void *ret_float, char *ret_char) { int dimid, varid, i, tmp_num, *ids; long ldum, num_sets, start[2], count[2]; nclong *stat_vals; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ switch (req_info) { case EX_INQ_FILE_TYPE: /* obsolete call */ /*returns "r" for regular EXODUS II file or "h" for history EXODUS file*/ *ret_char = '\0'; exerrval = EX_BADPARAM; sprintf(errmsg, "Warning: file type inquire is obsolete"); ex_err("ex_inquire",errmsg,exerrval); return (EX_WARN); case EX_INQ_API_VERS: /* returns the EXODUS II API version number */ if (ncattget (exoid, NC_GLOBAL, ATT_API_VERSION, ret_float) == -1) { /* try old (prior to db version 2.02) attribute name */ if (ncattget (exoid, NC_GLOBAL, ATT_API_VERSION_BLANK,ret_float) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get EXODUS API version for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } } break; case EX_INQ_DB_VERS: /* returns the EXODUS II database version number */ if (ncattget (exoid, NC_GLOBAL, ATT_VERSION, ret_float) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get EXODUS database version for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } break; case EX_INQ_LIB_VERS: /* returns the EXODUS II Library version number */ flt_cvt((float *)ret_float, EX_API_VERS); break; case EX_INQ_TITLE: /* returns the title of the database */ if (ncattget (exoid, NC_GLOBAL, ATT_TITLE, ret_char) == -1) { *ret_char = '\0'; exerrval = ncerr; sprintf(errmsg, "Error: failed to get database title for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } break; case EX_INQ_DIM: /* returns the dimensionality (2 or 3, for 2-d or 3-d) of the database */ if ((dimid = ncdimid (exoid, DIM_NUM_DIM)) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to locate database dimensionality in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get database dimensionality for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; break; case EX_INQ_NODES: /* returns the number of nodes */ if ((dimid = ncdimid (exoid, DIM_NUM_NODES)) == -1) { *ret_int = 0; } else { if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of nodes for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; } break; case EX_INQ_ELEM: /* returns the number of elements */ if ((dimid = ncdimid (exoid, DIM_NUM_ELEM)) == -1) { *ret_int = 0; } else { if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of elements for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; } break; case EX_INQ_ELEM_BLK: /* returns the number of element blocks */ if ((dimid = ncdimid (exoid, DIM_NUM_EL_BLK)) == -1) { *ret_int = 0; } else { if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of element blocks for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; } break; case EX_INQ_NODE_SETS: /* returns the number of node sets */ if ((dimid = ncdimid (exoid, DIM_NUM_NS)) < 0) *ret_int = 0; /* no node sets defined */ else { if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of node sets in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; } break; case EX_INQ_NS_NODE_LEN: /* returns the length of the concatenated node sets node list */ *ret_int = 0; /* default value if no node sets are defined */ if ((dimid = ncdimid (exoid, DIM_NUM_NS)) != -1 ) { if (ncdiminq (exoid, dimid, (char *) 0, &num_sets) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of node sets in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (!(ids = malloc(num_sets*sizeof(int)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for node set ids for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (ex_get_node_set_ids (exoid, ids) == EX_FATAL) { sprintf(errmsg, "Error: failed to get node sets in file id %d", exoid); /* pass back error code from ex_get_node_set_ids (in exerrval) */ ex_err("ex_inquire",errmsg,exerrval); free (ids); return (EX_FATAL); } /* allocate space for stat array */ if (!(stat_vals = malloc((int)num_sets*sizeof(nclong)))) { exerrval = EX_MEMFAIL; free (ids); sprintf(errmsg, "Error: failed to allocate memory for node set status array for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } /* get variable id of status array */ if ((varid = ncvarid (exoid, VAR_NS_STAT)) != -1) { /* if status array exists, use it, otherwise assume, object exists to be backward compatible */ start[0] = 0; start[1] = 0; count[0] = num_sets; count[1] = 0; if (ncvarget (exoid, varid, start, count, (void *)stat_vals) == -1) { exerrval = ncerr; free (ids); free(stat_vals); sprintf(errmsg, "Error: failed to get node set status array from file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } } else /* default: status is true */ for(i=0;i<num_sets;i++) stat_vals[i]=1; for (i=0; i<num_sets; i++) { if (stat_vals[i] == 0) /* is this object null? */ continue; if ((dimid = ncdimid (exoid, DIM_NUM_NOD_NS(i+1))) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of nodes in node set %d in file id %d", ids[i],exoid); ex_err("ex_inquire",errmsg,exerrval); free (ids); free (stat_vals); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of nodes in node set %d in file id %d", ids[i],exoid); ex_err("ex_inquire",errmsg,exerrval); free (stat_vals); free (ids); return (EX_FATAL); } *ret_int += ldum; } free (stat_vals); free (ids); } break; case EX_INQ_NS_DF_LEN: /* returns the length of the concatenated node sets dist factor list */ /* Determine the concatenated node sets distribution factor length: 1. Get the node set ids list. 2. Check see if the dist factor variable for a node set id exists. 3. If it exists, goto step 4, else the length is zero. 4. Get the dimension of the number of nodes in the node set -0 use this value as the length as by definition they are the same. 5. Sum the individual lengths for the total list length. */ *ret_int = 0; /* default value if no node sets defined */ if ((dimid = ncdimid (exoid, DIM_NUM_NS)) != -1) { if (ncdiminq (exoid, dimid, (char *) 0, &num_sets) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of node sets in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (!(ids = malloc(num_sets*sizeof(int)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for node set ids for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (ex_get_node_set_ids (exoid, ids) == EX_FATAL) { sprintf(errmsg, "Error: failed to get node sets in file id %d", exoid); /* pass back error code from ex_get_node_set_ids (in exerrval) */ ex_err("ex_inquire",errmsg,exerrval); free (ids); return (EX_FATAL); } for (i=0; i<num_sets; i++) { if (ncvarid (exoid, VAR_FACT_NS(i+1)) == -1) { if (ncerr == NC_ENOTVAR) { ldum = 0; /* this dist factor doesn't exist */ } else { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of dist fact for node set %d in file id %d", ids[i], exoid); ex_err("ex_inquire",errmsg,exerrval); free (ids); return (EX_FATAL); } } else { if ((dimid = ncdimid (exoid, DIM_NUM_NOD_NS(i+1))) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of nodes in node set %d in file id %d", ids[i], exoid); ex_err("ex_inquire",errmsg,exerrval); free (ids); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of nodes in node set %d in file id %d", ids[i],exoid); ex_err("ex_inquire",errmsg,exerrval); free(ids); return (EX_FATAL); } } *ret_int += ldum; } free(ids); } break; case EX_INQ_SIDE_SETS: /* returns the number of side sets */ *ret_int = 0; /* default return value */ if ((dimid = ncdimid (exoid, DIM_NUM_SS)) != -1) { if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of side sets in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; } break; case EX_INQ_SS_NODE_LEN: /* returns the length of the concatenated side sets node list */ *ret_int = 0; /* default return value */ if ((dimid = ncdimid (exoid, DIM_NUM_SS)) != -1) { if (ncdiminq (exoid, dimid, (char *) 0, &num_sets) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of side sets in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (!(ids = malloc(num_sets*sizeof(int)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for side set ids for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (ex_get_side_set_ids (exoid, ids) == EX_FATAL) { sprintf(errmsg, "Error: failed to get side set ids in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); free(ids); return (EX_FATAL); } /* allocate space for stat array */ if (!(stat_vals = malloc((int)num_sets*sizeof(nclong)))) { exerrval = EX_MEMFAIL; free (ids); sprintf(errmsg, "Error: failed to allocate memory for side set status array for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } /* get variable id of status array */ if ((varid = ncvarid (exoid, VAR_SS_STAT)) != -1) { /* if status array exists, use it, otherwise assume, object exists to be backward compatible */ start[0] = 0; start[1] = 0; count[0] = num_sets; count[1] = 0; if (ncvarget (exoid, varid, start, count, (void *)stat_vals) == -1) { exerrval = ncerr; free (ids); free(stat_vals); sprintf(errmsg, "Error: failed to get element block status array from file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } } else /* default: status is true */ for(i=0;i<num_sets;i++) stat_vals[i]=1; /* walk id list, get each side set node length and sum for total */ for (i=0; i<num_sets; i++) { if (stat_vals[i] == 0) /* is this object null? */ continue; if (ex_get_side_set_node_list_len(exoid, ids[i], &tmp_num) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to side set %d node length in file id %d", ids[i],exoid); ex_err("ex_inquire",errmsg,exerrval); free(stat_vals); free(ids); return (EX_FATAL); } *ret_int += tmp_num; } free(stat_vals); free (ids); } break; case EX_INQ_SS_ELEM_LEN: /* returns the length of the concatenated side sets element list */ EX_GET_CONCAT_SET_LEN(ret_int,"side",EX_SIDE_SET,DIM_NUM_SS,VAR_SS_STAT,DIM_NUM_SIDE_SS,0); break; case EX_INQ_SS_DF_LEN: /* returns the length of the concatenated side sets dist factor list */ /* Determine the concatenated side sets distribution factor length: 1. Get the side set ids list. 2. Check see if the dist factor dimension for a side set id exists. 3. If it exists, goto step 4, else set the individual length to zero. 4. Sum the dimension value into the running total length. */ *ret_int = 0; /* first check see if any side sets exist */ if ((dimid = ncdimid (exoid, DIM_NUM_SS)) != -1) { if (ncdiminq (exoid, dimid, (char *) 0, &num_sets) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of side sets in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (!(ids = malloc(num_sets*sizeof(int)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for side set ids for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (ex_get_side_set_ids (exoid, ids) == EX_FATAL) { sprintf(errmsg, "Error: failed to get side sets in file id %d", exoid); /* pass back error code from ex_get_side_set_ids (in exerrval) */ ex_err("ex_inquire",errmsg,exerrval); free (ids); return (EX_FATAL); } for (i=0; i<num_sets; i++) { if ((dimid = ncdimid (exoid, DIM_NUM_DF_SS(i+1))) == -1) { if (ncerr == NC_EBADDIM) { ldum = 0; /* this dist factor doesn't exist */ } else { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of dist fact for side set %d in file id %d", ids[i], exoid); ex_err("ex_inquire",errmsg,exerrval); free (ids); return (EX_FATAL); } } else { if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of dist factors in side set %d in file id %d", ids[i], exoid); ex_err("ex_inquire",errmsg,exerrval); free (ids); return (EX_FATAL); } } *ret_int += ldum; } free (ids); } break; case EX_INQ_QA: /* returns the number of QA records */ if ((dimid = ncdimid (exoid, DIM_NUM_QA)) < 0) *ret_int = 0; /* no QA records stored */ else { if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of QA records in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; } break; case EX_INQ_INFO: /* returns the number of information records */ if ((dimid = ncdimid (exoid, DIM_NUM_INFO)) < 0) *ret_int = 0; /* no information records stored */ else { if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of info records in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; } break; case EX_INQ_TIME: /* returns the number of time steps stored in the database; we find * this out by inquiring the maximum record number of the "unlimited" * dimension */ if ((dimid = ncdimid (exoid, DIM_TIME)) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to locate time dimension in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get time dimension in file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; break; case EX_INQ_EB_PROP: /* returns the number of element block properties */ *ret_int = ex_get_num_props (exoid, EX_ELEM_BLOCK); break; case EX_INQ_NS_PROP: /* returns the number of node set properties */ *ret_int = ex_get_num_props (exoid, EX_NODE_SET); break; case EX_INQ_SS_PROP: /* returns the number of side set properties */ *ret_int = ex_get_num_props (exoid, EX_SIDE_SET); break; case EX_INQ_ELEM_MAP: /* returns the number of element maps */ if ((dimid = ncdimid (exoid, DIM_NUM_EM)) == -1) { /* no element maps so return 0 */ *ret_int = 0; return (EX_NOERR); } if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of element maps for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; break; case EX_INQ_EM_PROP: /* returns the number of element map properties */ *ret_int = ex_get_num_props (exoid, EX_ELEM_MAP); break; case EX_INQ_NODE_MAP: /* returns the number of node maps */ if ((dimid = ncdimid (exoid, DIM_NUM_NM)) == -1) { /* no node maps so return 0 */ *ret_int = 0; return (EX_NOERR); } if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1) { *ret_int = 0; exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of node maps for file id %d", exoid); ex_err("ex_inquire",errmsg,exerrval); return (EX_FATAL); } *ret_int = ldum; break; case EX_INQ_NM_PROP: /* returns the number of element map properties */ *ret_int = ex_get_num_props (exoid, EX_NODE_MAP); break; case EX_INQ_EDGE: /* returns the number of edges (defined across all edge blocks). */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_EDGE, 1); break; case EX_INQ_EDGE_BLK: /* returns the number of edge blocks. */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_ED_BLK, 1); break; case EX_INQ_EDGE_SETS: /* returns the number of edge sets. */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_ES, 1); break; case EX_INQ_ES_LEN: /* returns the length of the concatenated edge set edge list. */ EX_GET_CONCAT_SET_LEN(ret_int,"edge",EX_EDGE_SET,DIM_NUM_ES,VAR_ES_STAT,DIM_NUM_EDGE_ES,0); break; case EX_INQ_ES_DF_LEN: /* returns the length of the concatenated edge set distribution factor list. */ EX_GET_CONCAT_SET_LEN(ret_int,"edge",EX_EDGE_SET,DIM_NUM_ES,VAR_ES_STAT,DIM_NUM_DF_ES,1); break; case EX_INQ_EDGE_PROP: /* returns the number of integer properties stored for each edge block. This includes the "ID" property. */ *ret_int = ex_get_num_props( exoid, EX_EDGE_BLOCK ); break; case EX_INQ_ES_PROP: /* returns the number of integer properties stored for each edge set.. This includes the "ID" property */ *ret_int = ex_get_num_props( exoid, EX_EDGE_SET ); break; case EX_INQ_FACE: /* returns the number of faces (defined across all face blocks). */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_FACE, 1); break; case EX_INQ_FACE_BLK: /* returns the number of edge blocks. */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_FA_BLK, 1); break; case EX_INQ_FACE_SETS: /* returns the number of edge sets. */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_FS, 1); break; case EX_INQ_FS_LEN: /* returns the length of the concatenated edge set edge list. */ EX_GET_CONCAT_SET_LEN(ret_int,"face",EX_FACE_SET,DIM_NUM_FS,VAR_FS_STAT,DIM_NUM_FACE_FS,0); break; case EX_INQ_FS_DF_LEN: /* returns the length of the concatenated edge set distribution factor list. */ EX_GET_CONCAT_SET_LEN(ret_int,"face",EX_FACE_SET,DIM_NUM_FS,VAR_FS_STAT,DIM_NUM_DF_FS,1); break; case EX_INQ_FACE_PROP: /* returns the number of integer properties stored for each edge block. This includes the "ID" property. */ *ret_int = ex_get_num_props( exoid, EX_FACE_BLOCK ); break; case EX_INQ_FS_PROP: /* returns the number of integer properties stored for each edge set.. This includes the "ID" property */ *ret_int = ex_get_num_props( exoid, EX_FACE_SET ); break; case EX_INQ_ELEM_SETS: /* returns the number of element sets. */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_ELS, 1); break; case EX_INQ_ELS_LEN: /* returns the length of the concatenated element set element list. */ EX_GET_CONCAT_SET_LEN(ret_int,"element",EX_ELEM_SET,DIM_NUM_ELS,VAR_ELS_STAT,DIM_NUM_ELE_ELS,0); break; case EX_INQ_ELS_DF_LEN: /* returns the length of the concatenated element set distribution factor list. */ EX_GET_CONCAT_SET_LEN(ret_int,"element",EX_ELEM_SET,DIM_NUM_ELS,VAR_ELS_STAT,DIM_NUM_DF_ELS,1); break; case EX_INQ_ELS_PROP: /* returns the number of integer properties stored for each element set. */ *ret_int = ex_get_num_props( exoid, EX_ELEM_SET ); break; case EX_INQ_EDGE_MAP: /* returns the number of edge sets. */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_EDM, 1); break; case EX_INQ_FACE_MAP: /* returns the number of edge sets. */ EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_FAM, 1); break; default: *ret_int = 0; exerrval = EX_FATAL; sprintf(errmsg, "Error: invalid inquiry %d", req_info); ex_err("ex_inquire",errmsg,exerrval); return(EX_FATAL); } return (EX_NOERR); }
int ex_put_node_set_param (int exoid, int node_set_id, int num_nodes_in_set, int num_dist_in_set) { int dimid, varid, dims[1]; long start[1]; nclong ldum; long num_node_sets; int cur_num_node_sets, node_set_stat; char *cdum; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ cdum = 0; /* first check if any node sets are specified */ if ((dimid = (ncdimid (exoid, DIM_NUM_NS))) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: no node sets specified in file id %d", exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } /* Check for duplicate node set id entry */ ex_id_lkup(exoid,VAR_NS_IDS,node_set_id); if (exerrval != EX_LOOKUPFAIL) /* found the node set id */ { exerrval = ncerr; sprintf(errmsg, "Error: node set %d already defined in file id %d", node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return(EX_FATAL); } /* Get number of node sets initialized for this file */ if ((ncdiminq (exoid,dimid,cdum,&num_node_sets)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of node sets in file id %d", exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } /* Keep track of the total number of node sets defined using a counter stored in a linked list keyed by exoid. NOTE: ex_get_file_item is used to find the number of node sets for a specific file and returns that value. */ cur_num_node_sets=ex_get_file_item(exoid, &ns_ctr_list ); if (cur_num_node_sets >= num_node_sets) { exerrval = EX_FATAL; sprintf(errmsg, "Error: exceeded number of node sets (%ld) specified in file id %d", num_node_sets,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } /* NOTE: ex_inc_file_item is used to find the number of node sets for a specific file and returns that value incremented. */ cur_num_node_sets=ex_inc_file_item(exoid, &ns_ctr_list ); /* write out information to previously defined variable */ /* first get id of variable */ if ((varid = ncvarid (exoid, VAR_NS_IDS)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate node set ids in file id %d", exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } /* then, write out node set id */ start[0] = cur_num_node_sets; ldum = (nclong)node_set_id; if (ncvarput1 (exoid, varid, start, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store node set id %d in file id %d", node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } if (num_nodes_in_set == 0) /* Is this a NULL side set? */ node_set_stat = 0; /* change node set status to NULL */ else node_set_stat = 1; /* change node set status to TRUE */ if ((varid = ncvarid (exoid, VAR_NS_STAT)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate node set status in file id %d", exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } ldum = (nclong)node_set_stat; if (ncvarput1 (exoid, varid, start, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store node set %d status to file id %d", node_set_id, exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } if (num_nodes_in_set == 0) /* Is this a NULL side set? */ { return(EX_NOERR); } /* put netcdf file into define mode */ if (ncredef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to put file id %d into define mode", exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } if ((dimid = ncdimdef (exoid, DIM_NUM_NOD_NS(cur_num_node_sets+1), (long)num_nodes_in_set)) == -1) { if (ncerr == NC_ENAMEINUSE) { exerrval = ncerr; sprintf(errmsg, "Error: node set %d size already defined in file id %d", node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); } else { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of nodes for set %d in file id %d", node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); } goto error_ret; } /* create variable array in which to store the node set node list */ dims[0] = dimid; if (ncvardef(exoid, VAR_NODE_NS(cur_num_node_sets+1), NC_LONG,1,dims) == -1) { if (ncerr == NC_ENAMEINUSE) { exerrval = ncerr; sprintf(errmsg, "Error: node set %d node list already defined in file id %d", node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); } else { exerrval = ncerr; sprintf(errmsg, "Error: failed to create node set %d node list in file id %d", node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); } goto error_ret; /* exit define mode and return */ } /* Create distribution factors variable if required */ if (num_dist_in_set > 0) { /* num_dist_in_set should equal num_nodes_in_set */ if (num_dist_in_set != num_nodes_in_set) { exerrval = EX_FATAL; sprintf(errmsg, "Error: # dist fact (%d) not equal to # nodes (%d) in node set %d file id %d", num_dist_in_set, num_nodes_in_set, node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } else { /* create variable for distribution factors */ if (ncvardef (exoid, VAR_FACT_NS(cur_num_node_sets+1), nc_flt_code(exoid), 1, dims) == -1) { if (ncerr == NC_ENAMEINUSE) { exerrval = ncerr; sprintf(errmsg, "Error: node set %d dist factors already exist in file id %d", node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); } else { exerrval = ncerr; sprintf(errmsg, "Error: failed to create node set %d dist factors in file id %d", node_set_id,exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); } goto error_ret; /* exit define mode and return */ } } } /* leave define mode */ if (ncendef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to complete definition in file id %d", exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (ncendef (exoid) == -1) /* exit define mode */ { sprintf(errmsg, "Error: failed to complete definition for file id %d", exoid); ex_err("ex_put_node_set_param",errmsg,exerrval); } return (EX_FATAL); }
/*! * writes an element map; this is a vector of integers of length number * of elements */ int ex_put_partial_elem_map (int exoid, int map_id, int ent_start, int ent_count, const int *elem_map) { int dimid, varid, iresult, map_ndx, map_exists; long start[1]; nclong ldum, *lptr; long num_elem_maps, num_elem, count[1]; int cur_num_elem_maps; char *cdum; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ map_exists = 0; cdum = 0; /* Make sure the file contains elements */ if ((dimid = (ncdimid (exoid, DIM_NUM_ELEM))) == -1 ) { return (EX_NOERR); } /* first check if any element maps are specified */ if ((dimid = (ncdimid (exoid, DIM_NUM_EM))) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: no element maps specified in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* Check for duplicate element map id entry */ map_ndx = ex_id_lkup(exoid,VAR_EM_PROP(1),map_id); if (exerrval == EX_LOOKUPFAIL) { /* did not find the element map id */ map_exists = 0; /* Map is being defined */ map_ndx = -1; } else { map_exists = 1; /* A portion of this map has already been written */ } if (!map_exists) { /* Get number of element maps initialized for this file */ if ((ncdiminq (exoid,dimid,cdum,&num_elem_maps)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of element maps in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* Keep track of the total number of element maps defined using a counter stored in a linked list keyed by exoid. NOTE: ex_get_file_item is used to find the number of element maps for a specific file and returns that value. */ cur_num_elem_maps = ex_get_file_item(exoid, &em_ctr_list ); if (cur_num_elem_maps >= num_elem_maps) { exerrval = EX_FATAL; sprintf(errmsg, "Error: exceeded number of element maps (%ld) specified in file id %d", num_elem_maps,exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* NOTE: ex_inc_file_item is used to find the number of element maps for a specific file and returns that value incremented. */ cur_num_elem_maps = ex_inc_file_item(exoid, &em_ctr_list ); } else { cur_num_elem_maps = map_ndx-1; } /* determine number of elements */ if ((dimid = (ncdimid (exoid, DIM_NUM_ELEM))) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: couldn't determine number of elements in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &num_elem) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of elements in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* Check input parameters for a valid range of numbers */ if (ent_start <= 0 || ent_start > num_elem) { exerrval = EX_FATAL; sprintf(errmsg, "Error: start count is invalid in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } if (ent_count < 0) { exerrval = EX_FATAL; sprintf(errmsg, "Error: Invalid count value in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } if (ent_start+ent_count-1 > num_elem) { exerrval = EX_FATAL; sprintf(errmsg, "Error: start+count-1 is larger than element count in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* write out information to previously defined variable */ /* first get id of variable */ if ((varid = ncvarid (exoid, VAR_EM_PROP(1))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate element map ids in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* then, write out element map id */ if (!map_exists) { start[0] = cur_num_elem_maps; ldum = (nclong)map_id; if (ncvarput1 (exoid, varid, start, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element map id %d in file id %d", map_id,exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } } /* locate variable array in which to store the element map */ if ((varid = ncvarid(exoid,VAR_ELEM_MAP(cur_num_elem_maps+1))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate element map %d in file id %d", map_id,exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* write out the element map */ /* this contortion is necessary because netCDF is expecting nclongs; fortunately it's necessary only when ints and nclongs aren't the same size */ start[0] = ent_start-1; count[0] = ent_count; if (sizeof(int) == sizeof(nclong)) { iresult = ncvarput (exoid, varid, start, count, elem_map); } else { lptr = itol (elem_map, (int)ent_count); iresult = ncvarput (exoid, varid, start, count, lptr); free(lptr); } if (iresult == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element map in file id %d", exoid); ex_err("ex_put_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); }
int ex_put_coord (int exoid, const void *x_coor, const void *y_coor, const void *z_coor) { int coordid; int coordidx, coordidy, coordidz; int numnoddim, ndimdim, i; long num_nod, num_dim, start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* inquire id's of previously defined dimensions */ if ((numnoddim = ncdimid (exoid, DIM_NUM_NODES)) == -1) { /* If not found, then this file is storing 0 nodes. Return immediately */ return (EX_NOERR); } if (ncdiminq (exoid, numnoddim, NULL, &num_nod) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: inquire failed to return number of nodes in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } if ((ndimdim = ncdimid (exoid, DIM_NUM_DIM)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of dimensions in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, ndimdim, NULL, &num_dim) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of dimensions in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } /* write out the coordinates */ if (ex_large_model(exoid) == 0) { if ((coordid = ncvarid (exoid, VAR_COORD)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate nodal coordinates in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } for (i=0; i<num_dim; i++) { start[0] = i; start[1] = 0; count[0] = 1; count[1] = num_nod; if (i == 0 && x_coor != NULL) { if (ncvarput (exoid, coordid, start, count, ex_conv_array(exoid,WRITE_CONVERT,x_coor,(int)num_nod)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to put X coord array in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } else if (i == 1 && y_coor != NULL) { if (ncvarput (exoid, coordid, start, count, ex_conv_array(exoid,WRITE_CONVERT,y_coor,(int)num_nod)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to put Y coord array in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } else if (i == 2 && z_coor != NULL) { if (ncvarput (exoid, coordid, start, count, ex_conv_array(exoid,WRITE_CONVERT,z_coor,(int)num_nod)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to put Z coord array in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } } } else { if ((coordidx = ncvarid (exoid, VAR_COORD_X)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate x nodal coordinates in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } if (num_dim > 1) { if ((coordidy = ncvarid (exoid, VAR_COORD_Y)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate y nodal coordinates in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } else { coordidy = 0; } if (num_dim > 2) { if ((coordidz = ncvarid (exoid, VAR_COORD_Z)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate z nodal coordinates in file id %d", exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } else { coordidz = 0; } /* write out the coordinates */ for (i=0; i<num_dim; i++) { const void *coor; char *which; int status; if (i == 0) { coor = x_coor; which = "X"; coordid = coordidx; } else if (i == 1) { coor = y_coor; which = "Y"; coordid = coordidy; } else if (i == 2) { coor = z_coor; which = "Z"; coordid = coordidz; } if (coor != NULL) { if (nc_flt_code(exoid) == NC_FLOAT) { status = nc_put_var_float(exoid, coordid, ex_conv_array(exoid,WRITE_CONVERT, coor,(int)num_nod)); } else { status = nc_put_var_double(exoid, coordid, ex_conv_array(exoid,WRITE_CONVERT, coor,(int)num_nod)); } if (status == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to put %s coord array in file id %d", which, exoid); ex_err("ex_put_coord",errmsg,exerrval); return (EX_FATAL); } } } } return (EX_NOERR); }
int ex_get_var_time( int exoid, int var_type, int var_index, int id, int beg_time_step, int end_time_step, void* var_vals ) { int i, dimid, varid, numel = 0, offset; nclong *obj_ids, *stat_vals; long num_obj, num_entries_this_obj = 0, start[2], count[2]; float fdum; char *cdum; char errmsg[MAX_ERR_LENGTH]; const char* tname; const char* dimnumobj; const char* varobjids; const char* varobstat; switch (var_type) { case EX_GLOBAL: return ex_get_glob_var_time( exoid, var_index, beg_time_step, end_time_step, var_vals ); case EX_NODAL: return ex_get_nodal_var_time( exoid, var_index, id, beg_time_step, end_time_step, var_vals ); case EX_EDGE_BLOCK: tname = "edge block"; dimnumobj = DIM_NUM_ED_BLK; varobjids = VAR_ID_ED_BLK; varobstat = VAR_STAT_ED_BLK; break; case EX_FACE_BLOCK: tname = "face block"; dimnumobj = DIM_NUM_FA_BLK; varobjids = VAR_ID_FA_BLK; varobstat = VAR_STAT_FA_BLK; break; case EX_ELEM_BLOCK: tname = "element block"; dimnumobj = DIM_NUM_EL_BLK; varobjids = VAR_ID_EL_BLK; varobstat = VAR_STAT_EL_BLK; break; case EX_NODE_SET: tname = "node set"; dimnumobj = DIM_NUM_NSET_VAR; varobjids = VAR_NS_IDS; varobstat = VAR_NS_STAT; break; case EX_EDGE_SET: tname = "edge set"; dimnumobj = DIM_NUM_ESET_VAR; varobjids = VAR_ES_IDS; varobstat = VAR_ES_STAT; break; case EX_FACE_SET: tname = "face set"; dimnumobj = DIM_NUM_FSET_VAR; varobjids = VAR_FS_IDS; varobstat = VAR_FS_STAT; break; case EX_SIDE_SET: tname = "side set"; dimnumobj = DIM_NUM_SSET_VAR; varobjids = VAR_SS_IDS; varobstat = VAR_SS_STAT; break; case EX_ELEM_SET: tname = "element set"; dimnumobj = DIM_NUM_ELSET_VAR; varobjids = VAR_ELS_IDS; varobstat = VAR_ELS_STAT; break; default: exerrval = EX_BADPARAM; sprintf( errmsg, "Error: Invalid variable type (%d) specified for file id %d", var_type, exoid ); ex_err( "ex_get_var_time", errmsg, exerrval ); return (EX_FATAL); } exerrval = 0; /* clear error code */ cdum = 0; /* initialize even though it is not used */ /* assume entry number is 1-based (the first entry of an object is 1, not 0); * adjust so it is 0-based */ id--; /* find what object the entry is in */ /* first, find out how many objects there are */ if ((dimid = ncdimid (exoid, dimnumobj)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of %ss in file id %d", tname,exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &num_obj) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of %ss in file id %d", tname,exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } /* get the array of object ids */ /* don't think we need this anymore since the netcdf variable names associated with objects don't contain the object ids */ if (!(obj_ids = malloc(num_obj*sizeof(nclong)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for %s ids for file id %d", tname,exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } if ((varid = ncvarid (exoid, varobjids)) == -1) { exerrval = ncerr; free(obj_ids); sprintf(errmsg, "Error: failed to locate %s ids in file id %d", tname,exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } start[0] = 0; count[0] = num_obj; if (ncvarget (exoid, varid, start, count, obj_ids) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get %s ids from file id %d", tname,exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } /* allocate space for stat array */ if (!(stat_vals = malloc((int)num_obj*sizeof(nclong)))) { exerrval = EX_MEMFAIL; free (obj_ids); sprintf(errmsg, "Error: failed to allocate memory for %s status array for file id %d", tname,exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } /* get variable id of status array */ if ((varid = ncvarid (exoid, varobstat)) != -1) { /* if status array exists, use it, otherwise assume, object exists to be backward compatible */ start[0] = 0; start[1] = 0; count[0] = num_obj; count[1] = 0; if (ncvarget (exoid, varid, start, count, (void *)stat_vals) == -1) { exerrval = ncerr; free (obj_ids); free(stat_vals); sprintf(errmsg, "Error: failed to get %s status array from file id %d", tname,exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } } else /* default: status is true */ for(i=0;i<num_obj;i++) stat_vals[i]=1; /* loop through each object until id is found; since entry * numbers are sequential (beginning with 1) id is in obj_i * when id_first_i <= id <= id_last_i, where * id_first_i is the entry number of the first entry in * obj_i and id_last_i is the entry number of the last * entry in obj_i */ i = 0; if (stat_vals[i] != 0) { if ((dimid = ncdimid (exoid, ex_dim_num_entries_in_object(var_type,i+1))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of entries in %s %d in file id %d", tname, obj_ids[i], exoid); ex_err("ex_get_var_time",errmsg,exerrval); free(stat_vals); free(obj_ids); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &num_entries_this_obj) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of entries in %s %d in file id %d", tname, obj_ids[i], exoid); ex_err("ex_get_var_time",errmsg,exerrval); free(stat_vals); free(obj_ids); return (EX_FATAL); } } /* End NULL object check */ numel = num_entries_this_obj; while (numel <= id) { if (stat_vals[++i] != 0) { if ((dimid = ncdimid(exoid,ex_dim_num_entries_in_object(var_type,i+1))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of entries in %s %d in file id %d", tname, obj_ids[i], exoid); ex_err("ex_get_var_time",errmsg,exerrval); free(stat_vals); free(obj_ids); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &num_entries_this_obj) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of entries in %s %d in file id %d", tname, obj_ids[i], exoid); ex_err("ex_get_var_time",errmsg,exerrval); free(stat_vals); free(obj_ids); return (EX_FATAL); } numel += num_entries_this_obj; } } offset = id - (numel - num_entries_this_obj); /* inquire previously defined variable */ if((varid=ncvarid(exoid,ex_name_var_of_object(var_type,var_index,i+1))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate variable %d for %s %d in file id %d", var_index,tname,obj_ids[i],exoid); ex_err("ex_get_var_time",errmsg,exerrval); free(stat_vals); free(obj_ids); return (EX_FATAL); } free(stat_vals); free(obj_ids); /* read values of object variable */ start[0] = --beg_time_step; start[1] = offset; if (end_time_step < 0) { /* user is requesting the maximum time step; we find this out using the * database inquire function to get the number of time steps; the ending * time step number is 1 less due to 0 based array indexing in C */ if (ex_inquire (exoid, EX_INQ_TIME, &end_time_step, &fdum, cdum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get maximum time step in file id %d", exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } } end_time_step--; count[0] = end_time_step - beg_time_step + 1; count[1] = 1; if (ncvarget (exoid, varid, start, count, ex_conv_array(exoid,RTN_ADDRESS,var_vals,count[0])) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get %s variable values in file id %d", tname,exoid); ex_err("ex_get_var_time",errmsg,exerrval); return (EX_FATAL); } ex_conv_array( exoid, READ_CONVERT, var_vals, count[0] ); return (EX_NOERR); }
int ex_put_prop_array (int exoid, int obj_type, const char *prop_name, const int *values) { int num_props, i, propid, dimid, dims[1], iresult; int found = FALSE; long start[1], count[1], num_obj; nclong *lptr; char name[MAX_VAR_NAME_LENGTH+1]; char tmpstr[MAX_STR_LENGTH+1]; char obj_stype[MAX_VAR_NAME_LENGTH+1]; char dim_name[MAX_VAR_NAME_LENGTH+1]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* check if property has already been created */ num_props = ex_get_num_props(exoid, obj_type); switch (obj_type) { case EX_ELEM_BLOCK: strcpy (obj_stype, VAR_ID_EL_BLK); strcpy (dim_name, DIM_NUM_EL_BLK); break; case EX_NODE_SET: strcpy (obj_stype, VAR_NS_IDS); strcpy (dim_name, DIM_NUM_NS); break; case EX_SIDE_SET: strcpy (obj_stype, VAR_SS_IDS); strcpy (dim_name, DIM_NUM_SS); break; case EX_ELEM_MAP: strcpy (obj_stype, VAR_EM_PROP(1)); strcpy (dim_name, DIM_NUM_EM); break; case EX_NODE_MAP: strcpy (obj_stype, VAR_NM_PROP(1)); strcpy (dim_name, DIM_NUM_NM); break; default: exerrval = EX_BADPARAM; sprintf(errmsg, "Error: object type %d not supported; file id %d", obj_type, exoid); ex_err("ex_put_prop_array",errmsg,exerrval); return (EX_FATAL); } /* inquire id of previously defined dimension (number of objects) */ if ((dimid = ncdimid (exoid, dim_name)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of %s objects in file id %d", obj_stype, exoid); ex_err("ex_put_prop_array",errmsg, exerrval); return (EX_FATAL); } /* get number of objects */ if (ncdiminq (exoid, dimid, dim_name, &num_obj) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of %s objects in file id %d", obj_stype, exoid); ex_err("ex_put_prop_array",errmsg, exerrval); return (EX_FATAL); } for (i=1; i<=num_props; i++) { switch (obj_type){ case EX_ELEM_BLOCK: strcpy (name, VAR_EB_PROP(i)); break; case EX_NODE_SET: strcpy (name, VAR_NS_PROP(i)); break; case EX_SIDE_SET: strcpy (name, VAR_SS_PROP(i)); break; case EX_ELEM_MAP: strcpy (name, VAR_EM_PROP(i)); break; case EX_NODE_MAP: strcpy (name, VAR_NM_PROP(i)); break; default: exerrval = EX_BADPARAM; sprintf(errmsg, "Error: object type %d not supported; file id %d", obj_type, exoid); ex_err("ex_put_prop_array",errmsg,exerrval); return(EX_FATAL); } if ((propid = ncvarid (exoid, name)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get property array id in file id %d", exoid); ex_err("ex_put_prop_array",errmsg,exerrval); return (EX_FATAL); } /* compare stored attribute name with passed property name */ memset(tmpstr, 0, MAX_STR_LENGTH+1); if ((ncattget (exoid, propid, ATT_PROP_NAME, tmpstr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get property name in file id %d", exoid); ex_err("ex_put_prop_array",errmsg,exerrval); return (EX_FATAL); } if (strcmp(tmpstr, prop_name) == 0) { found = TRUE; break; } } /* if property array has not been created, create it */ if (!found) { /* put netcdf file into define mode */ if (ncredef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid); ex_err("ex_put_prop_array",errmsg,exerrval); return (EX_FATAL); } /* create a variable with a name xx_prop#, where # is the new number */ /* of properties */ switch (obj_type){ case EX_ELEM_BLOCK: strcpy (name, VAR_EB_PROP(num_props+1)); break; case EX_NODE_SET: strcpy (name, VAR_NS_PROP(num_props+1)); break; case EX_SIDE_SET: strcpy (name, VAR_SS_PROP(num_props+1)); break; case EX_ELEM_MAP: strcpy (name, VAR_EM_PROP(num_props+1)); break; case EX_NODE_MAP: strcpy (name, VAR_NM_PROP(num_props+1)); break; default: exerrval = EX_BADPARAM; sprintf(errmsg, "Error: object type %d not supported; file id %d", obj_type, exoid); ex_err("ex_put_prop_array",errmsg,exerrval); goto error_ret; /* Exit define mode and return */ } dims[0] = dimid; ncsetfill(exoid, NC_FILL); /* fill with zeros per routine spec */ if ((propid = ncvardef (exoid, name, NC_LONG, 1, dims)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to create property array variable in file id %d", exoid); ex_err("ex_put_prop_array",errmsg,exerrval); goto error_ret; /* Exit define mode and return */ } ncsetfill(exoid, NC_NOFILL); /* default: nofill */ /* store property name as attribute of property array variable */ if ((ncattput (exoid, propid, ATT_PROP_NAME, NC_CHAR, strlen(prop_name)+1, prop_name)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store property name %s in file id %d", prop_name,exoid); ex_err("ex_put_prop_array",errmsg,exerrval); goto error_ret; /* Exit define mode and return */ } /* leave define mode */ if (ncendef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to leave define mode in file id %d", exoid); ex_err("ex_put_prop_array",errmsg,exerrval); return (EX_FATAL); } } /* put num_obj values in property array */ /* this contortion is necessary because netCDF is expecting nclongs; fortunately it's necessary only when ints and nclongs aren't the same size */ start[0] = 0; count[0] = num_obj; if (sizeof(int) == sizeof(nclong)) { iresult = ncvarput (exoid, propid, start, count, values); } else { lptr = itol (values, (int)num_obj); iresult = ncvarput (exoid, propid, start, count, lptr); free(lptr); } if (iresult == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store property values in file id %d", exoid); ex_err("ex_put_prop_array",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: ncsetfill(exoid, NC_NOFILL); /* default: nofill */ if (ncendef (exoid) == -1) /* exit define mode */ { sprintf(errmsg, "Error: failed to complete definition for file id %d", exoid); ex_err("ex_put_prop_array",errmsg,exerrval); } return (EX_FATAL); }
int ex_put_elem_block (int exoid, int elem_blk_id, const char *elem_type, int num_elem_this_blk, int num_nodes_per_elem, int num_attr) { int varid, dimid, dims[2], elem_blk_id_ndx, elem_blk_stat, strdim; long start[2], num_elem_blk; nclong ldum; int cur_num_elem_blk, nelnoddim, numelbdim, numattrdim, connid; char *cdum; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ cdum = 0; /* first check if any element blocks are specified */ if ((dimid = (ncdimid (exoid, DIM_NUM_EL_BLK))) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: no element blocks defined in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Get number of element blocks defined for this file */ if ((ncdiminq (exoid,dimid,cdum,&num_elem_blk)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of element blocks in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Next: Make sure that this is not a duplicate element block id by searching the VAR_ID_EL_BLK array. WARNING: This must be done outside of define mode because id_lkup accesses the database to determine the position */ if ((varid = ncvarid (exoid, VAR_ID_EL_BLK)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate element block ids in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); } elem_blk_id_ndx = ex_id_lkup(exoid,VAR_ID_EL_BLK,elem_blk_id); if (exerrval != EX_LOOKUPFAIL) /* found the element block id */ { exerrval = EX_FATAL; sprintf(errmsg, "Error: element block id %d already exists in file id %d", elem_blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Keep track of the total number of element blocks defined using a counter stored in a linked list keyed by exoid. NOTE: ex_get_file_item is a function that finds the number of element blocks for a specific file and returns that value incremented. */ cur_num_elem_blk=ex_get_file_item(exoid, &eb_ctr_list); if (cur_num_elem_blk >= num_elem_blk) { exerrval = EX_FATAL; sprintf(errmsg, "Error: exceeded number of element blocks (%ld) defined in file id %d", num_elem_blk,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* NOTE: ex_get_file_item is a function that finds the number of element blocks for a specific file and returns that value incremented. */ cur_num_elem_blk=ex_inc_file_item(exoid, &eb_ctr_list); start[0] = (long)cur_num_elem_blk; /* write out element block id to previously defined id array variable*/ ldum = (nclong)elem_blk_id; if (ncvarput1 (exoid, varid, start, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element block id to file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } elem_blk_id_ndx = start[0]+1; /* element id index into VAR_ID_EL_BLK array*/ if (num_elem_this_blk == 0) /* Is this a NULL element block? */ elem_blk_stat = 0; /* change element block status to NULL */ else elem_blk_stat = 1; /* change element block status to TRUE */ if ((varid = ncvarid (exoid, VAR_STAT_EL_BLK)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate element block status in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } ldum = (nclong)elem_blk_stat; if (ncvarput1 (exoid, varid, start, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element id %d status to file id %d", elem_blk_id, exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } if (num_elem_this_blk == 0) /* Is this a NULL element block? */ { return(EX_NOERR); } /* * Check that storage required for connectivity array is less * than 2GB which is maximum size permitted by netcdf * (in large file mode). 1<<29 == max number of integer items. */ if (num_elem_this_blk * num_nodes_per_elem > (1<<29)) { exerrval = EX_BADPARAM; sprintf(errmsg, "Error: Size to store connectivity for element block %d exceeds 2GB in file id %d", elem_blk_id, exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* put netcdf file into define mode */ if (ncredef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* define some dimensions and variables*/ if ((numelbdim = ncdimdef (exoid, DIM_NUM_EL_IN_BLK(elem_blk_id_ndx), (long)num_elem_this_blk)) == -1) { if (ncerr == NC_ENAMEINUSE) /* duplicate entry */ { exerrval = ncerr; sprintf(errmsg, "Error: element block %d already defined in file id %d", elem_blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); } else { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of elements/block for block %d file id %d", elem_blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); } goto error_ret; /* exit define mode and return */ } if ((nelnoddim = ncdimdef (exoid, DIM_NUM_NOD_PER_EL(elem_blk_id_ndx), (long)num_nodes_per_elem)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of nodes/element for block %d in file id %d", elem_blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* element attribute array */ if (num_attr > 0) { if ((numattrdim = ncdimdef (exoid, DIM_NUM_ATT_IN_BLK(elem_blk_id_ndx), (long)num_attr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of attributes in block %d in file id %d", elem_blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } dims[0] = numelbdim; dims[1] = numattrdim; if ((ncvardef (exoid, VAR_ATTRIB(elem_blk_id_ndx), nc_flt_code(exoid), 2, dims)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define attributes for element block %d in file id %d", elem_blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* inquire previously defined dimensions */ if ((strdim = ncdimid (exoid, DIM_STR)) < 0) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get string length in file id %d",exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Attribute names... */ dims[0] = numattrdim; dims[1] = strdim; if (ncvardef (exoid, VAR_NAME_ATTRIB(elem_blk_id_ndx), NC_CHAR, 2, dims) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define element attribute name array in file id %d",exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } /* element connectivity array */ dims[0] = numelbdim; dims[1] = nelnoddim; if ((connid = ncvardef (exoid, VAR_CONN(elem_blk_id_ndx), NC_LONG, 2, dims)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to create connectivity array for block %d in file id %d", elem_blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* store element type as attribute of connectivity variable */ if ((ncattput (exoid, connid, ATT_NAME_ELB, NC_CHAR, strlen(elem_type)+1, (void*) elem_type)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element type name %s in file id %d", elem_type,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* leave define mode */ if (ncendef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to complete element block definition in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (ncendef (exoid) == -1) /* exit define mode */ { sprintf(errmsg, "Error: failed to complete definition for file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); } return (EX_FATAL); }
int ex_get_prop_array (int exoid, int obj_type, const char *prop_name, int *values) { int num_props, i, propid, dimid, iresult; int found = FALSE; long start[1], count[1], num_obj; nclong *longs; char name[MAX_VAR_NAME_LENGTH+1]; char tmpstr[MAX_VAR_NAME_LENGTH+1]; char obj_stype[MAX_VAR_NAME_LENGTH+1]; char dim_name[MAX_VAR_NAME_LENGTH+1]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* open appropriate variable, depending on obj_type and prop_name */ num_props = ex_get_num_props(exoid, obj_type); switch (obj_type) { case EX_ELEM_BLOCK: strcpy (obj_stype, VAR_ID_EL_BLK); strcpy (dim_name, DIM_NUM_EL_BLK); break; case EX_NODE_SET: strcpy (obj_stype, VAR_NS_IDS); strcpy (dim_name, DIM_NUM_NS); break; case EX_SIDE_SET: strcpy (obj_stype, VAR_SS_IDS); strcpy (dim_name, DIM_NUM_SS); break; case EX_ELEM_MAP: strcpy (obj_stype, VAR_EM_PROP(1)); strcpy (dim_name, DIM_NUM_EM); break; case EX_NODE_MAP: strcpy (obj_stype, VAR_NM_PROP(1)); strcpy (dim_name, DIM_NUM_NM); break; default: exerrval = EX_BADPARAM; sprintf(errmsg, "Error: object type %d not supported; file id %d", obj_type, exoid); ex_err("ex_get_prop_array",errmsg,exerrval); return (EX_FATAL); } for (i=1; i<=num_props; i++) { switch (obj_type){ case EX_ELEM_BLOCK: strcpy (name, VAR_EB_PROP(i)); break; case EX_NODE_SET: strcpy (name, VAR_NS_PROP(i)); break; case EX_SIDE_SET: strcpy (name, VAR_SS_PROP(i)); break; case EX_ELEM_MAP: strcpy (name, VAR_EM_PROP(i)); break; case EX_NODE_MAP: strcpy (name, VAR_NM_PROP(i)); break; default: exerrval = EX_BADPARAM; sprintf(errmsg, "Error: object type %d not supported; file id %d", obj_type, exoid); ex_err("ex_get_prop_array",errmsg,exerrval); return(EX_FATAL); } if ((propid = ncvarid (exoid, name)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate property array %s in file id %d", name, exoid); ex_err("ex_get_prop_array",errmsg,exerrval); return (EX_FATAL); } /* compare stored attribute name with passed property name */ memset(tmpstr, 0, MAX_VAR_NAME_LENGTH+1); if ((ncattget (exoid, propid, ATT_PROP_NAME, tmpstr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get property name in file id %d", exoid); ex_err("ex_get_prop_array",errmsg,exerrval); return (EX_FATAL); } if (strcmp(tmpstr, prop_name) == 0) { found = TRUE; break; } } /* if property is not found, return warning */ if (!found) { exerrval = EX_BADPARAM; sprintf(errmsg, "Warning: object type %d, property %s not defined in file id %d", obj_type, prop_name, exoid); ex_err("ex_get_prop_array",errmsg,exerrval); return (EX_WARN); } if ((dimid = ncdimid (exoid, dim_name)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of objects in file id %d", exoid); ex_err("ex_get_prop_array",errmsg, exerrval); return(EX_FATAL); } /* get number of objects */ if (ncdiminq (exoid, dimid, dim_name, &num_obj) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of %s objects in file id %d", obj_stype, exoid); ex_err("ex_get_prop_array",errmsg, exerrval); return (EX_FATAL); } /* read num_obj values from property variable */ /* application code has allocated an array of ints but netcdf is expecting a pointer to nclongs; if ints are different sizes than nclongs, we must allocate an array of nclongs then convert them to ints with ltoi */ start[0] = 0; count[0] = num_obj; if (sizeof(int) == sizeof(nclong)) { iresult = ncvarget (exoid, propid, start, count, values); } else { if (!(longs = static_cast<nclong*>(malloc(num_obj * sizeof(nclong))))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for %s property array for file id %d", obj_stype, exoid); ex_err("ex_get_prop_array",errmsg,exerrval); return (EX_FATAL); } iresult = ncvarget (exoid, propid, start, count, longs); } if (iresult == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to read values in %s property array in file id %d", obj_stype, exoid); ex_err("ex_get_prop_array",errmsg,exerrval); return (EX_FATAL); } if (sizeof(int) != sizeof(nclong)) { ltoi (longs, values, num_obj); free (longs); } return (EX_NOERR); }
int ex_get_info (int exoid, char **info) { int i, j, dimid, varid; long num_info, start[2]; char *ptr; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* inquire previously defined dimensions and variables */ if ((dimid = ncdimid (exoid, DIM_NUM_INFO)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: failed to locate number of info records in file id %d", exoid); ex_err("ex_get_info",errmsg,exerrval); return (EX_WARN); } if (ncdiminq (exoid, dimid, (char *) 0, &num_info) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of info records in file id %d", exoid); ex_err("ex_get_info",errmsg,exerrval); return (EX_FATAL); } /* do this only if there are any information records */ if (num_info > 0) { if ((varid = ncvarid (exoid, VAR_INFO)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate info record data in file id %d", exoid); ex_err("ex_get_info",errmsg,exerrval); return (EX_FATAL); } /* read the information records */ for (i=0; i<num_info; i++) { start[0] = i; start[1] = 0; j = 0; ptr = info[i]; if (ncvarget1 (exoid, varid, start, ptr) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get info record data in file id %d", exoid); ex_err("ex_get_info",errmsg,exerrval); return (EX_FATAL); } while ((*ptr++ != '\0') && (j < MAX_LINE_LENGTH)) { start[1] = ++j; if (ncvarget1 (exoid, varid, start, ptr) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get info record data in file id %d", exoid); ex_err("ex_get_info",errmsg,exerrval); return (EX_FATAL); } } /* delete trailing blanks */ --ptr; while ( --ptr >= info[i] && *ptr == ' ' ); *(++ptr) = '\0'; } } return (EX_NOERR); }
int ex_put_block( int exoid, int blk_type, int blk_id, const char* entry_descrip, int num_entries_this_blk, int num_nodes_per_entry, int num_edges_per_entry, int num_faces_per_entry, int num_attr_per_entry ) { int varid, dimid, dims[2], blk_id_ndx, blk_stat, strdim; long start[2], num_blk; nclong ldum; int cur_num_blk, numblkdim, numattrdim; int nnodperentdim, nedgperentdim = -1, nfacperentdim = -1; int connid, econnid, fconnid; char *cdum; char errmsg[MAX_ERR_LENGTH]; const char* tname; const char* dnumblk; const char* vblkids; const char* vblksta; const char* vnodcon = 0; const char* vedgcon = 0; const char* vfaccon = 0; const char* vattnam = 0; const char* vblkatt = 0; const char* dneblk = 0; const char* dnape = 0; const char* dnnpe = 0; const char* dnepe = 0; const char* dnfpe = 0; struct list_item** ctr_list; exerrval = 0; /* clear error code */ cdum = 0; switch (blk_type) { case EX_EDGE_BLOCK: tname = "edge"; dnumblk = DIM_NUM_ED_BLK; vblkids = VAR_ID_ED_BLK; vblksta = VAR_STAT_ED_BLK; ctr_list = &ed_ctr_list; break; case EX_FACE_BLOCK: tname = "face"; dnumblk = DIM_NUM_FA_BLK; vblkids = VAR_ID_FA_BLK; vblksta = VAR_STAT_FA_BLK; ctr_list = &fa_ctr_list; break; case EX_ELEM_BLOCK: tname = "element"; dnumblk = DIM_NUM_EL_BLK; vblkids = VAR_ID_EL_BLK; vblksta = VAR_STAT_EL_BLK; ctr_list = &eb_ctr_list; break; default: exerrval = EX_BADPARAM; sprintf( errmsg, "Error: Bad block type (%d) specified for file id %d", blk_type, exoid ); ex_err( "ex_put_block", errmsg, exerrval ); return (EX_FATAL); } /* first check if any element blocks are specified */ if ((dimid = (ncdimid (exoid, dnumblk))) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: no element blocks defined in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Get number of element blocks defined for this file */ if ((ncdiminq (exoid,dimid,cdum,&num_blk)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of element blocks in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Next: Make sure that this is not a duplicate element block id by searching the vblkids array. WARNING: This must be done outside of define mode because id_lkup accesses the database to determine the position */ if ((varid = ncvarid (exoid, vblkids)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate element block ids in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); } blk_id_ndx = ex_id_lkup(exoid,vblkids,blk_id); if (exerrval != EX_LOOKUPFAIL) /* found the element block id */ { exerrval = EX_FATAL; sprintf(errmsg, "Error: element block id %d already exists in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Keep track of the total number of element blocks defined using a counter stored in a linked list keyed by exoid. NOTE: ex_get_file_item is a function that finds the number of element blocks for a specific file and returns that value incremented. */ cur_num_blk=ex_get_file_item(exoid, ctr_list); if (cur_num_blk >= num_blk) { exerrval = EX_FATAL; sprintf(errmsg, "Error: exceeded number of element blocks (%ld) defined in file id %d", num_blk,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* NOTE: ex_get_file_item is a function that finds the number of element blocks for a specific file and returns that value incremented. */ cur_num_blk=ex_inc_file_item(exoid, ctr_list); start[0] = (long)cur_num_blk; /* write out element block id to previously defined id array variable*/ ldum = (nclong)blk_id; if (ncvarput1 (exoid, varid, start, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element block id to file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } blk_id_ndx = start[0]+1; /* element id index into vblkids array*/ if (num_entries_this_blk == 0) /* Is this a NULL element block? */ blk_stat = 0; /* change element block status to NULL */ else blk_stat = 1; /* change element block status to TRUE */ if ((varid = ncvarid (exoid, vblksta)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate element block status in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } ldum = (nclong)blk_stat; if (ncvarput1 (exoid, varid, start, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element id %d status to file id %d", blk_id, exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } if (num_entries_this_blk == 0) /* Is this a NULL element block? */ { return(EX_NOERR); } /* * Check that storage required for connectivity array is less * than 2GB which is maximum size permitted by netcdf * (in large file mode). 1<<29 == max number of integer items. */ if (num_entries_this_blk * num_nodes_per_entry > (1<<29)) { exerrval = EX_BADPARAM; sprintf(errmsg, "Error: Size to store connectivity for element block %d exceeds 2GB in file id %d", blk_id, exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* put netcdf file into define mode */ if (ncredef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } switch (blk_type) { case EX_EDGE_BLOCK: dneblk = DIM_NUM_ED_IN_EBLK(blk_id_ndx); dnnpe = DIM_NUM_NOD_PER_ED(blk_id_ndx); dnepe = 0; dnfpe = 0; dnape = DIM_NUM_ATT_IN_EBLK(blk_id_ndx); vblkatt = VAR_EATTRIB(blk_id_ndx); vattnam = VAR_NAME_EATTRIB(blk_id_ndx); vnodcon = VAR_EBCONN(blk_id_ndx); vedgcon = 0; vfaccon = 0; break; case EX_FACE_BLOCK: dneblk = DIM_NUM_FA_IN_FBLK(blk_id_ndx); dnnpe = DIM_NUM_NOD_PER_FA(blk_id_ndx); dnepe = 0; dnfpe = 0; dnape = DIM_NUM_ATT_IN_FBLK(blk_id_ndx); vblkatt = VAR_FATTRIB(blk_id_ndx); vattnam = VAR_NAME_FATTRIB(blk_id_ndx); vnodcon = VAR_FBCONN(blk_id_ndx); vedgcon = 0; vfaccon = 0; break; case EX_ELEM_BLOCK: dneblk = DIM_NUM_EL_IN_BLK(blk_id_ndx); dnnpe = DIM_NUM_NOD_PER_EL(blk_id_ndx); dnepe = DIM_NUM_EDG_PER_EL(blk_id_ndx); dnfpe = DIM_NUM_FAC_PER_EL(blk_id_ndx); dnape = DIM_NUM_ATT_IN_BLK(blk_id_ndx); vblkatt = VAR_ATTRIB(blk_id_ndx); vattnam = VAR_NAME_ATTRIB(blk_id_ndx); vnodcon = VAR_CONN(blk_id_ndx); vedgcon = VAR_ECONN(blk_id_ndx); vfaccon = VAR_FCONN(blk_id_ndx); break; } /* define some dimensions and variables*/ if ((numblkdim = ncdimdef (exoid,dneblk,(long)num_entries_this_blk)) == -1) { if (ncerr == NC_ENAMEINUSE) /* duplicate entry */ { exerrval = ncerr; sprintf(errmsg, "Error: element block %d already defined in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); } else { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of elements/block for block %d file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); } goto error_ret; /* exit define mode and return */ } if ((nnodperentdim = ncdimdef (exoid,dnnpe,(long)num_nodes_per_entry)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of nodes/element for block %d in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } if ( dnepe && num_edges_per_entry > 0 ) { if ((nedgperentdim = ncdimdef (exoid,dnepe,(long)num_edges_per_entry)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of edges/element for block %d in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } if ( dnfpe && num_faces_per_entry > 0 ) { if ((nfacperentdim = ncdimdef (exoid,dnfpe,(long)num_faces_per_entry)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of faces/element for block %d in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } /* element attribute array */ if (num_attr_per_entry > 0) { if ((numattrdim = ncdimdef (exoid, dnape, (long)num_attr_per_entry)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define number of attributes in block %d in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } dims[0] = numblkdim; dims[1] = numattrdim; if ((ncvardef (exoid, vblkatt, nc_flt_code(exoid), 2, dims)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define attributes for element block %d in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* inquire previously defined dimensions */ if ((strdim = ncdimid (exoid, DIM_STR)) < 0) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get string length in file id %d",exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } /* Attribute names... */ dims[0] = numattrdim; dims[1] = strdim; if (ncvardef (exoid, vattnam, NC_CHAR, 2, dims) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define element attribute name array in file id %d",exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } /* element connectivity array */ dims[0] = numblkdim; dims[1] = nnodperentdim; if ((connid = ncvardef (exoid, vnodcon, NC_LONG, 2, dims)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to create connectivity array for block %d in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } /* store element type as attribute of connectivity variable */ if ((ncattput (exoid, connid, ATT_NAME_ELB, NC_CHAR, strlen(entry_descrip)+1, (void*) entry_descrip)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element type name %s in file id %d", entry_descrip,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } if ( vedgcon && num_edges_per_entry ) { dims[0] = numblkdim; dims[1] = nedgperentdim; if ((econnid = ncvardef (exoid, vedgcon, NC_LONG, 2, dims)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to create edge connectivity array for block %d in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } if ( vfaccon && num_faces_per_entry ) { dims[0] = numblkdim; dims[1] = nfacperentdim; if ((fconnid = ncvardef (exoid, vfaccon, NC_LONG, 2, dims)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to create face connectivity array for block %d in file id %d", blk_id,exoid); ex_err("ex_put_elem_block",errmsg,exerrval); goto error_ret; /* exit define mode and return */ } } /* leave define mode */ if (ncendef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to complete element block definition in file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (ncendef (exoid) == -1) /* exit define mode */ { sprintf(errmsg, "Error: failed to complete definition for file id %d", exoid); ex_err("ex_put_elem_block",errmsg,exerrval); } return (EX_FATAL); }
int ex_put_num_map ( int exoid, int map_type, int map_id, const int *map ) { int dimid, varid, iresult; long start[1]; nclong ldum, *lptr; long num_maps, num_entries, count[1]; int cur_num_maps; char *cdum; char errmsg[MAX_ERR_LENGTH]; const char* tname; const char* dnumentries; const char* dnummaps; const char* vmapids; const char* vmap = 0; struct list_item** map_ctr_list; exerrval = 0; /* clear error code */ cdum = 0; switch ( map_type ) { case EX_NODE_MAP: tname = "node"; dnumentries = DIM_NUM_NODES; dnummaps = DIM_NUM_NM; vmapids = VAR_NM_PROP(1); map_ctr_list = &nm_ctr_list; break; case EX_EDGE_MAP: tname = "edge"; dnumentries = DIM_NUM_EDGE; dnummaps = DIM_NUM_EDM; vmapids = VAR_EDM_PROP(1); map_ctr_list = &edm_ctr_list; break; case EX_FACE_MAP: tname = "face"; dnumentries = DIM_NUM_FACE; dnummaps = DIM_NUM_FAM; vmapids = VAR_FAM_PROP(1); map_ctr_list = &fam_ctr_list; break; case EX_ELEM_MAP: tname = "element"; dnumentries = DIM_NUM_ELEM; dnummaps = DIM_NUM_EM; vmapids = VAR_EM_PROP(1); map_ctr_list = &em_ctr_list; break; default: exerrval = EX_BADPARAM; sprintf( errmsg, "Error: Bad map type (%d) specified for file id %d", map_type, exoid ); ex_err( "ex_put_num_map", errmsg, exerrval ); return (EX_FATAL); } /* Make sure the file contains entries */ if ((dimid = (ncdimid (exoid, dnumentries))) == -1 ) { return (EX_NOERR); } /* first check if any maps are specified */ if ((dimid = (ncdimid (exoid, dnummaps))) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: no %s maps specified in file id %d", tname,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } /* Check for duplicate map id entry */ ex_id_lkup(exoid,vmapids,map_id); if (exerrval != EX_LOOKUPFAIL) /* found the map id */ { exerrval = ncerr; sprintf(errmsg, "Error: %s map %d already defined in file id %d", tname,map_id,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return(EX_FATAL); } /* Get number of maps initialized for this file */ if ((ncdiminq (exoid,dimid,cdum,&num_maps)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of %s maps in file id %d", tname,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } /* Keep track of the total number of maps defined using a counter stored in a linked list keyed by exoid. NOTE: ex_get_file_item is used to find the number of maps for a specific file and returns that value. */ cur_num_maps = ex_get_file_item(exoid, map_ctr_list ); if (cur_num_maps >= num_maps) { exerrval = EX_FATAL; sprintf(errmsg, "Error: exceeded number of %s maps (%ld) specified in file id %d", tname,num_maps,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } /* NOTE: ex_inc_file_item is used to find the number of maps for a specific file and returns that value incremented. */ cur_num_maps = ex_inc_file_item(exoid, map_ctr_list ); /* write out information to previously defined variable */ /* first get id of variable */ if ((varid = ncvarid (exoid, vmapids)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate %s map ids in file id %d", tname,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } /* then, write out map id */ start[0] = cur_num_maps; ldum = (nclong)map_id; if (ncvarput1 (exoid, varid, start, &ldum) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store %s map id %d in file id %d", tname,map_id,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } /* determine number of entries */ if ((dimid = (ncdimid (exoid, dnumentries))) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: couldn't determine number of %s entries in file id %d", tname,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &num_entries) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of %s entries in file id %d", tname,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } switch ( map_type ) { case EX_NODE_MAP: vmap = VAR_NODE_MAP(cur_num_maps+1); break; case EX_EDGE_MAP: vmap = VAR_EDGE_MAP(cur_num_maps+1); break; case EX_FACE_MAP: vmap = VAR_FACE_MAP(cur_num_maps+1); break; case EX_ELEM_MAP: vmap = VAR_ELEM_MAP(cur_num_maps+1); break; } /* locate variable array in which to store the map */ if ((varid = ncvarid(exoid,vmap)) == -1) { #if 0 exerrval = ncerr; sprintf(errmsg, "Error: failed to locate %s map %d in file id %d", vmap,map_id,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); #endif int dims[2]; ncerr = 0; if ( ncredef( exoid ) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: failed to place file id %d into define mode", exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } dims[0] = dimid; if ( (varid = ncvardef( exoid, vmap, NC_LONG, 1, dims )) == -1 ) { exerrval = ncerr; sprintf(errmsg, "Error: failed to define map %s in file id %d", vmap, exoid); ex_err("ex_put_num_map",errmsg,exerrval); } if ( ncendef( exoid ) == -1 ) { /* exit define mode */ sprintf( errmsg, "Error: failed to complete definition for file id %d", exoid ); ex_err( "ex_put_num_map", errmsg, exerrval ); varid = -1; /* force early exit */ } if ( varid == -1 ) /* we couldn't define variable and have prepared error message. */ return (EX_FATAL); } /* write out the map */ /* this contortion is necessary because netCDF is expecting nclongs; fortunately it's necessary only when ints and nclongs aren't the same size */ start[0] = 0; count[0] = num_entries; if (sizeof(int) == sizeof(nclong)) { iresult = ncvarput (exoid, varid, start, count, map); } else { lptr = itol (map, (int)num_entries); iresult = ncvarput (exoid, varid, start, count, lptr); free(lptr); } if (iresult == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store %s map in file id %d", tname,exoid); ex_err("ex_put_num_map",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); }
int ex_get_side_set_dist_fact (int exoid, int side_set_id, void *side_set_dist_fact) { int dimid, dist_id, side_set_id_ndx; long num_df_in_set, count[1], start[1]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* first check if any side sets are specified */ if ((dimid = ncdimid (exoid, DIM_NUM_SS)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no side sets stored in file id %d", exoid); ex_err("ex_get_side_set_dist_fact",errmsg,exerrval); return (EX_WARN); } /* Lookup index of side set id in VAR_SS_IDS array */ side_set_id_ndx = ex_id_lkup(exoid,VAR_SS_IDS,side_set_id); if (exerrval != 0) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: side set %d is NULL in file id %d", side_set_id,exoid); ex_err("ex_get_side_set_dist_fact",errmsg,EX_MSG); return (EX_WARN); } else { sprintf(errmsg, "Error: failed to locate side set id %d in VAR_SS_IDS array in file id %d", side_set_id,exoid); ex_err("ex_get_side_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } } /* inquire id's of previously defined dimensions and variables */ if ((dimid = ncdimid (exoid, DIM_NUM_DF_SS(side_set_id_ndx))) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: dist factors not stored for side set %d in file id %d", side_set_id,exoid); ex_err("ex_get_side_set_dist_fact",errmsg,exerrval); return (EX_WARN); /* complain - but not too loud */ } if (ncdiminq (exoid, dimid, (char *) 0, &num_df_in_set) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of dist factors in side set %d in file id %d", side_set_id,exoid); ex_err("ex_get_side_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } if ((dist_id = ncvarid (exoid, VAR_FACT_SS(side_set_id_ndx))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate dist factors list for side set %d in file id %d", side_set_id,exoid); ex_err("ex_get_side_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } /* read in the distribution factors array */ start[0] = 0; count[0] = num_df_in_set; if (ncvarget (exoid, dist_id, start, count, ex_conv_array(exoid,RTN_ADDRESS,side_set_dist_fact, (int)num_df_in_set)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get dist factors list for side set %d in file id %d", side_set_id,exoid); ex_err("ex_get_side_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } ex_conv_array( exoid, READ_CONVERT, side_set_dist_fact, num_df_in_set ); return (EX_NOERR); }
int ex_get_side_set_ids (int exoid, int *ids) { int dimid, varid, iresult; long num_side_sets, start[1], count[1]; nclong *longs; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* inquire id's of previously defined dimensions and variables */ if ((dimid = ncdimid (exoid, DIM_NUM_SS)) < 0) { exerrval = ncerr; sprintf(errmsg, "Warning: no side sets stored in file id %d", exoid); ex_err("ex_get_side_set_ids",errmsg,exerrval); return (EX_WARN); } if (ncdiminq (exoid, dimid, (char *) 0, &num_side_sets) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of side sets in file id %d", exoid); ex_err("ex_get_side_set_ids",errmsg,exerrval); return (EX_FATAL); } if ((varid = ncvarid (exoid, VAR_SS_IDS)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate side set ids in file id %d", exoid); ex_err("ex_get_side_set_ids",errmsg,exerrval); return (EX_FATAL); } /* read in the side set ids */ /* application code has allocated an array of ints but netcdf is expecting a pointer to nclongs; if ints are different sizes than nclongs, we must allocate an array of nclongs then convert them to ints with ltoi */ start[0] = 0; count[0] = num_side_sets; if (sizeof(int) == sizeof(nclong)) { iresult = ncvarget (exoid, varid, start, count, ids); } else { if (!(longs = malloc(num_side_sets * sizeof(nclong)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for side set ids for file id %d", exoid); ex_err("ex_get_side_set_ids",errmsg,exerrval); return (EX_FATAL); } iresult = ncvarget (exoid, varid, start, count, longs); } if (iresult == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get side set ids in file id %d", exoid); ex_err("ex_get_side_set_ids",errmsg,exerrval); return (EX_FATAL); } if (sizeof(int) != sizeof(nclong)) { ltoi (longs, ids, num_side_sets); free (longs); } return(EX_NOERR); }
int ex_get_concat_sets (int exoid, int set_type, struct ex_set_specs* set_specs) { int *set_ids = set_specs->sets_ids; int *num_entries_per_set = set_specs->num_entries_per_set; int *num_dist_per_set = set_specs->num_dist_per_set; int *sets_entry_index = set_specs->sets_entry_index; int *sets_dist_index = set_specs->sets_dist_index; int *sets_entry_list = set_specs->sets_entry_list; int *sets_extra_list = set_specs->sets_extra_list; void *sets_dist_fact = set_specs->sets_dist_fact; char *cdum; int num_sets, i; float fdum; float *flt_dist_fact; double *dbl_dist_fact; char errmsg[MAX_ERR_LENGTH]; char* typeName; char* dimptr; char* idsptr; int ex_inq_val; int *extra_list; exerrval = 0; /* clear error code */ cdum = 0; /* initialize even though it is not used */ /* setup pointers based on set_type NOTE: there is another block that sets more stuff later ... */ if (set_type == EX_NODE_SET) { typeName = "node"; ex_inq_val = EX_INQ_NODE_SETS; dimptr = DIM_NUM_NS; idsptr = VAR_NS_IDS; } else if (set_type == EX_EDGE_SET) { typeName = "edge"; ex_inq_val = EX_INQ_EDGE_SETS; dimptr = DIM_NUM_ES; idsptr = VAR_ES_IDS; } else if (set_type == EX_FACE_SET) { typeName = "face"; ex_inq_val = EX_INQ_FACE_SETS; dimptr = DIM_NUM_FS; idsptr = VAR_FS_IDS; } else if (set_type == EX_SIDE_SET) { typeName = "side"; ex_inq_val = EX_INQ_SIDE_SETS; dimptr = DIM_NUM_SS; idsptr = VAR_SS_IDS; } else if (set_type == EX_ELEM_SET) { typeName = "elem"; ex_inq_val = EX_INQ_ELEM_SETS; dimptr = DIM_NUM_ELS; idsptr = VAR_ELS_IDS; } else { exerrval = EX_FATAL; sprintf(errmsg, "Error: invalid set type (%d)", set_type); ex_err("ex_put_set_param",errmsg,exerrval); return (EX_FATAL); } /* first check if any sets are specified */ if (ncdimid (exoid, dimptr) == -1) { if (ncerr == NC_EBADDIM) { exerrval = ncerr; sprintf(errmsg, "Warning: no %s sets defined for file id %d", typeName, exoid); ex_err("ex_get_concat_sets",errmsg,exerrval); return (EX_WARN); } else { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate %s sets defined in file id %d", typeName, exoid); ex_err("ex_get_concat_sets",errmsg,exerrval); return (EX_FATAL); } } /* inquire how many sets have been stored */ if (ex_inquire(exoid, ex_inq_val, &num_sets, &fdum, cdum) == -1) { sprintf(errmsg, "Error: failed to get number of %s sets defined for file id %d", typeName, exoid); /* use error val from inquire */ ex_err("ex_get_concat_sets",errmsg,exerrval); return (EX_FATAL); } if (ex_get_ids (exoid, set_type, set_ids) == -1) { sprintf(errmsg, "Error: failed to get %s set ids for file id %d", typeName, exoid); /* use error val from inquire */ ex_err("ex_get_concat_sets",errmsg,exerrval); return (EX_FATAL); } sets_entry_index[0] = 0; sets_dist_index[0] = 0; for (i=0; i<num_sets; i++) { if (ex_get_set_param(exoid, set_type, set_ids[i], &(num_entries_per_set[i]), &(num_dist_per_set[i])) == -1) return(EX_FATAL); /* error will be reported by sub */ if (i < num_sets-1) { /* fill in entry and dist factor index arrays */ sets_entry_index[i+1] = sets_entry_index[i]+num_entries_per_set[i]; sets_dist_index[i+1] = sets_dist_index[i]+num_dist_per_set[i]; } if (num_entries_per_set[i] == 0) /* NULL set? */ continue; /* Now, use ExodusII call to get sets */ if (set_type == EX_EDGE_SET || set_type == EX_FACE_SET || set_type == EX_SIDE_SET) extra_list = &(sets_extra_list[sets_entry_index[i]]); else extra_list = NULL; if (ex_comp_ws(exoid) == sizeof(float)) { if (ex_get_set(exoid, set_type, set_ids[i], &(sets_entry_list[sets_entry_index[i]]), &(sets_extra_list[sets_entry_index[i]])) == -1) return(EX_FATAL); /* error will be reported by subroutine */ /* get distribution factors for this set */ flt_dist_fact = sets_dist_fact; if (num_dist_per_set[i] > 0) /* only get df if they exist */ { if (ex_get_set_dist_fact(exoid, set_type, set_ids[i], &(flt_dist_fact[sets_dist_index[i]])) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get %s set %d dist factors in file id %d", typeName, set_ids[i], exoid); ex_err("ex_get_concat_sets",errmsg,exerrval); return(EX_FATAL); } } else { /* fill distribution factor array with 1's */ } } else if (ex_comp_ws(exoid) == sizeof(double)) { if (ex_get_set(exoid, set_type, set_ids[i], &(sets_entry_list[sets_entry_index[i]]), &(sets_extra_list[sets_entry_index[i]])) == -1) return(EX_FATAL); /* error will be reported by subroutine */ /* get distribution factors for this set */ dbl_dist_fact = sets_dist_fact; if (num_dist_per_set[i] > 0) /* only get df if they exist */ { if (ex_get_set_dist_fact(exoid, set_type, set_ids[i], &(dbl_dist_fact[sets_dist_index[i]])) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get %s set %d dist factors in file id %d", typeName, set_ids[i], exoid); ex_err("ex_get_concat_sets",errmsg,exerrval); return(EX_FATAL); } } else { /* fill distribution factor array with 1's */ } } } return(EX_NOERR); }
int ex_get_partial_elem_map (int exoid, int map_id, int ent_start, int ent_count, int *elem_map) { int dimid, var_id, id_ndx, iresult; long num_elem, start[1], count[1]; nclong *longs; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* See if file contains any elements...*/ if ((dimid = ncdimid (exoid, DIM_NUM_ELEM)) == -1) { return (EX_NOERR); } if (ncdiminq (exoid, dimid, (char *) 0, &num_elem) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of elements in file id %d", exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* Check input parameters for a valid range of numbers */ if (ent_start <= 0 || ent_start > num_elem) { exerrval = EX_FATAL; sprintf(errmsg, "Error: start count is invalid in file id %d", exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } if (ent_count < 0) { exerrval = EX_FATAL; sprintf(errmsg, "Error: Invalid count value in file id %d", exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } if (ent_start+ent_count-1 > num_elem) { exerrval = EX_FATAL; sprintf(errmsg, "Error: start+count-1 is larger than element count in file id %d", exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* first check if any element maps have been defined */ if ((dimid = ncdimid (exoid, DIM_NUM_EM)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no element maps defined in file id %d", exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_WARN); } /* Lookup index of element map id property array */ id_ndx = ex_id_lkup(exoid,VAR_EM_PROP(1),map_id); if (exerrval != 0) { sprintf(errmsg, "Error: failed to locate element map id %d in %s in file id %d", map_id,VAR_EM_PROP(1),exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* inquire id's of previously defined dimensions and variables */ if ((var_id = ncvarid (exoid, VAR_ELEM_MAP(id_ndx))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate element map %d in file id %d", map_id,exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } /* read in the element map */ /* application code has allocated an array of ints but netcdf is expecting a pointer to nclongs; if ints are different sizes than nclongs, we must allocate an array of nclongs then convert them to ints with ltoi */ start[0] = ent_start-1; count[0] = ent_count; if (sizeof(int) == sizeof(nclong)) { iresult = ncvarget (exoid, var_id, start, count, elem_map); } else { if (!(longs = malloc(ent_count * sizeof(nclong)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for element map for file id %d", exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } iresult = ncvarget (exoid, var_id, start, count, longs); } if (iresult == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get element map in file id %d", exoid); ex_err("ex_get_partial_elem_map",errmsg,exerrval); return (EX_FATAL); } if (sizeof(int) != sizeof(nclong)) { ltoi (longs, elem_map, ent_count); free (longs); } return (EX_NOERR); }
int main(int argc, char **argv) { /* NIFTI stuff */ nifti_image *nii_ptr; nifti_image nii_rec; int nii_dimids[MAX_NII_DIMS]; int nii_dir[MAX_NII_DIMS]; int nii_map[MAX_NII_DIMS]; unsigned long nii_lens[MAX_NII_DIMS]; int nii_ndims; static int nifti_filetype; static int nifti_datatype; static int nifti_signed = 1; /* MINC stuff */ int mnc_fd; /* MINC file descriptor */ nc_type mnc_type; /* MINC data type as read */ int mnc_ndims; /* MINC image dimension count */ int mnc_dimids[MAX_VAR_DIMS]; /* MINC image dimension identifiers */ long mnc_dlen; /* MINC dimension length value */ double mnc_dstep; /* MINC dimension step value */ int mnc_icv; /* MINC image conversion variable */ int mnc_vid; /* MINC Image variable ID */ long mnc_start[MAX_VAR_DIMS]; /* MINC data starts */ long mnc_count[MAX_VAR_DIMS]; /* MINC data counts */ int mnc_signed; /* MINC if output voxels are signed */ double real_range[2]; /* MINC real range (min, max) */ double input_valid_range[2]; /* MINC valid range (min, max) */ double output_valid_range[2]; /* Valid range of output data. */ double nifti_slope; /* Slope to be applied to output voxels. */ double nifti_inter; /* Intercept to be applied to output voxels. */ double total_valid_range; /* Overall valid range (max - min). */ double total_real_range; /* Overall real range (max - min). */ /* Other stuff */ char out_str[1024]; /* Big string for filename */ char att_str[1024]; /* Big string for attribute values */ int i; /* Generic loop counter the first */ int j; /* Generic loop counter the second */ char *str_ptr; /* Generic ASCIZ string pointer */ int r; /* Result code. */ static int vflag = 0; /* Verbose flag (default is quiet) */ static ArgvInfo argTable[] = { {NULL, ARGV_HELP, NULL, NULL, "Output voxel data type specification"}, {"-byte", ARGV_CONSTANT, (char *)DT_INT8, (char *)&nifti_datatype, "Write voxel data in 8-bit signed integer format."}, {"-short", ARGV_CONSTANT, (char *)DT_INT16, (char *)&nifti_datatype, "Write voxel data in 16-bit signed integer format."}, {"-int", ARGV_CONSTANT, (char *)DT_INT32, (char *)&nifti_datatype, "Write voxel data in 32-bit signed integer format."}, {"-float", ARGV_CONSTANT, (char *)DT_FLOAT32, (char *)&nifti_datatype, "Write voxel data in 32-bit floating point format."}, {"-double", ARGV_CONSTANT, (char *)DT_FLOAT64, (char *)&nifti_datatype, "Write voxel data in 64-bit floating point format."}, {"-signed", ARGV_CONSTANT, (char *)1, (char *)&nifti_signed, "Write integer voxel data in signed format."}, {"-unsigned", ARGV_CONSTANT, (char *)0, (char *)&nifti_signed, "Write integer voxel data in unsigned format."}, {NULL, ARGV_HELP, NULL, NULL, "Output file format specification"}, {"-dual", ARGV_CONSTANT, (char *)FT_NIFTI_DUAL, (char *)&nifti_filetype, "Write NIfTI-1 two-file format (.img and .hdr)"}, {"-ASCII", ARGV_CONSTANT, (char *)FT_NIFTI_ASCII, (char *)&nifti_filetype, "Write NIfTI-1 ASCII header format (.nia)"}, {"-nii", ARGV_CONSTANT, (char *)FT_NIFTI_SINGLE, (char *)&nifti_filetype, "Write NIfTI-1 one-file format (.nii)"}, {"-analyze", ARGV_CONSTANT, (char *)FT_ANALYZE, (char *)&nifti_filetype, "Write an Analyze two-file format file (.img and .hdr)"}, {NULL, ARGV_HELP, NULL, NULL, "Other options"}, {"-quiet", ARGV_CONSTANT, (char *)0, (char *)&vflag, "Quiet operation"}, {"-verbose", ARGV_CONSTANT, (char *)1, (char *)&vflag, "Quiet operation"}, {NULL, ARGV_END, NULL, NULL, NULL} }; ncopts = 0; /* Clear global netCDF error reporting flag */ /* Default NIfTI file type is "NII", single binary file */ nifti_filetype = FT_UNSPECIFIED; nifti_datatype = DT_UNKNOWN; if (ParseArgv(&argc, argv, argTable, 0) || (argc < 2)) { fprintf(stderr, "Too few arguments\n"); return usage(); } if (!nifti_signed) { switch (nifti_datatype) { case DT_INT8: nifti_datatype = DT_UINT8; break; case DT_INT16: nifti_datatype = DT_UINT16; break; case DT_INT32: nifti_datatype = DT_UINT32; break; } } switch (nifti_datatype){ case DT_INT8: case DT_UINT8: mnc_type = NC_BYTE; break; case DT_INT16: case DT_UINT16: mnc_type = NC_SHORT; break; case DT_INT32: case DT_UINT32: mnc_type = NC_INT; break; case DT_FLOAT32: mnc_type = NC_FLOAT; break; case DT_FLOAT64: mnc_type = NC_DOUBLE; break; } if (argc == 2) { strcpy(out_str, argv[1]); str_ptr = strrchr(out_str, '.'); if (str_ptr != NULL && !strcmp(str_ptr, ".mnc")) { *str_ptr = '\0'; } } else if (argc == 3) { strcpy(out_str, argv[2]); str_ptr = strrchr(out_str, '.'); if (str_ptr != NULL) { /* See if a recognized file extension was specified. If so, * we trim it off and set the output file type if none was * specified. If the extension is not recognized, assume * that we will form the filename by just adding the right * extension for the selected output format. */ if (!strcmp(str_ptr, ".nii")) { if (nifti_filetype == FT_UNSPECIFIED) { nifti_filetype = FT_NIFTI_SINGLE; } *str_ptr = '\0'; } else if (!strcmp(str_ptr, ".img") || !strcmp(str_ptr, ".hdr")) { if (nifti_filetype == FT_UNSPECIFIED) { nifti_filetype = FT_NIFTI_DUAL; } *str_ptr = '\0'; } else if (!strcmp(str_ptr, ".nia")) { if (nifti_filetype == FT_UNSPECIFIED) { nifti_filetype = FT_NIFTI_ASCII; } *str_ptr = '\0'; } } } else { fprintf(stderr, "Filename argument required\n"); return usage(); } /* Open the MINC file. It needs to exist. */ mnc_fd = miopen(argv[1], NC_NOWRITE); if (mnc_fd < 0) { fprintf(stderr, "Can't find input file '%s'\n", argv[1]); return (-1); } /* Find the MINC image variable. If we can't find it, there is no * further processing possible... */ mnc_vid = ncvarid(mnc_fd, MIimage); if (mnc_vid < 0) { fprintf(stderr, "Can't locate the image variable (mnc_vid=%d)\n", mnc_vid); return (-1); } /* Find out about the MINC image variable - specifically, how many * dimensions, and which dimensions. */ r = ncvarinq(mnc_fd, mnc_vid, NULL, NULL, &mnc_ndims, mnc_dimids, NULL); if (r < 0) { fprintf(stderr, "Can't read information from image variable\n"); return (-1); } if (mnc_ndims > MAX_NII_DIMS) { fprintf(stderr, "NIfTI-1 files may contain at most %d dimensions\n", MAX_NII_DIMS); return (-1); } /* Initialize the NIfTI structure */ nii_ptr = &nii_rec; init_nifti_header(nii_ptr); /* For now we just use the mnc2nii command line as the description * field. Probably we should use something better, perhaps a * combination of some other standard MINC fields that might * provide more information. */ str_ptr = nii_ptr->descrip; for (i = 0; i < argc; i++) { char *arg_ptr = argv[i]; if ((str_ptr - nii_ptr->descrip) >= MAX_NII_DESCRIP) { break; } if (i != 0) { *str_ptr++ = ' '; } while (*arg_ptr != '\0' && (str_ptr - nii_ptr->descrip) < MAX_NII_DESCRIP) { *str_ptr++ = *arg_ptr++; } *str_ptr = '\0'; } nii_ptr->fname = malloc(strlen(out_str) + 4 + 1); nii_ptr->iname = malloc(strlen(out_str) + 4 + 1); strcpy(nii_ptr->fname, out_str); strcpy(nii_ptr->iname, out_str); switch (nifti_filetype) { case FT_ANALYZE: strcat(nii_ptr->fname, ".hdr"); strcat(nii_ptr->iname, ".img"); break; case FT_NIFTI_SINGLE: strcat(nii_ptr->fname, ".nii"); strcat(nii_ptr->iname, ".nii"); break; case FT_NIFTI_DUAL: strcat(nii_ptr->fname, ".hdr"); strcat(nii_ptr->iname, ".img"); break; case FT_NIFTI_ASCII: strcat(nii_ptr->fname, ".nia"); strcat(nii_ptr->iname, ".nia"); break; default: fprintf(stderr, "Unknown output file type %d\n", nifti_filetype); return (-1); } /* Get real voxel range for the input file. */ miget_image_range(mnc_fd, real_range); /* Get the actual valid voxel value range. */ miget_valid_range(mnc_fd, mnc_vid, input_valid_range); /* Find the default range for the output type. Our output file * will use the full legal range of the output type if it is * an integer. */ if (nifti_datatype == DT_UNKNOWN) { nii_ptr->datatype = DT_FLOAT32; /* Default */ mnc_type = NC_FLOAT; mnc_signed = 1; } else { nii_ptr->datatype = nifti_datatype; mnc_signed = nifti_signed; } if (vflag) { fprintf(stderr, "MINC type %d signed %d\n", mnc_type, mnc_signed); } miget_default_range(mnc_type, mnc_signed, output_valid_range); total_valid_range = input_valid_range[1] - input_valid_range[0]; total_real_range = real_range[1] - real_range[0]; if ((output_valid_range[1] - output_valid_range[0]) > total_valid_range) { /* Empirically, forcing the valid range to be the nearest power * of two greater than the existing valid range seems to improve * the behavior of the conversion. This is at least in part because * of the limited precision of the NIfTI-1 voxel scaling fields. */ double new_range = nearest_power_of_two(total_valid_range); if (new_range - 1.0 >= total_valid_range) { new_range -= 1.0; } if (output_valid_range[1] > total_valid_range) { output_valid_range[0] = 0; output_valid_range[1] = new_range; } else { output_valid_range[1] = output_valid_range[0] + new_range; } } else { /* The new range can't fully represent the input range. Use the * full available range, and warn the user that they may have a * problem. */ printf("WARNING: Range of input exceeds range of output format.\n"); } if (vflag) { printf("Real range: %f %f Input valid range: %f %f Output valid range: %f %f\n", real_range[0], real_range[1], input_valid_range[0], input_valid_range[1], output_valid_range[0], output_valid_range[1]); } /* If the output type is not floating point, we may need to scale the * voxel values. */ if (mnc_type != NC_FLOAT && mnc_type != NC_DOUBLE) { /* Figure out how to map pixel values into the range of the * output datatype. */ nifti_slope = ((real_range[1] - real_range[0]) / (output_valid_range[1] - output_valid_range[0])); if (nifti_slope == 0.0) { nifti_slope = 1.0; } nifti_inter = real_range[0] - (output_valid_range[0] * nifti_slope); /* One problem with NIfTI-1 is the limited precision of the * scl_slope and scl_inter fields (they are just 32-bits). So * we look for possible issues and warn about that here. */ if (nifti_inter != (float) nifti_inter || nifti_slope != (float) nifti_slope) { double epsilon_i = nifti_inter - (float) nifti_inter; double epsilon_s = nifti_slope - (float) nifti_slope; /* If the loss in precision is more than one part per thousand * of the real range, flag this as a problem! */ if ((epsilon_i > total_real_range / 1.0e3) || (epsilon_s > total_real_range / 1.0e3)) { fprintf(stderr, "ERROR: Slope and intercept cannot be represented in the NIfTI-1 header.\n"); fprintf(stderr, " slope %f (%f), intercept %f (%f)\n", nifti_slope, (float) nifti_slope, nifti_inter, (float) nifti_inter); return (-1); } } } else { nifti_slope = 0.0; } nii_ptr->scl_slope = nifti_slope; nii_ptr->scl_inter = nifti_inter; nii_ptr->nvox = 1; /* Initial value for voxel count */ /* Find all of the dimensions of the MINC file, in the order they * will be listed in the NIfTI-1/Analyze file. We use this to build * a map for restructuring the data according to the normal rules * of NIfTI-1. */ nii_ndims = 0; for (i = 0; i < MAX_NII_DIMS; i++) { if (dimnames[i] == NULL) { nii_dimids[nii_ndims] = -1; continue; } nii_dimids[nii_ndims] = ncdimid(mnc_fd, dimnames[i]); if (nii_dimids[nii_ndims] == -1) { continue; } /* Make sure the dimension is actually used to define the image. */ for (j = 0; j < mnc_ndims; j++) { if (nii_dimids[nii_ndims] == mnc_dimids[j]) { nii_map[nii_ndims] = j; break; } } if (j < mnc_ndims) { mnc_dlen = 1; mnc_dstep = 0; ncdiminq(mnc_fd, nii_dimids[nii_ndims], NULL, &mnc_dlen); ncattget(mnc_fd, ncvarid(mnc_fd, dimnames[i]), MIstep, &mnc_dstep); if (mnc_dstep < 0) { nii_dir[nii_ndims] = -1; mnc_dstep = -mnc_dstep; } else { nii_dir[nii_ndims] = 1; } nii_lens[nii_ndims] = mnc_dlen; nii_ndims++; } nii_ptr->dim[dimmap[i]] = (int) mnc_dlen; nii_ptr->nvox *= mnc_dlen; nii_ptr->pixdim[dimmap[i]] = (float) mnc_dstep; } /* Here we do some "post-processing" of the results. Make certain that * the nt value is never zero, and make certain that ndim is set to * 4 if there is a time dimension and 5 if there is a vector dimension */ if (nii_ptr->dim[3] > 1 && nii_ndims < 4) { nii_ndims = 4; } if (nii_ptr->dim[4] > 1) { nii_ptr->intent_code = NIFTI_INTENT_VECTOR; nii_ndims = 5; } nii_ptr->ndim = nii_ndims; /* Total number of dimensions in file */ nii_ptr->nx = nii_ptr->dim[0]; nii_ptr->ny = nii_ptr->dim[1]; nii_ptr->nz = nii_ptr->dim[2]; nii_ptr->nt = nii_ptr->dim[3]; nii_ptr->nu = nii_ptr->dim[4]; nii_ptr->dx = nii_ptr->pixdim[0]; nii_ptr->dy = nii_ptr->pixdim[1]; nii_ptr->dz = nii_ptr->pixdim[2]; nii_ptr->dt = nii_ptr->pixdim[3]; nii_ptr->du = 1; /* MINC files don't define a sample size for a vector_dimension */ nii_ptr->nifti_type = nifti_filetype; /* Load the direction_cosines and start values into the NIfTI-1 * sform structure. * */ for (i = 0; i < MAX_SPACE_DIMS; i++) { int id = ncvarid(mnc_fd, mnc_spatial_names[i]); double start; double step; double dircos[MAX_SPACE_DIMS]; int tmp; if (id < 0) { continue; } /* Set default values */ start = 0.0; step = 1.0; dircos[DIM_X] = dircos[DIM_Y] = dircos[DIM_Z] = 0.0; dircos[i] = 1.0; miattget(mnc_fd, id, MIstart, NC_DOUBLE, 1, &start, &tmp); miattget(mnc_fd, id, MIstep, NC_DOUBLE, 1, &step, &tmp); miattget(mnc_fd, id, MIdirection_cosines, NC_DOUBLE, MAX_SPACE_DIMS, dircos, &tmp); ncdiminq(mnc_fd, ncdimid(mnc_fd, mnc_spatial_names[i]), NULL, &mnc_dlen); if (step < 0) { step = -step; start = start - step * (mnc_dlen - 1); } nii_ptr->sto_xyz.m[0][i] = step * dircos[0]; nii_ptr->sto_xyz.m[1][i] = step * dircos[1]; nii_ptr->sto_xyz.m[2][i] = step * dircos[2]; nii_ptr->sto_xyz.m[0][3] += start * dircos[0]; nii_ptr->sto_xyz.m[1][3] += start * dircos[1]; nii_ptr->sto_xyz.m[2][3] += start * dircos[2]; miattgetstr(mnc_fd, id, MIspacetype, sizeof(att_str), att_str); /* Try to set the S-transform code correctly. */ if (!strcmp(att_str, MI_TALAIRACH)) { nii_ptr->sform_code = NIFTI_XFORM_TALAIRACH; } else if (!strcmp(att_str, MI_CALLOSAL)) { /* TODO: Not clear what do do here... */ nii_ptr->sform_code = NIFTI_XFORM_SCANNER_ANAT; } else { /* MI_NATIVE or unknown */ nii_ptr->sform_code = NIFTI_XFORM_SCANNER_ANAT; } } /* So the last row is right... */ nii_ptr->sto_xyz.m[3][0] = 0.0; nii_ptr->sto_xyz.m[3][1] = 0.0; nii_ptr->sto_xyz.m[3][2] = 0.0; nii_ptr->sto_xyz.m[3][3] = 1.0; nii_ptr->sto_ijk = nifti_mat44_inverse(nii_ptr->sto_xyz); nifti_datatype_sizes(nii_ptr->datatype, &nii_ptr->nbyper, &nii_ptr->swapsize); if (vflag) { nifti_image_infodump(nii_ptr); } /* Now load the actual MINC data. */ nii_ptr->data = malloc(nii_ptr->nbyper * nii_ptr->nvox); if (nii_ptr->data == NULL) { fprintf(stderr, "Out of memory.\n"); return (-1); } mnc_icv = miicv_create(); miicv_setint(mnc_icv, MI_ICV_TYPE, mnc_type); miicv_setstr(mnc_icv, MI_ICV_SIGN, (mnc_signed) ? MI_SIGNED : MI_UNSIGNED); miicv_setdbl(mnc_icv, MI_ICV_VALID_MAX, output_valid_range[1]); miicv_setdbl(mnc_icv, MI_ICV_VALID_MIN, output_valid_range[0]); miicv_setdbl(mnc_icv, MI_ICV_IMAGE_MAX, real_range[1]); miicv_setdbl(mnc_icv, MI_ICV_IMAGE_MIN, real_range[0]); miicv_setdbl(mnc_icv, MI_ICV_DO_NORM, TRUE); miicv_setdbl(mnc_icv, MI_ICV_USER_NORM, TRUE); miicv_attach(mnc_icv, mnc_fd, mnc_vid); /* Read in the entire hyperslab from the file. */ for (i = 0; i < mnc_ndims; i++) { ncdiminq(mnc_fd, mnc_dimids[i], NULL, &mnc_count[i]); mnc_start[i] = 0; } r = miicv_get(mnc_icv, mnc_start, mnc_count, nii_ptr->data); if (r < 0) { fprintf(stderr, "Read error\n"); return (-1); } /* Shut down the MINC stuff now that it has done its work. */ miicv_detach(mnc_icv); miicv_free(mnc_icv); miclose(mnc_fd); if (vflag) { /* Debugging stuff - just to check the contents of these arrays. */ for (i = 0; i < nii_ndims; i++) { printf("%d: %ld %d %d\n", i, nii_lens[i], nii_map[i], nii_dir[i]); } printf("bytes per voxel %d\n", nii_ptr->nbyper); printf("# of voxels %ld\n", nii_ptr->nvox); } /* Rearrange the data to correspond to the NIfTI dimension ordering. */ restructure_array(nii_ndims, nii_ptr->data, nii_lens, nii_ptr->nbyper, nii_map, nii_dir); if (vflag) { /* More debugging stuff - check coordinate transform. */ test_xform(nii_ptr->sto_xyz, 0, 0, 0); test_xform(nii_ptr->sto_xyz, 10, 0, 0); test_xform(nii_ptr->sto_xyz, 0, 10, 0); test_xform(nii_ptr->sto_xyz, 0, 0, 10); test_xform(nii_ptr->sto_xyz, 10, 10, 10); } if (vflag) { fprintf(stdout, "Writing NIfTI-1 file..."); } nifti_image_write(nii_ptr); if (vflag) { fprintf(stdout, "done.\n"); } return (0); }
int ex_get_set (int exoid, int set_type, int set_id, int *set_entry_list, int *set_extra_list) { int dimid, entry_list_id, extra_list_id, iresult; int set_id_ndx; long num_entry_in_set, count[1], start[1]; nclong *longs; char errmsg[MAX_ERR_LENGTH]; char* typeName; char* dimptr; char* idsptr; char* numentryptr; char* entryptr; char* extraptr; exerrval = 0; /* clear error code */ /* setup pointers based on set_type NOTE: there is another block that sets more stuff later ... */ if (set_type == EX_NODE_SET) { typeName = "node"; dimptr = DIM_NUM_NS; idsptr = VAR_NS_IDS; } else if (set_type == EX_EDGE_SET) { typeName = "edge"; dimptr = DIM_NUM_ES; idsptr = VAR_ES_IDS; } else if (set_type == EX_FACE_SET) { typeName = "face"; dimptr = DIM_NUM_FS; idsptr = VAR_FS_IDS; } else if (set_type == EX_SIDE_SET) { typeName = "side"; dimptr = DIM_NUM_SS; idsptr = VAR_SS_IDS; } else if (set_type == EX_ELEM_SET) { typeName = "elem"; dimptr = DIM_NUM_ELS; idsptr = VAR_ELS_IDS; } else { exerrval = EX_FATAL; sprintf(errmsg, "Error: invalid set type (%d)", set_type); ex_err("ex_put_set_param",errmsg,exerrval); return (EX_FATAL); } /* first check if any sets are specified */ if ((dimid = ncdimid (exoid, dimptr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no %s sets stored in file id %d", typeName, exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_WARN); } /* Lookup index of set id in VAR_*S_IDS array */ set_id_ndx = ex_id_lkup(exoid,idsptr,set_id); if (exerrval != 0) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: %s set %d is NULL in file id %d", typeName, set_id,exoid); ex_err("ex_get_set",errmsg,EX_MSG); return (EX_WARN); } else { sprintf(errmsg, "Error: failed to locate %s set id %d in VAR_*S_IDS array in file id %d", typeName, set_id,exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_FATAL); } } /* setup more pointers based on set_type */ if (set_type == EX_NODE_SET) { numentryptr = DIM_NUM_NOD_NS(set_id_ndx); entryptr = VAR_NODE_NS(set_id_ndx); extraptr = NULL; } else if (set_type == EX_EDGE_SET) { numentryptr = DIM_NUM_EDGE_ES(set_id_ndx); entryptr = VAR_EDGE_ES(set_id_ndx); extraptr = VAR_ORNT_ES(set_id_ndx); } else if (set_type == EX_FACE_SET) { numentryptr = DIM_NUM_FACE_FS(set_id_ndx); entryptr = VAR_FACE_FS(set_id_ndx); extraptr = VAR_ORNT_FS(set_id_ndx); } else if (set_type == EX_SIDE_SET) { numentryptr = DIM_NUM_SIDE_SS(set_id_ndx); entryptr = VAR_ELEM_SS(set_id_ndx); extraptr = VAR_SIDE_SS(set_id_ndx); } if (set_type == EX_ELEM_SET) { numentryptr = DIM_NUM_ELE_ELS(set_id_ndx); entryptr = VAR_ELEM_ELS(set_id_ndx); extraptr = NULL; } /* inquire id's of previously defined dimensions and variables */ if ((dimid = ncdimid (exoid, numentryptr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of entries in %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &num_entry_in_set) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of entries in %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_FATAL); } if ((entry_list_id = ncvarid (exoid, entryptr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate entry list for %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_FATAL); } /* only do extra list for edge, face and side sets */ if (extraptr) { if ((extra_list_id = ncvarid (exoid, extraptr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate extra list for %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_FATAL); } } /* read in the entry list and extra list arrays */ /* application code has allocated an array of ints but netcdf is expecting a pointer to nclongs; if ints are different sizes than nclongs, we must allocate an array of nclongs then convert them to ints with ltoi */ start[0] = 0; count[0] = num_entry_in_set; if (sizeof(int) == sizeof(nclong)) { iresult = ncvarget(exoid, entry_list_id, start, count, set_entry_list); } else { if (!(longs = malloc(num_entry_in_set * sizeof(nclong)))) { exerrval = EX_MEMFAIL; sprintf(errmsg, "Error: failed to allocate memory for entry list for %s set %d for file id %d", typeName, set_id, exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_FATAL); } iresult = ncvarget (exoid, entry_list_id, start, count, longs); } if (iresult) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get entry list for %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_FATAL); } /* only do extra list for edge, face and side sets */ if (extraptr) { if (sizeof(int) != sizeof(nclong)) { ltoi (longs, set_entry_list, num_entry_in_set); } if (sizeof(int) == sizeof(nclong)) { iresult = ncvarget(exoid, extra_list_id, start, count, set_extra_list); } else { iresult = ncvarget (exoid, extra_list_id, start, count, longs); } if (iresult == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get extra list for %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set",errmsg,exerrval); return (EX_FATAL); } } if (sizeof(int) != sizeof(nclong)) { ltoi (longs, set_extra_list, num_entry_in_set); free (longs); } return (EX_NOERR); }
int cpy_coord_def(int in_id,int out_id,int rec_dim_id,char *var_nm, int in_large, int out_large) /* int in_id: input netCDF input-file ID int out_id: input netCDF output-file ID int rec_dim_id: input input-file record dimension ID char *var_nm: input variable name int in_large: large file setting for input file int out_large: large file setting for output file int cpy_var_def(): output output-file variable ID */ { const char *routine = NULL; long spatial_dim; int nbr_dim; int temp; int dim_out_id[2]; int var_out_id = -1; /* Handle easiest situation first: in_large matches out_large */ if (in_large == out_large) { return cpy_var_def(in_id, out_id, rec_dim_id, var_nm); } /* At this point, know that in_large != out_large, so some change to the coord variable definition is needed. Also will need the spatial dimension, so get that now.*/ ex_get_dimension(in_id, DIM_NUM_DIM, "dimension", &spatial_dim, routine); if (in_large == 0 && out_large == 1) { /* output file will have coordx, coordy, coordz (if 3d). See if they are already defined in output file. Assume either all or none are defined. */ temp = ncopts; ncopts=0; { int var_out_idx = ncvarid(out_id, VAR_COORD_X); int var_out_idy = ncvarid(out_id, VAR_COORD_Y); int var_out_idz = ncvarid(out_id, VAR_COORD_Z); ncopts = temp; if (var_out_idx != -1 && var_out_idy != -1 && (spatial_dim == 2 || var_out_idz != -1)) { return var_out_idx; } } /* Get dimid of the num_nodes dimension in output file... */ dim_out_id[0]=ncdimid(out_id,DIM_NUM_NODES); /* Define the variables in the output file */ /* Define according to the EXODUS file's IO_word_size */ nbr_dim = 1; var_out_id=ncvardef(out_id,VAR_COORD_X,nc_flt_code(out_id), nbr_dim, dim_out_id); var_out_id=ncvardef(out_id,VAR_COORD_Y,nc_flt_code(out_id), nbr_dim, dim_out_id); if (spatial_dim == 3) var_out_id=ncvardef(out_id,VAR_COORD_Z,nc_flt_code(out_id), nbr_dim, dim_out_id); } if (in_large == 1 && out_large == 0) { /* input file has coordx, coordy, coordz (if 3d); output will only have "coord". See if is already defined in output file. */ temp = ncopts; ncopts=0; var_out_id = ncvarid(out_id, VAR_COORD); ncopts = temp; if (var_out_id != -1) return var_out_id; /* Get dimid of the spatial dimension and num_nodes dimensions in output file... */ dim_out_id[0]=ncdimid(out_id,DIM_NUM_DIM); dim_out_id[1]=ncdimid(out_id,DIM_NUM_NODES); /* Define the variable in the output file */ /* Define according to the EXODUS file's IO_word_size */ nbr_dim = 2; var_out_id=ncvardef(out_id,VAR_COORD,nc_flt_code(out_id), nbr_dim, dim_out_id); } return var_out_id; }
int ex_get_init_ext (int exoid, ex_init_params *info) { int dimid; long lnum_dim, lnum_nodes, lnum_elem, lnum_elem_blk, lnum_node_sets; long lnum_side_sets, lnum_edge_sets, lnum_face_sets, lnum_elem_sets; long lnum_node_maps, lnum_edge_maps, lnum_face_maps, lnum_elem_maps; long lnum_edge, lnum_face, lnum_edge_blk, lnum_face_blk; char errmsg[MAX_ERR_LENGTH]; int title_len; nc_type title_type; exerrval = 0; /* clear error code */ if (ncattinq (exoid, NC_GLOBAL, ATT_TITLE, &title_type, &title_len) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to inquire title in file id %d", exoid); ex_err("ex_get_init",errmsg,exerrval); return (EX_FATAL); } /* Check title length to avoid overrunning clients memory space; include trailing null */ if (title_len > MAX_LINE_LENGTH+1) { sprintf(errmsg, "Error: Title is too long (%d characters) in file id %d", title_len-1, exoid); exerrval = -1; ex_err("ex_get_init",errmsg,exerrval); return (EX_FATAL); } /* printf("[ex_get_init] title length: %d\n",title_len); */ if (ncattget (exoid, NC_GLOBAL, ATT_TITLE, info->title) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get title in file id %d", exoid); ex_err("ex_get_init",errmsg,exerrval); return (EX_FATAL); } /* printf("[ex_get_init] title: %s\n",info->title); */ if ((dimid = ncdimid (exoid, DIM_NUM_DIM)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of dimensions in file id %d", exoid); ex_err("ex_get_init",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &lnum_dim) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of dimensions in file id %d", exoid); ex_err("ex_get_init",errmsg,exerrval); return (EX_FATAL); } info->num_dim = lnum_dim; /* Handle case with zero-nodes */ #define EX_GET_DIM_VALUE(TNAME,DNAME,DIMVAR,LDIMVAL,SDIMVAL) \ if ((DIMVAR = ncdimid (exoid, DNAME)) == -1) { \ /* TNAME are optional and default to zero. */ \ SDIMVAL = 0; \ } else { \ \ if (ncdiminq (exoid, DIMVAR, (char *) 0, &LDIMVAL) == -1) \ { \ exerrval = ncerr; \ sprintf(errmsg, \ "Error: failed to get number of " TNAME " in file id %d", \ exoid); \ ex_err("ex_get_init",errmsg,exerrval); \ return (EX_FATAL); \ } \ SDIMVAL = LDIMVAL; \ } EX_GET_DIM_VALUE( "nodes",DIM_NUM_NODES,dimid,lnum_nodes,info->num_nodes); EX_GET_DIM_VALUE( "edges", DIM_NUM_EDGE,dimid, lnum_edge, info->num_edge); EX_GET_DIM_VALUE( "faces", DIM_NUM_FACE,dimid, lnum_face, info->num_face); EX_GET_DIM_VALUE("elements", DIM_NUM_ELEM,dimid, lnum_elem, info->num_elem); if (info->num_elem > 0) { if ((dimid = ncdimid (exoid, DIM_NUM_EL_BLK)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of element blocks in file id %d", exoid); ex_err("ex_get_init",errmsg,exerrval); return (EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &lnum_elem_blk) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of element blocks in file id %d", exoid); ex_err("ex_get_init",errmsg,exerrval); return (EX_FATAL); } info->num_elem_blk = lnum_elem_blk; } else { info->num_elem_blk = 0; } EX_GET_DIM_VALUE("node sets", DIM_NUM_NS,dimid,lnum_node_sets,info->num_node_sets); EX_GET_DIM_VALUE("edge sets", DIM_NUM_ES,dimid,lnum_edge_sets,info->num_edge_sets); EX_GET_DIM_VALUE("face sets", DIM_NUM_FS,dimid,lnum_face_sets,info->num_face_sets); EX_GET_DIM_VALUE("side sets", DIM_NUM_SS,dimid,lnum_side_sets,info->num_side_sets); EX_GET_DIM_VALUE("elem sets",DIM_NUM_ELS,dimid,lnum_elem_sets,info->num_elem_sets); EX_GET_DIM_VALUE("node maps", DIM_NUM_NM,dimid,lnum_node_maps,info->num_node_maps); EX_GET_DIM_VALUE("edge maps",DIM_NUM_EDM,dimid,lnum_edge_maps,info->num_edge_maps); EX_GET_DIM_VALUE("face maps",DIM_NUM_FAM,dimid,lnum_face_maps,info->num_face_maps); EX_GET_DIM_VALUE("elem maps", DIM_NUM_EM,dimid,lnum_elem_maps,info->num_elem_maps); /* Edge and face blocks are also optional (for backwards compatability) */ EX_GET_DIM_VALUE("edge blocks",DIM_NUM_ED_BLK,dimid,lnum_edge_blk,info->num_edge_blk); EX_GET_DIM_VALUE("face blocks",DIM_NUM_FA_BLK,dimid,lnum_face_blk,info->num_face_blk); return (EX_NOERR); }
int ex_copy (int in_exoid, int out_exoid) { int ndims; /* number of dimensions */ int nvars; /* number of variables */ int ngatts; /* number of global attributes */ int recdimid; /* id of unlimited dimension */ int dimid; /* dimension id */ int dim_out_id; /* dimension id */ int varid; /* variable id */ int var_out_id; /* variable id */ struct ncvar var; /* variable */ struct ncatt att; /* attribute */ int i, temp; long numrec; long dim_sz; char dim_nm[MAX_NC_NAME]; int in_large, out_large; extern int ncopts; exerrval = 0; /* clear error code */ /* * Get exodus_large_model setting on both input and output * databases so know how to handle coordinates. */ in_large = ex_large_model(in_exoid); out_large = ex_large_model(out_exoid); /* * get number of dimensions, number of variables, number of global * atts, and dimension id of unlimited dimension, if any */ ncinquire(in_exoid, &ndims, &nvars, &ngatts, &recdimid); ncdiminq (in_exoid, recdimid, (char *) 0, &numrec); /* put output file into define mode */ ncredef(out_exoid); /* copy global attributes */ for (i = 0; i < ngatts; i++) { ncattname(in_exoid, NC_GLOBAL, i, att.name); ncattinq(in_exoid, NC_GLOBAL, att.name, &att.type, &att.len); /* if attribute exists in output file, don't overwrite it; compute * word size, I/O word size etc. are global attributes stored when * file is created with ex_create; we don't want to overwrite those */ if (ncattinq (out_exoid, NC_GLOBAL, att.name, &att.type, &att.len) == -1){ /* The "last_written_time" attribute is a special attribute used by the Sierra IO system to determine whether a timestep has been fully written to the database in order to try to detect a database crash that happens in the middle of a database output step. Don't want to copy that attribute. */ if (strcmp(att.name,"last_written_time") != 0) { /* attribute doesn't exist in new file so OK to create it */ ncattcopy (in_exoid,NC_GLOBAL,att.name,out_exoid,NC_GLOBAL); } } } /* copy dimensions */ /* Get the dimension sizes and names */ for(dimid = 0; dimid < ndims; dimid++){ ncdiminq(in_exoid,dimid,dim_nm,&dim_sz); /* See if the dimension has already been defined */ temp = ncopts; ncopts = 0; dim_out_id = ncdimid(out_exoid,dim_nm); ncopts = temp; /* If the dimension isn't one we specifically don't want * to copy (ie, number of QA or INFO records) and it * hasn't been defined, copy it */ if ( ( strcmp(dim_nm,DIM_NUM_QA) != 0) && ( strcmp(dim_nm,DIM_NUM_INFO) != 0) && ( strcmp(dim_nm,DIM_NUM_NOD_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_EDG_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_FAC_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_ELE_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_NSET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_ESET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_FSET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_SSET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_ELSET_VAR) != 0) && ( strcmp(dim_nm,DIM_NUM_GLO_VAR) != 0) ) { if(dim_out_id == -1){ if(dimid != recdimid){ dim_out_id=ncdimdef(out_exoid,dim_nm,dim_sz); }else{ dim_out_id=ncdimdef(out_exoid,dim_nm,NC_UNLIMITED); } /* end else */ } /* end if */ } /* end if */ } /* end loop over dim */ /* copy variable definitions and variable attributes */ for (varid = 0; varid < nvars; varid++) { ncvarinq(in_exoid, varid, var.name, &var.type, &var.ndims, var.dims, &var.natts); /* we don't want to copy some variables because there is not a * simple way to add to them; * QA records, info records and all results variables (nodal * element, and global results) are examples */ if ( ( strcmp(var.name,VAR_QA_TITLE) != 0) && ( strcmp(var.name,VAR_INFO) != 0) && ( strcmp(var.name,VAR_EBLK_TAB) != 0) && ( strcmp(var.name,VAR_FBLK_TAB) != 0) && ( strcmp(var.name,VAR_ELEM_TAB) != 0) && ( strcmp(var.name,VAR_ELSET_TAB) != 0) && ( strcmp(var.name,VAR_SSET_TAB) != 0) && ( strcmp(var.name,VAR_FSET_TAB) != 0) && ( strcmp(var.name,VAR_ESET_TAB) != 0) && ( strcmp(var.name,VAR_NSET_TAB) != 0) && ( strcmp(var.name,VAR_NAME_GLO_VAR) != 0) && ( strcmp(var.name,VAR_GLO_VAR) != 0) && ( strcmp(var.name,VAR_NAME_NOD_VAR) != 0) && ( strcmp(var.name,VAR_NOD_VAR) != 0) && ( strcmp(var.name,VAR_NAME_EDG_VAR) != 0) && ( strcmp(var.name,VAR_NAME_FAC_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ELE_VAR) != 0) && ( strcmp(var.name,VAR_NAME_NSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ESET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_FSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_SSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ELSET_VAR) != 0) && ( strncmp(var.name,"vals_elset_var", 14) != 0) && ( strncmp(var.name,"vals_sset_var", 13) != 0) && ( strncmp(var.name,"vals_fset_var", 13) != 0) && ( strncmp(var.name,"vals_eset_var", 13) != 0) && ( strncmp(var.name,"vals_nset_var", 13) != 0) && ( strncmp(var.name,"vals_nod_var", 12) != 0) && ( strncmp(var.name,"vals_edge_var", 13) != 0) && ( strncmp(var.name,"vals_face_var", 13) != 0) && ( strncmp(var.name,"vals_elem_var", 13) != 0) ) { if (strncmp(var.name,VAR_COORD,5) == 0) { var_out_id = cpy_coord_def (in_exoid, out_exoid, recdimid, var.name, in_large, out_large); } else { var_out_id = cpy_var_def (in_exoid, out_exoid, recdimid, var.name); } /* copy the variable's attributes */ (void) cpy_att (in_exoid, out_exoid, varid, var_out_id); } } /* take the output file out of define mode */ ncendef (out_exoid); /* output variable data */ for (varid = 0; varid < nvars; varid++) { ncvarinq(in_exoid, varid, var.name, &var.type, &var.ndims, var.dims, &var.natts); /* we don't want to copy some variable values; * QA records and info records shouldn't be copied because there * isn't an easy way to add to them; * the time value array ("time_whole") and any results variables * (nodal, elemental, or global) shouldn't be copied */ if ( ( strcmp(var.name,VAR_QA_TITLE) != 0) && ( strcmp(var.name,VAR_INFO) != 0) && ( strcmp(var.name,VAR_EBLK_TAB) != 0) && ( strcmp(var.name,VAR_FBLK_TAB) != 0) && ( strcmp(var.name,VAR_ELEM_TAB) != 0) && ( strcmp(var.name,VAR_ELSET_TAB) != 0) && ( strcmp(var.name,VAR_SSET_TAB) != 0) && ( strcmp(var.name,VAR_FSET_TAB) != 0) && ( strcmp(var.name,VAR_ESET_TAB) != 0) && ( strcmp(var.name,VAR_NSET_TAB) != 0) && ( strcmp(var.name,VAR_NAME_GLO_VAR) != 0) && ( strcmp(var.name,VAR_GLO_VAR) != 0) && ( strcmp(var.name,VAR_NAME_NOD_VAR) != 0) && ( strcmp(var.name,VAR_NOD_VAR) != 0) && ( strcmp(var.name,VAR_NAME_EDG_VAR) != 0) && ( strcmp(var.name,VAR_NAME_FAC_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ELE_VAR) != 0) && ( strcmp(var.name,VAR_NAME_NSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ESET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_FSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_SSET_VAR) != 0) && ( strcmp(var.name,VAR_NAME_ELSET_VAR) != 0) && ( strncmp(var.name,"vals_elset_var", 14) != 0)&& ( strncmp(var.name,"vals_sset_var", 13) != 0)&& ( strncmp(var.name,"vals_fset_var", 13) != 0)&& ( strncmp(var.name,"vals_eset_var", 13) != 0)&& ( strncmp(var.name,"vals_nset_var", 13) != 0)&& ( strncmp(var.name,"vals_nod_var", 12) != 0) && ( strncmp(var.name,"vals_edge_var",13) != 0) && ( strncmp(var.name,"vals_face_var",13) != 0) && ( strncmp(var.name,"vals_elem_var",13) != 0) && ( strcmp(var.name,VAR_WHOLE_TIME) != 0) ) { if (strncmp(var.name,VAR_COORD,5) == 0) { (void) cpy_coord_val (in_exoid, out_exoid, var.name, in_large, out_large); } else { (void) cpy_var_val (in_exoid, out_exoid, var.name); } } } /* ensure internal data structures are updated */ /* if number of blocks > 0 */ update_internal_structs( out_exoid, EX_INQ_EDGE_BLK, &ed_ctr_list ); update_internal_structs( out_exoid, EX_INQ_FACE_BLK, &fa_ctr_list ); update_internal_structs( out_exoid, EX_INQ_ELEM_BLK, &eb_ctr_list ); /* if number of sets > 0 */ update_internal_structs( out_exoid, EX_INQ_NODE_SETS, &ns_ctr_list ); update_internal_structs( out_exoid, EX_INQ_EDGE_SETS, &es_ctr_list ); update_internal_structs( out_exoid, EX_INQ_FACE_SETS, &fs_ctr_list ); update_internal_structs( out_exoid, EX_INQ_SIDE_SETS, &ss_ctr_list ); update_internal_structs( out_exoid, EX_INQ_ELEM_SETS, &els_ctr_list ); /* if number of maps > 0 */ update_internal_structs( out_exoid, EX_INQ_NODE_MAP, &nm_ctr_list ); update_internal_structs( out_exoid, EX_INQ_EDGE_MAP, &edm_ctr_list ); update_internal_structs( out_exoid, EX_INQ_FACE_MAP, &fam_ctr_list ); update_internal_structs( out_exoid, EX_INQ_ELEM_MAP, &em_ctr_list ); return(EX_NOERR); }
int main(int argc, char *argv[]) { int i, j, d, h; int appid, wid, cnid, vcid, stid, txid, amid, mpid, tmid, stdmid; long stid_len; int vfield, vfield2, sfield, sfield2; int rlist; ng_size_t len_dims[2]; long strt[1], cnt[1]; long latlen, lonlen; long timelen; int *timestep; int ncid[6], uid, vid, u5id, v5id, pid, tid; int latid, lonid; float *lon, *lat; float *X, *Y; char filename[256]; char *rftime; const char *dir = _NGGetNCARGEnv("data"); char hour[3], day[3], mainstring[17]; extern void get_2d_array(float *, long, long, int, int, long); char const *wks_type = "x11"; /* * Initialize the high level utility library */ NhlInitialize(); /* * Create an application object. */ rlist = NhlRLCreate(NhlSETRL); NhlRLClear(rlist); NhlRLSetString(rlist,NhlNappUsrDir,"./"); NhlRLSetString(rlist,NhlNappDefaultParent,"True"); NhlCreate(&appid,"st04",NhlappClass,NhlDEFAULT_APP,rlist); if (!strcmp(wks_type,"ncgm") || !strcmp(wks_type,"NCGM")) { /* * Create a meta file workstation. */ NhlRLClear(rlist); NhlRLSetString(rlist,NhlNwkMetaName,"./st04c.ncgm"); NhlRLSetString(rlist,NhlNwkColorMap,"temp1"); NhlCreate(&wid,"st04Work", NhlncgmWorkstationClass,NhlDEFAULT_APP,rlist); } else if (!strcmp(wks_type,"x11") || !strcmp(wks_type,"X11")) { /* * Create an X workstation. */ NhlRLClear(rlist); NhlRLSetInteger(rlist,NhlNwkPause,True); NhlRLSetString(rlist,NhlNwkColorMap,"temp1"); NhlCreate(&wid,"st04Work",NhlcairoWindowWorkstationClass,appid,rlist); } else if (!strcmp(wks_type,"oldps") || !strcmp(wks_type,"OLDPS")) { /* * Create an older-style PostScript workstation. */ NhlRLClear(rlist); NhlRLSetString(rlist,NhlNwkPSFileName,"st04c.ps"); NhlRLSetString(rlist,NhlNwkColorMap,"temp1"); NhlCreate(&wid,"st04Work",NhlpsWorkstationClass,appid,rlist); } else if (!strcmp(wks_type,"oldpdf") || !strcmp(wks_type,"OLDPDF")) { /* * Create an older-style PDF workstation. */ NhlRLClear(rlist); NhlRLSetString(rlist,NhlNwkPDFFileName,"st04c.pdf"); NhlRLSetString(rlist,NhlNwkColorMap,"temp1"); NhlCreate(&wid,"st04Work",NhlpdfWorkstationClass,appid,rlist); } else if (!strcmp(wks_type,"pdf") || !strcmp(wks_type,"PDF") || !strcmp(wks_type,"ps") || !strcmp(wks_type,"PS")) { /* * Create a cairo PS/PDF workstation. */ NhlRLClear(rlist); NhlRLSetString(rlist,NhlNwkFileName,"st04c"); NhlRLSetString(rlist,NhlNwkFormat,(char*)wks_type); NhlRLSetString(rlist,NhlNwkColorMap,"temp1"); NhlCreate(&wid,"st04Work",NhlcairoDocumentWorkstationClass,appid,rlist); } else if (!strcmp(wks_type,"png") || !strcmp(wks_type,"PNG")) { /* * Create a cairo PNG workstation. */ NhlRLClear(rlist); NhlRLSetString(rlist,NhlNwkFileName,"st04c"); NhlRLSetString(rlist,NhlNwkFormat,(char*)wks_type); NhlRLSetString(rlist,NhlNwkColorMap,"temp1"); NhlCreate(&wid,"st04Work",NhlcairoImageWorkstationClass,appid,rlist); } /* * Open the netCDF files. */ for( i = 0; i <= 5; i++ ) { sprintf( filename, "%s/cdf/%s", dir, cdffiles[i] ); ncid[i] = ncopen(filename,NC_NOWRITE); } /* * Get the lat/lon dimensions (they happen to be the * same for all files in this case) */ latid = ncdimid(ncid[0],"lat"); lonid = ncdimid(ncid[0],"lon"); ncdiminq(ncid[0],latid,(char *)0,&latlen); ncdiminq(ncid[0],lonid,(char *)0,&lonlen); len_dims[0] = latlen; len_dims[1] = lonlen; /* * Get the variable ids */ uid = ncvarid(ncid[0],"u"); vid = ncvarid(ncid[1],"v"); pid = ncvarid(ncid[2],"p"); tid = ncvarid(ncid[3],"t"); u5id = ncvarid(ncid[4],"u"); v5id = ncvarid(ncid[5],"v"); latid = ncvarid(ncid[0],"lat"); lonid = ncvarid(ncid[0],"lon"); /* * allocate space for arrays */ X = (float *)malloc(sizeof(float)*latlen*lonlen); Y = (float *)malloc(sizeof(float)*latlen*lonlen); lat = (float *)malloc(sizeof(float)*latlen); lon = (float *)malloc(sizeof(float)*lonlen); /* * Get lat/lon values (they are the same for all files) */ strt[0] = 0; cnt[0] = latlen; ncvarget(ncid[0],latid,(long const *)strt,(long const *)cnt,lat); cnt[0] = lonlen; ncvarget(ncid[0],lonid,(long const *)strt,(long const *)cnt,lon); /* * Get U and V data values */ get_2d_array(X,latlen,lonlen,ncid[0],uid,0); get_2d_array(Y,latlen,lonlen,ncid[1],vid,0); /* * Create a VectorField of the surface wind data */ NhlRLClear(rlist); NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims); NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims); NhlRLSetFloat(rlist,NhlNvfXCStartV,lon[0]); NhlRLSetFloat(rlist,NhlNvfYCStartV,lat[0]); NhlRLSetFloat(rlist,NhlNvfXCEndV,lon[lonlen-1]); NhlRLSetFloat(rlist,NhlNvfYCEndV,lat[latlen-1]); NhlRLSetFloat(rlist,NhlNvfMissingUValueV,-9999.0); NhlCreate(&vfield,"VectorField",NhlvectorFieldClass,appid,rlist); /* * Create a VectorField of 500 millibar wind data * * Get U and V values */ get_2d_array(X,latlen,lonlen,ncid[4],u5id,0); get_2d_array(Y,latlen,lonlen,ncid[5],v5id,0); NhlRLClear(rlist); NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims); NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims); NhlRLSetFloat(rlist,NhlNvfXCStartV,lon[0]); NhlRLSetFloat(rlist,NhlNvfYCStartV,lat[0]); NhlRLSetFloat(rlist,NhlNvfXCEndV,lon[lonlen-1]); NhlRLSetFloat(rlist,NhlNvfYCEndV,lat[latlen-1]); NhlRLSetFloat(rlist,NhlNvfMissingUValueV,-9999.0); NhlCreate(&vfield2,"VectorField",NhlvectorFieldClass,appid,rlist); /* * Create a ScalarField of surface pressure * * Get P data values */ get_2d_array(X,latlen,lonlen,ncid[2],pid,0); for( i = 0; i < latlen*lonlen; i++ ) { if( X[i] != -9999.0 ) { X[i] /= 100.; } } NhlRLClear(rlist); NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims); NhlRLSetFloat(rlist,NhlNsfXCStartV,lon[0]); NhlRLSetFloat(rlist,NhlNsfYCStartV,lat[0]); NhlRLSetFloat(rlist,NhlNsfXCEndV,lon[lonlen-1]); NhlRLSetFloat(rlist,NhlNsfYCEndV,lat[latlen-1]); NhlRLSetFloat(rlist,NhlNsfMissingValueV,-9999.0); NhlCreate(&sfield,"ScalarField",NhlscalarFieldClass,appid,rlist); /* * Create a ScalarField of surface temperature * (convert from Kelvin to Farenheit) * * Get T data values */ get_2d_array(X,latlen,lonlen,ncid[3],tid,0); /* * Convert to Fahrenheit */ for( i = 0; i < latlen*lonlen; i++ ) { if( X[i] != -9999.0) { X[i] = (X[i] - 273.15) * 9.0/5.0 + 32.0; } } NhlRLClear(rlist); NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims); NhlRLSetFloat(rlist,NhlNsfXCStartV,lon[0]); NhlRLSetFloat(rlist,NhlNsfYCStartV,lat[0]); NhlRLSetFloat(rlist,NhlNsfXCEndV,lon[lonlen-1]); NhlRLSetFloat(rlist,NhlNsfYCEndV,lat[latlen-1]); NhlRLSetFloat(rlist,NhlNsfMissingValueV,-9999.0); NhlCreate(&sfield2,"ScalarField2",NhlscalarFieldClass,appid,rlist); /* * Create a ContourPlot with surface temperature data */ NhlRLClear(rlist); NhlRLSetString(rlist,NhlNcnFillOn,"true"); NhlRLSetString(rlist,NhlNcnLinesOn,"false"); NhlRLSetString(rlist,NhlNcnFillDrawOrder,"predraw"); NhlRLSetInteger(rlist,NhlNcnScalarFieldData,sfield2); NhlCreate(&cnid,"contourplot",NhlcontourPlotClass,wid,rlist); /* * Create a VectorPlot with the surface wind and pressure data */ NhlRLClear(rlist); NhlRLSetString(rlist,NhlNvcUseScalarArray,"true"); NhlRLSetInteger(rlist,NhlNvcVectorFieldData,vfield); NhlRLSetInteger(rlist,NhlNvcScalarFieldData,sfield); NhlCreate(&vcid,"vectorplot",NhlvectorPlotClass,wid,rlist); /* * Create a StreamlinePlot with 500 mb wind data */ NhlRLClear(rlist); NhlRLSetString(rlist,NhlNpmTitleDisplayMode,"always"); NhlRLSetString(rlist,NhlNtiMainFuncCode,"~"); NhlRLSetInteger(rlist,NhlNstVectorFieldData,vfield2); NhlCreate(&stid,"streamlineplot",NhlstreamlinePlotClass,wid,rlist); /* * Create an annotation used to explain the streamline data */ NhlCreate(&txid,"streamlineplotanno",NhltextItemClass,wid,0); amid = NhlAddAnnotation(stid,txid); /* * Create a map object */ NhlRLClear(rlist); /* NhlRLSetString(rlist,NhlNvpUseSegments,"true"); */ NhlCreate(&mpid,"mapplot",NhlmapPlotClass,wid,rlist); /* * Overlay everything on the MapPlot. The last object overlaid will * appear on top */ NhlAddOverlay(mpid,cnid,-1); NhlAddOverlay(mpid,vcid,-1); NhlAddOverlay(mpid,stid,-1); /* * Variables for manipulating the title string */ tmid = ncdimid(ncid[1],"timestep"); ncdiminq(ncid[1],tmid,(char *)0,&timelen); tmid = ncvarid(ncid[1],"timestep"); timestep = (int *)malloc(sizeof(int)*timelen); strt[0] = 0; cnt[0] = timelen; ncvarget(ncid[1],tmid,(long const *)strt,(long const *)cnt,timestep); sprintf( hour, "00"); sprintf( day, "05"); stdmid = ncdimid(ncid[1],"timelen"); ncdiminq(ncid[1], stdmid, (char *)0, &stid_len ); tmid = ncvarid(ncid[1],"reftime"); rftime = (char *)malloc((stid_len+1)*sizeof(char)); strt[0] = 0; cnt[0] = stid_len; ncvarget(ncid[1],tmid,(long const *)strt,(long const *)cnt,rftime); for( i = 0; i <= TIMESTEPS-1; i++ ) { if (i != 17 && i != 36 && i != 37) { /* * Figure out the hour and day from the timestep, convert to strings * and build the title string */ d = timestep[i] / 24 + 5; h = timestep[i] % 24; if (h > 9) { sprintf( hour, "%d", h ); } else { sprintf( hour, "0%d", h ); } if (d > 9) { sprintf(day, "%d", d ); } else { sprintf(day, "0%d", d ); } /* * Set the new title string */ strcpy(mainstring, rftime); sprintf(&mainstring[8], "%2s %2s:00", day, hour); printf("%s\n",mainstring); NhlRLClear(rlist); NhlRLSetString(rlist,NhlNtiMainString,mainstring); NhlSetValues(stid,rlist); /* * Modify the data objects with data for the current time step * * Get U and V values */ get_2d_array(X,latlen,lonlen,ncid[0],uid,i); get_2d_array(Y,latlen,lonlen,ncid[1],vid,i); NhlRLClear(rlist); NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims); NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims); NhlSetValues(vfield,rlist); /* * Get U and V values */ get_2d_array(X,latlen,lonlen,ncid[4],u5id,i); get_2d_array(Y,latlen,lonlen,ncid[5],v5id,i); NhlRLClear(rlist); NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims); NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims); NhlSetValues(vfield2,rlist); /* * Get P values */ get_2d_array(X,latlen,lonlen,ncid[2],pid,i); for( j = 0; j < latlen*lonlen; j++ ) { if( X[j] != -9999.0 ) { X[j] /= 100.; } } NhlRLClear(rlist); NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims); NhlSetValues(sfield,rlist); /* * Get T values */ get_2d_array(X,latlen,lonlen,ncid[3],tid,i); /* * Convert to Fahrenheit */ for( j = 0; j < latlen*lonlen; j++ ) { if( X[j] != -9999.0) { X[j] = (X[j] - 273.15) * 9.0/5.0 + 32.0; } } NhlRLClear(rlist); NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims); NhlSetValues(sfield2,rlist); /* * Draw the plot */ NhlDraw(mpid); NhlFrame(wid); } } /* * Destroy the workstation object and exit. */ NhlDestroy(wid); NhlClose(); exit(0); }
int ex_put_var_name (int exoid, const char *var_type, int var_num, const char *var_name) { int varid; long start[2], count[2]; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* inquire previously defined dimensions */ if ((ncdimid (exoid, DIM_STR)) < 0) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get string length in file id %d",exoid); ex_err("ex_put_var_name",errmsg,exerrval); return (EX_FATAL); } /* inquire previously defined variables */ if (*var_type == 'g' || *var_type == 'G') { if ((varid = ncvarid (exoid, VAR_NAME_GLO_VAR)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no global variables names stored in file id %d", exoid); ex_err("ex_put_var_name",errmsg,exerrval); return (EX_WARN); } } else if (*var_type == 'n' || *var_type == 'N') { if ((varid = ncvarid (exoid, VAR_NAME_NOD_VAR)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no nodal variable names stored in file id %d", exoid); ex_err("ex_put_var_name",errmsg,exerrval); return (EX_WARN); } } else if (*var_type == 'e' || *var_type == 'E') { if ((varid = ncvarid (exoid, VAR_NAME_ELE_VAR)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no element variable names stored in file id %d", exoid); ex_err("ex_put_var_name",errmsg,exerrval); return (EX_WARN); } } else if (*var_type == 'm' || *var_type == 'M') { if ((varid = ncvarid (exoid, VAR_NAME_NSET_VAR)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no nodeset variable names stored in file id %d", exoid); ex_err("ex_put_var_name",errmsg,exerrval); return (EX_WARN); } } else if (*var_type == 's' || *var_type == 'S') { if ((varid = ncvarid (exoid, VAR_NAME_SSET_VAR)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no sideset variable names stored in file id %d", exoid); ex_err("ex_put_var_name",errmsg,exerrval); return (EX_WARN); } } else /* invalid variable type */ { exerrval = EX_BADPARAM; sprintf(errmsg, "Error: Invalid variable type %c specified in file id %d", *var_type, exoid); ex_err("ex_put_var_name",errmsg,exerrval); return (EX_WARN); } /* write EXODUS variable name */ start[0] = var_num-1; start[1] = 0; count[0] = 1; count[1] = strlen(var_name) + 1; if (ncvarput (exoid, varid, start, count, (void*)var_name) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store %c variable name %d in file id %d", *var_type, var_num, exoid); ex_err("ex_put_var_name",errmsg,exerrval); return (EX_FATAL); } return(EX_NOERR); }
int cpy_var_def(int in_id,int out_id,int rec_dim_id,char *var_nm) /* int in_id: input netCDF input-file ID int out_id: input netCDF output-file ID int rec_dim_id: input input-file record dimension ID char *var_nm: input variable name int cpy_var_def(): output output-file variable ID */ { /* Routine to copy the variable metadata from an input netCDF file * to an output netCDF file. */ int *dim_in_id; int *dim_out_id; int idx; int nbr_dim; int var_in_id; int var_out_id; int temp; extern int ncopts; nc_type var_type; /* See if the requested variable is already in the output file. */ temp = ncopts; ncopts=0; var_out_id=ncvarid(out_id,var_nm); ncopts = temp; if(var_out_id != -1) return var_out_id; /* See if the requested variable is in the input file. */ var_in_id=ncvarid(in_id,var_nm); /* Get the type of the variable and the number of dimensions. */ ncvarinq(in_id,var_in_id,(char *)NULL,&var_type,&nbr_dim, (int *)NULL,(int *)NULL); /* Recall: 1. The dimensions must be defined before the variable. 2. The variable must be defined before the attributes. */ /* Allocate space to hold the dimension IDs */ dim_in_id=malloc(nbr_dim*sizeof(int)); dim_out_id=malloc(nbr_dim*sizeof(int)); /* Get the dimension IDs */ ncvarinq(in_id,var_in_id,(char *)NULL,(nc_type *)NULL, (int *)NULL,dim_in_id,(int *)NULL); /* Get the dimension sizes and names */ for(idx=0;idx<nbr_dim;idx++){ char dim_nm[MAX_NC_NAME]; long dim_sz; ncdiminq(in_id,dim_in_id[idx],dim_nm,&dim_sz); /* See if the dimension has already been defined */ temp = ncopts; ncopts = 0; dim_out_id[idx]=ncdimid(out_id,dim_nm); ncopts = temp; /* If the dimension hasn't been defined, copy it */ if(dim_out_id[idx] == -1){ if(dim_in_id[idx] != rec_dim_id){ dim_out_id[idx]=ncdimdef(out_id,dim_nm,dim_sz); }else{ dim_out_id[idx]=ncdimdef(out_id,dim_nm,NC_UNLIMITED); } /* end else */ } /* end if */ } /* end loop over dim */ /* Define the variable in the output file */ /* If variable is float or double, define it according to the EXODUS file's IO_word_size */ if ((var_type == NC_FLOAT) || (var_type == NC_DOUBLE)) { var_out_id=ncvardef(out_id,var_nm,nc_flt_code(out_id), nbr_dim,dim_out_id); } else { var_out_id=ncvardef(out_id,var_nm,var_type,nbr_dim,dim_out_id); } /* Free the space holding the dimension IDs */ (void)free(dim_in_id); (void)free(dim_out_id); return var_out_id; } /* end cpy_var_def() */
int ex_get_elem_block (int exoid, int elem_blk_id, char *elem_type, int *num_elem_this_blk, int *num_nodes_per_elem, int *num_attr) { int dimid, connid, len, elem_blk_id_ndx; long lnum_elem_this_blk, lnum_nodes_per_elem, lnum_attr; char *ptr; char errmsg[MAX_ERR_LENGTH]; nc_type dummy; exerrval = 0; /* First, locate index of element block id in VAR_ID_EL_BLK array */ elem_blk_id_ndx = ex_id_lkup(exoid,VAR_ID_EL_BLK,elem_blk_id); if (exerrval != 0) { if (exerrval == EX_NULLENTITY) /* NULL element block? */ { strcpy(elem_type, "NULL"); /* NULL element type name */ *num_elem_this_blk = 0; /* no elements */ *num_nodes_per_elem = 0; /* no nodes */ *num_attr = 0; /* no attributes */ return (EX_NOERR); } else { sprintf(errmsg, "Error: failed to locate element block id %d in %s array in file id %d", elem_blk_id,VAR_ID_EL_BLK,exoid); ex_err("ex_get_elem_block",errmsg,exerrval); return (EX_FATAL); } } /* inquire values of some dimensions */ if ((dimid = ncdimid (exoid, DIM_NUM_EL_IN_BLK(elem_blk_id_ndx))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of elements in block %d in file id %d", elem_blk_id,exoid); ex_err("ex_get_elem_block",errmsg, exerrval); return(EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &lnum_elem_this_blk) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of elements in block %d in file id %d", elem_blk_id, exoid); ex_err("ex_get_elem_block",errmsg, exerrval); return(EX_FATAL); } *num_elem_this_blk = lnum_elem_this_blk; if ((dimid = ncdimid (exoid, DIM_NUM_NOD_PER_EL(elem_blk_id_ndx))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate number of nodes/element in block %d in file id %d", elem_blk_id,exoid); ex_err("ex_get_elem_block",errmsg, exerrval); return(EX_FATAL); } if (ncdiminq (exoid, dimid, (char *) 0, &lnum_nodes_per_elem) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of nodes/element in block %d in file id %d", elem_blk_id, exoid); ex_err("ex_get_elem_block",errmsg, exerrval); return(EX_FATAL); } *num_nodes_per_elem = lnum_nodes_per_elem; if ((dimid = ncdimid (exoid, DIM_NUM_ATT_IN_BLK(elem_blk_id_ndx))) == -1) *num_attr = 0; /* dimension is undefined */ else { if (ncdiminq (exoid, dimid, (char *) 0, &lnum_attr) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of attributes in block %d in file id %d", elem_blk_id, exoid); ex_err("ex_get_elem_block",errmsg, exerrval); return(EX_FATAL); } *num_attr = lnum_attr; } /* look up connectivity array for this element block id */ if ((connid = ncvarid (exoid, VAR_CONN(elem_blk_id_ndx))) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to locate connectivity array for element block %d in file id %d", elem_blk_id,exoid); ex_err("ex_get_elem_block",errmsg, exerrval); return(EX_FATAL); } if (ncattinq (exoid, connid, ATT_NAME_ELB, &dummy, &len) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get element block %d type in file id %d", elem_blk_id,exoid); ex_err("ex_get_elem_block",errmsg, exerrval); return(EX_FATAL); } if (len > (MAX_STR_LENGTH+1)) { len = MAX_STR_LENGTH; sprintf (errmsg, "Warning: element block %d type will be truncated to %d chars", elem_blk_id,len); ex_err("ex_get_elem_block",errmsg,EX_MSG); } /* get the element type name */ if (ncattget (exoid, connid, ATT_NAME_ELB, elem_type) == -1) { exerrval = ncerr; sprintf(errmsg,"Error: failed to get element block %d type in file id %d", elem_blk_id, exoid); ex_err("ex_get_elem_block",errmsg, exerrval); return(EX_FATAL); } /* get rid of trailing blanks */ ptr = elem_type; /* fprintf(stderr,"[exgelb] %s, len: %d\n",ptr,len); */ while (ptr < elem_type + len && *ptr != ' ') { ptr++; } *(ptr) = '\0'; return (EX_NOERR); }
int ex_put_elem_num_map (int exoid, const int *elem_map) { int numelemdim, dims[1], mapid, iresult; long num_elem, start[1], count[1]; nclong *lptr; char errmsg[MAX_ERR_LENGTH]; exerrval = 0; /* clear error code */ /* inquire id's of previously defined dimensions */ /* determine number of elements. Return if zero... */ if ((numelemdim = ncdimid (exoid, DIM_NUM_ELEM)) == -1) { return (EX_NOERR); } if (ncdiminq (exoid, numelemdim, (char *) 0, &num_elem) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of elements in file id %d", exoid); ex_err("ex_put_elem_num_map",errmsg,exerrval); return (EX_FATAL); } /* put netcdf file into define mode */ if ((mapid = ncvarid (exoid, VAR_ELEM_NUM_MAP)) == -1) { if (ncredef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to put file id %d into define mode", exoid); ex_err("ex_put_elem_num_map",errmsg,exerrval); return (EX_FATAL); } /* create a variable array in which to store the element numbering map */ dims[0] = numelemdim; if ((mapid = ncvardef (exoid, VAR_ELEM_NUM_MAP, NC_LONG, 1, dims)) == -1) { if (ncerr == NC_ENAMEINUSE) { exerrval = ncerr; sprintf(errmsg, "Error: element numbering map already exists in file id %d", exoid); ex_err("ex_put_elem_num_map",errmsg,exerrval); } else { exerrval = ncerr; sprintf(errmsg, "Error: failed to create element numbering map in file id %d", exoid); ex_err("ex_put_elem_num_map",errmsg,exerrval); } goto error_ret; /* exit define mode and return */ } /* leave define mode */ if (ncendef (exoid) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to complete definition in file id %d", exoid); ex_err("ex_put_elem_num_map",errmsg,exerrval); return (EX_FATAL); } } /* write out the element numbering map */ /* this contortion is necessary because netCDF is expecting nclongs; fortunately it's necessary only when ints and nclongs aren't the same size */ start[0] = 0; count[0] = num_elem; if (sizeof(int) == sizeof(nclong)) { iresult = ncvarput (exoid, mapid, start, count, elem_map); } else { lptr = itol (elem_map, (int)num_elem); iresult = ncvarput (exoid, mapid, start, count, lptr); free(lptr); } if (iresult == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to store element numbering map in file id %d", exoid); ex_err("ex_put_elem_num_map",errmsg,exerrval); return (EX_FATAL); } return (EX_NOERR); /* Fatal error: exit definition mode and return */ error_ret: if (ncendef (exoid) == -1) /* exit define mode */ { sprintf(errmsg, "Error: failed to complete definition for file id %d", exoid); ex_err("ex_put_elem_num_map",errmsg,exerrval); } return (EX_FATAL); }
int ex_get_set_dist_fact (int exoid, int set_type, int set_id, void *set_dist_fact) { int dimid, dist_id, set_id_ndx; long num_df_in_set, count[1], start[1]; char errmsg[MAX_ERR_LENGTH]; char* typeName; char* dimptr; char* idsptr; char* numdfptr; char* factptr; exerrval = 0; /* clear error code */ /* setup pointers based on set_type NOTE: there is another block that sets more stuff later ... */ if (set_type == EX_NODE_SET) { typeName = "node"; dimptr = DIM_NUM_NS; idsptr = VAR_NS_IDS; } else if (set_type == EX_EDGE_SET) { typeName = "edge"; dimptr = DIM_NUM_ES; idsptr = VAR_ES_IDS; } else if (set_type == EX_FACE_SET) { typeName = "face"; dimptr = DIM_NUM_FS; idsptr = VAR_FS_IDS; } else if (set_type == EX_SIDE_SET) { typeName = "side"; dimptr = DIM_NUM_SS; idsptr = VAR_SS_IDS; } else if (set_type == EX_ELEM_SET) { typeName = "elem"; dimptr = DIM_NUM_ELS; idsptr = VAR_ELS_IDS; } else { exerrval = EX_FATAL; sprintf(errmsg, "Error: invalid set type (%d)", set_type); ex_err("ex_put_set_param",errmsg,exerrval); return (EX_FATAL); } /* first check if any sets are specified */ if ((dimid = ncdimid (exoid, dimptr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: no %s sets stored in file id %d", typeName, exoid); ex_err("ex_get_set_dist_fact",errmsg,exerrval); return (EX_WARN); } /* Lookup index of set id in VAR_*S_IDS array */ set_id_ndx = ex_id_lkup(exoid,idsptr,set_id); if (exerrval != 0) { if (exerrval == EX_NULLENTITY) { sprintf(errmsg, "Warning: %s set %d is NULL in file id %d", typeName, set_id,exoid); ex_err("ex_get_set_dist_fact",errmsg,EX_MSG); return (EX_WARN); } else { sprintf(errmsg, "Error: failed to locate %s set id %d in VAR_*S_IDS array in file id %d", typeName, set_id,exoid); ex_err("ex_get_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } } /* setup more pointers based on set_type */ if (set_type == EX_NODE_SET) { /* note we are using DIM_NUM_NODE_NS instead of DIM_NUM_DF_NS */ numdfptr = DIM_NUM_NOD_NS(set_id_ndx); factptr = VAR_FACT_NS(set_id_ndx); } else if (set_type == EX_EDGE_SET) { numdfptr = DIM_NUM_DF_ES(set_id_ndx); factptr = VAR_FACT_ES(set_id_ndx); } else if (set_type == EX_FACE_SET) { numdfptr = DIM_NUM_DF_FS(set_id_ndx); factptr = VAR_FACT_FS(set_id_ndx); } else if (set_type == EX_SIDE_SET) { numdfptr = DIM_NUM_DF_SS(set_id_ndx); factptr = VAR_FACT_SS(set_id_ndx); } if (set_type == EX_ELEM_SET) { numdfptr = DIM_NUM_DF_ELS(set_id_ndx); factptr = VAR_FACT_ELS(set_id_ndx); } /* inquire id's of previously defined dimensions and variables */ if ((dimid = ncdimid (exoid, numdfptr)) == -1) { exerrval = ncerr; sprintf(errmsg, "Warning: dist factors not stored for %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set_dist_fact",errmsg,exerrval); return (EX_WARN); /* complain - but not too loud */ } if (ncdiminq (exoid, dimid, (char *) 0, &num_df_in_set) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get number of dist factors in %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } if ((dist_id = ncvarid (exoid, factptr)) == -1) { exerrval = ncerr; /* not an error for node sets because this is how we check that df's exist */ if (set_type == EX_NODE_SET) { sprintf(errmsg, "Warning: dist factors not stored for %s set %d in file id %d", typeName, set_id, exoid); ex_err("ex_get_set_dist_fact",errmsg,exerrval); return (EX_WARN); /* complain - but not too loud */ } /* is an error for other sets */ else { sprintf(errmsg, "Error: failed to locate dist factors list for %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } } /* read in the distribution factors array */ start[0] = 0; count[0] = num_df_in_set; if (ncvarget (exoid, dist_id, start, count, ex_conv_array(exoid,RTN_ADDRESS,set_dist_fact, (int)num_df_in_set)) == -1) { exerrval = ncerr; sprintf(errmsg, "Error: failed to get dist factors list for %s set %d in file id %d", typeName, set_id,exoid); ex_err("ex_get_set_dist_fact",errmsg,exerrval); return (EX_FATAL); } ex_conv_array( exoid, READ_CONVERT, set_dist_fact, num_df_in_set ); return (EX_NOERR); }