Re: chars and f77

From: Dennis Shea (shea AT cgd.ucar.edu)
Date: Fri Jul 30 2004 - 10:53:48 MDT


>
>I know that passing characters to f77 routines is tricky, but I hope that
someone has done this before:
>
>I want to fill an array of strings in the f77 routine - how do I handle that?
For example, this might make sense but it does not work:
>
>-------------
>external MYMOD "./my_mod.f"
>
>begin
>
>char_array = new((/1000,14/),char)
>
>MYMOD::my_mod(char_array)
>
>end
>
>--------------
>C NCLFORTSTART
>subroutine my_mod(ch_array)
>
>character*14 ch_array(1000)
>
>C NCLEND
>
>C fill the array
>
>end
>
>Ideas? I am using version 4.2.0.a030 with Debian Linux.
>

Hi Josh,

Some general comments ... then a non-elegant 'work around'!

[0] I don't think this is a system issue. It is a fortran/C
    language issue. I speculate that the fortran standard
    does not specify how to pass fortran character variables.
    I think it is left to each compiler how they can most efficiently
    implement this. Also, fortran has no concept of "strings"
    even though they 'look' like NCL strings.

For fortran, I'll use f77 since that is what you are using

[1] You can pass an NCL string to fortran

    NCL: s = "Sample"
             mymod:foo1(...,s,...)
             
    f77: subroutine foo1 (...,sc,...)
             character*(*) sc ! note syntax
             print *, sc ! works fine
             
[2] You can pass a fortran character variable to NCL but
    (a) user must allocate a character variable in NCL script
    (a) then the user must convert it to a string.
    
    f77: subroutine foo2 (...,fchar,...)
             character*7 fchar
             fchar = "orange"
             
    NCL: char = new ( 7, "character") ; will be returned to NCL script
             mymod:foo2(...,char,...) ; invoke shared object
             str = chartostring(char) ; convert to string
             
[3] of course, you can pass multiple arguments
    ---
    sc1 = "..."
    sc2 = "..."
        :
    scn = "..."
    subroutine foo3(...,sc1,sc2,...,scn,...) ; ncl=>fortran
    ---
    char1 = new ( 7, "character")
    char2 = new ( 9, "character")
    charn = new (12, "character")
    mymod::foo4(...,char1,char2,charn,....)
    str1 = chartostring(char1)
    str2 = chartostring(char2)
    strn = chartostring(charn)
    
[4] A non-elegant work around for character variables in fortran
    being passed back to NCL
   
    f77: The following is in file my_mod.f
    
    C NCLFORTSTART
          subroutine josh(...,fname,...)
          character*(*) fname
          character*14 char_array(1000)
    C NCLEND
          open(11,file=fname) ! default is ascii, sequential
          do n=1,1000
             write(10,*) char_array(n)
          end do
          close (11)
    
          return
          end
    
    NCL: external MYMOD "./my_mod.so"
          
          fname = "whatever"
          MYMOD::josh(...,fname,...)
          
          fStrings = asciiread (fname, -1, "string")
          nStrings = dimsizes(fStrings)
          
          ; if u no longer need the ascii file
          
          system("/bin/rm "+fname) ; delete temporary file

[5] NCL strings [is this not what u really mean] passed to
    f77 code. It is basically the reverse of [4]
    
    NCL: sString = new ( 1000, "string")
                  : ; fill strings
          fname = "WhatEver"
          asciiwrite (fname, sString)
          
          system("/bin/rm "+fname)
    
    f77:
    C NCLFORTSTART
          subroutine josh(...,fname,...)
          character*(*) fname
          character*14 chrstr(1000)
    C NCLEND
          open (11,file=fname)
          do n=1,1000
             read(11,"(a)") chrstr(n)
          end do

HTH
D

_______________________________________________
ncl-talk mailing list
ncl-talk@ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk



This archive was generated by hypermail 2b29 : Fri Jul 30 2004 - 13:13:27 MDT