Пример #1
0
Файл: v2i.c Проект: stcorp/harp
int
ncvargets(
    int		ncid,
    int		varid,
    const long*	start,
    const long*	count,
    const long*	stride,
    void*	value
)
{
	if(stride == NULL)
		return ncvarget(ncid, varid, start, count, value);
	/* else */
	{
	NDIMS_DECL
	A_DECL(stp, size_t, ndims, start);
	A_DECL(cntp, size_t, ndims, count);
	A_DECL(strdp, ptrdiff_t, ndims, stride);
	A_INIT(stp, size_t, ndims, start);
	A_INIT(cntp, size_t, ndims, count);
	A_INIT(strdp, ptrdiff_t, ndims, stride);
	{
	const int status = nc_get_vars(ncid, varid, stp, cntp, strdp, value);
	A_FREE(strdp);
	A_FREE(cntp);
	A_FREE(stp);
	if(status != NC_NOERR)
	{
		nc_advise("ncvargets", status, "ncid %d", ncid);
		return -1;
	}
	}
	return 0;
	}
}
Пример #2
0
int ex_get_varid_var(int   exoid,
                     int   time_step,
                     int   varid,
                     int   num_entity,
                     void *var_vals)
{
  long start[2], count[2];
  char errmsg[MAX_ERR_LENGTH];
  void *array;
  
  exerrval = 0; /* clear error code */

  /* read values of element variable */

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

  array = ex_conv_array(exoid,RTN_ADDRESS,var_vals,num_entity);
  if (ncvarget (exoid, varid, start, count, array) == -1) {
    exerrval = ncerr;
    sprintf(errmsg,
            "Error: failed to get variable with variable id %d in file id %d",
            varid,exoid);/*this msg needs to be improved*/
    ex_err("ex_get_varid_var",errmsg,exerrval);
    return (EX_FATAL);
  }

  if (array != var_vals) {
    ex_conv_array(exoid, READ_CONVERT, var_vals, num_entity);
  }
  return (EX_NOERR);
}
Пример #3
0
MNCAPI int
MI2varget(int fd, int varid, const long *start_ptr, 
          const long *count_ptr, void *val_ptr)
{
    if (MI2_ISH5OBJ(fd)) {
        return (hdf_varget(fd, varid, start_ptr, count_ptr, val_ptr));
    }
    else {
        return (ncvarget(fd, varid, start_ptr, count_ptr, val_ptr));
    }
}
Пример #4
0
int ex_get_glob_vars (int   exoid,
                      int   time_step,
                      int   num_glob_vars,
                      void *glob_var_vals)
{
   int varid;
   long start[2], count[2];
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

/* inquire previously defined variable */

   if ((varid = ncvarid (exoid, VAR_GLO_VAR)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Warning: failed to locate global variables in file id %d",
            exoid);
     ex_err("ex_get_glob_vars",errmsg,exerrval);
     return (EX_WARN);
   }


/* read values of global variables */

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

   count[0] = 1;
   count[1] = num_glob_vars;

   if (ncvarget (exoid, varid, start, count,
           ex_conv_array(exoid,RTN_ADDRESS,glob_var_vals,num_glob_vars)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to get global variable values from file id %d",
            exoid);
     ex_err("ex_get_glob_vars",errmsg,exerrval);
     return (EX_FATAL);
   }


   ex_conv_array( exoid, READ_CONVERT, glob_var_vals, num_glob_vars );

   return (EX_NOERR);
}
Пример #5
0
void get_2d_array(
    float *array,
    long latlen,
    long lonlen,
    int fid,
    int aid,
    long timestep                  
)
{
    long start[3], count[3];

    start[0] = timestep;
    start[1] = start[2] = 0;
    count[0] = 1; count[1] = latlen; count[2] = lonlen;
    ncvarget(fid,aid,(long const *)start,(long const *)count,array);
}
Пример #6
0
nclong *get_status_array(int exoid, long var_count, const char *VARIABLE, const char *label)
{
  char errmsg[MAX_ERR_LENGTH];
  int varid;
  long start[2], count[2]; 
  nclong *stat_vals = NULL;
  
  if (!(stat_vals = static_cast<nclong*>(malloc(var_count*sizeof(nclong))))) {
    exerrval = EX_MEMFAIL;
    sprintf(errmsg,
	    "Error: failed to allocate memory for %s status array for file id %d",
	    label, exoid);
    ex_err("ex_put_all_var_param",errmsg,exerrval);
    return (NULL);
  }

  /* get variable id of status array */
  if ((varid = ncvarid (exoid, VARIABLE)) != -1) {
    /* if status array exists (V 2.01+), use it, otherwise assume
       object exists to be backward compatible */
     
    start[0] = 0;
    start[1] = 0;
    count[0] = var_count;
    count[1] = 0;
     
    if (ncvarget (exoid, varid, start, count, (void *)stat_vals) == -1) {
      exerrval = ncerr;
      stat_vals = static_cast<nclong*>(safe_free(stat_vals));
      sprintf(errmsg,
	      "Error: failed to get %s status array from file id %d",
	      label, exoid);
      ex_err("ex_put_all_var_param",errmsg,exerrval);
      return (NULL);
    }
  } else {
    /* status array doesn't exist (V2.00), dummy one up for later checking */
    int i;
    for(i=0; i<var_count; i++)
      stat_vals[i] = 1;
  }
 return stat_vals;
}
Пример #7
0
int ex_get_nset_var (int   exoid,
                     int   time_step,
                     int   nset_var_index,
                     int   nset_id, 
                     int   num_node_this_nset,
                     void *nset_var_vals)
{
   int varid, nset_id_ndx;
   long start[2], count[2];
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

  /* Determine index of nset_id in VAR_NS_IDS array */
  nset_id_ndx = ex_id_lkup(exoid,VAR_NS_IDS,nset_id);
  if (exerrval != 0) 
  {
    if (exerrval == EX_NULLENTITY)
    {
      sprintf(errmsg,
              "Warning: no nodeset variables for NULL nodeset %d in file id %d",
              nset_id,exoid);
      ex_err("ex_get_nset_var",errmsg,EX_MSG);
      return (EX_WARN);
    }
    else
    {
      sprintf(errmsg,
     "Error: failed to locate nodeset id %d in %s variable in file id %d",
              nset_id, VAR_ID_EL_BLK, exoid);
      ex_err("ex_get_nset_var",errmsg,exerrval);
      return (EX_FATAL);
    }
  }


/* inquire previously defined variable */

   if((varid=ncvarid(exoid,VAR_NS_VAR(nset_var_index,nset_id_ndx))) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
          "Error: failed to locate nodeset variable %d for nodeset %d in file id %d",
          nset_var_index,nset_id,exoid); /* this msg needs to be improved */
     ex_err("ex_get_nset_var",errmsg,exerrval);
     return (EX_FATAL);
   }

/* read values of nodeset variable */

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

   count[0] = 1;
   count[1] = num_node_this_nset;

   if (ncvarget (exoid, varid, start, count,
        ex_conv_array(exoid,RTN_ADDRESS,nset_var_vals,num_node_this_nset)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
        "Error: failed to get nodeset variable %d for nodeset %d in file id %d",
             nset_var_index,nset_id,exoid);/*this msg needs to be improved*/
     ex_err("ex_get_nset_var",errmsg,exerrval);
     return (EX_FATAL);
   }


   ex_conv_array( exoid, READ_CONVERT, nset_var_vals, num_node_this_nset );

   return (EX_NOERR);
}
Пример #8
0
int main(int argc, char *argv[])
{
    int i, j, d, h;
    int appid, wid, cnid, vcid, stid, txid, amid, mpid, tmid, stdmid;
    long stid_len;
    int vfield, vfield2, sfield, sfield2;
    int rlist;
    ng_size_t len_dims[2];
    long strt[1], cnt[1];
    long latlen, lonlen;
    long timelen;
    int *timestep;
    int ncid[6], uid, vid, u5id, v5id, pid, tid;
    int latid, lonid;
    float *lon, *lat;
    float *X, *Y;
    char  filename[256];
    char  *rftime;
    const char *dir = _NGGetNCARGEnv("data");
    char hour[3], day[3], mainstring[17];
    extern void get_2d_array(float *, long, long, int, int, long);
    char const *wks_type = "x11";

/*
 * Initialize the high level utility library
 */
    NhlInitialize();
/*
 * Create an application object.
 */
    rlist = NhlRLCreate(NhlSETRL);
    NhlRLClear(rlist);
    NhlRLSetString(rlist,NhlNappUsrDir,"./");
    NhlRLSetString(rlist,NhlNappDefaultParent,"True");
    NhlCreate(&appid,"st04",NhlappClass,NhlDEFAULT_APP,rlist);

    if (!strcmp(wks_type,"ncgm") || !strcmp(wks_type,"NCGM")) {
/*
 * Create a meta file workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkMetaName,"./st04c.ncgm");
        NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
        NhlCreate(&wid,"st04Work",
                  NhlncgmWorkstationClass,NhlDEFAULT_APP,rlist);
    }
    else if (!strcmp(wks_type,"x11") || !strcmp(wks_type,"X11")) {
/*
 * Create an X workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetInteger(rlist,NhlNwkPause,True);
        NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
        NhlCreate(&wid,"st04Work",NhlcairoWindowWorkstationClass,appid,rlist);
    }

    else if (!strcmp(wks_type,"oldps") || !strcmp(wks_type,"OLDPS")) {
/*
 * Create an older-style PostScript workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkPSFileName,"st04c.ps");
        NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
        NhlCreate(&wid,"st04Work",NhlpsWorkstationClass,appid,rlist);
    }
    else if (!strcmp(wks_type,"oldpdf") || !strcmp(wks_type,"OLDPDF")) {
/*
 * Create an older-style PDF workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkPDFFileName,"st04c.pdf");
        NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
        NhlCreate(&wid,"st04Work",NhlpdfWorkstationClass,appid,rlist);
    }
    else if (!strcmp(wks_type,"pdf") || !strcmp(wks_type,"PDF") ||
             !strcmp(wks_type,"ps") || !strcmp(wks_type,"PS")) {
/*
 * Create a cairo PS/PDF workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkFileName,"st04c");
        NhlRLSetString(rlist,NhlNwkFormat,(char*)wks_type);
        NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
        NhlCreate(&wid,"st04Work",NhlcairoDocumentWorkstationClass,appid,rlist);
    }
    else if (!strcmp(wks_type,"png") || !strcmp(wks_type,"PNG")) {
/*
 * Create a cairo PNG workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkFileName,"st04c");
        NhlRLSetString(rlist,NhlNwkFormat,(char*)wks_type);
        NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
        NhlCreate(&wid,"st04Work",NhlcairoImageWorkstationClass,appid,rlist);
    }
/*
 * Open the netCDF files.
 */
    for( i = 0; i <= 5; i++ ) {
        sprintf( filename, "%s/cdf/%s", dir, cdffiles[i] );
        ncid[i] = ncopen(filename,NC_NOWRITE);
    }
/*
 * Get the lat/lon dimensions (they happen to be the
 * same for all files in this case)
 */
    latid = ncdimid(ncid[0],"lat");
    lonid = ncdimid(ncid[0],"lon");
    ncdiminq(ncid[0],latid,(char *)0,&latlen);
    ncdiminq(ncid[0],lonid,(char *)0,&lonlen);
    len_dims[0] = latlen;
    len_dims[1] = lonlen;
/*
 * Get the variable ids
 */
    uid = ncvarid(ncid[0],"u");
    vid = ncvarid(ncid[1],"v");
    pid = ncvarid(ncid[2],"p");
    tid = ncvarid(ncid[3],"t");
    u5id = ncvarid(ncid[4],"u");
    v5id = ncvarid(ncid[5],"v");
    latid = ncvarid(ncid[0],"lat");
    lonid = ncvarid(ncid[0],"lon");
/*
 * allocate space for arrays
 */
    X = (float *)malloc(sizeof(float)*latlen*lonlen);
    Y = (float *)malloc(sizeof(float)*latlen*lonlen);
    lat = (float *)malloc(sizeof(float)*latlen);
    lon = (float *)malloc(sizeof(float)*lonlen);
/*
 * Get lat/lon values (they are the same for all files)
 */
    strt[0] = 0;
    cnt[0] = latlen;
    ncvarget(ncid[0],latid,(long const *)strt,(long const *)cnt,lat);
    cnt[0] = lonlen;
    ncvarget(ncid[0],lonid,(long const *)strt,(long const *)cnt,lon);
/*
 * Get U and V data values
 */
    get_2d_array(X,latlen,lonlen,ncid[0],uid,0);
    get_2d_array(Y,latlen,lonlen,ncid[1],vid,0);
/*
 * Create a VectorField of the surface wind data
 */
    NhlRLClear(rlist);
    NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims);
    NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims);
    NhlRLSetFloat(rlist,NhlNvfXCStartV,lon[0]);
    NhlRLSetFloat(rlist,NhlNvfYCStartV,lat[0]);
    NhlRLSetFloat(rlist,NhlNvfXCEndV,lon[lonlen-1]);
    NhlRLSetFloat(rlist,NhlNvfYCEndV,lat[latlen-1]);
    NhlRLSetFloat(rlist,NhlNvfMissingUValueV,-9999.0);
    NhlCreate(&vfield,"VectorField",NhlvectorFieldClass,appid,rlist);
/*
 * Create a VectorField of 500 millibar wind data
 *
 * Get U and V values
 */
    get_2d_array(X,latlen,lonlen,ncid[4],u5id,0);
    get_2d_array(Y,latlen,lonlen,ncid[5],v5id,0);

    NhlRLClear(rlist);
    NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims);
    NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims);
    NhlRLSetFloat(rlist,NhlNvfXCStartV,lon[0]);
    NhlRLSetFloat(rlist,NhlNvfYCStartV,lat[0]);
    NhlRLSetFloat(rlist,NhlNvfXCEndV,lon[lonlen-1]);
    NhlRLSetFloat(rlist,NhlNvfYCEndV,lat[latlen-1]);
    NhlRLSetFloat(rlist,NhlNvfMissingUValueV,-9999.0);
    NhlCreate(&vfield2,"VectorField",NhlvectorFieldClass,appid,rlist);
/*
 * Create a ScalarField of surface pressure 
 *
 * Get P data values
 */
    get_2d_array(X,latlen,lonlen,ncid[2],pid,0);

    for( i = 0; i < latlen*lonlen; i++ ) {
        if( X[i] != -9999.0 ) {
            X[i] /= 100.;
        }
    }

    NhlRLClear(rlist);
    NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims);
    NhlRLSetFloat(rlist,NhlNsfXCStartV,lon[0]);
    NhlRLSetFloat(rlist,NhlNsfYCStartV,lat[0]);
    NhlRLSetFloat(rlist,NhlNsfXCEndV,lon[lonlen-1]);
    NhlRLSetFloat(rlist,NhlNsfYCEndV,lat[latlen-1]);
    NhlRLSetFloat(rlist,NhlNsfMissingValueV,-9999.0);
    NhlCreate(&sfield,"ScalarField",NhlscalarFieldClass,appid,rlist);
/*
 * Create a ScalarField of surface temperature 
 * (convert from Kelvin to Farenheit)
 *
 * Get T data values
 */
    get_2d_array(X,latlen,lonlen,ncid[3],tid,0);
