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;
----------------------------- 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
---------------------------------------- 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.
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
On Fri, Sep 28, 2012 at 12:39 AM, JBT <jianbao....@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;
> ----------------------------- 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
> ---------------------------------------- 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.
> 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
> ------------------------------ 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
As normal in Python, you have to either prefix the names you use with the
module that you imported, or import the names that the module defines
directly into your code. I.e. either use
cimport nasacdf
cdef nasacdf.CDFid test_id
or
from nasacdf cimport CDFid
cdef CDFid test_id
or any mix of the two as you see fit. Cython is a lot closer to Python than
to C. It actually has namespaces.
So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport statement to: from cdc cimport *
Then I got this error: Error compiling Cython file: ------------------------------------------------------------ ... # File: cdf.pxd
cdef extern from "cdf34_1-dist/include/cdf.h": ctypedef void *CDFid ctypedef long CDFstatus static double *TT2000NULL = 0 ^ ------------------------------------------------------------
cdf.pxd:6:18: Syntax error in C variable declaration
As you can see, the *static double* statement is copied from cdf.h, so I don't understand why cython sees that as an error. Any ideas? Thank you so much.
BTW, Yichao, I removed all the semicolons per your suggestion. :-)
> > -------------------------------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
> As normal in Python, you have to either prefix the names you use with the > module that you imported, or import the names that the module defines > directly into your code. I.e. either use
> cimport nasacdf
> cdef nasacdf.CDFid test_id
> or
> from nasacdf cimport CDFid
> cdef CDFid test_id
> or any mix of the two as you see fit. Cython is a lot closer to Python > than > to C. It actually has namespaces.
> cdf.pxd:6:18: Syntax error in C variable declaration
> As you can see, the *static double* statement is copied from cdf.h, so I
> don't understand why cython sees that as an error. Any ideas? Thank you so
> much.
> BTW, Yichao, I removed all the semicolons per your suggestion. :-)
That is where the original syntax error coming from. afaik
>> > -------------------------------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
>> As normal in Python, you have to either prefix the names you use with the
>> module that you imported, or import the names that the module defines
>> directly into your code. I.e. either use
>> cimport nasacdf
>> cdef nasacdf.CDFid test_id
>> or
>> from nasacdf cimport CDFid
>> cdef CDFid test_id
>> or any mix of the two as you see fit. Cython is a lot closer to Python
>> than
>> to C. It actually has namespaces.
> On Fri, Sep 28, 2012 at 12:32 PM, JBT wrote:
>> So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport statement
>> to:
>> from cdc cimport *
>> Then I got this error:
>> Error compiling Cython file:
>> ------------------------------------------------------------
>> ...
>> # File: cdf.pxd
>> cdef extern from "cdf34_1-dist/include/cdf.h":
>> ctypedef void *CDFid
>> ctypedef long CDFstatus
>> static double *TT2000NULL = 0
> don't think cython understand static (or I'm wrong?)
> and u need a cdef here
> cdef double *.... = NULL
> works fine for me
The "cdef" isn't needed when you're inside of a "cdef extern" block anyway.
>> BTW, Yichao, I removed all the semicolons per your suggestion. :-)
> That is where the original syntax error coming from. afaik
On Fri, Sep 28, 2012 at 12:53 PM, Stefan Behnel <stefan...@behnel.de> wrote:
> Yichao Yu, 28.09.2012 18:45:
>> On Fri, Sep 28, 2012 at 12:32 PM, JBT wrote:
>>> So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport statement
>>> to:
>>> from cdc cimport *
>>> Then I got this error:
>>> Error compiling Cython file:
>>> ------------------------------------------------------------
>>> ...
>>> # File: cdf.pxd
>>> cdef extern from "cdf34_1-dist/include/cdf.h":
>>> ctypedef void *CDFid
>>> ctypedef long CDFstatus
>>> static double *TT2000NULL = 0
>> don't think cython understand static (or I'm wrong?)
>> and u need a cdef here
>> cdef double *.... = NULL
>> works fine for me
> The "cdef" isn't needed when you're inside of a "cdef extern" block anyway.
true....
>>> BTW, Yichao, I removed all the semicolons per your suggestion. :-)
>> That is where the original syntax error coming from. afaik
> No, semicolons work the same way as in Python.
well not for cdef, (as in Python def)
i.e. not here
cdef extern from "cdf34_1-dist/include/cdf.h":
ctypedef void *CDFid;
^
------------------------------------------------------------
nasacdf.pxd:4:24: Syntax error in ctypedef statement
So, here is another question. Cython won't compile these two statements: cdef static char CDFname[] = {"test1"} cdef static long dimSizes[2] = {100,200} because cython sees them with syntax error. I found the corresponding c code (without the *cdef*) in the NASA CDF C reference manual, so I presume the C counterpart is correct. So, why does cython think the two statements are not correct?
On Friday, September 28, 2012 9:53:19 AM UTC-7, Stefan Behnel wrote:
> Yichao Yu, 28.09.2012 18:45: > > On Fri, Sep 28, 2012 at 12:32 PM, JBT wrote: > >> So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport > statement > >> to: > >> from cdc cimport *
> >> Then I got this error: > >> Error compiling Cython file: > >> ------------------------------------------------------------ > >> ... > >> # File: cdf.pxd
On Fri, Sep 28, 2012 at 10:32 AM, JBT <jianbao....@gmail.com> wrote:
> So, here is another question. Cython won't compile these two statements:
> cdef static char CDFname[] = {"test1"}
> cdef static long dimSizes[2] = {100,200}
> because cython sees them with syntax error. I found the corresponding c code
> (without the *cdef*) in the NASA CDF C reference manual, so I presume the C
> counterpart is correct. So, why does cython think the two statements are not
> correct?
becaseu that is how you initialize arrays in C, not Python (or Cython)
-- remember that Cyton is closer to Python is syntax than C. For
instance, the {} are used to defien a dcitionaly literal.
> On Friday, September 28, 2012 9:53:19 AM UTC-7, Stefan Behnel wrote:
>> Yichao Yu, 28.09.2012 18:45:
>> > On Fri, Sep 28, 2012 at 12:32 PM, JBT wrote:
>> >> So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport
>> >> statement
>> >> to:
>> >> from cdc cimport *
>> >> Then I got this error:
>> >> Error compiling Cython file:
>> >> ------------------------------------------------------------
>> >> ...
>> >> # File: cdf.pxd
>> > don't think cython understand static (or I'm wrong?)
>> > and u need a cdef here
>> > cdef double *.... = NULL
>> > works fine for me
>> The "cdef" isn't needed when you're inside of a "cdef extern" block
>> anyway.
>> >> BTW, Yichao, I removed all the semicolons per your suggestion. :-)
>> > That is where the original syntax error coming from. afaik
>> No, semicolons work the same way as in Python.
>> Stefan
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
> On Fri, Sep 28, 2012 at 10:32 AM, JBT wrote:
>> So, here is another question. Cython won't compile these two statements:
>> cdef static char CDFname[] = {"test1"}
>> cdef static long dimSizes[2] = {100,200}
>> because cython sees them with syntax error. I found the corresponding c code
>> (without the *cdef*) in the NASA CDF C reference manual, so I presume the C
>> counterpart is correct. So, why does cython think the two statements are not
>> correct?
> becaseu that is how you initialize arrays in C, not Python (or Cython)
> -- remember that Cyton is closer to Python is syntax than C. For
> instance, the {} are used to defien a dcitionaly literal.
I successfully used CDFlib to open and close a .cdf file, which is a big milestone for my purposes. So, I'd like to say thank you to you guys for helping me out along the way, especially to Stefan, for he, with others, developed cython such a great tool/language. I don't think I would reach this stage this fast if it weren't for cython. So, again, thank you very much. :-)
On Friday, September 28, 2012 12:43:36 PM UTC-7, Stefan Behnel wrote:
> Chris Barker, 28.09.2012 21:36: > > On Fri, Sep 28, 2012 at 10:32 AM, JBT wrote: > >> So, here is another question. Cython won't compile these two > statements: > >> cdef static char CDFname[] = {"test1"} > >> cdef static long dimSizes[2] = {100,200} > >> because cython sees them with syntax error. I found the corresponding c > code > >> (without the *cdef*) in the NASA CDF C reference manual, so I presume > the C > >> counterpart is correct. So, why does cython think the two statements > are not > >> correct?
> > becaseu that is how you initialize arrays in C, not Python (or Cython) > > -- remember that Cyton is closer to Python is syntax than C. For > > instance, the {} are used to defien a dcitionaly literal.
On Fri, Sep 28, 2012 at 12:43 PM, Stefan Behnel <stefan...@behnel.de> wrote:
> Chris Barker, 28.09.2012 21:36:
> > On Fri, Sep 28, 2012 at 10:32 AM, JBT wrote:
> >> So, here is another question. Cython won't compile these two statements:
> >> cdef static char CDFname[] = {"test1"}
> >> cdef static long dimSizes[2] = {100,200}
> >> because cython sees them with syntax error. I found the corresponding c
> code
> >> (without the *cdef*) in the NASA CDF C reference manual, so I presume
> the C
> >> counterpart is correct. So, why does cython think the two statements
> are not
> >> correct?
> > becaseu that is how you initialize arrays in C, not Python (or Cython)
> > -- remember that Cyton is closer to Python is syntax than C. For
> > instance, the {} are used to defien a dcitionaly literal.
On Fri, Sep 28, 2012 at 1:32 PM, JBT <jianbao....@gmail.com> wrote:
> Thanks, Yichao, Stefan.
> So, here is another question. Cython won't compile these two statements:
> cdef static char CDFname[] = {"test1"}
> cdef static long dimSizes[2] = {100,200}
> because cython sees them with syntax error. I found the corresponding c code
> (without the *cdef*) in the NASA CDF C reference manual, so I presume the C
> counterpart is correct. So, why does cython think the two statements are not
> correct?
> On Friday, September 28, 2012 9:53:19 AM UTC-7, Stefan Behnel wrote:
>> Yichao Yu, 28.09.2012 18:45:
>> > On Fri, Sep 28, 2012 at 12:32 PM, JBT wrote:
>> >> So, after changing nasacdf.pxd to cdf.pxd, I modified the cimport
>> >> statement
>> >> to:
>> >> from cdc cimport *
>> >> Then I got this error:
>> >> Error compiling Cython file:
>> >> ------------------------------------------------------------
>> >> ...
>> >> # File: cdf.pxd