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
/*----< 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;
}