/*
 * Convert to Fahrenheit
 */
    for( i = 0; i < latlen*lonlen; i++ ) {
        if( X[i] != -9999.0) {
            X[i] = (X[i] - 273.15) * 9.0/5.0 + 32.0;
        }
    }

    NhlRLClear(rlist);
    NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims);
    NhlRLSetFloat(rlist,NhlNsfXCStartV,lon[0]);
    NhlRLSetFloat(rlist,NhlNsfYCStartV,lat[0]);
    NhlRLSetFloat(rlist,NhlNsfXCEndV,lon[lonlen-1]);
    NhlRLSetFloat(rlist,NhlNsfYCEndV,lat[latlen-1]);
    NhlRLSetFloat(rlist,NhlNsfMissingValueV,-9999.0);
    NhlCreate(&sfield2,"ScalarField2",NhlscalarFieldClass,appid,rlist);
/*
 * Create a ContourPlot with surface temperature data
 */
    NhlRLClear(rlist);
    NhlRLSetString(rlist,NhlNcnFillOn,"true");
    NhlRLSetString(rlist,NhlNcnLinesOn,"false");
    NhlRLSetString(rlist,NhlNcnFillDrawOrder,"predraw");
    NhlRLSetInteger(rlist,NhlNcnScalarFieldData,sfield2);
    NhlCreate(&cnid,"contourplot",NhlcontourPlotClass,wid,rlist);
/*
 * Create a VectorPlot with the surface wind and pressure data
 */
    NhlRLClear(rlist);
    NhlRLSetString(rlist,NhlNvcUseScalarArray,"true");
    NhlRLSetInteger(rlist,NhlNvcVectorFieldData,vfield);
    NhlRLSetInteger(rlist,NhlNvcScalarFieldData,sfield);
    NhlCreate(&vcid,"vectorplot",NhlvectorPlotClass,wid,rlist);
/*
 * Create a StreamlinePlot with 500 mb wind data
 */
    NhlRLClear(rlist);
    NhlRLSetString(rlist,NhlNpmTitleDisplayMode,"always");
    NhlRLSetString(rlist,NhlNtiMainFuncCode,"~");
    NhlRLSetInteger(rlist,NhlNstVectorFieldData,vfield2);
    NhlCreate(&stid,"streamlineplot",NhlstreamlinePlotClass,wid,rlist);
/*
 * Create an annotation used to explain the streamline data
 */
    NhlCreate(&txid,"streamlineplotanno",NhltextItemClass,wid,0);
    amid = NhlAddAnnotation(stid,txid);
/*
 * Create a map object
 */
    NhlRLClear(rlist);
/*    NhlRLSetString(rlist,NhlNvpUseSegments,"true"); */
    NhlCreate(&mpid,"mapplot",NhlmapPlotClass,wid,rlist);
/*
 * Overlay everything on the MapPlot. The last object overlaid will
 * appear on top
 */
    NhlAddOverlay(mpid,cnid,-1);
    NhlAddOverlay(mpid,vcid,-1);
    NhlAddOverlay(mpid,stid,-1);
/*
 * Variables for manipulating the title string
 */
    tmid = ncdimid(ncid[1],"timestep");
    ncdiminq(ncid[1],tmid,(char *)0,&timelen);
    tmid = ncvarid(ncid[1],"timestep");
    timestep = (int *)malloc(sizeof(int)*timelen);

    strt[0] = 0;
    cnt[0] = timelen;
    ncvarget(ncid[1],tmid,(long const *)strt,(long const *)cnt,timestep);
    sprintf( hour, "00");
    sprintf( day, "05");
    
    stdmid = ncdimid(ncid[1],"timelen");
    ncdiminq(ncid[1], stdmid, (char *)0, &stid_len );
    tmid = ncvarid(ncid[1],"reftime");
    rftime = (char *)malloc((stid_len+1)*sizeof(char));

    strt[0] = 0; cnt[0] = stid_len;
    ncvarget(ncid[1],tmid,(long const *)strt,(long const *)cnt,rftime);

    for( i = 0; i <= TIMESTEPS-1; i++ ) {
        if (i != 17 && i != 36 && i != 37) {
/*
 * Figure out the hour and day from the timestep, convert to strings
 * and build the title string
 */
            d = timestep[i] / 24 + 5;
            h = timestep[i] % 24;
            if (h > 9) {
                sprintf( hour, "%d", h );
            }
            else {
                sprintf( hour, "0%d", h );
            }
            if (d > 9) {
                sprintf(day, "%d", d );
            }
            else {
                sprintf(day, "0%d", d );
            }
/*
 * Set the new title string
 */
			strcpy(mainstring, rftime);
            sprintf(&mainstring[8], "%2s %2s:00", day, hour);
            printf("%s\n",mainstring);
            NhlRLClear(rlist);
            NhlRLSetString(rlist,NhlNtiMainString,mainstring);
            NhlSetValues(stid,rlist);
/*
 * Modify the data objects with data for the current time step
 *
 * Get U and V values
 */         
            get_2d_array(X,latlen,lonlen,ncid[0],uid,i);
            get_2d_array(Y,latlen,lonlen,ncid[1],vid,i);

            NhlRLClear(rlist);
            NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims);
            NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims);
            NhlSetValues(vfield,rlist);
/*
 * Get U and V values
 */
            get_2d_array(X,latlen,lonlen,ncid[4],u5id,i);
            get_2d_array(Y,latlen,lonlen,ncid[5],v5id,i);

            NhlRLClear(rlist);
            NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims);
            NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims);
            NhlSetValues(vfield2,rlist);
/*
 * Get P values
 */
            get_2d_array(X,latlen,lonlen,ncid[2],pid,i);

            for( j = 0; j < latlen*lonlen; j++ ) {
                if( X[j] != -9999.0 ) {
                    X[j] /= 100.;
                }
            }
            NhlRLClear(rlist);
            NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims);
            NhlSetValues(sfield,rlist);
/*
 * Get T values
 */
            get_2d_array(X,latlen,lonlen,ncid[3],tid,i);
/*
 * Convert to Fahrenheit
 */
            for( j = 0; j < latlen*lonlen; j++ ) {
                if( X[j] != -9999.0) {
                    X[j] = (X[j] - 273.15) * 9.0/5.0 + 32.0;
                }
            }

            NhlRLClear(rlist);
            NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims);
            NhlSetValues(sfield2,rlist);
/* 
 * Draw the plot
 */
            NhlDraw(mpid);
            NhlFrame(wid);
        }
    }
/* 
 *  Destroy the workstation object and exit.
 */
    NhlDestroy(wid);
    NhlClose();
    exit(0);
}
Пример #9
0
int ex_get_set_dist_fact (int   exoid,
			  int   set_type,
			  int   set_id,
			  void *set_dist_fact)
{

   int dimid, dist_id, set_id_ndx;
   long num_df_in_set, count[1], start[1];
   char errmsg[MAX_ERR_LENGTH];
   char* typeName;
   char* dimptr;
   char* idsptr;
   char* numdfptr;
   char* factptr;
 
   exerrval = 0; /* clear error code */

   /* setup pointers based on set_type 
    NOTE: there is another block that sets more stuff later ... */

   if (set_type == EX_NODE_SET) {
     typeName = "node";
     dimptr = DIM_NUM_NS;
     idsptr = VAR_NS_IDS;
   }
   else if (set_type == EX_EDGE_SET) {
     typeName = "edge";
     dimptr = DIM_NUM_ES;
     idsptr = VAR_ES_IDS;
   }
   else if (set_type == EX_FACE_SET) {
     typeName = "face";
     dimptr = DIM_NUM_FS;
     idsptr = VAR_FS_IDS;
   }
   else if (set_type == EX_SIDE_SET) {
     typeName = "side";
     dimptr = DIM_NUM_SS;
     idsptr = VAR_SS_IDS;
   }
   else if (set_type == EX_ELEM_SET) {
     typeName = "elem";
     dimptr = DIM_NUM_ELS;
     idsptr = VAR_ELS_IDS;
   }
   else {
     exerrval = EX_FATAL;
     sprintf(errmsg,
             "Error: invalid set type (%d)", set_type);
     ex_err("ex_put_set_param",errmsg,exerrval);
     return (EX_FATAL);
   }

/* first check if any sets are specified */

   if ((dimid = ncdimid (exoid, dimptr)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Warning: no %s sets stored in file id %d",
             typeName, exoid);
     ex_err("ex_get_set_dist_fact",errmsg,exerrval);
     return (EX_WARN);
   }

/* Lookup index of set id in VAR_*S_IDS array */

   set_id_ndx = ex_id_lkup(exoid,idsptr,set_id);
   if (exerrval != 0) 
   {
     if (exerrval == EX_NULLENTITY)
     {
       sprintf(errmsg,
              "Warning: %s set %d is NULL in file id %d",
               typeName, set_id,exoid);
       ex_err("ex_get_set_dist_fact",errmsg,EX_MSG);
       return (EX_WARN);
     }
     else
     {
       sprintf(errmsg,
     "Error: failed to locate %s set id %d in VAR_*S_IDS array in file id %d",
               typeName, set_id,exoid);
       ex_err("ex_get_set_dist_fact",errmsg,exerrval);
       return (EX_FATAL);
     }
   }

  /* setup more pointers based on set_type */
   if (set_type == EX_NODE_SET) {
     /* note we are using DIM_NUM_NODE_NS instead of DIM_NUM_DF_NS */
     numdfptr = DIM_NUM_NOD_NS(set_id_ndx);
     factptr = VAR_FACT_NS(set_id_ndx);
   }
   else if (set_type == EX_EDGE_SET) {
     numdfptr = DIM_NUM_DF_ES(set_id_ndx);
     factptr = VAR_FACT_ES(set_id_ndx);
   }
   else if (set_type == EX_FACE_SET) {
     numdfptr = DIM_NUM_DF_FS(set_id_ndx);
     factptr = VAR_FACT_FS(set_id_ndx);
   }
   else if (set_type == EX_SIDE_SET) {
     numdfptr = DIM_NUM_DF_SS(set_id_ndx);
     factptr = VAR_FACT_SS(set_id_ndx);
   }
   if (set_type == EX_ELEM_SET) {
     numdfptr = DIM_NUM_DF_ELS(set_id_ndx);
     factptr = VAR_FACT_ELS(set_id_ndx);
   }

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

   if ((dimid = ncdimid (exoid, numdfptr)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
     "Warning: dist factors not stored for %s set %d in file id %d",
             typeName, set_id,exoid);
     ex_err("ex_get_set_dist_fact",errmsg,exerrval);
     return (EX_WARN);          /* complain - but not too loud */
   }

   if (ncdiminq (exoid, dimid, (char *) 0, &num_df_in_set) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
     "Error: failed to get number of dist factors in %s set %d in file id %d",
             typeName, set_id,exoid);
     ex_err("ex_get_set_dist_fact",errmsg,exerrval);
     return (EX_FATAL);
   }


   if ((dist_id = ncvarid (exoid, factptr)) == -1)
   {
     exerrval = ncerr;
     /* not an error for node sets because this is how we check that df's exist */
     if (set_type == EX_NODE_SET)
     {
       sprintf(errmsg,
	       "Warning: dist factors not stored for %s set %d in file id %d",
	       typeName, set_id, exoid);
       ex_err("ex_get_set_dist_fact",errmsg,exerrval);
       return (EX_WARN);         /* complain - but not too loud */
     }
     /* is an error for other sets */
     else 
     {
       sprintf(errmsg,
	       "Error: failed to locate dist factors list for %s set %d in file id %d",
	       typeName, set_id,exoid);
       ex_err("ex_get_set_dist_fact",errmsg,exerrval);
       return (EX_FATAL);
     }
   }


