Re: [f2py] Character Arrays FROM Common Block
Kevin Manross <[email protected]> Mon, 10 Sep 2012 16:05:35 -0600
| Newsgroups | gmane.comp.python.f2py.user |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format. --===============1080712738647300273== Content-Type: multipart/alternative; boundary="------------020802060806050301000902" This is a multi-part message in MIME format. --------------020802060806050301000902 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit After futzing around with this for a few days, I believe I have a solution on the python side of things. Numpy's strides seems to work well here. (See http://scipy-lectures.github.com/advanced/advanced_numpy/index.html#indexing-scheme-strides and http://mentat.za.net/numpy/numpy_advanced_slides/). This assumes that the character length of any 2-D character array in FORTRAN is the second dimension. #!/usr/bin/env python import chartest import numpy as np from numpy.lib import stride_tricks chartest.foo() print chartest.test.strout print stride_tricks.as_strided(chartest.test.strout,\ strides=(chartest.test.strout.shape[1],1)) Hope this is helpful. -kevin. On 9/6/12 1:15 PM, Kevin Manross wrote: > > Greetings! > > I have been enjoying learning to use f2py as a relative novice in both > Python and FORTRAN. > > I have been working on wrapping the NOAA NCEP BUFRLIB library > <http://www.nco.ncep.noaa.gov/sib/decoders/BUFRLIB/> to make the I/O > more user friendly, to take advantage of the many efficient packages > available in Python, and to translate the data into other data formats > (such as netCDF). > > I have been successful in wrapping the library in order to use the > subroutines, but to have greater flexibility I need to access the > common block data. I can access it, and am trying to work through the > FORTRAN column-order issues between the respective arrays. The one > area that I'm really stuck on is trying to extract character arrays in > proper order. > > I have an example of the issue I'm running into: > > ============= FORTRAN File =============== > > C FILE: CHARTEST.F > SUBROUTINE FOO() > C > C ABSTRACT: Test character arrays when using with F2PY. Problem is in > C FORTRAN column-order, character arrays get all messed up. > C Hope to learn a way around this > C > C INPUTS: None > C OUTPUTS: STROUT - character array > C > C > COMMON /TEST/ STROUT(20) > CHARACTER*3 STROUT > > STROUT(1) = 'ABC' > STROUT(2) = 'DEF' > STROUT(3) = 'GHI' > STROUT(4) = 'JKL' > STROUT(5) = 'MNO' > STROUT(6) = 'PQR' > STROUT(7) = 'STU' > STROUT(8) = 'VWX' > STROUT(9) = 'YZ' > > END > > > > ========== Python Script ================= > > #!/usr/bin/env python > > import chartest > > chartest.foo() > print chartest.test.strout > > > > > So while I need to get the array elements back out in the order that > they are entered (I.e., 'ABC') the output I get is: > > % ./test_chartest.py > [['A' 'U' ''] > ['B' 'V' ''] > ['C' 'W' ''] > ['D' 'X' ''] > ['E' 'Y' ''] > ['F' 'Z' ''] > ['G' ' ' ''] > ['H' '' ''] > ['I' '' ''] > ['J' '' ''] > ['K' '' ''] > ['L' '' ''] > ['M' '' ''] > ['N' '' ''] > ['O' '' ''] > ['P' '' ''] > ['Q' '' ''] > ['R' '' ''] > ['S' '' ''] > ['T' '' '']] > > I've been playing with Numpy's myriad of array manipulation, but with > no success thus far. > > Is there a "built-in" way around this reordering of character arrays, > or will I need to create my own parser for this? > > Many thanks for any input! > > -kevin. > > > -- > Kevin Manross > NCAR/CISL/Data Support Section > Phone: (303)-497-1218 > Email:[email protected] <mailto:[email protected]> > Web:http://dss.ucar.edu > > > _______________________________________________ > f2py-users mailing list > f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] > http://cens.ioc.ee/mailman/listinfo/f2py-users -- Kevin Manross NCAR/CISL/Data Support Section Phone: (303)-497-1218 Email:[email protected] <mailto:[email protected]> Web:http://rda.ucar.edu --------------020802060806050301000902 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body bgcolor="#FFFFFF" text="#000000"> <br> After futzing around with this for a few days, I believe I have a solution on the python side of things. Numpy's strides seems to work well here. (See <a class="moz-txt-link-freetext" href="http://scipy-lectures.github.com/advanced/advanced_numpy/index.html#indexing-scheme-strides">http://scipy-lectures.github.com/advanced/advanced_numpy/index.html#indexing-scheme-strides</a> and <a class="moz-txt-link-freetext" href="http://mentat.za.net/numpy/numpy_advanced_slides/">http://mentat.za.net/numpy/numpy_advanced_slides/</a>).<br> <br> This assumes that the character length of any 2-D character array in FORTRAN is the second dimension.<br> <br> #!/usr/bin/env python<br> <br> <br> import chartest<br> import numpy as np<br> from numpy.lib import stride_tricks<br> <br> <br> chartest.foo()<br> print chartest.test.strout<br> <br> print stride_tricks.as_strided(chartest.test.strout,\<br> strides=(chartest.test.strout.shape[1],1))<br> <br> <br> <br> Hope this is helpful.<br> <br> -kevin.<br> <br> <div class="moz-cite-prefix">On 9/6/12 1:15 PM, Kevin Manross wrote:<br> </div> <blockquote cite="mid:[email protected]" type="cite"> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <br> Greetings!<br> <br> I have been enjoying learning to use f2py as a relative novice in both Python and FORTRAN.<br> <br> I have been working on wrapping the NOAA NCEP BUFRLIB library <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="http://www.nco.ncep.noaa.gov/sib/decoders/BUFRLIB/"><http://www.nco.ncep.noaa.gov/sib/decoders/BUFRLIB/></a> to make the I/O more user friendly, to take advantage of the many efficient packages available in Python, and to translate the data into other data formats (such as netCDF).<br> <br> I have been successful in wrapping the library in order to use the subroutines, but to have greater flexibility I need to access the common block data. I can access it, and am trying to work through the FORTRAN column-order issues between the respective arrays. The one area that I'm really stuck on is trying to extract character arrays in proper order.<br> <br> I have an example of the issue I'm running into:<br> <br> ============= FORTRAN File ===============<br> <br> C FILE: CHARTEST.F<br> SUBROUTINE FOO()<br> C<br> C ABSTRACT: Test character arrays when using with F2PY. Problem is in <br> C FORTRAN column-order, character arrays get all messed up. <br> C Hope to learn a way around this<br> C<br> C INPUTS: None<br> C OUTPUTS: STROUT - character array<br> C<br> C<br> COMMON /TEST/ STROUT(20)<br> CHARACTER*3 STROUT<br> <br> STROUT(1) = 'ABC'<br> STROUT(2) = 'DEF'<br> STROUT(3) = 'GHI'<br> STROUT(4) = 'JKL'<br> STROUT(5) = 'MNO'<br> STROUT(6) = 'PQR'<br> STROUT(7) = 'STU'<br> STROUT(8) = 'VWX'<br> STROUT(9) = 'YZ'<br> <br> END<br> <br> <br> <br> ========== Python Script =================<br> <br> #!/usr/bin/env python<br> <br> import chartest<br> <br> chartest.foo()<br> print chartest.test.strout<br> <br> <br> <br> <br> So while I need to get the array elements back out in the order that they are entered (I.e., 'ABC') the output I get is:<br> <br> % ./test_chartest.py<br> [['A' 'U' '']<br> ['B' 'V' '']<br> ['C' 'W' '']<br> ['D' 'X' '']<br> ['E' 'Y' '']<br> ['F' 'Z' '']<br> ['G' ' ' '']<br> ['H' '' '']<br> ['I' '' '']<br> ['J' '' '']<br> ['K' '' '']<br> ['L' '' '']<br> ['M' '' '']<br> ['N' '' '']<br> ['O' '' '']<br> ['P' '' '']<br> ['Q' '' '']<br> ['R' '' '']<br> ['S' '' '']<br> ['T' '' '']]<br> <br> I've been playing with Numpy's myriad of array manipulation, but with no success thus far.<br> <br> Is there a "built-in" way around this reordering of character arrays, or will I need to create my own parser for this?<br> <br> Many thanks for any input!<br> <br> -kevin.<br> <br> <br> <div class="moz-signature">-- <br> Kevin Manross<br> NCAR/CISL/Data Support Section<br> Phone: (303)-497-1218<br> Email:<a moz-do-not-send="true" href="mailto:[email protected]">[email protected]</a><br> Web:<a moz-do-not-send="true" href="http://dss.ucar.edu">http://dss.ucar.edu</a><br> </div> <br> <fieldset class="mimeAttachmentHeader"></fieldset> <br> <pre wrap="">_______________________________________________ f2py-users mailing list <a class="moz-txt-link-abbreviated" href="mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a> <a class="moz-txt-link-freetext" href="http://cens.ioc.ee/mailman/listinfo/f2py-users">http://cens.ioc.ee/mailman/listinfo/f2py-users</a> </pre> </blockquote> <br> <div class="moz-signature">-- <br> Kevin Manross<br> NCAR/CISL/Data Support Section<br> Phone: (303)-497-1218<br> Email:<a href="mailto:[email protected]">[email protected]</a><br> Web:<a href="http://rda.ucar.edu">http://rda.ucar.edu</a><br> </div> </body> </html> --------------020802060806050301000902-- --===============1080712738647300273== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ f2py-users mailing list f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] http://cens.ioc.ee/mailman/listinfo/f2py-users --===============1080712738647300273==--