On Fri, Sep 28, 2012 at 12:39 AM, JBT <
jianb...@gmail.com> wrote:
> Hi,
>
> I am new to cython. I am currently trying to make a python wrapper to use
> NASA CDF library. This is currently for practicing purposes, but hopefully
> it will eventually work out and turn into something useful.
>
> Anyway, for now I am trying to translate the following c codes to cython
> codes.
> ------------------------ below is from cdf.h
> ------------------------------------------------
> typedef void *CDFid;
> typedef long CDFstatus;
>
> static double *TT2000NULL = 0;
>
> #define HOST_ENCODING 8L
> #define ROW_MAJOR 1L
> #define SINGLE_FILE 1L
>
>
> ----------------------------- below is from an example, say, example.c
> -----------------------
> CDFid test_id; /* CDF identifier (handle). */
> CDFstatus status; /* Status returned from CDF library.
> */
> static char CDFname[] = {"test1"}; /* File name of the CDF. */
> long numDims = 2; /* Number of dimensions. */
> static long dimSizes[2] = {100,200}; /* Dimension sizes. */
> long encoding = HOST_ENCODING; /* Data encoding. */
> long majority = ROW_MAJOR; /* Variable data majority. */
> long form = SINGLE_FILE; /* Format of CDF. */
>
>
> Below is my translation.
> ------------------------------ below is from nasacdf.pxd
> ----------------------------
> cdef extern from "cdf34_1-dist/include/cdf.h":
> ctypedef void *CDFid;
> ctypedef long CDFstatus;
> static double *TT2000NULL = 0;
> enum:
> HOST_ENCODING = 8;
> ROW_MAJOR = 1;
> SINGLE_FILE = 1;
>
> -------------------------------below is from test.pyx
> ------------------------------
> cimport nasacdf
>
> def hello():
> cdef CDFid test_id
> cdef CDFstatus status
> # cdef char CDFname[] = {"test1"}
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> cdef long majority = ROW_MAJOR
> cdef long form = SINGLE_FILE
>
> print CDFname
>
> ----------------------------- below is my setup.py ---------------------
> from distutils.core import setup
> from distutils.extension import Extension
> from Cython.Distutils import build_ext
>
> cdfdist = 'cdf34_1-dist/'
> modules = Extension("cdf",
> sources = ['test.pyx'],
> libraries = [cdfdist + 'lib/libcdf.a'],
> extra_compile_args = ['-D_FILE_OFFSET_BITS=64', '-D_LARGEFILE64_SOURCE',
> '-D_LARGEFILE_SOURCE'])
>
> setup(name = 'pyNASACDF',
> version = '0.1.0',
> cmdclass = {'build_ext':build_ext},
> ext_modules = [modules]
> )
>
> ---------------------------------------- end of code
> --------------------------------------
> I modified the CDFname declaration and commented out dimSizes declaration
> because the compiler issued errors on them. After I run:
> python setup.py build_ext --inplace
> I got errors. I attached the error message below. Can anyone tell me what is
> wrong with my translation, please? Thank you very much.
>
>>$ python setup.py build_ext --inplace
> running build_ext
> cythoning test.pyx to test.c
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> # File: nasacdf.pxd
>
> cdef extern from "cdf34_1-dist/include/cdf.h":
> ctypedef void *CDFid;
> ^
remove all your semicolon
> ------------------------------------------------------------
>
> nasacdf.pxd:4:24: Syntax error in ctypedef statement
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> # File test.pyx
>
> cimport nasacdf
>
> def hello():
> cdef CDFid test_id
> ^
> ------------------------------------------------------------
>
> test.pyx:6:9: 'CDFid' is not a type identifier
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
>
> cimport nasacdf
>
> def hello():
> cdef CDFid test_id
> cdef CDFstatus status
> ^
> ------------------------------------------------------------
>
> test.pyx:7:9: 'CDFstatus' is not a type identifier
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> cdef CDFstatus status
> # cdef char CDFname[] = {"test1"}
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> ^
> ------------------------------------------------------------
>
> test.pyx:12:45: undeclared name not builtin: HOST_ENCODING
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> # cdef char CDFname[] = {"test1"}
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> cdef long majority = ROW_MAJOR
> ^
> ------------------------------------------------------------
>
> test.pyx:13:41: undeclared name not builtin: ROW_MAJOR
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> cdef char *CDFname = "test1"
> cdef long numDims = 2
> # cdef static long dimSizes[2] = {100,200}
> cdef long encoding = HOST_ENCODING
> cdef long majority = ROW_MAJOR
> cdef long form = SINGLE_FILE
> ^
> ------------------------------------------------------------
>
> test.pyx:14:39: undeclared name not builtin: SINGLE_FILE
> building 'cdf' extension
> clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common
> -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX
> -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall
> -Wstrict-prototypes -DENABLE_DTRACE -pipe -arch i386 -arch x86_64
> -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
> -c test.c -o build/temp.macosx-10.8-intel-2.7/test.o -D_FILE_OFFSET_BITS=64
> -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE
> clang: warning: argument unused during compilation: '-mno-fused-madd'
> test.c:1:2: error: Do not use this file, it is the result of a failed Cython
> compilation.
> #error Do not use this file, it is the result of a failed Cython
> compilation.
> ^
> 1 error generated.
> error: command 'clang' failed with exit status 1
>
>