Commit 79998961 authored by Andy Cedilnik's avatar Andy Cedilnik
Browse files

ENH: Initial import of NetCDF

parent 84c39da7
PROJECT(VTKNETCDF)
INCLUDE_REGULAR_EXPRESSION(".*")
SET(netcdf_SRCS
attr.c
dim.c
error.c
libvers.c
nc.c
ncio.c
ncx.c
putget.c
string.c
v1hpg.c
v2i.c
var.c
)
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
# Include all the necessary files for macros
INCLUDE (CheckIncludeFiles)
INCLUDE (CheckFunctionExists)
INCLUDE (CheckTypeSize)
INCLUDE (CheckSymbolExists)
INCLUDE (TestBigEndian)
MACRO(MANGLE_VARIABLE_NAME str var prefix)
STRING(TOUPPER "${str}" mangle_variable_name_var)
STRING(REGEX REPLACE "[/. ]" "_" mangle_variable_name_var "${mangle_variable_name_var}")
SET(${var} "${prefix}${mangle_variable_name_var}")
ENDMACRO(MANGLE_VARIABLE_NAME str var)
# Check if header file exists and add it to the list.
MACRO(CHECK_INCLUDE_FILE_CONCAT FILE)
MANGLE_VARIABLE_NAME("${FILE}" "CHECK_INCLUDE_FILE_CONCAT_VAR" "HAVE_")
CHECK_INCLUDE_FILES("${HEADER_INCLUDES};${FILE}" ${CHECK_INCLUDE_FILE_CONCAT_VAR})
IF(${CHECK_INCLUDE_FILE_CONCAT_VAR})
SET(HEADER_INCLUDES ${HEADER_INCLUDES} ${FILE})
ENDIF(${CHECK_INCLUDE_FILE_CONCAT_VAR})
ENDMACRO(CHECK_INCLUDE_FILE_CONCAT)
MACRO(CHECK_FUNCTION_EXISTS_EX FUNC)
MANGLE_VARIABLE_NAME("${FUNC}" "CHECK_FUNCTION_EXISTS_EX_VAR" "HAVE_")
CHECK_FUNCTION_EXISTS("${FUNC}" "${CHECK_FUNCTION_EXISTS_EX_VAR}")
ENDMACRO(CHECK_FUNCTION_EXISTS_EX)
MACRO(CHECK_SYMBOL_EXISTS_EX SYM)
MANGLE_VARIABLE_NAME("${SYM}" "CHECK_SYMBOL_EXISTS_EX_VAR" "HAVE_")
CHECK_SYMBOL_EXISTS("${SYM}" "${HEADER_INCLUDES}" "${CHECK_SYMBOL_EXISTS_EX_VAR}")
ENDMACRO(CHECK_SYMBOL_EXISTS_EX)
MACRO(CHECK_TYPE_SIZE_EX type)
MANGLE_VARIABLE_NAME("${type}" "check_type_size_var" "")
CHECK_TYPE_SIZE("${type}" "SIZEOF_${check_type_size_var}")
IF(HAVE_${check_type_size_var})
SET("HAVE_${check_type_size_var}" 1)
ENDIF(HAVE_${check_type_size_var})
ENDMACRO(CHECK_TYPE_SIZE_EX)
MACRO(C_SOURCE_COMPILES SOURCE VAR)
IF("${VAR}" MATCHES "^${VAR}$")
SET(MACRO_CHECK_FUNCTION_DEFINITIONS
"-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
IF(CMAKE_REQUIRED_LIBRARIES)
SET(C_SOURCE_COMPILES_ADD_LIBRARIES
"-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
ENDIF(CMAKE_REQUIRED_LIBRARIES)
FILE(WRITE "${CMAKE_BINARY_DIR}/CMakeTmp/src.c"
"${SOURCE}")
MESSAGE(STATUS "Performing NetCDF Test ${VAR}")
TRY_COMPILE(${VAR}
${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}/CMakeTmp/src.c
CMAKE_FLAGS
"${C_SOURCE_COMPILES_ADD_LIBRARIES}"
OUTPUT_VARIABLE OUTPUT)
IF(${VAR})
SET(${VAR} 1 CACHE INTERNAL "NetCDF test ${FUNCTION}")
MESSAGE(STATUS "Performing NetCDF Test ${VAR} - Success")
WRITE_FILE(${CMAKE_BINARY_DIR}/CMakeOutput.log
"Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
"${OUTPUT}\n"
"Source file was:\n${SOURCE}\n" APPEND)
ELSE(${VAR})
MESSAGE(STATUS "Performing NetCDF Test ${VAR} - Failed")
SET(${VAR} "" CACHE INTERNAL "NetCDF test ${FUNCTION}")
WRITE_FILE(${CMAKE_BINARY_DIR}/CMakeError.log
"Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
"${OUTPUT}\n"
"Source file was:\n${SOURCE}\n" APPEND)
ENDIF(${VAR})
ENDIF("${VAR}" MATCHES "^${VAR}$")
ENDMACRO(C_SOURCE_COMPILES)
CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
FOREACH(file
"alloca.h"
"stdlib.h"
"sys/types.h"
"sys/stat.h"
"unistd.h"
"fcntl.h"
"stdio.h"
"string.h"
"stddef.h"
"stdint.h"
)
CHECK_INCLUDE_FILE_CONCAT("${file}")
ENDFOREACH(file)
FOREACH(func
alloca
strerror
)
CHECK_SYMBOL_EXISTS_EX("${func}")
ENDFOREACH(func)
FOREACH(type
"size_t"
"ssize_t"
"ptrdiff_t"
"off_t"
"double"
"float"
"int"
"long"
"short"
"unsigned char"
)
CHECK_TYPE_SIZE_EX("${type}")
ENDFOREACH(type)
IF(HAVE_SIZEOF_UNSIGNED_CHAR)
SET(uchar "unsigned char")
ENDIF(HAVE_SIZEOF_UNSIGNED_CHAR)
SET(testsrc
"#include <sys/stat.h>
int main() { return 0; }
int t() {
struct stat s; s.st_blksize;
; return 0; }")
IF(HAVE_SYS_TYPES_H)
SET(testsrc "#include <sys/types.h>\n${testsrc}")
ENDIF(HAVE_SYS_TYPES_H)
IF(HAVE_SYS_STAT_H)
C_SOURCE_COMPILES("${testsrc}" HAVE_ST_BLKSIZE)
ENDIF(HAVE_SYS_STAT_H)
#
SET(testsrc
"main()
{
char* path = tmpnam(NULL);
int exitStatus = 1;
if (path != NULL)
{
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd != -1)
{
if (write(fd, \"0\", 1) == 1)
{
off_t pos = lseek(fd, 0, SEEK_CUR);
if (pos != (off_t)-1)
{
if (ftruncate(fd, 512) != -1)
{
if (pos == lseek(fd, 0, SEEK_CUR))
{
if (lseek(fd, 0, SEEK_SET) == 0)
{
char buf[512];
if (read(fd, buf, 512) == 512)
exitStatus = 0;
}
}
}
}
}
close(fd);
unlink(path);
}
}
return exitStatus;
}")
IF(HAVE_SYS_TYPES_H)
SET(testsrc "#include <sys/types.h>\n${testsrc}")
ENDIF(HAVE_SYS_TYPES_H)
IF(HAVE_SYS_STAT_H)
SET(testsrc "#include <sys/stat.h>\n${testsrc}")
ENDIF(HAVE_SYS_STAT_H)
IF(HAVE_FCNTL_H)
SET(testsrc "#include <fcntl.h>\n${testsrc}")
ENDIF(HAVE_FCNTL_H)
IF(HAVE_STDIO_H)
SET(testsrc "#include <stdio.h>\n${testsrc}")
ENDIF(HAVE_STDIO_H)
IF(HAVE_UNISTD_H)
SET(testsrc "#include <unistd.h>\n${testsrc}")
ENDIF(HAVE_UNISTD_H)
C_SOURCE_COMPILES("${testsrc}" HAVE_FTRUNCATE)
MACRO(INVERT_VARIABLE var val)
SET(INVERT_VARIABLE_VAR "${val}")
SET(${var} "1")
IF(INVERT_VARIABLE_VAR)
SET(${var} "0")
ENDIF(INVERT_VARIABLE_VAR)
ENDMACRO(INVERT_VARIABLE)
INVERT_VARIABLE(NO_STDLIB_H "${HAVE_STDLIB_H}")
INVERT_VARIABLE(NO_SYS_TYPES_H "${HAVE_SYS_TYPES_H}")
INVERT_VARIABLE(NO_STRERROR "${HAVE_STRERROR}")
IF(NOT HAVE_SIZEOF_SIZE_T)
SET(size_t long)
ENDIF(NOT HAVE_SIZEOF_SIZE_T)
IF(NOT HAVE_SIZEOF_OFF_T)
SET(off_t long)
ENDIF(NOT HAVE_SIZEOF_OFF_T)
IF(NOT HAVE_SIZEOF_SSIZE_T)
SET(ssize_t int)
ENDIF(NOT HAVE_SIZEOF_SSIZE_T)
IF(NOT HAVE_SIZEOF_PTRDIFF_T)
SET(ptrdiff_t int)
ENDIF(NOT HAVE_SIZEOF_PTRDIFF_T)
TEST_BIG_ENDIAN(WORDS_BIGENDIAN)
CONFIGURE_FILE(${NETCDF_SOURCE_DIR}/ncconfig.h.in
${NETCDF_BINARY_DIR}/ncconfig.h @ONLY IMMEDIATE)
ADD_LIBRARY(vtke2NetCDF
${netcdf_SRCS})
IF(NOT VTK_INSTALL_NO_LIBRARIES)
INSTALL_TARGETS(${VTK_INSTALL_LIB_DIR} vtke2NetCDF)
ENDIF(NOT VTK_INSTALL_NO_LIBRARIES)
IF(NOT VTK_INSTALL_NO_DEVELOPMENT)
INSTALL_FILES(${VTK_INSTALL_INCLUDE_DIR}/vtknetcdf .h
netcdf.h ncconfig.h)
ENDIF(NOT VTK_INSTALL_NO_DEVELOPMENT)
This diff is collapsed.
/*
* Copyright 1996, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
/* Id */
#include "nc.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "ncx.h"
#include "fbits.h"
/*
* Free dim
* Formerly
NC_free_dim(dim)
*/
void
free_NC_dim(NC_dim *dimp)
{
if(dimp == NULL)
return;
free_NC_string(dimp->name);
free(dimp);
}
NC_dim *
new_x_NC_dim(NC_string *name)
{
NC_dim *dimp;
dimp = (NC_dim *) malloc(sizeof(NC_dim));
if(dimp == NULL)
return NULL;
dimp->name = name;
dimp->size = 0;
return(dimp);
}
/*
* Formerly
NC_new_dim(const char *name, long size)
*/
static NC_dim *
new_NC_dim(const char *name, size_t size)
{
NC_string *strp;
NC_dim *dimp;
strp = new_NC_string(strlen(name), name);
if(strp == NULL)
return NULL;
dimp = new_x_NC_dim(strp);
if(dimp == NULL)
{
free_NC_string(strp);
return NULL;
}
dimp->size = size;
return(dimp);
}
static NC_dim *
dup_NC_dim(const NC_dim *dimp)
{
return new_NC_dim(dimp->name->cp, dimp->size);
}
/*
* Step thru NC_DIMENSION array, seeking the UNLIMITED dimension.
* Return dimid or -1 on not found.
* *dimpp is set to the appropriate NC_dim.
* The loop structure is odd. In order to parallelize,
* we moved a clearer 'break' inside the loop body to the loop test.
*/
int
find_NC_Udim(const NC_dimarray *ncap, NC_dim **dimpp)
{
assert(ncap != NULL);
if(ncap->nelems == 0)
return -1;
{
int dimid = 0;
NC_dim **loc = ncap->value;
for(; (size_t) dimid < ncap->nelems
&& (*loc)->size != NC_UNLIMITED; dimid++, loc++)
{
/*EMPTY*/
}
if(dimid >= ncap->nelems)
return(-1); /* not found */
/* else, normal return */
if(dimpp != NULL)
*dimpp = *loc;
return dimid;
}
}
/*
* Step thru NC_DIMENSION array, seeking match on name.
* Return dimid or -1 on not found.
* *dimpp is set to the appropriate NC_dim.
* The loop structure is odd. In order to parallelize,
* we moved a clearer 'break' inside the loop body to the loop test.
*/
static int
NC_finddim(const NC_dimarray *ncap, const char *name, NC_dim **dimpp)
{
assert(ncap != NULL);
if(ncap->nelems == 0)
return -1;
{
size_t slen = strlen(name);
int dimid = 0;
NC_dim **loc = (NC_dim **) ncap->value;
for(; (size_t) dimid < ncap->nelems
&& (strlen((*loc)->name->cp) != slen
|| strncmp((*loc)->name->cp, name, slen) != 0);
dimid++, loc++)
{
/*EMPTY*/
}
if(dimid >= ncap->nelems)
return(-1); /* not found */
/* else, normal return */
if(dimpp != NULL)
*dimpp = *loc;
return(dimid);
}
}
/* dimarray */
/*
* Free the stuff "in" (referred to by) an NC_dimarray.
* Leaves the array itself allocated.
*/
void
free_NC_dimarrayV0(NC_dimarray *ncap)
{
assert(ncap != NULL);
if(ncap->nelems == 0)
return;
assert(ncap->value != NULL);
{
NC_dim **dpp = ncap->value;
NC_dim *const *const end = &dpp[ncap->nelems];
for( /*NADA*/; dpp < end; dpp++)
{
free_NC_dim(*dpp);
*dpp = NULL;
}
}
ncap->nelems = 0;
}
/*
* Free NC_dimarray values.
* formerly
NC_free_array()
*/
void
free_NC_dimarrayV(NC_dimarray *ncap)
{
assert(ncap != NULL);
if(ncap->nalloc == 0)
return;
assert(ncap->value != NULL);
free_NC_dimarrayV0(ncap);
free(ncap->value);
ncap->value = NULL;
ncap->nalloc = 0;
}
int
dup_NC_dimarrayV(NC_dimarray *ncap, const NC_dimarray *ref)
{
int status = NC_NOERR;
assert(ref != NULL);
assert(ncap != NULL);
if(ref->nelems != 0)
{
const size_t sz = ref->nelems * sizeof(NC_dim *);
ncap->value = (NC_dim **) malloc(sz);
if(ncap->value == NULL)
return NC_ENOMEM;
(void) memset(ncap->value, 0, sz);
ncap->nalloc = ref->nelems;
}
ncap->nelems = 0;
{
NC_dim **dpp = ncap->value;
const NC_dim **drpp = (const NC_dim **)ref->value;
NC_dim *const *const end = &dpp[ref->nelems];
for( /*NADA*/; dpp < end; drpp++, dpp++, ncap->nelems++)
{
*dpp = dup_NC_dim(*drpp);
if(*dpp == NULL)
{
status = NC_ENOMEM;
break;
}
}
}
if(status != NC_NOERR)
{
free_NC_dimarrayV(ncap);
return status;
}
assert(ncap->nelems == ref->nelems);
return NC_NOERR;
}
/*
* Add a new handle on the end of an array of handles
* Formerly
NC_incr_array(array, tail)
*/
static int
incr_NC_dimarray(NC_dimarray *ncap, NC_dim *newelemp)
{
NC_dim **vp;
assert(ncap != NULL);
if(ncap->nalloc == 0)
{
assert(ncap->nelems == 0);
vp = (NC_dim **) malloc(NC_ARRAY_GROWBY * sizeof(NC_dim *));
if(vp == NULL)
return NC_ENOMEM;
ncap->value = vp;
ncap->nalloc = NC_ARRAY_GROWBY;
}
else if(ncap->nelems +1 > ncap->nalloc)
{
vp = (NC_dim **) realloc(ncap->value,
(ncap->nalloc + NC_ARRAY_GROWBY) * sizeof(NC_dim *));
if(vp == NULL)
return NC_ENOMEM;
ncap->value = vp;
ncap->nalloc += NC_ARRAY_GROWBY;
}
if(newelemp != NULL)
{
ncap->value[ncap->nelems] = newelemp;
ncap->nelems++;
}
return NC_NOERR;
}
NC_dim *
elem_NC_dimarray(const NC_dimarray *ncap, size_t elem)
{
assert(ncap != NULL);
/* cast needed for braindead systems with signed size_t */
if(ncap->nelems == 0 || (unsigned long) elem >= ncap->nelems)
return NULL;
assert(ncap->value != NULL);
return ncap->value[elem];
}
/* Public */
int
nc_def_dim(int ncid, const char *name, size_t size, int *dimidp)
{
int status;
NC *ncp;
int dimid;
NC_dim *dimp;
status = NC_check_id(ncid, &ncp);
if(status != NC_NOERR)
return status;
if(!NC_indef(ncp))
return NC_ENOTINDEFINE;
status = NC_check_name(name);
if(status != NC_NOERR)
return status;
/* cast needed for braindead systems with signed size_t */
if((unsigned long) size > X_INT_MAX) /* Backward compat */
return NC_EINVAL;
if(size == NC_UNLIMITED)
{
dimid = find_NC_Udim(&ncp->dims, &dimp);
if(dimid != -1)
{
assert(dimid != -1);
return NC_EUNLIMIT;
}
}
if(ncp->dims.nelems >= NC_MAX_DIMS)
return NC_EMAXDIMS;
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;
}
int
nc_inq_dimid(int ncid, const char *name, int *dimid_ptr)
{
int status;
NC *ncp;
int dimid;
status = NC_check_id(ncid, &ncp);