/* Missing functionality that should be in nc_inq_dimid(), to get
 * dimid from a full dimension path name that may include group
 * names */
int 
nc_inq_dimid2(int ncid, const char *dimname, int *dimidp) {
    int ret = NC_NOERR;
    /* If '/' doesn't occur in dimname, just return id found by
     * nc_inq_dimid() */
    char *sp = strrchr(dimname, '/');
    if(!sp) { /* No '/' in dimname, so return nc_inq_dimid() result */
	ret = nc_inq_dimid(ncid, dimname, dimidp);
    } 
#ifdef USE_NETCDF4
    else {  /* Parse group name out and get dimid using that */
	size_t grp_namelen = sp - dimname;
	char *grpname = emalloc(grp_namelen + 1);
	int grpid;
	strncpy(grpname, dimname, grp_namelen);
	grpname[grp_namelen] = '\0';
	ret = nc_inq_grp_full_ncid(ncid, grpname, &grpid);
	if(ret == NC_NOERR) {
	    ret = nc_inq_dimid(grpid, dimname, dimidp);
	}
	free(grpname);
    }	
#endif	/* USE_NETCDF4 */
    return ret;
}
示例#2
0
/* Determine whether a group named formatting_specs.lgrps[igrp] exists
 * in a netCDF file or group with id ncid.  If so, return the count of
 * how many matching groups were found, else return a count of 0.  If
 * the name begins with "/", it is interpreted as an absolute group
 * name, in which case only 0 or 1 is returned.  Otherwise, interpret
 * it as a relative name, and the total number of occurrences within
 * the file/group identified by ncid is returned.  
 *
 * Also has side effect of updating the ngrpids and the associate
 * grpids array that represent the group list specified by the -g
 * option.  TODO: put this in its own function instead.
 */
static size_t
nc_inq_grpname_count(int ncid, int igrp, char **lgrps, idnode_t *grpids) {
    size_t count = 0;
#ifdef USE_NETCDF4
    int numgrps;
    int *ncids;
    int g;
    int grpid;
    int status;
#endif
    char *grpname = lgrps[igrp];

    /* permit empty string to also designate root group */
    if(grpname[0] == '\0' || STREQ(grpname,"/")) { 
	count = 1;
	idadd(grpids, ncid);
	return count;
    }
#ifdef USE_NETCDF4
    /* Handle absolute group names */
    if(grpname[0] == '/') {
	int grpid;
	status = nc_inq_grp_full_ncid(ncid, grpname, &grpid);
	if(status == NC_NOERR) {
	    count = 1;
	    idadd(grpids, grpid);
	} else if(status == NC_ENOGRP) {
	    count = 0;
	} else {
	    error("when looking up group %s: %s ", grpname, nc_strerror(status));
	}
	return count;
    }
    
    /* look in this group */
    status = nc_inq_grp_ncid(ncid, grpname, &grpid);
    if (status == NC_NOERR) {
	count++;
	idadd(grpids, grpid);
    }
    /* if this group has subgroups, call recursively on each of them */
    NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) );
    if(numgrps > 0) {
	/* Allocate memory to hold the list of group ids. */
	ncids = emalloc(numgrps * sizeof(int));
	/* Get the list of group ids. */
	NC_CHECK( nc_inq_grps(ncid, NULL, ncids) );
	/* Call this function recursively for each group. */
	for (g = 0; g < numgrps; g++) {
	    count += nc_inq_grpname_count(ncids[g], igrp, lgrps, grpids);
	}
	free(ncids);
    }
#endif /* USE_NETCDF4 */
    return count;    
}
示例#3
0
int
main(int argc, char **argv)
{
    int i,stat;
    int ncid, grpid;
    char* filename;
    char* oldname;
    char* newname;

    switch (argc) {
    case 0:
    case 1:
        usage("No arguments");
	break;
    case 2:
        usage("Too few arguments");
	break;
    case 3:
    default:
	filename = argv[1];
	oldname = argv[2];
	newname = argv[3];
	break;
    }

    if(strlen(filename) == 0)
	usage("bad filename argument");
    if(strlen(oldname) == 0)
	usage("bad old name argument");
    if(strlen(newname) == 0)
	usage("bad new name argument");

    stat = nc_open(filename,NC_WRITE,&ncid);
    check(stat);

#ifdef RENAME_DEBUG
    stat = nc_set_log_level(0);
    check(stat);
    nc_log_hdf5();
#endif

    stat = nc_inq_grp_full_ncid(ncid,oldname,&grpid);
    check(stat);

    stat = nc_rename_grp(grpid,newname);
    check(stat);

    stat = nc_close(ncid);
    check(stat);

    exit(0);
}
示例#4
0
/**
 * Given an exoid and group name (NULL gets root group), return id of that
 * group.
 * If the name is NULL, return the root group.
 * If the name contains "/", then the name is assumed to be a full path name
 * and all groups in the file are searched.
 * Otherwise, the name is assumed to be the name of a child group of exoid
 */
