Beispiel #1
0
int ex_get_var_time( int   exoid,
                     int   var_type,
                     int   var_index,
                     int   id,
                     int   beg_time_step, 
                     int   end_time_step,
                     void* var_vals )
{
  int i, dimid, varid, numel = 0, offset;
  nclong *obj_ids, *stat_vals;
  long num_obj, num_entries_this_obj = 0, start[2], count[2];
  float fdum;
  char *cdum;
  char errmsg[MAX_ERR_LENGTH];
  const char* tname;
  const char* dimnumobj;
  const char* varobjids;
  const char* varobstat;

  switch (var_type) {
  case EX_GLOBAL:
    return ex_get_glob_var_time( exoid, var_index, beg_time_step, end_time_step, var_vals );
  case EX_NODAL:
    return ex_get_nodal_var_time( exoid, var_index, id, beg_time_step, end_time_step, var_vals );
  case EX_EDGE_BLOCK:
    tname = "edge block";
    dimnumobj =  DIM_NUM_ED_BLK;
    varobjids =   VAR_ID_ED_BLK; 
    varobstat = VAR_STAT_ED_BLK;
    break;
  case EX_FACE_BLOCK:
    tname = "face block";
    dimnumobj =  DIM_NUM_FA_BLK;
    varobjids =   VAR_ID_FA_BLK; 
    varobstat = VAR_STAT_FA_BLK;
    break;
  case EX_ELEM_BLOCK:
    tname = "element block";
    dimnumobj =  DIM_NUM_EL_BLK;
    varobjids =   VAR_ID_EL_BLK; 
    varobstat = VAR_STAT_EL_BLK;
    break;
  case EX_NODE_SET:
    tname = "node set";
    dimnumobj =  DIM_NUM_NSET_VAR;
    varobjids =      VAR_NS_IDS; 
    varobstat =      VAR_NS_STAT;
    break;
  case EX_EDGE_SET:
    tname = "edge set";
    dimnumobj =  DIM_NUM_ESET_VAR;
    varobjids =      VAR_ES_IDS;
    varobstat =      VAR_ES_STAT;
    break;
  case EX_FACE_SET:
    tname = "face set";
    dimnumobj =  DIM_NUM_FSET_VAR;
    varobjids =      VAR_FS_IDS; 
    varobstat =      VAR_FS_STAT;
    break;
  case EX_SIDE_SET:
    tname = "side set";
    dimnumobj =  DIM_NUM_SSET_VAR;
    varobjids =      VAR_SS_IDS; 
    varobstat =      VAR_SS_STAT;
    break;
  case EX_ELEM_SET:
    tname = "element set";
    dimnumobj =  DIM_NUM_ELSET_VAR;
    varobjids =      VAR_ELS_IDS; 
    varobstat =      VAR_ELS_STAT;
    break;
  default:
    exerrval = EX_BADPARAM;
    sprintf( errmsg, "Error: Invalid variable type (%d) specified for file id %d", var_type, exoid );
    ex_err( "ex_get_var_time", errmsg, exerrval );
    return (EX_FATAL);
  }

  exerrval = 0; /* clear error code */

  cdum = 0; /* initialize even though it is not used */

  /* assume entry number is 1-based (the first entry of an object is 1, not 0);
   * adjust so it is 0-based
   */
  id--;

  /* find what object the entry is in */

  /* first, find out how many objects there are */

  if ((dimid = ncdimid (exoid, dimnumobj)) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to locate number of %ss in file id %d", 
              tname,exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      return (EX_FATAL);
    }

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

  /* get the array of object ids */
  /* don't think we need this anymore since the netcdf variable names 
     associated with objects don't contain the object ids */

  if (!(obj_ids = malloc(num_obj*sizeof(nclong))))
    {
      exerrval = EX_MEMFAIL;
      sprintf(errmsg,
              "Error: failed to allocate memory for %s ids for file id %d",
              tname,exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      return (EX_FATAL);
    }


  if ((varid = ncvarid (exoid, varobjids)) == -1)
    {
      exerrval = ncerr;
      free(obj_ids);
      sprintf(errmsg,
              "Error: failed to locate %s ids in file id %d", tname,exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      return (EX_FATAL);
    }


  start[0] = 0;
  count[0] = num_obj;
  if (ncvarget (exoid, varid, start, count, obj_ids) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to get %s ids from file id %d", tname,exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      return (EX_FATAL);
    }

  /* allocate space for stat array */
  if (!(stat_vals = malloc((int)num_obj*sizeof(nclong))))
    {
      exerrval = EX_MEMFAIL;
      free (obj_ids);
      sprintf(errmsg,
              "Error: failed to allocate memory for %s status array for file id %d",
              tname,exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      return (EX_FATAL);
    }

  /* get variable id of status array */
  if ((varid = ncvarid (exoid, varobstat)) != -1)
    {
      /* if status array exists, use it, otherwise assume, object exists
         to be backward compatible */

      start[0] = 0;
      start[1] = 0;
      count[0] = num_obj;
      count[1] = 0;

      if (ncvarget (exoid, varid, start, count, (void *)stat_vals) == -1)
        {
          exerrval = ncerr;
          free (obj_ids);
          free(stat_vals);
          sprintf(errmsg,
                  "Error: failed to get %s status array from file id %d",
                  tname,exoid);
          ex_err("ex_get_var_time",errmsg,exerrval);
          return (EX_FATAL);
        }
    }
  else /* default: status is true */
    for(i=0;i<num_obj;i++)
      stat_vals[i]=1;


  /* loop through each object until id is found;  since entry
   * numbers are sequential (beginning with 1) id is in obj_i
   * when id_first_i <= id <= id_last_i, where
   * id_first_i is the entry number of the first entry in 
   * obj_i and id_last_i is the entry number of the last
   * entry in obj_i
   */

  i = 0;
  if (stat_vals[i] != 0)  {
    if ((dimid = ncdimid (exoid, ex_dim_num_entries_in_object(var_type,i+1))) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to locate number of entries in %s %d in file id %d",
              tname, obj_ids[i], exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      free(stat_vals);
      free(obj_ids);
      return (EX_FATAL);
    }

    if (ncdiminq (exoid, dimid, (char *) 0, &num_entries_this_obj) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to get number of entries in %s %d in file id %d",
              tname, obj_ids[i], exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      free(stat_vals);
      free(obj_ids);
      return (EX_FATAL);
    }

  } /* End NULL object check */

  numel = num_entries_this_obj;

  while (numel <= id) {
    if (stat_vals[++i] != 0) {
      if ((dimid = ncdimid(exoid,ex_dim_num_entries_in_object(var_type,i+1))) == -1) {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to locate number of entries in %s %d in file id %d",
                tname, obj_ids[i], exoid);
        ex_err("ex_get_var_time",errmsg,exerrval);
        free(stat_vals);
        free(obj_ids);
        return (EX_FATAL);
      }

      if (ncdiminq (exoid, dimid, (char *) 0, &num_entries_this_obj) == -1) {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to get number of entries in %s %d in file id %d",
                tname, obj_ids[i], exoid);
        ex_err("ex_get_var_time",errmsg,exerrval);
        free(stat_vals);
        free(obj_ids);
        return (EX_FATAL);
      }

      numel += num_entries_this_obj;
    }
  }

  offset = id - (numel - num_entries_this_obj);

  /* inquire previously defined variable */

  if((varid=ncvarid(exoid,ex_name_var_of_object(var_type,var_index,i+1))) == -1) {
    exerrval = ncerr;
    sprintf(errmsg,
            "Error: failed to locate variable %d for %s %d in file id %d",
            var_index,tname,obj_ids[i],exoid);
    ex_err("ex_get_var_time",errmsg,exerrval);
    free(stat_vals);
    free(obj_ids);
    return (EX_FATAL);
  }

  free(stat_vals);
  free(obj_ids);

  /* read values of object variable */

  start[0] = --beg_time_step;
  start[1] = offset;

  if (end_time_step < 0) {

    /* user is requesting the maximum time step;  we find this out using the
     * database inquire function to get the number of time steps;  the ending
     * time step number is 1 less due to 0 based array indexing in C
     */
    if (ex_inquire (exoid, EX_INQ_TIME, &end_time_step, &fdum, cdum) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to get maximum time step in file id %d",
              exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      return (EX_FATAL);
    }
  }

  end_time_step--;

  count[0] = end_time_step - beg_time_step + 1;
  count[1] = 1;

  if (ncvarget (exoid, varid, start, count,
                ex_conv_array(exoid,RTN_ADDRESS,var_vals,count[0])) == -1) {
    exerrval = ncerr;
    sprintf(errmsg,
            "Error: failed to get %s variable values in file id %d", tname,exoid);
    ex_err("ex_get_var_time",errmsg,exerrval);
    return (EX_FATAL);
  }

  ex_conv_array( exoid, READ_CONVERT, var_vals, count[0] );

  return (EX_NOERR);
}
Beispiel #2
0
int ex_get_var(int exoid, int time_step, ex_entity_type var_type, int var_index,
               ex_entity_id obj_id, int64_t num_entry_this_obj, void *var_vals)
{
  int    status;
  int    varid, obj_id_ndx;
  size_t start[2], count[2];
  char   errmsg[MAX_ERR_LENGTH];

  ex_check_valid_file_id(exoid);

  if (var_type == EX_NODAL) {
    /* FIXME: Special case: ignore obj_id, possible large_file complications,
     * etc. */
    return ex_get_nodal_var_int(exoid, time_step, var_index, num_entry_this_obj, var_vals);
  }
  if (var_type == EX_GLOBAL) {
    /* FIXME: Special case: all vars stored in 2-D single array. */
    return ex_get_glob_vars_int(exoid, time_step, num_entry_this_obj, var_vals);
  }

  exerrval = 0; /* clear error code */

  /* Determine index of obj_id in VAR_ID_EL_BLK array */
  obj_id_ndx = ex_id_lkup(exoid, var_type, obj_id);
  if (exerrval != 0) {
    if (exerrval == EX_NULLENTITY) {
      snprintf(errmsg, MAX_ERR_LENGTH,
               "Warning: no %s variables for NULL block %" PRId64 " in file id %d",
               ex_name_of_object(var_type), obj_id, exoid);
      ex_err("ex_get_var", errmsg, EX_NULLENTITY);
      return (EX_WARN);
    }
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to locate %s id %" PRId64 " in id variable in file id %d",
             ex_name_of_object(var_type), obj_id, exoid);
    ex_err("ex_get_var", errmsg, exerrval);
    return (EX_FATAL);
  }

  /* inquire previously defined variable */

  if ((status = nc_inq_varid(exoid, ex_name_var_of_object(var_type, var_index, obj_id_ndx),
                             &varid)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s %" PRId64 " var %d in file id %d",
             ex_name_of_object(var_type), obj_id, var_index, exoid);
    ex_err("ex_get_var", errmsg, exerrval);
    return (EX_FATAL);
  }

/* Verify that time_step is within bounds */
#ifndef NDEBUG
  {
    int num_time_steps = ex_inquire_int(exoid, EX_INQ_TIME);
    if (time_step <= 0 || time_step > num_time_steps) {
      snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: time_step is out-of-range. Value = %d, valid "
                                       "range is 1 to %d in file id %d",
               time_step, num_time_steps, exoid);
      ex_err("ex_get_var", errmsg, EX_BADPARAM);
      return (EX_FATAL);
    }
  }
