| 1 |
/* |
| 2 |
* Copyright (c) 2005 Sandia Corporation. Under the terms of Contract |
| 3 |
* DE-AC04-94AL85000 with Sandia Corporation, the U.S. Governement |
| 4 |
* retains certain rights in this software. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions are |
| 8 |
* met: |
| 9 |
* |
| 10 |
* * Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* * Redistributions in binary form must reproduce the above |
| 14 |
* copyright notice, this list of conditions and the following |
| 15 |
* disclaimer in the documentation and/or other materials provided |
| 16 |
* with the distribution. |
| 17 |
* |
| 18 |
* * Neither the name of Sandia Corporation nor the names of its |
| 19 |
* contributors may be used to endorse or promote products derived |
| 20 |
* from this software without specific prior written permission. |
| 21 |
* |
| 22 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
* |
| 34 |
*/ |
| 35 |
|
| 36 |
#include "exodusII.h" |
| 37 |
#include "exodusII_int.h" |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
/*! |
| 41 |
|
| 42 |
The function ex_put_prop_names() writes object property names and |
| 43 |
allocates space for object property arrays used to assign integer |
| 44 |
properties to element blocks, node sets, or side sets. The property |
| 45 |
arrays are initialized to zero (0). Although this function is |
| 46 |
optional, since ex_put_prop() will allocate space within the data file |
| 47 |
if it hasn't been previously allocated, it is more efficient to use |
| 48 |
ex_put_prop_names() if there is more than one property to store. \see |
| 49 |
Efficiency for a discussion of efficiency issues. |
| 50 |
|
| 51 |
\return In case of an error, ex_put_prop_names() returns a negative number; a |
| 52 |
warning will return a positive number. Possible causes of errors |
| 53 |
include: |
| 54 |
- data file not properly opened with call to ex_create() or ex_open() |
| 55 |
- data file opened for read only. |
| 56 |
- data file not initialized properly with call to ex_put_init(). |
| 57 |
- invalid object type specified. |
| 58 |
- no object of the specified type is stored in the file. |
| 59 |
|
| 60 |
\param[in] exoid exodus file ID returned from a previous call to ex_create() or ex_open(). |
| 61 |
\param[in] obj_type Type of object; use one of the options in the table below. |
| 62 |
\param[in] num_props The number of integer properties to be assigned to all of the objects |
| 63 |
of the type specified (element blocks, node sets, or side sets). |
| 64 |
\param[in] prop_names Array containing \c num_props names (of maximum length |
| 65 |
of \p MAX_STR_LENGTH ) of properties to be stored. |
| 66 |
|
| 67 |
<table> |
| 68 |
<tr><td> \c EX_NODE_SET </td><td> Node Set entity type </td></tr> |
| 69 |
<tr><td> \c EX_EDGE_BLOCK </td><td> Edge Block entity type </td></tr> |
| 70 |
<tr><td> \c EX_EDGE_SET </td><td> Edge Set entity type </td></tr> |
| 71 |
<tr><td> \c EX_FACE_BLOCK </td><td> Face Block entity type </td></tr> |
| 72 |
<tr><td> \c EX_FACE_SET </td><td> Face Set entity type </td></tr> |
| 73 |
<tr><td> \c EX_ELEM_BLOCK </td><td> Element Block entity type</td></tr> |
| 74 |
<tr><td> \c EX_ELEM_SET </td><td> Element Set entity type </td></tr> |
| 75 |
<tr><td> \c EX_SIDE_SET </td><td> Side Set entity type </td></tr> |
| 76 |
<tr><td> \c EX_ELEM_MAP </td><td> Element Map entity type </td></tr> |
| 77 |
<tr><td> \c EX_NODE_MAP </td><td> Node Map entity type </td></tr> |
| 78 |
<tr><td> \c EX_EDGE_MAP </td><td> Edge Map entity type </td></tr> |
| 79 |
<tr><td> \c EX_FACE_MAP </td><td> Face Map entity type </td></tr> |
| 80 |
</table> |
| 81 |
|
| 82 |
For instance, suppose a user wanted to assign the 1st, 3rd, and 5th |
| 83 |
element blocks (those element blocks stored 1st, 3rd, and 5th, |
| 84 |
regardless of their ID) to a group (property) called \b TOP, and the |
| 85 |
2nd, 3rd, and 4th element blocks to a group called \b LSIDE. This |
| 86 |
could be accomplished with the following code: |
| 87 |
|
| 88 |
\code |
| 89 |
#include "exodusII.h"; |
| 90 |
|
| 91 |
char* prop_names[2]; |
| 92 |
int top_part[] = {1,0,1,0,1}; |
| 93 |
int lside_part[] = {0,1,1,1,0}; |
| 94 |
|
| 95 |
int id[] = {10, 20, 30, 40, 50}; |
| 96 |
|
| 97 |
prop_names[0] = "TOP"; |
| 98 |
prop_names[1] = "LSIDE"; |
| 99 |
|
| 100 |
\comment{This call to ex_put_prop_names is optional, but more efficient} |
| 101 |
ex_put_prop_names (exoid, EX_ELEM_BLOCK, 2, prop_names); |
| 102 |
|
| 103 |
\comment{The property values can be output individually thus} |
| 104 |
for (i=0; i < 5; i++) { |
| 105 |
ex_put_prop (exoid, EX_ELEM_BLOCK, id[i], prop_names[0], |
| 106 |
top_part[i]); |
| 107 |
|
| 108 |
ex_put_prop (exoid, EX_ELEM_BLOCK, id[i], prop_names[1], |
| 109 |
lside_part[i]); |
| 110 |
} |
| 111 |
|
| 112 |
\comment{Alternatively, the values can be output as an array} |
| 113 |
ex_put_prop_array (exoid, EX_ELEM_BLOCK, prop_names[0], |
| 114 |
top_part); |
| 115 |
|
| 116 |
ex_put_prop_array (exoid, EX_ELEM_BLOCK, prop_names[1], |
| 117 |
lside_part); |
| 118 |
|
| 119 |
\endcode |
| 120 |
|
| 121 |
*/ |
| 122 |
|
| 123 |
int ex_put_prop_names (int exoid, |
| 124 |
ex_entity_type obj_type, |
| 125 |
int num_props, |
| 126 |
char **prop_names) |
| 127 |
{ |
| 128 |
int status; |
| 129 |
int oldfill, temp; |
| 130 |
int i, propid, dimid, dims[1]; |
| 131 |
size_t name_length, prop_name_len; |
| 132 |
char name[MAX_VAR_NAME_LENGTH+1]; |
| 133 |
long long vals[1]; |
| 134 |
int max_name_len = 0; |
| 135 |
int int_type = NC_INT; |
| 136 |
|
| 137 |
char errmsg[MAX_ERR_LENGTH]; |
| 138 |
|
| 139 |
exerrval = 0; /* clear error code */ |
| 140 |
|
| 141 |
if (ex_int64_status(exoid) & EX_IDS_INT64_DB) { |
| 142 |
int_type = NC_INT64; |
| 143 |
} |
| 144 |
|
| 145 |
/* Get the name string length */ |
| 146 |
name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH)+1; |
| 147 |
|
| 148 |
/* inquire id of previously defined dimension (number of objects) */ |
| 149 |
if ((status = nc_inq_dimid(exoid, ex_dim_num_objects(obj_type), &dimid)) != NC_NOERR) { |
| 150 |
exerrval = status; |
| 151 |
sprintf(errmsg, |
| 152 |
"Error: failed to locate number of %s in file id %d", |
| 153 |
ex_name_of_object(obj_type), exoid); |
| 154 |
ex_err("ex_put_prop_names",errmsg, exerrval); |
| 155 |
return(EX_FATAL); |
| 156 |
} |
| 157 |
|
| 158 |
nc_set_fill(exoid, NC_FILL, &oldfill); /* fill with zeros per routine spec */ |
| 159 |
|
| 160 |
/* put netcdf file into define mode */ |
| 161 |
if ((status = nc_redef (exoid)) != NC_NOERR) { |
| 162 |
exerrval = status; |
| 163 |
sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid); |
| 164 |
ex_err("ex_put_prop_names",errmsg,exerrval); |
| 165 |
return (EX_FATAL); |
| 166 |
} |
| 167 |
|
| 168 |
/* define num_props variables; we postpend the netcdf variable name with */ |
| 169 |
/* a counter starting at 2 because "xx_prop1" is reserved for the id array*/ |
| 170 |
dims[0] = dimid; |
| 171 |
|
| 172 |
for (i=0; i<num_props; i++) { |
| 173 |
switch (obj_type) { |
| 174 |
case EX_ELEM_BLOCK: |
| 175 |
strcpy (name, VAR_EB_PROP(i+2)); |
| 176 |
break; |
| 177 |
case EX_FACE_BLOCK: |
| 178 |
strcpy (name, VAR_FA_PROP(i+2)); |
| 179 |
break; |
| 180 |
case EX_EDGE_BLOCK: |
| 181 |
strcpy (name, VAR_ED_PROP(i+2)); |
| 182 |
break; |
| 183 |
case EX_NODE_SET: |
| 184 |
strcpy (name, VAR_NS_PROP(i+2)); |
| 185 |
break; |
| 186 |
case EX_SIDE_SET: |
| 187 |
strcpy (name, VAR_SS_PROP(i+2)); |
| 188 |
break; |
| 189 |
case EX_EDGE_SET: |
| 190 |
strcpy (name, VAR_ES_PROP(i+2)); |
| 191 |
break; |
| 192 |
case EX_FACE_SET: |
| 193 |
strcpy (name, VAR_FS_PROP(i+2)); |
| 194 |
break; |
| 195 |
case EX_ELEM_SET: |
| 196 |
strcpy (name, VAR_ELS_PROP(i+2)); |
| 197 |
break; |
| 198 |
case EX_ELEM_MAP: |
| 199 |
strcpy (name, VAR_EM_PROP(i+2)); |
| 200 |
break; |
| 201 |
case EX_FACE_MAP: |
| 202 |
strcpy (name, VAR_FAM_PROP(i+2)); |
| 203 |
break; |
| 204 |
case EX_EDGE_MAP: |
| 205 |
strcpy (name, VAR_EDM_PROP(i+2)); |
| 206 |
break; |
| 207 |
case EX_NODE_MAP: |
| 208 |
strcpy (name, VAR_NM_PROP(i+2)); |
| 209 |
break; |
| 210 |
default: |
| 211 |
exerrval = EX_BADPARAM; |
| 212 |
sprintf(errmsg, "Error: object type %d not supported; file id %d", |
| 213 |
obj_type, exoid); |
| 214 |
ex_err("ex_put_prop_names",errmsg,exerrval); |
| 215 |
goto error_ret; /* Exit define mode and return */ |
| 216 |
} |
| 217 |
|
| 218 |
if ((status = nc_def_var(exoid, name, int_type, 1, dims, &propid)) != NC_NOERR) { |
| 219 |
exerrval = status; |
| 220 |
sprintf(errmsg, |
| 221 |
"Error: failed to create property array variable in file id %d", |
| 222 |
exoid); |
| 223 |
ex_err("ex_put_prop_names",errmsg,exerrval); |
| 224 |
goto error_ret; /* Exit define mode and return */ |
| 225 |
} |
| 226 |
|
| 227 |
vals[0] = 0; /* fill value */ |
| 228 |
|
| 229 |
/* create attribute to cause variable to fill with zeros per routine spec */ |
| 230 |
if ((status = nc_put_att_longlong(exoid, propid, _FillValue, int_type, 1, vals)) != NC_NOERR) { |
| 231 |
exerrval = status; |
| 232 |
sprintf(errmsg, |
| 233 |
"Error: failed to create property name fill attribute in file id %d", |
| 234 |
exoid); |
| 235 |
ex_err("ex_put_prop_names",errmsg,exerrval); |
| 236 |
goto error_ret; /* Exit define mode and return */ |
| 237 |
} |
| 238 |
|
| 239 |
/* Check that the property name length is less than MAX_NAME_LENGTH */ |
| 240 |
prop_name_len = strlen(prop_names[i])+1; |
| 241 |
if (prop_name_len > name_length) { |
| 242 |
fprintf(stderr, |
| 243 |
"Warning: The property name '%s' is too long.\n\tIt will be truncated from %d to %d characters\n", |
| 244 |
prop_names[i], (int)prop_name_len-1, (int)name_length-1); |
| 245 |
prop_name_len = name_length; |
| 246 |
} |
| 247 |
|
| 248 |
if (prop_name_len > max_name_len) |
| 249 |
max_name_len = prop_name_len; |
| 250 |
|
| 251 |
/* store property name as attribute of property array variable */ |
| 252 |
if ((status = nc_put_att_text(exoid, propid, ATT_PROP_NAME, |
| 253 |
prop_name_len, prop_names[i])) != NC_NOERR) { |
| 254 |
exerrval = status; |
| 255 |
sprintf(errmsg, |
| 256 |
"Error: failed to store property name %s in file id %d", |
| 257 |
prop_names[i],exoid); |
| 258 |
ex_err("ex_put_prop_names",errmsg,exerrval); |
| 259 |
goto error_ret; /* Exit define mode and return */ |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/* leave define mode */ |
| 264 |
if ((status = nc_enddef(exoid)) != NC_NOERR) { |
| 265 |
exerrval = status; |
| 266 |
sprintf(errmsg, |
| 267 |
"Error: failed to leave define mode in file id %d", |
| 268 |
exoid); |
| 269 |
ex_err("ex_put_prop_names",errmsg,exerrval); |
| 270 |
return (EX_FATAL); |
| 271 |
} |
| 272 |
|
| 273 |
/* Update the maximum_name_length attribute on the file. */ |
| 274 |
ex_update_max_name_length(exoid, max_name_len-1); |
| 275 |
|
| 276 |
nc_set_fill(exoid, oldfill, &temp); /* default: turn off fill */ |
| 277 |
return (EX_NOERR); |
| 278 |
|
| 279 |
/* Fatal error: exit definition mode and return */ |
| 280 |
error_ret: |
| 281 |
if (nc_enddef (exoid) != NC_NOERR) { /* exit define mode */ |
| 282 |
sprintf(errmsg, |
| 283 |
"Error: failed to complete definition for file id %d", |
| 284 |
exoid); |
| 285 |
ex_err("ex_put_prop_names",errmsg,exerrval); |
| 286 |
} |
| 287 |
return (EX_FATAL); |
| 288 |
} |