int ex_get_group_id(int parent_id, const char *group_name, int *group_id)
{
  char errmsg[MAX_ERR_LENGTH];
#if NC_HAS_HDF5
  EX_FUNC_ENTER();
  /* See if name contains "/" indicating it is a full path name... */
  if (group_name == NULL) {
    /* Return root */
    *group_id = (unsigned)parent_id & EX_FILE_ID_MASK;
  }
  else if (strchr(group_name, '/') == NULL) {
    /* Local child */
    int status = nc_inq_grp_ncid(parent_id, group_name, group_id);
    if (status != NC_NOERR) {
      snprintf(errmsg, MAX_ERR_LENGTH,
               "ERROR: Failed to locate group with name %s as child "
               "group in file id %d",
               group_name, parent_id);
      ex_err(__func__, errmsg, status);
      EX_FUNC_LEAVE(EX_FATAL);
    }
  }
  else {
    /* Full path name */
    int status = nc_inq_grp_full_ncid(parent_id, group_name, group_id);
    if (status != NC_NOERR) {
      snprintf(errmsg, MAX_ERR_LENGTH,
               "ERROR: Failed to locate group with full path name %s in file id %d", group_name,
               parent_id);
      ex_err(__func__, errmsg, status);
      EX_FUNC_LEAVE(EX_FATAL);
    }
  }
  EX_FUNC_LEAVE(EX_NOERR);
#else
  (void)parent_id;
  (void)group_name;
  (void)group_id;
  EX_FUNC_ENTER();
  snprintf(errmsg, MAX_ERR_LENGTH,
           "ERROR: Group capabilities are not available in this netcdf "
           "version--not netcdf4");
  ex_err(__func__, errmsg, NC_ENOTNC4);
  EX_FUNC_LEAVE(EX_FATAL);
#endif
}
示例#5
0
/**
 * Given an exoid and group name (NULL gets root group), return id of that
 * group.
 * If the name is NULL, return the root group.
 * If the name contains "/", then the name is assumed to be a full path name
 * and all groups in the file are searched.
 * Otherwise, the name is assumed to be the name of a child group of exoid
 */
int ex_get_group_id(int parent_id, const char *group_name, int *group_id)
{
  char errmsg[MAX_ERR_LENGTH];

  exerrval = 0; /* clear error code */

#if NC_HAS_HDF5
  /* See if name contains "/" indicating it is a full path name... */
  if (group_name == NULL) {
    /* Return root */
    *group_id = (unsigned)parent_id & EX_FILE_ID_MASK;
  }
  else if (strchr(group_name, '/') == NULL) {
    /* Local child */
    int status = nc_inq_grp_ncid(parent_id, group_name, group_id);
    if (status != NC_NOERR) {
      exerrval = status;
      snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Failed to locate group with name %s as child "
                                       "group in file id %d",
               group_name, parent_id);
      ex_err("ex_get_group_id", errmsg, exerrval);
      return (EX_FATAL);
    }
  }
  else {
    /* Full path name */
    int status = nc_inq_grp_full_ncid(parent_id, group_name, group_id);
    if (status != NC_NOERR) {
      exerrval = status;
      snprintf(errmsg, MAX_ERR_LENGTH,
               "ERROR: Failed to locate group with full path name %s in file id %d", group_name,
               parent_id);
      ex_err("ex_get_group_id", errmsg, exerrval);
      return (EX_FATAL);
    }
  }
  return (EX_NOERR);
#else
  exerrval = NC_ENOTNC4;
  snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Group capabilities are not available in this netcdf "
                                   "version--not netcdf4");
  ex_err("ex_get_group_id", errmsg, exerrval);
  return (EX_FATAL);
#endif
}
示例#6
0
文件: nccopy.c 项目: U-238/gempak
/* Get parent id needed to define a new group from its full name in an
 * open file identified by ncid.  Assumes all intermediate groups are
 * already defined.  */
