Exemplo n.º 1
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : CopyOthers
@INPUT      : ParentCDF  - CDF ID of the parent file
              ChildCDF   - CDF ID of the child file
              NumExclude - number of variables to exclude from the copy
              Exclude    - list of variable ID's to be excluded
              TimeStamp  - line to add to the history attribute
@OUTPUT     : 
@RETURNS    : TRUE if successfull
              FALSE if any of micopy_all_var_defs(), ncendef(), 
                 or mi_copy_all_var_values() indicate failure
@DESCRIPTION: Copies the definitions and values of all variables except 
              those in the exclusion list.  Also calls UpdateHistory() to
              update the history line.  The child file should be in 
              definition mode when CopyOthers() is called; it will be
	      ncendef()'d (put in update mode) before variable values are
	      copied, and left that way on exit.
@METHOD     : 
@GLOBALS    : 
@CALLS      : UpdateHistory
@CREATED    : fall 1993, Greg Ward
@MODIFIED   : 
---------------------------------------------------------------------------- */
Boolean CopyOthers (int ParentCDF, int ChildCDF, 
		    int NumExclude, int Exclude[],
		    char *TimeStamp)
{
#ifdef DEBUG
   printf ("CopyOthers:\n");
   printf (" copying variable definitions...\n");
#endif

   if (micopy_all_var_defs(ParentCDF, ChildCDF, NumExclude, Exclude) == MI_ERROR)
   {
      sprintf (ErrMsg, "Error copying variable definitions: %s", 
	       NCErrMsg (ncerr, errno));
      ncclose (ChildCDF);
      return (FALSE);
   }

#ifdef DEBUG
   printf (" updating history...\n");
#endif 

   UpdateHistory (ChildCDF, TimeStamp);

#ifdef DEBUG
   printf (" ncendef'ing and copying variable values...\n");
#endif
   if (ncendef (ChildCDF) == MI_ERROR)
   {
      sprintf (ErrMsg, "Error updating file (ncendef): %s",
	       NCErrMsg (ncerr, errno));
      ncclose (ChildCDF);
      return (FALSE);
   }

   if (micopy_all_var_values(ParentCDF, ChildCDF, NumExclude, Exclude) == MI_ERROR)
   {
      sprintf (ErrMsg, "Error copying variable values: %s", 
	       NCErrMsg (ncerr, errno));
      ncclose (ChildCDF);
      return (FALSE);
   }

   return (TRUE);

}     /* CopyOthers () */
Exemplo n.º 2
0
void FileNetcdf::writeReferenceTime() {
   if(!hasVar("forecast_reference_time")) {
      int id;
      int status = ncredef(mFile);
      handleNetcdfError(status, "could not put into define mode");
      status = nc_def_var(mFile, "forecast_reference_time", NC_DOUBLE, 0, NULL, &id);

      handleNetcdfError(status, "writing reference time");

      status = ncendef(mFile);
      handleNetcdfError(status, "could not put into data mode");
   }
   int vTime = getVar("forecast_reference_time");
   double referenceTime = getReferenceTime();
   if(!Util::isValid(referenceTime))
      referenceTime = NC_FILL_DOUBLE;
   int status = nc_put_var_double(mFile, vTime, &referenceTime);
   handleNetcdfError(status, "could not write reference time");
   setAttribute(vTime, "standard_name", "forecast_reference_time");
   setAttribute(vTime, "units", "seconds since 1970-01-01 00:00:00 +00:00");
}
Exemplo n.º 3
0
void FileNetcdf::writeTimes() {
   std::vector<double> times = getTimes();
   if(times.size() != getNumTime()) {
      std::stringstream ss;
      ss << "The times specified for NetCDF file '" << getFilename() << "' has " << times.size()
         << " elements, but the time dimension is " << getNumTime() << ". Putting missing values.";
      Util::warning(ss.str());
      times = std::vector<double>(getNumTime(), Util::MV);
   }

   // Convert missing
   for(int i = 0; i < times.size(); i++) {
      if(!Util::isValid(times[i]))
         times[i] = NC_FILL_FLOAT;
   }
   if(!hasVar("time")) {
      int dTime  = getDim("time");
      int id;
      int status = ncredef(mFile);
      handleNetcdfError(status, "could not put into define mode");
      status = nc_def_var(mFile, "time", NC_DOUBLE, 1, &dTime, &id);

      handleNetcdfError(status, "creating time variable");

      status = ncendef(mFile);
      handleNetcdfError(status, "could not put into data mode");
   }
   int vTime = getVar("time");
   double timesArr[getNumTime()];
   for(int t = 0; t < getNumTime(); t++) {
      timesArr[t] = times[t];
   }
   int status = nc_put_var_double(mFile, vTime, timesArr);
   handleNetcdfError(status, "could not write times");
   setAttribute(vTime, "long_name", "time");
   setAttribute(vTime, "standard_name", "time");
   setAttribute(vTime, "units", "seconds since 1970-01-01 00:00:00 +00:00");
}
Exemplo n.º 4
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.º 5
0
int ex_put_num_map ( int exoid,
                     int map_type,
                     int map_id,
                     const int *map )
{
   int dimid, varid, iresult;
   long start[1]; 
   nclong ldum, *lptr;
   long num_maps, num_entries, count[1];
   int cur_num_maps;
   char *cdum;
   char errmsg[MAX_ERR_LENGTH];
   const char* tname;
   const char* dnumentries;
   const char* dnummaps;
   const char* vmapids;
   const char* vmap = 0;
   struct list_item** map_ctr_list;

   exerrval = 0; /* clear error code */

   cdum = 0;

   switch ( map_type ) {
   case EX_NODE_MAP:
     tname = "node";
     dnumentries = DIM_NUM_NODES;
     dnummaps = DIM_NUM_NM;
     vmapids = VAR_NM_PROP(1);
     map_ctr_list = &nm_ctr_list;
     break;
   case EX_EDGE_MAP:
     tname = "edge";
     dnumentries = DIM_NUM_EDGE;
     dnummaps = DIM_NUM_EDM;
     vmapids = VAR_EDM_PROP(1);
     map_ctr_list = &edm_ctr_list;
     break;
   case EX_FACE_MAP:
     tname = "face";
     dnumentries = DIM_NUM_FACE;
     dnummaps = DIM_NUM_FAM;
     vmapids = VAR_FAM_PROP(1);
     map_ctr_list = &fam_ctr_list;
     break;
   case EX_ELEM_MAP:
     tname = "element";
     dnumentries = DIM_NUM_ELEM;
     dnummaps = DIM_NUM_EM;
     vmapids = VAR_EM_PROP(1);
     map_ctr_list = &em_ctr_list;
     break;
   default:
     exerrval = EX_BADPARAM;
     sprintf( errmsg,
       "Error: Bad map type (%d) specified for file id %d",
       map_type, exoid );
     ex_err( "ex_put_num_map", errmsg, exerrval );
     return (EX_FATAL);
   }

/* Make sure the file contains entries */
   if ((dimid = (ncdimid (exoid, dnumentries))) == -1 )
   {
     return (EX_NOERR);
   }

/* first check if any maps are specified */

   if ((dimid = (ncdimid (exoid, dnummaps))) == -1 )
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: no %s maps specified in file id %d",
             tname,exoid);
     ex_err("ex_put_num_map",errmsg,exerrval);
     return (EX_FATAL);
   }

/* Check for duplicate map id entry */
   ex_id_lkup(exoid,vmapids,map_id); 
   if (exerrval != EX_LOOKUPFAIL)   /* found the map id */
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: %s map %d already defined in file id %d",
             tname,map_id,exoid);
     ex_err("ex_put_num_map",errmsg,exerrval);
     return(EX_FATAL);
   }

/* Get number of maps initialized for this file */
   if ((ncdiminq (exoid,dimid,cdum,&num_maps)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to get number of %s maps in file id %d",
             tname,exoid);
     ex_err("ex_put_num_map",errmsg,exerrval);
     return (EX_FATAL);
   }

/* Keep track of the total number of maps defined using a counter stored
   in a linked list keyed by exoid.
   NOTE: ex_get_file_item  is used to find the number of maps
         for a specific file and returns that value.
*/
   cur_num_maps = ex_get_file_item(exoid, map_ctr_list );
   if (cur_num_maps >= num_maps)
   {
     exerrval = EX_FATAL;
     sprintf(errmsg,
          "Error: exceeded number of %s maps (%ld) specified in file id %d",
             tname,num_maps,exoid);
     ex_err("ex_put_num_map",errmsg,exerrval);
     return (EX_FATAL);
   }

/*   NOTE: ex_inc_file_item  is used to find the number of maps
         for a specific file and returns that value incremented. */

   cur_num_maps = ex_inc_file_item(exoid, map_ctr_list );

/* write out information to previously defined variable */

   /* first get id of variable */

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

   /* then, write out map id */

   start[0] = cur_num_maps;

   ldum = (nclong)map_id;
   if (ncvarput1 (exoid, varid, start, &ldum) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to store %s map id %d in file id %d",
             tname,map_id,exoid);
     ex_err("ex_put_num_map",errmsg,exerrval);
     return (EX_FATAL);
   }

/* determine number of entries */

   if ((dimid = (ncdimid (exoid, dnumentries))) == -1 )
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: couldn't determine number of %s entries in file id %d",
             tname,exoid);
     ex_err("ex_put_num_map",errmsg,exerrval);
     return (EX_FATAL);
   }

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

   switch ( map_type ) {
   case EX_NODE_MAP:
     vmap = VAR_NODE_MAP(cur_num_maps+1);
     break;
   case EX_EDGE_MAP:
     vmap = VAR_EDGE_MAP(cur_num_maps+1);
     break;
   case EX_FACE_MAP:
     vmap = VAR_FACE_MAP(cur_num_maps+1);
     break;
   case EX_ELEM_MAP:
     vmap = VAR_ELEM_MAP(cur_num_maps+1);
     break;
   }

/* locate variable array in which to store the map */
   if ((varid = ncvarid(exoid,vmap)) == -1)
     {
#if 0
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to locate %s map %d in file id %d",
               vmap,map_id,exoid);
       ex_err("ex_put_num_map",errmsg,exerrval);
       return (EX_FATAL);
#endif
       int dims[2];
       ncerr = 0;

       if ( ncredef( exoid ) == -1 ) {
         exerrval = ncerr;
         sprintf(errmsg, "Error: failed to place file id %d into define mode", exoid);
         ex_err("ex_put_num_map",errmsg,exerrval);
         return (EX_FATAL);
       }

       dims[0] = dimid;
       if ( (varid = ncvardef( exoid, vmap, NC_LONG, 1, dims )) == -1 ) {
         exerrval = ncerr;
         sprintf(errmsg, "Error: failed to define map %s in file id %d", vmap, exoid);
         ex_err("ex_put_num_map",errmsg,exerrval);
       }

       if ( ncendef( exoid ) == -1 ) { /* exit define mode */
         sprintf( errmsg, "Error: failed to complete definition for file id %d", exoid );
         ex_err( "ex_put_num_map", errmsg, exerrval );
         varid = -1; /* force early exit */
       }

       if ( varid == -1 ) /* we couldn't define variable and have prepared error message. */
         return (EX_FATAL);
     }

/* write out the map  */

/* this contortion is necessary because netCDF is expecting nclongs; fortunately
   it's necessary only when ints and nclongs aren't the same size */

   start[0] = 0;
   count[0] = num_entries;

   if (sizeof(int) == sizeof(nclong)) {
      iresult = ncvarput (exoid, varid, start, count, map);
   } else {
      lptr = itol (map, (int)num_entries);
      iresult = ncvarput (exoid, varid, start, count, lptr);
      free(lptr);
   }

   if (iresult == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to store %s map in file id %d",
             tname,exoid);
     ex_err("ex_put_num_map",errmsg,exerrval);
     return (EX_FATAL);
   }

   return (EX_NOERR);
}
Exemplo n.º 6
0
void
mexFunction	(
	INT			nlhs,
	Matrix	*	plhs[],
	INT			nrhs,
	const Matrix	*	prhs[]
	)

{
	char		*	opname;
	OPCODE			opcode;
	
	Matrix		*	mat;
	
	int				status;
	char		*	path;
	int				cmode;
	int				mode;
	int				cdfid;
	int				ndims;
	int				nvars;
	int				natts;
	int				recdim;
	char		*	name;
	long			length;
	int				dimid;
	nc_type			datatype;
	int			*	dim;
	int				varid;
	long		*	coords;
	VOIDP			value;
	long		*	start;
	long		*	count;
	int			*	intcount;
	long		*	stride;
	long		*	imap;
	long			recnum;
	int				nrecvars;
	int			*	recvarids;
	long		*	recsizes;
	VOIDPP			datap;		/*	pointers for record access.	*/
	int				len;
	int				incdf;
	int				invar;
	int				outcdf;
	int				outvar;
	int				attnum;
	char		*	attname;
	char		*	newname;
	int				fillmode;
	
	int				i;
	int				m;
	int				n;
	char		*	p;
	char			buffer[MAX_BUFFER];
	
	DOUBLE		*	pr;
	DOUBLE			addoffset;
	DOUBLE			scalefactor;
	int				autoscale;		/*	do auto-scaling if this flag is non-zero.	*/
	
	/*	Disable the NC_FATAL option from ncopts.	*/
	
	if (ncopts & NC_FATAL)	{
		ncopts -= NC_FATAL;
	}
	
	/*	Display usage if less than one input argument.	*/
	
	if (nrhs < 1)	{
	
		Usage();
		
		return;
	}
	
	/*	Convert the operation name to its opcode.	*/
	
	opname = Mat2Str(prhs[0]);
	for (i = 0; i < strlen(opname); i++)	{
		opname[i] = (char) tolower((int) opname[i]);
	}
	p = opname;
	if (strncmp(p, "nc", 2) == 0)	{	/*	Trim away "nc".	*/
		p += 2;
	}
	
	i = 0;
	opcode = NONE;
	while (ops[i].opcode != NONE)	{
		if (!strcmp(p, ops[i].opname))	{
			opcode = ops[i].opcode;
			if (ops[i].nrhs > nrhs)	{
				mexPrintf("MEXCDF: opname = %s\n", opname);
				mexErrMsgTxt("MEXCDF: Too few input arguments.\n");
			}
			else if (0 && ops[i].nlhs > nlhs)	{	/*	Disabled.	*/
				mexPrintf("MEXCDF: opname = %s\n", opname);
				mexErrMsgTxt("MEXCDF: Too few output arguments.\n");
			}
			break;
		}
		else	{
			i++;
		}
	}
	
	if (opcode == NONE)	{
		mexPrintf("MEXCDF: opname = %s\n", opname);
		mexErrMsgTxt("MEXCDF: No such operation.\n");
	}
	
	Free((VOIDPP) & opname);
	
	/*	Extract the cdfid by number.	*/
	
	switch (opcode)	{
	
	case USAGE:
	case CREATE:
	case OPEN:
	case TYPELEN:
	case SETOPTS:
	case ERR:
	case PARAMETER:
	
		break;
	
	default:

		cdfid = Scalar2Int(prhs[1]);
	
		break;
	}
	
	/*	Extract the dimid by number or name.	*/
	
	switch (opcode)	{

	case DIMINQ:
	case DIMRENAME:
	
		if (mxIsNumeric(prhs[2]))	{
			dimid = Scalar2Int(prhs[2]);
		}
		else	{
			name = Mat2Str(prhs[2]);
			dimid = ncdimid(cdfid, name);
			Free((VOIDPP) & name);
		}
		break;
	
	default:
	
		break;
	}
	
	/*	Extract the varid by number or name.	*/
	
	switch (opcode)	{

	case VARINQ:
	case VARPUT1:
	case VARGET1:
	case VARPUT:
	case VARGET:
	case VARPUTG:
	case VARGETG:
	case VARRENAME:
	case VARCOPY:
	case ATTPUT:
	case ATTINQ:
	case ATTGET:
	case ATTCOPY:
	case ATTNAME:
	case ATTRENAME:
	case ATTDEL:
	
		if (mxIsNumeric(prhs[2]))	{
			varid = Scalar2Int(prhs[2]);
		}
		else	{
			name = Mat2Str(prhs[2]);
			varid = ncvarid(cdfid, name);
			Free((VOIDPP) & name);
			if (varid == -1)	{
				varid = Parameter(prhs[2]);
			}
		}
		break;
	
	default:
	
		break;
	}
	
	/*	Extract the attname by name or number.	*/
	
	switch (opcode)	{
	
	case ATTPUT:
	case ATTINQ:
	case ATTGET:
	case ATTCOPY:
	case ATTRENAME:
	case ATTDEL:
	
		if (mxIsNumeric(prhs[3]))	{
			attnum = Scalar2Int(prhs[3]);
			attname = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
			status = ncattname(cdfid, varid, attnum, attname);
		}
		else	{
			attname = Mat2Str(prhs[3]);
		}
		break;
	
	default:
	
		break;
	}
	
	/*	Extract the "add_offset" and "scale_factor" attributes.	*/
	
	switch (opcode)	{
	
	case VARPUT1:
	case VARGET1:
	case VARPUT:
	case VARGET:
	case VARPUTG:
	case VARGETG:

		addoffset = Add_Offset(cdfid, varid);
		scalefactor = Scale_Factor(cdfid, varid);
		if (scalefactor == 0.0)	{
			scalefactor = 1.0;
		}
		
		break;
	
	default:
	
		break;
	}
	
	/*	Perform the NetCDF operation.	*/
	
	switch (opcode)	{
		
	case USAGE:
	
		Usage();
		
		break;
	
	case CREATE:
		
		path = Mat2Str(prhs[1]);
		
		if (nrhs > 2)	{
			cmode = Parameter(prhs[2]);
		}
		else	{
			cmode = NC_NOCLOBBER;	/*	Default.	*/
		}
		
		cdfid = nccreate(path, cmode);
		
		plhs[0] = Int2Scalar(cdfid);
		plhs[1] = Int2Scalar((cdfid >= 0) ? 0 : -1);
		
		Free((VOIDPP) & path);
		
		break;
		
	case OPEN:
		
		path = Mat2Str(prhs[1]);
		
		if (nrhs > 2)	{
			mode = Parameter(prhs[2]);
		}
		else	{
			mode = NC_NOWRITE;	/*	Default.	*/
		}
		
		cdfid = ncopen(path, mode);
		
		plhs[0] = Int2Scalar(cdfid);
		plhs[1] = Int2Scalar((cdfid >= 0) ? 0 : -1);
		
		Free((VOIDPP) & path);
		
		break;
		
	case REDEF:
		
		status = ncredef(cdfid);
		
		plhs[0] = Int2Scalar(status);
		
		break;
		
	case ENDEF:
		
		status = ncendef(cdfid);
		
		plhs[0] = Int2Scalar(status);
		
		break;
		
	case CLOSE:
		
		status = ncclose(cdfid);
		
		plhs[0] = Int2Scalar(status);
		
		break;
		
	case INQUIRE:
	
		status = ncinquire(cdfid, & ndims, & nvars, & natts, & recdim);
		
		if (nlhs > 1)	{
			plhs[0] = Int2Scalar(ndims);
			plhs[1] = Int2Scalar(nvars);
			plhs[2] = Int2Scalar(natts);
			plhs[3] = Int2Scalar(recdim);
			plhs[4] = Int2Scalar(status);
		}
		else	{	/*	Default to 1 x 5 row vector.	*/
			plhs[0] = mxCreateFull(1, 5, REAL);
			pr = mxGetPr(plhs[0]);
			if (status == 0)	{
				pr[0] = (DOUBLE) ndims;
				pr[1] = (DOUBLE) nvars;
				pr[2] = (DOUBLE) natts;
				pr[3] = (DOUBLE) recdim;
			}
			pr[4] = (DOUBLE) status;
		}
		
		break;
		
	case SYNC:
	
		status = ncsync(cdfid);
		
		plhs[0] = Int2Scalar(status);
		
		break;
		
	case ABORT:
	
		status = ncabort(cdfid);
		
		plhs[0] = Int2Scalar(status);
		
		break;
		
	case DIMDEF:
	
		name = Mat2Str(prhs[2]);
		length = Parameter(prhs[3]);
		
		dimid = ncdimdef(cdfid, name, length);
		
		plhs[0] = Int2Scalar(dimid);
		plhs[1] = Int2Scalar((dimid >= 0) ? 0 : dimid);
		
		Free((VOIDPP) & name);
		
		break;
		
	case DIMID:
	
		name = Mat2Str(prhs[2]);
		
		dimid = ncdimid(cdfid, name);
		
		plhs[0] = Int2Scalar(dimid);
		plhs[1] = Int2Scalar((dimid >= 0) ? 0 : dimid);
		
		Free((VOIDPP) & name);
		
		break;
		
	case DIMINQ:
		
		name = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		
		status = ncdiminq(cdfid, dimid, name, & length);
		
		plhs[0] = Str2Mat(name);
		plhs[1] = Long2Scalar(length);
		plhs[2] = Int2Scalar(status);
		
		Free((VOIDPP) & name);
		
		break;
		
	case DIMRENAME:
		
		name = Mat2Str(prhs[3]);
		
		status = ncdimrename(cdfid, dimid, name);
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & name);
		
		break;
		
	case VARDEF:
	
		name = Mat2Str(prhs[2]);
		datatype = (nc_type) Parameter(prhs[3]);
		ndims = Scalar2Int(prhs[4]);
		if (ndims == -1)	{
			ndims = Count(prhs[5]);
		}
		dim = Mat2Int(prhs[5]);
		
		varid = ncvardef(cdfid, name, datatype, ndims, dim);
		
		Free((VOIDPP) & name);
		
		plhs[0] = Int2Scalar(varid);
		plhs[1] = Int2Scalar((varid >= 0) ? 0 : varid);
		
		break;
		
	case VARID:
	
		name = Mat2Str(prhs[2]);
		
		varid = ncvarid(cdfid, name);
		
		Free((VOIDPP) & name);
		
		plhs[0] = Int2Scalar(varid);
		plhs[1] = Int2Scalar((varid >= 0) ? 0 : varid);
		
		break;
		
	case VARINQ:
	
		name = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		dim = (int *) mxCalloc(MAX_VAR_DIMS, sizeof(int));
		
		status = ncvarinq(cdfid, varid, name, & datatype, & ndims, dim, & natts);
		
		datatype = RepairBadDataType(datatype);
		
		plhs[0] = Str2Mat(name);
		plhs[1] = Int2Scalar(datatype);
		plhs[2] = Int2Scalar(ndims);
		plhs[3] = Int2Mat(dim, 1, ndims);
		plhs[4] = Int2Scalar(natts);
		plhs[5] = Int2Scalar(status);
		
		Free((VOIDPP) & name);
		Free((VOIDPP) & dim);
		
		break;
		
	case VARPUT1:
		
		coords = Mat2Long(prhs[3]);
		
		name = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		dim = (int *) mxCalloc(MAX_NC_DIMS, sizeof(int));
		
		status = ncvarinq(cdfid, varid, name, & datatype, & ndims, dim, & natts);
		
		datatype = RepairBadDataType(datatype);
		
		Free((VOIDPP) & name);
		Free((VOIDPP) & dim);
		
		if (datatype == NC_CHAR)	{
			mat = SetNum(prhs[4]);
		}
		else	{
			mat = prhs[4];
		}
		if (mat == NULL)	{
			mat = prhs[4];
		}
		
		pr = mxGetPr(mat);
		
		autoscale = (nrhs > 5 && Scalar2Int(prhs[5]) != 0);
		
		if (!autoscale)	{
			scalefactor = 1.0;
			addoffset = 0.0;
		}
		
		status = Convert(opcode, datatype, 1, buffer, scalefactor, addoffset, pr);
		status = ncvarput1(cdfid, varid, coords, buffer);
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & coords);
		
		break;
		
	case VARGET1:
		
		coords = Mat2Long(prhs[3]);
		
		autoscale = (nrhs > 4 && Scalar2Int(prhs[4]) != 0);
		
		if (!autoscale)	{
			scalefactor = 1.0;
			addoffset = 0.0;
		}
		
		name = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		dim = (int *) mxCalloc(MAX_NC_DIMS, sizeof(int));
		
		status = ncvarinq(cdfid, varid, name, & datatype, & ndims, dim, & natts);
		
		datatype = RepairBadDataType(datatype);
		
		Free((VOIDPP) & name);
		Free((VOIDPP) & dim);
		
		mat = Int2Scalar(0);
		
		pr = mxGetPr(mat);
		
		status = ncvarget1(cdfid, varid, coords, buffer);
		status = Convert(opcode, datatype, 1, buffer, scalefactor, addoffset, pr);
		
		if (datatype == NC_CHAR)	{
			plhs[0] = SetStr(mat);
		}
		else	{
			plhs[0] = mat;
		}
		if (plhs[0] == NULL)	{
/*			prhs[0] = mat;		*/
			plhs[0] = mat;		/*	ZYDECO 24Jan2000	*/
		}
		
		plhs[1] = Int2Scalar(status);
		
		Free((VOIDPP) & coords);
		
		break;
		
	case VARPUT:
		
		start = Mat2Long(prhs[3]);
		count = Mat2Long(prhs[4]);
		
		autoscale = (nrhs > 6 && Scalar2Int(prhs[6]) != 0);
		
		if (!autoscale)	{
			scalefactor = 1.0;
			addoffset = 0.0;
		}
		
		name = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		dim = (int *) mxCalloc(MAX_NC_DIMS, sizeof(int));
		
		status = ncvarinq(cdfid, varid, name, & datatype, & ndims, dim, & natts);
		
		datatype = RepairBadDataType(datatype);
		
		if (datatype == NC_CHAR)	{
			mat = SetNum(prhs[5]);
		}
		else	{
			mat = prhs[5];
		}
		if (mat == NULL)	{
			mat = prhs[5];
		}
		
		pr = mxGetPr(mat);
		
		for (i = 0; i < ndims; i++)	{
			if (count[i] == -1)	{
				status = ncdiminq(cdfid, dim[i], name, & count[i]);
				count[i] -= start[i];
			}
		}
		
		Free((VOIDPP) & name);
		Free((VOIDPP) & dim);
		
		len = 0;
		if (ndims > 0)	{
			len = 1;
			for (i = 0; i < ndims; i++)	{
				len *= count[i];
			}
		}
		
		value = (VOIDP) mxCalloc(len, nctypelen(datatype));
		status = Convert(opcode, datatype, len, value, scalefactor, addoffset, pr);
		status = ncvarput(cdfid, varid, start, count, value);
		Free((VOIDPP) & value);
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & start);
		Free((VOIDPP) & count);
		
		break;
		
	case VARGET:
		
		start = Mat2Long(prhs[3]);
		count = Mat2Long(prhs[4]);
        intcount = Mat2Int(prhs[4]);
		
		autoscale = (nrhs > 5 && Scalar2Int(prhs[5]) != 0);
		
		if (!autoscale)	{
			scalefactor = 1.0;
			addoffset = 0.0;
		}
		
		name = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		dim = (int *) mxCalloc(MAX_NC_DIMS, sizeof(int));
		
		status = ncvarinq(cdfid, varid, name, & datatype, & ndims, dim, & natts);
		
		datatype = RepairBadDataType(datatype);
		
		for (i = 0; i < ndims; i++)	{
			if (count[i] == -1)	{
				status = ncdiminq(cdfid, dim[i], name, & count[i]);
				count[i] -= start[i];
			}
		}
		
		Free((VOIDPP) & name);
		Free((VOIDPP) & dim);
		
		m = 0;
		n = 0;
		if (ndims > 0)	{
			m = count[0];
			n = count[0];
			for (i = 1; i < ndims; i++)	{
				n *= count[i];
				if (count[i] > 1)	{
					m = count[i];
				}
			}
			n /= m;
		}
		len = m * n;
		if (ndims < 2)	{
			m = 1;
			n = len;
		}
		
		for (i = 0; i < ndims; i++)	{
			intcount[i] = count[ndims-i-1];   /*	Reverse order.	*/
		}
		
		if (MEXCDF_4 || ndims < 2)	{
			mat = mxCreateFull(m, n, mxREAL);	/*	mxCreateDoubleMatrix	*/
		}