/* read in the distribution factors array */

   start[0] = 0;

   count[0] = num_df_in_set;

   if (ncvarget (exoid, dist_id, start, count,
             ex_conv_array(exoid,RTN_ADDRESS,set_dist_fact,
                           (int)num_df_in_set)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
         "Error: failed to get dist factors list for %s set %d in file id %d",
             typeName, set_id,exoid);
     ex_err("ex_get_set_dist_fact",errmsg,exerrval);
     return (EX_FATAL);
   }


   ex_conv_array( exoid, READ_CONVERT, set_dist_fact, num_df_in_set );

   return (EX_NOERR);

}
Пример #10
0
int ex_get_side_set_dist_fact (int   exoid,
                               int   side_set_id,
                               void *side_set_dist_fact)
{

    int dimid, dist_id, side_set_id_ndx;
    long num_df_in_set, count[1], start[1];
    char errmsg[MAX_ERR_LENGTH];

    exerrval = 0; /* clear error code */

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

    if ((dimid = ncdimid (exoid, DIM_NUM_SS)) == -1)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Warning: no side sets stored in file id %d",
                exoid);
        ex_err("ex_get_side_set_dist_fact",errmsg,exerrval);
        return (EX_WARN);
    }

    /* Lookup index of side set id in VAR_SS_IDS array */

    side_set_id_ndx = ex_id_lkup(exoid,VAR_SS_IDS,side_set_id);
    if (exerrval != 0)
    {
        if (exerrval == EX_NULLENTITY)
        {
            sprintf(errmsg,
                    "Warning: side set %d is NULL in file id %d",
                    side_set_id,exoid);
            ex_err("ex_get_side_set_dist_fact",errmsg,EX_MSG);
            return (EX_WARN);
        }
        else
        {
            sprintf(errmsg,
                    "Error: failed to locate side set id %d in VAR_SS_IDS array in file id %d",
                    side_set_id,exoid);
            ex_err("ex_get_side_set_dist_fact",errmsg,exerrval);
            return (EX_FATAL);
        }
    }

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

    if ((dimid = ncdimid (exoid, DIM_NUM_DF_SS(side_set_id_ndx))) == -1)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Warning: dist factors not stored for side set %d in file id %d",
                side_set_id,exoid);
        ex_err("ex_get_side_set_dist_fact",errmsg,exerrval);
        return (EX_WARN);          /* complain - but not too loud */
    }

    if (ncdiminq (exoid, dimid, (char *) 0, &num_df_in_set) == -1)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to get number of dist factors in side set %d in file id %d",
                side_set_id,exoid);
        ex_err("ex_get_side_set_dist_fact",errmsg,exerrval);
        return (EX_FATAL);
    }


    if ((dist_id = ncvarid (exoid, VAR_FACT_SS(side_set_id_ndx))) == -1)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to locate dist factors list for side set %d in file id %d",
                side_set_id,exoid);
        ex_err("ex_get_side_set_dist_fact",errmsg,exerrval);
        return (EX_FATAL);
    }


    /* read in the distribution factors array */

    start[0] = 0;

    count[0] = num_df_in_set;

    if (ncvarget (exoid, dist_id, start, count,
                  ex_conv_array(exoid,RTN_ADDRESS,side_set_dist_fact,
                                (int)num_df_in_set)) == -1)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to get dist factors list for side set %d in file id %d",
                side_set_id,exoid);
        ex_err("ex_get_side_set_dist_fact",errmsg,exerrval);
        return (EX_FATAL);
    }


    ex_conv_array( exoid, READ_CONVERT, side_set_dist_fact, num_df_in_set );

    return (EX_NOERR);

}
Пример #11
0
int ex_get_prop_array (int   exoid,
                       int   obj_type,
                       const char *prop_name,
                       int  *values)
{
   int num_props, i, propid, dimid, iresult;
   int found = FALSE;
   long start[1], count[1], num_obj; 
   nclong *longs;
   char name[MAX_VAR_NAME_LENGTH+1];
   char tmpstr[MAX_VAR_NAME_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 */

/* open appropriate variable, depending on obj_type and prop_name */

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

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

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

     memset(tmpstr, 0, MAX_VAR_NAME_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_get_prop_array",errmsg,exerrval);
       return (EX_FATAL);
     }

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

/* if property is not found, return warning */

   if (!found)
   {
     exerrval = EX_BADPARAM;
     sprintf(errmsg,
       "Warning: object type %d, property %s not defined in file id %d",
        obj_type, prop_name, exoid);
     ex_err("ex_get_prop_array",errmsg,exerrval);
     return (EX_WARN);
   }

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

/* read num_obj values from property variable */

/* application code has allocated an array of ints but netcdf is expecting
   a pointer to nclongs;  if ints are different sizes than nclongs,
   we must allocate an array of nclongs then convert them to ints with ltoi */

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

   if (sizeof(int) == sizeof(nclong)) {
      iresult = ncvarget (exoid, propid, start, count, values);
   } else {
     if (!(longs = static_cast<nclong*>(malloc(num_obj * sizeof(nclong))))) {
       exerrval = EX_MEMFAIL;
       sprintf(errmsg,
               "Error: failed to allocate memory for %s property array for file id %d",
               obj_stype, exoid);
       ex_err("ex_get_prop_array",errmsg,exerrval);
       return (EX_FATAL);
     }
     iresult = ncvarget (exoid, propid, start, count, longs);
   }

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

   if (sizeof(int) != sizeof(nclong)) {
      ltoi (longs, values, num_obj);
      free (longs);
   }

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

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

  exerrval = 0; /* clear error code */

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

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

  /* find what object the entry is in */

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

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

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

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

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


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


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

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

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

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

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


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

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

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

  } /* End NULL object check */

  numel = num_entries_this_obj;

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

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

      numel += num_entries_this_obj;
    }
  }

  offset = id - (numel - num_entries_this_obj);

  /* inquire previously defined variable */

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

  free(stat_vals);
  free(obj_ids);

  /* read values of object variable */

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

  if (end_time_step < 0) {

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

  end_time_step--;

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

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

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

  return (EX_NOERR);
}
Пример #13
0
int main(int argc, char *argv[])
{
    char const *wks_type = "x11";
    int appid,wid,vcid,vfid, sfid, mpid;
    int rlist;
    float U[73][73],V[73][73], PSL[73][73];
    char  smindist0[6] ;
    char  smindist1[5] ;
    char  smindist2[4] ;
    char  title[35];
    char  smindist[7] ;
    char slongitude[100] ;
/*
 * Declare variables for getting information from netCDF file.
 */
    int   uv, p, u_id, v_id, p_id, lon_id, lat_id, FRAME_COUNT;
    int  i, mindistval, longitudeval;
    ng_size_t icount[3];
    float val;
    long  start[2], count[2], lonlen, latlen; 
    float CenLonF;
    char  filenameUV[256];
    char  filenamePsl[256];
    const char *dirUV = _NGGetNCARGEnv("data");
    const char *dirPsl = _NGGetNCARGEnv("data");
/*
 * Generate vector data array
 */
    FRAME_COUNT=13;
/*
 * Open the netCDF file.
 */
    sprintf( filenameUV, "%s/cdf/941110_UV.cdf", dirUV );
    uv = ncopen(filenameUV,NC_NOWRITE);
/*
 * Open the netCDF file.
 */
    sprintf( filenamePsl, "%s/cdf/941110_P.cdf", dirPsl );
    p = ncopen(filenamePsl,NC_NOWRITE);
/*
 * Initialize the high level utility library
 */
    NhlInitialize();
/*
 * Create an application context. Set the app dir to the current
 * directory so the application looks for a resource file in the working
 * directory. 
 */
    rlist = NhlRLCreate(NhlSETRL);
    NhlRLClear(rlist);
    NhlRLSetString(rlist,NhlNappUsrDir,"./");
    NhlCreate(&appid,"vc06",NhlappClass,NhlDEFAULT_APP,rlist);

    if (!strcmp(wks_type,"ncgm") || !strcmp(wks_type,"NCGM")) {
/*
 * Create a meta file workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkMetaName,"./vc06c.ncgm");
        NhlCreate(&wid,"vc06Work",
                  NhlncgmWorkstationClass,NhlDEFAULT_APP,rlist);
    }
    else if (!strcmp(wks_type,"x11") || !strcmp(wks_type,"X11")) {
/*
 * Create an X workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetInteger(rlist,NhlNwkPause,True);
        NhlCreate(&wid,"vc06Work",NhlcairoWindowWorkstationClass,appid,rlist);
    }

    else if (!strcmp(wks_type,"oldps") || !strcmp(wks_type,"OLDPS")) {
/*
 * Create an older-style PostScript workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkPSFileName,"vc06c.ps");
        NhlCreate(&wid,"vc06Work",NhlpsWorkstationClass,appid,rlist);
    }
    else if (!strcmp(wks_type,"oldpdf") || !strcmp(wks_type,"OLDPDF")) {
/*
 * Create an older-style PDF workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkPDFFileName,"vc06c.pdf");
        NhlCreate(&wid,"vc06Work",NhlpdfWorkstationClass,appid,rlist);
    }
    else if (!strcmp(wks_type,"pdf") || !strcmp(wks_type,"PDF") ||
             !strcmp(wks_type,"ps") || !strcmp(wks_type,"PS")) {
/*
 * Create a cairo PS/PDF workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkFileName,"vc06c");
        NhlRLSetString(rlist,NhlNwkFormat,(char*)wks_type);
        NhlCreate(&wid,"vc06Work",NhlcairoDocumentWorkstationClass,appid,rlist);
    }
    else if (!strcmp(wks_type,"png") || !strcmp(wks_type,"PNG")) {
/*
 * Create a cairo PNG workstation.
 */
        NhlRLClear(rlist);
        NhlRLSetString(rlist,NhlNwkFileName,"vc06c");
        NhlRLSetString(rlist,NhlNwkFormat,(char*)wks_type);
        NhlCreate(&wid,"vc06Work",NhlcairoImageWorkstationClass,appid,rlist);
    }
/*
 * Get the U and V and lat/lon dimensions.
 */
    lat_id = ncdimid(uv,"lat");
    lon_id = ncdimid(uv,"lon");
    u_id = ncvarid(uv,"u");
    v_id = ncvarid(uv,"v");
    ncdiminq(uv,lat_id,(char *)0,&latlen);
    ncdiminq(uv,lon_id,(char *)0,&lonlen);

    start[0] = start[1] = 0;
    count[0] = latlen; count[1] = lonlen;
    ncvarget(uv,u_id,(long const *)start,(long const *)count,U);
    ncvarget(uv,v_id,(long const *)start,(long const *)count,V);
/*
 * Create a VectorField data object using the data set defined above.
 * By default the array bounds will define the data boundaries (zero-based,
 * as in C language conventions)
 */
    icount[0] = latlen; icount[1] = lonlen;

    NhlRLClear(rlist);
    NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,&U[0][0],2,icount);
    NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,&V[0][0],2,icount);
    NhlRLSetFloat(rlist,NhlNvfXCStartV, -180.0);
    NhlRLSetFloat(rlist,NhlNvfXCEndV, 180.0);
    NhlRLSetFloat(rlist,NhlNvfYCStartV,-90.0);
    NhlRLSetFloat(rlist,NhlNvfYCEndV, 90.0);
    NhlCreate(&vfid,"vectorfield",NhlvectorFieldClass,appid,rlist);
/*
 * Get the PSL and lat/lon dimensions.
 */
    lat_id = ncdimid(p,"lat");
    lon_id = ncdimid(p,"lon");
    p_id = ncvarid(p,"Psl");
    ncdiminq(p,lat_id,(char *)0,&latlen);
    ncdiminq(p,lon_id,(char *)0,&lonlen);

    start[0] = start[1] = 0;
    count[0] = latlen; count[1] = lonlen;
    ncvarget(p,p_id,(long const *)start,(long const *)count,PSL);
    icount[0] = latlen; icount[1] = lonlen;

    NhlRLClear(rlist);
    NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,&PSL[0][0],2,icount);
    NhlRLSetFloat(rlist,NhlNsfXCStartV, -180.0);
    NhlRLSetFloat(rlist,NhlNsfXCEndV, 180.0);
    NhlRLSetFloat(rlist,NhlNsfYCStartV, -90.0);
    NhlRLSetFloat(rlist,NhlNsfYCEndV, 90.0);
    NhlCreate(&sfid,"scalarfield",NhlscalarFieldClass,appid,rlist);
/*
 * Create a VectorPlot object, supplying the VectorField object as data
 * Setting vcMonoFillArrowFillColor False causes VectorPlot to color the
 * vector arrows individually based, by default, on the vector magnitude.
 * Also supply the ScalarField object that will be used to determine the
 * color of each individual vector arrow.
 * Setting vcMonoVectorLineColor False causes VectorPlot to color the
 * vector arrows individually and setting vcUseScalarArray True results
 * in VectorPlot applying the colors based on the contents of the scalarfield.
 */
    NhlRLClear(rlist);
    NhlRLSetFloat(rlist,NhlNvcRefMagnitudeF, 20.0);
    NhlRLSetString(rlist,NhlNvcUseScalarArray, "True");
    NhlRLSetString(rlist,NhlNvcFillArrowsOn, "True");
    NhlRLSetString(rlist,NhlNvcMonoFillArrowFillColor, "False");
    NhlRLSetFloat(rlist,NhlNvcMinFracLengthF, 0.25);
    NhlRLSetInteger(rlist,NhlNvcVectorFieldData,vfid);
    NhlRLSetInteger(rlist,NhlNvcScalarFieldData,sfid);
    NhlCreate(&vcid,"vectorplot",NhlvectorPlotClass,wid,rlist);

    NhlRLClear(rlist);
    NhlRLSetString(rlist,NhlNmpProjection, "ORTHOGRAPHIC");
    NhlRLSetFloat(rlist,NhlNmpCenterLatF, 50);
    NhlCreate(&mpid,"mapplot",NhlmapPlotClass,wid,rlist);

    NhlAddOverlay(mpid,vcid, -1);
/*
 * Strings used to create fixed length numbers
 */
    strcpy(smindist0 ,"0.000");
    strcpy(smindist1 ,"0.00");
    strcpy( smindist2 ,"0.0");
/*
 * Create FRAME_COUNT frames, increasing the value of vcMinDistanceF
 * and decreasing the value of mpCenterLonF at each successive frame.
 *
 * Note that the first frame and the last frame are equivalent in
 * longitude.
 */
    for(i = (FRAME_COUNT-1);i > -1; i--){
        NhlRLClear(rlist);       
        CenLonF =  i * 360./(FRAME_COUNT-1);
        NhlRLSetFloat(rlist,NhlNmpCenterLonF,CenLonF);
        NhlSetValues(mpid,rlist);
/*
 * create fixed length strings representing the current longitude
 * and the value of vcMinDistanceF
 */
        longitudeval = (int)(i * 360./(FRAME_COUNT-1) + 0.5);

        sprintf(slongitude,"%d:S:o:N:",longitudeval);

        val = ((FRAME_COUNT-1) - i) * 0.0175/(FRAME_COUNT-1);
        mindistval = (int)(10000*val + 0.5);

        if (mindistval < 10){
            sprintf(smindist,"%s%d",smindist0,mindistval);
        }
        else {
            if (mindistval < 100){
                sprintf(smindist,"%s%d",smindist1,mindistval);
            }
            else {
                sprintf(smindist,"%s%d",smindist2,mindistval);
            }
        }

        NhlRLClear(rlist);
        
        strcpy(title,"Varying vcMinDistanceF :: ");
        strcat(title,smindist);
        
        NhlRLSetString(rlist,NhlNtiMainString,title);
        NhlRLSetString(rlist,NhlNtiXAxisString,slongitude);
        NhlRLSetFloat(rlist,NhlNvcMinDistanceF,val);
        NhlSetValues(vcid,rlist);

        NhlDraw(mpid);
        NhlFrame(wid);

    }/*end for*/
