1 /*
2  * Copyright(C) 1999-2020 National Technology & Engineering Solutions
3  * of Sandia, LLC (NTESS).  Under the terms of Contract DE-NA0003525 with
4  * NTESS, the U.S. Government retains certain rights in this software.
5  *
6  * See packages/seacas/LICENSE for details
7  */
8 /*****************************************************************************
9  *
10  * expmp - ex_get_attr_param
11  *
12  * entry conditions -
13  *   input parameters:
14  *       int     exoid           exodus file id
15  *       int     obj_type        block/set type (node, edge, face, elem)
16  *       int     obj_id          block/set id (ignored for NODAL)
17  *       int     num_attrs       number of attributes
18  *
19  * exit conditions -
20  *
21  *
22  *****************************************************************************/
23 
24 #include "exodusII.h"     // for ex_err, etc
25 #include "exodusII_int.h" // for EX_FATAL, EX_NOERR, etc
26 
27 /*!
28  * \undoc retrieves the number of attributes.
29  */
30 
ex_get_attr_param(int exoid,ex_entity_type obj_type,ex_entity_id obj_id,int * num_attrs)31 int ex_get_attr_param(int exoid, ex_entity_type obj_type, ex_entity_id obj_id, int *num_attrs)
32 {
33   int status;
34   int dimid;
35 
36   char        errmsg[MAX_ERR_LENGTH];
37   const char *dnumobjatt;
38 
39   int    obj_id_ndx;
40   size_t lnum_attr_per_entry;
41 
42   EX_FUNC_ENTER();
43   if (ex__check_valid_file_id(exoid, __func__) == EX_FATAL) {
44     EX_FUNC_LEAVE(EX_FATAL);
45   }
46 
47   /* Determine index of obj_id in vobjids array */
48   if (obj_type == EX_NODAL) {
49     obj_id_ndx = 0;
50   }
51   else {
52     obj_id_ndx = ex__id_lkup(exoid, obj_type, obj_id);
53     if (obj_id_ndx <= 0) {
54       ex_get_err(NULL, NULL, &status);
55 
56       if (status != 0) {
57         if (status == EX_NULLENTITY) {
58           *num_attrs = 0;
59           EX_FUNC_LEAVE(EX_NOERR);
60         }
61         snprintf(errmsg, MAX_ERR_LENGTH,
62                  "Warning: failed to locate %s id %" PRId64 " in id array in file id %d",
63                  ex_name_of_object(obj_type), obj_id, exoid);
64         ex_err_fn(exoid, __func__, errmsg, status);
65         EX_FUNC_LEAVE(EX_WARN);
66       }
67     }
68   }
69 
70   switch (obj_type) {
71   case EX_SIDE_SET: dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx); break;
72   case EX_NODE_SET: dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx); break;
73   case EX_EDGE_SET: dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx); break;
74   case EX_FACE_SET: dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx); break;
75   case EX_ELEM_SET: dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx); break;
76   case EX_NODAL: dnumobjatt = DIM_NUM_ATT_IN_NBLK; break;
77   case EX_EDGE_BLOCK: dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx); break;
78   case EX_FACE_BLOCK: dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx); break;
79   case EX_ELEM_BLOCK: dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx); break;
80   default:
81     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Bad block type (%d) specified for file id %d",
82              obj_type, exoid);
83     ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
84     EX_FUNC_LEAVE(EX_FATAL);
85   }
86 
87   if ((status = nc_inq_dimid(exoid, dnumobjatt, &dimid)) != NC_NOERR) {
88     /* dimension is undefined */
89     *num_attrs = 0;
90   }
91   else {
92     if ((status = nc_inq_dimlen(exoid, dimid, &lnum_attr_per_entry)) != NC_NOERR) {
93       snprintf(errmsg, MAX_ERR_LENGTH,
94                "ERROR: failed to get number of attributes in %s %" PRId64 " in file id %d",
95                ex_name_of_object(obj_type), obj_id, exoid);
96       ex_err_fn(exoid, __func__, errmsg, status);
97       EX_FUNC_LEAVE(EX_FATAL);
98     }
99     *num_attrs = lnum_attr_per_entry;
100   }
101   EX_FUNC_LEAVE(EX_NOERR);
102 }
103