Example #1
0
int
NC3_def_dim(int ncid, const char *name, size_t size, int *dimidp)
{
	int status;
	NC *nc;
	NC3_INFO* ncp;
	int dimid;
	NC_dim *dimp;

	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	if(!NC_indef(ncp))
		return NC_ENOTINDEFINE;

	status = NC_check_name(name);
	if(status != NC_NOERR)
		return status;

	if(ncp->flags & NC_64BIT_DATA) {/*CDF-5*/
	    if((sizeof(size_t) > 4) && (size > X_UINT64_MAX - 3)) /* "- 3" handles rounded-up size */
		return NC_EDIMSIZE;
	} else if(ncp->flags & NC_64BIT_OFFSET) {/* CDF2 format and LFS */
	    if((sizeof(size_t) > 4) && (size > X_UINT_MAX - 3)) /* "- 3" handles rounded-up size */
		return NC_EDIMSIZE;
	} else {/*CDF-1*/
	    if(size > X_INT_MAX - 3)
		return NC_EDIMSIZE;
	}

	if(size == NC_UNLIMITED)
	{
		dimid = find_NC_Udim(&ncp->dims, &dimp);
		if(dimid != -1)
		{
			assert(dimid != -1);
			return NC_EUNLIMIT;
		}
	}

	dimid = NC_finddim(&ncp->dims, name, &dimp);
	if(dimid != -1)
		return NC_ENAMEINUSE;

	dimp = new_NC_dim(name, size);
	if(dimp == NULL)
		return NC_ENOMEM;
	status = incr_NC_dimarray(&ncp->dims, dimp);
	if(status != NC_NOERR)
	{
		free_NC_dim(dimp);
		return status;
	}

	if(dimidp != NULL)
		*dimidp = (int)ncp->dims.nelems -1;
	return NC_NOERR;
}
Example #2
0
int
ncmpi_rename_dim( int ncid, int dimid, const char *newname)
{
    int file_ver, status, existid;
    NC *ncp;
    NC_dim *dimp;

    status = ncmpii_NC_check_id(ncid, &ncp); 
    if (status != NC_NOERR)
        return status;

    if (NC_readonly(ncp))
        return NC_EPERM;

    file_ver = 1;
    if (fIsSet(ncp->flags, NC_64BIT_OFFSET))
        file_ver = 2;
    else if (fIsSet(ncp->flags, NC_64BIT_DATA))
        file_ver = 5;

    status = ncmpii_NC_check_name(newname, file_ver);
    if (status != NC_NOERR) return status;

    existid = NC_finddim(&ncp->dims, newname, &dimp);
    if (existid != -1)
        return NC_ENAMEINUSE;

    dimp = ncmpii_elem_NC_dimarray(&ncp->dims, (size_t) dimid);
    if (dimp == NULL)
        return NC_EBADDIM;

    if (NC_indef(ncp)) {
        NC_string *old = dimp->name;
        NC_string *newStr = ncmpii_new_NC_string(strlen(newname), newname);
        if (newStr == NULL)
            return NC_ENOMEM;
        dimp->name = newStr;
        ncmpii_free_NC_string(old);
        return NC_NOERR;
    }
    /* else, not in define mode */

    status = ncmpii_set_NC_string(dimp->name, newname);
    if (status != NC_NOERR)
        return status;

    set_NC_hdirty(ncp);

    if (NC_doHsync(ncp)) {
        status = ncmpii_NC_sync(ncp, 1);
        if (status != NC_NOERR)
            return status;
    }

    return NC_NOERR;
}
Example #3
0
int CALLCONVENTION
nc_inq_dimid(int ncid, const char *name, int *dimid_ptr)
{
	int status;
	NC *ncp;
	int dimid;

	status = NC_check_id(ncid, &ncp); 
	if(status != NC_NOERR)
		return status;

	dimid = NC_finddim(&ncp->dims, name, NULL);

	if(dimid == -1)
		return NC_EBADDIM;

	*dimid_ptr = dimid;
	return NC_NOERR;
}
Example #4
0
int
NC3_inq_dimid(int ncid, const char *name, int *dimid_ptr)
{
	int status;
	NC *nc;
	NC3_INFO* ncp;
	int dimid;

	status = NC_check_id(ncid, &nc); 
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	dimid = NC_finddim(&ncp->dims, name, NULL);

	if(dimid == -1)
		return NC_EBADDIM;

	if (dimid_ptr)
	   *dimid_ptr = dimid;
	return NC_NOERR;
}
Example #5
0
int
NC3_rename_dim( int ncid, int dimid, const char *unewname)
{
	int status;
	NC *nc;
	NC3_INFO* ncp;
	int existid;
	NC_dim *dimp;
	char *newname;		/* normalized */

	status = NC_check_id(ncid, &nc); 
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	if(NC_readonly(ncp))
		return NC_EPERM;

	status = NC_check_name(unewname);
	if(status != NC_NOERR)
		return status;

	existid = NC_finddim(&ncp->dims, unewname, &dimp);
	if(existid != -1)
		return NC_ENAMEINUSE;

	dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid);
	if(dimp == NULL)
		return NC_EBADDIM;

	NC_string *old = dimp->name;
	newname = (char *)utf8proc_NFC((const unsigned char *)unewname);
	if(newname == NULL)
	    return NC_ENOMEM;
	if(NC_indef(ncp))
	{
		NC_string *newStr = new_NC_string(strlen(newname), newname);
		free(newname);
		if(newStr == NULL)
			return NC_ENOMEM;
		dimp->name = newStr;

		/* Remove old name from hashmap; add new... */
		NC_hashmapRemoveDim(&ncp->dims, old->cp);
		NC_hashmapAddDim(&ncp->dims, dimid, newStr->cp);
		free_NC_string(old);

		return NC_NOERR;
	}

	/* else, not in define mode */

	status = set_NC_string(dimp->name, newname);
	free(newname);
	if(status != NC_NOERR)
		return status;

	/* Remove old name from hashmap; add new... */
	NC_hashmapRemoveDim(&ncp->dims, old->cp);
	NC_hashmapAddDim(&ncp->dims, dimid, dimp->name->cp);

	set_NC_hdirty(ncp);

	if(NC_doHsync(ncp))
	{
		status = NC_sync(ncp);
		if(status != NC_NOERR)
			return status;
	}

	return NC_NOERR;
}
Example #6
0
int
NC3_rename_dim( int ncid, int dimid, const char *unewname)
{
	int status = NC_NOERR;
	NC *nc;
	NC3_INFO* ncp;
	int existid;
	NC_dim *dimp;
	char *newname = NULL; /* normalized */
	NC_string *old = NULL;
 	uintptr_t intdata;


	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		goto done;
	ncp = NC3_DATA(nc);

	if(NC_readonly(ncp))
		{status = NC_EPERM; goto done;}

	status = NC_check_name(unewname);
	if(status != NC_NOERR)
		goto done;

	existid = NC_finddim(&ncp->dims, unewname, &dimp);
	if(existid != -1)
		{status = NC_ENAMEINUSE; goto done;}

	dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid);
	if(dimp == NULL)
		{status = NC_EBADDIM; goto done;}

    old = dimp->name;
    status = nc_utf8_normalize((const unsigned char *)unewname,(unsigned char **)&newname);
    if(status != NC_NOERR)
	goto done;
    if(NC_indef(ncp))
	{
		NC_string *newStr = new_NC_string(strlen(newname), newname);
		if(newStr == NULL)
			{status = NC_ENOMEM; goto done;}

		/* Remove old name from hashmap; add new... */
	        NC_hashmapremove(ncp->dims.hashmap, old->cp, strlen(old->cp), NULL);
		dimp->name = newStr;

		intdata = dimid;
		NC_hashmapadd(ncp->dims.hashmap, intdata, newStr->cp, strlen(newStr->cp));
		free_NC_string(old);
		goto done;
	}

	/* else, not in define mode */

	/* If new name is longer than old, then complain,
           but otherwise, no change (test is same as set_NC_string)*/
	if(dimp->name->nchars < strlen(newname)) {
	    {status = NC_ENOTINDEFINE; goto done;}
	}

	/* Remove old name from hashmap; add new... */
	/* WARNING: strlen(NC_string.cp) may be less than NC_string.nchars */
	NC_hashmapremove(ncp->dims.hashmap, old->cp, strlen(old->cp), NULL);

	/* WARNING: strlen(NC_string.cp) may be less than NC_string.nchars */
	status = set_NC_string(dimp->name, newname);
	if(status != NC_NOERR)
		goto done;

        intdata = (uintptr_t)dimid;
	NC_hashmapadd(ncp->dims.hashmap, intdata, dimp->name->cp, strlen(dimp->name->cp));

	set_NC_hdirty(ncp);

	if(NC_doHsync(ncp))
	{
		status = NC_sync(ncp);
		if(status != NC_NOERR)
			goto done;
	}