/*
 * Destroy the objects created, close the HLU library and exit.
 */
    NhlDestroy(mpid);
    NhlDestroy(wid);
    NhlDestroy(appid);
    NhlClose();
    exit(0);
}
Пример #14
0
int ex_inquire (int   exoid,
                int   req_info,
                int  *ret_int,
                void *ret_float,
                char *ret_char)
{
   int dimid, varid, i, tmp_num, *ids;
   long ldum, num_sets, start[2], count[2];
   nclong *stat_vals;
   char  errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

   switch (req_info)
   {
     case EX_INQ_FILE_TYPE:

       /* obsolete call */
       /*returns "r" for regular EXODUS II file or "h" for history EXODUS file*/

       *ret_char = '\0';
       exerrval = EX_BADPARAM;
       sprintf(errmsg,
              "Warning: file type inquire is obsolete");
       ex_err("ex_inquire",errmsg,exerrval);
       return (EX_WARN);

     case EX_INQ_API_VERS:

/*     returns the EXODUS II API version number */

       if (ncattget (exoid, NC_GLOBAL, ATT_API_VERSION, ret_float) == -1)
       {  /* try old (prior to db version 2.02) attribute name */
         if (ncattget (exoid, NC_GLOBAL, ATT_API_VERSION_BLANK,ret_float) == -1)
         {
           exerrval = ncerr;
           sprintf(errmsg,
             "Error: failed to get EXODUS API version for file id %d", exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }
       }

       break;

     case EX_INQ_DB_VERS:

/*     returns the EXODUS II database version number */

       if (ncattget (exoid, NC_GLOBAL, ATT_VERSION, ret_float) == -1)
       {
         exerrval = ncerr;
         sprintf(errmsg,
          "Error: failed to get EXODUS database version for file id %d", exoid);
         ex_err("ex_inquire",errmsg,exerrval);
         return (EX_FATAL);
       }

       break;

     case EX_INQ_LIB_VERS:

/*     returns the EXODUS II Library version number */

       flt_cvt((float *)ret_float, EX_API_VERS);

       break;

     case EX_INQ_TITLE:

/*     returns the title of the database */

       if (ncattget (exoid, NC_GLOBAL, ATT_TITLE, ret_char) == -1)
       {
         *ret_char = '\0';
         exerrval = ncerr;
         sprintf(errmsg,
             "Error: failed to get database title for file id %d", exoid);
         ex_err("ex_inquire",errmsg,exerrval);
         return (EX_FATAL);
       }

       break;

     case EX_INQ_DIM:

/*     returns the dimensionality (2 or 3, for 2-d or 3-d) of the database */

       if ((dimid = ncdimid (exoid, DIM_NUM_DIM)) == -1)
       {
         *ret_int = 0;
         exerrval = ncerr;
         sprintf(errmsg,
                "Error: failed to locate database dimensionality in file id %d",
                exoid);
         ex_err("ex_inquire",errmsg,exerrval);
         return (EX_FATAL);
       }

       if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
       {
         *ret_int = 0;
         exerrval = ncerr;
         sprintf(errmsg,
            "Error: failed to get database dimensionality for file id %d",
            exoid);
         ex_err("ex_inquire",errmsg,exerrval);
         return (EX_FATAL);
       }
       *ret_int = ldum;

       break;

     case EX_INQ_NODES:

/*     returns the number of nodes */

       if ((dimid = ncdimid (exoid, DIM_NUM_NODES)) == -1)
       {
         *ret_int = 0;
       } else {

         if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
           {
             *ret_int = 0;
             exerrval = ncerr;
             sprintf(errmsg,
                     "Error: failed to get number of nodes for file id %d",
                     exoid);
             ex_err("ex_inquire",errmsg,exerrval);
             return (EX_FATAL);
           }
         *ret_int = ldum;
       }
       break;

     case EX_INQ_ELEM:

/*     returns the number of elements */

       if ((dimid = ncdimid (exoid, DIM_NUM_ELEM)) == -1)
       {
         *ret_int = 0;
       } else {

         if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
           {
             *ret_int = 0;
             exerrval = ncerr;
             sprintf(errmsg,
                     "Error: failed to get number of elements for file id %d",
                     exoid);
             ex_err("ex_inquire",errmsg,exerrval);
             return (EX_FATAL);
           }
         *ret_int = ldum;
       }
       break;

     case EX_INQ_ELEM_BLK:

/*     returns the number of element blocks */

       if ((dimid = ncdimid (exoid, DIM_NUM_EL_BLK)) == -1)
       {
         *ret_int = 0;
       } else {

         if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
           {
             *ret_int = 0;
             exerrval = ncerr;
             sprintf(errmsg,
                     "Error: failed to get number of element blocks for file id %d",
                     exoid);
             ex_err("ex_inquire",errmsg,exerrval);
             return (EX_FATAL);
           }
         *ret_int = ldum;
       }

       break;

     case EX_INQ_NODE_SETS:

/*     returns the number of node sets */

       if ((dimid = ncdimid (exoid, DIM_NUM_NS)) < 0)
         *ret_int = 0;      /* no node sets defined */
       else
       {
         if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
         {
           *ret_int = 0;
           exerrval = ncerr;
           sprintf(errmsg,
                 "Error: failed to get number of node sets in file id %d",
                  exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }
         *ret_int = ldum;
       }

       break;

     case EX_INQ_NS_NODE_LEN:

/*     returns the length of the concatenated node sets node list */

       *ret_int = 0;       /* default value if no node sets are defined */
       if ((dimid = ncdimid (exoid, DIM_NUM_NS)) != -1 )
       {
         if (ncdiminq (exoid, dimid, (char *) 0, &num_sets) == -1)
         {
           exerrval = ncerr;
           sprintf(errmsg,
                 "Error: failed to get number of node sets in file id %d",
                  exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }


         if (!(ids =  malloc(num_sets*sizeof(int))))
         {
           exerrval = EX_MEMFAIL;
           sprintf(errmsg,
             "Error: failed to allocate memory for node set ids for file id %d",
              exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }

         if (ex_get_node_set_ids (exoid, ids) == EX_FATAL)
         {
           sprintf(errmsg,
                   "Error: failed to get node sets in file id %d",
                    exoid);
           /* pass back error code from ex_get_node_set_ids (in exerrval) */
           ex_err("ex_inquire",errmsg,exerrval);
           free (ids);
           return (EX_FATAL);
         }
         /* allocate space for stat array */
         if (!(stat_vals = malloc((int)num_sets*sizeof(nclong))))
         {
           exerrval = EX_MEMFAIL;
           free (ids);
           sprintf(errmsg,
    "Error: failed to allocate memory for node set status array for file id %d",
                   exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }

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

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

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

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

           if (stat_vals[i] == 0) /* is this object null? */
              continue;

           if ((dimid = ncdimid (exoid, DIM_NUM_NOD_NS(i+1))) == -1)
           {
             *ret_int = 0;
             exerrval = ncerr;
             sprintf(errmsg,
         "Error: failed to locate number of nodes in node set %d in file id %d",
                  ids[i],exoid);
             ex_err("ex_inquire",errmsg,exerrval);
             free (ids);
             free (stat_vals);
             return (EX_FATAL);
           }

           if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
           {
             *ret_int = 0;
             exerrval = ncerr;
             sprintf(errmsg,
            "Error: failed to get number of nodes in node set %d in file id %d",
                  ids[i],exoid);
             ex_err("ex_inquire",errmsg,exerrval);
             free (stat_vals);
             free (ids);
             return (EX_FATAL);
           }

           *ret_int += ldum;
         }

         free (stat_vals);
         free (ids);
       }

       break;

     case EX_INQ_NS_DF_LEN:

/*     returns the length of the concatenated node sets dist factor list */

/*
     Determine the concatenated node sets distribution factor length:

        1. Get the node set ids list.
        2. Check see if the dist factor variable for a node set id exists.
        3. If it exists, goto step 4, else the length is zero.
        4. Get the dimension of the number of nodes in the node set -0
             use this value as the length as by definition they are the same.
        5. Sum the individual lengths for the total list length.
*/

       *ret_int = 0;    /* default value if no node sets defined */

       if ((dimid = ncdimid (exoid, DIM_NUM_NS))  != -1)
       {
         if (ncdiminq (exoid, dimid, (char *) 0, &num_sets) == -1)
         {
           exerrval = ncerr;
           sprintf(errmsg,
                 "Error: failed to get number of node sets in file id %d",
                  exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }


         if (!(ids = malloc(num_sets*sizeof(int))))
         {
           exerrval = EX_MEMFAIL;
           sprintf(errmsg,
             "Error: failed to allocate memory for node set ids for file id %d",
              exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }

         if (ex_get_node_set_ids (exoid, ids) == EX_FATAL)
         {
           sprintf(errmsg,
                   "Error: failed to get node sets in file id %d",
                    exoid);
           /* pass back error code from ex_get_node_set_ids (in exerrval) */
           ex_err("ex_inquire",errmsg,exerrval);
           free (ids);
           return (EX_FATAL);
         }

         for (i=0; i<num_sets; i++)
         {
           if (ncvarid (exoid, VAR_FACT_NS(i+1)) == -1)
           {
             if (ncerr == NC_ENOTVAR)
             {
               ldum = 0;        /* this dist factor doesn't exist */
             }
             else
             {
               *ret_int = 0;
               exerrval = ncerr;
               sprintf(errmsg,
    "Error: failed to locate number of dist fact for node set %d in file id %d",
                        ids[i], exoid);
               ex_err("ex_inquire",errmsg,exerrval);
               free (ids);
               return (EX_FATAL);
             }
           }
           else
           {
             if ((dimid = ncdimid (exoid, DIM_NUM_NOD_NS(i+1))) == -1)
             {
               *ret_int = 0;
               exerrval = ncerr;
               sprintf(errmsg,
         "Error: failed to locate number of nodes in node set %d in file id %d",
                       ids[i], exoid);
               ex_err("ex_inquire",errmsg,exerrval);
               free (ids);
               return (EX_FATAL);
             }
             if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
             {
               *ret_int = 0;
               exerrval = ncerr;
               sprintf(errmsg,
            "Error: failed to get number of nodes in node set %d in file id %d",
                       ids[i],exoid);
               ex_err("ex_inquire",errmsg,exerrval);
               free(ids);
               return (EX_FATAL);
             }
           }
           *ret_int += ldum;
         }
         free(ids);
       }

       break;

     case EX_INQ_SIDE_SETS:

/*     returns the number of side sets */

       *ret_int = 0;     /* default return value */

       if ((dimid = ncdimid (exoid, DIM_NUM_SS)) != -1)
       {
         if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
         {
           exerrval = ncerr;
           sprintf(errmsg,
                 "Error: failed to get number of side sets in file id %d",
                  exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }
         *ret_int = ldum;
       }

       break;

     case EX_INQ_SS_NODE_LEN:

/*     returns the length of the concatenated side sets node list */

       *ret_int = 0;     /* default return value */

       if ((dimid = ncdimid (exoid, DIM_NUM_SS)) != -1)
       {
         if (ncdiminq (exoid, dimid, (char *) 0, &num_sets) == -1)
         {
           exerrval = ncerr;
           sprintf(errmsg,
                 "Error: failed to get number of side sets in file id %d",
                  exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }


         if (!(ids = malloc(num_sets*sizeof(int))))
         {
           exerrval = EX_MEMFAIL;
           sprintf(errmsg,
             "Error: failed to allocate memory for side set ids for file id %d",
              exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }

         if (ex_get_side_set_ids (exoid, ids) == EX_FATAL)
         {
           sprintf(errmsg,
                  "Error: failed to get side set ids in file id %d",
                   exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           free(ids);
           return (EX_FATAL);
         }

         /* allocate space for stat array */
         if (!(stat_vals = malloc((int)num_sets*sizeof(nclong))))
         {
           exerrval = EX_MEMFAIL;
           free (ids);
           sprintf(errmsg,
    "Error: failed to allocate memory for side set status array for file id %d",
                   exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }
         /* get variable id of status array */
         if ((varid = ncvarid (exoid, VAR_SS_STAT)) != -1)
         {
         /* if status array exists, use it, otherwise assume, object exists
            to be backward compatible */

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

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

         /* walk id list, get each side set node length and sum for total */

         for (i=0; i<num_sets; i++)
         {
           if (stat_vals[i] == 0) /* is this object null? */
             continue;

           if (ex_get_side_set_node_list_len(exoid, ids[i], &tmp_num) == -1)
           {
             *ret_int = 0;
             exerrval = ncerr;
             sprintf(errmsg,
                 "Error: failed to side set %d node length in file id %d",
                  ids[i],exoid);
             ex_err("ex_inquire",errmsg,exerrval);
             free(stat_vals);
             free(ids);
             return (EX_FATAL);
           }
           *ret_int += tmp_num;
         }

         free(stat_vals);
         free (ids);
       }

       break;

     case EX_INQ_SS_ELEM_LEN:
/*     returns the length of the concatenated side sets element list */
       EX_GET_CONCAT_SET_LEN(ret_int,"side",EX_SIDE_SET,DIM_NUM_SS,VAR_SS_STAT,DIM_NUM_SIDE_SS,0);
       break;

     case EX_INQ_SS_DF_LEN:

/*     returns the length of the concatenated side sets dist factor list */

/*
     Determine the concatenated side sets distribution factor length:

        1. Get the side set ids list.
        2. Check see if the dist factor dimension for a side set id exists.
        3. If it exists, goto step 4, else set the individual length to zero.
        4. Sum the dimension value into the running total length.
*/

       *ret_int = 0;

       /* first check see if any side sets exist */

       if ((dimid = ncdimid (exoid, DIM_NUM_SS))  != -1)
       {
         if (ncdiminq (exoid, dimid, (char *) 0, &num_sets) == -1)
         {
           exerrval = ncerr;
           sprintf(errmsg,
                 "Error: failed to get number of side sets in file id %d",
                  exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }


         if (!(ids = malloc(num_sets*sizeof(int))))
         {
           exerrval = EX_MEMFAIL;
           sprintf(errmsg,
             "Error: failed to allocate memory for side set ids for file id %d",
              exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }

         if (ex_get_side_set_ids (exoid, ids) == EX_FATAL)
         {
           sprintf(errmsg,
                   "Error: failed to get side sets in file id %d",
                    exoid);
           /* pass back error code from ex_get_side_set_ids (in exerrval) */
           ex_err("ex_inquire",errmsg,exerrval);
           free (ids);
           return (EX_FATAL);
         }

         for (i=0; i<num_sets; i++)
         {
           if ((dimid = ncdimid (exoid, DIM_NUM_DF_SS(i+1))) == -1)
           {
             if (ncerr == NC_EBADDIM)
             {
               ldum = 0;        /* this dist factor doesn't exist */
             }
             else
             {
               *ret_int = 0;
               exerrval = ncerr;
               sprintf(errmsg,
    "Error: failed to locate number of dist fact for side set %d in file id %d",
                        ids[i], exoid);
               ex_err("ex_inquire",errmsg,exerrval);
               free (ids);
               return (EX_FATAL);
             }
           }
           else
           {
             if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
             {
               *ret_int = 0;
               exerrval = ncerr;
               sprintf(errmsg,
     "Error: failed to get number of dist factors in side set %d in file id %d",
                       ids[i], exoid);
               ex_err("ex_inquire",errmsg,exerrval);
               free (ids);
               return (EX_FATAL);
             }
           }
           *ret_int += ldum;
         }
         free (ids);
       }

       break;

     case EX_INQ_QA:

/*     returns the number of QA records */

       if ((dimid = ncdimid (exoid, DIM_NUM_QA)) < 0)
         *ret_int = 0;      /* no QA records stored */
       else
       {
         if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
         {
           *ret_int = 0;
           exerrval = ncerr;
           sprintf(errmsg,
                  "Error: failed to get number of QA records in file id %d",
                   exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }
         *ret_int = ldum;
       }

       break;

     case EX_INQ_INFO:

/*     returns the number of information records */

       if ((dimid = ncdimid (exoid, DIM_NUM_INFO)) < 0)
         *ret_int = 0;        /* no information records stored */
       else
       {
         if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
         {
           *ret_int = 0;
           exerrval = ncerr;
           sprintf(errmsg,
                  "Error: failed to get number of info records in file id %d",
                   exoid);
           ex_err("ex_inquire",errmsg,exerrval);
           return (EX_FATAL);
         }
         *ret_int = ldum;
       }
       break;

     case EX_INQ_TIME:

/*     returns the number of time steps stored in the database; we find 
 *     this out by inquiring the maximum record number of the "unlimited" 
 *     dimension
 */

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

       if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
       {
         *ret_int = 0;
         exerrval = ncerr;
         sprintf(errmsg,
                "Error: failed to get time dimension in file id %d",
                 exoid);
         ex_err("ex_inquire",errmsg,exerrval);
         return (EX_FATAL);
       }
       *ret_int = ldum;

       break;
     case EX_INQ_EB_PROP:

/*     returns the number of element block properties */

       *ret_int = ex_get_num_props (exoid, EX_ELEM_BLOCK);
       break;

     case EX_INQ_NS_PROP:

/*     returns the number of node set properties */

       *ret_int = ex_get_num_props (exoid, EX_NODE_SET);
       break;

     case EX_INQ_SS_PROP:

/*     returns the number of side set properties */

       *ret_int = ex_get_num_props (exoid, EX_SIDE_SET);
       break;

     case EX_INQ_ELEM_MAP:

/*     returns the number of element maps */

       if ((dimid = ncdimid (exoid, DIM_NUM_EM)) == -1)
       {
         /* no element maps so return 0 */

         *ret_int = 0;
         return (EX_NOERR);
       }

       if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
       {
         *ret_int = 0;
         exerrval = ncerr;
         sprintf(errmsg,
           "Error: failed to get number of element maps for file id %d",
           exoid);
         ex_err("ex_inquire",errmsg,exerrval);
         return (EX_FATAL);
       }
       *ret_int = ldum;

       break;

     case EX_INQ_EM_PROP:

/*     returns the number of element map properties */

       *ret_int = ex_get_num_props (exoid, EX_ELEM_MAP);
       break;

     case EX_INQ_NODE_MAP:

/*     returns the number of node maps */

       if ((dimid = ncdimid (exoid, DIM_NUM_NM)) == -1)
       {
         /* no node maps so return 0 */

         *ret_int = 0;
         return (EX_NOERR);
       }

       if (ncdiminq (exoid, dimid, (char *) 0, &ldum) == -1)
       {
         *ret_int = 0;
         exerrval = ncerr;
         sprintf(errmsg,
           "Error: failed to get number of node maps for file id %d",
           exoid);
         ex_err("ex_inquire",errmsg,exerrval);
         return (EX_FATAL);
       }
       *ret_int = ldum;

       break;

     case EX_INQ_NM_PROP:
/*     returns the number of element map properties */
       *ret_int = ex_get_num_props (exoid, EX_NODE_MAP);
       break;

     case EX_INQ_EDGE:
/*     returns the number of edges (defined across all edge blocks). */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_EDGE, 1);
       break;

     case EX_INQ_EDGE_BLK:
/*     returns the number of edge blocks. */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_ED_BLK, 1);
       break;

     case EX_INQ_EDGE_SETS:
/*     returns the number of edge sets. */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_ES, 1);
       break;

     case EX_INQ_ES_LEN:
/*     returns the length of the concatenated edge set edge list. */
       EX_GET_CONCAT_SET_LEN(ret_int,"edge",EX_EDGE_SET,DIM_NUM_ES,VAR_ES_STAT,DIM_NUM_EDGE_ES,0);
       break;

     case EX_INQ_ES_DF_LEN:
/*     returns the length of the concatenated edge set distribution factor list. */
       EX_GET_CONCAT_SET_LEN(ret_int,"edge",EX_EDGE_SET,DIM_NUM_ES,VAR_ES_STAT,DIM_NUM_DF_ES,1);
       break;

     case EX_INQ_EDGE_PROP:
/*     returns the number of integer properties stored for each edge block. This includes the "ID" property. */
       *ret_int = ex_get_num_props( exoid, EX_EDGE_BLOCK );
       break;

     case EX_INQ_ES_PROP:
/*     returns the number of integer properties stored for each edge set.. This includes the "ID" property */
       *ret_int = ex_get_num_props( exoid, EX_EDGE_SET );
       break;

     case EX_INQ_FACE:
/*     returns the number of faces (defined across all face blocks). */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_FACE, 1);
       break;

     case EX_INQ_FACE_BLK:
/*     returns the number of edge blocks. */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_FA_BLK, 1);
       break;

     case EX_INQ_FACE_SETS:
/*     returns the number of edge sets. */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_FS, 1);
       break;

     case EX_INQ_FS_LEN:
/*     returns the length of the concatenated edge set edge list. */
       EX_GET_CONCAT_SET_LEN(ret_int,"face",EX_FACE_SET,DIM_NUM_FS,VAR_FS_STAT,DIM_NUM_FACE_FS,0);
       break;

     case EX_INQ_FS_DF_LEN:
/*     returns the length of the concatenated edge set distribution factor list. */
       EX_GET_CONCAT_SET_LEN(ret_int,"face",EX_FACE_SET,DIM_NUM_FS,VAR_FS_STAT,DIM_NUM_DF_FS,1);
       break;

     case EX_INQ_FACE_PROP:
/*     returns the number of integer properties stored for each edge block. This includes the "ID" property. */
       *ret_int = ex_get_num_props( exoid, EX_FACE_BLOCK );
       break;

     case EX_INQ_FS_PROP:
/*     returns the number of integer properties stored for each edge set.. This includes the "ID" property */
       *ret_int = ex_get_num_props( exoid, EX_FACE_SET );
       break;

     case EX_INQ_ELEM_SETS:
/*     returns the number of element sets. */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_ELS, 1);
       break;

     case EX_INQ_ELS_LEN:
/*     returns the length of the concatenated element set element list. */
       EX_GET_CONCAT_SET_LEN(ret_int,"element",EX_ELEM_SET,DIM_NUM_ELS,VAR_ELS_STAT,DIM_NUM_ELE_ELS,0);
       break;

     case EX_INQ_ELS_DF_LEN:
/*     returns the length of the concatenated element set distribution factor list. */
       EX_GET_CONCAT_SET_LEN(ret_int,"element",EX_ELEM_SET,DIM_NUM_ELS,VAR_ELS_STAT,DIM_NUM_DF_ELS,1);
       break;

    case EX_INQ_ELS_PROP:
/*     returns the number of integer properties stored for each element set. */
       *ret_int = ex_get_num_props( exoid, EX_ELEM_SET );
       break;

    case EX_INQ_EDGE_MAP:
/*     returns the number of edge sets. */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_EDM, 1);
       break;

    case EX_INQ_FACE_MAP:
/*     returns the number of edge sets. */
       EX_GET_DIMENSION_VALUE(ret_int, 0, DIM_NUM_FAM, 1);
       break;


     default:
       *ret_int = 0;
       exerrval = EX_FATAL;
       sprintf(errmsg, "Error: invalid inquiry %d", req_info);
       ex_err("ex_inquire",errmsg,exerrval);
       return(EX_FATAL);
   }
   return (EX_NOERR);
}
Пример #15
0
int ex_get_set (int   exoid,
                int   set_type,
                int   set_id,
                int  *set_entry_list,
                int  *set_extra_list)
{

    int dimid, entry_list_id, extra_list_id, iresult;
    int set_id_ndx;
    long num_entry_in_set, count[1], start[1];
    nclong *longs;
    char errmsg[MAX_ERR_LENGTH];
    char* typeName;
    char* dimptr;
    char* idsptr;
    char* numentryptr;
    char* entryptr;
    char* extraptr;

    exerrval = 0; /* clear error code */

    /* setup pointers based on set_type
     NOTE: there is another block that sets more stuff later ... */
    if (set_type == EX_NODE_SET) {
        typeName = "node";
        dimptr = DIM_NUM_NS;
        idsptr = VAR_NS_IDS;
    }
    else if (set_type == EX_EDGE_SET) {
        typeName = "edge";
        dimptr = DIM_NUM_ES;
        idsptr = VAR_ES_IDS;
    }
    else if (set_type == EX_FACE_SET) {
        typeName = "face";
        dimptr = DIM_NUM_FS;
        idsptr = VAR_FS_IDS;
    }
    else if (set_type == EX_SIDE_SET) {
        typeName = "side";
        dimptr = DIM_NUM_SS;
        idsptr = VAR_SS_IDS;
    }
    else if (set_type == EX_ELEM_SET) {
        typeName = "elem";
        dimptr = DIM_NUM_ELS;
        idsptr = VAR_ELS_IDS;
    }
    else {
        exerrval = EX_FATAL;
        sprintf(errmsg,
                "Error: invalid set type (%d)", set_type);
        ex_err("ex_put_set_param",errmsg,exerrval);
        return (EX_FATAL);
    }

    /* first check if any sets are specified */

    if ((dimid = ncdimid (exoid, dimptr)) == -1)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Warning: no %s sets stored in file id %d",
                typeName, exoid);
        ex_err("ex_get_set",errmsg,exerrval);
        return (EX_WARN);
    }

    /* Lookup index of set id in VAR_*S_IDS array */

    set_id_ndx = ex_id_lkup(exoid,idsptr,set_id);
    if (exerrval != 0)
    {
        if (exerrval == EX_NULLENTITY)
        {
            sprintf(errmsg,
                    "Warning: %s set %d is NULL in file id %d",
                    typeName, set_id,exoid);
            ex_err("ex_get_set",errmsg,EX_MSG);
            return (EX_WARN);
        }
        else
        {

            sprintf(errmsg,
                    "Error: failed to locate %s set id %d in VAR_*S_IDS array in file id %d",
                    typeName, set_id,exoid);
            ex_err("ex_get_set",errmsg,exerrval);
            return (EX_FATAL);
        }
    }

    /* setup more pointers based on set_type */

    if (set_type == EX_NODE_SET) {
        numentryptr = DIM_NUM_NOD_NS(set_id_ndx);
        entryptr = VAR_NODE_NS(set_id_ndx);
        extraptr = NULL;
    }
    else if (set_type == EX_EDGE_SET) {
        numentryptr = DIM_NUM_EDGE_ES(set_id_ndx);
        entryptr = VAR_EDGE_ES(set_id_ndx);
        extraptr = VAR_ORNT_ES(set_id_ndx);
    }
    else if (set_type == EX_FACE_SET) {
        numentryptr = DIM_NUM_FACE_FS(set_id_ndx);
        entryptr = VAR_FACE_FS(set_id_ndx);
        extraptr = VAR_ORNT_FS(set_id_ndx);
    }
    else if (set_type == EX_SIDE_SET) {
        numentryptr = DIM_NUM_SIDE_SS(set_id_ndx);
        entryptr = VAR_ELEM_SS(set_id_ndx);
        extraptr = VAR_SIDE_SS(set_id_ndx);
    }
    if (set_type == EX_ELEM_SET) {
        numentryptr = DIM_NUM_ELE_ELS(set_id_ndx);
        entryptr = VAR_ELEM_ELS(set_id_ndx);
        extraptr = NULL;
    }

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

    if ((dimid = ncdimid (exoid, numentryptr)) == -1)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to locate number of entries in %s set %d in file id %d",
                typeName, set_id,exoid);
        ex_err("ex_get_set",errmsg,exerrval);
        return (EX_FATAL);
    }

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

    if ((entry_list_id = ncvarid (exoid, entryptr)) == -1)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to locate entry list for %s set %d in file id %d",
                typeName, set_id,exoid);
        ex_err("ex_get_set",errmsg,exerrval);
        return (EX_FATAL);
    }

    /* only do extra list for edge, face and side sets */

    if (extraptr)
    {
        if ((extra_list_id = ncvarid (exoid, extraptr)) == -1)
        {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: failed to locate extra list for %s set %d in file id %d",
                    typeName, set_id,exoid);
            ex_err("ex_get_set",errmsg,exerrval);
            return (EX_FATAL);
        }
    }


    /* read in the entry list and extra list arrays */

    /* application code has allocated an array of ints but netcdf is expecting
       a pointer to nclongs;  if ints are different sizes than nclongs,
       we must allocate an array of nclongs then convert them to ints with ltoi */

    start[0] = 0;
    count[0] = num_entry_in_set;

    if (sizeof(int) == sizeof(nclong)) {
        iresult = ncvarget(exoid, entry_list_id, start, count, set_entry_list);
    } else {
        if (!(longs = malloc(num_entry_in_set * sizeof(nclong)))) {
            exerrval = EX_MEMFAIL;
            sprintf(errmsg,
                    "Error: failed to allocate memory for entry list for %s set %d for file id %d",
                    typeName, set_id, exoid);
            ex_err("ex_get_set",errmsg,exerrval);
            return (EX_FATAL);
        }
        iresult = ncvarget (exoid, entry_list_id, start, count, longs);
    }

    if (iresult)
    {
        exerrval = ncerr;
        sprintf(errmsg,
                "Error: failed to get entry list for %s set %d in file id %d",
                typeName, set_id,exoid);
        ex_err("ex_get_set",errmsg,exerrval);
        return (EX_FATAL);
    }

    /* only do extra list for edge, face and side sets */

    if (extraptr)
    {
        if (sizeof(int) != sizeof(nclong)) {
            ltoi (longs, set_entry_list, num_entry_in_set);
        }

        if (sizeof(int) == sizeof(nclong)) {
            iresult = ncvarget(exoid, extra_list_id, start, count, set_extra_list);
        } else {
            iresult = ncvarget (exoid, extra_list_id, start, count, longs);
        }

        if (iresult == -1)
        {
            exerrval = ncerr;
            sprintf(errmsg,
                    "Error: failed to get extra list for %s set %d in file id %d",
                    typeName, set_id,exoid);
            ex_err("ex_get_set",errmsg,exerrval);
            return (EX_FATAL);
        }
    }

    if (sizeof(int) != sizeof(nclong)) {
        ltoi (longs, set_extra_list, num_entry_in_set);
        free (longs);
    }

    return (EX_NOERR);

}
Пример #16
0
int ex_get_object_truth_vector (int  exoid,
				const char *obj_type,
				int  entity_id,
				int  num_var,
				int *var_vec)
{
   int varid, tabid, i, iresult, ent_ndx;
   long num_var_db = -1;
   long start[2], count[2]; 
   nclong *longs;
   char errmsg[MAX_ERR_LENGTH];
   const char* routine = "ex_get_object_truth_vector";

   /*
    * The ent_type and the var_name are used to build the netcdf
    * variables name.  Normally this is done via a macro defined in
    * exodusII_int.h
    */
   const char* ent_type = NULL;
   const char* var_name = NULL;

   exerrval = 0; /* clear error code */
   
   if (*obj_type == 'e' || *obj_type == 'E') {
     varid = ex_get_dimension(exoid, DIM_NUM_ELE_VAR,  "element variables", &num_var_db, routine);
     tabid = ncvarid (exoid, VAR_ELEM_TAB);
     var_name = "vals_elem_var";
     ent_ndx = ex_id_lkup(exoid, VAR_ID_EL_BLK, entity_id);
     ent_type = "eb";
   }
   else if (*obj_type == 'm' || *obj_type == 'M') {
     varid = ex_get_dimension(exoid, DIM_NUM_NSET_VAR, "nodeset variables", &num_var_db, routine);
     tabid = ncvarid (exoid, VAR_NSET_TAB);
     var_name = "vals_nset_var";
     ent_ndx = ex_id_lkup(exoid, VAR_NS_IDS, entity_id);
     ent_type = "ns";
   }
   else if (*obj_type == 's' || *obj_type == 'S') {
     varid = ex_get_dimension(exoid, DIM_NUM_SSET_VAR, "sideset variables", &num_var_db, routine);
     tabid = ncvarid (exoid, VAR_SSET_TAB);
     var_name = "vals_sset_var";
     ent_ndx = ex_id_lkup(exoid, VAR_SS_IDS, entity_id);
     ent_type = "ss";
   }
   else {       /* invalid variable type */
     exerrval = EX_BADPARAM;
     sprintf(errmsg,
	     "Error: Invalid variable type %c specified in file id %d",
	     *obj_type, exoid);
     ex_err("ex_get_varid",errmsg,exerrval);
     return (EX_WARN);
   }
   
   if (varid == -1) {
     exerrval = ncerr;
     return (EX_WARN);
   }


   if (num_var_db != num_var) {
     exerrval = EX_FATAL;
     sprintf(errmsg,
	     "Error: # of variables doesn't match those defined in file id %d", exoid);
     ex_err("ex_get_object_truth_vector",errmsg,exerrval);
     return (EX_FATAL);
   }

   if (tabid == -1) {
     /* since truth vector isn't stored in the data file, derive it dynamically */
     for (i=0; i<num_var; i++) {
       /* NOTE: names are 1-based */
       if ((tabid = ncvarid (exoid, ex_catstr2(var_name, i+1, ent_type, ent_ndx))) == -1) {
	 
	 /* variable doesn't exist; put a 0 in the truth vector */
	 var_vec[i] = 0;
       } else {
	 /* variable exists; put a 1 in the truth vector */
	 var_vec[i] = 1;
       }
     }
   } else {

     /* read in the truth vector */

     /*
      * application code has allocated an array of ints but netcdf is
      * expecting a pointer to nclongs; if ints are different sizes
      * than nclongs, we must allocate an array of nclongs then
      * convert them to ints with ltoi
      */

     /* If this is a null entity, then 'ent_ndx' will be negative.
      * We don't care in this routine, so make it positive and continue...
      */
     if (ent_ndx < 0) ent_ndx = -ent_ndx;
     
     start[0] = ent_ndx-1;
     start[1] = 0;

     count[0] = 1;
     count[1] = num_var;

     if (sizeof(int) == sizeof(nclong)) {
        iresult = ncvarget (exoid, tabid, start, count, var_vec);
     } else {
       if (!(longs = malloc (num_var * sizeof(nclong)))) {
         exerrval = EX_MEMFAIL;
         sprintf(errmsg,
                 "Error: failed to allocate memory for truth vector for file id %d",
                 exoid);
         ex_err("ex_get_object_truth_vector",errmsg,exerrval);
         return (EX_FATAL);
       }
       iresult = ncvarget (exoid, tabid, start, count, longs);
     }
     
     if (iresult == -1) {
       exerrval = ncerr;
       sprintf(errmsg,
               "Error: failed to get truth vector from file id %d", exoid);
       ex_err("ex_get_object_truth_vector",errmsg,exerrval);
       return (EX_FATAL);
     }

     if (sizeof(int) != sizeof(nclong)) {
       ltoi (longs, var_vec, num_var);
       free (longs);
     }

   } 


   return (EX_NOERR);

}
Пример #17
0
int ex_get_node_num_map (int  exoid,
                int *node_map)
{
   int numnodedim, mapid, i, iresult;
   long num_nodes,  start[1], count[1]; 
   nclong *longs;
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

/* inquire id's of previously defined dimensions and variables  */
   if ((numnodedim = ncdimid (exoid, DIM_NUM_NODES)) == -1)
   {
     return (EX_NOERR);
   }

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


   if ((mapid = ncvarid (exoid, VAR_NODE_NUM_MAP)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
  "Warning: node numbering map not stored in file id %d; returning default map",
             exoid);
     ex_err("ex_get_node_num_map",errmsg,exerrval);

/* generate default map of 1..n, where n is num_nodes */
     for (i=0; i<num_nodes; i++)
        node_map[i] = i+1;

     return (EX_WARN);
   }


/* read in the node numbering map  */

/* application code has allocated an array of ints but netcdf is expecting
   a pointer to nclongs;  if ints are different sizes than nclongs,
   we must allocate an array of nclongs then convert them to ints with ltoi */

   start[0] = 0;
   count[0] = num_nodes;

   if (sizeof(int) == sizeof(nclong)) {
      iresult = ncvarget (exoid, mapid, start, count, node_map);
   } else {
     if (!(longs = malloc(num_nodes * sizeof(nclong)))) {
       exerrval = EX_MEMFAIL;
       sprintf(errmsg,
               "Error: failed to allocate memory for node numbering map for file id %d",
               exoid);
       ex_err("ex_get_node_num_map",errmsg,exerrval);
       return (EX_FATAL);
     }
      iresult = ncvarget (exoid, mapid, start, count, longs);
   }

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

   if (sizeof(int) != sizeof(nclong)) {
      ltoi (longs, node_map, num_nodes);
      free (longs);
   }


   return(EX_NOERR);

}
Пример #18
0
int ex_get_nodal_var (int   exoid,
                      int   time_step,
                      int   nodal_var_index,
                      int   num_nodes, 
                      void *nodal_var_vals)
{
  int varid;
  long start[3], count[3];
  char errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

  /* inquire previously defined variable */

  if (ex_large_model(exoid) == 0) {
    /* read values of the nodal variable */
    if ((varid = ncvarid (exoid, VAR_NOD_VAR)) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
              "Warning: could not find nodal variables in file id %d",
              exoid);
      ex_err("ex_get_nodal_var",errmsg,exerrval);
      return (EX_WARN);
    }

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

    count[0] = 1;
    count[1] = 1;
    count[2] = num_nodes;

  } else {
    /* read values of the nodal variable  -- stored as separate variables... */
    /* Get the varid.... */
    if ((varid = ncvarid (exoid, VAR_NOD_VAR_NEW(nodal_var_index))) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
              "Warning: could not find nodal variable %d in file id %d",
              nodal_var_index, exoid);
      ex_err("ex_get_nodal_var",errmsg,exerrval);
      return (EX_WARN);
    }

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

    count[0] = 1;
    count[1] = num_nodes;

  }
  if (ncvarget (exoid, varid, start, count,
                ex_conv_array(exoid,RTN_ADDRESS,nodal_var_vals,num_nodes)) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to get nodal variables in file id %d",
              exoid);
      ex_err("ex_get_nodal_var",errmsg,exerrval);
      return (EX_FATAL);
    }

  ex_conv_array( exoid, READ_CONVERT,nodal_var_vals, num_nodes );

  return (EX_NOERR);
}
Пример #19
0
int ex_get_node_set (int   exoid,
                     int   node_set_id,
                     int  *node_set_node_list)
{
   int dimid, node_list_id, node_set_id_ndx, iresult;
   long num_nodes_in_set, start[1], count[1]; 
   nclong *longs;
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

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

   if ((dimid = ncdimid (exoid, DIM_NUM_NS))  == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Warning: no node sets defined in file id %d",
             exoid);
     ex_err("ex_get_node_set",errmsg,exerrval);
     return (EX_WARN);
   }