# if MEXCDF_5
		else	{
			mat = mxCreateNumericArray(ndims, intcount, mxDOUBLE_CLASS, mxREAL);
		}
# endif
		
		pr = mxGetPr(mat);
		
		value = (VOIDP) mxCalloc(len, nctypelen(datatype));
		status = ncvarget(cdfid, varid, start, count, value);
		status = Convert(opcode, datatype, len, value, scalefactor, addoffset, pr);
		Free((VOIDPP) & value);
		
		if (datatype == NC_CHAR)	{
			plhs[0] = SetStr(mat);
		}
		else	{
			plhs[0] = mat;
		}
		if (plhs[0] == NULL)	{
			plhs[0] = mat;
		}
		
		plhs[1] = Int2Scalar(status);
		
		Free((VOIDPP) & intcount);
		Free((VOIDPP) & count);
		Free((VOIDPP) & start);
		
		break;
		
	case VARPUTG:
		
		name = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		dim = (int *) mxCalloc(MAX_NC_DIMS, sizeof(int));
		
		status = ncvarinq(cdfid, varid, name, & datatype, & ndims, dim, & natts);
		
		datatype = RepairBadDataType(datatype);
		
		if (nrhs > 7)	{
			if (datatype == NC_CHAR)	{
				mat = SetStr(prhs[7]);
			}
			else	{
				mat = prhs[7];
			}
			if (mat == NULL)	{
				mat = prhs[7];
			}
		}
		else	{
			if (datatype == NC_CHAR)	{
				mat = SetStr(prhs[6]);
			}
			else	{
				mat = prhs[6];
			}
			if (mat == NULL)	{
				mat = prhs[6];
			}
		}
		pr = mxGetPr(mat);
		
		start = Mat2Long(prhs[3]);
		count = Mat2Long(prhs[4]);
		stride = Mat2Long(prhs[5]);
		imap = NULL;
		
		for (i = 0; i < ndims; i++)	{
			if (count[i] == -1)	{
				status = ncdiminq(cdfid, dim[i], name, & count[i]);
				count[i] -= start[i];
			}
		}
		
		Free((VOIDPP) & name);
		Free((VOIDPP) & dim);
		
		len = 0;
		if (ndims > 0)	{
			len = 1;
			for (i = 0; i < ndims; i++)	{
				len *= count[i];
			}
		}
		
		autoscale = (nrhs > 8 && Scalar2Int(prhs[8]) != 0);
		
		if (!autoscale)	{
			scalefactor = 1.0;
			addoffset = 0.0;
		}
		
		value = (VOIDP) mxCalloc(len, nctypelen(datatype));
		status = Convert(opcode, datatype, len, value, scalefactor, addoffset, pr);
		status = ncvarputg(cdfid, varid, start, count, stride, imap, value);
		Free((VOIDPP) & value);
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & stride);
		Free((VOIDPP) & count);
		Free((VOIDPP) & start);
		
		break;
		
	case VARGETG:
		
		start = Mat2Long(prhs[3]);
		count = Mat2Long(prhs[4]);
        intcount = Mat2Int(prhs[4]);
		stride = Mat2Long(prhs[5]);
		imap = NULL;
		
		autoscale = (nrhs > 7 && Scalar2Int(prhs[7]) != 0);
		
		if (!autoscale)	{
			scalefactor = 1.0;
			addoffset = 0.0;
		}
		
		name = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		dim = (int *) mxCalloc(MAX_NC_DIMS, sizeof(int));
		
		status = ncvarinq(cdfid, varid, name, & datatype, & ndims, dim, & natts);
		
		datatype = RepairBadDataType(datatype);
		
		for (i = 0; i < ndims; i++)	{
			if (count[i] == -1)	{
				status = ncdiminq(cdfid, dim[i], name, & count[i]);
				count[i] -= start[i];
			}
		}
		
		Free((VOIDPP) & name);
		Free((VOIDPP) & dim);
		
		m = 0;
		n = 0;
		if (ndims > 0)	{
			m = count[0];
			n = count[0];
			for (i = 1; i < ndims; i++)	{
				n *= count[i];
				if (count[i] > 1)	{
					m = count[i];
				}
			}
			n /= m;
		}
		len = m * n;
		if (ndims < 2)	{
			m = 1;
			n = len;
		}
		
		for (i = 0; i < ndims; i++)	{
			intcount[i] = count[ndims-i-1];   /*	Reverse order.	*/
		}
		
		if (MEXCDF_4 || ndims < 2)	{
			mat = mxCreateFull(m, n, mxREAL);	/*	mxCreateDoubleMatrix	*/
		}
# if MEXCDF_5
		else	{
			mat = mxCreateNumericArray(ndims, intcount, mxDOUBLE_CLASS, mxREAL);
		}
# endif
		
		pr = mxGetPr(mat);
		
		value = (VOIDP) mxCalloc(len, nctypelen(datatype));
		status = ncvargetg(cdfid, varid, start, count, stride, imap, value);
		status = Convert(opcode, datatype, len, value, scalefactor, addoffset, pr);
		Free((VOIDPP) & value);
		
		if (datatype == NC_CHAR)	{
			plhs[0] = SetStr(mat);
		}
		else	{
			plhs[0] = mat;
		}
		if (plhs[0] == NULL)	{
/*			prhs[0] = mat;		*/
			plhs[0] = mat;		/*	ZYDECO 24Jan2000	*/
		}
		
		plhs[1] = Int2Scalar(status);
		
		Free((VOIDPP) & stride);
		Free((VOIDPP) & intcount);
		Free((VOIDPP) & count);
		Free((VOIDPP) & start);
		
		break;

	case VARRENAME:
		
		name = Mat2Str(prhs[3]);
		
		status = ncvarrename(cdfid, varid, name);
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & name);
		
		break;
		
	case VARCOPY:
	
		incdf = cdfid;
		
		invar = varid;
		
		outcdf = Scalar2Int(prhs[3]);
	
		outvar = -1;