done:
	if(newname) free(newname);
	return status;
}
Example #7
0
/*----< ncmpi_def_dim() >---------------------------------------------------*/
int
ncmpi_def_dim(int         ncid,    /* IN:  file ID */
              const char *name,    /* IN:  name of dimension */
              MPI_Offset  size,    /* IN:  dimension size */
              int        *dimidp)  /* OUT: dimension ID */
{
    int dimid, file_ver, status;
    NC *ncp;
    NC_dim *dimp;

    /* check if ncid is valid */
    status = ncmpii_NC_check_id(ncid, &ncp); 
    if (status != NC_NOERR) return status;

    /* check if called in define mode */
    if (!NC_indef(ncp)) return NC_ENOTINDEFINE;

    /* check if the name string is legal for netcdf format */
    file_ver = 1;
    if (fIsSet(ncp->flags, NC_64BIT_OFFSET))
        file_ver = 2;
    else if (fIsSet(ncp->flags, NC_64BIT_DATA))
        file_ver = 5;

    status = ncmpii_NC_check_name(name, file_ver);
    if (status != NC_NOERR) return status;

    /* MPI_Offset is usually a signed value, but serial netcdf uses 
     * MPI_Offset -- normally unsigned */
    if ((ncp->flags & NC_64BIT_OFFSET) && sizeof(off_t) > 4) {
        /* CDF2 format and LFS */
        if (size > X_UINT_MAX - 3 || (size < 0)) 
            /* "-3" handles rounded-up size */
            return NC_EDIMSIZE;
    } else if ((ncp->flags & NC_64BIT_DATA)) {
        /* CDF5 format*/
        if (size < 0) 
            return NC_EDIMSIZE;
    } else {
        /* CDF1 format */
        if (size > X_INT_MAX - 3 || (size < 0))
            /* "-3" handles rounded-up size */
            return NC_EDIMSIZE;
    }

    if (size == NC_UNLIMITED) {
        /* check for any existing unlimited dimension, netcdf allows
         * one per file
         */
        dimid = ncmpii_find_NC_Udim(&ncp->dims, &dimp);
        if (dimid != -1) return NC_EUNLIMIT; /* found an existing one */
    }

    /* check if exceeds the upperbound has reached */
    if (ncp->dims.ndefined >= NC_MAX_DIMS) return NC_EMAXDIMS;

    /* check if the name string is previously used */
    dimid = NC_finddim(&ncp->dims, name, &dimp);
    if (dimid != -1) return NC_ENAMEINUSE;
    
    /* create a new dimension object */
    dimp = ncmpii_new_NC_dim(name, size);
    if (dimp == NULL) return NC_ENOMEM;

    /* Add a new handle to the end of an array of handles */
    status = incr_NC_dimarray(&ncp->dims, dimp);
    if (status != NC_NOERR) {
        ncmpii_free_NC_dim(dimp);
        return status;
    }

    if (dimidp != NULL)
        *dimidp = (int)ncp->dims.ndefined -1;
        /* ncp->dims.ndefined has been increased in incr_NC_dimarray() */

    return NC_NOERR;
}