/* Lookup index of node set id in VAR_NS_IDS array */

   node_set_id_ndx = ex_id_lkup(exoid,VAR_NS_IDS,node_set_id);
   if (exerrval != 0) 
   {
     if (exerrval == EX_NULLENTITY)
     {
       sprintf(errmsg,
              "Warning: node set %d is NULL in file id %d",
               node_set_id,exoid);
       ex_err("ex_get_node_set",errmsg,EX_MSG);
       return (EX_WARN);
     }
     else
     {

       sprintf(errmsg,
              "Error: failed to locate node set id %d in %s in file id %d",
               node_set_id,VAR_NS_IDS,exoid);
       ex_err("ex_get_node_set",errmsg,exerrval);
       return (EX_FATAL);
     }
   }

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

   if ((dimid = ncdimid (exoid, DIM_NUM_NOD_NS(node_set_id_ndx))) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
         "Error: failed to locate number of nodes in node set %d in file id %d",
             node_set_id,exoid);
     ex_err("ex_get_node_set",errmsg,exerrval);
     return (EX_FATAL);
   }

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


   if ((node_list_id = ncvarid (exoid, VAR_NODE_NS(node_set_id_ndx))) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to locate node set %d node list in file id %d",
             node_set_id,exoid);
     ex_err("ex_get_node_set",errmsg,exerrval);
     return (EX_FATAL);
   }


