Re: Trouble passing array to f90 subroutine using WRAPIT

From: Rick Brownrigg <brownrig_at_nyahnyahspammersnyahnyah>
Date: Mon Aug 26 2013 - 15:19:05 MDT

Hmmm…these codes work for me when I replace the PRINT statement by "write(6,'(f6.3)') xtas(1,1) (I also replaced the indexing in the script to be (0,0), which as Dennis points out is NCL's convention for the 1st element):

ncl writearr2.ncl
 Copyright (C) 1995-2012 - All Rights Reserved
 University Corporation for Atmospheric Research
 NCAR Command Language Version 6.2.0
 The use of this software is governed by a License Agreement.
 See http://www.ncl.ucar.edu/ for more details.
(0) xtas(0,0) in NCL: 0.323711
 0.324

This is one a Mac as well. I'm not sure what to suggest. It might be useful to look at the generated code (file WRAPIT.c), i.e., maybe try running wrapit with the "-d" switch.

Rick

On Aug 26, 2013, at 2:05 PM, David Rasmussen <drasmussen@ucdavis.edu> wrote:

> Here are the files I am using to reproduce this behavior. Perhaps it is a problem that is due to my system's environment.
>
> ; test_pass_arr.ncl
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> begin
> external WRITEARRAY "./writearr2.so"
>
> ntim=365*95
> nmod=33
> xtas=new((/ntim,nmod/),"float",-999.999)
> xtas=random_uniform(0,1,(/ntim,nmod/))
> print("xtas(1,1) in NCL: "+xtas(1,1))
>
> WRITEARRAY::writearr(ntim,nmod,xtas)
>
> end
>
> writearr2.f90:
>
> subroutine writearr(ntim,nmod,xtas)
> implicit none
> integer, intent(in) ::ntim,nmod
> real, intent(in) ::xtas(ntim,nmod)
> print*, xtas(1,1)
> return
> end subroutine writearr
>
> stub file:
>
> C NCLFORTSTART
> subroutine writearr(ntim,nmod,xtas)
> implicit none
> integer ntim,nmod
> real xtas(nmod,ntim)
> C NCLEND
>
>
> Compiling with:
>
> WRAPIT -L /usr/local/gfortran/lib/ writearr2.stub writearr2.f90
>
>
>
>
>
> On Mon, Aug 26, 2013 at 3:43 PM, David Rasmussen <drasmussen@ucdavis.edu> wrote:
> I forgot to mention that when I tried passing a 2-D integer array, I re-declared all relevant variables as integers.
>
> What I found is that using...
>
> C NCLFORTSTART
> subroutine writeint(ntim,nmod,xtas)
> implicit none
> integer ntim,nmod
> integer xtas(nmod,ntim)
> C NCLEND
>
> ...works as expected. When I re-declare all relevant variables as floats/reals and use the following...
>
>
> C NCLFORTSTART
> subroutine writeflt(ntim,nmod,xtas)
> implicit none
>
> integer ntim,nmod
> real xtas(nmod,ntim)
> C NCLEND
>
> ...I get the error:
>
> Line: 32 write(6,'(f6.3)') xtas(1,1)
>
> At line 32 of file writearr.f90 (unit = 6, file = 'stdout')
> Fortran runtime error: Expected REAL for item 2 in formatted transfer, got
> CHARACTER
> (f6.3)
>
> ...when I print xtas(1,1) unformatted (i.e. using print*), I get "????" (4 question marks).
>
> I am also changing data types & format codes in my .f90 and data types in my .ncl file in between these tests. Everything compiles without error.
>
> There seems to be an issue passing float/reals. I also tried passing double precision and the same result occurred. I am not sure why I am experiencing this behavior.
>
>
> On Mon, Aug 26, 2013 at 3:22 PM, Dennis Shea <shea@ucar.edu> wrote:
> Below you created
>
>
> >>> C NCLFORTSTART
> >>> subroutine writearr(ntim,nmod,xtas)
> >>> integer ntim,nmod
> >>> real xtas(nmod,ntim)
> >>> C NCLEND
>
> which declares 'xtas' as type 'real' (ie, a float)
>
> If you passed integers in 'xtas', there would be a problem.
> However, passing variables of type float/real should be
> exactly what is expected. Just the opposite of what you
> stated below. One approach to writing floats and
> integers is:
>
>
>
> C NCLFORTSTART
> subroutine writeint(ntim,nmod,xtas)
> implicit none
> integer ntim,nmod
> integer xtas(nmod,ntim)
> C NCLEND
>
>
>
>
> On 8/26/13 12:55 PM, David Rasmussen wrote:
> I take back some of what I said. The problem seems to be dependent on the
> variable type. I can pass 2-D integer arrays just fine, but I have trouble
> with floats, which is what I would like to pass between NCL and my f90
> subroutine.
>
>
> On Mon, Aug 26, 2013 at 1:34 PM, David Rasmussen <drasmussen@ucdavis.edu>wrote:
>
> No errors now, but when I print elements of the array I just get 4
> question marks (i.e "????") (I am guessing one for each byte?)
>
> When I try and format the output, Fortran tells me that my format choice
> is invalid because the data is of type character....
>
> Line: 32 write(6,'(f6.3)') xtas(1,1)
>
> At line 32 of file writearr.f90 (unit = 6, file = 'stdout')
> Fortran runtime error: Expected REAL for item 2 in formatted transfer, got
> CHARACTER
> (f6.3)
>
> I am able to print results from passed 1-D arrays OK. For passing 2-D
> arrays, does anything else special need to happen besides swapping array
> dimensions in the stub file? Again, my subroutine is f90.
>
> Thanks!
>
>
> On Mon, Aug 26, 2013 at 11:07 AM, Dennis Shea <shea@ucar.edu> wrote:
>
> Computer rule: Fastest varying dimension maps into fastest
> varying dimension. NCL (row major like C) is 0 based and the
> rightmost dimension varies fastest. Fortran is column major
> and the leftmost dimension varies fastest.
>
> Dimension order and subscripting are different for different languages.
>
> NCL: x(NA,NB,NC) <===> x(NC,NC,NA) : fortran
>
> In computer memory, think of the arrays as a long linear list and the
> elements are accessed via a particular language's conventions
>
> See: page 37: http://www.ncl.ucar.edu/**Document/Manuals/language_man.**
> pdf <http://www.ncl.ucar.edu/Document/Manuals/language_man.pdf>
>
>
> ---
> It should be:
>
>
> C NCLFORTSTART
> subroutine writearr(ntim,nmod,xtas)
> integer ntim,nmod
> real xtas(nmod,ntim)
> C NCLEND
>
> Note:No data rearranging should be done.
>
>
>
> On 8/26/13 8:47 AM, David Rasmussen wrote:
>
> Hi,
>
> I would like to write an array of data to a text file, similar to this
> request from a previous thread:
> http://www.ncl.ucar.edu/**Support/talk_archives/2012/**0230.html<http://www.ncl.ucar.edu/Support/talk_archives/2012/0230.html>
>
>
> I have chosen to use WRAPIT to write my data to disk.
>
> I am trying to pass an array to an f90 subroutine, but NCL/WRAPIT keeps
> telling me that my array is not dimensioned correctly.
>
> I have an array defined in the driving NCL script as:
>
> xtas(ntim,nmod), where ntim=34675, nmod=33
>
> I would like to pass this array to a f90 routine. When I try and do this,
> the routine expects xtas to be dimensioned xtas(nmod,ntim) which is NOT
> how
> it is defined in the driving NCL script...
>
> fatal:writearr: dimension size of dimension (1) of xtas must be equal to
> the value of ntim
> fatal:["Execute.c":8128]:**Execute: Error occurred at or near line 226
>
> in
> file extract_BCSD_lat_lon.ncl
>
> The call to the routine in the NCL script is:
> WRITEARRAY::writearr(ntim,**nmod,xtas)
>
>
> Immediately before calling the f90 routine, I do
> "printVarSummary(xtas)"...
>
> I get...
>
> Variable: xtas
> Type: float
> Total Size: 4577100 bytes
> 1144275 values
> Number of Dimensions: 2
> Dimensions and sizes: [34675] x [33]
> Coordinates:
> Number Of Attributes: 1
> _FillValue : -999.999
>
> The print summary for xtas is as expected...xtas(ntim,nmod)...
>
> my stub file is:
>
> C NCLFORTSTART
> subroutine writearr(ntim,nmod,xtas)
> integer ntim,nmod
> real xtas(ntim,nmod)
> C NCLEND
>
> my f90 routine is:
>
> subroutine writearr(ntim,nmod,xtas)
> integer ntim, nmod
> real xtas(ntim,nmod)
>
> print*, xtas
>
> return
> end subroutine writearr
>
> Both compile with no errors.
>
> Other info:
>
> WRAPIT Version: 120209
> OPERATING SYSTEM: Darwin
> FORTRAN COMPILER (f90c): gfortran
> FORTRAN COMPILER OPTIONS (fopts): -m64 -fPIC -v -c
> -fno-second-underscore
> gcc -m64 -c -fno-common -I/usr/local/ncl-6.1.2/include WRAPIT.c
> COMPILING writearr.f90
> gfortran -m64 -fPIC -v -c -fno-second-underscore writearr.f90
> Using built-in specs.
> COLLECT_GCC=gfortran
> COLLECT_LTO_WRAPPER=/usr/**local/gfortran/libexec/gcc/**
> x86_64-apple-darwin11/4.6.2/**lto-wrapper
> Target: x86_64-apple-darwin11
> Configured with: ../gcc-4.6.2-RC-20111019/**configure
> --prefix=/usr/local/gfortran
> --with-gmp=/Users/fx/devel/**gcc/deps-static/x86_64
> --enable-languages=c,c++,**fortran,objc,obj-c++
>
> --build=x86_64-apple-darwin11
> Thread model: posix
> gcc version 4.6.2 20111019 (prerelease) (GCC)
> COLLECT_GCC_OPTIONS='-mmacosx-**version-min=10.8.3' '-m64' '-fPIC' '-v'
> '-c'
> '-fno-second-underscore' '-mtune=core2'
> /usr/local/gfortran/libexec/**gcc/x86_64-apple-darwin11/4.6.**2/f951
>
> writearr.f90 -fPIC -quiet -dumpbase writearr.f90
> -mmacosx-version-min=10.8.3 -m64 -mtune=core2 -auxbase writearr -version
> -fPIC -fno-second-underscore -fintrinsic-modules-path
> /usr/local/gfortran/lib/gcc/**x86_64-apple-darwin11/4.6.2/**finclude -o
> /var/folders/fl/**p8bxmn6x0gb7vk2qlzv90bdh0000gn**/T//cc8VXUoN.s
>
> GNU Fortran (GCC) version 4.6.2 20111019 (prerelease)
> (x86_64-apple-darwin11)
> compiled by GNU C version 4.6.2 20111019 (prerelease), GMP version 5.0.2,
> MPFR version 3.0.1-p4, MPC version 0.9
> GGC heuristics: --param ggc-min-expand=100 --param
> ggc-min-heapsize=131072
> GNU Fortran (GCC) version 4.6.2 20111019 (prerelease)
> (x86_64-apple-darwin11)
> compiled by GNU C version 4.6.2 20111019 (prerelease), GMP version 5.0.2,
> MPFR version 3.0.1-p4, MPC version 0.9
> GGC heuristics: --param ggc-min-expand=100 --param
> ggc-min-heapsize=131072
> COLLECT_GCC_OPTIONS='-mmacosx-**version-min=10.8.3' '-m64' '-fPIC' '-v'
>
> '-c'
> '-fno-second-underscore' '-mtune=core2'
> as -arch x86_64 -force_cpusubtype_ALL -o writearr.o
> /var/folders/fl/**p8bxmn6x0gb7vk2qlzv90bdh0000gn**/T//cc8VXUoN.s
> COMPILER_PATH=/usr/local/**gfortran/libexec/gcc/x86_64-**
> apple-darwin11/4.6.2/:/usr/**local/gfortran/libexec/gcc/**
> x86_64-apple-darwin11/4.6.2/:/**usr/local/gfortran/libexec/**
> gcc/x86_64-apple-darwin11/:/**usr/local/gfortran/lib/gcc/**
> x86_64-apple-darwin11/4.6.2/:/**usr/local/gfortran/lib/gcc/**
> x86_64-apple-darwin11/
> LIBRARY_PATH=/usr/local/**gfortran/lib/gcc/x86_64-apple-**
> darwin11/4.6.2/:/usr/local/**gfortran/lib/gcc/x86_64-apple-**
> darwin11/4.6.2/../../../:/usr/**lib/
> COLLECT_GCC_OPTIONS='-mmacosx-**version-min=10.8.3' '-m64' '-fPIC' '-v'
>
> '-c'
> '-fno-second-underscore' '-mtune=core2'
> SHARED OBJECT NAME (SharedObj): writearr2.so
> LINKER SUFFIX (ld_suffix): WRAPIT.o writearr.o -L/usr/local/gfortran/lib/
> -o writearr2.so
>
> LINKING
> gcc -m64 -bundle -flat_namespace -undefined suppress WRAPIT.o writearr.o
> -L/usr/local/gfortran/lib/ -o writearr2.so -lgfortran
> END WRAPIT
>
> I must be missing something here. I am not trying to do anything
> complicated. Thanks in advance.
>
>
>
> ______________________________**_________________
>
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/**mailman/listinfo/ncl-talk<http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
>
>
>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Aug 26 15:19:18 2013

This archive was generated by hypermail 2.1.8 : Fri Aug 30 2013 - 14:04:57 MDT