/*		outvar = ncvarcopy(incdf, invar, outcdf);	*/
		
		plhs[0] = Int2Scalar(outvar);
		plhs[1] = Int2Scalar((outvar >= 0) ? 0 : outvar);
		
		break;
		
	case ATTPUT:
		
		datatype = (nc_type) Parameter(prhs[4]);
		
		datatype = RepairBadDataType(datatype);
		
		if (datatype == NC_CHAR)	{
			mat = SetNum(prhs[6]);
		}
		else	{
			mat = prhs[6];
		}
		if (mat == NULL)	{
			mat = prhs[6];
		}
		
		len = Scalar2Int(prhs[5]);
		if (len == -1)	{
			len = Count(mat);
		}
		
		pr = mxGetPr(mat);
		value = (VOIDP) mxCalloc(len, nctypelen(datatype));
		status = Convert(opcode, datatype, len, value, (DOUBLE) 1.0, (DOUBLE) 0.0, pr);
		
		status = ncattput(cdfid, varid, attname, datatype, len, value);
		
		if (value != NULL)	{
			Free((VOIDPP) & value);
		}
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & attname);
		
		break;
		
	case ATTINQ:
		
		status = ncattinq(cdfid, varid, attname, & datatype, & len);
		
		datatype = RepairBadDataType(datatype);
		
		plhs[0] = Int2Scalar((int) datatype);
		plhs[1] = Int2Scalar(len);
		plhs[2] = Int2Scalar(status);
		
		Free((VOIDPP) & attname);
		
		break;
		
	case ATTGET:
		
		status = ncattinq(cdfid, varid, attname, & datatype, & len);
		
		datatype = RepairBadDataType(datatype);
		
		value = (VOIDP) mxCalloc(len, nctypelen(datatype));
		status = ncattget(cdfid, varid, attname, value);
		
		mat = mxCreateDoubleMatrix(1, len, mxREAL);
		
		pr = mxGetPr(mat);
		
		status = Convert(opcode, datatype, len, value, (DOUBLE) 1.0, (DOUBLE) 0.0, pr);
		
		if (value != NULL)	{
			Free((VOIDPP) & value);
		}
		
		if (datatype == NC_CHAR)	{
			plhs[0] = SetStr(mat);
		}
		else	{
			plhs[0] = mat;
		}
		if (plhs[0] == NULL)	{
/*			prhs[4] = mat;		*/
			plhs[0] = mat;		/*	ZYDECO 24Jan2000	*/
		}
		
		plhs[1] = Int2Scalar(status);
		
		Free((VOIDPP) & attname);
		
		break;
		
	case ATTCOPY:
	
		incdf = cdfid;
		
		invar = varid;
		
		outcdf = Scalar2Int(prhs[4]);
	
		if (mxIsNumeric(prhs[5]))	{
			outvar = Scalar2Int(prhs[2]);
		}
		else	{
			name = Mat2Str(prhs[5]);
			outvar = ncvarid(cdfid, name);
			Free((VOIDPP) & name);
		}
	
		status = ncattcopy(incdf, invar, attname, outcdf, outvar);
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & attname);
		
		break;
		
	case ATTNAME:
		
		attnum = Scalar2Int(prhs[3]);
		attname = (char *) mxCalloc(MAX_NC_NAME, sizeof(char));
		
		status = ncattname(cdfid, varid, attnum, attname);
		
		plhs[0] = Str2Mat(attname);
		plhs[1] = Int2Scalar(status);
		
		Free((VOIDPP) & attname);
		
		break;
		
	case ATTRENAME:
	
		newname = Mat2Str(prhs[4]);
		
		status = ncattrename(cdfid, varid, attname, newname);
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & attname);
		Free((VOIDPP) & newname);
		
		break;
		
	case ATTDEL:
		
		status = ncattdel(cdfid, varid, attname);
		
		plhs[0] = Int2Scalar(status);
		
		Free((VOIDPP) & attname);
		
		break;
		
	case RECPUT:
		
		recnum = Scalar2Long(prhs[2]);
		pr = mxGetPr(prhs[3]);
		
		autoscale = (nrhs > 4 && Scalar2Int(prhs[4]) != 0);
		
		if (!autoscale)	{
			scalefactor = 1.0;
			addoffset = 0.0;
		}
		
		recvarids = (int *) mxCalloc(MAX_VAR_DIMS, sizeof(int));
		recsizes = (long *) mxCalloc(MAX_VAR_DIMS, sizeof(long));
		datap = (VOIDPP) mxCalloc(MAX_VAR_DIMS, sizeof(VOIDP));
		
		status = ncrecinq(cdfid, & nrecvars, recvarids, recsizes);
		
		if (status == -1)	{
			plhs[0] = Int2Scalar(status);
			break;
		}
		
		length = 0;
		n = 0;
		for (i = 0; i < nrecvars; i++)	{
			ncvarinq(cdfid, recvarids[i], NULL, & datatype, NULL, NULL, NULL);
		
			datatype = RepairBadDataType(datatype);
			
			length += recsizes[i];
			n += (recsizes[i] / nctypelen(datatype));
		}
		
		if (Count(prhs[3]) < n)	{
			status = -1;
			plhs[0] = Int2Scalar(status);
			break;
		}
		
		if ((value = (VOIDP) mxCalloc((int) length, sizeof(char))) == NULL)	{
			status = -1;
			plhs[0] = Int2Scalar(status);
			break;
		}
		
		length = 0;
		p = value;
		for (i = 0; i < nrecvars; i++)	{
			datap[i] = p;
			p += recsizes[i];
		}
		
		p = (char *) value;
		pr = mxGetPr(prhs[3]);
		
		for (i = 0; i < nrecvars; i++)	{
			ncvarinq(cdfid, recvarids[i], NULL, & datatype, NULL, NULL, NULL);
		
			datatype = RepairBadDataType(datatype);
		
			length = recsizes[i] / nctypelen(datatype);
			if (autoscale)	{
				addoffset = Add_Offset(cdfid, recvarids[i]);
				scalefactor = Scale_Factor(cdfid, recvarids[i]);
				if (scalefactor == 0.0)	{
					scalefactor = 1.0;
				}
			}
			Convert(opcode, datatype, length, (VOIDP) p,  scalefactor, addoffset, pr);
			pr += length;
			p += recsizes[i];
		}
		
		status = ncrecput(cdfid, recnum, datap);
		
		plhs[0] = Int2Scalar(status);
		
		Free ((VOIDPP) & value);
		Free ((VOIDPP) & datap);
		Free ((VOIDPP) & recsizes);
		Free ((VOIDPP) & recvarids);
		
		break;
		
	case RECGET:
		
		recnum = Scalar2Long(prhs[2]);
		
		autoscale = (nrhs > 3 && Scalar2Int(prhs[3]) != 0);
		
		if (!autoscale)	{
			scalefactor = 1.0;
			addoffset = 0.0;
		}
		
		recvarids = (int *) mxCalloc(MAX_VAR_DIMS, sizeof(int));
		recsizes = (long *) mxCalloc(MAX_VAR_DIMS, sizeof(long));
		datap = (VOIDPP) mxCalloc(MAX_VAR_DIMS, sizeof(VOIDP));
		
		status = ncrecinq(cdfid, & nrecvars, recvarids, recsizes);
		
		if (status == -1)	{
			Free ((VOIDPP) & recsizes);
			Free ((VOIDPP) & recvarids);
			plhs[1] = Int2Scalar(status);
			break;
		}
		
		if (nrecvars == 0)	{
			Free ((VOIDPP) & recsizes);
			Free ((VOIDPP) & recvarids);
			plhs[0] = mxCreateFull(0, 0, REAL);
			break;
		}
		
		length = 0;
		n = 0;
		for (i = 0; i < nrecvars; i++)	{
			ncvarinq(cdfid, recvarids[i], NULL, & datatype, NULL, NULL, NULL);
		
			datatype = RepairBadDataType(datatype);
			
			length += recsizes[i];
			n += (recsizes[i] / nctypelen(datatype));
		}
		
		if ((value = (VOIDP) mxCalloc((int) length, sizeof(char))) == NULL)	{
			status = -1;
			plhs[1] = Int2Scalar(status);
			break;
		}
		
		if (value == NULL)	{
			status = -1;
			plhs[1] = Int2Scalar(status);
			break;
		}
		
		length = 0;
		p = value;
		for (i = 0; i < nrecvars; i++)	{
			datap[i] = p;
			p += recsizes[i];
		}
		
		if ((status = ncrecget(cdfid, recnum, datap)) == -1)	{
			plhs[1] = Int2Scalar(status);
			break;
		}
		
		m = 1;
		
		plhs[0] = mxCreateFull(m, n, REAL);
		
		if (plhs[0] == NULL)	{
			status = -1;
			plhs[1] = Int2Scalar(status);
			break;
		}
		
		pr = mxGetPr(plhs[0]);
		p = (char *) value;
		
		for (i = 0; i < nrecvars; i++)	{
			status = ncvarinq(cdfid, recvarids[i], NULL, & datatype, NULL, NULL, NULL);
		
			datatype = RepairBadDataType(datatype);
			
			if (status == -1)	{
				plhs[1] = Int2Scalar(status);
				break;
			}
			length = recsizes[i] / nctypelen(datatype);
			if (autoscale)	{
				addoffset = Add_Offset(cdfid, recvarids[i]);
				scalefactor = Scale_Factor(cdfid, recvarids[i]);
				if (scalefactor == 0.0)	{
					scalefactor = 1.0;
				}
			}
			Convert(opcode, datatype, length, (VOIDP) p,  scalefactor, addoffset, pr);
			pr += length;
			p += recsizes[i];
		}
		
		plhs[1] = Int2Scalar(status);
		
		Free ((VOIDPP) & value);
		Free ((VOIDPP) & datap);
		Free ((VOIDPP) & recsizes);
		Free ((VOIDPP) & recvarids);
		
		break;

	case RECINQ:
		
		recvarids = (int *) mxCalloc(MAX_VAR_DIMS, sizeof(int));
		recsizes = (long *) mxCalloc(MAX_VAR_DIMS, sizeof(long));
		
		status = ncrecinq(cdfid, & nrecvars, recvarids, recsizes);
		
		if (status != -1)	{
			for (i = 0; i < nrecvars; i++)	{
				ncvarinq(cdfid, recvarids[i], NULL, & datatype, NULL, NULL, NULL);
		
				datatype = RepairBadDataType(datatype);
			
				recsizes[i] /= nctypelen(datatype);
			}
			m = 1;
			n = nrecvars;
			plhs[0] = Int2Mat(recvarids, m, n);
			plhs[1] = Long2Mat(recsizes, m, n);
		}
		
		plhs[2] = Int2Scalar(status);
		
		Free ((VOIDPP) & recsizes);
		Free ((VOIDPP) & recvarids);
		
		break;
		
	case TYPELEN:
	
		datatype = (nc_type) Parameter(prhs[1]);
		
		len = nctypelen(datatype);
		
		plhs[0] = Int2Scalar(len);
		plhs[1] = Int2Scalar((len >= 0) ? 0 : 1);
		
		break;
		
	case SETFILL:
	
		fillmode = Scalar2Int(prhs[1]);
		
		status = ncsetfill(cdfid, fillmode);
		
		plhs[0] = Int2Scalar(status);
		plhs[1] = Int2Scalar(0);
		
		break;

	case SETOPTS:
		
		plhs[0] = Int2Scalar(ncopts);
		plhs[1] = Int2Scalar(0);
		ncopts = Scalar2Int(prhs[1]);
		
		break;
		
	case ERR:
	
		plhs[0] = Int2Scalar(ncerr);
		ncerr = 0;
		plhs[1] = Int2Scalar(0);
		
		break;
		
	case PARAMETER:
	
		if (nrhs > 1)	{
			plhs[0] = Int2Scalar(Parameter(prhs[1]));
			plhs[1] = Int2Scalar(0);
		}
		else	{
			i = 0;
			while (strcmp(parms[i].name, "NONE") != 0)	{
				mexPrintf("%12d %s\n", parms[i].code, parms[i].name);
				i++;
			}
			plhs[0] = Int2Scalar(0);
			plhs[1] = Int2Scalar(-1);
		}
		
		break;
		
	default:
	
		break;
	}
	
	return;
}
Exemplo n.º 7
0
int ex_put_elem_num_map (int  exoid,
                         const int *elem_map)
{
  int numelemdim, dims[1], mapid, iresult;
  long num_elem, start[1], count[1]; 
  nclong *lptr;
  char errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

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

  /* determine number of elements. Return if zero... */
  if ((numelemdim = ncdimid (exoid, DIM_NUM_ELEM)) == -1)
    {
      return (EX_NOERR);
    }

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


  /* put netcdf file into define mode  */

  if ((mapid = ncvarid (exoid, VAR_ELEM_NUM_MAP)) == -1) {
    if (ncredef (exoid) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to put file id %d into define mode",
                exoid);
        ex_err("ex_put_elem_num_map",errmsg,exerrval);
        return (EX_FATAL);
      }


    /* create a variable array in which to store the element numbering map  */

    dims[0] = numelemdim;

    if ((mapid = ncvardef (exoid, VAR_ELEM_NUM_MAP, NC_LONG, 1, dims)) == -1)
      {
        if (ncerr == NC_ENAMEINUSE)
          {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: element numbering map already exists in file id %d",
                    exoid);
            ex_err("ex_put_elem_num_map",errmsg,exerrval);
          }
        else
          {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: failed to create element numbering map in file id %d",
                    exoid);
            ex_err("ex_put_elem_num_map",errmsg,exerrval);
          }
        goto error_ret;         /* exit define mode and return */
      }


    /* leave define mode  */

    if (ncendef (exoid) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to complete definition in file id %d",
                exoid);
        ex_err("ex_put_elem_num_map",errmsg,exerrval);
        return (EX_FATAL);
      }
  }

  /* write out the element numbering map  */

  /* this contortion is necessary because netCDF is expecting nclongs; fortunately
     it's necessary only when ints and nclongs aren't the same size */

  start[0] = 0;
  count[0] = num_elem;

  if (sizeof(int) == sizeof(nclong)) {
    iresult = ncvarput (exoid, mapid, start, count, elem_map);
  } else {
    lptr = itol (elem_map, (int)num_elem);
    iresult = ncvarput (exoid, mapid, start, count, lptr);
    free(lptr);
  }

  if (iresult == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to store element numbering map in file id %d",
              exoid);
      ex_err("ex_put_elem_num_map",errmsg,exerrval);
      return (EX_FATAL);
    }


  return (EX_NOERR);

  /* Fatal error: exit definition mode and return */
 error_ret:
  if (ncendef (exoid) == -1)     /* exit define mode */
    {
      sprintf(errmsg,
              "Error: failed to complete definition for file id %d",
              exoid);
      ex_err("ex_put_elem_num_map",errmsg,exerrval);
    }
  return (EX_FATAL);
}
Exemplo n.º 8
0
int main(int argc, char **argv)
{
   int cdf;
   int img, img2;
   int dim[MAX_VAR_DIMS];
   long start[MAX_VAR_DIMS];
   long count[MAX_VAR_DIMS];
   double image[256*256];
   int i, itype, jtype;
   int cflag = 0;
   char filename[256];

#if MINC2
   if (argc == 2 && !strcmp(argv[1], "-2")) {
       cflag = MI2_CREATE_V2;
   }
#endif /* MINC2 */

   snprintf(filename, sizeof(filename), "test_minc_types-%d.mnc", getpid());

   ncopts=NC_VERBOSE|NC_FATAL;
   for (itype=0; itype<ntypes; itype++) {
      for (jtype=0; jtype<ntypes; jtype++) {
         cdf=micreate(filename, NC_CLOBBER | cflag);
         count[2]=256;
         count[1]=20;
         count[0]=7;
         dim[2]=ncdimdef(cdf, MIzspace, count[2]);
         dim[1]=ncdimdef(cdf, MIxspace, count[1]);
         dim[0]=ncdimdef(cdf, MIyspace, count[0]);
         img=ncvardef(cdf, MIimage, types[itype].type, 1, dim);
         (void) miattputstr(cdf, img, MIsigntype, types[itype].sign);
         img2=ncvardef(cdf, "image2", types[jtype].type, 1, dim);
         (void) miattputstr(cdf, img2, MIsigntype, types[jtype].sign);
         image[0]=10.0;
         image[1]=2.0*(-FLT_MAX);
         image[2]=2.0*FLT_MAX;
         image[3]=3.2;
         image[4]=3.7;
         image[5]=(-3.2);
         image[6]=(-3.7);
#if 0
   for (j=0; j<count[0]; j++) {
      for (i=0; i<count[1]; i++) {
         ioff=(j*count[1]+i)*count[2];
         for (k=0; k<count[2]; k++)
            image[ioff+k]=ioff+k+10;
      }
   }
#endif
         (void) ncendef(cdf);
         (void) miset_coords(3,0L,start);
         (void) mivarput(cdf, img, start, count, NC_DOUBLE, NULL, image);

         (void) mivarget(cdf, img, start, count, types[jtype].type,
                         types[jtype].sign, image);
         (void) mivarput(cdf, img2, start, count, types[jtype].type,
                         types[jtype].sign, image);

         (void) mivarget(cdf, img2, start, count, NC_DOUBLE, NULL, image);
         (void) printf("Image 1 : %s %s\n",types[itype].sign,
                       types[itype].ctype);
         (void) printf("Image 2 : %s %s\n",types[jtype].sign,
                       types[jtype].ctype);
         for (i=0;i<count[0];i++)
            (void) printf("   image[%d] = %g\n",(int) i, image[i]);
         (void) miclose(cdf);
      }
   }
   unlink(filename);
   return(0);
}
Exemplo n.º 9
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.º 10
0
int ex_put_init (int   exoid,
                 const char *title,
                 int   num_dim,
                 int   num_nodes,
                 int   num_elem,
                 int   num_elem_blk,
                 int   num_node_sets,
                 int   num_side_sets)
{
  int numdimdim, numnoddim, elblkdim, nsetdim, ssetdim, strdim, dim[2], varid;
#if 0
  /* used for header size calculations which are turned off for now */
  int header_size, fixed_var_size, iows;
#endif  
  char errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

  if ((ncdimid (exoid, DIM_NUM_DIM)) != -1)
    {
      exerrval = EX_MSG;
      sprintf(errmsg,
              "Error: initialization already done for file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      return (EX_FATAL);
    }


  /* put file into define mode */

  if (ncredef (exoid) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to put file id %d into define mode", exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      return (EX_FATAL);
    }


  /* define some attributes... */

  if (ncattput (exoid, NC_GLOBAL, (const char*) ATT_TITLE, 
                NC_CHAR, strlen(title)+1, (void *)title) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to define title attribute to file id %d", exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

  /* ...and some dimensions... */

  if ((numdimdim = ncdimdef (exoid, DIM_NUM_DIM, (long)num_dim)) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to define number of dimensions in file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

  /*
   * Need to handle "empty file" that may be the result of a strange
   * load balance or some other strange run.  Note that if num_node
   * == 0, then num_elem must be zero since you cannot have elements
   * with no nodes. It *is* permissible to have zero elements with
   * non-zero node count.
   */
     
  if (num_nodes > 0) {
    if ((numnoddim = ncdimdef (exoid, DIM_NUM_NODES, (long)num_nodes)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define number of nodes in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }
  }
  
  if (num_elem > 0) {
    if (num_nodes <=  0) {
      exerrval = EX_MSG;
      sprintf(errmsg,
              "Error: Cannot have non-zero element count if node count is zero.in file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
    
    if ((ncdimdef (exoid, DIM_NUM_ELEM, (long)num_elem)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define number of elements in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }
  }

  /* Can have nonzero num_elem_blk even if num_elem == 0 */
  if (num_elem_blk > 0) {
    if ((elblkdim = ncdimdef (exoid, DIM_NUM_EL_BLK, (long)num_elem_blk)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define number of element blocks in file id %d",
                exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }
    
    /* ...and some variables */
    
    /* element block id status array */
    
    dim[0] = elblkdim;
    if ((varid = ncvardef (exoid, VAR_STAT_EL_BLK, NC_LONG, 1, dim)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define element block status array in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }
    
#if 0
    /*   store property name as attribute of property array variable */
    if ((ncattput (exoid, varid, ATT_PROP_NAME, NC_CHAR, 7, "STATUS")) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to store element block property name %s in file id %d",
                "STATUS",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        return (EX_FATAL);
      }
#endif
    
    /* element block id array */
    
    if ((varid = ncvardef (exoid, VAR_ID_EL_BLK, NC_LONG, 1, dim)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define element block id array in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }
    
    /*   store property name as attribute of property array variable */
    if ((ncattput (exoid, varid, ATT_PROP_NAME, NC_CHAR, 3, "ID")) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to store element block property name %s in file id %d",
                "ID",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        return (EX_FATAL);
      }
  }

  /* node set id array: */

  nsetdim = 0;
  if (num_node_sets > 0) {

    if ((nsetdim = ncdimdef (exoid, DIM_NUM_NS, (long)num_node_sets)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define number of node sets in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }

    /* node set id status array: */

    dim[0] = nsetdim;
    if ((varid = ncvardef (exoid, VAR_NS_STAT, NC_LONG, 1, dim)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to create node sets status array in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }


#if 0
    /*   store property name as attribute of property array variable */
    if ((ncattput (exoid, varid, ATT_PROP_NAME, NC_CHAR, 7, "STATUS")) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to store node set property name %s in file id %d",
                "ID",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        return (EX_FATAL);
      }
#endif

    /* node set id array: */

    dim[0] = nsetdim;
    if ((varid = ncvardef (exoid, VAR_NS_IDS, NC_LONG, 1, dim)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to create node sets property array in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }


    /*   store property name as attribute of property array variable */
    if ((ncattput (exoid, varid, ATT_PROP_NAME, NC_CHAR, 3, "ID")) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to store node set property name %s in file id %d",
                "ID",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        return (EX_FATAL);
      }


  }

  /* side set id array: */

  ssetdim = 0;
  if (num_side_sets > 0) {

    if ((ssetdim = ncdimdef (exoid, DIM_NUM_SS, (long)num_side_sets)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define number of side sets in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }

    /* side set id status array: */

    dim[0] = ssetdim;
    if ((varid = ncvardef (exoid, VAR_SS_STAT, NC_LONG, 1, dim)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define side set status in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }

#if 0
    /*   store property name as attribute of property array variable */
    if ((ncattput (exoid, varid, ATT_PROP_NAME, NC_CHAR, 7, "STATUS")) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to store side set property name %s in file id %d",
                "ID",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        return (EX_FATAL);
      }
#endif
    
    /* side set id array: */

    if ((varid = ncvardef (exoid, VAR_SS_IDS, NC_LONG, 1, dim)) == -1) 
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to define side set property in file id %d",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }

    /*   store property name as attribute of property array variable */
    if ((ncattput (exoid, varid, ATT_PROP_NAME, NC_CHAR, 3, "ID")) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to store side set property name %s in file id %d",
                "ID",exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        return (EX_FATAL);
      }

  }

  /*
   * To reduce the maximum dataset sizes, the storage of the nodal
   * coordinates and the nodal variables was changed from a single
   * dataset to a dataset per component or variable.  However, we
   * want to maintain some form of compatability with the old
   * exodusII version.  It is easy to do this on read; however, we
   * also want to be able to store in the old format using the new
   * library. 
   *
   * The mode is set in the ex_create call. The setting can be checked
   * via the ATT_FILESIZE attribute in the file (1=large,
   * 0=normal). Also handle old files that do not contain this
   * attribute.
   */

  if (num_nodes > 0) {
    if (ex_large_model(exoid) == 1) {
      /* node coordinate arrays -- separate storage... */

      /*
       * Check that storage required for coordinates  is less
       * than 2GB which is maximum size permitted by netcdf
       * (in large file mode). 1<<29 == max number of integer items.
       */
      int shift = nc_flt_code(exoid) == NC_DOUBLE ? 28 : 29; 
      if (num_nodes  > (1<<shift)) {
        exerrval = EX_BADPARAM;
        sprintf(errmsg,
                "Error: Size to store nodal coordinates exceeds 2GB in file id %d",
                exoid);
        ex_err("ex_put_init",errmsg,exerrval);
        return (EX_FATAL);
      }
    
      dim[0] = numnoddim;
      if (num_dim > 0) {
        if (ncvardef (exoid, VAR_COORD_X, nc_flt_code(exoid), 1, dim) == -1)
          {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: failed to define node x coordinate array in file id %d",exoid);
            ex_err("ex_put_init",errmsg,exerrval);
            goto error_ret;         /* exit define mode and return */
          }
      }
    
      if (num_dim > 1) {
        if (ncvardef (exoid, VAR_COORD_Y, nc_flt_code(exoid), 1, dim) == -1)
          {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: failed to define node y coordinate array in file id %d",exoid);
            ex_err("ex_put_init",errmsg,exerrval);
            goto error_ret;         /* exit define mode and return */
          }
      }

      if (num_dim > 2) {
        if (ncvardef (exoid, VAR_COORD_Z, nc_flt_code(exoid), 1, dim) == -1)
          {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: failed to define node z coordinate array in file id %d",exoid);
            ex_err("ex_put_init",errmsg,exerrval);
            goto error_ret;         /* exit define mode and return */
          }
      }
    } else {
      /* node coordinate arrays: -- all stored together (old method) */

      dim[0] = numdimdim;
      dim[1] = numnoddim;
      if (ncvardef (exoid, VAR_COORD, nc_flt_code(exoid), 2, dim) == -1)
        {
          exerrval = ncerr;
          sprintf(errmsg,
                  "Error: failed to define node coordinate array in file id %d",exoid);
          ex_err("ex_put_init",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_init",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }

  /* coordinate names array */

  dim[0] = numdimdim;
  dim[1] = strdim;

  if (ncvardef (exoid, VAR_NAME_COOR, NC_CHAR, 2, dim) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to define coordinate name array in file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  

  /* Element block names... */
  if (num_elem_blk > 0) {
    dim[0] = elblkdim;
    dim[1] = strdim;
    
    if (ncvardef (exoid, VAR_NAME_EL_BLK, NC_CHAR, 2, dim) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
	      "Error: failed to define element block name array in file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  }
  
  /* Nodeset names... */
  if (num_node_sets > 0) {
    dim[0] = nsetdim;
    dim[1] = strdim;
    
    if (ncvardef (exoid, VAR_NAME_NS, NC_CHAR, 2, dim) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
	      "Error: failed to define nodeset name array in file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  }
  
  /* Sideset names... */
  if (num_side_sets > 0) {
    dim[0] = ssetdim;
    dim[1] = strdim;
    
    if (ncvardef (exoid, VAR_NAME_SS, NC_CHAR, 2, dim) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
	      "Error: failed to define sideset name array in file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      goto error_ret;         /* exit define mode and return */
    }
  }
  
  
  /* leave define mode */
#if 1
  if (ncendef (exoid) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to complete variable definitions in file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      return (EX_FATAL);
    }
  
#else
  /* estimate (guess) size of header of netCDF file */
  header_size = 1200 + 
    num_elem_blk * 800 + 
    num_node_sets * 220 + 
    num_side_sets * 300;

  if (header_size > MAX_HEADER_SIZE) header_size = MAX_HEADER_SIZE;

  /* estimate (guess) size of fixed size variable section of netCDF file */

  if (nc_flt_code(exoid) == NC_DOUBLE) 
    iows = 8;
  else
    iows = 4;

  fixed_var_size = num_dim * num_nodes * iows +
    num_nodes * sizeof(int) +
    num_elem * 16 * sizeof(int) +
    num_elem_blk * sizeof(int) +
    num_node_sets * num_nodes/100 * sizeof(int) +
    num_node_sets * num_nodes/100 * iows +
    num_node_sets * sizeof(int) +
    num_side_sets * num_elem/100 * 2 * sizeof(int) +
    num_side_sets * num_elem/100 * iows +
    num_side_sets * sizeof(int);



  /* With netcdf-3.4, this produces very large files on the
   * SGI.  Also with netcdf-3.5beta3
   */
  /*
   * This is also causing other problems on other systems .. disable for now
   */
  if (nc__enddef (exoid, 
                  header_size, NC_ALIGN_CHUNK, 
                  fixed_var_size, NC_ALIGN_CHUNK) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to complete variable definitions in file id %d",exoid);
      ex_err("ex_put_init",errmsg,exerrval);
      return (EX_FATAL);
    }

#endif
  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_init",errmsg,exerrval);
    }
  return (EX_FATAL);
}
Exemplo n.º 11
0
void FileNetcdf::startDataMode() {
   int status = ncendef(mFile);
   handleNetcdfError(status, "could not put into data mode");
}
Exemplo n.º 12
0
int
main() {			/* create example.cdf */

   int  ncid;			/* netCDF id */

   /* dimension ids */
   int  lat_dim, lon_dim, frtime_dim, timelen_dim;

   /* variable ids */
   int  P_id, lat_id, lon_id, frtime_id, reftime_id, scalarv_id;

   /* variable shapes */
   int dims[3];

   /* containers for scalar attributes */
   float  float_val;
   double  double_val;

   /* attribute vectors */
   float  P_valid_range[2];

   /* enter define mode */
   ncid = nccreate("example.nc", NC_CLOBBER);

   /* define dimensions */
   lat_dim = ncdimdef(ncid, "lat", 4L);
   lon_dim = ncdimdef(ncid, "lon", 3L);
   frtime_dim = ncdimdef(ncid, "frtime", NC_UNLIMITED);
   timelen_dim = ncdimdef(ncid, "timelen", 20L);

   /* define variables and assign attributes */

   dims[0] = frtime_dim;
   dims[1] = lat_dim;
   dims[2] = lon_dim;
   P_id = ncvardef (ncid, "P", NC_FLOAT, 3, dims);
   ncattput (ncid, P_id, "long_name", NC_CHAR, 24,
	     (void *)"pressure at maximum wind");
   ncattput (ncid, P_id, "units", NC_CHAR, 12,
	     (void *)"hectopascals");
   P_valid_range[0] = 0;
   P_valid_range[1] = 1500;
   ncattput (ncid, P_id, "valid_range", NC_FLOAT, 2,
	     (void *) P_valid_range);
   float_val = -9999;
   ncattput (ncid, P_id, "_FillValue", NC_FLOAT, 1,
	     (void *) &float_val);

   dims[0] = lat_dim;
   lat_id = ncvardef (ncid, "lat", NC_FLOAT, 1, dims);
   ncattput (ncid, lat_id, "long_name", NC_CHAR, 8,
	     (void *)"latitude");
   ncattput (ncid, lat_id, "units", NC_CHAR, 13,
	     (void *)"degrees_north");

   dims[0] = lon_dim;
   lon_id = ncvardef (ncid, "lon", NC_FLOAT, 1, dims);
   ncattput (ncid, lon_id, "long_name", NC_CHAR, 9,
	     (void *)"longitude");
   ncattput (ncid, lon_id, "units", NC_CHAR, 12,
	     (void *)"degrees_east");

   dims[0] = frtime_dim;
   frtime_id = ncvardef (ncid, "frtime", NC_LONG, 1, dims);
   ncattput (ncid, frtime_id, "long_name", NC_CHAR, 13,
	     (void *)"forecast time");
   ncattput (ncid, frtime_id, "units", NC_CHAR, 5,
	     (void *)"hours");

   dims[0] = timelen_dim;
   reftime_id = ncvardef (ncid, "reftime", NC_CHAR, 1, dims);
   ncattput (ncid, reftime_id, "long_name", NC_CHAR, 14,
	     (void *)"reference time");
   ncattput (ncid, reftime_id, "units", NC_CHAR, 9,
	     (void *)"text_time");

   scalarv_id = ncvardef (ncid, "scalarv", NC_LONG, 0, 0);
   double_val = 1;
   ncattput (ncid, scalarv_id, "scalar_att", NC_DOUBLE, 1,
	     (void *) &double_val);

   /* Global attributes */
   ncattput (ncid, NC_GLOBAL, "history", NC_CHAR, 41,
	     (void *)"created by Unidata LDM from NPS broadcast");
   ncattput (ncid, NC_GLOBAL, "title", NC_CHAR, 48,
	     (void *)"NMC Global Product Set: Pressure at Maximum Wind");

   /* leave define mode */
   ncendef (ncid);
  
   {			/* store lat */
    static long lat_start[] = {0};
    static long lat_edges[] = {4};
    static float lat[] = {-90, -87.5, -85, -82.5};
    ncvarput(ncid, lat_id, lat_start, lat_edges, (void *)lat);
   }

   {			/* store lon */
    static long lon_start[] = {0};
    static long lon_edges[] = {3};
    static float lon[] = {-180, -175, -170};
    ncvarput(ncid, lon_id, lon_start, lon_edges, (void *)lon);
   }

   {			/* store frtime */
    static long frtime_start[] = {0};
    static long frtime_edges[] = {1};
    static long frtime[] = {12};
    ncvarput(ncid, frtime_id, frtime_start, frtime_edges,
	     (void *)frtime);
   }

   {			/* store frtime */
    static long frtime_start[] = {1};
    static long frtime_edges[] = {1};
    static long frtime[] = {18};
    ncvarput(ncid, frtime_id, frtime_start, frtime_edges,
	     (void *)frtime);
   }

   {			/* store reftime */
    static long reftime_start[] = {0};
    static long reftime_edges[] = {20};
    static char reftime[] = {"1992 03 04 12:00"};
    ncvarput(ncid, reftime_id, reftime_start, reftime_edges,
	     (void *)reftime);
   }

   {			/* store P */
    static long P_start[] = {0, 0, 0};
    static long P_edges[] = {2, 4, 3};
    static float P[2][4][3] = {
	{{950, 951, 952}, {953, 954, 955}, {956, 957, 958}, {959, 960, 961}},
	{{962, 963, 964}, {965, 966, 967}, {968, 969, 970}, {971, 972, 973}}
      };
    ncvarput(ncid, P_id, P_start, P_edges, (void *)&P[0][0][0]);
   }

   {			/* store scalarv */
    static long scalarv = {-2147483647};
    ncvarput1(ncid, scalarv_id, (long *)0, (void *)&scalarv);
   }
   ncclose (ncid);
   return 0;
}
Exemplo n.º 13
0
int ex_put_concat_side_sets (int   exoid,
                             const int  *side_set_ids,
                             const int  *num_elem_per_set,
                             const int  *num_dist_per_set,
                             const int  *side_sets_elem_index,
                             const int  *side_sets_dist_index,
                             const int  *side_sets_elem_list,
                             const int  *side_sets_side_list,
                             const void *side_sets_dist_fact)
{
  char *cdum;
  int i, num_side_sets, cur_num_side_sets, dimid, varid, dims[1], *ss_stat;
  int iresult;
  long start[1], count[1]; 
  nclong *lptr;
  float fdum;
  const float *flt_dist_fact;
  const double *dbl_dist_fact;
  char errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

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

/* first check if any side sets are specified */

  if (ncdimid (exoid, DIM_NUM_SS) == -1)
  {
    if (ncerr == NC_EBADDIM)
    {
      exerrval = ncerr;
      sprintf(errmsg,
             "Error: no side sets defined for file id %d", exoid);
      ex_err("ex_put_concat_side_sets",errmsg,exerrval);
    }
    else
    {
      exerrval = ncerr;
      sprintf(errmsg,
             "Error: failed to locate side sets defined in file id %d", exoid);
      ex_err("ex_put_concat_side_sets",errmsg,exerrval);
    }
    return (EX_FATAL);
  }

/* inquire how many side sets are to be stored */

  if (ex_inquire(exoid, EX_INQ_SIDE_SETS, &num_side_sets, &fdum, cdum) == -1)
  {
    sprintf(errmsg,
           "Error: failed to get number of side sets defined for file id %d",
            exoid);
    /* use error val from inquire */
    ex_err("ex_put_concat_side_sets",errmsg,exerrval);
    return (EX_FATAL);
  }
/* fill out side set status array */

  /* First, allocate space for the side set status list */
  if (!(ss_stat= malloc(num_side_sets*sizeof(int))))
  {
    exerrval = EX_MEMFAIL;
    sprintf(errmsg,
    "Error: failed to allocate space for side set status array in file id %d",
            exoid);
    ex_err("ex_put_concat_side_sets",errmsg,exerrval);
    return (EX_FATAL);
  }

  for (i=0;i<num_side_sets;i++)
  {
    if (num_elem_per_set[i] == 0) /* Is this a NULL side set? */
      ss_stat[i] = 0; /* change side set status to NULL */
    else
      ss_stat[i] = 1; /* change side set status to TRUE */
  }

  /* Next, get variable id of status array */
  if ((varid = ncvarid (exoid, VAR_SS_STAT)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to locate side set status in file id %d", exoid);
    ex_err("ex_put_concat_node_set",errmsg,exerrval);
    return (EX_FATAL);
  }

/* this contortion is necessary because netCDF is expecting nclongs; fortunately
   it's necessary only when ints and nclongs aren't the same size */

  start[0] = 0;
  count[0] = num_side_sets;

  if (sizeof(int) == sizeof(nclong)) {
     iresult = ncvarput (exoid, varid, start, count, ss_stat);
  } else {
     lptr = itol (ss_stat, num_side_sets);
     iresult = ncvarput (exoid, varid, start, count, lptr);
     free(lptr);
  }

  if (iresult == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to store side set status array to file id %d",
            exoid);
    ex_err("ex_put_concat_side_set",errmsg,exerrval);
    return (EX_FATAL);
  }

  free(ss_stat);

/* put netcdf file into define mode  */

  if (ncredef (exoid) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to put file id %d into define mode",
            exoid);
    ex_err("ex_put_concat_side_sets",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* create side set definitions */
  for (i=0; i<num_side_sets; i++)
  {
/* Keep track of the total number of side sets defined using a counter stored
   in a linked list keyed by exoid.
   NOTE: ex_get_file_item  is used to find the number of side sets
         for a specific file and returns that value.
*/
    cur_num_side_sets=ex_get_file_item(exoid, &ss_ctr_list );
    if (cur_num_side_sets >= num_side_sets)
    {
      exerrval = EX_FATAL;
      sprintf(errmsg,
             "Error: exceeded number of side sets (%d) defined in file id %d",
              num_side_sets,exoid);
      ex_err("ex_put_concat_side_sets",errmsg,exerrval);
      goto error_ret;
    }

/*   NOTE: ex_inc_file_item  is used to find the number of side sets
         for a specific file and returns that value incremented. */

    cur_num_side_sets=ex_inc_file_item(exoid, &ss_ctr_list );

/*  define dimension for number of sides/elements per side set */

    if (num_elem_per_set[i] == 0) /* Is this a NULL side set? */
      continue; /* Do not create anything for NULL side sets! */


    if ((dimid = ncdimdef (exoid, DIM_NUM_SIDE_SS(cur_num_side_sets+1),
                   (long)num_elem_per_set[i])) == -1)
    {
      if (ncerr == NC_ENAMEINUSE)
      {
        exerrval = ncerr;
        sprintf(errmsg,
               "Error: side set side count %d already defined in file id %d",
                side_set_ids[i],exoid);
        ex_err("ex_put_concat_side_sets",errmsg,exerrval);
      }
      else
      {
        exerrval = ncerr;
        sprintf(errmsg,
             "Error: failed to define number of sides for set %d in file id %d",
                side_set_ids[i],exoid);
        ex_err("ex_put_concat_side_sets",errmsg,exerrval);
      }
      goto error_ret;
    }

    /* create element list variable for side set */

    dims[0] = dimid;
    if (ncvardef (exoid,VAR_ELEM_SS(cur_num_side_sets+1),NC_LONG,1,dims) == -1)
    {
      if (ncerr == NC_ENAMEINUSE)
      {
        exerrval = ncerr;
        sprintf(errmsg,
             "Error: element list already exists for side set %d in file id %d",
                side_set_ids[i],exoid);
        ex_err("ex_put_concat_side_sets",errmsg,exerrval);
      }
      else
      {
        exerrval = ncerr;
        sprintf(errmsg,
           "Error: failed to create element list for side set %d in file id %d",
                side_set_ids[i],exoid);
        ex_err("ex_put_concat_side_sets",errmsg,exerrval);
      }
      goto error_ret;            /* exit define mode and return */
    }

    /* create side list variable for side set */

    if (ncvardef (exoid,VAR_SIDE_SS(cur_num_side_sets+1),NC_LONG,1,dims) == -1)
    {
      if (ncerr == NC_ENAMEINUSE)
      {
        exerrval = ncerr;
        sprintf(errmsg,
               "Error: side list already exists for side set %d in file id %d",
                side_set_ids[i],exoid);
        ex_err("ex_put_concat_side_sets",errmsg,exerrval);
      }
      else
      {
        exerrval = ncerr;
        sprintf(errmsg,
              "Error: failed to create side list for side set %d in file id %d",
                side_set_ids[i],exoid);
        ex_err("ex_put_concat_side_sets",errmsg,exerrval);
      }
      goto error_ret;         /* exit define mode and return */

     }

/*  define dimension for number of dist factors/nodes per side set */

/*  NOTE: only define df count if  the dist factors exist! */

    if (num_dist_per_set[i] > 0)
    {

      if ((dimid = ncdimdef (exoid, DIM_NUM_DF_SS(cur_num_side_sets+1),
                              (long)num_dist_per_set[i])) == -1)
      {
        if (ncerr == NC_ENAMEINUSE)
        {
          exerrval = ncerr;
          sprintf(errmsg,
                 "Error: side set df count %d already defined in file id %d",
                  side_set_ids[i],exoid);
          ex_err("ex_put_concat_side_sets",errmsg,exerrval);
        }
        else
        {
          exerrval = ncerr;
          sprintf(errmsg,
           "Error: failed to define side set df count for set %d in file id %d",
                  side_set_ids[i],exoid);
          ex_err("ex_put_concat_side_sets",errmsg,exerrval);
        }
        goto error_ret;
      }

      /* create distribution factor list variable for side set */

      dims[0] = dimid;
      if (ncvardef (exoid, VAR_FACT_SS(cur_num_side_sets+1),
                    nc_flt_code(exoid), 1, dims) == -1)
      {
        if (ncerr == NC_ENAMEINUSE)
        {
          exerrval = ncerr;
          sprintf(errmsg,
         "Error: dist factor list already exists for side set %d in file id %d",
                  side_set_ids[i],exoid);
          ex_err("ex_put_concat_side_sets",errmsg,exerrval);
        }
        else
        {
          exerrval = ncerr;
          sprintf(errmsg,
       "Error: failed to create dist factor list for side set %d in file id %d",
                  side_set_ids[i],exoid);
          ex_err("ex_put_concat_side_sets",errmsg,exerrval);
        }
        goto error_ret;            /* exit define mode and return */
      }
    } /* end define dist factors */
  }

/* leave define mode  */

  if (ncendef (exoid) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to complete definition in file id %d",
            exoid);
    ex_err("ex_put_concat_side_sets",errmsg,exerrval);
    return (EX_FATAL);
  }

/* Next, fill out side set ids array */

  /* first get id of side set ids array variable */

  if ((varid = ncvarid (exoid, VAR_SS_IDS)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to locate side set ids array in file id %d",
            exoid);
    ex_err("ex_put_concat_side_sets",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* then, write out side set id list */

/* this contortion is necessary because netCDF is expecting nclongs; fortunately
   it's necessary only when ints and nclongs aren't the same size */

  start[0] = 0;
  count[0] = num_side_sets;

  if (sizeof(int) == sizeof(nclong)) {
     iresult = ncvarput (exoid, varid, start, count, side_set_ids);
  } else {
     lptr = itol (side_set_ids, num_side_sets);
     iresult = ncvarput (exoid, varid, start, count, lptr);
     free(lptr);
  }

  if (iresult == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to store side set id array in file id %d",
            exoid);
    ex_err("ex_put_concat_side_sets",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* If the side_sets_elem_index is passed in as a NULL pointer, then
   *  the user only wants us to define the sidesets and not populate
   *  the data structures.
   */
  if (side_sets_elem_index == 0)
    return(EX_NOERR);
  
  /* Now, use ExodusII call to store side sets */
  for (i=0; i<num_side_sets; i++)
  {

    if (num_elem_per_set[i] == 0) /* Is this a NULL side set? */
      continue; /* Do not create anything for NULL side sets! */

    if (ex_comp_ws(exoid) == sizeof(float))
    {
      flt_dist_fact = side_sets_dist_fact;
      if (ex_put_side_set(exoid, side_set_ids[i], 
                         &(side_sets_elem_list[side_sets_elem_index[i]]),
                         &(side_sets_side_list[side_sets_elem_index[i]])) == -1)
         return(EX_FATAL); /* error will be reported by subroutine */
      if (num_dist_per_set[i] > 0)      /* store dist factors if required */
      {
        if (ex_put_side_set_dist_fact(exoid, side_set_ids[i],
                         &(flt_dist_fact[side_sets_dist_index[i]])) == -1)
        {
          sprintf(errmsg,
               "Error: failed to store side set %d dist factors for file id %d",
                  side_set_ids[i],exoid);
          /* use error val from exodusII routine */
          ex_err("ex_put_concat_side_sets",errmsg,exerrval);
          return (EX_FATAL);
        }
      }
    }
    else if (ex_comp_ws(exoid) == sizeof(double))
    {
      dbl_dist_fact = side_sets_dist_fact;
      if (ex_put_side_set(exoid, side_set_ids[i], 
                         &(side_sets_elem_list[side_sets_elem_index[i]]),
                         &(side_sets_side_list[side_sets_elem_index[i]])) == -1)
        return(EX_FATAL); /* error will be reported by subroutine */
      if (num_dist_per_set[i] > 0)             /* only store if they exist */
      {
        if (ex_put_side_set_dist_fact(exoid, side_set_ids[i],
                        &(dbl_dist_fact[side_sets_dist_index[i]])) == -1)
        {
          sprintf(errmsg,
               "Error: failed to store side set %d dist factors for file id %d",
                  side_set_ids[i],exoid);
          /* use error val from exodusII routine */
          ex_err("ex_put_concat_side_sets",errmsg,exerrval);
          return (EX_FATAL);
        }
      }
    }
    else
    {
      /* unknown floating point word size */
      exerrval = EX_BADPARAM;
      sprintf(errmsg,
             "Error: unsupported floating point word size %d for file id %d",
              ex_comp_ws(exoid), exoid);
      ex_err("ex_put_concat_side_sets", 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_concat_side_sets",errmsg,exerrval);
  }
  return (EX_FATAL);
}
Exemplo n.º 14
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : CreateBloodCDF
@INPUT      :
@OUTPUT     :
@RETURNS    :
@DESCRIPTION:
@METHOD     :
@GLOBALS    :
@CALLS      :
@CREATED    :
@MODIFIED   :
---------------------------------------------------------------------------- */
int CreateBloodCDF (char name[], header *cnt_header)
{
    int file_CDF;
    int dim_id[1];
    int parent_id;
    int sample_start_id;
    int sample_stop_id;
    int sample_length_id;
    int count_start_id;
    int count_length_id;
    int counts_id;
    int empty_weight_id;
    int full_weight_id;
    int corrected_activity_id;
    int activity_id;

    file_CDF = nccreate (name, NC_CLOBBER);
    if (file_CDF == MI_ERROR)
    {
        return (file_CDF);
    }


    /* Create the dimension */

    dim_id[0] = ncdimdef (file_CDF, "sample", (long)cnt_header->num_samples);

    /* Create the variables */

    sample_start_id  = ncvardef (file_CDF, MIsamplestart, NC_DOUBLE, 1, dim_id);
    sample_stop_id   = ncvardef (file_CDF, MIsamplestop, NC_DOUBLE, 1, dim_id);
    sample_length_id = ncvardef (file_CDF, MIsamplelength, NC_DOUBLE, 1, dim_id);
    count_start_id   = ncvardef (file_CDF, MIcountstart, NC_DOUBLE, 1, dim_id);
    count_length_id  = ncvardef (file_CDF, MIcountlength, NC_DOUBLE, 1, dim_id);
    counts_id        = ncvardef (file_CDF, MIcounts, NC_DOUBLE, 1, dim_id);
    empty_weight_id  = ncvardef (file_CDF, MIemptyweight, NC_DOUBLE, 1, dim_id);
    full_weight_id   = ncvardef (file_CDF, MIfullweight, NC_DOUBLE, 1, dim_id);
    corrected_activity_id = ncvardef (file_CDF, MIcorrectedactivity, NC_DOUBLE,
                                      1, dim_id);
    activity_id      = ncvardef (file_CDF, MIactivity, NC_DOUBLE, 1, dim_id);

    /* Create variable attributes */

    miattputstr (file_CDF, sample_start_id, "units", "seconds");
    miattputstr (file_CDF, sample_stop_id, "units", "seconds");
    miattputstr (file_CDF, sample_length_id, "units", "seconds");
    miattputstr (file_CDF, count_start_id, "units", "seconds");
    miattputstr (file_CDF, count_length_id, "units", "seconds");
    miattputstr (file_CDF, counts_id, "units", "counts");
    miattputstr (file_CDF, empty_weight_id, "units", "grams");
    miattputstr (file_CDF, full_weight_id, "units", "grams");
    miattputstr (file_CDF, corrected_activity_id, "units", "Bq/gram");
    miattputstr (file_CDF, activity_id, "units", "Bq");

    /* Make a root for the blood analysis info */

    parent_id = ncvardef (file_CDF, MIbloodroot, NC_LONG, 0, NULL);
    (void) miattputstr (file_CDF, parent_id, MIbloodname,
                        cnt_header->patient_name);
    (void) ncattput (file_CDF, parent_id, MIbloodrunnumber, NC_LONG, 1,
                     &(cnt_header->run_number));
    (void) miattputstr (file_CDF, parent_id, MIbloodstarttime,
                        cnt_header->start_time);
    (void) miattputstr (file_CDF, parent_id, MIblooddate, cnt_header->date);
    (void) miattputstr (file_CDF, parent_id, MIbloodisotope,
                        cnt_header->isotope);
    (void) miattputstr (file_CDF, parent_id, MIbloodstudytype,
                        cnt_header->study_type);
    (void) miattputint (file_CDF, parent_id, MIbloodbackground,
                        cnt_header->background);
    (void) miattputstr (file_CDF, parent_id, MIbloodcomplete, "false");

    /* Set up the hierarchy */

    (void) miadd_child (file_CDF, parent_id, sample_start_id);
    (void) miadd_child (file_CDF, parent_id, sample_stop_id);
    (void) miadd_child (file_CDF, parent_id, sample_length_id);
    (void) miadd_child (file_CDF, parent_id, count_start_id);
    (void) miadd_child (file_CDF, parent_id, count_length_id);
    (void) miadd_child (file_CDF, parent_id, counts_id);
    (void) miadd_child (file_CDF, parent_id, empty_weight_id);
    (void) miadd_child (file_CDF, parent_id, full_weight_id);
    (void) miadd_child (file_CDF, parent_id, corrected_activity_id);
    (void) miadd_child (file_CDF, parent_id, activity_id);

    /* End definition mode */

    ncendef (file_CDF);

    return (file_CDF);
}
Exemplo n.º 15
0
int ex_put_prop_names (int   exoid,
                       int   obj_type,
                       int   num_props,
                       char **prop_names)
{
   int i, propid, dimid, dims[1];
   char name[MAX_VAR_NAME_LENGTH+1];
   long vals[1];

   char errmsg[MAX_ERR_LENGTH];

   exerrval  = 0; /* clear error code */

/* determine what type of object (element block, node set, or side set) */

   switch (obj_type){
     case EX_ELEM_BLOCK:
       strcpy (name, DIM_NUM_EL_BLK);
       break;
     case EX_NODE_SET:
       strcpy (name, DIM_NUM_NS);
       break;
     case EX_SIDE_SET:
       strcpy (name, DIM_NUM_SS);
       break;
     case EX_ELEM_MAP:
       strcpy (name, DIM_NUM_EM);
       break;
     case EX_NODE_MAP:
       strcpy (name, DIM_NUM_NM);
       break;
     default:
       exerrval = EX_BADPARAM;
       sprintf(errmsg, "Error: object type %d not supported; file id %d",
         obj_type, exoid);
       ex_err("ex_put_prop_names",errmsg,exerrval);
       return(EX_FATAL);
   }

/* inquire id of previously defined dimension (number of objects) */

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

   ncsetfill(exoid, NC_FILL); /* fill with zeros per routine spec */
/* put netcdf file into define mode  */

   if (ncredef (exoid) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid);
     ex_err("ex_put_prop_names",errmsg,exerrval);
     return (EX_FATAL);
   }

/* define num_props variables; we postpend the netcdf variable name with  */
/* a counter starting at 2 because "xx_prop1" is reserved for the id array*/

   dims[0] = dimid;

   for (i=0; i<num_props; i++)
   {
     switch (obj_type){
       case EX_ELEM_BLOCK:
         strcpy (name, VAR_EB_PROP(i+2));
         break;
       case EX_NODE_SET:
         strcpy (name, VAR_NS_PROP(i+2));
         break;
       case EX_SIDE_SET:
         strcpy (name, VAR_SS_PROP(i+2));
         break;
       case EX_ELEM_MAP:
         strcpy (name, VAR_EM_PROP(i+2));
         break;
       case EX_NODE_MAP:
         strcpy (name, VAR_NM_PROP(i+2));
         break;
       default:
         exerrval = EX_BADPARAM;
         sprintf(errmsg, "Error: object type %d not supported; file id %d",
           obj_type, exoid);
         ex_err("ex_put_prop_names",errmsg,exerrval);
         goto error_ret;        /* Exit define mode and return */
     }

     if ((propid = ncvardef (exoid, name, NC_LONG, 1, dims)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
          "Error: failed to create property array variable in file id %d",
               exoid);
       ex_err("ex_put_prop_names",errmsg,exerrval);
       goto error_ret;  /* Exit define mode and return */
     }

     vals[0] = 0; /* fill value */
     /*   create attribute to cause variable to fill with zeros per routine spec */
     if ((ncattput (exoid, propid, _FillValue, NC_LONG, 1, vals)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
           "Error: failed to create property name fill attribute in file id %d",
               exoid);
       ex_err("ex_put_prop_names",errmsg,exerrval);
       goto error_ret;  /* Exit define mode and return */
     }

/*   store property name as attribute of property array variable */

     if ((ncattput (exoid, propid, ATT_PROP_NAME, NC_CHAR, 
                    strlen(prop_names[i])+1, prop_names[i])) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to store property name %s in file id %d",
               prop_names[i],exoid);
       ex_err("ex_put_prop_names",errmsg,exerrval);
       goto error_ret;  /* Exit define mode and return */
     }

   }

/* leave define mode  */

   if (ncendef (exoid) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
       "Error: failed to leave define mode in file id %d", 
        exoid);
     ex_err("ex_put_prop_names",errmsg,exerrval);
     return (EX_FATAL);
   }

   ncsetfill(exoid, NC_NOFILL); /* default: turn off fill */
   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_prop_names",errmsg,exerrval);
       }
       return (EX_FATAL);
}
Exemplo n.º 16
0
int ex_put_sset_var (int   exoid,
                     int   time_step,
                     int   sset_var_index,
                     int   sset_id,
                     int   num_faces_this_sset,
                     const void *sset_var_vals)
{
  int varid, dimid,time_dim, numelbdim, dims[2], sset_id_ndx;
  long num_ssets, num_sset_var, start[2], count[2];
  nclong *sset_var_tab;
  char errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

  /* Determine index of sset_id in VAR_SS_ID array */
  sset_id_ndx = ex_id_lkup(exoid,VAR_SS_IDS,sset_id);
  if (exerrval != 0) 
  {
    if (exerrval == EX_NULLENTITY)
    {
      sprintf(errmsg,
              "Warning: no variables allowed for NULL sideset %d in file id %d",
              sset_id,exoid);
      ex_err("ex_put_sset_var",errmsg,EX_MSG);
      return (EX_WARN);
    }
    else
    {
    sprintf(errmsg,
        "Error: failed to locate sideset id %d in %s array in file id %d",
            sset_id, VAR_SS_IDS, exoid);
    ex_err("ex_put_sset_var",errmsg,exerrval);
    return (EX_FATAL);
    }
  }

  if ((varid = ncvarid (exoid,
                        VAR_SS_VAR(sset_var_index,sset_id_ndx))) == -1)
  {
    if (ncerr == NC_ENOTVAR) /* variable doesn't exist, create it! */
    {

/*    inquire previously defined dimensions */

      /* check for the existance of an sideset variable truth table */
      if ((varid = ncvarid (exoid, VAR_SSET_TAB)) != -1)
      {
        /* find out number of sidesets and sideset variables */
        if ((dimid = ncdimid (exoid, DIM_NUM_SS)) == -1)
        {
          exerrval = ncerr;
          sprintf(errmsg,
               "Error: failed to locate number of sidesets in file id %d",
                  exoid);
          ex_err("ex_put_sset_var",errmsg,exerrval);
          return (EX_FATAL);
        }

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

        if ((dimid = ncdimid (exoid, DIM_NUM_SSET_VAR)) == -1)
        {
          exerrval = EX_BADPARAM;
          sprintf(errmsg,
               "Error: no sideset variables stored in file id %d",
                  exoid);
          ex_err("ex_put_sset_var",errmsg,exerrval);
          return (EX_FATAL);
        }

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

        if (!(sset_var_tab = malloc(num_ssets*num_sset_var*sizeof(nclong))))
        {
          exerrval = EX_MEMFAIL;
          sprintf(errmsg,
                 "Error: failed to allocate memory for sideset variable truth table in file id %d",
                  exoid);
          ex_err("ex_put_sset_var",errmsg,exerrval);
          return (EX_FATAL);
        }

        /*   read in the sideset variable truth table */

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

        count[0] = num_ssets;
        count[1] = num_sset_var;

        if (ncvarget (exoid, varid, start, count, sset_var_tab) == -1)
        {
          exerrval = ncerr;
          sprintf(errmsg,
                 "Error: failed to get truth table from file id %d", exoid);
          ex_err("ex_put_sset_var",errmsg,exerrval);
          return (EX_FATAL);
        }

        if(sset_var_tab[num_sset_var*(sset_id_ndx-1)+sset_var_index-1] 
           == 0L)
        {
          free(sset_var_tab);
          exerrval = EX_BADPARAM;
          sprintf(errmsg,
              "Error: Invalid sideset variable %d, sideset %d in file id %d",
                  sset_var_index, sset_id, exoid);
          ex_err("ex_put_sset_var",errmsg,exerrval);
          return (EX_FATAL);
        }
        free(sset_var_tab);
      }

      if ((time_dim = ncdimid (exoid, DIM_TIME)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
               "Error: failed to locate time dimension in file id %d", exoid);
        ex_err("ex_put_sset_var",errmsg,exerrval);
        goto error_ret;         /* exit define mode and return */
      }

      if ((numelbdim=ncdimid(exoid, DIM_NUM_SIDE_SS(sset_id_ndx))) == -1)
      {
        if (ncerr == NC_EBADDIM)
        {
          exerrval = ncerr;
          sprintf(errmsg,
      "Error: number of faces in sideset %d not defined in file id %d",
                  sset_id, exoid);
          ex_err("ex_put_sset_var",errmsg,exerrval);
        }
        else
        {
          exerrval = ncerr;
          sprintf(errmsg,
 "Error: failed to locate number of sides in sideset %d in file id %d",
                  sset_id, exoid);
          ex_err("ex_put_sset_var",errmsg,exerrval);
        }
        goto error_ret;
      }

/*    variable doesn't exist so put file into define mode  */

      if (ncredef (exoid) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
               "Error: failed to put file id %d into define mode", exoid);
        ex_err("ex_put_sset_var",errmsg,exerrval);
        return (EX_FATAL);
      }


/*    define netCDF variable to store sideset variable values */

      dims[0] = time_dim;
      dims[1] = numelbdim;
      if ((varid = ncvardef(exoid,VAR_SS_VAR(sset_var_index,sset_id_ndx),
                            nc_flt_code(exoid), 2, dims)) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
               "Error: failed to define sideset variable %d in file id %d",
                sset_var_index,exoid);
        ex_err("ex_put_sset_var",errmsg,exerrval);
        goto error_ret;
      }


/*    leave define mode  */

      if (ncendef (exoid) == -1)
      {
        exerrval = ncerr;
        sprintf(errmsg,
       "Error: failed to complete sideset variable %s definition to file id %d",
                VAR_SS_VAR(sset_var_index,sset_id_ndx), exoid);
        ex_err("ex_put_sset_var",errmsg,exerrval);
        return (EX_FATAL);
      }
    }
    else
    {
      exerrval = ncerr;
      sprintf(errmsg,
             "Error: failed to locate sideset variable %s in file id %d",
              VAR_SS_VAR(sset_var_index,sset_id_ndx),exoid);
      ex_err("ex_put_sset_var",errmsg,exerrval);
      return (EX_FATAL);
    }
  }

/* store sideset variable values */

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

  count[0] = 1;
  count[1] = num_faces_this_sset;

  if (ncvarput (exoid, varid, start, count, 
                ex_conv_array(exoid,WRITE_CONVERT,sset_var_vals,
                num_faces_this_sset)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to store sideset variable %d in file id %d", 
            sset_var_index,exoid);
    ex_err("ex_put_sset_var",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_sset_var",errmsg,exerrval);
  }
  return (EX_FATAL);
}
Exemplo n.º 17
0
int ex_put_prop (int   exoid,
                 int   obj_type,
                 int   obj_id,
                 const char *prop_name,
                 int   value)
{
   int found = FALSE;
   int num_props, i, dimid, propid, dims[1];
   long start[1]; 
   nclong ldum;
   char name[MAX_VAR_NAME_LENGTH+1];
   char obj_stype[MAX_VAR_NAME_LENGTH+1];
   char obj_vtype[MAX_VAR_NAME_LENGTH+1];
   char tmpstr[MAX_VAR_NAME_LENGTH+1];
   char dim_name[MAX_VAR_NAME_LENGTH+1];

   char errmsg[MAX_ERR_LENGTH];

   exerrval  = 0; /* clear error code */

/* check if property has already been created */

   num_props = ex_get_num_props(exoid, obj_type);

   switch (obj_type)
   {
     case EX_ELEM_BLOCK:
       strcpy (obj_vtype, VAR_ID_EL_BLK);
       strcpy (obj_stype, "element block");
       break;
     case EX_NODE_SET:
       strcpy (obj_vtype, VAR_NS_IDS);
       strcpy (obj_stype, "node set");
       break;
     case EX_SIDE_SET:
       strcpy (obj_vtype, VAR_SS_IDS);
       strcpy (obj_stype, "side set");
       break;
     case EX_ELEM_MAP:
       strcpy (obj_vtype, VAR_EM_PROP(1));
       strcpy (obj_stype, "element map");
       break;
     case EX_NODE_MAP:
       strcpy (obj_vtype, VAR_NM_PROP(1));
       strcpy (obj_stype, "node map");
       break;
     default:
       exerrval = EX_BADPARAM;
       sprintf(errmsg, "Error: object type %d not supported; file id %d",
               obj_type, exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       return(EX_FATAL);
   }

   if (num_props > 1)   /* any properties other than the default 1? */
   {

   for (i=1; i<=num_props; i++)
   {
     switch (obj_type)
     {
       case EX_ELEM_BLOCK:
         strcpy (name, VAR_EB_PROP(i));
         break;
       case EX_NODE_SET:
         strcpy (name, VAR_NS_PROP(i));
         break;
       case EX_SIDE_SET:
         strcpy (name, VAR_SS_PROP(i));
         break;
       case EX_ELEM_MAP:
         strcpy (name, VAR_EM_PROP(i));
         break;
       case EX_NODE_MAP:
         strcpy (name, VAR_NM_PROP(i));
         break;
       default:
         exerrval = EX_BADPARAM;
         sprintf(errmsg, "Error: object type %d not supported; file id %d",
                 obj_type, exoid);
         ex_err("ex_put_prop",errmsg,exerrval);
         return(EX_FATAL);
     }

     if ((propid = ncvarid (exoid, name)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
          "Error: failed to get property array id in file id %d",
               exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       return (EX_FATAL);
     }

/*   compare stored attribute name with passed property name   */

     if ((ncattget (exoid, propid, ATT_PROP_NAME, tmpstr)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to get property name in file id %d", exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       return (EX_FATAL);
     }

     if (strcmp(tmpstr, prop_name) == 0) 
     {
       found = TRUE;
       break;
     }
   }
   }

/* if property array has not been created, create it */

   if (!found)
   {
/* put netcdf file into define mode  */

     if (ncredef (exoid) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       return (EX_FATAL);
     }

/*   create a variable with a name xx_prop#, where # is the new number   */
/*   of the property                                                     */

     switch (obj_type){
       case EX_ELEM_BLOCK:
         strcpy (name, VAR_EB_PROP(num_props+1));
         strcpy (dim_name, DIM_NUM_EL_BLK);
         break;
       case EX_NODE_SET:
         strcpy (name, VAR_NS_PROP(num_props+1));
         strcpy (dim_name, DIM_NUM_NS);
         break;
       case EX_SIDE_SET:
         strcpy (name, VAR_SS_PROP(num_props+1));
         strcpy (dim_name, DIM_NUM_SS);
         break;
       case EX_ELEM_MAP:
         strcpy (name, VAR_EM_PROP(num_props+1));
         strcpy (dim_name, DIM_NUM_EM);
         break;
       case EX_NODE_MAP:
         strcpy (name, VAR_NM_PROP(num_props+1));
         strcpy (dim_name, DIM_NUM_NM);
         break;
       default:
         exerrval = EX_BADPARAM;
         sprintf(errmsg, "Error: object type %d not supported; file id %d",
           obj_type, exoid);
         ex_err("ex_put_prop",errmsg,exerrval);
         goto error_ret;        /* Exit define mode and return */
     }

/*   inquire id of previously defined dimension (number of objects) */

     if ((dimid = ncdimid (exoid, dim_name)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
       "Error: failed to locate number of objects in file id %d",
                exoid);
       ex_err("ex_put_prop",errmsg, exerrval);
       goto error_ret;  /* Exit define mode and return */
     }

     dims[0] = dimid;
     ncsetfill(exoid, NC_FILL); /* fill with zeros per routine spec */

     if ((propid = ncvardef (exoid, name, NC_LONG, 1, dims)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
          "Error: failed to create property array variable in file id %d",
               exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       goto error_ret;  /* Exit define mode and return */
     }
     ncsetfill(exoid, NC_NOFILL); /* default: nofill */

/*   store property name as attribute of property array variable */

     if ((ncattput (exoid, propid, ATT_PROP_NAME, NC_CHAR,
                    strlen(prop_name)+1, prop_name)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to store property name %s in file id %d",
               prop_name,exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       goto error_ret;  /* Exit define mode and return */
     }

/* leave define mode  */

     if (ncendef (exoid) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to leave define mode in file id %d",
          exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       return (EX_FATAL);
     }

   }

/* find index into property array using obj_id; put value in property */
/* array at proper index; ex_id_lkup returns an index that is 1-based,*/
/* but netcdf expects 0-based arrays so subtract 1                    */

   /* special case: property name ID - check for duplicate ID assignment */
   if (strcmp("ID",prop_name) == 0)
   {
     start[0] = ex_id_lkup (exoid, obj_vtype, value);
     if (exerrval != EX_LOOKUPFAIL)   /* found the id */
     {
       exerrval = EX_BADPARAM;
       sprintf(errmsg,
              "Warning: attempt to assign duplicate %s ID %d in file id %d",
               obj_stype, value, exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       return (EX_WARN);
     }
   }

   start[0] = ex_id_lkup (exoid, obj_vtype, obj_id);
   if (exerrval != 0) 
   {
     if (exerrval == EX_NULLENTITY)
     {
       sprintf(errmsg,
              "Warning: no properties allowed for NULL %s id %d in file id %d",
               obj_stype, obj_id,exoid);
       ex_err("ex_put_prop",errmsg,EX_MSG);
       return (EX_WARN);
     }
     else
     {

       exerrval = ncerr;
       sprintf(errmsg,
            "Error: failed to find value %d in %s property array in file id %d",
               obj_id, obj_stype, exoid);
       ex_err("ex_put_prop",errmsg,exerrval);
       return (EX_FATAL);
     }
   }

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

   ldum = (nclong)value;
   if (ncvarput1 (exoid, propid, start, &ldum) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to store property value in file id %d",
             exoid);
     ex_err("ex_put_prop",errmsg,exerrval);
     return (EX_FATAL);
   }

   return (EX_NOERR);

/* Fatal error: exit definition mode and return */
error_ret:
  ncsetfill(exoid, NC_NOFILL); /* default: nofill */

  if (ncendef (exoid) == -1)     /* exit define mode */
  {
    sprintf(errmsg,
           "Error: failed to complete definition for file id %d",
            exoid);
    ex_err("ex_put_prop",errmsg,exerrval);
  }
  return (EX_FATAL);
}
Exemplo n.º 18
0
/* -------------------- end of local defines -------------------- */
int ex_put_coordinate_frames( int exoid, int nframes, const int cf_ids[], 
                              void* pt_coordinates, const char* tags)
{
  int dim, dim9;                   /* dimension id for nframes, nframes*9 */
  char errmsg[MAX_ERR_LENGTH];     /* buffer for error messages      */
  int exerrval;                    /* returned error value           */
  int varcoords;                   /* variable id for the coordinates */
  int varids;                      /* variable id for the frame ids  */
  int vartags;                     /* variable id for the frame tags */
  long int start=0;                /* start value for varputs        */
  long int count=nframes;          /* number vars to put in varput   */
  long int count9=nframes*9;       /* ditto, but for coordinates     */
  int i;                           /* general indices */
  void* pt_c=0;                    /* pointer to size converted array */



  if ( exoid < 0 )
    return exoid;
  if ( nframes == 0 ) /* write nothing */
    return (EX_NOERR);
  if ( nframes<0 )
    return 1;
  assert( cf_ids!=0 );
  assert( pt_coordinates !=0 );
  assert( tags != 0 );

  /* make the definitions */
  /* go into define mode. define num_frames, num_frames9 */
  if (ncredef (exoid) == -1){
    exerrval = ncerr;
    sprintf(errmsg,"Error: failed to place file id %d into define mode",
            exoid);
    ex_err(PROCNAME,errmsg,exerrval);
    return (EX_FATAL);
  }

  if ( (dim=ncdimdef (exoid, NUM_CFRAMES, nframes)) == -1  ||
       (dim9=ncdimdef (exoid, NUM_CFRAME9, nframes*9))== -1 ){
    exerrval = ncerr;
    sprintf(errmsg,
         "Error: failed to define number of coordinate frames in file id %d",
            exoid);
    ex_err(PROCNAME,errmsg,exerrval);
    goto error_ret;
  }
 

  /* define the variables. coordinates, tags and ids */
  if ((varcoords=ncvardef (exoid, FRAME_COORDS,
                 nc_flt_code(exoid), 1, &dim9)) == -1  ||
      (varids=ncvardef (exoid, FRAME_IDS,NC_INT, 1, &dim)) == -1 ||
      (vartags=ncvardef(exoid, FRAME_TAGS,NC_CHAR,1,&dim)) == -1 ) {
    exerrval = ncerr;
    sprintf(errmsg,
            "Error:  failed to define coordinate frames in file id %d",
            exoid);
    ex_err(PROCNAME,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 coordinate frame definition in file id %d", 
         exoid);
    ex_err(PROCNAME,errmsg,exerrval);
    return (EX_FATAL);
  }

  /* check variables consistency */
  exerrval = EX_NOERR;
  for (i=0;i<nframes;i++)
    if ( strchr("RrCcSs",tags[i])==0 ){
      sprintf(errmsg,"Warning: Unrecognized coordinate frame tag: '%c'.",
              tags[i]);
      exerrval=2;
      ex_err(PROCNAME,errmsg,exerrval);
    }
  /* could also check vectors. Leave this up to the application */

  /* put the variables into the file */
  pt_c = ex_conv_array(exoid,RTN_ADDRESS,pt_coordinates,count9);
  if (  ncvarput (exoid, vartags, &start, &count, tags) == -1 ||
        ncvarput (exoid, varids, &start, &count, cf_ids) == -1  ||
        ncvarput (exoid, varcoords, &start, &count9, pt_c )==-1 ){
    exerrval = ncerr;
    sprintf(errmsg,
            "Error: failed writing frame data in file id %d",exoid);
    ex_err(PROCNAME,errmsg,exerrval);
    return (EX_FATAL);
  }



  return (EX_NOERR);


 
error_ret:
  if (ncendef (exoid) == -1)     /* exit define mode */
    {
      sprintf(errmsg,
              "Error: failed to complete frame definition for file id %d",
              exoid);
      ex_err(PROCNAME,errmsg,exerrval);
    }
  return (EX_FATAL);


}
Exemplo n.º 19
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : setup_variables
@INPUT      : inmincid - id of input minc file (MI_ERROR if no file)
              mincid - id of output minc file
              volume_info - volume information
              arg_string - string giving argument list
@OUTPUT     : (nothing)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to set up variables in the output minc file
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : August 26, 1993 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
static void setup_variables(int inmincid, int mincid, 
                            Volume_Info *volume_info, 
                            char *arg_string)
{
   int dim[MAX_VAR_DIMS], ndims, idim, varid;
   int excluded_vars[10], nexcluded;

   /* Create the dimensions */
   ndims = NUMBER_OF_DIMENSIONS;
   dim[0] = ncdimdef(mincid, volume_info->dimension_names[0], 
                     volume_info->nslices);
   dim[1] = ncdimdef(mincid, volume_info->dimension_names[1], 
                     volume_info->nrows);
   dim[2] = ncdimdef(mincid, volume_info->dimension_names[2], 
                     volume_info->ncolumns);

   /* If an input file is provided, copy all header info from that file except
      image, image-max, image-min */
   if (inmincid != MI_ERROR) {

      /* Look for the image variable and the image-max/min variables so that
         we can exclude them from the copy */
      nexcluded = 0;
      excluded_vars[nexcluded] = ncvarid(inmincid, MIimage);
      if (excluded_vars[nexcluded] != MI_ERROR) nexcluded++;
      excluded_vars[nexcluded] = ncvarid(inmincid, MIimagemax);
      if (excluded_vars[nexcluded] != MI_ERROR) nexcluded++;
      excluded_vars[nexcluded] = ncvarid(inmincid, MIimagemin);
      if (excluded_vars[nexcluded] != MI_ERROR) nexcluded++;

      /* Copy the variable definitions */
      (void) micopy_all_var_defs(inmincid, mincid, nexcluded, excluded_vars);

   }

   /* Set up the dimension variables. If the variable doesn't exist, create
      it (either no input file or variable did not exist in it). If the
      dimensions are not standard, then no variable is created. */

   for (idim=0; idim < ndims; idim++) {
      ncopts = 0;
      varid = ncvarid(mincid, volume_info->dimension_names[idim]);
      if (varid == MI_ERROR) {
         varid = micreate_std_variable(mincid, 
                                       volume_info->dimension_names[idim],
                                       NC_INT, 0, NULL);
      }
      ncopts = NC_OPTS_VAL;
      if (varid != MI_ERROR) {
         (void) miattputdbl(mincid, varid, MIstep, 
                            volume_info->step[idim]);
         (void) miattputdbl(mincid, varid, MIstart, 
                            volume_info->start[idim]);
      }
   }
   
   /* Create the image, image-max and image-min variables */
   setup_image_variables(inmincid, mincid, ndims, dim);

   /* Add the time stamp to the history */
   update_history(mincid, arg_string);

   /* Put the file in data mode */
   (void) ncendef(mincid);

   /* Copy over variable values */
   if (inmincid != MI_ERROR) {
      (void) micopy_all_var_values(inmincid, mincid,
                                   nexcluded, excluded_vars);
   }

}
Exemplo n.º 20
0
int ex_put_all_var_param (int   exoid,
			  int   num_g,
			  int   num_n,
			  int   num_e,
			  int  *elem_var_tab,
			  int   num_m,
			  int  *nset_var_tab,
			  int   num_s,
			  int  *sset_var_tab)
{
  int in_define = 0;
  int time_dim, num_nod_dim, dimid, iresult;
  long num_elem_blk, num_nset, num_sset;
  int numelblkdim, numelvardim, numnsetdim, nsetvardim, numssetdim, ssetvardim;
  int i;

  int eblk_varid, nset_varid, sset_varid;
  
  int *eblk_ids = 0;
  int *nset_ids = 0;
  int *sset_ids = 0;

  nclong *eblk_stat = 0;
  nclong *nset_stat = 0;
  nclong *sset_stat = 0;
  
  int dims[3];
  char errmsg[MAX_ERR_LENGTH];
  const char* routine = "ex_put_all_var_param";

  exerrval = 0; /* clear error code */

  /* inquire previously defined dimensions  */

  if ((time_dim = ncdimid (exoid, DIM_TIME)) == -1) {
    exerrval = ncerr;
    sprintf(errmsg,
	    "Error: failed to locate time dimension in file id %d", exoid);
    ex_err("ex_put_all_var_param",errmsg,exerrval);
    goto error_ret;
  }

  if ((num_nod_dim = ncdimid (exoid, DIM_NUM_NODES)) == -1) {
    if (num_n > 0) {
      exerrval = ncerr;
      sprintf(errmsg,
	      "Error: failed to locate number of nodes in file id %d",
	      exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;
    }
  }

  /* Check this now so we can use it later without checking for errors */
  if (ncdimid (exoid, DIM_STR) < 0) {
    exerrval = ncerr;
    sprintf(errmsg,
	    "Error: failed to get string length in file id %d",exoid);
    ex_err("ex_put_all_var_param",errmsg,exerrval);
    goto error_ret;
  }

  if (num_e > 0) {
    numelblkdim = ex_get_dimension(exoid, DIM_NUM_EL_BLK, "element blocks", &num_elem_blk, routine);
    if (numelblkdim == -1)
      goto error_ret;
    
    /* get element block IDs */
    if (!(eblk_ids = static_cast<int*>(malloc(num_elem_blk*sizeof(int))))) {
      exerrval = EX_MEMFAIL;
      sprintf(errmsg,
	      "Error: failed to allocate memory for element block id array for file id %d",
	      exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;
    }
    ex_get_elem_blk_ids (exoid, eblk_ids);

    /* Get element block status array for later use (allocates memory) */
    eblk_stat = get_status_array(exoid, num_elem_blk, VAR_STAT_EL_BLK, "element block");
    if (eblk_stat == NULL) {
      goto error_ret;
    }
  }

  if (num_m > 0) {
    numnsetdim = ex_get_dimension(exoid, DIM_NUM_NS, "nodesets", &num_nset, routine);
    if (numnsetdim == -1)
      goto error_ret;
    
    /* get nodeset IDs */
    if (!(nset_ids = static_cast<int*>(malloc(num_nset*sizeof(int))))) {
      exerrval = EX_MEMFAIL;
      sprintf(errmsg,
	      "Error: failed to allocate memory for nodeset id array for file id %d",
	      exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;
    }
    ex_get_node_set_ids (exoid, nset_ids);

    /* Get nodeset status array for later use (allocates memory) */
    nset_stat = get_status_array(exoid, num_nset, VAR_NS_STAT, "nodeset");
    if (nset_stat == NULL) {
      goto error_ret;
    }
  }

  if (num_s > 0) {
    numssetdim = ex_get_dimension(exoid, DIM_NUM_SS, "sidesets", &num_sset, routine);
    if (numssetdim == -1)
      goto error_ret;
    
    /* get sideset IDs */
    if (!(sset_ids = static_cast<int*>(malloc(num_sset*sizeof(int))))) {
      exerrval = EX_MEMFAIL;
      sprintf(errmsg,
	      "Error: failed to allocate memory for sideset id array for file id %d",
	      exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;
    }
    ex_get_side_set_ids (exoid, sset_ids);

    /* Get sideset status array for later use (allocates memory) */
    sset_stat = get_status_array(exoid, num_sset, VAR_SS_STAT, "sideset");
    if (sset_stat == NULL) {
      goto error_ret;
    }
  }

  /* put file into define mode  */
  if (ncredef (exoid) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to put file id %d into define mode", exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;
    }
  in_define = 1;

  /* define dimensions and variables */

  if (num_g > 0) 
    {
      dimid = define_dimension(exoid, DIM_NUM_GLO_VAR, num_g, "global");
      if (dimid == -1) goto error_ret;

      
      dims[0] = time_dim;
      dims[1] = dimid;
      if ((ncvardef (exoid, VAR_GLO_VAR, 
                     nc_flt_code(exoid), 2, dims)) == -1)
        {
          exerrval = ncerr;
          sprintf(errmsg,
                  "Error: failed to define global variables in file id %d",
                  exoid);
          ex_err("ex_put_all_var_param",errmsg,exerrval);
          goto error_ret;          /* exit define mode and return */
        }

      /* Now define global variable name variable */
      if (define_variable_name_variable(exoid, VAR_NAME_GLO_VAR, dimid, "global") == -1)
	goto error_ret;
    }

  if (num_n > 0) 
    {
      /*
       * There are two ways to store the nodal variables. The old way *
       * was a blob (#times,#vars,#nodes), but that was exceeding the
       * netcdf maximum dataset size for large models. The new way is
       * to store #vars separate datasets each of size (#times,#nodes)
       *
       * We want this routine to be capable of storing both formats
       * based on some external flag.  Since the storage format of the
       * coordinates have also been changed, we key off of their
       * storage type to decide which method to use for nodal
       * variables. If the variable 'coord' is defined, then store old
       * way; otherwise store new.
       */
      dimid = define_dimension(exoid, DIM_NUM_NOD_VAR, num_n, "nodal");
      if (dimid == -1) goto error_ret;

      if (ex_large_model(exoid) == 0) { /* Old way */
        dims[0] = time_dim;
        dims[1] = dimid;
        dims[2] = num_nod_dim;
        if ((ncvardef (exoid, VAR_NOD_VAR,
                       nc_flt_code(exoid), 3, dims)) == -1)
          {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: failed to define nodal variables in file id %d",
                    exoid);
            ex_err("ex_put_all_var_param",errmsg,exerrval);
            goto error_ret;          /* exit define mode and return */
          }
      } else { /* Store new way */
        for (i = 1; i <= num_n; i++) {
          dims[0] = time_dim;
          dims[1] = num_nod_dim;
          if ((ncvardef (exoid, VAR_NOD_VAR_NEW(i),
                         nc_flt_code(exoid), 2, dims)) == -1)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define nodal variable %d in file id %d",
                      i, exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
              goto error_ret;          /* exit define mode and return */
            }
        }
      }

      /* Now define nodal variable name variable */
      if (define_variable_name_variable(exoid, VAR_NAME_NOD_VAR, dimid, "nodal") == -1)
	goto error_ret;
    }

  if (num_e > 0) {
    numelvardim = define_dimension(exoid, DIM_NUM_ELE_VAR, num_e, "element");
    if (numelvardim == -1) goto error_ret;

    /* Now define element variable name variable */
    if (define_variable_name_variable(exoid, VAR_NAME_ELE_VAR, numelvardim, "element") == -1)
      goto error_ret;

    if (define_truth_table('e', exoid, num_elem_blk, num_e, elem_var_tab, eblk_stat, eblk_ids, "element block") == -1)
      goto error_ret;

    eblk_stat = static_cast<nclong*>(safe_free (eblk_stat));
    eblk_ids  = static_cast<int*>(   safe_free (eblk_ids));

    /* create a variable array in which to store the element variable truth
     * table
     */

    dims[0] = numelblkdim;
    dims[1] = numelvardim;

    if ((eblk_varid = ncvardef (exoid, VAR_ELEM_TAB, NC_LONG, 2, dims)) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
	      "Error: failed to define element variable truth table in file id %d",
	      exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;          /* exit define mode and return */
    }

  }

  if (num_m > 0) {
    nsetvardim = define_dimension(exoid, DIM_NUM_NSET_VAR, num_m, "nodeset");
    if (nsetvardim == -1) goto error_ret;

    /* Now define nodeset variable name variable */
    if (define_variable_name_variable(exoid, VAR_NAME_NSET_VAR, nsetvardim, "nodeset") == -1)
      goto error_ret;

    if (define_truth_table('m', exoid, num_nset, num_m, nset_var_tab, nset_stat, nset_ids, "nodeset") == -1)
      goto error_ret;

    nset_stat = static_cast<nclong*>(safe_free (nset_stat));
    nset_ids  = static_cast<int*>(safe_free (nset_ids));

    /* create a variable array in which to store the truth table
     */

    dims[0] = numnsetdim;
    dims[1] = nsetvardim;

    if ((nset_varid = ncvardef (exoid, VAR_NSET_TAB, NC_LONG, 2, dims)) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
	      "Error: failed to define nodeset variable truth table in file id %d",
	      exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;          /* exit define mode and return */
    }
  }

  if (num_s > 0) {
    ssetvardim = define_dimension(exoid, DIM_NUM_SSET_VAR, num_s, "sideset");
    if (ssetvardim == -1) goto error_ret;

    /* Now define sideset variable name variable */
    if (define_variable_name_variable(exoid, VAR_NAME_SSET_VAR, ssetvardim, "sideset") == -1)
      goto error_ret;

    if (define_truth_table('s', exoid, num_sset, num_s, sset_var_tab, sset_stat, sset_ids, "sideset") == -1)
      goto error_ret;
      
    sset_stat = static_cast<nclong*>(safe_free (sset_stat));
    sset_ids  = static_cast<int*>(safe_free (sset_ids));

    /* create a variable array in which to store the truth table
     */

    dims[0] = numssetdim;
    dims[1] = ssetvardim;

    if ((sset_varid = ncvardef (exoid, VAR_SSET_TAB, NC_LONG, 2, dims)) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
	      "Error: failed to define sideset variable truth table in file id %d",
	      exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;          /* exit define mode and return */
    }
  }

  /* leave define mode  */

  in_define = 0;
  if (ncendef (exoid) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to complete definition in file id %d",
              exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      goto error_ret;
    }

  /* write out the variable truth tables */
  if (num_e > 0) {
    iresult = put_truth_table(exoid, num_elem_blk, num_e, eblk_varid, elem_var_tab, "element");
    if (iresult == -1) goto error_ret;
  }

  if (num_m > 0) {
    iresult = put_truth_table(exoid, num_nset, num_m, nset_varid, nset_var_tab, "nodeset");
    if (iresult == -1) goto error_ret;
  }

  if (num_s > 0) {
    iresult = put_truth_table(exoid, num_sset, num_s, sset_varid, sset_var_tab, "sideset");
    if (iresult == -1) goto error_ret;
  }

  return(EX_NOERR);
  
  /* Fatal error: exit definition mode and return */
 error_ret:
  if (in_define == 1) {
    if (ncendef (exoid) == -1)     /* exit define mode */
      {
	sprintf(errmsg,
		"Error: failed to complete definition for file id %d",
		exoid);
	ex_err("ex_put_all_var_param",errmsg,exerrval);
      }
  }
  safe_free(eblk_ids);
  safe_free(nset_ids);
  safe_free(sset_ids);

  safe_free(eblk_stat);
  safe_free(nset_stat);
  safe_free(sset_stat);
  return(EX_FATAL);
}
Exemplo n.º 21
0
int main(int argc, char **argv)
{
   int icv, cdfid, img, max, min, dimvar;
   static int dim[MAX_VAR_DIMS];
   static struct { long len; char *name;} diminfo[] = {
      { 7, MIzspace }, 
      { 9, MIyspace }, 
      { 2, MIxspace }
   };
/*   static struct { long len; char *name;} diminfo[]=
      {3, MIzspace, 4, MIyspace, 5, MIxspace}; */
   static int numdims=sizeof(diminfo)/sizeof(diminfo[0]);
   static long coord[]={0,0,0};
   static long count[]={3,4,5};
   double dvalue;
   short int ivalue[]={
      111, 113, 115, 117, 119,
      121, 123, 125, 127, 129,
      131, 133, 135, 137, 139,
      141, 143, 145, 147, 149,
      211, 213, 215, 217, 219,
      221, 223, 225, 227, 229,
      231, 233, 235, 237, 239,
      241, 243, 245, 247, 249,
      311, 313, 315, 317, 319,
      321, 323, 325, 327, 329,
      331, 333, 335, 337, 339,
      341, 343, 345, 347, 349
   };
   int i, j, k;
   int cflag = 0;

#if MINC2
   if (argc == 2 && !strcmp(argv[1], "-2")) {
       cflag = MI2_CREATE_V2;
   }
#endif /* MINC2 */

   icv=miicv_create();
   miicv_setint(icv, MI_ICV_XDIM_DIR, MI_ICV_NEGATIVE);
   miicv_setint(icv, MI_ICV_YDIM_DIR, MI_ICV_NEGATIVE);
   miicv_setint(icv, MI_ICV_ZDIM_DIR, MI_ICV_NEGATIVE);
   miicv_setint(icv, MI_ICV_NUM_IMGDIMS, 3);
   miicv_setint(icv, MI_ICV_DIM_SIZE+0, 5);
   miicv_setint(icv, MI_ICV_DIM_SIZE+1, 4);
   miicv_setint(icv, MI_ICV_DIM_SIZE+2, 3);
   miicv_setint(icv, MI_ICV_DO_DIM_CONV, TRUE);
   miicv_setint(icv, MI_ICV_KEEP_ASPECT, FALSE);
   miicv_setint(icv, MI_ICV_DO_NORM, TRUE);
   cdfid=micreate("test.mnc", NC_CLOBBER | cflag);
   for (i=0; i<numdims; i++) {
      dim[i]=ncdimdef(cdfid, diminfo[i].name, diminfo[i].len);
      dimvar=micreate_std_variable(cdfid, diminfo[i].name, NC_DOUBLE,
                                   0, &dim[i]);
      miattputdbl(cdfid, dimvar, MIstep, 0.8);
      miattputdbl(cdfid, dimvar, MIstart, 22.0);
   }
   
   img=micreate_std_variable(cdfid, MIimage, NC_SHORT,
                             numdims, dim);
   max=micreate_std_variable(cdfid, MIimagemax, NC_DOUBLE, 1, dim);
   min=micreate_std_variable(cdfid, MIimagemin, NC_DOUBLE, 1, dim);
   ncendef(cdfid);
   for (i=0; i<diminfo[0].len; i++) {
      dvalue = 200;
      coord[0]=i;
      ncvarput1(cdfid, max, coord, &dvalue);
      dvalue = -dvalue;
      ncvarput1(cdfid, min, coord, &dvalue);
   }
   coord[0]=0;
   miicv_attach(icv, cdfid, img);
   miicv_inqdbl(icv, MI_ICV_ADIM_START, &dvalue);
   printf("adim start = %g", dvalue);
   miicv_inqdbl(icv, MI_ICV_ADIM_STEP, &dvalue);
   printf(", step = %g\n", dvalue);
   miicv_inqdbl(icv, MI_ICV_BDIM_START, &dvalue);
   printf("bdim start = %g", dvalue);
   miicv_inqdbl(icv, MI_ICV_BDIM_STEP, &dvalue);
   printf(", step = %g\n", dvalue);
   miicv_inqdbl(icv, MI_ICV_NORM_MAX, &dvalue);
   printf("norm : max = %g", dvalue);
   miicv_inqdbl(icv, MI_ICV_NORM_MIN, &dvalue);
   printf(", min = %g\n", dvalue);
   miicv_put(icv, coord, count, ivalue);
   miicv_get(icv, coord, count, ivalue);
   for (i=0; i<3; i++) {
      for (j=0; j<4; j++) {
         for (k=0; k<5; k++) {
            printf("%5d",ivalue[i*20+j*5+k]);
         }
         printf("\n");
      }
   }
   miclose(cdfid);
   miicv_free(icv);
   
   return 0;
}
Exemplo n.º 22
0
MNCAPI int
minc_save_start(char *path,     /* Path to the file */
                int filetype,   /* Date type as stored in the file */
                long ct,        /* Total length of time axis, in voxels */
                long cz,        /* Total length of Z axis, in voxels */
                long cy,        /* Total length of Y axis, in voxels */
                long cx,        /* Total length of X axis, in voxels */
                double dt,      /* Sample width along time axis, in seconds */
                double dz,      /* Sample width along Z axis, in mm */
                double dy,      /* Sample width along Y axis, in mm */
                double dx,      /* Sample width along X axis, in mm */
                void *infoptr,  /* Opaque file structure information */
                const char *history) /* New history information */
{
    int fd;                     /* MINC file descriptor */
    int dim_id[MI_S_NDIMS];     /* netCDF dimension ID array */
    int var_ndims;              /* Number of dimensions per variable */
    int var_dims[MI_S_NDIMS];   /* Dimension ID's per variable */
    int i, j;                   /* Generic loop counters */
    int old_ncopts;             /* For supressing fatal error messages */
    struct file_info *p_file;   /* For accessing the file structure */
    struct var_info *p_var;
    struct att_info *p_att;
    int var_id;                 /* netCDF ID for variable */
    char *signstr;
    nc_type nctype;

    old_ncopts =get_ncopts();
    set_ncopts(0);

    fd = micreate(path, NC_CLOBBER);

    set_ncopts(old_ncopts);
    
    if (fd < 0) {
        return (MINC_STATUS_ERROR);
    }

    if (ct > 0) {
        dim_id[MI_S_T] = ncdimdef(fd, MItime, ct);
        micreate_std_variable(fd, MItime, NC_INT, 0, NULL);
        if (dt > 0.0) {
            miattputdbl(fd, ncvarid(fd, MItime), MIstep, dt);
        }
    }
    else {
        dim_id[MI_S_T] = -1;
    }

    if (cz > 0) {
        dim_id[MI_S_Z] = ncdimdef(fd, MIzspace, cz);
        micreate_std_variable(fd, MIzspace, NC_INT, 0, NULL);
        if (dz > 0.0) {
            miattputdbl(fd, ncvarid(fd, MIzspace), MIstep, dz);
        }
    }
    else {
        dim_id[MI_S_Z] = -1;
    }

    if (cy > 0) {
        dim_id[MI_S_Y] = ncdimdef(fd, MIyspace, cy);
        micreate_std_variable(fd, MIyspace, NC_INT, 0, NULL);
        if (dy > 0.0) {
            miattputdbl(fd, ncvarid(fd, MIyspace), MIstep, dy);
        }
    }
    else {
        return (MINC_STATUS_ERROR); /* Must define Y */
    }

    if (cx > 0) {
        dim_id[MI_S_X] = ncdimdef(fd, MIxspace, cx);
        micreate_std_variable(fd, MIxspace, NC_INT, 0, NULL);
        if (dx > 0.0) {
            miattputdbl(fd, ncvarid(fd, MIxspace), MIstep, dx);
        }
    }
    else {
        return (MINC_STATUS_ERROR); /* Must define X */
    }
    

    /* The var_dims[] array is the array of actual dimension ID's to
     * be used in defining the image variables.  Here I set it up by
     * copying all valid dimension ID's from the dim_id[] array.
     */
    var_ndims = 0;
    for (i = 0; i < MI_S_NDIMS; i++) {
        if (dim_id[i] >= 0) {
            var_dims[var_ndims] = dim_id[i];
            var_ndims++;
        }
    }

    minc_simple_to_nc_type(filetype, &nctype, &signstr);

    /* Create the image variable with the standard
     * dimension order, and the same type as the template
     * file.
     */
    micreate_std_variable(fd, MIimage, nctype, var_ndims, var_dims);
    micreate_std_variable(fd, MIimagemin, NC_DOUBLE, 1, var_dims);
    micreate_std_variable(fd, MIimagemax, NC_DOUBLE, 1, var_dims);

    /* Copy information from the infoptr to the output. 
     */
    if ((p_file = infoptr) != NULL) {
        old_ncopts =get_ncopts();
        set_ncopts(0);

        for (i = 0; i < p_file->file_natts; i++) {
            p_att = &p_file->file_atts[i];
            if (strcmp(p_att->att_name, "ident") != 0) {
                ncattput(fd, NC_GLOBAL, p_att->att_name, p_att->att_type, 
                         p_att->att_len, p_att->att_val);
            }
        }

        for (i = 0; i < p_file->file_nvars; i++) {
            p_var = &p_file->file_vars[i];

            if ((var_id = ncvarid(fd, p_var->var_name)) < 0) {
                var_id = ncvardef(fd, p_var->var_name, p_var->var_type, 
                                  p_var->var_ndims, p_var->var_dims);
            }
            
            for (j = 0; j < p_var->var_natts; j++) {
                p_att = &p_var->var_atts[j];
                ncattput(fd, var_id, p_att->att_name, p_att->att_type,
                         p_att->att_len, p_att->att_val);
            }
        }
        
        set_ncopts(old_ncopts);
    }

    miattputstr(fd, ncvarid(fd, MIimage), MIcomplete, MI_FALSE);
    miattputstr(fd, ncvarid(fd, MIimage), MIsigntype, signstr);

    miappend_history(fd, history);
    ncendef(fd);
    return fd;
}
Exemplo n.º 23
0
int ex_put_node_set_param (int exoid,
                           int node_set_id,
                           int num_nodes_in_set,
                           int num_dist_in_set)
{
   int dimid, varid, dims[1];
   long start[1]; 
   nclong ldum;
   long num_node_sets;
   int cur_num_node_sets, node_set_stat;
   char *cdum;
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

   cdum = 0;

/* first check if any node sets are specified */

   if ((dimid = (ncdimid (exoid, DIM_NUM_NS))) == -1 )
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: no node sets specified in file id %d",
             exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }

/* Check for duplicate node set id entry */
   ex_id_lkup(exoid,VAR_NS_IDS,node_set_id);
   if (exerrval != EX_LOOKUPFAIL)   /* found the node set id */
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: node set %d already defined in file id %d",
             node_set_id,exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return(EX_FATAL);
   }

/* Get number of node sets initialized for this file */
   if ((ncdiminq (exoid,dimid,cdum,&num_node_sets)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to get number of node sets in file id %d",
             exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }


/* Keep track of the total number of node sets defined using a counter stored
   in a linked list keyed by exoid.
   NOTE: ex_get_file_item  is used to find the number of node sets
         for a specific file and returns that value.
*/
   cur_num_node_sets=ex_get_file_item(exoid, &ns_ctr_list );
   if (cur_num_node_sets >= num_node_sets)
   {
     exerrval = EX_FATAL;
     sprintf(errmsg,
           "Error: exceeded number of node sets (%ld) specified in file id %d",
             num_node_sets,exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }

/*   NOTE: ex_inc_file_item  is used to find the number of node sets
         for a specific file and returns that value incremented. */

   cur_num_node_sets=ex_inc_file_item(exoid, &ns_ctr_list );

/* write out information to previously defined variable */

   /* first get id of variable */

   if ((varid = ncvarid (exoid, VAR_NS_IDS)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to locate node set ids in file id %d",
             exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }

   /* then, write out node set id */

   start[0] = cur_num_node_sets;

   ldum = (nclong)node_set_id;
   if (ncvarput1 (exoid, varid, start, &ldum) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to store node set id %d in file id %d",
             node_set_id,exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }

   if (num_nodes_in_set == 0) /* Is this a NULL side set? */
     node_set_stat = 0; /* change node set status to NULL */
   else
     node_set_stat = 1; /* change node set status to TRUE */

   if ((varid = ncvarid (exoid, VAR_NS_STAT)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
           "Error: failed to locate node set status in file id %d", exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }

   ldum = (nclong)node_set_stat;
   if (ncvarput1 (exoid, varid, start, &ldum) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to store node set %d status to file id %d",
             node_set_id, exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }

   if (num_nodes_in_set == 0) /* Is this a NULL side set? */
   {
     return(EX_NOERR);
   }


/* put netcdf file into define mode  */

   if (ncredef (exoid) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to put file id %d into define mode",
             exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }


   if ((dimid = ncdimdef (exoid, DIM_NUM_NOD_NS(cur_num_node_sets+1),
                 (long)num_nodes_in_set)) == -1)
   {
     if (ncerr == NC_ENAMEINUSE)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: node set %d size already defined in file id %d",
               node_set_id,exoid);
       ex_err("ex_put_node_set_param",errmsg,exerrval);
     }
     else
     {
       exerrval = ncerr;
       sprintf(errmsg,
             "Error: failed to define number of nodes for set %d in file id %d",
               node_set_id,exoid);
       ex_err("ex_put_node_set_param",errmsg,exerrval);
     }
     goto error_ret;
   }


/* create variable array in which to store the node set node list */

   dims[0] = dimid;

   if (ncvardef(exoid, VAR_NODE_NS(cur_num_node_sets+1), NC_LONG,1,dims) == -1)
   {
     if (ncerr == NC_ENAMEINUSE)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: node set %d node list already defined in file id %d",
               node_set_id,exoid);
       ex_err("ex_put_node_set_param",errmsg,exerrval);
     }
     else
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to create node set %d node list in file id %d",
               node_set_id,exoid);
       ex_err("ex_put_node_set_param",errmsg,exerrval);
     }
     goto error_ret;          /* exit define mode and return */
   }

/* Create distribution factors variable if required */

   if (num_dist_in_set > 0)
   {

     /* num_dist_in_set should equal num_nodes_in_set */
     if (num_dist_in_set != num_nodes_in_set)
     {
       exerrval = EX_FATAL;
       sprintf(errmsg, 
"Error: # dist fact (%d) not equal to # nodes (%d) in node set %d file id %d",
                 num_dist_in_set, num_nodes_in_set, node_set_id,exoid);
       ex_err("ex_put_node_set_param",errmsg,exerrval);
       goto error_ret;          /* exit define mode and return */
     }
     else
     {
/* create variable for distribution factors */

       if (ncvardef (exoid, VAR_FACT_NS(cur_num_node_sets+1),
                       nc_flt_code(exoid), 1, dims) == -1)
       {
         if (ncerr == NC_ENAMEINUSE)
         {
           exerrval = ncerr;
           sprintf(errmsg,
                  "Error: node set %d dist factors already exist in file id %d",
                   node_set_id,exoid);
           ex_err("ex_put_node_set_param",errmsg,exerrval);
         }
         else
         {
           exerrval = ncerr;
           sprintf(errmsg,
               "Error: failed to create node set %d dist factors in file id %d",
                node_set_id,exoid);
           ex_err("ex_put_node_set_param",errmsg,exerrval);
         }
         goto error_ret;            /* exit define mode and return */
       }
     }
   }

/* leave define mode  */

   if (ncendef (exoid) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to complete definition in file id %d",
             exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }


   return (EX_NOERR);


/* Fatal error: exit definition mode and return */
error_ret:
   if (ncendef (exoid) == -1)     /* exit define mode */
   {
     sprintf(errmsg,
            "Error: failed to complete definition for file id %d",
             exoid);
     ex_err("ex_put_node_set_param",errmsg,exerrval);
   }
   return (EX_FATAL);
}
Exemplo n.º 24
0
/* Open an input netCDF file and get some information about it */
int process_ncinfile(char *ncname, unsigned char appendnc, int outncfid,
                     char *outncname, int *nfiles, unsigned char verbose)
{
    struct fileinfo ncinfile;  /* Information about an input netCDF file */
    int nfiles2;  /* Number of files in the decomposed domain */
    int d, v, n;  /* Loop variables */
    int dimid;  /* ID of a dimension */
    int decomp[4];  /* "domain_decomposition" information */
    char attname[MAX_NC_NAME];  /* Name of a global or variable attribute */
    unsigned char ncinfileerror=0;  /* Were there any file errors? */

    /* Open an input netCDF file; return if not openable - possibly IEEE */
    if ((ncinfile.ncfid=ncopen(ncname,NC_NOWRITE))==(-1)) return(2);

    /* Determine the number of files in the decomposed domain */
    if (ncattget(ncinfile.ncfid,NC_GLOBAL,"NumFilesInSet",
                 (void *)&nfiles2)==(-1))
    {
        if (*nfiles==1)
        {
            fprintf(stderr,"Error: missing the \"NumFilesInSet\" global attribute!\n");
            return(1);
        }
        else if (*nfiles==(-1))
        {
            fprintf(stderr,"Warning: missing the \"NumFilesInSet\" global attribute.\n");
        }
    }
    *nfiles=nfiles2;

    /* Get some general information about the input netCDF file */
    if (ncinquire(ncinfile.ncfid,&(ncinfile.ndims),&(ncinfile.nvars),
                  &(ncinfile.ngatts),&(ncinfile.recdim))==(-1))
    {
        fprintf(stderr,"Error: cannot read the file's metadata!\n");
        ncclose(ncinfile.ncfid);
        return(1);
    }

    /* Get some information about the dimensions */
    for (d=0; d < ncinfile.ndims; d++)
    {
        if ((ncdiminq(ncinfile.ncfid,d,ncinfile.dimname[d],
                      &(ncinfile.dimsize[d])))==(-1))
        {
            fprintf(stderr,"Error: cannot read dimension #%d's metadata!\n",d);
            ncclose(ncinfile.ncfid);
            return(1);
        }
        ncinfile.dimfullsize[d]=ncinfile.dimsize[d];
        ncinfile.dimstart[d]=1;
        ncinfile.dimend[d]=(-1);
    }

    /* Get some information about the variables */
    for (v=0; v < ncinfile.nvars; v++)
    {
        if ((ncvarinq(ncinfile.ncfid,v,ncinfile.varname[v],
                      &(ncinfile.datatype[v]),&(ncinfile.varndims[v]),
                      ncinfile.vardim[v],&(ncinfile.natts[v])))==(-1))
        {
            fprintf(stderr,"Error: cannot read variable #%d's metadata!\n",v);
            ncclose(ncinfile.ncfid);
            return(1);
        }

        /* If the variable is also a dimension then get decomposition info */
        if ((dimid=ncdimid(ncinfile.ncfid,ncinfile.varname[v]))!=(-1))
        {
            if (ncattget(ncinfile.ncfid,v,"domain_decomposition",
                         (void *)decomp)!=(-1))
            {
                ncinfile.dimfullsize[dimid]=decomp[1]-decomp[0]+1;
                ncinfile.dimstart[dimid]=decomp[2]-(decomp[0]-1);
                ncinfile.dimend[dimid]=decomp[3]-(decomp[0]-1);
            }
            else
            {
                ncinfile.dimfullsize[dimid]=ncinfile.dimsize[dimid];
                ncinfile.dimstart[dimid]=1;
                ncinfile.dimend[dimid]=(-1);
            }
        }
    }

#if DEBUG==1
    print_debug(&ncinfile,verbose);
#endif

    /* If the output netCDF file was just created then define its structure */
    if (!appendnc)
    {
#if DEBUG==1
        printf("Creating output netCDF file... \"%s\"\n",outncname);
#endif
        /* Define the dimensions */
        for (d=0; d < ncinfile.ndims; d++)
        {
            if (d==ncinfile.recdim)
                ncdimdef(outncfid,ncinfile.dimname[d],NC_UNLIMITED);
            else ncdimdef(outncfid,ncinfile.dimname[d],ncinfile.dimfullsize[d]);
        }

        /* Define the variables and copy their attributes */
        for (v=0; v < ncinfile.nvars; v++)
        {
            ncvardef(outncfid,ncinfile.varname[v],ncinfile.datatype[v],
                     ncinfile.varndims[v],ncinfile.vardim[v]);
            for (n=0; n < ncinfile.natts[v]; n++)
            {
                ncattname(ncinfile.ncfid,v,n,attname);
                if (!strcmp(attname,"domain_decomposition")) continue;
                else
                {
                    if (ncattcopy(ncinfile.ncfid,v,attname,outncfid,v)==(-1))
                    {
                        fprintf(stderr,"Error: cannot copy variable \"%s\"'s attributes!\n",
                                ncinfile.varname[v]);
                        return(1);
                    }
                }
            }
        }

        /* Copy the global attributes */
        for (n=0; n < ncinfile.ngatts; n++)
        {
            ncattname(ncinfile.ncfid,NC_GLOBAL,n,attname);
            if (!strcmp(attname,"NumFilesInSet")) continue;
            else if (!strcmp(attname,"filename"))
                ncattput(outncfid,NC_GLOBAL,attname,NC_CHAR,strlen(outncname),
                         (void *)outncname);
            else
            {
                if (ncattcopy(ncinfile.ncfid,NC_GLOBAL,attname,outncfid,
                              NC_GLOBAL)==(-1))
                {
                    fprintf(stderr,"Error: cannot copy the file's global attributes!\n");
                    return(1);
                }
            }
        }

        /* Definitions done */
        ncendef(outncfid);
    }

    /* Copy all the data values of the dimensions and variables */
    ncinfileerror=copy_nc_data(&ncinfile,outncfid,appendnc,verbose);

    /* Done */
    ncclose(ncinfile.ncfid);
    return(ncinfileerror);
}
Exemplo n.º 25
0
int ex_put_prop_array (int   exoid,
                       int   obj_type,
                       const char *prop_name,
                       const int  *values)
{
   int num_props, i, propid, dimid, dims[1], iresult;
   int found = FALSE;
   long start[1], count[1], num_obj; 
   nclong *lptr;
   char name[MAX_VAR_NAME_LENGTH+1];
   char tmpstr[MAX_STR_LENGTH+1];
   char obj_stype[MAX_VAR_NAME_LENGTH+1];
   char dim_name[MAX_VAR_NAME_LENGTH+1];

   char errmsg[MAX_ERR_LENGTH];

   exerrval  = 0; /* clear error code */

/* check if property has already been created */

   num_props = ex_get_num_props(exoid, obj_type);

   switch (obj_type)
   {
     case EX_ELEM_BLOCK:
       strcpy (obj_stype, VAR_ID_EL_BLK);
       strcpy (dim_name, DIM_NUM_EL_BLK);
       break;
     case EX_NODE_SET:
       strcpy (obj_stype, VAR_NS_IDS);
       strcpy (dim_name, DIM_NUM_NS);
       break;
     case EX_SIDE_SET:
       strcpy (obj_stype, VAR_SS_IDS);
       strcpy (dim_name, DIM_NUM_SS);
       break;
     case EX_ELEM_MAP:
       strcpy (obj_stype, VAR_EM_PROP(1));
       strcpy (dim_name, DIM_NUM_EM);
       break;
     case EX_NODE_MAP:
       strcpy (obj_stype, VAR_NM_PROP(1));
       strcpy (dim_name, DIM_NUM_NM);
       break;
     default:
       exerrval = EX_BADPARAM;
       sprintf(errmsg, "Error: object type %d not supported; file id %d",
               obj_type, exoid);
       ex_err("ex_put_prop_array",errmsg,exerrval);
       return (EX_FATAL);
   }
/*   inquire id of previously defined dimension (number of objects) */

     if ((dimid = ncdimid (exoid, dim_name)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to locate number of %s objects in file id %d",
               obj_stype, exoid);
       ex_err("ex_put_prop_array",errmsg, exerrval);
       return (EX_FATAL);
     }

/*   get number of objects */

     if (ncdiminq (exoid, dimid, dim_name, &num_obj) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to get number of %s objects in file id %d",
               obj_stype, exoid);
       ex_err("ex_put_prop_array",errmsg, exerrval);
       return (EX_FATAL);
     }



   for (i=1; i<=num_props; i++)
   {
     switch (obj_type){
       case EX_ELEM_BLOCK:
         strcpy (name, VAR_EB_PROP(i));
         break;
       case EX_NODE_SET:
         strcpy (name, VAR_NS_PROP(i));
         break;
       case EX_SIDE_SET:
         strcpy (name, VAR_SS_PROP(i));
         break;
       case EX_ELEM_MAP:
         strcpy (name, VAR_EM_PROP(i));
         break;
       case EX_NODE_MAP:
         strcpy (name, VAR_NM_PROP(i));
         break;
       default:
         exerrval = EX_BADPARAM;
         sprintf(errmsg, "Error: object type %d not supported; file id %d",
           obj_type, exoid);
         ex_err("ex_put_prop_array",errmsg,exerrval);
         return(EX_FATAL);
     }

     if ((propid = ncvarid (exoid, name)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
          "Error: failed to get property array id in file id %d",
               exoid);
       ex_err("ex_put_prop_array",errmsg,exerrval);
       return (EX_FATAL);
     }

/*   compare stored attribute name with passed property name   */

     memset(tmpstr, 0, MAX_STR_LENGTH+1);
     if ((ncattget (exoid, propid, ATT_PROP_NAME, tmpstr)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to get property name in file id %d", exoid);
       ex_err("ex_put_prop_array",errmsg,exerrval);
       return (EX_FATAL);
     }

     if (strcmp(tmpstr, prop_name) == 0) 
     {
       found = TRUE;
       break;
     }
   }

/* if property array has not been created, create it */

   if (!found)
   {
/* put netcdf file into define mode  */

     if (ncredef (exoid) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid);
       ex_err("ex_put_prop_array",errmsg,exerrval);
       return (EX_FATAL);
     }

/*   create a variable with a name xx_prop#, where # is the new number   */
/*   of properties                                                       */

     switch (obj_type){
       case EX_ELEM_BLOCK:
         strcpy (name, VAR_EB_PROP(num_props+1));
         break;
       case EX_NODE_SET:
         strcpy (name, VAR_NS_PROP(num_props+1));
         break;
       case EX_SIDE_SET:
         strcpy (name, VAR_SS_PROP(num_props+1));
         break;
       case EX_ELEM_MAP:
         strcpy (name, VAR_EM_PROP(num_props+1));
         break;
       case EX_NODE_MAP:
         strcpy (name, VAR_NM_PROP(num_props+1));
         break;
       default:
         exerrval = EX_BADPARAM;
         sprintf(errmsg, "Error: object type %d not supported; file id %d",
           obj_type, exoid);
         ex_err("ex_put_prop_array",errmsg,exerrval);
         goto error_ret;        /* Exit define mode and return */
     }

     dims[0] = dimid;
     ncsetfill(exoid, NC_FILL); /* fill with zeros per routine spec */

     if ((propid = ncvardef (exoid, name, NC_LONG, 1, dims)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
          "Error: failed to create property array variable in file id %d",
               exoid);
       ex_err("ex_put_prop_array",errmsg,exerrval);
       goto error_ret;  /* Exit define mode and return */
     }
     ncsetfill(exoid, NC_NOFILL); /* default: nofill */


/*   store property name as attribute of property array variable */

     if ((ncattput (exoid, propid, ATT_PROP_NAME, NC_CHAR,
                    strlen(prop_name)+1, prop_name)) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
              "Error: failed to store property name %s in file id %d",
               prop_name,exoid);
       ex_err("ex_put_prop_array",errmsg,exerrval);
       goto error_ret;  /* Exit define mode and return */
     }

/* leave define mode  */

     if (ncendef (exoid) == -1)
     {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to leave define mode in file id %d",
          exoid);
       ex_err("ex_put_prop_array",errmsg,exerrval);
       return (EX_FATAL);
     }

   }

/* put num_obj values in property array */

/* this contortion is necessary because netCDF is expecting nclongs; fortunately
   it's necessary only when ints and nclongs aren't the same size  */

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

   if (sizeof(int) == sizeof(nclong)) {
      iresult = ncvarput (exoid, propid, start, count, values);
   } else {
      lptr = itol (values, (int)num_obj);
      iresult = ncvarput (exoid, propid, start, count, lptr);
      free(lptr);
   }

   if (iresult == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to store property values in file id %d",
             exoid);
     ex_err("ex_put_prop_array",errmsg,exerrval);
     return (EX_FATAL);
   }


   return (EX_NOERR);

/* Fatal error: exit definition mode and return */
error_ret:
  ncsetfill(exoid, NC_NOFILL); /* default: nofill */
  if (ncendef (exoid) == -1)     /* exit define mode */
  {
    sprintf(errmsg,
           "Error: failed to complete definition for file id %d",
            exoid);
    ex_err("ex_put_prop_array",errmsg,exerrval);
  }
  return (EX_FATAL);
}
Exemplo n.º 26
0
int ex_create (const char *path,
               int   cmode,
               int  *comp_ws,
               int  *io_ws)
{
   int exoid, time_dim, dims[1];
   nclong lio_ws;
   nclong filesiz;
   float vers;
   char errmsg[MAX_ERR_LENGTH];
   char *mode_name;
   int mode = 0;
   
   exerrval = 0; /* clear error code */

   /*
    * See if "large file" mode was specified in a ex_create cmode. If
    * so, then pass the NC_64BIT_OFFSET flag down to netcdf.
    */
   if (cmode & EX_LARGE_MODEL || ex_large_model(-1) == 1) {
     mode |= NC_64BIT_OFFSET;
   }

/*
 * set error handling mode to no messages, non-fatal errors
 */
  ex_opts(exoptval);    /* call required to set ncopts first time through */

   if (cmode & EX_NOCLOBBER) {
     mode |= NC_NOCLOBBER;
     mode_name = "NOCLOBBER";
   } else if (cmode & EX_CLOBBER) {
     mode |= NC_CLOBBER;
     mode_name = "CLOBBER";
   } else {
     exerrval = EX_BADFILEMODE;
     sprintf(errmsg,"Error: invalid file create mode: %d, for file %s",
             cmode,path);
     ex_err("ex_create",errmsg,exerrval);
     return (EX_FATAL);
   }

#ifndef TFLOP
   mode |= NC_SHARE;
#endif
   
   if ((exoid = nccreate (path, mode)) == -1) {
     exerrval = ncerr;
     sprintf(errmsg,
             "Error: file create failed for %s, mode: %s",
             path, mode_name);
     ex_err("ex_create",errmsg,exerrval);
     return (EX_FATAL);
   }

/* turn off automatic filling of netCDF variables
 */

  if (ncsetfill (exoid, NC_NOFILL) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to set nofill mode in file id %d",
            exoid);
    ex_err("ex_create", errmsg, exerrval);
    return (EX_FATAL);
  }

/* initialize floating point size conversion.  since creating new file, 
 * i/o wordsize attribute from file is zero.
 */

   if (ex_conv_ini( exoid, comp_ws, io_ws, 0 ) != EX_NOERR) {
     exerrval = EX_FATAL;
     sprintf(errmsg,
           "Error: failed to init conversion routines in file id %d",
            exoid);
     ex_err("ex_create", errmsg, exerrval);
     return (EX_FATAL);
   }

/* put the EXODUS version number, and i/o floating point word size as
 * netcdf global attributes
 */

/* store Exodus API version # as an attribute */
  vers = (float)(EX_API_VERS);
  if (ncattput (exoid, NC_GLOBAL, ATT_API_VERSION, NC_FLOAT, 1, &vers) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
         "Error: failed to store Exodus II API version attribute in file id %d",
            exoid);
    ex_err("ex_create",errmsg, exerrval);
    return (EX_FATAL);
  }

/* store Exodus file version # as an attribute */
  vers = (float)(EX_VERS);
  if (ncattput (exoid, NC_GLOBAL, ATT_VERSION, NC_FLOAT, 1, &vers) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
        "Error: failed to store Exodus II file version attribute in file id %d",
            exoid);
    ex_err("ex_create",errmsg, exerrval);
    return (EX_FATAL);
  }

/* store Exodus file float word size  as an attribute */
  lio_ws = (nclong)(*io_ws);
  if (ncattput (exoid, NC_GLOBAL, ATT_FLT_WORDSIZE, NC_LONG, 1, &lio_ws) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
 "Error: failed to store Exodus II file float word size attribute in file id %d",
           exoid);
    ex_err("ex_create",errmsg, exerrval);
    return (EX_FATAL);
  }

  /* store Exodus file size (1=large, 0=normal) as an attribute */
  filesiz = (nclong)(((cmode & EX_LARGE_MODEL) != 0) || (ex_large_model(-1) == 1));
  if (ncattput (exoid, NC_GLOBAL, ATT_FILESIZE, NC_LONG, 1, &filesiz) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to store Exodus II file size attribute in file id %d",
              exoid);
      ex_err("ex_create",errmsg, exerrval);
      return (EX_FATAL);
    }
  
  /* define some dimensions and variables
   */
  
  /* create string length dimension */
  if (ncdimdef (exoid, DIM_STR, (MAX_STR_LENGTH+1)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to define string length in file id %d",exoid);
    ex_err("ex_create",errmsg,exerrval);
    return (EX_FATAL);
  }


  /* create line length dimension */
  if (ncdimdef (exoid, DIM_LIN, (MAX_LINE_LENGTH+1)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to define line length in file id %d",exoid);
    ex_err("ex_create",errmsg,exerrval);
    return (EX_FATAL);
  }

  /* create number "4" dimension; must be of type long */
  if (ncdimdef (exoid, DIM_N4, 4L) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to define number \"4\" dimension in file id %d",exoid);
    ex_err("ex_create",errmsg,exerrval);
    return (EX_FATAL);
  }


  if ((time_dim = ncdimdef (exoid, DIM_TIME, NC_UNLIMITED)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to define time dimension in file id %d", exoid);
    ex_err("ex_create",errmsg,exerrval);
    return (EX_FATAL);
  }

  dims[0] = time_dim;
  if ((ncvardef (exoid, VAR_WHOLE_TIME, nc_flt_code(exoid), 1, dims)) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to define whole time step variable in file id %d",
            exoid);
    ex_err("ex_create",errmsg,exerrval);
    return (EX_FATAL);
  }

  if (ncendef (exoid) == -1)
  {
    exerrval = ncerr;
    sprintf(errmsg,
           "Error: failed to complete definition for file id %d", exoid);
    ex_err("ex_create",errmsg,exerrval);
    return (EX_FATAL);
  }

  return (exoid);
}
Exemplo n.º 27
0
int ex_copy (int in_exoid, int out_exoid)
{
   int ndims;                   /* number of dimensions */
   int nvars;                   /* number of variables */
   int ngatts;                  /* number of global attributes */
   int recdimid;                /* id of unlimited dimension */
   int dimid;                   /* dimension id */
   int dim_out_id;              /* dimension id */
   int varid;                   /* variable id */
   int var_out_id;              /* variable id */
   struct ncvar var;            /* variable */
   struct ncatt att;            /* attribute */
   int i, temp;
   long numrec;
   long dim_sz;
   char dim_nm[MAX_NC_NAME];
   int in_large, out_large;
   
   extern int ncopts;

   exerrval = 0; /* clear error code */

   /*
    * Get exodus_large_model setting on both input and output
    * databases so know how to handle coordinates.
    */
   in_large  = ex_large_model(in_exoid);
   out_large = ex_large_model(out_exoid);
   
   /*
    * get number of dimensions, number of variables, number of global
    * atts, and dimension id of unlimited dimension, if any
    */

   ncinquire(in_exoid, &ndims, &nvars, &ngatts, &recdimid);
   ncdiminq (in_exoid, recdimid, (char *) 0, &numrec);

   /* put output file into define mode */

   ncredef(out_exoid);

   /* copy global attributes */

   for (i = 0; i < ngatts; i++) {

      ncattname(in_exoid, NC_GLOBAL, i, att.name);
        
      ncattinq(in_exoid, NC_GLOBAL, att.name, &att.type, &att.len);

      /* if attribute exists in output file, don't overwrite it; compute 
       * word size, I/O word size etc. are global attributes stored when
       * file is created with ex_create;  we don't want to overwrite those
       */

      if (ncattinq (out_exoid, NC_GLOBAL, att.name, &att.type, &att.len) == -1){

        /* The "last_written_time" attribute is a special attribute
           used by the Sierra IO system to determine whether a
           timestep has been fully written to the database in order to
           try to detect a database crash that happens in the middle
           of a database output step. Don't want to copy that attribute.
        */
        if (strcmp(att.name,"last_written_time") != 0) {
          /* attribute doesn't exist in new file so OK to create it */
          ncattcopy (in_exoid,NC_GLOBAL,att.name,out_exoid,NC_GLOBAL);
        }
      }
   }

   /* copy dimensions */

   /* Get the dimension sizes and names */

   for(dimid = 0; dimid < ndims; dimid++){

      ncdiminq(in_exoid,dimid,dim_nm,&dim_sz);

      /* See if the dimension has already been defined */

      temp = ncopts;
      ncopts = 0;
      dim_out_id = ncdimid(out_exoid,dim_nm);
      ncopts = temp;

      /* If the dimension isn't one we specifically don't want 
       * to copy (ie, number of QA or INFO records) and it 
       * hasn't been defined, copy it */

      if ( ( strcmp(dim_nm,DIM_NUM_QA)        != 0) &&
           ( strcmp(dim_nm,DIM_NUM_INFO)      != 0) &&
           ( strcmp(dim_nm,DIM_NUM_NOD_VAR)   != 0) &&
           ( strcmp(dim_nm,DIM_NUM_EDG_VAR)   != 0) &&
           ( strcmp(dim_nm,DIM_NUM_FAC_VAR)   != 0) &&
           ( strcmp(dim_nm,DIM_NUM_ELE_VAR)   != 0) &&
           ( strcmp(dim_nm,DIM_NUM_NSET_VAR)  != 0) &&
           ( strcmp(dim_nm,DIM_NUM_ESET_VAR)  != 0) &&
           ( strcmp(dim_nm,DIM_NUM_FSET_VAR)  != 0) &&
           ( strcmp(dim_nm,DIM_NUM_SSET_VAR)  != 0) &&
           ( strcmp(dim_nm,DIM_NUM_ELSET_VAR) != 0) &&
           ( strcmp(dim_nm,DIM_NUM_GLO_VAR)   != 0) ) {

         if(dim_out_id == -1){
            if(dimid != recdimid){
               dim_out_id=ncdimdef(out_exoid,dim_nm,dim_sz);
            }else{
               dim_out_id=ncdimdef(out_exoid,dim_nm,NC_UNLIMITED);
            } /* end else */
         } /* end if */
      } /* end if */
   } /* end loop over dim */

   /* copy variable definitions and variable attributes */

   for (varid = 0; varid < nvars; varid++) {

      ncvarinq(in_exoid, varid, var.name, &var.type, &var.ndims, 
                      var.dims, &var.natts);

      /* we don't want to copy some variables because there is not a
       * simple way to add to them;
       * QA records, info records and all results variables (nodal
       * element, and global results) are examples
       */

      if ( ( strcmp(var.name,VAR_QA_TITLE) != 0)     &&
           ( strcmp(var.name,VAR_INFO) != 0)         &&
           ( strcmp(var.name,VAR_EBLK_TAB) != 0)     &&
           ( strcmp(var.name,VAR_FBLK_TAB) != 0)     &&
           ( strcmp(var.name,VAR_ELEM_TAB) != 0)     &&
           ( strcmp(var.name,VAR_ELSET_TAB) != 0)     &&
           ( strcmp(var.name,VAR_SSET_TAB) != 0)     &&
           ( strcmp(var.name,VAR_FSET_TAB) != 0)     &&
           ( strcmp(var.name,VAR_ESET_TAB) != 0)     &&
           ( strcmp(var.name,VAR_NSET_TAB) != 0)     &&
           ( strcmp(var.name,VAR_NAME_GLO_VAR) != 0) &&
           ( strcmp(var.name,VAR_GLO_VAR) != 0)      &&
           ( strcmp(var.name,VAR_NAME_NOD_VAR) != 0) &&
           ( strcmp(var.name,VAR_NOD_VAR) != 0)      &&
           ( strcmp(var.name,VAR_NAME_EDG_VAR) != 0) &&
           ( strcmp(var.name,VAR_NAME_FAC_VAR) != 0) &&
           ( strcmp(var.name,VAR_NAME_ELE_VAR) != 0) &&
           ( strcmp(var.name,VAR_NAME_NSET_VAR) != 0)   &&
           ( strcmp(var.name,VAR_NAME_ESET_VAR) != 0)   &&
           ( strcmp(var.name,VAR_NAME_FSET_VAR) != 0)   &&
           ( strcmp(var.name,VAR_NAME_SSET_VAR) != 0)   &&
           ( strcmp(var.name,VAR_NAME_ELSET_VAR) != 0)  &&
           ( strncmp(var.name,"vals_elset_var", 14) != 0) &&
           ( strncmp(var.name,"vals_sset_var",  13) != 0) &&
           ( strncmp(var.name,"vals_fset_var",  13) != 0) &&
           ( strncmp(var.name,"vals_eset_var",  13) != 0) &&
           ( strncmp(var.name,"vals_nset_var",  13) != 0) &&
           ( strncmp(var.name,"vals_nod_var",   12) != 0) &&
           ( strncmp(var.name,"vals_edge_var",  13) != 0) &&
           ( strncmp(var.name,"vals_face_var",  13) != 0) &&
           ( strncmp(var.name,"vals_elem_var",  13) != 0) ) {

        if (strncmp(var.name,VAR_COORD,5) == 0) {
          var_out_id = cpy_coord_def (in_exoid, out_exoid, recdimid, var.name,
                                      in_large, out_large);
        } else {
          var_out_id = cpy_var_def (in_exoid, out_exoid, recdimid, var.name);
        }

         /* copy the variable's attributes */
         (void) cpy_att (in_exoid, out_exoid, varid, var_out_id);

      }
   }

   /* take the output file out of define mode */
   ncendef (out_exoid);

   /* output variable data */

   for (varid = 0; varid < nvars; varid++) {
      ncvarinq(in_exoid, varid, var.name, &var.type, &var.ndims,
                    var.dims, &var.natts);

      /* we don't want to copy some variable values;
       * QA records and info records shouldn't be copied because there
       * isn't an easy way to add to them;
       * the time value array ("time_whole") and any results variables
       * (nodal, elemental, or global) shouldn't be copied 
       */

      if ( ( strcmp(var.name,VAR_QA_TITLE) != 0)        &&
           ( strcmp(var.name,VAR_INFO) != 0)            &&
           ( strcmp(var.name,VAR_EBLK_TAB) != 0)        &&
           ( strcmp(var.name,VAR_FBLK_TAB) != 0)        &&
           ( strcmp(var.name,VAR_ELEM_TAB) != 0)        &&
           ( strcmp(var.name,VAR_ELSET_TAB) != 0)       &&
           ( strcmp(var.name,VAR_SSET_TAB) != 0)        &&
           ( strcmp(var.name,VAR_FSET_TAB) != 0)        &&
           ( strcmp(var.name,VAR_ESET_TAB) != 0)        &&
           ( strcmp(var.name,VAR_NSET_TAB) != 0)        &&
           ( strcmp(var.name,VAR_NAME_GLO_VAR) != 0)    &&
           ( strcmp(var.name,VAR_GLO_VAR) != 0)         &&
           ( strcmp(var.name,VAR_NAME_NOD_VAR) != 0)    &&
           ( strcmp(var.name,VAR_NOD_VAR) != 0)         &&
           ( strcmp(var.name,VAR_NAME_EDG_VAR) != 0)    &&
           ( strcmp(var.name,VAR_NAME_FAC_VAR) != 0)    &&
           ( strcmp(var.name,VAR_NAME_ELE_VAR) != 0)    &&
           ( strcmp(var.name,VAR_NAME_NSET_VAR) != 0)   &&
           ( strcmp(var.name,VAR_NAME_ESET_VAR) != 0)   &&
           ( strcmp(var.name,VAR_NAME_FSET_VAR) != 0)   &&
           ( strcmp(var.name,VAR_NAME_SSET_VAR) != 0)   &&
           ( strcmp(var.name,VAR_NAME_ELSET_VAR) != 0)  &&
           ( strncmp(var.name,"vals_elset_var", 14) != 0)&&
           ( strncmp(var.name,"vals_sset_var", 13) != 0)&&
           ( strncmp(var.name,"vals_fset_var", 13) != 0)&&
           ( strncmp(var.name,"vals_eset_var", 13) != 0)&&
           ( strncmp(var.name,"vals_nset_var", 13) != 0)&&
           ( strncmp(var.name,"vals_nod_var", 12) != 0) &&
           ( strncmp(var.name,"vals_edge_var",13) != 0) &&
           ( strncmp(var.name,"vals_face_var",13) != 0) &&
           ( strncmp(var.name,"vals_elem_var",13) != 0) &&
           ( strcmp(var.name,VAR_WHOLE_TIME) != 0) ) {

        if (strncmp(var.name,VAR_COORD,5) == 0) {
          (void) cpy_coord_val (in_exoid, out_exoid, var.name,
                                in_large, out_large);
        } else {
          (void) cpy_var_val (in_exoid, out_exoid, var.name);
        }

      }
   }

   /* ensure internal data structures are updated */

   /* if number of blocks > 0 */
   update_internal_structs( out_exoid, EX_INQ_EDGE_BLK, &ed_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_FACE_BLK, &fa_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_ELEM_BLK, &eb_ctr_list );

   /* if number of sets > 0 */
   update_internal_structs( out_exoid, EX_INQ_NODE_SETS, &ns_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_EDGE_SETS, &es_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_FACE_SETS, &fs_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_SIDE_SETS, &ss_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_ELEM_SETS, &els_ctr_list );

   /* if number of maps > 0 */
   update_internal_structs( out_exoid, EX_INQ_NODE_MAP, &nm_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_EDGE_MAP, &edm_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_FACE_MAP, &fam_ctr_list );
   update_internal_structs( out_exoid, EX_INQ_ELEM_MAP, &em_ctr_list );

   return(EX_NOERR);
}
Exemplo n.º 28
0
int main(int argc, char **argv)
{
   int icv, cdfid, img, max, min;
   static char *typenm[]={"short", "double"};
   static char *boolnm[] = {"true", "false"};
   static nc_type intypes[] = {NC_SHORT, NC_DOUBLE};
   static int norms[] = {TRUE, FALSE};
   static nc_type outtypes[] = {NC_SHORT, NC_DOUBLE};
   static int maxpresent[] = {TRUE, FALSE};
   static int valpresent[] = {TRUE, FALSE};
   static int dim[MAX_VAR_DIMS];
   static struct { long len; char *name;} diminfo[] = {
      { 3, MIzspace }, 
      { 1, MIyspace }, 
      { 1, MIxspace } 
   };
   static int numdims=sizeof(diminfo)/sizeof(diminfo[0]);
   static long coord[]={0,0,0};
   static long count[]={1,1,1};
   static double max_values[] = {0.4, 0.6, 0.8};
   double dvalue;
   short int ivalue;
   int i, intype, inorm, outtype, imax, ival;
   int cflag = 0;
   char filename[256];

#if MINC2
   if (argc == 2 && !strcmp(argv[1], "-2")) {
       cflag = MI2_CREATE_V2;
   }
#endif /* MINC2 */

   snprintf(filename, sizeof(filename), "test_icv_range-%d.mnc", getpid());

   for (intype=0; intype<MAX_IN_TYPES; intype++) {
      for (inorm=0; inorm<MAX_NORM; inorm++) {
         icv=miicv_create();
         miicv_setint(icv, MI_ICV_TYPE, intypes[intype]);
         miicv_setint(icv, MI_ICV_DO_NORM, norms[inorm]);
         miicv_setdbl(icv, MI_ICV_VALID_MAX, 20000.0);
         miicv_setdbl(icv, MI_ICV_VALID_MIN, 0.0);
         for (outtype=0; outtype<MAX_OUT_TYPES; outtype++) {
            for (imax=0; imax<MAX_MAX; imax++) {
               for (ival=0; ival<MAX_VAL; ival++) {
                  printf(
                     "in : %s, out : %s, norm : %s, imgmax : %s, valid : %s\n",
                         typenm[intype], typenm[outtype], boolnm[inorm],
                         boolnm[imax], boolnm[ival]);
                  cdfid=micreate(filename, NC_CLOBBER | cflag);
                  for (i=0; i<numdims; i++) 
                     dim[i]=ncdimdef(cdfid, diminfo[i].name, diminfo[i].len);
                  img=micreate_std_variable(cdfid, MIimage, outtypes[outtype],
                                            numdims, dim);
                  if (maxpresent[imax]) {
                     max=micreate_std_variable(cdfid, MIimagemax, NC_DOUBLE,
                                               1, dim);
                     min=micreate_std_variable(cdfid, MIimagemin, NC_DOUBLE,
                                               1, dim);
                  }
                  if (valpresent[ival]) {
                     dvalue = 32000;
                     ncattput(cdfid, img, MIvalid_max, NC_DOUBLE, 1, &dvalue);
                     dvalue = 0;
                     ncattput(cdfid, img, MIvalid_min, NC_DOUBLE, 1, &dvalue);
                  }
                  ncendef(cdfid);
                  if (maxpresent[imax]) {
                     for (i=0; i<3; i++) {
                        dvalue = max_values[i];
                        coord[0]=i;
                        ncvarput1(cdfid, max, coord, &dvalue);
                        dvalue = -dvalue;
                        ncvarput1(cdfid, min, coord, &dvalue);
                     }
                     coord[0]=0;
                  }
                  miicv_attach(icv, cdfid, img);
                  if (intypes[intype]==NC_DOUBLE) {
                     dvalue = 0.2;
                     miicv_put(icv, coord, count, &dvalue);
                  }
                  else {
                     ivalue = 12500;
                     miicv_put(icv, coord, count, &ivalue);
                  }
                  dvalue = 0;
                  mivarget1(cdfid, img, coord, NC_DOUBLE, MI_SIGNED, &dvalue);
                  printf("   file value = %g\n", dvalue);
                  if (intypes[intype]==NC_DOUBLE) {
                     miicv_get(icv, coord, count, &dvalue);
                  }
                  else {
                     miicv_get(icv, coord, count, &ivalue);
                     dvalue=ivalue;
                  }
                  printf("   icv value = %g\n", dvalue);
                  miclose(cdfid);
               }
            }
         }
         miicv_free(icv);
      }
   }
   unlink(filename);
   return 0;
}
Exemplo n.º 29
0
Arquivo: wr_dpi.c Projeto: goma/brkfix
int
wr_dpi(Dpi *d,
       char *filename,
       int verbosity)
{
  int err = 0;
  int status = 0;
  int u = 0;			/* short hand for unit... */

  struct Shadow_Identifiers si;

  memset(&si, 0, sizeof(struct Shadow_Identifiers));

  /*
   * From the C interface guide the basic calling sequence is given
   * for the case of adding new dimensions, variables and attributes to
   * an existing netCDF dataset.
   *
   *  nc_open();
   *  nc_redef();
   *    nc_def_dim();
   *    nc_def_var();
   *    nc_put_att();
   *  nc_enddef();
   *  nc_put_var();
   *  nc_close();
   *
   */

  /*
   * Open the file.
   */

#ifdef NETCDF_3
  err = nc_open(filename, NC_WRITE, &u);
  if ( err != NC_NOERR )
    {
      EH(-1, "nc_open() problem.");
    }
#endif
#ifdef NETCDF_2
  err = ncopen(filename, NC_WRITE);
  EH(err, "ncopen() problem.");
  u   = err;
#endif

  /*
   * Go into define mode.
   */

#ifdef NETCDF_3
  err = nc_redef(u);
  if ( err != NC_NOERR )
    {
      EH(-1, "nc_redef() problem.");
    }
#endif
#ifdef NETCDF_2
  err = ncredef(u);
  EH(err, "ncredef() problem.");
#endif

  /*
   * Define each of the netCDF dimensions that will be needed to describe
   * the extent of netCDF variables that are arrays.
   */
 
  define_dimension(u, DIM_LEN_EB_NUM_PRIVATE_ELEMS, 
		   d->len_eb_num_private_elems,
		   &si.len_eb_num_private_elems);

  define_dimension(u, DIM_LEN_ELEM_VAR_TAB_GLOBAL,
		   d->len_elem_var_tab_global,
		   &si.len_elem_var_tab_global);

  define_dimension(u, DIM_LEN_ELEM_ELEM_LIST, /* new e-e */
		   d->len_elem_elem_list,
		   &si.len_elem_elem_list);

  define_dimension(u, DIM_LEN_NODE_DESCRIPTION,
		   d->len_node_description,
		   &si.len_node_description);

  define_dimension(u, DIM_LEN_NS_NODE_LIST,
		   d->len_ns_node_list,
		   &si.len_ns_node_list);

  define_dimension(u, DIM_LEN_NS_DISTFACT_LIST,
		   d->len_ns_distfact_list,
		   &si.len_ns_distfact_list);

  define_dimension(u, DIM_LEN_SS_ELEM_LIST,
		   d->len_ss_elem_list,
		   &si.len_ss_elem_list);

  define_dimension(u, DIM_LEN_SS_DISTFACT_LIST,
		   d->len_ss_distfact_list,
		   &si.len_ss_distfact_list);

  define_dimension(u, DIM_LEN_STRING,
		   d->len_string,
		   &si.len_string);

  define_dimension(u, DIM_LEN_PTR_SET_MEMBERSHIP,
		   d->len_ptr_set_membership,
		   &si.len_ptr_set_membership);

  define_dimension(u, DIM_LEN_SET_MEMBERSHIP,
		   d->len_set_membership,
		   &si.len_set_membership);

  define_dimension(u, DIM_NUM_ELEM_BLOCKS,
		   d->num_elem_blocks,
		   &si.num_elem_blocks);

  define_dimension(u, DIM_NUM_ELEM_BLOCKS_GLOBAL,
		   d->num_elem_blocks_global,
		   &si.num_elem_blocks_global);

  define_dimension(u, DIM_NUM_ELEMS,
		   d->num_elems,
		   &si.num_elems);

  define_dimension(u, DIM_NUM_GLOBAL_NODE_DESCRIPTIONS,
		   d->num_global_node_descriptions,
		   &si.num_global_node_descriptions);

  define_dimension(u, DIM_NUM_NEIGHBORS,
		   d->num_neighbors,
		   &si.num_neighbors);

  define_dimension(u, DIM_NUM_NODE_SETS,
		   d->num_node_sets,
		   &si.num_node_sets);

  define_dimension(u, DIM_NUM_NODE_SETS_GLOBAL,
		   d->num_node_sets_global,
		   &si.num_node_sets_global);

  define_dimension(u, DIM_NUM_NODES,
		   d->num_nodes,
		   &si.num_nodes);

  define_dimension(u, DIM_NUM_PROPS_EB, d->num_props_eb,
		   &si.num_props_eb);

      define_dimension(u, DIM_NUM_PROPS_NS,
		       d->num_props_ns,
		       &si.num_props_ns);

      define_dimension(u, DIM_NUM_PROPS_SS,
		       d->num_props_ss,
		       &si.num_props_ss);

  define_dimension(u, DIM_NUM_SIDE_SETS,
		   d->num_side_sets,
		   &si.num_side_sets);

  define_dimension(u, DIM_NUM_SIDE_SETS_GLOBAL,
		   d->num_side_sets_global,
		   &si.num_side_sets_global);

  define_dimension(u, DIM_NUM_UNIVERSE_NODES,
		   d->num_universe_nodes,
		   &si.num_universe_nodes);

  if (d->num_side_sets_global > 0) {
    define_dimension(u, DIM_LEN_SS_BLOCK_INDEX_GLOBAL,
                     d->num_side_sets_global + 1,
                     &si.len_ss_block_index_global);
    
    define_dimension(u, DIM_LEN_SS_BLOCK_LIST_GLOBAL,
                     d->ss_block_index_global[d->num_side_sets_global],
                     &si.len_ss_block_list_global);
  } else {
      define_dimension(u, DIM_LEN_SS_BLOCK_INDEX_GLOBAL,
                       0,
                       &si.len_ss_block_index_global);
      define_dimension(u, DIM_LEN_SS_BLOCK_LIST_GLOBAL,
                       0,
                       &si.len_ss_block_list_global);
  }
  
  /*
   * Define variables. Arrays only get defined if their respective dimensions
   * are greater than zero.
   *
   * Also, this handy routine uses two arguments for the possibility of
   * up to 2D arrays. Dummy arguments of "-1" are inserted for 1D arrays 
   * or for scalar variables ( zero dimensional arrays).
   */
  
  define_variable(u, VAR_DPI_VERSION_STRING, NC_CHAR, 1,
		  si.len_string, -1,
		  d->len_string, -1,
		  &si.dpi_version_string);

  define_variable(u, VAR_EB_ELEM_TYPE_GLOBAL, NC_CHAR, 2, 
		  si.num_elem_blocks_global, si.len_string,
		  d->num_elem_blocks_global, d->len_string,
		  &si.eb_elem_type_global);

  define_variable(u, VAR_EB_ID_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks_global, -1,
		  d->num_elem_blocks_global, -1,
		  &si.eb_id_global);

  define_variable(u, VAR_EB_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks, -1,
		  d->num_elem_blocks, -1,
		  &si.eb_index_global);

  define_variable(u, VAR_EB_NUM_ATTR_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks_global, -1,
		  d->num_elem_blocks_global, -1,
		  &si.eb_num_attr_global);

  define_variable(u, VAR_EB_NUM_ELEMS_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks_global, -1,
		  d->num_elem_blocks_global, -1,
		  &si.eb_num_elems_global);

  define_variable(u, VAR_EB_NUM_NODES_PER_ELEM_GLOBAL, NC_INT, 1, 
		  si.num_elem_blocks_global, -1,
		  d->num_elem_blocks_global, -1,
		  &si.eb_num_nodes_per_elem_global);

  define_variable(u, VAR_EB_NUM_PRIVATE_ELEMS, NC_INT, 1, 
		  si.num_elem_blocks, -1,
		  d->num_elem_blocks, -1,
		  &si.eb_num_private_elems);

  if ( d->num_props_eb > 1 )	/* Properties are weird, recall. */
    {
      define_variable(u, VAR_EB_PROP_GLOBAL, NC_INT, 2,
		      si.num_props_eb, si.num_elem_blocks_global,
		      d->num_props_eb, d->num_elem_blocks_global,
		      &si.eb_prop_global);
    }

  if ( d->num_elems > 0 )
    {
      define_variable(u, VAR_ELEM_INDEX_GLOBAL, NC_INT, 1,
		      si.num_elems, -1,
		      d->num_elems, -1,
		      &si.elem_index_global);
    }

  if ( d->len_elem_var_tab_global > 0 )
    {
      define_variable(u, VAR_ELEM_VAR_TAB_GLOBAL, NC_INT, 1,
		      si.len_elem_var_tab_global, -1,
		      d->len_elem_var_tab_global, -1,
		      &si.elem_var_tab_global);
    }

  if ( d->len_elem_elem_list > 0 )
    {
      define_variable(u, VAR_ELEM_OWNER, NC_INT, 1,
		      si.num_elems, -1,
		      d->num_elems, -1,
		      &si.elem_owner);

      define_variable(u, VAR_ELEM_ELEM_LIST_GLOBAL, NC_INT, 1,
		      si.len_elem_elem_list, -1,
		      d->len_elem_elem_list, -1,
		      &si.elem_elem_list_global);

      define_variable(u, VAR_ELEM_ELEM_TWST_GLOBAL, NC_INT, 1,
		      si.len_elem_elem_list, -1,
		      d->len_elem_elem_list, -1,
		      &si.elem_elem_twst_global);

      define_variable(u, VAR_ELEM_ELEM_FACE_GLOBAL, NC_INT, 1,
		      si.len_elem_elem_list, -1,
		      d->len_elem_elem_list, -1,
		      &si.elem_elem_face_global);

      define_variable(u, VAR_ELEM_ELEM_PROC_GLOBAL, NC_INT, 1,
		      si.len_elem_elem_list, -1,
		      d->len_elem_elem_list, -1,
		      &si.elem_elem_proc_global);
    }

  define_variable(u, VAR_GLOBAL_NODE_DESCRIPTION, NC_INT, 2, 
		  si.num_global_node_descriptions, si.len_node_description,
		  d->num_global_node_descriptions, d->len_node_description,
		  &si.global_node_description);

  define_variable(u, VAR_MY_NAME, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.my_name);

  define_variable(u, VAR_NEIGHBOR, NC_INT, 1, 
		  si.num_neighbors, -1,
		  d->num_neighbors, -1,
		  &si.neighbor);

  if ( d->num_nodes > 0 )
    {
      define_variable(u, VAR_NODE_INDEX_GLOBAL, NC_INT, 1,
		      si.num_nodes, -1,
		      d->num_nodes, -1,
		      &si.node_index_global);
    }

  define_variable(u, VAR_NS_DISTFACT_INDEX_GLOBAL, NC_INT, 1,
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_distfact_index_global);

  define_variable(u, VAR_NS_DISTFACT_LEN_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.ns_distfact_len_global);

  define_variable(u, VAR_NS_DISTFACT_LIST_INDEX_GLOBAL, NC_INT, 1,
		  si.len_ns_distfact_list, -1,
		  d->len_ns_distfact_list, -1,
		  &si.ns_distfact_list_index_global);

  define_variable(u, VAR_NS_ID_GLOBAL, NC_INT, 1, 
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_id_global);

  define_variable(u, VAR_NS_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_node_sets, -1,
		  d->num_node_sets, -1,
		  &si.ns_index_global);

  define_variable(u, VAR_NS_NODE_INDEX_GLOBAL, NC_INT, 1,
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_node_index_global);


  define_variable(u, VAR_NS_NODE_LEN_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.ns_node_len_global);

  define_variable(u, VAR_NS_NODE_LIST_INDEX_GLOBAL, NC_INT, 1,
		  si.len_ns_node_list, -1,
		  d->len_ns_node_list, -1,
		  &si.ns_node_list_index_global);

  define_variable(u, VAR_NS_NUM_DISTFACTS_GLOBAL, NC_INT, 1, 
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_num_distfacts_global);

  define_variable(u, VAR_NS_NUM_NODES_GLOBAL, NC_INT, 1, 
		  si.num_node_sets_global, -1,
		  d->num_node_sets_global, -1,
		  &si.ns_num_nodes_global);

  if ( d->num_props_ns > 1 )
    {
      define_variable(u, VAR_NS_PROP_GLOBAL, NC_INT, 2, 
		      si.num_props_ns, si.num_node_sets_global,
		      d->num_props_ns, d->num_node_sets_global,
		      &si.ns_prop_global);
    }

  define_variable(u, VAR_NUM_BOUNDARY_NODES, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_boundary_nodes);

  define_variable(u, VAR_NUM_DOFS_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_dofs_global);

  define_variable(u, VAR_NUM_ELEMS_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_elems_global);

  define_variable(u, VAR_NUM_EXTERNAL_NODES, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_external_nodes);

  define_variable(u, VAR_NUM_INTERNAL_NODES, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_internal_nodes);

  define_variable(u, VAR_NUM_NODES_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.num_nodes_global);

  define_variable(u, VAR_PTR_SET_MEMBERSHIP, NC_INT, 1, 
		  si.len_ptr_set_membership, -1,
		  d->len_ptr_set_membership, -1,
		  &si.ptr_set_membership);

  define_variable(u, VAR_SET_MEMBERSHIP, NC_INT, 1, 
		  si.len_set_membership, -1,
		  d->len_set_membership, -1,
		  &si.set_membership);

  define_variable(u, VAR_SS_DISTFACT_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_distfact_index_global);

  define_variable(u, VAR_SS_DISTFACT_LIST_INDEX_GLOBAL, NC_INT, 1, 
		  si.len_ss_distfact_list, -1,
		  d->len_ss_distfact_list, -1,
		  &si.ss_distfact_list_index_global);

  define_variable(u, VAR_SS_DISTFACT_LEN_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.ss_distfact_len_global);

  define_variable(u, VAR_SS_ELEM_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_elem_index_global);

  define_variable(u, VAR_SS_ELEM_LEN_GLOBAL, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.ss_elem_len_global);

  define_variable(u, VAR_SS_ELEM_LIST_INDEX_GLOBAL, NC_INT, 1, 
		  si.len_ss_elem_list, -1,
		  d->len_ss_elem_list, -1,
		  &si.ss_elem_list_index_global);

  define_variable(u, VAR_SS_ID_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_id_global);

  define_variable(u, VAR_SS_INDEX_GLOBAL, NC_INT, 1, 
		  si.num_side_sets, -1,
		  d->num_side_sets, -1,
		  &si.ss_index_global);

  define_variable(u, VAR_SS_NUM_DISTFACTS_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_num_distfacts_global);

  define_variable(u, VAR_SS_NUM_SIDES_GLOBAL, NC_INT, 1, 
		  si.num_side_sets_global, -1,
		  d->num_side_sets_global, -1,
		  &si.ss_num_sides_global);

  if ( d->num_props_ss > 1 )
    {
      define_variable(u, VAR_SS_PROP_GLOBAL, NC_INT, 2, 
		      si.num_props_ss, si.num_side_sets_global,
		      d->num_props_ss, d->num_side_sets_global,
		      &si.ss_prop_global);
    }
    
  if ( d->num_side_sets_global > 0 )
    {
      define_variable(u, VAR_SS_INTERNAL_GLOBAL, NC_INT, 1,
                      si.num_side_sets_global, -1,
                      d->num_side_sets_global, -1,
                      &si.ss_internal_global);

      define_variable(u, VAR_SS_BLOCK_INDEX_GLOBAL, NC_INT, 1,
                      si.len_ss_block_index_global, -1,
                      d->num_side_sets_global + 1, -1,
                      &si.ss_block_index_global);

      define_variable(u, VAR_SS_BLOCK_LIST_GLOBAL, NC_INT, 1,
                      si.len_ss_block_list_global, -1,
                      d->ss_block_index_global[d->num_side_sets_global], -1,
                      &si.ss_block_list_global);
    }

  define_variable(u, VAR_UNDEFINED_BASIC_EQNVAR_ID, NC_INT, 0, 
		  -1, -1,
		  -1, -1,
		  &si.undefined_basic_eqnvar_id);


  /*
   * Leave define mode.
   */

#ifdef NETCDF_3
  err = nc_enddef(u);
  if ( err != NC_NOERR )
    {
      EH(-1, "nc_enddef() problem.");
    }
#endif
#ifdef NETCDF_2
  err = ncendef(u);
  EH(err, "ncendef() problem.");
#endif

  /*
   * Put variable values.
   *
   * This form is good for scalars, 1d arrays and 2d arrays. Any more and
   * you'll need to add another argument to the list for the backward
   * compatible to netCDF implementation to work properly. We'll assume
   * that start[] arrays that ncvarput() uses will be full of zeroes.
   * If not, then you'll need to do that case by hand.
   */

  put_variable(u, NC_CHAR, 1, 
	       d->len_string,			-1, 
	       si.dpi_version_string,		d->dpi_version_string);

  put_variable(u, NC_CHAR, 2, 
	       d->num_elem_blocks_global,	d->len_string,
	       si.eb_elem_type_global, &(d->eb_elem_type_global[0][0]));

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks_global,	-1, 
	       si.eb_id_global,			d->eb_id_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks,		-1, 
	       si.eb_index_global,		d->eb_index_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks_global,	-1, 
	       si.eb_num_attr_global,		d->eb_num_attr_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks_global,	-1, 
	       si.eb_num_elems_global,		d->eb_num_elems_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks_global,	-1, 
	       si.eb_num_nodes_per_elem_global,	
	       d->eb_num_nodes_per_elem_global);

  put_variable(u, NC_INT, 1, 
	       d->num_elem_blocks,		-1, 
	       si.eb_num_private_elems,		d->eb_num_private_elems);

  if ( d->num_props_eb > 1 )
    {
      put_variable(u, NC_INT, 2,
		   d->num_props_eb, d->num_elem_blocks_global,
		   si.eb_prop_global, &(d->eb_prop_global[0][0]));
    }

  if ( d->num_elems > 0 )
    {
      put_variable(u, NC_INT, 1, 
		   d->num_elems, -1, 
		   si.elem_index_global,	d->elem_index_global);

      put_variable(u, NC_INT, 1, d->num_elems,	-1, si.elem_owner,
		   d->elem_owner);

    }

  if ( d->len_elem_var_tab_global > 0 )
    {
      put_variable(u, NC_INT, 1, 
		   d->len_elem_var_tab_global,	-1, 
		   si.elem_var_tab_global,	d->elem_var_tab_global);
    }

  if ( d->len_elem_elem_list > 0 )
    {
      put_variable(u, NC_INT, 1, d->len_elem_elem_list,	-1, 
		   si.elem_elem_list_global, d->elem_elem_list_global);
      put_variable(u, NC_INT, 1, d->len_elem_elem_list,	-1, 
		   si.elem_elem_face_global, d->elem_elem_face_global);
      put_variable(u, NC_INT, 1, d->len_elem_elem_list,	-1, 
		   si.elem_elem_twst_global, d->elem_elem_twst_global);
      put_variable(u, NC_INT, 1, d->len_elem_elem_list,	-1, 
		   si.elem_elem_proc_global, d->elem_elem_proc_global);
    }

  put_variable(u, NC_INT, 2, 
	       d->num_global_node_descriptions,	d->len_node_description, 
	       si.global_node_description,&(d->global_node_description[0][0]));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.my_name,		&(d->my_name));

  put_variable(u, NC_INT, 1, 
	       d->num_neighbors,	-1, 
	       si.neighbor,		d->neighbor);

  if ( d->num_nodes > 0 )
    {
      put_variable(u, NC_INT, 1, 
		   d->num_nodes,	-1, 
		   si.node_index_global,	d->node_index_global);
    }

  put_variable(u, NC_INT, 0, 
	       -1, 	-1, 
	       si.ns_distfact_len_global, &(d->ns_distfact_len_global));

  put_variable(u, NC_INT, 0, 
	       -1, 	-1, 
	       si.ns_node_len_global, &(d->ns_node_len_global));

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,	-1, 
	       si.ns_id_global,		d->ns_id_global);

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets,	-1, 
	       si.ns_index_global,	d->ns_index_global);

  put_variable(u, NC_INT, 1, 
	       d->len_ns_distfact_list,	-1, 
	       si.ns_distfact_list_index_global, 
	       d->ns_distfact_list_index_global);

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,		-1, 
	       si.ns_distfact_index_global,	d->ns_distfact_index_global);

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,		-1, 
	       si.ns_node_index_global,	d->ns_node_index_global);

  put_variable(u, NC_INT, 1, 
	       d->len_ns_node_list,	-1, 
	       si.ns_node_list_index_global, 
	       d->ns_node_list_index_global);



  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,		-1, 
	       si.ns_num_distfacts_global,	d->ns_num_distfacts_global);

  put_variable(u, NC_INT, 1, 
	       d->num_node_sets_global,	-1, 
	       si.ns_num_nodes_global,	d->ns_num_nodes_global);

  if ( d->num_props_ns > 1 )
    {
      put_variable(u, NC_INT, 2,
		   d->num_props_ns,		d->num_node_sets_global,
		   si.ns_prop_global,	&(d->ns_prop_global[0][0]));
    }

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_boundary_nodes,	&(d->num_boundary_nodes));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_dofs_global,	&(d->num_dofs_global));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_elems_global,	&(d->num_elems_global));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_external_nodes,	&(d->num_external_nodes));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_internal_nodes,	&(d->num_internal_nodes));

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.num_nodes_global,	&(d->num_nodes_global));

  put_variable(u, NC_INT, 1, 
	       d->len_ptr_set_membership,	-1, 
	       si.ptr_set_membership,	d->ptr_set_membership);

  put_variable(u, NC_INT, 1, 
	       d->len_set_membership,	-1, 
	       si.set_membership,	d->set_membership);

  put_variable(u, NC_INT, 1, d->num_side_sets_global, -1, 
	       si.ss_distfact_index_global, d->ss_distfact_index_global);

  put_variable(u, NC_INT, 0, -1, -1, 
	       si.ss_distfact_len_global, &(d->ss_distfact_len_global));

  if ( d->len_ss_distfact_list > 0 )
    {
      put_variable(u, NC_INT, 1, d->len_ss_distfact_list, -1, 
		   si.ss_distfact_list_index_global, 
		   d->ss_distfact_list_index_global);
    }

  put_variable(u, NC_INT, 1, d->num_side_sets_global, -1, 
	       si.ss_elem_index_global, d->ss_elem_index_global);

  put_variable(u, NC_INT, 0, -1, -1, 
	       si.ss_elem_len_global, &(d->ss_elem_len_global));

  if ( d->len_ss_elem_list > 0 )
    {
      put_variable(u, NC_INT, 1, d->len_ss_elem_list, -1, 
		   si.ss_elem_list_index_global, d->ss_elem_list_index_global);
    }

  put_variable(u, NC_INT, 1, 
	       d->num_side_sets_global,	-1, 
	       si.ss_id_global,		d->ss_id_global);

  if ( d->num_side_sets > 0 )
    {
      put_variable(u, NC_INT, 1, 
		   d->num_side_sets,	-1, 
		   si.ss_index_global,	d->ss_index_global);
    }

  put_variable(u, NC_INT, 1, 
	       d->num_side_sets_global,		-1, 
	       si.ss_num_distfacts_global,	d->ss_num_distfacts_global);

  put_variable(u, NC_INT, 1, 
	       d->num_side_sets_global,		-1, 
	       si.ss_num_sides_global,		d->ss_num_sides_global);

  if ( d->num_props_ss > 1 )
    {
      put_variable(u, NC_INT, 2,
		   d->num_props_ss,		d->num_side_sets_global,
		   si.ss_prop_global,		&(d->ss_prop_global[0][0]));
    }

  put_variable(u, NC_INT, 0, 
	       -1,	-1, 
	       si.undefined_basic_eqnvar_id, &(d->undefined_basic_eqnvar_id));
  
  if (d->num_side_sets_global > 0) {
      put_variable(u, NC_INT, 1,
                   d->num_side_sets_global, -1,
                   si.ss_internal_global, d->ss_internal_global);

      put_variable(u, NC_INT, 1,
                   d->num_side_sets_global+1, -1,
                   si.ss_block_index_global, d->ss_block_index_global);

      put_variable(u, NC_INT, 1,
                   d->ss_block_index_global[d->num_side_sets_global], -1,
                   si.ss_block_list_global, d->ss_block_list_global);
  }  
  /*
   * Close the file (flush buffers).
   */

#ifdef NETCDF_3
  err = nc_close(u);
  if ( err != NC_NOERR )
    {
      EH(-1, "nc_close() problem.");
    }
#endif
#ifdef NETCDF_2
  err = ncclose(u);
  EH(err, "ncclose()");
#endif

  return(status);
}
Exemplo n.º 30
0
int ex_put_var_param (int   exoid,
                      const char *var_type,
                      int   num_vars)
{
  int time_dim, num_nod_dim, dimid, strdim;
  int dims[3];
  char *vptr;
  char errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

  /* if no variables are to be stored, return with warning */
  if (num_vars == 0)
    {
      exerrval = EX_MSG;
      if (tolower(*var_type) == 'e')
        vptr="element";
      else if (tolower(*var_type) == 'g')
        vptr="global";
      else if (tolower(*var_type) == 'n')
        vptr="nodal";
      else if (tolower(*var_type) == 'm')
        vptr="nodeset";
      else
        vptr="invalid type"; 

      sprintf(errmsg,
              "Warning: zero %s variables specified for file id %d",
              vptr,exoid);
      ex_err("ex_put_var_param",errmsg,exerrval);

      return (EX_WARN);
    }

  /* inquire previously defined dimensions  */

  if ((time_dim = ncdimid (exoid, DIM_TIME)) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to locate time dimension in file id %d", exoid);
      ex_err("ex_put_var_param",errmsg,exerrval);
      return (EX_FATAL);
    }

  if ((num_nod_dim = ncdimid (exoid, DIM_NUM_NODES)) == -1) {
    if (tolower(*var_type) == 'n') {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to locate number of nodes in file id %d", exoid);
      ex_err("ex_put_var_param",errmsg,exerrval);
      return (EX_FATAL);
    }
  }
  

  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_var_param",errmsg,exerrval);
      return (EX_FATAL);
    }

  /* put file into define mode  */

  if (ncredef (exoid) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to put file id %d into define mode", exoid);
      ex_err("ex_get_var_param",errmsg,exerrval);
      return (EX_FATAL);
    }


  /* define dimensions and variables */

  if (tolower(*var_type) == 'g')
    {
      if ((dimid = ncdimdef (exoid, DIM_NUM_GLO_VAR, (long)num_vars)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: global variable name parameters are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define number of global variables in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }


      dims[0] = time_dim;
      dims[1] = dimid;
      if ((ncvardef (exoid, VAR_GLO_VAR, 
                     nc_flt_code(exoid), 2, dims)) == -1)
        {
          exerrval = ncerr;
          sprintf(errmsg,
                  "Error: failed to define global variables in file id %d",
                  exoid);
          ex_err("ex_put_var_param",errmsg,exerrval);
          goto error_ret;          /* exit define mode and return */
        }

      /* Now define global variable name variable */
      dims[0] = dimid;
      dims[1] = strdim;
      if ((ncvardef (exoid, VAR_NAME_GLO_VAR, NC_CHAR, 2, dims)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: global variable names are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define global variable names in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }

    }

  else if (tolower(*var_type) == 'n')
    {
      /*
       * There are two ways to store the nodal variables. The old way *
       * was a blob (#times,#vars,#nodes), but that was exceeding the
       * netcdf maximum dataset size for large models. The new way is
       * to store #vars separate datasets each of size (#times,#nodes)
       *
       * We want this routine to be capable of storing both formats
       * based on some external flag.  Since the storage format of the
       * coordinates have also been changed, we key off of their
       * storage type to decide which method to use for nodal
       * variables. If the variable 'coord' is defined, then store old
       * way; otherwise store new.
       */
      if ((dimid = ncdimdef (exoid, DIM_NUM_NOD_VAR, (long)num_vars)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: nodal variable name parameters are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define number of nodal variables in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }

      if (ex_large_model(exoid) == 0) { /* Old way */
        dims[0] = time_dim;
        dims[1] = dimid;
        dims[2] = num_nod_dim;
        if ((ncvardef (exoid, VAR_NOD_VAR,
                       nc_flt_code(exoid), 3, dims)) == -1)
          {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: failed to define nodal variables in file id %d",
                    exoid);
            ex_err("ex_put_var_param",errmsg,exerrval);
            goto error_ret;          /* exit define mode and return */
          }
      } else { /* New way */
        int i;
        for (i = 1; i <= num_vars; i++) {
          dims[0] = time_dim;
          dims[1] = num_nod_dim;
          if ((ncvardef (exoid, VAR_NOD_VAR_NEW(i),
                         nc_flt_code(exoid), 2, dims)) == -1)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define nodal variable %d in file id %d",
                      i, exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
              goto error_ret;          /* exit define mode and return */
            }
        }
      }

      /* Now define nodal variable name variable */
      dims[0] = dimid;
      dims[1] = strdim;
      if ((ncvardef (exoid, VAR_NAME_NOD_VAR, NC_CHAR, 2, dims)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: nodal variable names are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define nodal variable names in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }

    }

  else if (tolower(*var_type) == 'e')
    {
      if ((dimid = ncdimdef (exoid, DIM_NUM_ELE_VAR, (long)num_vars)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: element variable name parameters are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define number of element variables in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }

      /* Now define element variable name variable */
      dims[0] = dimid;
      dims[1] = strdim;
      if ((ncvardef (exoid, VAR_NAME_ELE_VAR, NC_CHAR, 2, dims)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: element variable names are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define element variable names in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }


      /* netCDF variables in which to store the EXODUS element variable values will
       * be defined in ex_put_elem_var_tab or ex_put_elem_var; at this point, we 
       * don't know what element variables are valid for which element blocks 
       * (the info that is stored in the element variable truth table)
       */
    }

  else if (tolower(*var_type) == 'm')
    {
      if ((dimid = ncdimdef (exoid, DIM_NUM_NSET_VAR, (long)num_vars)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: nodeset variable name parameters are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define number of nodeset variables in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }

      /* Now define nodeset variable name variable */
      dims[0] = dimid;
      dims[1] = strdim;
      if ((ncvardef (exoid, VAR_NAME_NSET_VAR, NC_CHAR, 2, dims)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: nodeset variable names are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define nodeset variable names in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }


      /* netCDF variables in which to store the EXODUS nodeset variable values will
       * be defined in ex_put_nset_var_tab or ex_put_nset_var; at this point, we 
       * don't know what nodeset variables are valid for which nodesets
       * (the info that is stored in the nodeset variable truth table)
       */
    }
  else if (tolower(*var_type) == 's')
    {
      if ((dimid = ncdimdef (exoid, DIM_NUM_SSET_VAR, (long)num_vars)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: sideset variable name parameters are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define number of sideset variables in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }

      /* Now define sideset variable name variable */
      dims[0] = dimid;
      dims[1] = strdim;
      if ((ncvardef (exoid, VAR_NAME_SSET_VAR, NC_CHAR, 2, dims)) == -1)
        {
          if (ncerr == NC_ENAMEINUSE)
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: sideset variable names are already defined in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          else
            {
              exerrval = ncerr;
              sprintf(errmsg,
                      "Error: failed to define sideset variable names in file id %d",
                      exoid);
              ex_err("ex_put_var_param",errmsg,exerrval);
            }
          goto error_ret;          /* exit define mode and return */
        }


      /* netCDF variables in which to store the EXODUS sideset variable values will
       * be defined in ex_put_nset_var_tab or ex_put_nset_var; at this point, we 
       * don't know what sideset variables are valid for which sidesets
       * (the info that is stored in the sideset variable truth table)
       */
    }
  /* leave define mode  */

  if (ncendef (exoid) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to complete definition in file id %d",
              exoid);
      ex_err("ex_put_var_param",errmsg,exerrval);
      return (EX_FATAL);
    }

  return(EX_NOERR);

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