/* read in the node list array */

/* application code has allocated an array of ints but netcdf is expecting
   a pointer to nclongs;  if ints are different sizes than nclongs,
   we must allocate an array of nclongs then convert them to ints with ltoi */

   start[0] = 0;
   count[0] = num_nodes_in_set;

   if (sizeof(int) == sizeof(nclong)) {
     iresult = ncvarget (exoid, node_list_id, start, count, node_set_node_list);
   } else {
     if (!(longs = malloc(num_nodes_in_set * sizeof(nclong)))) {
       exerrval = EX_MEMFAIL;
       sprintf(errmsg,
               "Error: failed to allocate memory for node set node list for file id %d",
               exoid);
       ex_err("ex_get_node_set",errmsg,exerrval);
       return (EX_FATAL);
     }
      iresult = ncvarget (exoid, node_list_id, start, count, longs);
   }

   if (iresult == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to get node set node list in file id %d",
             exoid);
     ex_err("ex_get_node_set",errmsg,exerrval);
     return (EX_FATAL);
   }

   if (sizeof(int) != sizeof(nclong)) {
      ltoi (longs, node_set_node_list, num_nodes_in_set);
      free (longs);
   }

   return (EX_NOERR);

}
Пример #20
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);
}
Пример #21
0
int ex_get_elem_var (int   exoid,
                     int   time_step,
                     int   elem_var_index,
                     int   elem_blk_id, 
                     int   num_elem_this_blk,
                     void *elem_var_vals)
{
   int varid, elem_blk_id_ndx;
   long start[2], count[2];
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

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


/* inquire previously defined variable */

   if((varid=ncvarid(exoid,VAR_ELEM_VAR(elem_var_index,elem_blk_id_ndx))) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
          "Error: failed to locate elem var %d for elem block %d in file id %d",
          elem_var_index,elem_blk_id,exoid); /* this msg needs to be improved */
     ex_err("ex_get_elem_var",errmsg,exerrval);
     return (EX_FATAL);
   }

/* read values of element variable */

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

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

   if (ncvarget (exoid, varid, start, count,
        ex_conv_array(exoid,RTN_ADDRESS,elem_var_vals,num_elem_this_blk)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
        "Error: failed to get elem var %d for block %d in file id %d",
             elem_var_index,elem_blk_id,exoid);/*this msg needs to be improved*/
     ex_err("ex_get_elem_var",errmsg,exerrval);
     return (EX_FATAL);
   }


   ex_conv_array( exoid, READ_CONVERT, elem_var_vals, num_elem_this_blk );

   return (EX_NOERR);
}
Пример #22
0
int ex_get_conn( int   exoid,
                 int   blk_type,
                 int   blk_id,
                 int*  nodeconn,
                 int*  edgeconn,
                 int*  faceconn )
{
   int numblkentriesdim, connid, econnid, fconnid, blk_id_ndx, iresult;
   int numnodperentdim, numedgperentdim, numfacperentdim;
   int iexit = (EX_NOERR); /* exit status */
   long num_entries_this_blk, num_nodes_per_entry, num_edges_per_entry, num_faces_per_entry;
   long start[2], count[2]; 
   nclong *longs;
   char errmsg[MAX_ERR_LENGTH];

   const char* tname;
   const char* vblkids;
   const char* dnumblkent;
   const char* dnumnodent;
   const char* dnumedgent;
   const char* dnumfacent;
   const char* vnodeconn;
   const char* vedgeconn;
   const char* vfaceconn;

   /* Should we warn if edgeconn or faceconn are non-NULL?
    * No, fail silently so the same code can be used to read any type of block info.
    * However, we will warn if edgeconn or faceconn are NULL but num_edges_per_entry
    * or num_faces_per_entry (respectively) are positive.
    */
   switch (blk_type) {
   case EX_EDGE_BLOCK:
     tname = "edge";
     vblkids = VAR_ID_ED_BLK;
     break;
   case EX_FACE_BLOCK:
     tname = "face";
     vblkids = VAR_ID_FA_BLK;
     break;
   case EX_ELEM_BLOCK:
     tname = "element";
     vblkids = VAR_ID_EL_BLK;
     break;
   default:
     exerrval = EX_BADPARAM;
     sprintf( errmsg, "Error: Invalid block type (%d) specified in file id %d", blk_type, exoid );
     ex_err( "ex_get_conn", errmsg, exerrval );
     return (EX_FATAL);
   }

   exerrval = 0; /* clear error code */

   /* Locate index of element block id in VAR_ID_EL_BLK array */

   blk_id_ndx = ex_id_lkup(exoid,vblkids,blk_id);
   if (exerrval != 0) 
   {
     if (exerrval == EX_NULLENTITY)
     {
       sprintf(errmsg,
              "Warning: no connectivity array for NULL %s block %d in file id %d",
               tname,blk_id,exoid);
       ex_err("ex_get_conn",errmsg,EX_MSG);
       return (EX_WARN); /* no connectivity array for this element block */
     }
     else
     {
       sprintf(errmsg,
        "Error: failed to locate %s block id %d in %s array in file id %d",
               tname,blk_id,vblkids,exoid);
       ex_err("ex_get_conn",errmsg,exerrval);
       return (EX_FATAL);
     }
   }

   switch (blk_type) {
   case EX_EDGE_BLOCK:
     dnumblkent = DIM_NUM_ED_IN_EBLK(blk_id_ndx);
     dnumnodent = DIM_NUM_NOD_PER_ED(blk_id_ndx);
     dnumedgent = 0;
     dnumfacent = 0;
     vnodeconn = VAR_EBCONN(blk_id_ndx);
     vedgeconn = 0;
     vfaceconn = 0;
     break;
   case EX_FACE_BLOCK:
     dnumblkent = DIM_NUM_FA_IN_FBLK(blk_id_ndx);
     dnumnodent = DIM_NUM_NOD_PER_FA(blk_id_ndx);
     dnumedgent = 0;
     dnumfacent = 0;
     vnodeconn = VAR_FBCONN(blk_id_ndx);
     vedgeconn = 0;
     vfaceconn = 0;
     break;
   case EX_ELEM_BLOCK:
     dnumblkent = DIM_NUM_EL_IN_BLK(blk_id_ndx);
     dnumnodent = DIM_NUM_NOD_PER_EL(blk_id_ndx);
     dnumedgent = DIM_NUM_EDG_PER_EL(blk_id_ndx);
     dnumfacent = DIM_NUM_FAC_PER_EL(blk_id_ndx);
     vnodeconn = VAR_CONN(blk_id_ndx);
     vedgeconn = VAR_ECONN(blk_id_ndx);
     vfaceconn = VAR_FCONN(blk_id_ndx);
     break;
   }
/* inquire id's of previously defined dimensions  */

   if ((numblkentriesdim = ncdimid (exoid, dnumblkent)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
     "Error: failed to locate number of elements in %s block %d in file id %d",
              tname,blk_id,exoid);
     ex_err("ex_get_conn",errmsg, exerrval);
     return(EX_FATAL);
   }

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


   if ((numnodperentdim = ncdimid (exoid, dnumnodent)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
      "Error: failed to locate number of nodes/elem for block %d in file id %d",
             blk_id,exoid);
     ex_err("ex_get_conn",errmsg,exerrval);
     return(EX_FATAL);
   }

   if (ncdiminq (exoid, numnodperentdim, (char *) 0, &num_nodes_per_entry) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
       "Error: failed to get number of nodes/elem for block %d in file id %d",
             blk_id,exoid);
     ex_err("ex_get_conn",errmsg, exerrval);
     return(EX_FATAL);
   }

   if ( dnumedgent ) {
     num_edges_per_entry = 0;
     if ((numedgperentdim = ncdimid (exoid, dnumedgent)) == -1) {
       numedgperentdim = -1;
     } else {
       if (ncdiminq (exoid, numedgperentdim, (char *) 0, &num_edges_per_entry) == -1) {
         exerrval = ncerr;
         sprintf(errmsg,
           "Error: failed to get number of edges/entry for %s block %d in file id %d",
           tname,blk_id,exoid);
         ex_err("ex_get_conn",errmsg, exerrval);
         return(EX_FATAL);
       }
       if ( num_edges_per_entry < 0 )
         num_edges_per_entry = 0;
     }
     if ( num_edges_per_entry > 0 && (!edgeconn) ) {
       exerrval = EX_BADPARAM;
       sprintf( errmsg, "Edge connectivity present but NULL pointer passed for file id %d", exoid );
       ex_err( "ex_get_conn", errmsg, exerrval );
       iexit = exerrval;
     }
   }

   if ( dnumfacent ) {
     num_faces_per_entry = 0;
     if ((numfacperentdim = ncdimid (exoid, dnumfacent)) == -1) {
       numfacperentdim = -1;
     } else {
       if (ncdiminq (exoid, numfacperentdim, (char *) 0, &num_faces_per_entry) == -1) {
         exerrval = ncerr;
         sprintf(errmsg,
           "Error: failed to get number of faces/entry for %s block %d in file id %d",
           tname,blk_id,exoid);
         ex_err("ex_get_conn",errmsg, exerrval);
         return(EX_FATAL);
       }
       if ( num_faces_per_entry < 0 )
         num_faces_per_entry = 0;
     }
     if ( num_faces_per_entry > 0 && (!faceconn) ) {
       exerrval = EX_BADPARAM;
       sprintf( errmsg, "Face connectivity present but NULL pointer passed for file id %d", exoid );
       ex_err( "ex_get_conn", errmsg, exerrval );
       iexit = exerrval;
     }
   }


   if ((connid = ncvarid (exoid, vnodeconn)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
        "Error: failed to locate connectivity array for block %d in file id %d",
             blk_id,exoid);
     ex_err("ex_get_conn",errmsg, exerrval);
     return(EX_FATAL);
   }

   if ( edgeconn && (numedgperentdim > 0) && ((econnid = ncvarid (exoid, vedgeconn)) == -1) )
   {
     exerrval = ncerr;
     sprintf(errmsg,
        "Error: failed to locate edge connectivity array for %s block %d in file id %d",
             tname,blk_id,exoid);
     ex_err("ex_get_conn",errmsg, exerrval);
     return(EX_FATAL);
   }

   if ( faceconn && (numfacperentdim > 0) && ((fconnid = ncvarid (exoid, vfaceconn)) == -1) )
   {
     exerrval = ncerr;
     sprintf(errmsg,
        "Error: failed to locate face connectivity array for %s block %d in file id %d",
             tname,blk_id,exoid);
     ex_err("ex_get_conn",errmsg, exerrval);
     return(EX_FATAL);
   }