static int
nc_inq_parid(int ncid, const char *fullname, int *locidp) {
    int stat = NC_NOERR;
    char *parent = strdup(fullname);
    char *slash = "/";		/* groupname separator */
    char *last_slash;
    if(parent == NULL) {
	NC_CHECK(NC_ENOMEM);
    }
    last_slash = strrchr(parent, '/');
    if(last_slash == parent) {	/* parent is root */
	free(parent);
	parent = strdup(slash);
    } else {
	*last_slash = '\0';	/* truncate to get parent name */
    }
    NC_CHECK(nc_inq_grp_full_ncid(ncid, parent, locidp));
       free(parent);
    return stat;
}
示例#7
0
    int
    rsNcOpenGroup( rsComm_t *rsComm, ncOpenInp_t *ncOpenGroupInp, int **ncid ) {
        int remoteFlag;
        rodsServerHost_t *rodsServerHost;
        int status;
        dataObjInfo_t *dataObjInfo = NULL;
        int rl1descInx, l1descInx, myncid;
        int rootNcid;

        if ( getValByKey( &ncOpenGroupInp->condInput, NATIVE_NETCDF_CALL_KW ) != NULL ) {
            /* just all nc_open with objPath as file nc file path */
            /* must to be called internally */
            if ( rsComm->proxyUser.authInfo.authFlag < REMOTE_PRIV_USER_AUTH ) {
                return( CAT_INSUFFICIENT_PRIVILEGE_LEVEL );
            }
            status = nc_inq_grp_full_ncid( ncOpenGroupInp->rootNcid,
                                           ncOpenGroupInp->objPath, &myncid );
            if ( status == NC_NOERR ) {
                *ncid = ( int * ) malloc( sizeof( int ) );
                *( *ncid ) = myncid;
                return 0;
            }
            else {
                rodsLog( LOG_ERROR,
                         "rsNcOpenGroup: nc_open %s error, status = %d, %s",
                         ncOpenGroupInp->objPath, status, nc_strerror( status ) );
                return ( NETCDF_OPEN_ERR + status );
            }
        }
        rl1descInx = ncOpenGroupInp->rootNcid;
        l1desc_t& my_desc = irods::get_l1desc( rl1descInx );
        if ( rl1descInx < 2 || rl1descInx >= NUM_L1_DESC ) {
            rodsLog( LOG_ERROR,
                     "rsNcClose: rl1descInx %d out of range",
                     rl1descInx );
            return ( SYS_FILE_DESC_OUT_OF_RANGE );
        }
        if ( my_desc.inuseFlag != FD_INUSE ) {
            return BAD_INPUT_DESC_INDEX;
        }
        if ( my_desc.remoteZoneHost != NULL ) {
            ncOpenInp_t myNcOpenGroupInp;
            bzero( &myNcOpenGroupInp, sizeof( myNcOpenGroupInp ) );
            rstrcpy( myNcOpenGroupInp.objPath,
                     ncOpenGroupInp->objPath, MAX_NAME_LEN );
            myNcOpenGroupInp.rootNcid = my_desc.remoteL1descInx;

            status = rcNcOpenGroup( my_desc.remoteZoneHost->conn,
                                    &myNcOpenGroupInp, &myncid );
            if ( status < 0 ) {
                rodsLog( LOG_ERROR,
                         "rsNcOpenGroup: _rcNcOpenGroup %s error, status = %d",
                         ncOpenGroupInp->objPath, status );
                return ( status );
            }
            l1descInx = allocAndSetL1descForZoneOpr( myncid,
                        my_desc.dataObjInp,
                        my_desc.remoteZoneHost, NULL );
        }
        else {
            /* local zone */
            rootNcid = my_desc.l3descInx;
            remoteFlag = resoAndConnHostByDataObjInfo( rsComm,
                         my_desc.dataObjInfo, &rodsServerHost );
            if ( remoteFlag < 0 ) {
                return ( remoteFlag );
            }
            else if ( remoteFlag == LOCAL_HOST ) {
                status = nc_inq_grp_full_ncid( rootNcid,
                                               ncOpenGroupInp->objPath, &myncid );
                if ( status != NC_NOERR ) {
                    rodsLog( LOG_ERROR,
                             "rsNcOpenGroup: nc_inq_grp_full_ncid %s err, stat = %d, %s",
                             ncOpenGroupInp->objPath, status, nc_strerror( status ) );
                    return ( NETCDF_OPEN_ERR + status );
                }
            }
            else {
                /* execute it remotely */
                ncOpenInp_t myNcOpenGroupInp;
                bzero( &myNcOpenGroupInp, sizeof( myNcOpenGroupInp ) );
                rstrcpy( myNcOpenGroupInp.objPath,
                         ncOpenGroupInp->objPath, MAX_NAME_LEN );
                myNcOpenGroupInp.rootNcid = rootNcid;
                addKeyVal( &myNcOpenGroupInp.condInput, NATIVE_NETCDF_CALL_KW, "" );
                status = rcNcOpenGroup( rodsServerHost->conn, &myNcOpenGroupInp,
                                        &myncid );
                clearKeyVal( &myNcOpenGroupInp.condInput );
                if ( status < 0 ) {
                    rodsLog( LOG_ERROR,
                             "rsNcOpenGroup: rcNcOpenGroup %s error, status = %d",
                             myNcOpenGroupInp.objPath, status );
                    return ( status );
                }
            }
            l1descInx = allocL1desc();
            l1desc_t& new_desc = irods::get_l1desc( l1descInx );
            new_desc.dataObjInfo = dataObjInfo =
                                                ( dataObjInfo_t * ) calloc( 1, sizeof( dataObjInfo_t ) );
            rstrcpy( dataObjInfo->objPath,
                     ncOpenGroupInp->objPath, 
                     MAX_NAME_LEN );
            new_desc.l3descInx = myncid;
        }
        my_desc.oprType = NC_OPEN_GROUP;
        *ncid = ( int * ) malloc( sizeof( int ) );
        *( *ncid ) = l1descInx;

        return 0;
    }