#endif

  /* read values of element variable */
  start[0] = --time_step;
  start[1] = 0;

  count[0] = 1;
  count[1] = num_entry_this_obj;

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

  if (status != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH,
             "ERROR: failed to get %s %" PRId64 " variable %d in file id %d",
             ex_name_of_object(var_type), obj_id, var_index, exoid);
    ex_err("ex_get_var", errmsg, exerrval);
    return (EX_FATAL);
  }
  return (EX_NOERR);
}
static int define_truth_table(ex_entity_type obj_type, int exoid, int num_ent, int num_var,
                              int *var_tab, int *status_tab, void_int *ids, const char *label)
{
  char errmsg[MAX_ERR_LENGTH];
  int  k = 0;
  int  i, j;
  int  time_dim;
  int  dims[2];
  int  varid;
  int  status;

  if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) {
    exerrval = status;
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time dimension in file id %d", exoid);
    ex_err("ex_put_all_var_param_ext", errmsg, exerrval);
    return -1;
  }

  if (var_tab == NULL) {
    exerrval = EX_BADPARAM;
    snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s variable truth table is NULL in file id %d", label,
             exoid);
    ex_err("ex_put_all_var_param_ext", errmsg, exerrval);
    return -1;
  }

  for (i = 0; i < num_ent; i++) {
    int64_t id;
    if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
      id = ((int64_t *)ids)[i];
    }
    else {
      id = ((int *)ids)[i];
    }

    for (j = 1; j <= num_var; j++) {

      /* check if variables are to be put out for this block */
      if (var_tab[k] != 0) {
        if (status_tab[i] != 0) { /* only define variable if active */
          dims[0] = time_dim;

          /* Determine number of entities in entity */
          /* Need way to make this more generic... */
          status = nc_inq_dimid(exoid, ex_dim_num_entries_in_object(obj_type, i + 1), &dims[1]);
          if (status != NC_NOERR) {
            exerrval = status;
            snprintf(errmsg, MAX_ERR_LENGTH,
                     "ERROR: failed to locate number of entities in %s %" PRId64 " in file id %d",
                     label, id, exoid);
            ex_err("ex_put_all_var_param_ext", errmsg, exerrval);
            return status;
          }

          /* define netCDF variable to store variable values;
           * the j index cycles from 1 through the number of variables so
           * that the index of the EXODUS variable (which is part of
           * the name of the netCDF variable) will begin at 1 instead of 0
           */
          status = nc_def_var(exoid, ex_name_var_of_object(obj_type, j, i + 1), nc_flt_code(exoid),
                              2, dims, &varid);
          if (status != NC_NOERR) {
            if (status != NC_ENAMEINUSE) {
              exerrval = status;
              snprintf(errmsg, MAX_ERR_LENGTH,
                       "ERROR: failed to define %s variable for %s %" PRId64 " in file id %d",
                       label, label, id, exoid);
              ex_err("ex_put_all_var_param_ext", errmsg, exerrval);
              return status;
            }
          }
          ex_compress_variable(exoid, varid, 2);
        }
      }    /* if */
      k++; /* increment truth table pointer */
    }      /* for j */
  }        /* for i */
  return NC_NOERR;
}
Beispiel #4
0
static int
ex_look_up_var(int exoid, ex_entity_type var_type, int var_index, ex_entity_id obj_id, 
	       const char *VOBJID, const char *VOBJTAB, const char *DNUMOBJ,
	       const char *DNUMOBJVAR, int *varid)
{
  int status;
  int obj_id_ndx;
  int dimid,time_dim, numobjdim, dims[2];

  size_t num_obj;
  size_t num_obj_var;
  size_t num_entity;

  int *obj_var_truth_tab;
  char errmsg[MAX_ERR_LENGTH];

  /* Determine index of obj_id in VOBJID array */
  obj_id_ndx = ex_id_lkup(exoid,var_type,obj_id);
  if (exerrval != 0) {
    if (exerrval == EX_NULLENTITY) {
      sprintf(errmsg,
	      "Warning: no variables allowed for NULL block %"PRId64" in file id %d",
	      obj_id,exoid);
      ex_err("ex_put_var",errmsg,EX_MSG);
      return (EX_WARN);
    }
    else {
      sprintf(errmsg,
	      "Error: failed to locate %s id %"PRId64" in %s array in file id %d",
	      ex_name_of_object(var_type), obj_id, VOBJID, exoid);
      ex_err("ex_put_var",errmsg,exerrval);
      return (EX_FATAL);
    }
  }

  if ((status = nc_inq_varid (exoid, ex_name_var_of_object(var_type, var_index, obj_id_ndx), varid)) != NC_NOERR) {
    if (status == NC_ENOTVAR) {/* variable doesn't exist, create it! */
      /* check for the existance of an TNAME variable truth table */
      if (nc_inq_varid(exoid, VOBJTAB, varid) == NC_NOERR) {
	/* find out number of TNAMEs and TNAME variables */
	status = ex_get_dimension(exoid, DNUMOBJ, ex_name_of_object(var_type), &num_obj, &dimid, "ex_put_var");
	if (status != NC_NOERR) return status;

	status = ex_get_dimension(exoid, DNUMOBJVAR, ex_name_of_object(var_type), &num_obj_var, &dimid, "ex_put_var");
	if (status != NC_NOERR) return status;

	if (!(obj_var_truth_tab = malloc(num_obj*num_obj_var*sizeof(int)))) {
	  exerrval = EX_MEMFAIL;
	  sprintf(errmsg,
		  "Error: failed to allocate memory for %s variable truth table in file id %d",
		  ex_name_of_object(var_type), exoid);
	  ex_err("ex_put_var",errmsg,exerrval);
	  return (EX_FATAL);
	}

	/*   read in the TNAME variable truth table */
	if ((status = nc_get_var_int (exoid, *varid, obj_var_truth_tab)) != NC_NOERR) {
	  exerrval = status;
	  sprintf(errmsg,
		  "Error: failed to get truth table from file id %d", exoid);
	  ex_err("ex_put_var",errmsg,exerrval);
	  return (EX_FATAL);
	}

	if(obj_var_truth_tab[num_obj_var*(obj_id_ndx-1)+var_index-1] == 0L) {
	  free(obj_var_truth_tab);
	  exerrval = EX_BADPARAM;
	  sprintf(errmsg,
		  "Error: Invalid %s variable %d, %s %"PRId64" in file id %d",
		  ex_name_of_object(var_type), var_index, ex_name_of_object(var_type), obj_id, exoid);
	  ex_err("ex_put_var",errmsg,exerrval);
	  return (EX_FATAL);
	}
	free(obj_var_truth_tab);
      }

      if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to locate time dimension in file id %d", exoid);
	ex_err("ex_put_var",errmsg,exerrval);
	goto error_ret;         /* exit define mode and return */
      }

      ex_get_dimension(exoid, ex_dim_num_entries_in_object(var_type, obj_id_ndx),
		       ex_name_of_object(var_type), &num_entity, &numobjdim,  "ex_put_var");

      /*    variable doesn't exist so put file into define mode  */
      if ((status = nc_redef (exoid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to put file id %d into define mode", exoid);
	ex_err("ex_put_var",errmsg,exerrval);
	return (EX_FATAL);
      }

      /* define netCDF variable to store TNAME variable values */
      dims[0] = time_dim;
      dims[1] = numobjdim;
      if ((status = nc_def_var(exoid, ex_name_var_of_object(var_type, var_index, obj_id_ndx),
			       nc_flt_code(exoid), 2, dims, varid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to define %s variable %d in file id %d",
		ex_name_of_object(var_type), var_index,exoid);
	ex_err("ex_put_var",errmsg,exerrval);
	goto error_ret;
      }
      ex_compress_variable(exoid, *varid, 2);

      /*    leave define mode  */
      if ((status = nc_enddef (exoid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to complete %s variable %s definition to file id %d",
		ex_name_of_object(var_type), ex_name_var_of_object(var_type, var_index, obj_id_ndx), exoid);
	ex_err("ex_put_var",errmsg,exerrval);
	return (EX_FATAL);
      }
    }
    else {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to locate %s variable %s in file id %d",
	      ex_name_of_object(var_type), ex_name_var_of_object(var_type, var_index, obj_id_ndx), exoid);
      ex_err("ex_put_var",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_var",errmsg,exerrval);
  }
  return (EX_FATAL);
}
Beispiel #5
0
int ex_get_partial_var( int   exoid,
		  int   time_step,
		  ex_entity_type var_type,
		  int   var_index,
		  ex_entity_id   obj_id, 
		  int64_t   start_index,
		  int64_t   num_entities,
		  void* var_vals )
{
  int status = 0;
  int varid, obj_id_ndx;
  size_t start[2], count[2];
  char errmsg[MAX_ERR_LENGTH];

  if (num_entities == 0)
    return status;
  
  if (var_type == EX_NODAL) {
    /* FIXME: Special case: ignore obj_id, possible large_file complications, etc. */
    return ex_get_partial_nodal_var( exoid, time_step, var_index, start_index, num_entities, var_vals );
  } else if (var_type == EX_GLOBAL) {
    /* FIXME: Special case: all vars stored in 2-D single array. */
    return ex_get_glob_vars( exoid, time_step, num_entities, var_vals );
  }
   
  exerrval = 0; /* clear error code */

  /* Determine index of obj_id in VAR_ID_EL_BLK array */
  obj_id_ndx = ex_id_lkup(exoid,var_type,obj_id);
  if (exerrval != 0) {
    if (exerrval == EX_NULLENTITY) {
      sprintf(errmsg,
	      "Warning: no %s variables for NULL block %"PRId64" in file id %d",
	      ex_name_of_object(var_type), obj_id,exoid);
      ex_err("ex_get_partial_var",errmsg,EX_NULLENTITY);
      return (EX_WARN);
    } else {
      sprintf(errmsg,
	      "Error: failed to locate %s id %"PRId64" in id variable in file id %d",
	      ex_name_of_object(var_type), obj_id, exoid);
      ex_err("ex_get_partial_var",errmsg,exerrval);
      return (EX_FATAL);
    }
  }

  /* inquire previously defined variable */

  if((status = nc_inq_varid(exoid, ex_name_var_of_object(var_type,var_index,
							 obj_id_ndx), &varid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate %s %"PRId64" var %d in file id %d",
	    ex_name_of_object(var_type),obj_id,var_index,exoid); 
    ex_err("ex_get_partial_var",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* read values of element variable */
  start[0] = --time_step;
  start[1] = start_index-1;

  count[0] = 1;
  count[1] = num_entities;

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

  if (status != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to get %s %"PRId64" variable %d in file id %d", 
	    ex_name_of_object(var_type), obj_id, var_index,exoid);
    ex_err("ex_get_partial_var",errmsg,exerrval);
    return (EX_FATAL);
  }
  return (EX_NOERR);
}
Beispiel #6
0
int ex_get_var_time( int   exoid,
                     ex_entity_type var_type,
                     int   var_index,
                     int64_t   id,
                     int   beg_time_step, 
                     int   end_time_step,
                     void* var_vals )
{
  int dimid, varid, numel = 0, offset;
  int status;
  int *stat_vals;
  size_t num_obj, i;
  size_t num_entries_this_obj = 0;
  size_t start[2], count[2];
  char errmsg[MAX_ERR_LENGTH];
  const char* varobjids;
  const char* varobstat;

  switch (var_type) {
  case EX_GLOBAL:
    return ex_get_glob_var_time( exoid, var_index, beg_time_step, end_time_step, var_vals );
  case EX_NODAL:
    return ex_get_nodal_var_time( exoid, var_index, id, beg_time_step, end_time_step, var_vals );
  case EX_EDGE_BLOCK:
    varobjids =   VAR_ID_ED_BLK; 
    varobstat = VAR_STAT_ED_BLK;
    break;
  case EX_FACE_BLOCK:
    varobjids =   VAR_ID_FA_BLK; 
    varobstat = VAR_STAT_FA_BLK;
    break;
  case EX_ELEM_BLOCK:
    varobjids =   VAR_ID_EL_BLK; 
    varobstat = VAR_STAT_EL_BLK;
    break;
  case EX_NODE_SET:
    varobjids =      VAR_NS_IDS; 
    varobstat =      VAR_NS_STAT;
    break;
  case EX_EDGE_SET:
    varobjids =      VAR_ES_IDS;
    varobstat =      VAR_ES_STAT;
    break;
  case EX_FACE_SET:
    varobjids =      VAR_FS_IDS; 
    varobstat =      VAR_FS_STAT;
    break;
  case EX_SIDE_SET:
    varobjids =      VAR_SS_IDS; 
    varobstat =      VAR_SS_STAT;
    break;
  case EX_ELEM_SET:
    varobjids =      VAR_ELS_IDS; 
    varobstat =      VAR_ELS_STAT;
    break;
  default:
    exerrval = EX_BADPARAM;
    sprintf( errmsg, "Error: Invalid variable type (%d) specified for file id %d", var_type, exoid );
    ex_err( "ex_get_var_time", errmsg, exerrval );
    return (EX_FATAL);
  }

  exerrval = 0; /* clear error code */

  /* assume entry number is 1-based (the first entry of an object is 1, not 0);
   * adjust so it is 0-based
   */
  id--;

  /* find what object the entry is in */

  /* first, find out how many objects there are */
  status = ex_get_dimension(exoid, ex_dim_num_objects(var_type), ex_name_of_object(var_type),
			    &num_obj, &dimid, "ex_get_var_time");
  if (status != NC_NOERR) return status;

  /* get the array of object ids */
  /* don't think we need this anymore since the netcdf variable names 
     associated with objects don't contain the object ids */

  if ((status = nc_inq_varid (exoid, varobjids, &varid )) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate %s ids in file id %d",
	    ex_name_of_object(var_type),exoid);
    ex_err("ex_get_var_time",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* allocate space for stat array */
  if (!(stat_vals = malloc((int)num_obj*sizeof(int)))) {
    exerrval = EX_MEMFAIL;
    sprintf(errmsg,
	    "Error: failed to allocate memory for %s status array for file id %d",
	    ex_name_of_object(var_type),exoid);
    ex_err("ex_get_var_time",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* get variable id of status array */
  if (nc_inq_varid (exoid, varobstat, &varid) == NC_NOERR) {
    /* if status array exists, use it, otherwise assume, object exists
       to be backward compatible */

    if ((status = nc_get_var_int(exoid, varid, stat_vals)) != NC_NOERR) {
      exerrval = status;
      free(stat_vals);
      sprintf(errmsg,
	      "Error: failed to get %s status array from file id %d",
	      ex_name_of_object(var_type),exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      return (EX_FATAL);
    }
  }
  else { /* default: status is true */
    for(i=0;i<num_obj;i++)
      stat_vals[i]=1;
  }

  /* loop through each object until id is found;  since entry
   * numbers are sequential (beginning with 1) id is in obj_i
   * when id_first_i <= id <= id_last_i, where
   * id_first_i is the entry number of the first entry in 
   * obj_i and id_last_i is the entry number of the last
   * entry in obj_i
   */

  i = 0;
  if (stat_vals[i] != 0)  {
    if ((status = nc_inq_dimid(exoid, ex_dim_num_entries_in_object(var_type,i+1), &dimid)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to locate number of entries in %"ST_ZU"th %s in file id %d",
	      i, ex_name_of_object(var_type), exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      free(stat_vals);
      return (EX_FATAL);
    }

    if ((status = nc_inq_dimlen(exoid, dimid, &num_entries_this_obj)) != NC_NOERR) {
      exerrval = status;
      sprintf(errmsg,
	      "Error: failed to get number of entries in %"ST_ZU"th %s in file id %d",
	      i, ex_name_of_object(var_type), exoid);
      ex_err("ex_get_var_time",errmsg,exerrval);
      free(stat_vals);
      return (EX_FATAL);
    }
  } /* End NULL object check */

  numel = num_entries_this_obj;

  while (numel <= id) {
    if (stat_vals[++i] != 0) {
      if ((status = nc_inq_dimid(exoid,ex_dim_num_entries_in_object(var_type,i+1), &dimid)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to locate number of entries in %"ST_ZU"th %s in file id %d",
		i, ex_name_of_object(var_type), exoid);
	ex_err("ex_get_var_time",errmsg,exerrval);
	free(stat_vals);
	return (EX_FATAL);
      }

      if ((status = nc_inq_dimlen(exoid, dimid, &num_entries_this_obj)) != NC_NOERR) {
	exerrval = status;
	sprintf(errmsg,
		"Error: failed to get number of entries in %"ST_ZU"th %s in file id %d",
		i, ex_name_of_object(var_type), exoid);
	ex_err("ex_get_var_time",errmsg,exerrval);
	free(stat_vals);
	return (EX_FATAL);
      }
      numel += num_entries_this_obj;
    }
  }
  offset = id - (numel - num_entries_this_obj);

  /* inquire previously defined variable */
  if ((status = nc_inq_varid(exoid,ex_name_var_of_object(var_type,var_index,i+1), &varid)) != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to locate variable %"ST_ZU" for %dth %s in file id %d",
	    i, var_index,ex_name_of_object(var_type),exoid);
    ex_err("ex_get_var_time",errmsg,exerrval);
    free(stat_vals);
    return (EX_FATAL);
  }

  free(stat_vals);

  /* read values of object variable */
  start[0] = --beg_time_step;
  start[1] = offset;

  if (end_time_step < 0) {
    /* user is requesting the maximum time step;  we find this out using the
     * database inquire function to get the number of time steps;  the ending
     * time step number is 1 less due to 0 based array indexing in C
     */
    end_time_step = ex_inquire_int (exoid, EX_INQ_TIME);
  }

  end_time_step--;

  count[0] = end_time_step - beg_time_step + 1;
  count[1] = 1;

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

  if (status != NC_NOERR) {
    exerrval = status;
    sprintf(errmsg,
	    "Error: failed to get %s variable values in file id %d",
	    ex_name_of_object(var_type),exoid);
    ex_err("ex_get_var_time",errmsg,exerrval);
    return (EX_FATAL);
  }
  return (EX_NOERR);
}