/* read in the connectivity array */

/* application code has allocated an array of ints but netcdf is expecting
   a pointer to nclongs;  if ints are different sizes than nclongs,
   we must allocate an array of nclongs then convert them to ints with ltoi */

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

   if ( edgeconn && num_edges_per_entry ) {
     count[0] = num_entries_this_blk;
     count[1] = num_edges_per_entry;

     if (sizeof(int) == sizeof(nclong)) {
       iresult = ncvarget (exoid, econnid, start, count, edgeconn);
     } else {
       if (!(longs = malloc (num_entries_this_blk*num_edges_per_entry * sizeof(nclong)))) {
         exerrval = EX_MEMFAIL;
         sprintf(errmsg,
           "Error: failed to allocate memory for edge connectivity array for file id %d",
           exoid);
         ex_err("ex_get_conn",errmsg,exerrval);
         return (EX_FATAL);
       }
       iresult = ncvarget (exoid, econnid, start, count, longs);
     }

     if (iresult == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to get edge connectivity array for block %d in file id %d",
         blk_id,exoid);
       ex_err("ex_get_conn",errmsg, exerrval);
       return(EX_FATAL);
       }

     if (sizeof(int) != sizeof(nclong)) {
       ltoi (longs, edgeconn, num_entries_this_blk*num_edges_per_entry);
       free (longs);
     }
   }

   if ( faceconn && num_faces_per_entry ) {
     count[0] = num_entries_this_blk;
     count[1] = num_faces_per_entry;

     if (sizeof(int) == sizeof(nclong)) {
       iresult = ncvarget (exoid, fconnid, start, count, faceconn);
     } else {
       if (!(longs = malloc (num_entries_this_blk*num_faces_per_entry * sizeof(nclong)))) {
         exerrval = EX_MEMFAIL;
         sprintf(errmsg,
           "Error: failed to allocate memory for face connectivity array of %s blockfor file id %d",
           tname,exoid);
         ex_err("ex_get_conn",errmsg,exerrval);
         return (EX_FATAL);
       }
       iresult = ncvarget (exoid, fconnid, start, count, longs);
     }

     if (iresult == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to get face connectivity array for %s block %d in file id %d",
         tname,blk_id,exoid);
       ex_err("ex_get_conn",errmsg, exerrval);
       return(EX_FATAL);
       }

     if (sizeof(int) != sizeof(nclong)) {
       ltoi (longs, faceconn, num_entries_this_blk*num_faces_per_entry);
       free (longs);
     }
   }

   if ( nodeconn && num_nodes_per_entry ) {
     count[0] = num_entries_this_blk;
     count[1] = num_nodes_per_entry;

     if (sizeof(int) == sizeof(nclong)) {
       iresult = ncvarget (exoid, connid, start, count, nodeconn);
     } else {
       if (!(longs = malloc (num_entries_this_blk*num_nodes_per_entry * sizeof(nclong)))) {
         exerrval = EX_MEMFAIL;
         sprintf(errmsg,
           "Error: failed to allocate memory for element connectivity array for file id %d",
           exoid);
         ex_err("ex_get_conn",errmsg,exerrval);
         return (EX_FATAL);
       }
       iresult = ncvarget (exoid, connid, start, count, longs);
     }

     if (iresult == -1)
       {
       exerrval = ncerr;
       sprintf(errmsg,
         "Error: failed to get connectivity array for block %d in file id %d",
         blk_id,exoid);
       ex_err("ex_get_conn",errmsg, exerrval);
       return(EX_FATAL);
       }

     if (sizeof(int) != sizeof(nclong)) {
       ltoi (longs, nodeconn, num_entries_this_blk*num_nodes_per_entry);
       free (longs);
     }
   }

   return iexit;
}
Пример #23
0
int ex_get_partial_elem_map (int   exoid,
           int   map_id,
           int ent_start,
           int ent_count, 
           int  *elem_map)
{
   int dimid, var_id, id_ndx, iresult;
   long num_elem, start[1], count[1]; 
   nclong *longs;
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

   /* See if file contains any elements...*/
   if ((dimid = ncdimid (exoid, DIM_NUM_ELEM)) == -1)
   {
     return (EX_NOERR);
   }

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

  /* Check input parameters for a valid range of numbers */
  if (ent_start <= 0 || ent_start > num_elem) {
    exerrval = EX_FATAL;
    sprintf(errmsg,
      "Error: start count is invalid in file id %d",
      exoid);
    ex_err("ex_get_partial_elem_map",errmsg,exerrval);
    return (EX_FATAL);
  }
  if (ent_count < 0) {
    exerrval = EX_FATAL;
    sprintf(errmsg,
      "Error: Invalid count value in file id %d",
      exoid);
    ex_err("ex_get_partial_elem_map",errmsg,exerrval);
    return (EX_FATAL);
  }
  if (ent_start+ent_count-1 > num_elem) {
    exerrval = EX_FATAL;
    sprintf(errmsg,
      "Error: start+count-1 is larger than element count in file id %d",
      exoid);
    ex_err("ex_get_partial_elem_map",errmsg,exerrval);
    return (EX_FATAL);
  }

/* first check if any element maps have been defined */

   if ((dimid = ncdimid (exoid, DIM_NUM_EM))  == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Warning: no element maps defined in file id %d",
             exoid);
     ex_err("ex_get_partial_elem_map",errmsg,exerrval);
     return (EX_WARN);
   }

/* Lookup index of element map id property array */

   id_ndx = ex_id_lkup(exoid,VAR_EM_PROP(1),map_id);
   if (exerrval != 0) 
   {

      sprintf(errmsg,
              "Error: failed to locate element map id %d in %s in file id %d",
               map_id,VAR_EM_PROP(1),exoid);
      ex_err("ex_get_partial_elem_map",errmsg,exerrval);
      return (EX_FATAL);
   }

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

   if ((var_id = ncvarid (exoid, VAR_ELEM_MAP(id_ndx))) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to locate element map %d in file id %d",
             map_id,exoid);
     ex_err("ex_get_partial_elem_map",errmsg,exerrval);
     return (EX_FATAL);
   }


/* read in the element map */

/* application code has allocated an array of ints but netcdf is expecting
   a pointer to nclongs;  if ints are different sizes than nclongs,
   we must allocate an array of nclongs then convert them to ints with ltoi */

   start[0] = ent_start-1;
   count[0] = ent_count;

   if (sizeof(int) == sizeof(nclong)) {
     iresult = ncvarget (exoid, var_id, start, count, elem_map);
   } else {
     if (!(longs = malloc(ent_count * sizeof(nclong)))) {
       exerrval = EX_MEMFAIL;
       sprintf(errmsg,
               "Error: failed to allocate memory for element map for file id %d",
               exoid);
       ex_err("ex_get_partial_elem_map",errmsg,exerrval);
       return (EX_FATAL);
     }
      iresult = ncvarget (exoid, var_id, start, count, longs);
   }

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

   if (sizeof(int) != sizeof(nclong)) {
      ltoi (longs, elem_map, ent_count);
      free (longs);
   }

   return (EX_NOERR);

}
Пример #24
0
int
cpy_var_val(int in_id,int out_id,char *var_nm)
/*
   int in_id: input netCDF input-file ID
   int out_id: input netCDF output-file ID
   char *var_nm: input variable name
 */
{
  /* Routine to copy the variable data from an input netCDF file
   * to an output netCDF file. 
   */

  int *dim_id;
  int idx;
  int nbr_dim;
  int var_in_id;
  int var_out_id;
  long *dim_cnt;
  long *dim_sz;
  long *dim_srt;
  long var_sz=1L;

  nc_type var_type_in, var_type_out;

  void *void_ptr;

  /* Get the var_id for the requested variable from both files. */
  var_in_id=ncvarid(in_id,var_nm);

  var_out_id=ncvarid(out_id,var_nm);
 
  /* Get the number of dimensions for the variable. */

  ncvarinq(out_id,var_out_id,(char *)NULL,&var_type_out,&nbr_dim,
                (int *)NULL,(int *)NULL);

  ncvarinq(in_id,var_in_id,(char *)NULL,&var_type_in,&nbr_dim,
                (int *)NULL,(int *)NULL);
 
  /* Allocate space to hold the dimension IDs */
  dim_cnt = malloc(nbr_dim*sizeof(long));

  dim_id=malloc(nbr_dim*sizeof(int));

  dim_sz=malloc(nbr_dim*sizeof(long));

  dim_srt=malloc(nbr_dim*sizeof(long));
 
  /* Get the dimension IDs from the input file */
  ncvarinq(in_id,var_in_id,(char *)NULL,(nc_type *)NULL,
                (int *)NULL,dim_id,(int *)NULL);
 
  /* Get the dimension sizes and names from the input file */
  for(idx=0;idx<nbr_dim;idx++){
  /* NB: For the unlimited dimension, ncdiminq() returns the maximum
     value used so far in writing data for that dimension.
     Thus if you read the dimension sizes from the output file, then
     the ncdiminq() returns dim_sz=0 for the unlimited dimension
     until a variable has been written with that dimension. This is
     the reason for always reading the input file for the dimension
     sizes. */

    ncdiminq(in_id,dim_id[idx],(char *)NULL,dim_cnt+idx);

    /* Initialize the indicial offset and stride arrays */
    dim_srt[idx]=0L;
    var_sz*=dim_cnt[idx];
  } /* end loop over dim */

  /* Allocate enough space to hold the variable */
  void_ptr=malloc(var_sz*nctypelen(var_type_in));

  /* Get the variable */

  /* if variable is float or double, convert if necessary */

  if(nbr_dim==0){  /* variable is a scalar */

    ncvarget1(in_id,var_in_id,0L,void_ptr);

    if ( ( (var_type_in == NC_FLOAT) && (var_type_out == NC_FLOAT) ) ||
         ( (var_type_in == NC_DOUBLE) && (var_type_out == NC_DOUBLE) ) ) {
      /* no conversion necessary */

      ncvarput1(out_id,var_out_id,0L,void_ptr);

    } else if ( (var_type_in == NC_FLOAT) && (var_type_out == NC_DOUBLE) ) {
      /* convert up */

      ncvarput1(out_id,var_out_id,0L,
                ex_conv_array (out_id, WRITE_CONVERT_UP, void_ptr, 1));

    } else if ( (var_type_in == NC_DOUBLE) && (var_type_out == NC_FLOAT) ) {
      /* convert down */

      ncvarput1(out_id,var_out_id,0L,
                ex_conv_array (out_id, WRITE_CONVERT_DOWN, void_ptr, 1));

    } else {  /* variable isn't float or double */

      /* no conversion necessary */

      ncvarput1(out_id,var_out_id,0L,void_ptr);

    }

  } else { /* variable is a vector */

    ncvarget(in_id,var_in_id,dim_srt,dim_cnt,void_ptr);

    if ( ( (var_type_in == NC_FLOAT) && (var_type_out == NC_FLOAT) ) ||
         ( (var_type_in == NC_DOUBLE) && (var_type_out == NC_DOUBLE) ) ) {
      /* no conversion necessary */

      ncvarput(out_id,var_out_id,dim_srt,dim_cnt,void_ptr);

    } else if ( (var_type_in == NC_FLOAT) && (var_type_out == NC_DOUBLE) ) {
      /* convert up */

      ncvarput(out_id,var_out_id,dim_srt,dim_cnt,
               ex_conv_array (out_id,WRITE_CONVERT_UP,void_ptr,var_sz));

    } else if ( (var_type_in == NC_DOUBLE) && (var_type_out == NC_FLOAT) ) {
      /* convert down */

      ncvarput(out_id,var_out_id,dim_srt,dim_cnt,
               ex_conv_array (out_id,WRITE_CONVERT_DOWN,void_ptr,var_sz));

    } else {  /* variable isn't float or double */

      /* no conversion necessary */

      ncvarput(out_id,var_out_id,dim_srt,dim_cnt,void_ptr);

    }

  } /* end if variable is an array */

  /* Free the space that held the dimension IDs */
  (void)free(dim_cnt);
  (void)free(dim_id);
  (void)free(dim_sz);
  (void)free(dim_srt);

  /* Free the space that held the variable */
  (void)free(void_ptr);

  return(EX_NOERR);

} /* end cpy_var_val() */
Пример #25
0
int ex_get_nodal_var_time (int   exoid,
                           int   nodal_var_index,
                           int   node_number,
                           int   beg_time_step, 
                           int   end_time_step,
                           void *nodal_var_vals)
{
  int varid;
  long start[3], count[3];
  float fdum;
  char *cdum; 
  char errmsg[MAX_ERR_LENGTH];

  /* inquire previously defined variable */

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

  if (end_time_step < 0)
    {

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

  end_time_step--;

  if (ex_large_model(exoid) == 0) {
    /* read values of the nodal variable;
     * assume node number is 1-based (first node is numbered 1);  adjust
     * so it is 0-based
     */
    if ((varid = ncvarid (exoid, VAR_NOD_VAR)) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
              "Warning: could not find nodal variable %d in file id %d",
              nodal_var_index, exoid);
      ex_err("ex_get_nodal_var",errmsg,exerrval);
      return (EX_WARN);
    }
    start[0] = --beg_time_step;
    start[1] = --nodal_var_index;
    start[2] = --node_number;

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

  } else {
    if ((varid = ncvarid (exoid, VAR_NOD_VAR_NEW(nodal_var_index))) == -1) {
      exerrval = ncerr;
      sprintf(errmsg,
              "Warning: could not find nodal variable %d in file id %d",
              nodal_var_index, exoid);
      ex_err("ex_get_nodal_var",errmsg,exerrval);
      return (EX_WARN);
    }

    /* read values of the nodal variable;
     * assume node number is 1-based (first node is numbered 1);  adjust
     * so it is 0-based
     */

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

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

  }
  if (ncvarget (exoid, varid, start, count,
                ex_conv_array(exoid,RTN_ADDRESS,nodal_var_vals,count[0])) == -1)
    {
      exerrval = ncerr;
      sprintf(errmsg,
              "Error: failed to get nodal variables in file id %d",
              exoid);
      ex_err("ex_get_nodal_var_time",errmsg,exerrval);
      return (EX_FATAL);
    }

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

  return (EX_NOERR);
}
Пример #26
0
int ex_get_side_set_ids (int  exoid,
                         int *ids)
{
   int dimid, varid, iresult;
   long num_side_sets, start[1], count[1]; 
   nclong *longs;
   char errmsg[MAX_ERR_LENGTH];

   exerrval = 0;        /* clear error code */

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

   if ((dimid = ncdimid (exoid, DIM_NUM_SS)) < 0)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Warning: no side sets stored in file id %d", exoid);
     ex_err("ex_get_side_set_ids",errmsg,exerrval);
     return (EX_WARN);
   }

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


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


