Exemplo n.º 1
0
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);
}
Exemplo n.º 2
0
int ex_add_attr(int exoid, ex_entity_type obj_type, ex_entity_id obj_id, int64_t num_attr_per_entry)
{
  int    status;
  int    dims[2];
  int    strdim, varid, att_name_varid;
  size_t num_obj;

  char        errmsg[MAX_ERR_LENGTH];
  const char *dnumobjent;
  const char *dnumobjatt;
  const char *vobjatt;
  const char *vattnam;
  int         numobjentdim;
  int         obj_id_ndx;
  int         numattrdim;

  exerrval = 0; /* clear error code */

  if (num_attr_per_entry <= 0) {
    return (EX_NOERR);
  }

  /* Determine index of obj_id in obj_type id array */
  if (obj_type == EX_NODAL) {
    obj_id_ndx = 0;
  }
  else {
    obj_id_ndx = ex_id_lkup(exoid, obj_type, obj_id);

    if (exerrval != 0) {
      if (exerrval == EX_NULLENTITY) {
        snprintf(errmsg, MAX_ERR_LENGTH,
                 "Warning: no attributes found for NULL %s %" PRId64 " in file id %d",
                 ex_name_of_object(obj_type), obj_id, exoid);
        ex_err("ex_add_attr", errmsg, EX_NULLENTITY);
        return (EX_WARN); /* no attributes for this object */
      }
      snprintf(errmsg, MAX_ERR_LENGTH,
               "Warning: failed to locate %s id %" PRId64 " in id array in file id %d",
               ex_name_of_object(obj_type), obj_id, exoid);
      ex_err("ex_add_attr", errmsg, exerrval);
      return (EX_WARN);
    }
  }

  switch (obj_type) {
  case EX_SIDE_SET:
    dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
    vobjatt    = VAR_SSATTRIB(obj_id_ndx);
    vattnam    = VAR_NAME_SSATTRIB(obj_id_ndx);
    break;
  case EX_NODE_SET:
    dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
    vobjatt    = VAR_NSATTRIB(obj_id_ndx);
    vattnam    = VAR_NAME_NSATTRIB(obj_id_ndx);
    break;
  case EX_EDGE_SET:
    dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
    vobjatt    = VAR_ESATTRIB(obj_id_ndx);
    vattnam    = VAR_NAME_ESATTRIB(obj_id_ndx);
    break;
  case EX_FACE_SET:
    dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
    vobjatt    = VAR_FSATTRIB(obj_id_ndx);
    vattnam    = VAR_NAME_FSATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_SET:
    dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
    vobjatt    = VAR_ELSATTRIB(obj_id_ndx);
    vattnam    = VAR_NAME_ELSATTRIB(obj_id_ndx);
    break;
  case EX_NODAL:
    dnumobjent = DIM_NUM_NODES;
    dnumobjatt = DIM_NUM_ATT_IN_NBLK;
    vobjatt    = VAR_NATTRIB;
    vattnam    = VAR_NAME_NATTRIB;
    break;
  case EX_EDGE_BLOCK:
    dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
    vobjatt    = VAR_EATTRIB(obj_id_ndx);
    vattnam    = VAR_NAME_EATTRIB(obj_id_ndx);
    break;
  case EX_FACE_BLOCK:
    dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
    vobjatt    = VAR_FATTRIB(obj_id_ndx);
    vattnam    = VAR_NAME_FATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_BLOCK:
    dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
    vobjatt    = VAR_ATTRIB(obj_id_ndx);
    vattnam    = VAR_NAME_ATTRIB(obj_id_ndx);
    break;
  default:
    exerrval = EX_BADPARAM;
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Bad block type (%d) specified for file id %d",
             obj_type, exoid);
    ex_err("ex_put_attr_param", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* element attribute array */
  /* put netcdf file into define mode  */
  if ((status = nc_redef(exoid)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid);
    ex_err("ex_add_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  if ((status = nc_def_dim(exoid, dnumobjatt, num_attr_per_entry, &numattrdim)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to define number of attributes in %s %" PRId64 " in file id %d",
             ex_name_of_object(obj_type), obj_id, exoid);
    ex_err("ex_add_attr", errmsg, exerrval);
    goto error_ret; /* exit define mode and return */
  }

  ex_get_dimension(exoid, dnumobjent, ex_name_of_object(obj_type), &num_obj, &numobjentdim,
                   "ex_add_attr");
  dims[0] = numobjentdim;
  dims[1] = numattrdim;

  if ((status = nc_def_var(exoid, vobjatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR:  failed to define attributes for %s %" PRId64 " in file id %d",
             ex_name_of_object(obj_type), obj_id, exoid);
    ex_err("ex_add_attr", errmsg, exerrval);
    goto error_ret; /* exit define mode and return */
  }
  ex_compress_variable(exoid, varid, 2);

  /* inquire previously defined dimensions  */
  if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid);
    ex_err("ex_add_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* Attribute names... */
  dims[0] = numattrdim;
  dims[1] = strdim;

  if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &att_name_varid)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to define %s attribute name array in file id %d",
             ex_name_of_object(obj_type), exoid);
    ex_err("ex_add_attr", errmsg, exerrval);
    goto error_ret; /* exit define mode and return */
  }

  /* leave define mode  */

  if ((exerrval = nc_enddef(exoid)) != NC_NOERR) {
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete %s definition in file id %d",
             ex_name_of_object(obj_type), exoid);
    ex_err("ex_add_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* Output a dummy empty attribute name in case client code doesn't
     write anything; avoids corruption in some cases.
  */
  if (att_name_varid >= 0) {
    size_t count[2];
    size_t start[2];
    char * text = "";
    size_t i;

    count[0] = 1;
    start[1] = 0;
    count[1] = strlen(text) + 1;

    for (i = 0; i < num_attr_per_entry; i++) {
      start[0] = i;
      nc_put_vara_text(exoid, att_name_varid, start, count, text);
    }
  }

  return (EX_NOERR);

/* Fatal error: exit definition mode and return */
error_ret:
  if (nc_enddef(exoid) != NC_NOERR) { /* exit define mode */
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition for file id %d", exoid);
    ex_err("ex_add_attr", errmsg, exerrval);
  }
  return (EX_FATAL);
}
Exemplo n.º 3
0
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);
}
Exemplo n.º 4
0
Arquivo: exgblk.c Projeto: 0004c/VTK
int ex_get_block( int exoid,
      ex_entity_type blk_type,
      int blk_id,
      char* elem_type,
      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 dimid, connid, blk_id_ndx;
  size_t len;
  char *ptr;
  char  errmsg[MAX_ERR_LENGTH];
  int status;
  const char* dnument;
  const char* dnumnod;
  const char* dnumedg;
  const char* dnumfac;
  const char* dnumatt;
  const char* ablknam;
  const char* vblkcon;

  exerrval = 0;

  /* First, locate index of element block id in VAR_ID_EL_BLK array */
  blk_id_ndx = ex_id_lkup(exoid,blk_type,blk_id);
  if (exerrval != 0)  {
    if (exerrval == EX_NULLENTITY) {    /* NULL element block?    */
      if ( elem_type )
  strcpy(elem_type, "NULL");     /* NULL element type name */
      *num_entries_this_blk = 0;       /* no elements            */
      *num_nodes_per_entry = 0;        /* no nodes               */
      *num_attr_per_entry = 0;         /* no attributes          */
      return (EX_NOERR);
    } else {
      sprintf(errmsg,
        "Error: failed to locate %s id %d in id array in file id %d",
        ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_get_block",errmsg,exerrval);
      return (EX_FATAL);
    }
  }

  switch (blk_type) {
  case EX_EDGE_BLOCK:
    dnument = DIM_NUM_ED_IN_EBLK(blk_id_ndx);
    dnumnod = DIM_NUM_NOD_PER_ED(blk_id_ndx);
    dnumedg = 0;
    dnumfac = 0;
    dnumatt = DIM_NUM_ATT_IN_EBLK(blk_id_ndx);
    vblkcon = VAR_EBCONN(blk_id_ndx);
    ablknam = ATT_NAME_ELB;
    break;
  case EX_FACE_BLOCK:
    dnument = DIM_NUM_FA_IN_FBLK(blk_id_ndx);
    dnumnod = DIM_NUM_NOD_PER_FA(blk_id_ndx);
    dnumedg = 0; /* it is possible this might be non-NULL some day */
    dnumfac = 0;
    dnumatt = DIM_NUM_ATT_IN_FBLK(blk_id_ndx);
    vblkcon = VAR_FBCONN(blk_id_ndx);
    ablknam = ATT_NAME_ELB;
    break;
  case EX_ELEM_BLOCK:
    dnument = DIM_NUM_EL_IN_BLK(blk_id_ndx);
    dnumnod = DIM_NUM_NOD_PER_EL(blk_id_ndx);
    dnumedg = DIM_NUM_EDG_PER_EL(blk_id_ndx);
    dnumfac = DIM_NUM_FAC_PER_EL(blk_id_ndx);
    dnumatt = DIM_NUM_ATT_IN_BLK(blk_id_ndx);
    vblkcon = VAR_CONN(blk_id_ndx);
    ablknam = ATT_NAME_ELB;
    break;
  default:
    exerrval = EX_BADPARAM;
    sprintf( errmsg, "Bad block type parameter (%d) specified for file id %d.",
       blk_type, exoid );
    return (EX_FATAL);
  }

  /* inquire values of some dimensions */
  if ( num_entries_this_blk ) {
    if ((status = nc_inq_dimid (exoid, dnument, &dimid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
        "Error: failed to locate number of entities in %s %d in file id %d",
        ex_name_of_object(blk_type),blk_id,exoid);
      ex_err("ex_get_block",errmsg, exerrval);
      return(EX_FATAL);
    }

    if ((status = nc_inq_dimlen (exoid, dimid, &len)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
        "Error: failed to get number of %ss in block %d in file id %d",
        ex_name_of_object(blk_type),blk_id, exoid);
      ex_err("ex_get_block",errmsg, exerrval);
      return(EX_FATAL);
    }
    *num_entries_this_blk = len;
  }

  if ( num_nodes_per_entry ) {
    if ((status = nc_inq_dimid (exoid, dnumnod, &dimid)) != NC_NOERR) {
      /* undefined => no node entries per element */
      len = 0;
    } else {
      if ((status = nc_inq_dimlen (exoid, dimid, &len)) != NC_NOERR) {
  exerrval = status;
  sprintf(errmsg,
    "Error: failed to get number of nodes/entity in %s %d in file id %d",
    ex_name_of_object(blk_type),blk_id, exoid);
  ex_err("ex_get_block",errmsg, exerrval);
  return(EX_FATAL);
      }
    }
    *num_nodes_per_entry = len;
  }

  if ( num_edges_per_entry ) {
    if ( blk_type != EX_ELEM_BLOCK ) {
      exerrval = (EX_WARN);
      sprintf(errmsg,
        "Warning: non-NULL pointer passed to num_edges_per_entry for %s query in file id %d",
        ex_name_of_object(blk_type),exoid);
      ex_err("ex_get_block",errmsg,exerrval);
    } else {
      if ((status = nc_inq_dimid (exoid, dnumedg, &dimid)) != NC_NOERR) {
  /* undefined => no edge entries per element */
  len = 0;
      } else {
  if ((status = nc_inq_dimlen (exoid, dimid, &len)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
      "Error: failed to get number of edges/entry in %s %d in file id %d",
      ex_name_of_object(blk_type),blk_id, exoid);
    ex_err("ex_get_block",errmsg, exerrval);
    return(EX_FATAL);
  }
      }
      *num_edges_per_entry = len;
    }
  }

  if ( num_faces_per_entry ) {
    if ( blk_type != EX_ELEM_BLOCK ) {
      exerrval = (EX_WARN);
      sprintf(errmsg,
        "Warning: non-NULL pointer passed to num_faces_per_entry for %s query in file id %d",
        ex_name_of_object(blk_type),exoid);
      ex_err("ex_get_block",errmsg,exerrval);
    } else {
      if ((status = nc_inq_dimid (exoid, dnumfac, &dimid)) != NC_NOERR) {
  /* undefined => no face entries per element */
  len = 0;
      } else {
  if ((status = nc_inq_dimlen(exoid, dimid, &len)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
      "Error: failed to get number of faces/entity in %s %d in file id %d",
      ex_name_of_object(blk_type),blk_id, exoid);
    ex_err("ex_get_block",errmsg, exerrval);
    return(EX_FATAL);
  }
      }
      *num_faces_per_entry = len;
    }
  }

  if ( num_attr_per_entry ) {
    if ((status = nc_inq_dimid (exoid, dnumatt, &dimid)) != NC_NOERR) {
      /* dimension is undefined */
      *num_attr_per_entry = 0;
    } else {
      if ((status = nc_inq_dimlen(exoid, dimid, &len)) != NC_NOERR) {
  exerrval = status;
  sprintf(errmsg,
    "Error: failed to get number of attributes in %s %d in file id %d",
    ex_name_of_object(blk_type),blk_id, exoid);
  ex_err("ex_get_block",errmsg, exerrval);
  return(EX_FATAL);
      }
      *num_attr_per_entry = len;
    }
  }

  if ( elem_type ) {
    if (*num_nodes_per_entry > 0) {
      ; /* Do nothing, vblkcon should be correctly set already */
    } else if (*num_edges_per_entry > 0) {
      vblkcon = VAR_EBCONN(blk_id_ndx);
    } else if (*num_faces_per_entry > 0) {
      vblkcon = VAR_FCONN(blk_id_ndx);
    }

    if (vblkcon) {
      /* look up connectivity array for this element block id */
      if ((status = nc_inq_varid (exoid, vblkcon, &connid)) != NC_NOERR) {
  exerrval = status;
  sprintf(errmsg,
    "Error: failed to locate connectivity array for %s %d in file id %d",
    ex_name_of_object(blk_type), blk_id,exoid);
  ex_err("ex_get_block",errmsg, exerrval);
  return(EX_FATAL);
      }

      if ((status = nc_inq_attlen (exoid, connid, ablknam, &len)) != NC_NOERR) {
  exerrval = status;
  sprintf(errmsg,
    "Error: failed to get %s %d type in file id %d",
    ex_name_of_object(blk_type), blk_id,exoid);
  ex_err("ex_get_block",errmsg, exerrval);
  return(EX_FATAL);
      }

      if (len > (MAX_STR_LENGTH+1)) {
  len = MAX_STR_LENGTH;
  sprintf (errmsg,
     "Warning: %s %d type will be truncated to %ld chars", 
     ex_name_of_object(blk_type), blk_id, (long)len);
  ex_err("ex_get_block",errmsg,EX_MSG);
      }
      
      /* get the element type name */
      if ((status = nc_get_att_text (exoid, connid, ablknam, elem_type)) != NC_NOERR) {
  exerrval = status;
  sprintf(errmsg,"Error: failed to get %s %d type in file id %d",
    ex_name_of_object(blk_type), blk_id, exoid);
  ex_err("ex_get_block",errmsg, exerrval);
  return(EX_FATAL);
      }
      
      /* get rid of trailing blanks */
      ptr = elem_type;
      /* fprintf(stderr,"[exgblk] %s, len: %d\n",ptr,len); */
      while (ptr < elem_type + len && *ptr != ' ') {
  ptr++;
      }
      *(ptr) = '\0';
    }
  }
  return (EX_NOERR);
}
Exemplo n.º 5
0
Arquivo: expclb.c Projeto: hpcdev/xdm
/*!
 * writes the parameters used to describe an element block
 * \param    exoid                   exodus file id
 * \param    elem_blk_id             element block id
 * \param    elem_type               element type string
 * \param    num_elem_this_blk       number of elements in the element blk
 * \param    num_nodes_per_elem      number of nodes per element block
 * \param    num_attr                number of attributes
 * \param    define_maps             if != 0, write maps, else don't
 */
int ex_put_concat_elem_block (int    exoid,
                              const int*   elem_blk_id,
                              char *elem_type[],
                              const int*   num_elem_this_blk,
                              const int*   num_nodes_per_elem,
                              const int*   num_attr,
                              int    define_maps)
{
  int i, varid, dimid, dims[2], strdim, *eb_array;
  int temp;
  int iblk;
  int status;
  int num_elem_blk;
  size_t length;
  int cur_num_elem_blk, nelnoddim, numelbdim, numattrdim, connid, numelemdim, numnodedim;
  char errmsg[MAX_ERR_LENGTH];

  exerrval  = 0; /* clear error code */

  /* first check if any element blocks are specified
   * OK if zero...
   */
  if (nc_inq_dimid(exoid, DIM_NUM_EL_BLK, &dimid) != NC_NOERR) {
    return (EX_NOERR);
  }

  /* Get number of element blocks defined for this file */
  if ((status = nc_inq_dimlen(exoid,dimid,&length)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to get number of element blocks in file id %d",
	    exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }
  num_elem_blk = length;
  
  /* Fill out the element block status array */
  if (!(eb_array = malloc(num_elem_blk*sizeof(int)))) {
    exerrval = EX_MEMFAIL;
    sprintf(errmsg,
	    "Error: failed to allocate space for element block status array in file id %d",
	    exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  for (i=0;i<num_elem_blk;i++) {
    if (num_elem_this_blk[i] == 0) /* Is this a NULL element block? */
      eb_array[i] = 0; /* change element block status to NULL */
    else
      eb_array[i] = 1; /* change element block status to TRUE */
  }

  /* Next, get variable id of status array */
  if ((status = nc_inq_varid(exoid, VAR_STAT_EL_BLK, &varid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate element block status in file id %d",
	    exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  status = nc_put_var_int(exoid, varid, eb_array);

  if (status != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to store element block status array to file id %d",
            exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* Next, fill out ids array */
  /* first get id of ids array variable */
  if ((status = nc_inq_varid(exoid, VAR_ID_EL_BLK, &varid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate element block ids array in file id %d",
            exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* then, write out id list */
  status = nc_put_var_int(exoid, varid, elem_blk_id);

  if (status != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to store element block id array in file id %d",
            exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* inquire previously defined dimensions  */
  if ((status = nc_inq_dimid(exoid, DIM_STR, &strdim)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to get string length in file id %d",exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* put netcdf file into define mode  */
  if ((status = nc_redef (exoid)) != NC_NOERR)  {
    exerrval = status;
    sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* Iterate over element blocks ... */
  for (iblk = 0; iblk < num_elem_blk; iblk++) {

    cur_num_elem_blk=ex_get_file_item(exoid, ex_get_counter_list(EX_ELEM_BLOCK));
    if (cur_num_elem_blk >= num_elem_blk) {
      exerrval = EX_FATAL;
      sprintf(errmsg,
	      "Error: exceeded number of element blocks (%d) defined in file id %d",
              num_elem_blk,exoid);
      ex_err("ex_put_concat_elem_block",errmsg,exerrval);
      goto error_ret;
    }

    /* NOTE: ex_inc_file_item  is used to find the number of element blocks
       for a specific file and returns that value incremented. */
    cur_num_elem_blk=ex_inc_file_item(exoid, ex_get_counter_list(EX_ELEM_BLOCK));

    if (num_elem_this_blk[iblk] == 0) /* Is this a NULL element block? */
      continue;

    /* define some dimensions and variables*/
    if ((status = nc_def_dim(exoid,
			     DIM_NUM_EL_IN_BLK(cur_num_elem_blk+1),
			     num_elem_this_blk[iblk], &numelbdim)) != NC_NOERR) {
      exerrval = status;
      if (status == NC_ENAMEINUSE) {     /* duplicate entry */
	sprintf(errmsg,
		"Error: element block %d already defined in file id %d",
		elem_blk_id[iblk],exoid);
      } else {
	sprintf(errmsg,
		"Error: failed to define number of elements/block for block %d file id %d",
		elem_blk_id[iblk],exoid);
      }
      ex_err("ex_put_concat_elem_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

    if ((status = nc_def_dim(exoid,
			     DIM_NUM_NOD_PER_EL(cur_num_elem_blk+1),
			     num_nodes_per_elem[iblk], &nelnoddim)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to define number of nodes/element for block %d in file id %d",
	      elem_blk_id[iblk],exoid);
      ex_err("ex_put_concat_elem_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

    /* element connectivity array */
    dims[0] = numelbdim;
    dims[1] = nelnoddim;

    if ((status = nc_def_var (exoid, VAR_CONN(cur_num_elem_blk+1),
			      NC_INT, 2, dims, &connid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to create connectivity array for block %d in file id %d",
	      elem_blk_id[iblk],exoid);
      ex_err("ex_put_concat_elem_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

    /* store element type as attribute of connectivity variable */
    if ((status = nc_put_att_text(exoid, connid, ATT_NAME_ELB, strlen(elem_type[iblk])+1, 
				  (void*)elem_type[iblk])) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to store element type name %s in file id %d",
	      elem_type[iblk],exoid);
      ex_err("ex_put_concat_elem_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

    /* element attribute array */
    if (num_attr[iblk] > 0) {
      if ((status = nc_def_dim (exoid, 
				DIM_NUM_ATT_IN_BLK(cur_num_elem_blk+1),
				num_attr[iblk], &numattrdim)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define number of attributes in block %d in file id %d",
		elem_blk_id[iblk],exoid);
	ex_err("ex_put_concat_elem_block",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
      
      /* Attribute names... */
      dims[0] = numattrdim;
      dims[1] = strdim;
      
      if ((status = nc_def_var(exoid, VAR_NAME_ATTRIB(cur_num_elem_blk+1),
			       NC_CHAR, 2, dims, &temp)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define element attribute name array in file id %d",exoid);
	ex_err("ex_put_concat_elem_block",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
      eb_array[iblk] = temp;

      dims[0] = numelbdim;
      dims[1] = numattrdim;
      
      if ((status = nc_def_var(exoid, VAR_ATTRIB(cur_num_elem_blk+1),
			       nc_flt_code(exoid), 2, dims, &temp)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error:  failed to define attributes for element block %d in file id %d",
		elem_blk_id[iblk],exoid);
	ex_err("ex_put_concat_elem_block",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }

    }
    
  }

  /* Define the element map here to avoid a later redefine call */
  if (define_maps != 0) {
    if (nc_inq_varid(exoid, VAR_ELEM_NUM_MAP, &temp) != NC_NOERR) {
      /* Map does not exist */
      /* Possible to have zero elements but >0 element blocks.
       * Only define map if there are nonzero elements
       */
      if (nc_inq_dimid(exoid, DIM_NUM_ELEM, &numelemdim) == NC_NOERR) {
	dims[0] = numelemdim;
	
	if ((status = nc_def_var(exoid, VAR_ELEM_NUM_MAP, NC_INT, 1, dims, &temp)) != NC_NOERR) {
	  exerrval = status;
	  if (status == NC_ENAMEINUSE) {
	    sprintf(errmsg,
		    "Error: element numbering map already exists in file id %d",
		    exoid);
	  } else {
	    sprintf(errmsg,
		    "Error: failed to create element numbering map in file id %d",
		    exoid);
	  }
	  ex_err("ex_put_concat_elem_block",errmsg,exerrval);
	  goto error_ret;         /* exit define mode and return */
	}
      }
    }

    /* Do the same for the node numbering map */
    if (nc_inq_varid(exoid, VAR_NODE_NUM_MAP, &temp) != NC_NOERR) {
      /* Map does not exist */
      if ((nc_inq_dimid(exoid, DIM_NUM_NODES, &numnodedim)) == NC_NOERR) {
	dims[0] = numnodedim;
	if ((status = nc_def_var(exoid, VAR_NODE_NUM_MAP, NC_INT, 1, dims, &temp)) != NC_NOERR) {
	  exerrval = status;
	  if (status == NC_ENAMEINUSE) {
	    sprintf(errmsg,
		    "Error: node numbering map already exists in file id %d",
		    exoid);
	  } else {
	    sprintf(errmsg,
		    "Error: failed to create node numbering map array in file id %d",
		    exoid);
	  }
	  ex_err("ex_put_concat_elem_block",errmsg,exerrval);
	  goto error_ret;         /* exit define mode and return */
	}
      }
    }
  }

  /* leave define mode  */
  if ((status = nc_enddef(exoid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to complete element block definition in file id %d", 
	    exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  {
  /* Write dummy attribute name. Without this we get corruption in the
   * attribute name.
   */
    size_t  start[2], count[2];
    char *text = "";
    count[0] = 1;
    start[1] = 0;
    count[1] = strlen(text)+1;
    
    for (iblk = 0; iblk < num_elem_blk; iblk++) {
      if (num_elem_this_blk[iblk] == 0) /* Is this a NULL element block? */
	continue;
      for (i = 0; i < num_attr[iblk]; i++) {
	start[0] = i;
	nc_put_vara_text(exoid, eb_array[iblk], start, count, text);
      }
    }
  }
  free(eb_array);
  
  return (EX_NOERR);
  
  /* Fatal error: exit definition mode and return */
 error_ret:
  if (nc_enddef (exoid) != NC_NOERR) {     /* exit define mode */
    sprintf(errmsg,
	    "Error: failed to complete definition for file id %d",
	    exoid);
    ex_err("ex_put_concat_elem_block",errmsg,exerrval);
  }
  return (EX_FATAL);
}
Exemplo n.º 6
0
int ex_get_block_param( int exoid,
			ex_block *block )
{
  int dimid, connid, blk_id_ndx;
  size_t len, i;
  char  errmsg[MAX_ERR_LENGTH];
  int status;
  const char* dnument;
  const char* dnumnod;
  const char* dnumedg;
  const char* dnumfac;
  const char* dnumatt;
  const char* ablknam;
  const char* vblkcon;

  struct ex_file_item* file = ex_find_file_item(exoid);
  if (!file ) {
    char errmsg[MAX_ERR_LENGTH];
    exerrval = EX_BADFILEID;
    sprintf(errmsg,"Error: unknown file id %d in ex_get_block_param().",exoid);
    ex_err("ex_get_block_param",errmsg,exerrval);
  }
  
  exerrval = 0;

  /* First, locate index of element block id in VAR_ID_EL_BLK array */
  blk_id_ndx = ex_id_lkup(exoid,block->type,block->id);
  if (exerrval != 0)  {
    strcpy(block->topology, "NULL");     	/* NULL element type name */
    block->num_entry = 0;  /* no elements            */
    block->num_nodes_per_entry = 0;   /* no nodes               */
    block->num_edges_per_entry = 0;
    block->num_faces_per_entry = 0;
    block->num_attribute = 0;    /* no attributes          */
    if (exerrval == EX_NULLENTITY) {    /* NULL element block?    */
      return (EX_NOERR);
    } 
      sprintf(errmsg,
	      "Error: failed to locate %s id  %"PRId64" in id array in file id %d",
	      ex_name_of_object(block->type), block->id,exoid);
      ex_err("ex_get_block_param",errmsg,exerrval);
      return (EX_FATAL);
    
  }

  switch (block->type) {
  case EX_EDGE_BLOCK:
    dnument = DIM_NUM_ED_IN_EBLK(blk_id_ndx);
    dnumnod = DIM_NUM_NOD_PER_ED(blk_id_ndx);
    dnumedg = 0;
    dnumfac = 0;
    dnumatt = DIM_NUM_ATT_IN_EBLK(blk_id_ndx);
    vblkcon = VAR_EBCONN(blk_id_ndx);
    ablknam = ATT_NAME_ELB;
    break;
  case EX_FACE_BLOCK:
    dnument = DIM_NUM_FA_IN_FBLK(blk_id_ndx);
    dnumnod = DIM_NUM_NOD_PER_FA(blk_id_ndx);
    dnumedg = 0; /* it is possible this might be non-NULL some day */
    dnumfac = 0;
    dnumatt = DIM_NUM_ATT_IN_FBLK(blk_id_ndx);
    vblkcon = VAR_FBCONN(blk_id_ndx);
    ablknam = ATT_NAME_ELB;
    break;
  case EX_ELEM_BLOCK:
    dnument = DIM_NUM_EL_IN_BLK(blk_id_ndx);
    dnumnod = DIM_NUM_NOD_PER_EL(blk_id_ndx);
    dnumedg = DIM_NUM_EDG_PER_EL(blk_id_ndx);
    dnumfac = DIM_NUM_FAC_PER_EL(blk_id_ndx);
    dnumatt = DIM_NUM_ATT_IN_BLK(blk_id_ndx);
    vblkcon = VAR_CONN(blk_id_ndx);
    ablknam = ATT_NAME_ELB;
    break;
  default:
    exerrval = EX_BADPARAM;
    sprintf( errmsg, "Bad block type parameter (%d) specified for file id %d.",
	     block->type, exoid );
    return (EX_FATAL);
  }

  /* inquire values of some dimensions */
  if ((status = nc_inq_dimid (exoid, dnument, &dimid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate number of entities in %s  %"PRId64" in file id %d",
	    ex_name_of_object(block->type),block->id,exoid);
    ex_err("ex_get_block_param",errmsg, exerrval);
    return(EX_FATAL);
  }
  
  if ((status = nc_inq_dimlen (exoid, dimid, &len)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to get number of %ss in block  %"PRId64" in file id %d",
	    ex_name_of_object(block->type),block->id, exoid);
    ex_err("ex_get_block_param",errmsg, exerrval);
    return(EX_FATAL);
  }
  block->num_entry = len;

  if ((status = nc_inq_dimid (exoid, dnumnod, &dimid)) != NC_NOERR) {
    /* undefined => no node entries per element */
    len = 0;
  } else {
    if ((status = nc_inq_dimlen (exoid, dimid, &len)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to get number of nodes/entity in %s  %"PRId64" in file id %d",
	      ex_name_of_object(block->type),block->id, exoid);
      ex_err("ex_get_block_param",errmsg, exerrval);
      return(EX_FATAL);
    }
  }
  block->num_nodes_per_entry = len;

  if (!file->has_edges || block->type != EX_ELEM_BLOCK) {
    block->num_edges_per_entry = 0;
  } else {
    if ((status = nc_inq_dimid (exoid, dnumedg, &dimid)) != NC_NOERR) {
      /* undefined => no edge entries per element */
      len = 0;
    } else {
      if ((status = nc_inq_dimlen (exoid, dimid, &len)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to get number of edges/entry in %s  %"PRId64" in file id %d",
		ex_name_of_object(block->type),block->id, exoid);
	ex_err("ex_get_block_param",errmsg, exerrval);
	return(EX_FATAL);
      }
    }
    block->num_edges_per_entry = len;
  }

  if (!file->has_faces || block->type != EX_ELEM_BLOCK ) {
    block->num_faces_per_entry = 0;
  } else {
    if ((status = nc_inq_dimid (exoid, dnumfac, &dimid)) != NC_NOERR) {
      /* undefined => no face entries per element */
      len = 0;
    } else {
      if ((status = nc_inq_dimlen(exoid, dimid, &len)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to get number of faces/entity in %s  %"PRId64" in file id %d",
		ex_name_of_object(block->type),block->id, exoid);
	ex_err("ex_get_block_param",errmsg, exerrval);
	return(EX_FATAL);
      }
    }
    block->num_faces_per_entry = len;
  }

  if ((status = nc_inq_dimid (exoid, dnumatt, &dimid)) != NC_NOERR) {
    /* dimension is undefined */
    block->num_attribute = 0;
  } else {
    if ((status = nc_inq_dimlen(exoid, dimid, &len)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to get number of attributes in %s  %"PRId64" in file id %d",
	      ex_name_of_object(block->type),block->id, exoid);
      ex_err("ex_get_block_param",errmsg, exerrval);
      return(EX_FATAL);
    }
    block->num_attribute = len;
  }

  if (block->num_nodes_per_entry > 0) {
    ; /* Do nothing, vblkcon should be correctly set already */
  } else if (block->num_edges_per_entry > 0) {
    vblkcon = VAR_EBCONN(blk_id_ndx);
  } else if (block->num_faces_per_entry > 0) {
    vblkcon = VAR_FCONN(blk_id_ndx);
  }

  if (vblkcon) {
    /* look up connectivity array for this element block id */
    if ((status = nc_inq_varid (exoid, vblkcon, &connid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to locate connectivity array for %s  %"PRId64" in file id %d",
	      ex_name_of_object(block->type), block->id,exoid);
      ex_err("ex_get_block_param",errmsg, exerrval);
      return(EX_FATAL);
    }
    
    if ((status = nc_inq_attlen (exoid, connid, ablknam, &len)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to get %s  %"PRId64" type in file id %d",
	      ex_name_of_object(block->type), block->id,exoid);
      ex_err("ex_get_block_param",errmsg, exerrval);
      return(EX_FATAL);
    }
    
    if (len > (MAX_STR_LENGTH+1)) {
      len = MAX_STR_LENGTH;
      sprintf (errmsg,
	       "Warning: %s  %"PRId64" type will be truncated to %ld chars", 
	       ex_name_of_object(block->type), block->id, (long)len);
      ex_err("ex_get_block_param",errmsg,EX_MSG);
    }
    
    for (i=0; i < MAX_STR_LENGTH+1; i++) {
      block->topology[i] = '\0';

    /* get the element type name */
    
}if ((status = nc_get_att_text (exoid, connid, ablknam, block->topology)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,"Error: failed to get %s  %"PRId64" type in file id %d",
	      ex_name_of_object(block->type), block->id, exoid);
      ex_err("ex_get_block_param",errmsg, exerrval);
      return(EX_FATAL);
    }
    
    /* get rid of trailing blanks */
    ex_trim_internal(block->topology);
  }
  return (EX_NOERR);
}
Exemplo n.º 7
0
int ex_get_partial_attr(int exoid, ex_entity_type obj_type, ex_entity_id obj_id, int64_t start_num,
                        int64_t num_ent, void *attrib)

{
  int         status;
  int         attrid, obj_id_ndx;
  int         temp;
  size_t      num_entries_this_obj, num_attr;
  size_t      start[2], count[2];
  char        errmsg[MAX_ERR_LENGTH];
  const char *dnumobjent;
  const char *dnumobjatt;
  const char *vattrbname;

  ex_check_valid_file_id(exoid);

  exerrval = 0; /* clear error code */

  if (num_ent == 0) {
    return 0;
  }
  /* Determine index of obj_id in vobjids array */
  if (obj_type != EX_NODAL) {
    obj_id_ndx = ex_id_lkup(exoid, obj_type, obj_id);
    if (exerrval != 0) {
      if (exerrval == EX_NULLENTITY) {
        snprintf(errmsg, MAX_ERR_LENGTH,
                 "Warning: no attributes found for NULL %s %" PRId64 " in file id %d",
                 ex_name_of_object(obj_type), obj_id, exoid);
        ex_err("ex_get_partial_attr", errmsg, EX_NULLENTITY);
        return (EX_WARN); /* no attributes for this object */
      }
      snprintf(errmsg, MAX_ERR_LENGTH,
               "Warning: failed to locate %s id%" PRId64 " in id array in file id %d",
               ex_name_of_object(obj_type), obj_id, exoid);
      ex_err("ex_get_partial_attr", errmsg, exerrval);
      return (EX_WARN);
    }
  }

  switch (obj_type) {
  case EX_SIDE_SET:
    dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
    vattrbname = VAR_SSATTRIB(obj_id_ndx);
    break;
  case EX_NODE_SET:
    dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
    vattrbname = VAR_NSATTRIB(obj_id_ndx);
    break;
  case EX_EDGE_SET:
    dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
    vattrbname = VAR_ESATTRIB(obj_id_ndx);
    break;
  case EX_FACE_SET:
    dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
    vattrbname = VAR_FSATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_SET:
    dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
    vattrbname = VAR_ELSATTRIB(obj_id_ndx);
    break;
  case EX_NODAL:
    dnumobjent = DIM_NUM_NODES;
    dnumobjatt = DIM_NUM_ATT_IN_NBLK;
    vattrbname = VAR_NATTRIB;
    break;
  case EX_EDGE_BLOCK:
    dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
    vattrbname = VAR_EATTRIB(obj_id_ndx);
    break;
  case EX_FACE_BLOCK:
    dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
    vattrbname = VAR_FATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_BLOCK:
    dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
    vattrbname = VAR_ATTRIB(obj_id_ndx);
    break;
  default:
    exerrval = 1005;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "Internal ERROR: unrecognized object type in switch: %d in file id %d", obj_type,
             exoid);
    ex_err("ex_get_partial_attr", errmsg, EX_MSG);
    return (EX_FATAL); /* number of attributes not defined */
  }

  /* inquire id's of previously defined dimensions  */
  if (ex_get_dimension(exoid, dnumobjent, "entries", &num_entries_this_obj, &temp,
                       "ex_get_partial_attr") != NC_NOERR) {
    return EX_FATAL;
  }

  if (start_num + num_ent - 1 > num_entries_this_obj) {
    exerrval = EX_BADPARAM;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: start index (%" PRId64 ") + count (%" PRId64
             ") is larger than total number of entities (%" ST_ZU ") in file id %d",
             start_num, num_ent, num_entries_this_obj, exoid);
    ex_err("ex_get_partial_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  if (ex_get_dimension(exoid, dnumobjatt, "attributes", &num_attr, &temp, "ex_get_partial_attr") !=
      NC_NOERR) {
    return EX_FATAL;
  }

  if ((status = nc_inq_varid(exoid, vattrbname, &attrid)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to locate attributes for %s %" PRId64 " in file id %d",
             ex_name_of_object(obj_type), obj_id, exoid);
    ex_err("ex_get_partial_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* read in the attributes */
  start[0] = start_num - 1;
  start[1] = 0;

  count[0] = num_ent;
  count[1] = num_attr;

  if (ex_comp_ws(exoid) == 4) {
    status = nc_get_vara_float(exoid, attrid, start, count, attrib);
  }
  else {
    status = nc_get_vara_double(exoid, attrid, start, count, attrib);
  }

  if (status != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to get attributes for %s %" PRId64 " in file id %d",
             ex_name_of_object(obj_type), obj_id, exoid);
    ex_err("ex_get_partial_attr", errmsg, exerrval);
    return (EX_FATAL);
  }
  return (EX_NOERR);
}
Exemplo n.º 8
0
int ex_put_block_params( int         exoid,
			 size_t      block_count,
			 const struct ex_block *blocks)
{
  size_t i;
  int conn_int_type;
  int status;
  int arbitrary_polyhedra = 0; /* 1 if block is arbitrary 2d polyhedra type; 2 if 3d polyhedra */
  int att_name_varid = -1;
  int varid, dimid, dims[2], blk_id_ndx, blk_stat, strdim;
  size_t start[2];
  size_t num_blk;
  int cur_num_blk, numblkdim, numattrdim;
  int nnodperentdim = -1;
  int nedgperentdim = -1;
  int nfacperentdim = -1;
  int connid;
  int npeid;
  char errmsg[MAX_ERR_LENGTH];
  char entity_type1[5];
  char entity_type2[5];
  int* blocks_to_define = NULL;
  const char* dnumblk = NULL;
  const char* vblkids = NULL;
  const char* vblksta = NULL;
  const char* vnodcon = NULL;
  const char* vnpecnt = NULL;
  const char* vedgcon = NULL;
  const char* vfaccon = NULL;
  const char* vconn   = NULL;
  const char* vattnam = NULL;
  const char* vblkatt = NULL;
  const char* dneblk  = NULL;
  const char* dnape   = NULL;
  const char* dnnpe   = NULL;
  const char* dnepe   = NULL;
  const char* dnfpe   = NULL;

  exerrval  = 0; /* clear error code */

  blocks_to_define = malloc(block_count*sizeof(int));

  for (i=0; i < block_count; i++) {
    switch (blocks[i].type) {
    case EX_EDGE_BLOCK:
      dnumblk = DIM_NUM_ED_BLK;
      vblkids = VAR_ID_ED_BLK;
      vblksta = VAR_STAT_ED_BLK;
      break;
    case EX_FACE_BLOCK:
      dnumblk = DIM_NUM_FA_BLK;
      vblkids = VAR_ID_FA_BLK;
      vblksta = VAR_STAT_FA_BLK;
      break;
    case EX_ELEM_BLOCK:
      dnumblk = DIM_NUM_EL_BLK;
      vblkids = VAR_ID_EL_BLK;
      vblksta = VAR_STAT_EL_BLK;
      break;
    default:
      exerrval = EX_BADPARAM;
      sprintf( errmsg, "Error: Bad block type (%d) specified for entry %d file id %d",
	       blocks[i].type, (int)i, exoid );
      ex_err( "ex_put_block_params", errmsg, exerrval );
      free(blocks_to_define);
      return (EX_FATAL);
    }

    /* first check if any blocks of that type are specified */
    if ((status = ex_get_dimension(exoid, dnumblk, ex_name_of_object(blocks[i].type),
				   &num_blk, &dimid, "ex_put_block_params")) != NC_NOERR) {
      sprintf(errmsg,
	      "Error: No %ss defined in file id %d",
	      ex_name_of_object(blocks[i].type), exoid);
      ex_err("ex_put_block_params",errmsg,exerrval);
      free(blocks_to_define);
      return EX_FATAL;
    }

    /* Next: Make sure that there are not any duplicate block ids 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 ((status = nc_inq_varid(exoid, vblkids, &varid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to locate %s ids in file id %d",
	      ex_name_of_object(blocks[i].type), exoid);
      ex_err("ex_put_block_params",errmsg,exerrval);
      free(blocks_to_define);
      return EX_FATAL;
    }

    ex_id_lkup(exoid,blocks[i].type,blocks[i].id); /* Error value used, but don't need return value */
    if (exerrval != EX_LOOKUPFAIL) {   /* found the element block id */
      exerrval = EX_FATAL;
      sprintf(errmsg,
	      "Error: %s id %"PRId64" already exists in file id %d",
	      ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
      ex_err("ex_put_block_params",errmsg,exerrval);
      free(blocks_to_define);
      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.
    */
    cur_num_blk=ex_get_file_item(exoid, ex_get_counter_list(blocks[i].type));
    if (cur_num_blk >= (int)num_blk) {
      exerrval = EX_FATAL;
      sprintf(errmsg,
	      "Error: exceeded number of %ss (%d) defined in file id %d",
	      ex_name_of_object(blocks[i].type), (int)num_blk,exoid);
      ex_err("ex_put_block_params",errmsg,exerrval);
      free(blocks_to_define);
      return (EX_FATAL);
    }

    /*   NOTE: ex_inc_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, ex_get_counter_list(blocks[i].type));
    start[0] = cur_num_blk;

    /* write out block id to previously defined id array variable*/
    status = nc_put_var1_longlong(exoid, varid, start, (long long*)&blocks[i].id);

    if (status != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to store %s id to file id %d",
	      ex_name_of_object(blocks[i].type), exoid);
      ex_err("ex_put_block_params",errmsg,exerrval);
      free(blocks_to_define);
      return (EX_FATAL);
    }

    blocks_to_define[i] = start[0]+1; /* element id index into vblkids array*/

    if (blocks[i].num_entry == 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 EX_EX_TRUE */

    if ((status = nc_inq_varid (exoid, vblksta, &varid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to locate %s status in file id %d",
	      ex_name_of_object(blocks[i].type), exoid);
      ex_err("ex_put_block_params",errmsg,exerrval);
      free(blocks_to_define);
      return (EX_FATAL);
    }

    if ((status = nc_put_var1_int(exoid, varid, start, &blk_stat)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to store %s id %"PRId64" status to file id %d",
	      ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
      ex_err("ex_put_block_params",errmsg,exerrval);
      free(blocks_to_define);
      return (EX_FATAL);
    }
  }

  /* put netcdf file into define mode  */
  if ((status=nc_redef (exoid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid);
    ex_err("ex_put_block_params",errmsg,exerrval);
    free(blocks_to_define);
    return (EX_FATAL);
  }


  for (i=0; i < block_count; i++) {
    if (blocks[i].num_entry == 0) {/* Is this a NULL element block? */
      continue;
    }

    blk_id_ndx = blocks_to_define[i];

    switch (blocks[i].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);
      vnpecnt = VAR_FBEPEC(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);
      vnpecnt = VAR_EBEPEC(blk_id_ndx);
      vedgcon = VAR_ECONN(blk_id_ndx);
      vfaccon = VAR_FCONN(blk_id_ndx);
      break;
    default:
      goto error_ret;
    }

    /* define some dimensions and variables*/
    if ((status = nc_def_dim(exoid,dneblk,blocks[i].num_entry, &numblkdim )) != NC_NOERR) {
      if (status == NC_ENAMEINUSE) {        /* duplicate entry */
	exerrval = status;
	sprintf(errmsg,
		"Error: %s %"PRId64" already defined in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
      } else {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define number of entities/block for %s %"PRId64" file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
      }
      goto error_ret;         /* exit define mode and return */
    }

    if ( dnnpe && blocks[i].num_nodes_per_entry > 0) {
      /* A nfaced block would not have any nodes defined... */
      if ((status = nc_def_dim(exoid,dnnpe,blocks[i].num_nodes_per_entry, &nnodperentdim)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define number of nodes/entity for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
    }

    if (dnepe && blocks[i].num_edges_per_entry > 0 ) {
      if ((status = nc_def_dim (exoid,dnepe,blocks[i].num_edges_per_entry, &nedgperentdim)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define number of edges/entity for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
    }

    if ( dnfpe && blocks[i].num_faces_per_entry > 0 ) {
      if ((status = nc_def_dim(exoid,dnfpe,blocks[i].num_faces_per_entry, &nfacperentdim)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define number of faces/entity for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
    }

    /* element attribute array */
    if (blocks[i].num_attribute > 0) {

      if ((status = nc_def_dim(exoid, dnape, blocks[i].num_attribute, &numattrdim)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define number of attributes in %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }

      dims[0] = numblkdim;
      dims[1] = numattrdim;

      if ((status = nc_def_var(exoid, vblkatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error:  failed to define attributes for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
      ex_compress_variable(exoid, varid, 2);

      /* inquire previously defined dimensions  */
      if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to get string length in file id %d",exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;
      }
     
      /* Attribute names... */
      dims[0] = numattrdim;
      dims[1] = strdim;
	    
      if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &att_name_varid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define %s attribute name array in file id %d",
		ex_name_of_object(blocks[i].type), exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
    }

    conn_int_type = NC_INT;
    if (ex_int64_status(exoid) & EX_BULK_INT64_DB) {
      conn_int_type = NC_INT64;
    }

    /* See if storing an 'nsided' element block (arbitrary 2d polyhedra or super element) */
    if (strlen(blocks[i].topology) >= 3) {
      if ((blocks[i].topology[0] == 'n' || blocks[i].topology[0] == 'N') &&
	  (blocks[i].topology[1] == 's' || blocks[i].topology[1] == 'S') &&
	  (blocks[i].topology[2] == 'i' || blocks[i].topology[2] == 'I'))
	arbitrary_polyhedra = 1;
      else if ((blocks[i].topology[0] == 'n' || blocks[i].topology[0] == 'N') &&
	       (blocks[i].topology[1] == 'f' || blocks[i].topology[1] == 'F') &&
	       (blocks[i].topology[2] == 'a' || blocks[i].topology[2] == 'A'))
	/* If a FACE_BLOCK, then we are dealing with the faces of the nfaced blocks[i]. */
	arbitrary_polyhedra = blocks[i].type == EX_FACE_BLOCK ? 1 : 2;
    }

    /* element connectivity array */
    if (arbitrary_polyhedra > 0) {
      if (blocks[i].type != EX_FACE_BLOCK && blocks[i].type != EX_ELEM_BLOCK) {
	exerrval = EX_BADPARAM;
	sprintf( errmsg, "Error: Bad block type (%d) for nsided/nfaced block in file id %d",
		 blocks[i].type, exoid );
	ex_err( "ex_put_block_params", errmsg, exerrval );
	goto error_ret;
      }

      if (arbitrary_polyhedra == 1) {
	dims[0] = nnodperentdim;
	vconn = vnodcon;

	/* store entity types as attribute of npeid variable -- node/elem, node/face, face/elem*/
	strcpy(entity_type1, "NODE");
	if (blocks[i].type == EX_ELEM_BLOCK)
	  strcpy(entity_type2, "ELEM");
	else
	  strcpy(entity_type2, "FACE");
      } else if (arbitrary_polyhedra == 2) {
	dims[0] = nfacperentdim;
	vconn = vfaccon;

	/* store entity types as attribute of npeid variable -- node/elem, node/face, face/elem*/
	strcpy(entity_type1, "FACE");
	strcpy(entity_type2, "ELEM");
      }

      if ((status = nc_def_var(exoid, vconn, conn_int_type, 1, dims, &connid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to create connectivity array for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
    
      /* element face-per-element or node-per-element count array */
      dims[0] = numblkdim;
    
      if ((status = nc_def_var(exoid, vnpecnt, conn_int_type, 1, dims, &npeid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to create face- or node- per-entity count array for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }

      if ((status = nc_put_att_text(exoid, npeid, "entity_type1", strlen(entity_type1)+1,
				    entity_type1)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to store entity type attribute text for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
      if ((status = nc_put_att_text(exoid, npeid, "entity_type2", strlen(entity_type2)+1,
				    entity_type2)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to store entity type attribute text for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
    } else {
      /* "Normal" (non-polyhedra) element block type */
      dims[0] = numblkdim;
      dims[1] = nnodperentdim;
    
      if ((status = nc_def_var(exoid, vnodcon, conn_int_type, 2, dims, &connid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to create connectivity array for %s %"PRId64" in file id %d",
		ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	ex_err("ex_put_block_params",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }
      ex_compress_variable(exoid, connid, 1);
    }
    /* store element type as attribute of connectivity variable */
    if ((status = nc_put_att_text(exoid, connid, ATT_NAME_ELB, strlen(blocks[i].topology)+1, 
				  blocks[i].topology)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to store %s type name %s in file id %d",
	      ex_name_of_object(blocks[i].type), blocks[i].topology,exoid);
      ex_err("ex_put_block_params",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

    if (arbitrary_polyhedra == 0) {
      if (vedgcon && blocks[i].num_edges_per_entry ) {
	dims[0] = numblkdim;
	dims[1] = nedgperentdim;
      
	if ((status = nc_def_var(exoid, vedgcon, conn_int_type, 2, dims, &varid)) != NC_NOERR) {
	  exerrval = status;
	  sprintf(errmsg,
		  "Error: failed to create edge connectivity array for %s %"PRId64" in file id %d",
		  ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	  ex_err("ex_put_block_params",errmsg,exerrval);
	  goto error_ret;         /* exit define mode and return */
	}
      }
    
      if ( vfaccon && blocks[i].num_faces_per_entry ) {
	dims[0] = numblkdim;
	dims[1] = nfacperentdim;
      
	if ((status = nc_def_var(exoid, vfaccon, conn_int_type, 2, dims, &varid)) != NC_NOERR) {
	  exerrval = status;
	  sprintf(errmsg,
		  "Error: failed to create face connectivity array for %s %"PRId64" in file id %d",
		  ex_name_of_object(blocks[i].type), blocks[i].id,exoid);
	  ex_err("ex_put_block_params",errmsg,exerrval);
	  goto error_ret;         /* exit define mode and return */
	}
      }
    }
  }

  free(blocks_to_define);

  /* leave define mode  */
  if ((exerrval=nc_enddef (exoid)) != NC_NOERR) {
    sprintf(errmsg,
	    "Error: failed to complete %s definition in file id %d", 
	    ex_name_of_object(blocks[i].type), exoid);
    ex_err("ex_put_block_params",errmsg,exerrval);
    return (EX_FATAL);
  }


  for (i=0; i < block_count; i++) {
    switch (blocks[i].type) {
    case EX_EDGE_BLOCK:
      vblkids = VAR_ID_ED_BLK;
      break;
    case EX_FACE_BLOCK:
      vblkids = VAR_ID_FA_BLK;
      break;
    case EX_ELEM_BLOCK:
      vblkids = VAR_ID_EL_BLK;
      break;
    default:
      return (EX_FATAL); /* should have been handled earlier; quiet compiler here */
    }

    nc_inq_varid(exoid, vblkids, &att_name_varid);

    if (blocks[i].num_attribute > 0 && att_name_varid >= 0) {
      /* Output a dummy empty attribute name in case client code doesn't
	 write anything; avoids corruption in some cases.
      */
      size_t  count[2];
      char *text = "";
      size_t j;

      count[0] = 1;
      start[1] = 0;
      count[1] = strlen(text)+1;
  
      for (j = 0; j < blocks[i].num_attribute; j++) {
	start[0] = j;
	nc_put_vara_text(exoid, att_name_varid, start, count, text);
      }
    }
  }

  return (EX_NOERR);

  /* Fatal error: exit definition mode and return */
 error_ret:
  free(blocks_to_define);

  if (nc_enddef (exoid) != NC_NOERR) {    /* exit define mode */
    sprintf(errmsg,
	    "Error: failed to complete definition for file id %d",
	    exoid);
    ex_err("ex_put_block_params",errmsg,exerrval);
  }
  return (EX_FATAL);
}
Exemplo n.º 9
0
int ex_put_partial_attr(int exoid, ex_entity_type blk_type, ex_entity_id blk_id,
                        int64_t start_entity, int64_t num_entity, const void *attrib)
{
  int    status;
  int    attrid;
  int    blk_id_ndx = 0;
  int    numattrdim;
  size_t start[2], count[2];
  size_t num_attr;
  char   errmsg[MAX_ERR_LENGTH];

  EX_FUNC_ENTER();
  ex_check_valid_file_id(exoid, __func__);

  if (blk_type != EX_NODAL) {
    /* Determine index of blk_id in VAR_ID_EL_BLK array */
    blk_id_ndx = ex_id_lkup(exoid, blk_type, blk_id);
    if (blk_id_ndx <= 0) {
      ex_get_err(NULL, NULL, &status);

      if (status != 0) {
        if (status == EX_NULLENTITY) {
          snprintf(errmsg, MAX_ERR_LENGTH,
                   "Warning: no attributes allowed for NULL %s %" PRId64 " in file id %d",
                   ex_name_of_object(blk_type), blk_id, exoid);
          ex_err(__func__, errmsg, EX_NULLENTITY);
          EX_FUNC_LEAVE(EX_WARN); /* no attributes for this block */
        }
        snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %s id %" PRId64 " in in file id %d",
                 ex_name_of_object(blk_type), blk_id, exoid);
        ex_err(__func__, errmsg, status);
        EX_FUNC_LEAVE(EX_FATAL);
      }
    }
  }

  switch (blk_type) {
  case EX_SIDE_SET: status = nc_inq_varid(exoid, VAR_SSATTRIB(blk_id_ndx), &attrid); break;
  case EX_NODE_SET: status = nc_inq_varid(exoid, VAR_NSATTRIB(blk_id_ndx), &attrid); break;
  case EX_EDGE_SET: status = nc_inq_varid(exoid, VAR_ESATTRIB(blk_id_ndx), &attrid); break;
  case EX_FACE_SET: status = nc_inq_varid(exoid, VAR_FSATTRIB(blk_id_ndx), &attrid); break;
  case EX_ELEM_SET: status = nc_inq_varid(exoid, VAR_ELSATTRIB(blk_id_ndx), &attrid); break;
  case EX_NODAL: status = nc_inq_varid(exoid, VAR_NATTRIB, &attrid); break;
  case EX_EDGE_BLOCK: status = nc_inq_varid(exoid, VAR_EATTRIB(blk_id_ndx), &attrid); break;
  case EX_FACE_BLOCK: status = nc_inq_varid(exoid, VAR_FATTRIB(blk_id_ndx), &attrid); break;
  case EX_ELEM_BLOCK: status = nc_inq_varid(exoid, VAR_ATTRIB(blk_id_ndx), &attrid); break;
  default:
    snprintf(errmsg, MAX_ERR_LENGTH,
             "Internal ERROR: unrecognized object type in switch: %d in file id %d", blk_type,
             exoid);
    ex_err(__func__, errmsg, EX_BADPARAM);
    EX_FUNC_LEAVE(EX_FATAL); /* number of attributes not defined */
  }

  if (status != NC_NOERR) {
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to locate attribute variable for %s %" PRId64 " in file id %d",
             ex_name_of_object(blk_type), blk_id, exoid);
    ex_err(__func__, errmsg, status);
    EX_FUNC_LEAVE(EX_FATAL);
  }

  /* Determine number of attributes */
  switch (blk_type) {
  case EX_SIDE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_SS(blk_id_ndx), &numattrdim); break;
  case EX_NODE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_NS(blk_id_ndx), &numattrdim); break;
  case EX_EDGE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_ES(blk_id_ndx), &numattrdim); break;
  case EX_FACE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_FS(blk_id_ndx), &numattrdim); break;
  case EX_ELEM_SET:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_ELS(blk_id_ndx), &numattrdim);
    break;
  case EX_NODAL: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_NBLK, &numattrdim); break;
  case EX_EDGE_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_EBLK(blk_id_ndx), &numattrdim);
    break;
  case EX_FACE_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_FBLK(blk_id_ndx), &numattrdim);
    break;
  case EX_ELEM_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_BLK(blk_id_ndx), &numattrdim);
    break;
  default:
    /* No need for error message, handled in previous switch; just to quiet
     * compiler. */
    EX_FUNC_LEAVE(EX_FATAL);
  }

  if (status != NC_NOERR) {
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: number of attributes not defined for %s %" PRId64 " in file id %d",
             ex_name_of_object(blk_type), blk_id, exoid);
    ex_err(__func__, errmsg, status);
    EX_FUNC_LEAVE(EX_FATAL); /* number of attributes not defined */
  }

  if ((status = nc_inq_dimlen(exoid, numattrdim, &num_attr)) != NC_NOERR) {
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to get number of attributes for %s %" PRId64 " in file id %d",
             ex_name_of_object(blk_type), blk_id, exoid);
    ex_err(__func__, errmsg, status);
    EX_FUNC_LEAVE(EX_FATAL);
  }

  /* write out the attributes  */
  start[0] = --start_entity;
  start[1] = 0;

  count[0] = num_entity;
  count[1] = num_attr;
  if (count[0] == 0) {
    start[0] = 0;
  }

  if (ex_comp_ws(exoid) == 4) {
    status = nc_put_vara_float(exoid, attrid, start, count, attrib);
  }
  else {
    status = nc_put_vara_double(exoid, attrid, start, count, attrib);
  }

  if (status != NC_NOERR) {
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to put attributes for %s %" PRId64 " in file id %d",
             ex_name_of_object(blk_type), blk_id, exoid);
    ex_err(__func__, errmsg, status);
    EX_FUNC_LEAVE(EX_FATAL);
  }
  EX_FUNC_LEAVE(EX_NOERR);
}
Exemplo n.º 10
0
int ex_put_one_attr( int   exoid,
                     int   obj_type,
                     int   obj_id,
                     int   attrib_index,
                     const void *attrib )
{
  int numobjentdim, numattrdim, attrid, obj_id_ndx;
  long num_entries_this_obj, num_attr;
  size_t start[2], count[2];
  ptrdiff_t stride[2];
  int error;
  char errmsg[MAX_ERR_LENGTH];
  const char* tname;
  const char* vobjids;
  const char* dnumobjent = 0;
  const char* dnumobjatt = 0;
  const char* vattrbname = 0;

  switch (obj_type) {
  case EX_EDGE_BLOCK:
    tname = "edge block";
    vobjids = VAR_ID_ED_BLK;
    break;
  case EX_FACE_BLOCK:
    tname = "face block";
    vobjids = VAR_ID_FA_BLK;
    break;
  case EX_ELEM_BLOCK:
    tname = "element block";
    vobjids = VAR_ID_EL_BLK;
    break;
  default:
    exerrval = EX_BADPARAM;
    sprintf(errmsg, "Error: Bad block type (%d) specified for file id %d",
      obj_type, exoid );
    ex_err("ex_put_attr",errmsg,exerrval);
    return (EX_FATAL);
  }

  exerrval = 0; /* clear error code */

  /* Determine index of obj_id in vobjids array */
  obj_id_ndx = ex_id_lkup(exoid,vobjids,obj_id);
  if (exerrval != 0) 
    {
      if (exerrval == EX_NULLENTITY)
        {
          sprintf(errmsg,
                  "Warning: no attributes allowed for NULL %s %d in file id %d",
                  tname,obj_id,exoid);
          ex_err("ex_put_one_attr",errmsg,EX_MSG);
          return (EX_WARN);              /* no attributes for this element block */
        }
      else
        {
          sprintf(errmsg,
                  "Error: no %s id %d in %s array in file id %d",
                  tname, obj_id, vobjids, exoid);
          ex_err("ex_put_one_attr",errmsg,exerrval);
          return (EX_FATAL);
        }
    }

  switch (obj_type) {
  case EX_EDGE_BLOCK:
    dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
    vattrbname = VAR_EATTRIB(obj_id_ndx);
    break;
  case EX_FACE_BLOCK:
    dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
    vattrbname = VAR_FATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_BLOCK:
    dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
    vattrbname = VAR_ATTRIB(obj_id_ndx);
    break;
  }

  /* inquire id's of previously defined dimensions  */
  if ((numobjentdim = ncdimid (exoid, dnumobjent)) == -1)
    {
      if (ncerr == NC_EBADDIM)
        {
          exerrval = ncerr;
          sprintf(errmsg,
                  "Error: no %s with id %d in file id %d",
                  tname, obj_id, exoid);
          ex_err("ex_put_one_attr",errmsg,exerrval);
          return (EX_FATAL);
        }
      else
        {
          exerrval = ncerr;
          sprintf(errmsg,
                  "Error: failed to locate number of entries for %s %d in file id %d",
                  tname, obj_id, exoid);
          ex_err("ex_put_one_attr",errmsg,exerrval);
          return (EX_FATAL);
        }
    }


  if (ncdiminq (exoid, numobjentdim, (char *) 0, &num_entries_this_obj) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to get number of entries for %s %d in file id %d",
              tname,obj_id,exoid);
      ex_err("ex_put_one_attr",errmsg,exerrval);
      return (EX_FATAL);
    }


  if ((numattrdim = ncdimid(exoid, dnumobjatt)) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: number of attributes not defined for %s %d in file id %d",
              tname,obj_id,exoid);
      ex_err("ex_put_one_attr",errmsg,EX_MSG);
      return (EX_FATAL);              /* number of attributes not defined */
    }

  if (ncdiminq (exoid, numattrdim, (char *) 0, &num_attr) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to get number of attributes for block %d in file id %d",
              obj_id,exoid);
      ex_err("ex_put_one_attr",errmsg,exerrval);
      return (EX_FATAL);
    }

  if (attrib_index < 1 || attrib_index > num_attr) {
    exerrval = EX_FATAL;
    sprintf(errmsg,
            "Error: Invalid attribute index specified: %d.  Valid range is 1 to %ld for %s %d in file id %d",
            attrib_index, num_attr, tname, obj_id, exoid);
    ex_err("ex_put_one_attr",errmsg,exerrval);
    return (EX_FATAL);
  }

  if ((attrid = ncvarid (exoid, vattrbname)) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to locate attribute variable for %s %d in file id %d",
              tname,obj_id,exoid);
      ex_err("ex_put_one_attr",errmsg,exerrval);
      return (EX_FATAL);
    }


  /* write out the attributes  */

  start[0] = 0;
  start[1] = attrib_index-1;

  count[0] = num_entries_this_obj;
  count[1] = 1;

  stride[0] = 1;
  stride[1] = num_attr;
  
  if (nc_flt_code(exoid) == NC_FLOAT) {
    error = nc_put_vars_float(exoid, attrid, start, count, stride,
                              ex_conv_array(exoid,WRITE_CONVERT,attrib,
                                            (int)num_attr*num_entries_this_obj));
  } else {
    error = nc_put_vars_double(exoid, attrid, start, count, stride,
                               ex_conv_array(exoid,WRITE_CONVERT,attrib,
                                             (int)num_attr*num_entries_this_obj));
  }
  if (error == -1) {
    exerrval = ncerr;
    sprintf(errmsg,
            "Error: failed to put attribute %d for %s %d in file id %d",
            attrib_index, tname, obj_id, exoid);
    ex_err("ex_put_one_attr",errmsg,exerrval);
    return (EX_FATAL);
  }


  return(EX_NOERR);

}
Exemplo n.º 11
0
/*
 * reads the attributes for an edge, face, or element block
 */
int ex_get_one_attr( int   exoid,
                     ex_entity_type obj_type,
                     ex_entity_id   obj_id,
                     int   attrib_index,
                     void* attrib )

{
  int status;
  int attrid, obj_id_ndx;
  int temp;
  size_t num_entries_this_obj, num_attr;
  size_t start[2], count[2];
  ptrdiff_t stride[2];
  char errmsg[MAX_ERR_LENGTH];
  const char* dnumobjent;
  const char* dnumobjatt;
  const char* vattrbname;

  exerrval = 0; /* clear error code */

  /* Determine index of obj_id in vobjids array */
  if (obj_type != EX_NODAL) {
    obj_id_ndx = ex_id_lkup(exoid,obj_type,obj_id);
    if (exerrval != 0) {
      if (exerrval == EX_NULLENTITY) {
	sprintf(errmsg,
		"Warning: no attributes found for NULL %s %"PRId64" in file id %d",
		ex_name_of_object(obj_type),obj_id,exoid);
	ex_err("ex_get_one_attr",errmsg,EX_NULLENTITY);
	return (EX_WARN);              /* no attributes for this object */
      } else {
	sprintf(errmsg,
		"Warning: failed to locate %s id %"PRId64" in id array in file id %d",
		ex_name_of_object(obj_type),obj_id, exoid);
	ex_err("ex_get_one_attr",errmsg,exerrval);
	return (EX_WARN);
      }
    }
  }

  switch (obj_type) {
  case EX_SIDE_SET:
    dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
    vattrbname = VAR_SSATTRIB(obj_id_ndx);
    break;
  case EX_NODE_SET:
    dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
    vattrbname = VAR_NSATTRIB(obj_id_ndx);
    break;
  case EX_EDGE_SET:
    dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
    vattrbname = VAR_ESATTRIB(obj_id_ndx);
    break;
  case EX_FACE_SET:
    dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
    vattrbname = VAR_FSATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_SET:
    dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
    vattrbname = VAR_ELSATTRIB(obj_id_ndx);
    break;
  case EX_NODAL:
    dnumobjent = DIM_NUM_NODES;
    dnumobjatt = DIM_NUM_ATT_IN_NBLK;
    vattrbname = VAR_NATTRIB;
    break;
  case EX_EDGE_BLOCK:
    dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
    vattrbname = VAR_EATTRIB(obj_id_ndx);
    break;
  case EX_FACE_BLOCK:
    dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
    vattrbname = VAR_FATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_BLOCK:
    dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
    vattrbname = VAR_ATTRIB(obj_id_ndx);
    break;
  default:
    exerrval = 1005;
    sprintf(errmsg,
	    "Internal Error: unrecognized object type in switch: %d in file id %d",
	    obj_type,exoid);
    ex_err("ex_get_one_attr",errmsg,EX_MSG);
    return (EX_FATAL);              /* number of attributes not defined */
  }

  /* inquire id's of previously defined dimensions  */
  if (ex_get_dimension(exoid, dnumobjent,"entries", &num_entries_this_obj, &temp, "ex_get_one_attr") != NC_NOERR)
    return EX_FATAL;
  
  if (ex_get_dimension(exoid, dnumobjatt,"attributes", &num_attr, &temp, "ex_get_one_attr") != NC_NOERR)
    return EX_FATAL;

  if (attrib_index < 1 || attrib_index > (int)num_attr) {
    exerrval = EX_FATAL;
    sprintf(errmsg,
            "Error: Invalid attribute index specified: %d.  Valid range is 1 to %d for %s %"PRId64" in file id %d",
            attrib_index, (int)num_attr, ex_name_of_object(obj_type), obj_id, exoid);
    ex_err("ex_get_one_attr",errmsg,exerrval);
    return (EX_FATAL);
  }

  if ((status = nc_inq_varid(exoid, vattrbname, &attrid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate attributes for %s %"PRId64" in file id %d",
	    ex_name_of_object(obj_type),obj_id,exoid);
    ex_err("ex_get_one_attr",errmsg,exerrval);
    return (EX_FATAL);
  }


  /* read in the attributes */
  start[0] = 0;
  start[1] = attrib_index-1;

  count[0] = num_entries_this_obj;
  count[1] = 1;

  stride[0] = 1;
  stride[1] = num_attr;
  
  if (ex_comp_ws(exoid) == 4) {
    status = nc_get_vars_float(exoid, attrid, start, count, stride, attrib);
  } else {
    status = nc_get_vars_double(exoid, attrid, start, count, stride, attrib);
  }

  if (status != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
            "Error: failed to get attribute %d for %s %"PRId64" in file id %d",
            attrib_index, ex_name_of_object(obj_type), obj_id,exoid);
    ex_err("ex_get_one_attr",errmsg,exerrval);
    return (EX_FATAL);
  }
  return(EX_NOERR);
}
Exemplo n.º 12
0
/*
 * reads the attribute names for an element block
 */
int ex_get_attr_names( int   exoid,
                       int   obj_type,
                       int   obj_id,
                       char **names)
{
  int varid, numattrdim, obj_id_ndx;
  long num_attr, start[2];
  char *ptr;
  char errmsg[MAX_ERR_LENGTH];
  int i, j;
  const char* tname;
  const char* vobjids;
  const char* dnumobjatt = 0;
  const char* vattrbname = 0;

  switch (obj_type) {
  case EX_EDGE_BLOCK:
    tname = "edge block";
    vobjids = VAR_ID_ED_BLK;
    break;
  case EX_FACE_BLOCK:
    tname = "face block";
    vobjids = VAR_ID_FA_BLK;
    break;
  case EX_ELEM_BLOCK:
    tname = "element block";
    vobjids = VAR_ID_EL_BLK;
    break;
  default:
    exerrval = EX_BADPARAM;
    sprintf( errmsg, "Error: Invalid object type (%d) specified for file id %d",
      obj_type, exoid );
    ex_err( "ex_get_attr_names", errmsg, exerrval );
    return (EX_FATAL);
  }
 
  exerrval = 0; /* clear error code */

  /* Determine index of obj_id in vobjids array */
  obj_id_ndx = ex_id_lkup(exoid,vobjids,obj_id);
  if (exerrval != 0) 
  {
    if (exerrval == EX_NULLENTITY)
    {
      sprintf(errmsg,
              "Warning: no attributes found for NULL block %d in file id %d",
              obj_id,exoid);
      ex_err("ex_get_attr_names",errmsg,EX_MSG);
      return (EX_WARN);              /* no attributes for this object */
    }
    else
    {
      sprintf(errmsg,
      "Warning: failed to locate %s id %d in %s array in file id %d",
              tname, obj_id,vobjids, exoid);
      ex_err("ex_get_attr_names",errmsg,exerrval);
      return (EX_WARN);
    }
  }


  switch (obj_type) {
  case EX_EDGE_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
    vattrbname = VAR_NAME_EATTRIB(obj_id_ndx);
    break;
  case EX_FACE_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
    vattrbname = VAR_NAME_FATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
    vattrbname = VAR_NAME_ATTRIB(obj_id_ndx);
    break;
  }
/* inquire id's of previously defined dimensions  */

  if ((numattrdim = ncdimid(exoid, dnumobjatt)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
            "Warning: no attributes found for %s %d in file id %d",
            tname,obj_id,exoid);
    ex_err("ex_get_attr_names",errmsg,EX_MSG);
    return (EX_WARN);              /* no attributes for this object */
  }

  if (ncdiminq (exoid, numattrdim, (char *) 0, &num_attr) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
         "Error: failed to get number of attributes for block %d in file id %d",
            obj_id,exoid);
    ex_err("ex_get_attr_names",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* It is OK if we don't find the attribute names since they were
     added at version 4.26; earlier databases won't have the names.
  */
  varid = ncvarid (exoid, vattrbname);

/* read in the attributes */

  if (varid != -1) {
    /* read the names */
    for (i=0; i < num_attr; i++) {
      start[0] = i;
      start[1] = 0;
      
      j = 0;
      ptr = names[i];
      
      if (ncvarget1 (exoid, varid, start, ptr) == -1) {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to get names for %s %d in file id %d",
                tname, obj_id, exoid);
        ex_err("ex_get_attr_names",errmsg,exerrval);
        return (EX_FATAL);
      }
      
      while ((*ptr++ != '\0') && (j < MAX_STR_LENGTH)) {
        start[1] = ++j;
        if (ncvarget1 (exoid, varid, start, ptr) == -1) {
          exerrval = ncerr;
          sprintf(errmsg,
                  "Error: failed to get names for %s %d in file id %d",
                  tname, obj_id, exoid);
          ex_err("ex_get_attr_names",errmsg,exerrval);
          return (EX_FATAL);
        }
       }
       --ptr;
       if (ptr > names[i]) {
         /*    get rid of trailing blanks */
         while (*(--ptr) == ' ');
       }
       *(++ptr) = '\0';
     }
   } else {
     /* Names variable does not exist on the database; probably since this is an
      * older version of the database.  Return an empty array...
      */
     for (i=0; i<num_attr; i++) {
       names[i][0] = '\0';
     }
   }
  return(EX_NOERR);
}
Exemplo n.º 13
0
Arquivo: expatn.c Projeto: hpcdev/xdm
/*!
 * writes the attribute names for a block
 * \param   exoid                   exodus file id
 * \param   blk_type                block type (edge, face, elem)
 * \param   blk_id                  block id
 * \param   names                   ptr to array of attribute names
 */
int ex_put_attr_names(int   exoid,
		      ex_entity_type blk_type,
		      int   blk_id,
		      char* names[])
{
  int status;
  int varid, numattrdim, blk_id_ndx;
  size_t num_attr, start[2], count[2];
  char errmsg[MAX_ERR_LENGTH];
  size_t i;
   
  exerrval = 0; /* clear error code */

  blk_id_ndx = ex_id_lkup(exoid, blk_type, blk_id);

  /* Determine index of blk_id in blk_id_ndx array */
  if (exerrval != 0) {
    if (exerrval == EX_NULLENTITY) {
      sprintf(errmsg,
	      "Warning: no attributes allowed for NULL %s %d in file id %d",
	      ex_name_of_object(blk_type),blk_id,exoid);
      ex_err("ex_put_attr_names",errmsg,EX_MSG);
      return (EX_WARN);              /* no attributes for this block */
    } else {
      sprintf(errmsg,
	      "Error: no %s id %d in %s array in file id %d",
	      ex_name_of_object(blk_type), blk_id, VAR_ID_EL_BLK, exoid);
      ex_err("ex_put_attr_names",errmsg,exerrval);
      return (EX_FATAL);
    }
  }

  /* inquire id's of previously defined dimensions  */
  switch (blk_type) {
  case EX_SIDE_SET:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_SS(blk_id_ndx), &numattrdim);
    break;
  case EX_NODE_SET:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_NS(blk_id_ndx), &numattrdim);
    break;
  case EX_EDGE_SET:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_ES(blk_id_ndx), &numattrdim);
    break;
  case EX_FACE_SET:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_FS(blk_id_ndx), &numattrdim);
    break;
  case EX_ELEM_SET:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_ELS(blk_id_ndx), &numattrdim);
    break;
  case EX_NODAL:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_NBLK, &numattrdim);
    break;
  case EX_EDGE_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_EBLK(blk_id_ndx), &numattrdim);
    break;
  case EX_FACE_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_FBLK(blk_id_ndx), &numattrdim);
    break;
  case EX_ELEM_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_BLK(blk_id_ndx), &numattrdim);
    break;
  default:
    exerrval = 1005;
    sprintf(errmsg,
	    "Internal Error: unrecognized object type in switch: %d in file id %d",
	    blk_type,exoid);
    ex_err("ex_put_attr_names",errmsg,EX_MSG);
    return (EX_FATAL);              /* number of attributes not defined */
  }

  if (status != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: number of attributes not defined for %s %d in file id %d",
	    ex_name_of_object(blk_type),blk_id,exoid);
    ex_err("ex_put_attr_names",errmsg,EX_MSG);
    return (EX_FATAL);              /* number of attributes not defined */
  }

  if ((status = nc_inq_dimlen(exoid, numattrdim, &num_attr)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to get number of attributes for %s %d in file id %d",
	    ex_name_of_object(blk_type),blk_id,exoid);
    ex_err("ex_put_attr_names",errmsg,exerrval);
    return (EX_FATAL);
  }

  switch (blk_type) {
  case EX_SIDE_SET:
    status = nc_inq_varid (exoid, VAR_NAME_SSATTRIB(blk_id_ndx), &varid);
    break;
  case EX_NODE_SET:
    status = nc_inq_varid (exoid, VAR_NAME_NSATTRIB(blk_id_ndx), &varid);
    break;
  case EX_EDGE_SET:
    status = nc_inq_varid (exoid, VAR_NAME_ESATTRIB(blk_id_ndx), &varid);
    break;
  case EX_FACE_SET:
    status = nc_inq_varid (exoid, VAR_NAME_FSATTRIB(blk_id_ndx), &varid);
    break;
  case EX_ELEM_SET:
    status = nc_inq_varid (exoid, VAR_NAME_ELSATTRIB(blk_id_ndx), &varid);
    break;
  case EX_NODAL:
    status = nc_inq_varid (exoid, VAR_NAME_NATTRIB, &varid);
    break;
  case EX_EDGE_BLOCK:
    status = nc_inq_varid (exoid, VAR_NAME_EATTRIB(blk_id_ndx), &varid);
    break;
  case EX_FACE_BLOCK:
    status = nc_inq_varid (exoid, VAR_NAME_FATTRIB(blk_id_ndx), &varid);
    break;
  case EX_ELEM_BLOCK:
    status = nc_inq_varid (exoid, VAR_NAME_ATTRIB(blk_id_ndx), &varid);
    break;
  default:
    exerrval = 1005;
    sprintf(errmsg,
	    "Internal Error: unrecognized object type in switch: %d in file id %d",
	    blk_type,exoid);
    ex_err("ex_put_attr_names",errmsg,EX_MSG);
    return (EX_FATAL);              /* number of attributes not defined */
  }

  if (status != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate %s attribute names for %s %d in file id %d",
	    ex_name_of_object(blk_type),ex_name_of_object(blk_type),blk_id, exoid);
    ex_err("ex_put_attr_names",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* write out the attributes  */
  for (i = 0; i < num_attr; i++) {
    start[0] = i;
    start[1] = 0;

    count[0] = 1;
    count[1] = strlen(names[i])+1;

    if ((status = nc_put_vara_text(exoid, varid, start, count, (void*) names[i])) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to put attribute namess for %s %d in file id %d",
	      ex_name_of_object(blk_type),blk_id,exoid);
      ex_err("ex_put_attr_names",errmsg,exerrval);
      return (EX_FATAL);
    }
  }
  return(EX_NOERR);
}
Exemplo n.º 14
0
int ex_get_block( int exoid,
  int blk_type,
  int blk_id,
  char* elem_type,
  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 dimid, connid, len, blk_id_ndx;
   long lnum_entries_this_blk, lnum_nodes_per_entry, lnum_attr_per_entry;
   long lnum_edges_per_entry, lnum_faces_per_entry;
   char *ptr;
   char  errmsg[MAX_ERR_LENGTH];
   nc_type dummy;
   const char* tname;
   const char* dnument;
   const char* dnumnod;
   const char* dnumedg;
   const char* dnumfac;
   const char* dnumatt;
   const char* ablknam;
   const char* vblkcon;
   const char* vblkids;

   exerrval = 0;

   /* First, locate index of element block id in VAR_ID_EL_BLK array */
   switch (blk_type) {
   case EX_EDGE_BLOCK:
     tname = "edge";
     vblkids = VAR_ID_ED_BLK;
     blk_id_ndx = ex_id_lkup(exoid,vblkids,blk_id);
     dnument = DIM_NUM_ED_IN_EBLK(blk_id_ndx);
     dnumnod = DIM_NUM_NOD_PER_ED(blk_id_ndx);
     dnumedg = 0;
     dnumfac = 0;
     dnumatt = DIM_NUM_ATT_IN_EBLK(blk_id_ndx);
     vblkcon = VAR_EBCONN(blk_id_ndx);
     ablknam = ATT_NAME_ELB;
     break;
   case EX_FACE_BLOCK:
     tname = "face";
     vblkids = VAR_ID_FA_BLK;
     blk_id_ndx = ex_id_lkup(exoid,vblkids,blk_id);
     dnument = DIM_NUM_FA_IN_FBLK(blk_id_ndx);
     dnumnod = DIM_NUM_NOD_PER_FA(blk_id_ndx);
     dnumedg = 0; /* it is possible this might be non-NULL some day */
     dnumfac = 0;
     dnumatt = DIM_NUM_ATT_IN_FBLK(blk_id_ndx);
     vblkcon = VAR_FBCONN(blk_id_ndx);
     ablknam = ATT_NAME_ELB;
     break;
   case EX_ELEM_BLOCK:
     tname = "element";
     vblkids = VAR_ID_EL_BLK;
     blk_id_ndx = ex_id_lkup(exoid,vblkids,blk_id);
     dnument = DIM_NUM_EL_IN_BLK(blk_id_ndx);
     dnumnod = DIM_NUM_NOD_PER_EL(blk_id_ndx);
     dnumedg = DIM_NUM_EDG_PER_EL(blk_id_ndx);
     dnumfac = DIM_NUM_FAC_PER_EL(blk_id_ndx);
     dnumatt = DIM_NUM_ATT_IN_BLK(blk_id_ndx);
     vblkcon = VAR_CONN(blk_id_ndx);
     ablknam = ATT_NAME_ELB;
     break;
   default:
     exerrval = EX_BADPARAM;
     sprintf( errmsg, "Bad block type parameter (%d) specified for file id %d.",
       blk_type, exoid );
     return (EX_FATAL);
   }

   if (exerrval != 0) 
     {
     if (exerrval == EX_NULLENTITY)     /* NULL element block?    */
       {
       if ( elem_type )
         strcpy(elem_type, "NULL");     /* NULL element type name */
       *num_entries_this_blk = 0;       /* no elements            */
       *num_nodes_per_entry = 0;        /* no nodes               */
       *num_attr_per_entry = 0;         /* no attributes          */
       return (EX_NOERR);
       }
     else
       {
       sprintf(errmsg,
        "Error: failed to locate element block id %d in %s array in file id %d",
               blk_id,vblkids,exoid);
       ex_err("ex_get_block",errmsg,exerrval);
       return (EX_FATAL);
       }
     }

   /* inquire values of some dimensions */
   if ( num_entries_this_blk )
     {
     if ((dimid = ncdimid (exoid, dnument)) == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to locate number of %ss in block %d in file id %d",
         tname,blk_id,exoid);
       ex_err("ex_get_block",errmsg, exerrval);
       return(EX_FATAL);
       }

     if (ncdiminq (exoid, dimid, (char *) 0, &lnum_entries_this_blk) == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to get number of %ss in block %d in file id %d",
         tname,blk_id, exoid);
       ex_err("ex_get_block",errmsg, exerrval);
       return(EX_FATAL);
       }
     *num_entries_this_blk = lnum_entries_this_blk;
     }

   if ( num_nodes_per_entry )
     {
     if ((dimid = ncdimid (exoid, dnumnod)) == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to locate number of nodes/%s in block %d in file id %d",
         tname,blk_id,exoid);
       ex_err("ex_get_block",errmsg, exerrval);
       return(EX_FATAL);
       }
     if (ncdiminq (exoid, dimid, (char *) 0, &lnum_nodes_per_entry) == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to get number of nodes/%s in block %d in file id %d",
         tname,blk_id, exoid);
       ex_err("ex_get_block",errmsg, exerrval);
       return(EX_FATAL);
       }
     *num_nodes_per_entry = lnum_nodes_per_entry;
     }

   if ( num_edges_per_entry )
     {
     if ( blk_type != EX_ELEM_BLOCK )
       {
       exerrval = (EX_WARN);
       sprintf(errmsg,
         "Warning: non-NULL pointer passed to num_edges_per_entry for %s block query in file id %d",
         tname,exoid);
       ex_err("ex_get_block",errmsg,exerrval);
       }
     else
       {
       if ((dimid = ncdimid (exoid, dnumedg)) == -1)
         {
         /* undefined => no edge entries per element */
         lnum_edges_per_entry = 0;
         }
       else
         {
         if (ncdiminq (exoid, dimid, (char *) 0, &lnum_edges_per_entry) == -1)
           {
           exerrval = ncerr;
           sprintf(errmsg,
             "Error: failed to get number of edges/%s in block %d in file id %d",
             tname,blk_id, exoid);
           ex_err("ex_get_block",errmsg, exerrval);
           return(EX_FATAL);
           }
         }
       *num_edges_per_entry = lnum_edges_per_entry;
       }
     }

   if ( num_faces_per_entry )
     {
     if ( blk_type != EX_ELEM_BLOCK )
       {
       exerrval = (EX_WARN);
       sprintf(errmsg,
         "Warning: non-NULL pointer passed to num_faces_per_entry for %s block query in file id %d",
         tname,exoid);
       ex_err("ex_get_block",errmsg,exerrval);
       }
     else
       {
       if ((dimid = ncdimid (exoid, dnumfac)) == -1)
         {
         /* undefined => no face entries per element */
         lnum_faces_per_entry = 0;
         }
       else
         {
         if (ncdiminq (exoid, dimid, (char *) 0, &lnum_faces_per_entry) == -1)
           {
           exerrval = ncerr;
           sprintf(errmsg,
             "Error: failed to get number of faces/%s in block %d in file id %d",
             tname,blk_id, exoid);
           ex_err("ex_get_block",errmsg, exerrval);
           return(EX_FATAL);
           }
         }
         *num_faces_per_entry = lnum_faces_per_entry;
       }
     }

   if ( num_attr_per_entry )
     {
     if ((dimid = ncdimid (exoid, dnumatt)) == -1)
       {
       /* dimension is undefined */
       *num_attr_per_entry = 0;
       }
     else
       {
       if (ncdiminq (exoid, dimid, (char *) 0, &lnum_attr_per_entry) == -1)
         {
         exerrval = ncerr;
         sprintf(errmsg,
           "Error: failed to get number of attributes in %s block %d in file id %d",
           tname,blk_id, exoid);
         ex_err("ex_get_block",errmsg, exerrval);
         return(EX_FATAL);
         }
       *num_attr_per_entry = lnum_attr_per_entry;
       }
     }

   if ( elem_type )
     {
     /* look up connectivity array for this element block id */
     if ((connid = ncvarid (exoid, vblkcon)) == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to locate connectivity array for element block %d in file id %d",
         blk_id,exoid);
       ex_err("ex_get_block",errmsg, exerrval);
       return(EX_FATAL);
       }

     if (ncattinq (exoid, connid, ablknam, &dummy, &len) == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to get element block %d type in file id %d",
         blk_id,exoid);
       ex_err("ex_get_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", 
         blk_id,len);
       ex_err("ex_get_block",errmsg,EX_MSG);
       }
     /* get the element type name */

     if (ncattget (exoid, connid, ablknam, elem_type) == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,"Error: failed to get element block %d type in file id %d",
         blk_id, exoid);
       ex_err("ex_get_block",errmsg, exerrval);
       return(EX_FATAL);
       }

     /* get rid of trailing blanks */
     ptr = elem_type;
     /* fprintf(stderr,"[exgblk] %s, len: %d\n",ptr,len); */
     while (ptr < elem_type + len && *ptr != ' ')
       {
       ptr++;
       }
     *(ptr) = '\0';
     }

   return (EX_NOERR);
}
Exemplo n.º 15
0
int ex_get_partial_elem_attr(int exoid, ex_entity_id elem_blk_id, int64_t start_elem_num,
                             int64_t num_elems, void *attrib)

{
  int    numelbdim, numattrdim, attrid, elem_blk_id_ndx, status;
  size_t num_elem_this_blk, num_attr, start[2], count[2];
  char   errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

  /* Determine index of elem_blk_id in VAR_ID_EL_BLK array */
  if ((elem_blk_id_ndx = ex_id_lkup(exoid, EX_ELEM_BLOCK, elem_blk_id)) < 0) {
    if (exerrval == EX_NULLENTITY) {
      snprintf(errmsg, MAX_ERR_LENGTH,
               "Warning: no attributes found for NULL block %" PRId64 " in file id %d", elem_blk_id,
               exoid);
      ex_err("ex_get_partial_elem_attr", errmsg, EX_NULLENTITY);
      return (EX_WARN); /* no attributes for this element block */
    }
    snprintf(errmsg, MAX_ERR_LENGTH,
             "Warning: failed to locate element block %" PRId64 " in %s array in file id %d",
             elem_blk_id, VAR_ID_EL_BLK, exoid);
    ex_err("ex_get_partial_elem_attr", errmsg, exerrval);
    return (EX_WARN);
  }

  /* inquire id's of previously defined dimensions  */

  if ((status = nc_inq_dimid(exoid, DIM_NUM_EL_IN_BLK(elem_blk_id_ndx), &numelbdim)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to locate number of elements for block %" PRId64 " in file id %d",
             elem_blk_id, exoid);
    ex_err("ex_get_partial_elem_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  if ((status = nc_inq_dimlen(exoid, numelbdim, &num_elem_this_blk)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to get number of elements for block %" PRId64 " in file id %d",
             elem_blk_id, exoid);
    ex_err("ex_get_partial_elem_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  if ((status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_BLK(elem_blk_id_ndx), &numattrdim)) !=
      NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "Warning: no attributes found for block %" PRId64 " in file id %d", elem_blk_id,
             exoid);
    ex_err("ex_get_partial_elem_attr", errmsg, EX_MSG);
    return (EX_WARN); /* no attributes for this element block */
  }

  if ((status = nc_inq_dimlen(exoid, numattrdim, &num_attr)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to get number of attributes for block %" PRId64 " in file id %d",
             elem_blk_id, exoid);
    ex_err("ex_get_partial_elem_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  if ((status = nc_inq_varid(exoid, VAR_ATTRIB(elem_blk_id_ndx), &attrid)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to locate attributes for block %" PRId64 " in file id %d", elem_blk_id,
             exoid);
    ex_err("ex_get_partial_elem_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* do some error checking */
  if (num_elem_this_blk < (start_elem_num + num_elems - 1)) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: requested attributes from too many elements in "
                                     "this block, %" PRId64 "",
             elem_blk_id);
    ex_err("ex_get_partial_elem_attr", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* read in the attributes */

  start[0] = --start_elem_num;
  start[1] = 0;

  count[0] = num_elems;
  count[1] = num_attr;

  if (ex_comp_ws(exoid) == 4) {
    status = nc_get_vara_float(exoid, attrid, start, count, attrib);
  }
  else {
    status = nc_get_vara_double(exoid, attrid, start, count, attrib);
  }

  if (status != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to get attributes for block %" PRId64 " in file id %d", elem_blk_id,
             exoid);
    ex_err("ex_get_partial_elem_attr", errmsg, exerrval);
    return (EX_FATAL);
  }
  return (EX_NOERR);
}
Exemplo n.º 16
0
/*!
 * writes the attribute names for a block
 * \param   exoid                   exodus file id
 * \param   blk_type                block type (edge, face, elem)
 * \param   blk_id                  block id
 * \param   names                   ptr to array of attribute names
 */
int ex_put_attr_names(int exoid, ex_entity_type blk_type, ex_entity_id blk_id, char *names[])
{
  int    varid, numattrdim, blk_id_ndx;
  size_t num_attr;

  int  status;
  char errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

  blk_id_ndx = ex_id_lkup(exoid, blk_type, blk_id);

  /* Determine index of blk_id in blk_id_ndx array */
  if (exerrval != 0) {
    if (exerrval == EX_NULLENTITY) {
      snprintf(errmsg, MAX_ERR_LENGTH,
               "Warning: no attributes allowed for NULL %s %" PRId64 " in file id %d",
               ex_name_of_object(blk_type), blk_id, exoid);
      ex_err("ex_put_attr_names", errmsg, EX_NULLENTITY);
      return (EX_WARN); /* no attributes for this block */
    }
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %s id %" PRId64 " in %s array in file id %d",
             ex_name_of_object(blk_type), blk_id, VAR_ID_EL_BLK, exoid);
    ex_err("ex_put_attr_names", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* inquire id's of previously defined dimensions  */
  switch (blk_type) {
  case EX_SIDE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_SS(blk_id_ndx), &numattrdim); break;
  case EX_NODE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_NS(blk_id_ndx), &numattrdim); break;
  case EX_EDGE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_ES(blk_id_ndx), &numattrdim); break;
  case EX_FACE_SET: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_FS(blk_id_ndx), &numattrdim); break;
  case EX_ELEM_SET:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_ELS(blk_id_ndx), &numattrdim);
    break;
  case EX_NODAL: status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_NBLK, &numattrdim); break;
  case EX_EDGE_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_EBLK(blk_id_ndx), &numattrdim);
    break;
  case EX_FACE_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_FBLK(blk_id_ndx), &numattrdim);
    break;
  case EX_ELEM_BLOCK:
    status = nc_inq_dimid(exoid, DIM_NUM_ATT_IN_BLK(blk_id_ndx), &numattrdim);
    break;
  default:
    exerrval = 1005;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "Internal ERROR: unrecognized object type in switch: %d in file id %d", blk_type,
             exoid);
    ex_err("ex_put_attr_names", errmsg, EX_MSG);
    return (EX_FATAL); /* number of attributes not defined */
  }

  if (status != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: number of attributes not defined for %s %" PRId64 " in file id %d",
             ex_name_of_object(blk_type), blk_id, exoid);
    ex_err("ex_put_attr_names", errmsg, EX_MSG);
    return (EX_FATAL); /* number of attributes not defined */
  }

  if ((status = nc_inq_dimlen(exoid, numattrdim, &num_attr)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to get number of attributes for %s %" PRId64 " in file id %d",
             ex_name_of_object(blk_type), blk_id, exoid);
    ex_err("ex_put_attr_names", errmsg, exerrval);
    return (EX_FATAL);
  }

  switch (blk_type) {
  case EX_SIDE_SET: status   = nc_inq_varid(exoid, VAR_NAME_SSATTRIB(blk_id_ndx), &varid); break;
  case EX_NODE_SET: status   = nc_inq_varid(exoid, VAR_NAME_NSATTRIB(blk_id_ndx), &varid); break;
  case EX_EDGE_SET: status   = nc_inq_varid(exoid, VAR_NAME_ESATTRIB(blk_id_ndx), &varid); break;
  case EX_FACE_SET: status   = nc_inq_varid(exoid, VAR_NAME_FSATTRIB(blk_id_ndx), &varid); break;
  case EX_ELEM_SET: status   = nc_inq_varid(exoid, VAR_NAME_ELSATTRIB(blk_id_ndx), &varid); break;
  case EX_NODAL: status      = nc_inq_varid(exoid, VAR_NAME_NATTRIB, &varid); break;
  case EX_EDGE_BLOCK: status = nc_inq_varid(exoid, VAR_NAME_EATTRIB(blk_id_ndx), &varid); break;
  case EX_FACE_BLOCK: status = nc_inq_varid(exoid, VAR_NAME_FATTRIB(blk_id_ndx), &varid); break;
  case EX_ELEM_BLOCK: status = nc_inq_varid(exoid, VAR_NAME_ATTRIB(blk_id_ndx), &varid); break;
  default:
    exerrval = 1005;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "Internal ERROR: unrecognized object type in switch: %d in file id %d", blk_type,
             exoid);
    ex_err("ex_put_attr_names", errmsg, EX_MSG);
    return (EX_FATAL); /* number of attributes not defined */
  }

  if (status != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to locate %s attribute names for %s %" PRId64 " in file id %d",
             ex_name_of_object(blk_type), ex_name_of_object(blk_type), blk_id, exoid);
    ex_err("ex_put_attr_names", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* write out the attributes  */
  status = ex_put_names_internal(exoid, varid, num_attr, names, blk_type, "attribute",
                                 "ex_put_attr_names");

  return (status);
}
Exemplo n.º 17
0
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);
}
Exemplo n.º 18
0
int ex_put_block( int         exoid,
                  ex_entity_type 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 status;
  int arbitrary_polyhedra = 0; /* 1 if block is arbitrary 2d polyhedra type; 2 if 3d polyhedra */
  int varid, dimid, dims[2], blk_id_ndx, blk_stat, strdim;
  size_t start[2];
  int num_blk;
  size_t temp;
  int cur_num_blk, numblkdim, numattrdim;
  int nnodperentdim, nedgperentdim, nfacperentdim;
  int connid;
  int npeid;
  char errmsg[MAX_ERR_LENGTH];
  char entity_type1[5];
  char entity_type2[5];
  const char* dnumblk = NULL;
  const char* vblkids = NULL;
  const char* vblksta = NULL;
  const char* vnodcon = NULL;
  const char* vnpecnt = NULL;
  const char* vedgcon = NULL;
  const char* vfaccon = NULL;
  const char* vconn   = NULL;
  const char* vattnam = NULL;
  const char* vblkatt = NULL;
  const char* dneblk  = NULL;
  const char* dnape   = NULL;
  const char* dnnpe   = NULL;
  const char* dnepe   = NULL;
  const char* dnfpe   = NULL;

  exerrval  = 0; /* clear error code */

  switch (blk_type) {
  case EX_EDGE_BLOCK:
    dnumblk = DIM_NUM_ED_BLK;
    vblkids = VAR_ID_ED_BLK;
    vblksta = VAR_STAT_ED_BLK;
    break;
  case EX_FACE_BLOCK:
    dnumblk = DIM_NUM_FA_BLK;
    vblkids = VAR_ID_FA_BLK;
    vblksta = VAR_STAT_FA_BLK;
    break;
  case EX_ELEM_BLOCK:
    dnumblk = DIM_NUM_EL_BLK;
    vblkids = VAR_ID_EL_BLK;
    vblksta = VAR_STAT_EL_BLK;
    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 ((status = ex_get_dimension(exoid, dnumblk, ex_name_of_object(blk_type),
                                 &temp, &dimid, "ex_put_block")) != NC_NOERR) {
    return EX_FATAL;
  }
  num_blk = (int)temp;
  
  /* 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 ((status = nc_inq_varid(exoid, vblkids, &varid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
            "Error: failed to locate %s ids in file id %d",
            ex_name_of_object(blk_type), exoid);
    ex_err("ex_put_block",errmsg,exerrval);
  }

  blk_id_ndx = ex_id_lkup(exoid,blk_type,blk_id);
  if (exerrval != EX_LOOKUPFAIL) {   /* found the element block id */
    exerrval = EX_FATAL;
    sprintf(errmsg,
            "Error: %s id %d already exists in file id %d",
            ex_name_of_object(blk_type), blk_id,exoid);
    ex_err("ex_put_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, ex_get_counter_list(blk_type));
  if (cur_num_blk >= num_blk) {
    exerrval = EX_FATAL;
    sprintf(errmsg,
            "Error: exceeded number of %ss (%d) defined in file id %d",
            ex_name_of_object(blk_type), num_blk,exoid);
    ex_err("ex_put_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, ex_get_counter_list(blk_type));
  start[0] = cur_num_blk;

  /* write out element block id to previously defined id array variable*/
  if ((status = nc_put_var1_int(exoid, varid, start, &blk_id)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
            "Error: failed to store %s id to file id %d",
            ex_name_of_object(blk_type), exoid);
    ex_err("ex_put_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  blk_id_ndx = ((int)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 ((status = nc_inq_varid (exoid, vblksta, &varid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
            "Error: failed to locate %s status in file id %d",
            ex_name_of_object(blk_type), exoid);
    ex_err("ex_put_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  if ((status = nc_put_var1_int(exoid, varid, start, &blk_stat)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
            "Error: failed to store %s id %d status to file id %d",
            ex_name_of_object(blk_type), blk_id, exoid);
    ex_err("ex_put_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  if (num_entries_this_blk == 0) {/* Is this a NULL element block? */
    return(EX_NOERR);
  }


  /* put netcdf file into define mode  */
  if ((status=nc_redef (exoid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid);
    ex_err("ex_put_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);
    vnpecnt = VAR_FBEPEC(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);
    vnpecnt = VAR_EBEPEC(blk_id_ndx);
    vedgcon = VAR_ECONN(blk_id_ndx);
    vfaccon = VAR_FCONN(blk_id_ndx);
    break;
  default:
    exerrval = 1005;
    sprintf(errmsg,
            "Internal Error: unrecognized block type in switch: %d in file id %d",
            blk_type,exoid);
    ex_err("ex_put_block",errmsg,EX_MSG);
    return (EX_FATAL);              /* number of attributes not defined */
  }
  /* define some dimensions and variables*/

  if ((status = nc_def_dim(exoid,dneblk,num_entries_this_blk, &numblkdim )) != NC_NOERR) {
    if (status == NC_ENAMEINUSE) {        /* duplicate entry */
      exerrval = status;
      sprintf(errmsg,
              "Error: %s %d already defined in file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
    } else {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to define number of entities/block for %s %d file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
    }
    goto error_ret;         /* exit define mode and return */
  }

  if ( dnnpe && num_nodes_per_entry > 0) {
    /* A nfaced block would not have any nodes defined... */
    if ((status = nc_def_dim(exoid,dnnpe,num_nodes_per_entry, &nnodperentdim)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to define number of nodes/entity for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  }

  if (dnepe && num_edges_per_entry > 0 ) {
    if ((status = nc_def_dim (exoid,dnepe,num_edges_per_entry, &nedgperentdim)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to define number of edges/entity for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  }

  if ( dnfpe && num_faces_per_entry > 0 ) {
    if ((status = nc_def_dim(exoid,dnfpe,num_faces_per_entry, &nfacperentdim)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to define number of faces/entity for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  }

  /* element attribute array */
  if (num_attr_per_entry > 0) {

    if ((status = nc_def_dim(exoid, dnape, num_attr_per_entry, &numattrdim)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to define number of attributes in %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

    dims[0] = numblkdim;
    dims[1] = numattrdim;

    if ((status = nc_def_var(exoid, vblkatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error:  failed to define attributes for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

    /* inquire previously defined dimensions  */
    if ((status = nc_inq_dimid(exoid, DIM_STR, &strdim)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to get string length in file id %d",exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      return (EX_FATAL);
    }
     
    /* Attribute names... */
    dims[0] = numattrdim;
    dims[1] = strdim;
            
    if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &varid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to define %s attribute name array in file id %d",
              ex_name_of_object(blk_type), exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  }

  /* See if storing an 'nsided' element block (arbitrary 2d polyhedra or super element) */
  if (strlen(entry_descrip) >= 3) {
    if ((entry_descrip[0] == 'n' || entry_descrip[0] == 'N') &&
        (entry_descrip[1] == 's' || entry_descrip[1] == 'S') &&
        (entry_descrip[2] == 'i' || entry_descrip[2] == 'I'))
      arbitrary_polyhedra = 1;
    else if ((entry_descrip[0] == 'n' || entry_descrip[0] == 'N') &&
             (entry_descrip[1] == 'f' || entry_descrip[1] == 'F') &&
             (entry_descrip[2] == 'a' || entry_descrip[2] == 'A'))
      /* If a FACE_BLOCK, then we are dealing with the faces of the nfaced block. */
      arbitrary_polyhedra = blk_type == EX_FACE_BLOCK ? 1 : 2;
  }

  /* element connectivity array */
  if (arbitrary_polyhedra > 0) {
    if (blk_type != EX_FACE_BLOCK && blk_type != EX_ELEM_BLOCK) {
      exerrval = EX_BADPARAM;
      sprintf( errmsg, "Error: Bad block type (%d) for nsided/nfaced block in file id %d",
               blk_type, exoid );
      ex_err( "ex_put_block", errmsg, exerrval );
      return (EX_FATAL);
    }

    if (arbitrary_polyhedra == 1) {
      dims[0] = nnodperentdim;
      vconn = vnodcon;

      /* store entity types as attribute of npeid variable -- node/elem, node/face, face/elem*/
      strcpy(entity_type1, "NODE");
      if (blk_type == EX_ELEM_BLOCK)
        strcpy(entity_type2, "ELEM");
      else
        strcpy(entity_type2, "FACE");
    } else if (arbitrary_polyhedra == 2) {
      dims[0] = nfacperentdim;
      vconn = vfaccon;

      /* store entity types as attribute of npeid variable -- node/elem, node/face, face/elem*/
      strcpy(entity_type1, "FACE");
      strcpy(entity_type2, "ELEM");
    }

    if ((status = nc_def_var(exoid, vconn, NC_INT, 1, dims, &connid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to create connectivity array for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
    
    /* element face-per-element or node-per-element count array */
    dims[0] = numblkdim;
    
    if ((status = nc_def_var(exoid, vnpecnt, NC_INT, 1, dims, &npeid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to create face- or node- per-entity count array for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id, exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

    if ((status = nc_put_att_text(exoid, npeid, "entity_type1", strlen(entity_type1)+1,
                                  entity_type1)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to store entity type attribute text for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id, exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
    if ((status = nc_put_att_text(exoid, npeid, "entity_type2", strlen(entity_type2)+1,
                                  entity_type2)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to store entity type attribute text for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id, exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  } else {
    /* "Normal" (non-polyhedra) element block type */
    dims[0] = numblkdim;
    dims[1] = nnodperentdim;
    
    if ((status = nc_def_var(exoid, vnodcon, NC_INT, 2, dims, &connid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
              "Error: failed to create connectivity array for %s %d in file id %d",
              ex_name_of_object(blk_type), blk_id,exoid);
      ex_err("ex_put_block",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  }
  /* store element type as attribute of connectivity variable */
  if ((status = nc_put_att_text(exoid, connid, ATT_NAME_ELB, strlen(entry_descrip)+1, 
                                entry_descrip)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
            "Error: failed to store %s type name %s in file id %d",
            ex_name_of_object(blk_type), entry_descrip,exoid);
    ex_err("ex_put_block",errmsg,exerrval);
    goto error_ret;         /* exit define mode and return */
  }

  if (arbitrary_polyhedra == 0) {
    if (vedgcon && num_edges_per_entry ) {
      dims[0] = numblkdim;
      dims[1] = nedgperentdim;
      
      if ((status = nc_def_var(exoid, vedgcon, NC_INT, 2, dims, &varid)) != NC_NOERR) {
        exerrval = status;
        sprintf(errmsg,
                "Error: failed to create edge connectivity array for %s %d in file id %d",
                ex_name_of_object(blk_type), blk_id,exoid);
        ex_err("ex_put_block",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }
    }
    
    if ( vfaccon && num_faces_per_entry ) {
      dims[0] = numblkdim;
      dims[1] = nfacperentdim;
      
      if ((status = nc_def_var(exoid, vfaccon, NC_INT, 2, dims, &varid)) != NC_NOERR) {
        exerrval = status;
        sprintf(errmsg,
                "Error: failed to create face connectivity array for %s %d in file id %d",
                ex_name_of_object(blk_type), blk_id,exoid);
        ex_err("ex_put_block",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }
    }
  }
  /* leave define mode  */

  if ((exerrval=nc_enddef (exoid)) != NC_NOERR) {
    sprintf(errmsg,
            "Error: failed to complete %s definition in file id %d", 
            ex_name_of_object(blk_type), exoid);
    ex_err("ex_put_block",errmsg,exerrval);
    return (EX_FATAL);
  }

  return (EX_NOERR);

  /* Fatal error: exit definition mode and return */
 error_ret:
  if (nc_enddef (exoid) != NC_NOERR) {    /* exit define mode */
    sprintf(errmsg,
            "Error: failed to complete definition for file id %d",
            exoid);
    ex_err("ex_put_block",errmsg,exerrval);
  }
  return (EX_FATAL);
}
Exemplo n.º 19
0
/*
 * reads the attribute names for an element block
 */
int ex_get_attr_names( int   exoid,
                       ex_entity_type obj_type,
                       ex_entity_id   obj_id,
                       char **names)
{
  int status;
  int varid, numattrdim, obj_id_ndx;
  size_t num_attr, i;
  char errmsg[MAX_ERR_LENGTH];
  const char* dnumobjatt;
  const char* vattrbname;

  exerrval = 0; /* clear error code */

  /* Determine index of obj_id in vobjids array */
  if (obj_type != EX_NODAL) {
    obj_id_ndx = ex_id_lkup(exoid,obj_type,obj_id);
    if (exerrval != 0) {
      if (exerrval == EX_NULLENTITY) {
	sprintf(errmsg,
		"Warning: no attributes found for NULL %s %"PRId64" in file id %d",
		ex_name_of_object(obj_type), obj_id, exoid);
	ex_err("ex_get_attr_names",errmsg,EX_NULLENTITY);
	return (EX_WARN);              /* no attributes for this object */
      } else {
	sprintf(errmsg,
		"Warning: failed to locate %s id %"PRId64" in id array in file id %d",
		ex_name_of_object(obj_type), obj_id, exoid);
	ex_err("ex_get_attr_names",errmsg,exerrval);
	return (EX_WARN);
      }
    }
  }

  switch (obj_type) {
  case EX_NODE_SET:
    dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
    vattrbname = VAR_NAME_NSATTRIB(obj_id_ndx);
    break;
  case EX_SIDE_SET:
    dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
    vattrbname = VAR_NAME_SSATTRIB(obj_id_ndx);
    break;
  case EX_EDGE_SET:
    dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
    vattrbname = VAR_NAME_ESATTRIB(obj_id_ndx);
    break;
  case EX_FACE_SET:
    dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
    vattrbname = VAR_NAME_FSATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_SET:
    dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
    vattrbname = VAR_NAME_ELSATTRIB(obj_id_ndx);
    break;
  case EX_NODAL:
    dnumobjatt = DIM_NUM_ATT_IN_NBLK;
    vattrbname = VAR_NAME_NATTRIB;
    break;
  case EX_EDGE_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
    vattrbname = VAR_NAME_EATTRIB(obj_id_ndx);
    break;
  case EX_FACE_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
    vattrbname = VAR_NAME_FATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
    vattrbname = VAR_NAME_ATTRIB(obj_id_ndx);
    break;
  default:
    exerrval = 1005;
    sprintf(errmsg,
	    "Internal Error: unrecognized object type in switch: %d in file id %d",
	    obj_type,exoid);
    ex_err("ex_get_attr_names",errmsg,EX_MSG);
    return (EX_FATAL);              /* number of attributes not defined */
  }
  /* inquire id's of previously defined dimensions  */

  if ((status = nc_inq_dimid(exoid, dnumobjatt, &numattrdim)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Warning: no attributes found for %s %"PRId64" in file id %d",
	    ex_name_of_object(obj_type),obj_id,exoid);
    ex_err("ex_get_attr_names",errmsg,EX_MSG);
    return (EX_WARN);              /* no attributes for this object */
  }

  if ((status = nc_inq_dimlen(exoid, numattrdim, &num_attr)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to get number of attributes for %s %"PRId64" in file id %d",
	    ex_name_of_object(obj_type),obj_id,exoid);
    ex_err("ex_get_attr_names",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* It is OK if we don't find the attribute names since they were
     added at version 4.26; earlier databases won't have the names.
  */
  status = nc_inq_varid(exoid, vattrbname, &varid);

  /* read in the attributes */

  if (status == NC_NOERR) {
    /* read the names */
    status = ex_get_names_internal(exoid, varid, num_attr, names, obj_type, "ex_get_attr_names");
    if (status != NC_NOERR) {
      return (EX_FATAL);
    }
  } else {
    /* Names variable does not exist on the database; probably since this is an
     * older version of the database.  Return an empty array...
     */
    for (i=0; i<num_attr; i++) {
      names[i][0] = '\0';
    }
  }
  return(EX_NOERR);
}
Exemplo n.º 20
0
int ex_put_elem_attr (int   exoid,
                      int   elem_blk_id,
                      const void *attrib)
{
   int numelbdim, numattrdim, attrid, elem_blk_id_ndx;
   long num_elem_this_blk, num_attr, start[2], count[2];
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

  /* Determine index of elem_blk_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)
    {
      sprintf(errmsg,
              "Warning: no attributes allowed for NULL block %d in file id %d",
              elem_blk_id,exoid);
      ex_err("ex_put_elem_attr",errmsg,EX_MSG);
      return (EX_WARN);              /* no attributes for this element block */
    }
    else
    {
      sprintf(errmsg,
             "Error: no element block id %d in %s array in file id %d",
              elem_blk_id, VAR_ID_EL_BLK, exoid);
      ex_err("ex_put_elem_attr",errmsg,exerrval);
      return (EX_FATAL);
    }
  }

/* inquire id's of previously defined dimensions  */

  if ((numelbdim = ncdimid (exoid, DIM_NUM_EL_IN_BLK(elem_blk_id_ndx))) == -1)
  {
    if (ncerr == NC_EBADDIM)
    {
      exerrval = ncerr;
      sprintf(errmsg,
         "Error: no element block with id %d in file id %d",
             elem_blk_id, exoid);
      ex_err("ex_put_elem_attr",errmsg,exerrval);
      return (EX_FATAL);
    }
    else
    {
      exerrval = ncerr;
      sprintf(errmsg,
        "Error: failed to locate number of elements for block %d in file id %d",
             elem_blk_id, exoid);
      ex_err("ex_put_elem_attr",errmsg,exerrval);
      return (EX_FATAL);
    }
  }


  if (ncdiminq (exoid, numelbdim, (char *) 0, &num_elem_this_blk) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to get number of elements for block %d in file id %d",
            elem_blk_id,exoid);
    ex_err("ex_put_elem_attr",errmsg,exerrval);
    return (EX_FATAL);
  }


  if ((numattrdim = ncdimid(exoid, DIM_NUM_ATT_IN_BLK(elem_blk_id_ndx))) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: number of attributes not defined for block %d in file id %d",
            elem_blk_id,exoid);
    ex_err("ex_put_elem_attr",errmsg,EX_MSG);
    return (EX_FATAL);              /* number of attributes not defined */
  }

  if (ncdiminq (exoid, numattrdim, (char *) 0, &num_attr) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
         "Error: failed to get number of attributes for block %d in file id %d",
            elem_blk_id,exoid);
    ex_err("ex_put_elem_attr",errmsg,exerrval);
    return (EX_FATAL);
  }

  if ((attrid = ncvarid (exoid, VAR_ATTRIB(elem_blk_id_ndx))) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
        "Error: failed to locate attribute variable for block %d in file id %d",
            elem_blk_id,exoid);
    ex_err("ex_put_elem_attr",errmsg,exerrval);
    return (EX_FATAL);
  }


/* write out the attributes  */

  start[0] = 0;
  start[1] = 0;

  count[0] = num_elem_this_blk;
  count[1] = num_attr;

  if (ncvarput (exoid, attrid, start, count,
                ex_conv_array(exoid,WRITE_CONVERT,attrib,
                (int)num_attr*num_elem_this_blk)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
            "Error: failed to put attributes for block %d in file id %d",
            elem_blk_id,exoid);
    ex_err("ex_put_elem_attr",errmsg,exerrval);
    return (EX_FATAL);
  }


  return(EX_NOERR);

}
int ex_get_attr_param (int   exoid,
		       ex_entity_type obj_type,
		       ex_entity_id   obj_id,
		       int*  num_attrs)
{
  int status;
  int dimid;
  
  char errmsg[MAX_ERR_LENGTH];
  const char *dnumobjatt;

  int obj_id_ndx;
  size_t lnum_attr_per_entry;
  
  /* Determine index of obj_id in vobjids array */
  if (obj_type == EX_NODAL) {
    obj_id_ndx = 0;
  } else {
    obj_id_ndx = ex_id_lkup(exoid,obj_type,obj_id);
    
    if (exerrval != 0) {
      if (exerrval == EX_NULLENTITY) {
	*num_attrs = 0;
	return (EX_NOERR);
      } 
	sprintf(errmsg,
		"Warning: failed to locate %s id %"PRId64" in id array in file id %d",
		ex_name_of_object(obj_type),obj_id,exoid);
	ex_err("ex_get_attr_param",errmsg,exerrval);
	return (EX_WARN);
      
    }
  }

  switch (obj_type) {
  case EX_SIDE_SET:
    dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
    break;
  case EX_NODE_SET:
    dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
    break;
  case EX_EDGE_SET:
    dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
    break;
  case EX_FACE_SET:
    dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
    break;
  case EX_ELEM_SET:
    dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
    break;
  case EX_NODAL:
    dnumobjatt = DIM_NUM_ATT_IN_NBLK;
    break;
  case EX_EDGE_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
    break;
  case EX_FACE_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
    break;
  case EX_ELEM_BLOCK:
    dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
    break;
  default:
    exerrval = EX_BADPARAM;
    sprintf(errmsg, "Error: Bad block type (%d) specified for file id %d",
	    obj_type, exoid );
    ex_err("ex_get_attr_param",errmsg,exerrval);
    return (EX_FATAL);
  }

  exerrval = 0; /* clear error code */

  if ((status = nc_inq_dimid(exoid, dnumobjatt, &dimid)) != NC_NOERR) {
    /* dimension is undefined */
    *num_attrs = 0;
  } else {
    if ((status = nc_inq_dimlen(exoid, dimid, &lnum_attr_per_entry)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to get number of attributes in %s %"PRId64" in file id %d",
	      ex_name_of_object(obj_type),obj_id, exoid);
      ex_err("ex_get_attr_param",errmsg, exerrval);
      return(EX_FATAL);
    }
    *num_attrs = lnum_attr_per_entry;
  }
  return (EX_NOERR);
}
Exemplo n.º 22
0
int ex_put_attr_param (int   exoid,
		       ex_entity_type obj_type,
		       int   obj_id,
		       int   num_attrs)
{
  int status;
  int dims[2];
  int strdim, varid;
  
  char errmsg[MAX_ERR_LENGTH];
  const char *dnumobjent;
  const char *dnumobjatt;
  const char *vobjatt;
  const char *vattnam;
  int numobjentdim;
  int obj_id_ndx;
  int numattrdim;
  
  /* Determine index of obj_id in obj_type id array */
  if (obj_type == EX_NODAL)
    obj_id_ndx = 0;
  else {
    obj_id_ndx = ex_id_lkup(exoid,obj_type,obj_id);
    
    if (exerrval != 0) {
      if (exerrval == EX_NULLENTITY) {
	sprintf(errmsg,
		"Warning: no attributes found for NULL %s %d in file id %d",
		ex_name_of_object(obj_type),obj_id,exoid);
	ex_err("ex_put_attr_param",errmsg,EX_MSG);
	return (EX_WARN);              /* no attributes for this object */
      } else {
	sprintf(errmsg,
		"Warning: failed to locate %s id %d in id array in file id %d",
		ex_name_of_object(obj_type),obj_id, exoid);
	ex_err("ex_put_attr_param",errmsg,exerrval);
	return (EX_WARN);
      }
    }
  }

  switch (obj_type) {
  case EX_SIDE_SET:
    dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
    vobjatt = VAR_SSATTRIB(obj_id_ndx);
    vattnam = VAR_NAME_SSATTRIB(obj_id_ndx);
    break;
  case EX_NODE_SET:
    dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
    vobjatt = VAR_NSATTRIB(obj_id_ndx);
    vattnam = VAR_NAME_NSATTRIB(obj_id_ndx);
    break;
  case EX_EDGE_SET:
    dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
    vobjatt = VAR_ESATTRIB(obj_id_ndx);
    vattnam = VAR_NAME_ESATTRIB(obj_id_ndx);
    break;
  case EX_FACE_SET:
    dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
    vobjatt = VAR_FSATTRIB(obj_id_ndx);
    vattnam = VAR_NAME_FSATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_SET:
    dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
    vobjatt = VAR_ELSATTRIB(obj_id_ndx);
    vattnam = VAR_NAME_ELSATTRIB(obj_id_ndx);
    break;
  case EX_NODAL:
    dnumobjent = DIM_NUM_NODES;
    dnumobjatt = DIM_NUM_ATT_IN_NBLK;
    vobjatt = VAR_NATTRIB;
    vattnam = VAR_NAME_NATTRIB;
    break;
  case EX_EDGE_BLOCK:
    dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
    vobjatt = VAR_EATTRIB(obj_id_ndx);
    vattnam = VAR_NAME_EATTRIB(obj_id_ndx);
    break;
  case EX_FACE_BLOCK:
    dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
    vobjatt = VAR_FATTRIB(obj_id_ndx);
    vattnam = VAR_NAME_FATTRIB(obj_id_ndx);
    break;
  case EX_ELEM_BLOCK:
    dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
    dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
    vobjatt = VAR_ATTRIB(obj_id_ndx);
    vattnam = VAR_NAME_ATTRIB(obj_id_ndx);
    break;
  default:
    exerrval = EX_BADPARAM;
    sprintf(errmsg, "Error: Bad block type (%d) specified for file id %d",
	    obj_type, exoid );
    ex_err("ex_put_attr_param",errmsg,exerrval);
    return (EX_FATAL);
  }

  exerrval = 0; /* clear error code */

  if ((status = nc_inq_dimid(exoid, dnumobjent, &numobjentdim)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate number of entries for %s %d in file id %d",
	    ex_name_of_object(obj_type), obj_id, exoid);
    ex_err("ex_put_attr_param",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* put netcdf file into define mode  */
  if ((status = nc_redef (exoid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid);
    ex_err("ex_put_attr_param",errmsg,exerrval);
    return (EX_FATAL);
  }
  
  if ((status = nc_def_dim(exoid, dnumobjatt, num_attrs, &numattrdim)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to define number of attributes in %s %d in file id %d",
	    ex_name_of_object(obj_type), obj_id,exoid);
    ex_err("ex_put_attr_param",errmsg,exerrval);
    goto error_ret;         /* exit define mode and return */
  }

  dims[0] = numobjentdim;
  dims[1] = numattrdim;
  
  if ((status = nc_def_var(exoid, vobjatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error:  failed to define attributes for %s %d in file id %d",
	    ex_name_of_object(obj_type), obj_id,exoid);
    ex_err("ex_put_attr_param",errmsg,exerrval);
    goto error_ret;         /* exit define mode and return */
  }
  
  /* inquire previously defined dimensions  */
  if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to get string length in file id %d",exoid);
    ex_err("ex_put_attr_param",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* Attribute names... */
  dims[0] = numattrdim;
  dims[1] = strdim;

  if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &varid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to define %s attribute name array in file id %d",
	    ex_name_of_object(obj_type), exoid);
    ex_err("ex_put_attr_param",errmsg,exerrval);
    goto error_ret;         /* exit define mode and return */
  }

  /* leave define mode  */
  if ((status = nc_enddef(exoid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to complete %s attribute parameter definition in file id %d",
	    ex_name_of_object(obj_type), exoid);
    ex_err("ex_put_attr_param",errmsg,exerrval);
    return (EX_FATAL);
  }

  return (EX_NOERR);

  /* Fatal error: exit definition mode and return */
 error_ret:
  if (nc_enddef (exoid) != NC_NOERR) {     /* exit define mode */       
    sprintf(errmsg,
	    "Error: failed to complete definition for file id %d",
	    exoid);
    ex_err("ex_put_attr_param",errmsg,exerrval);
  }
  return (EX_FATAL);
}