/* read in the side set ids  */

/* application code has allocated an array of ints but netcdf is expecting
   a pointer to nclongs;  if ints are different sizes than nclongs,
   we must allocate an array of nclongs then convert them to ints with ltoi */

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

   if (sizeof(int) == sizeof(nclong)) {
      iresult = ncvarget (exoid, varid, start, count, ids);
   } else {
     if (!(longs = malloc(num_side_sets * sizeof(nclong)))) {
       exerrval = EX_MEMFAIL;
       sprintf(errmsg,
               "Error: failed to allocate memory for side set ids for file id %d",
               exoid);
       ex_err("ex_get_side_set_ids",errmsg,exerrval);
       return (EX_FATAL);
     }
     iresult = ncvarget (exoid, varid, start, count, longs);
   }

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

   if (sizeof(int) != sizeof(nclong)) {
      ltoi (longs, ids, num_side_sets);
      free (longs);
   }

   return(EX_NOERR);
}
Пример #27
0
/* Copy all the data values in an input netCDF file */
int copy_nc_data(struct fileinfo *ncinfile, int outncfid,
                 unsigned char appendnc, unsigned char verbose)
{
    int v, d, r;  /* Loop variables */
    int dimid;  /* ID of a dimension */
    void *values;  /* Data values to copy */
    long instart[MAX_NC_DIMS], outstart[MAX_NC_DIMS];  /* Copy array sizes */
    long count[MAX_NC_DIMS];                           /*        "         */
    long nrecs;  /* Number of records */
    long recsize;  /* Number of values in a record */
    int varrecdim;  /* Position of a variable's record dimension */

    /* Loop over all the records */
    nrecs=ncinfile->dimsize[ncinfile->recdim];
    for (r=0; r < nrecs; r++)
    {
#if DEBUG==0
        if (verbose) printf("\n  record=%d",r+1);
#endif

        /* Loop over all the variables */
        for (v=0; v < ncinfile->nvars; v++)
        {
#if DEBUG==0
            if (verbose) printf("\n    variable=%s",ncinfile->varname[v]);
#endif

            /* Avoid multiple reads/writes of non-decomposed dimensions */
            if ((dimid=ncdimid(ncinfile->ncfid,ncinfile->varname[v]))!=(-1))
                if (appendnc && ncinfile->dimend[dimid]==(-1)) continue;

            /* Get read/write dimension sizes for the variable */
            recsize=1;
            varrecdim=(-1);
            for (d=0; d < ncinfile->varndims[v]; d++)
            {
                if (ncinfile->vardim[v][d]==ncinfile->recdim)
                {
                    count[d]=1;
                    varrecdim=d;
                }
                else
                {
                    count[d]=ncinfile->dimsize[ncinfile->vardim[v][d]];
                    recsize*=count[d];
                    instart[d]=0;
                    outstart[d]=ncinfile->dimstart[ncinfile->vardim[v][d]]-1;
                }
#if DEBUG==1
                printf("%d:  instart=%ld  ouststart=%ld  count=%ld\n",d,
                       instart[d],outstart[d],count[d]);
#endif
            }

            /* Avoid multiple reads/writes of non-record variables */
            if (varrecdim==(-1) && r > 0) continue;

#if DEBUG==0
            if (verbose) printf("  (read/write)");
#endif

            /* Allocate a buffer for the variable's record */
            if ((values=malloc(nctypelen(ncinfile->datatype[v])*recsize))==NULL)
            {
                fprintf(stderr,"Error: cannot allocate memory for variable \"%s\"'s values!\n",
                        ncinfile->varname[v]);
                return(1);
            }

            /* Copy the record */
            if (varrecdim!=(-1)) instart[varrecdim]=outstart[varrecdim]=r;
            if (ncvarget(ncinfile->ncfid,v,instart,count,values)==(-1))
            {
                fprintf(stderr,"Error: cannot read variable \"%s\"'s values!\n",
                        ncinfile->varname[v]);
                return(1);
            }
            if (ncvarput(outncfid,v,outstart,count,values)==(-1))
            {
                fprintf(stderr,"Error: cannot write variable \"%s\"'s values!\n",
                        ncinfile->varname[v]);
                return(1);
            }

            /* Deallocate the record buffer */
            free(values);
        }
    }
    return(0);
}
Пример #28
0
/* Output the data for a single variable, in CDL syntax. */
int
vardata(
     const struct ncvar *vp,	/* variable */
     long vdims[],		/* variable dimension sizes */
     int ncid,			/* netcdf id */
     int varid,			/* variable id */
     const struct fspec* fsp	/* formatting specs */
     )
{
    long cor[NC_MAX_DIMS];	/* corner coordinates */
    long edg[NC_MAX_DIMS];	/* edges of hypercube */
    long add[NC_MAX_DIMS];      /* "odometer" increment to next "row"  */
#define VALBUFSIZ 1000
    double vals[VALBUFSIZ] ; /* aligned buffer */

    int gulp = VALBUFSIZ;

    int id;
    int ir;
    long nels;
    long ncols;
    long nrows;
    int vrank = vp->ndims;
    static int initeps = 0;

    /* printf format used to print each value */
    char *fmt = get_fmt(ncid, varid, vp->type);

    if (!initeps) {		/* make sure epsilons get initialized */
	init_epsilons();
	initeps = 1;
    }

    nels = 1;
    for (id = 0; id < vrank; id++) {
	cor[id] = 0;
	edg[id] = 1;
	nels *= vdims[id];	/* total number of values for variable */
    }

    if (vrank <= 1) {
	Printf("\n %s = ", vp->name);
	set_indent ((int)strlen(vp->name) + 4);
    } else {
	Printf("\n %s =\n  ", vp->name);
	set_indent (2);
    }

    if (vrank < 1) {
	ncols = 1;
    } else {
	ncols = vdims[vrank-1];	/* size of "row" along last dimension */
	edg[vrank-1] = vdims[vrank-1];
	for (id = 0; id < vrank; id++)
	  add[id] = 0;
	if (vrank > 1)
	  add[vrank-2] = 1;
    }
    nrows = nels/ncols;		/* number of "rows" */
    
    for (ir = 0; ir < nrows; ir++) {
	/*
	 * rather than just printing a whole row at once (which might exceed
	 * the capacity of MSDOS platforms, for example), we break each row
	 * into smaller chunks, if necessary.
	 */
	long corsav;
	int left = (int)ncols;
	boolean lastrow;

	if (vrank > 0) {
	    corsav = cor[vrank-1];
	    if (fsp->brief_data_cmnts != false
		&& vrank > 1
		&& left > 0) {	/* print brief comment with indices range */
		Printf("// %s(",vp->name);
		switch (fsp->data_lang) {
		  case LANG_C:
		    /* print brief comment with C variable indices */
		    for (id = 0; id < vrank-1; id++)
		      Printf("%lu,", (unsigned long)cor[id]);
		    if (vdims[vrank-1] == 1)
		      Printf("0");
		    else
		      Printf(" 0-%lu", (unsigned long)vdims[vrank-1]-1);
		    break;
		  case LANG_F:
		    /* print brief comment with Fortran variable indices */
		    if (vdims[vrank-1] == 1)
		      Printf("1");
		    else
		      Printf("1-%lu ", (unsigned long)vdims[vrank-1]);
		    for (id = vrank-2; id >=0 ; id--) {
			Printf(",%lu", (unsigned long)(1 + cor[id]));
		    }
		    break;
		}
		Printf(")\n    ");
		set_indent(4);
	    }
	}
	lastrow = (boolean)(ir == nrows-1);
	while (left > 0) {
	    long toget = left < gulp ? left : gulp;
	    if (vrank > 0)
	      edg[vrank-1] = toget;
	    switch(vp->type) {
	    case NC_CHAR:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (char *)vals) );
	        pr_tvals(vp, toget, fmt, left > toget, lastrow,
			 (char *) vals, fsp, cor);
		break;
	    case NC_BYTE:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (signed char *)vals) );
	        pr_bvals(vp, toget, fmt, left > toget, lastrow,
			 (signed char *) vals, fsp, cor);
		break;
	    case NC_SHORT:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (short *)vals) );
	        pr_svals(vp, toget, fmt, left > toget, lastrow,
			 (short *) vals, fsp, cor);
		break;
	    case NC_INT:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (int *)vals) );
	        pr_ivals(vp, toget, fmt, left > toget, lastrow,
			 (int *) vals, fsp, cor);
		break;
	    case NC_FLOAT:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (float *)vals) );
	        pr_fvals(vp, toget, fmt, left > toget, lastrow,
			 (float *) vals, fsp, cor);
		break;
	    case NC_DOUBLE:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (double *)vals) );
	        pr_dvals(vp, toget, fmt, left > toget, lastrow,
			 (double *) vals, fsp, cor);
		break;
	    default:
		error("vardata: bad type");
	    }
	    left -= toget;
	    if (vrank > 0)
	      cor[vrank-1] += toget;
	}
	if (vrank > 0)
	  cor[vrank-1] = corsav;
	if (ir < nrows-1)
	  if (!upcorner(vdims,vp->ndims,cor,add))
	    error("vardata: odometer overflowed!");
	set_indent(2);
    }

    return 0;
}
Пример #29
0
int
cpy_coord_val(int in_id,int out_id,char *var_nm,
              int in_large, int out_large)
/*
   int in_id: input netCDF input-file ID
   int out_id: input netCDF output-file ID
   char *var_nm: input variable name
 */
{
  /* Routine to copy the coordinate data from an input netCDF file
   * to an output netCDF file. 
   */

  const char *routine = NULL;
  int i;
  long spatial_dim, num_nodes;
  long start[2], count[2];
  nc_type var_type_in, var_type_out;

  void *void_ptr;

  /* Handle easiest situation first: in_large matches out_large */
  if (in_large == out_large)
    return cpy_var_val(in_id, out_id, var_nm);
  
  /* At this point, know that in_large != out_large, so will need to
     either copy a vector to multiple scalars or vice-versa.  Also
     will a couple dimensions, so get them now.*/
  ex_get_dimension(in_id, DIM_NUM_DIM, "dimension", &spatial_dim, routine);
  ex_get_dimension(in_id, DIM_NUM_NODES, "nodes",   &num_nodes, routine);

  if (in_large == 0 && out_large == 1) {
    /* output file will have coordx, coordy, coordz (if 3d). */
    /* Get the var_id for the requested variable from both files. */
    int var_in_id, var_out_id[3];
    var_in_id = ncvarid(in_id, VAR_COORD);
    var_out_id[0] = ncvarid(out_id,VAR_COORD_X);
    var_out_id[1] = ncvarid(out_id,VAR_COORD_Y);
    var_out_id[2] = ncvarid(out_id,VAR_COORD_Z);

    ncvarinq(in_id,var_in_id,(char *)NULL,&var_type_in,(int*)NULL,
                (int *)NULL,(int *)NULL);
    ncvarinq(out_id,var_out_id[0],(char *)NULL,&var_type_out,(int *)NULL,
                (int *)NULL,(int *)NULL);

    void_ptr=malloc(num_nodes * nctypelen(var_type_in));

    /* Copy each component of the variable... */
    for (i=0; i < spatial_dim; i++) {
      start[0] = i; start[1] = 0;
      count[0] = 1; count[1] = num_nodes;
      ncvarget(in_id, var_in_id, start, count, void_ptr);
      
      if (var_type_in == var_type_out) {
        if (var_type_out == NC_FLOAT) {
          nc_put_var_float(out_id, var_out_id[i], void_ptr);
        } else {
          nc_put_var_double(out_id, var_out_id[i], void_ptr);
        }
      } else if (var_type_in == NC_FLOAT && var_type_out == NC_DOUBLE) {
        nc_put_var_double(out_id, var_out_id[i],
                          ex_conv_array(out_id, WRITE_CONVERT_UP, void_ptr, num_nodes));
      } else if (var_type_in == NC_DOUBLE && var_type_out == NC_FLOAT) {
        nc_put_var_float(out_id, var_out_id[i],
                          ex_conv_array(out_id, WRITE_CONVERT_DOWN, void_ptr, num_nodes));
      }
    }
  }

  if (in_large == 1 && out_large == 0) {
    /* input file will have coordx, coordy, coordz (if 3d); output has
       only "coord" */
    int var_in_id[3], var_out_id;
    var_in_id[0] = ncvarid(in_id,  VAR_COORD_X);
    var_in_id[1] = ncvarid(in_id,  VAR_COORD_Y);
    var_in_id[2] = ncvarid(in_id,  VAR_COORD_Z);
    var_out_id   = ncvarid(out_id, VAR_COORD);
    
    ncvarinq(in_id,var_in_id[0],(char *)NULL,&var_type_in,(int *)NULL,
                (int *)NULL,(int *)NULL);

    ncvarinq(out_id,var_out_id,(char *)NULL,&var_type_out,(int*)NULL,
                (int *)NULL,(int *)NULL);

    void_ptr=malloc(num_nodes * nctypelen(var_type_in));

    /* Copy each component of the variable... */
    for (i=0; i < spatial_dim; i++) {
      if (var_type_in == NC_FLOAT) {
        nc_get_var_float(in_id, var_in_id[i], void_ptr);
      } else {
        nc_get_var_double(in_id, var_in_id[i], void_ptr);
      }

      start[0] = i; start[1] = 0;
      count[0] = 1; count[1] = num_nodes;
      if (var_type_in == var_type_out) {
        ncvarput(out_id, var_out_id, start, count, void_ptr);
      } else if (var_type_in == NC_FLOAT && var_type_out == NC_DOUBLE) {
        ncvarput(out_id, var_out_id, start, count,
                 ex_conv_array(out_id, WRITE_CONVERT_UP, void_ptr, num_nodes));
      } else if (var_type_in == NC_DOUBLE && var_type_out == NC_FLOAT) {
        ncvarput(out_id, var_out_id, start, count,
                 ex_conv_array(out_id, WRITE_CONVERT_DOWN, void_ptr, num_nodes));
      }
    }
  }

  /* Free the space that held the variable */
  (void)free(void_ptr);

  return(EX_NOERR);

} /* end cpy_coord_val() */
Пример #30
0
int ex_get_glob_var_time (int   exoid,
                          int   glob_var_index,
                          int   beg_time_step,
                          int   end_time_step,
                          void *glob_var_vals)
{
   int varid;
   long start[2], count[2];
   float fdum;
   char *cdum;
   char  errmsg[MAX_ERR_LENGTH];

   exerrval = 0; /* clear error code */

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


/* inquire previously defined variable */

   if ((varid = ncvarid (exoid, VAR_GLO_VAR)) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to locate global variables in file id %d",
             exoid);
     ex_err("ex_get_glob_var_time",errmsg,exerrval);
     return (EX_WARN);
   }

/* read values of global variables */

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

   if (end_time_step < 0)
   {

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

   end_time_step--;

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

   if (ncvarget (exoid, varid, start, count,
             ex_conv_array(exoid,RTN_ADDRESS,glob_var_vals,count[0])) == -1)
   {
     exerrval = ncerr;
     sprintf(errmsg,
            "Error: failed to get global variable %d values from file id %d",
            glob_var_index, exoid);
     ex_err("ex_get_glob_var_time",errmsg,exerrval);
     return (EX_FATAL);
   }

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

   return (EX_NOERR);
}