[ncl-talk] how to process string variable

Dennis Shea shea at ucar.edu
Wed Aug 28 10:16:21 MDT 2019


NCL is written in C.

[1] Passing *one* string C/NCL<==>Fortran is not a problem.
[2] Passing an *array of strings/character  *C<==>Fortran can be an issue.
Fortran does not allow variable length strings.

In the following, I changed the subroutine name: *mpr_match ==> match_mpr*
In the NCL code change the 1st string array element fro 5 to 4 characters.
See what happens.


C NCLFORTSTART
      subroutine *match_mpr*(ncvs,cvsid,obsid,istd)
      implicit none
      integer ncvs, istd
      character*(*) cvsid(ncvs)
      character*(*) obsid
C NCLEND
      integer  j, ls

       print *,  "ncvs=",ncvs
       print *,  "obsid=",obsid
       print *,  "cvsid=",cvsid    ! should print 'ncvs' values
       print *,  "istd=",istd
       print *,  "================"

       do j = 1, ncvs
         if(obsid.eq.cvsid(j)) then
            istd = j
         endif
         ls = len_trim(cvsid(j))
         print *, j,":", obsid,":", cvsid(j),":", istd,":",ls
       enddo
      end

I am sure there may be ways to work around the issue. Perhaps, make all
string lengths 10 with, say, a $ terminator

  "F1234$    "
Then in the fortran code extract the string-subset with the '$' terminator.
That is a wild guess.

[3] The Fortran 2003 standard introduced the *language-binding-spec*
attribute, using the keyword *BIND*.
See:
*https://software.intel.com/en-us/fortran-compiler-developer-guide-and-reference-bind*
<https://software.intel.com/en-us/fortran-compiler-developer-guide-and-reference-bind>

subroutine *match_mpr*(ncvs,cvsid,obsid,istd) BIND(C)

 *use iso_c_binding**, only*: ...
 implicit none

however, I am not sure how one would be able to implement this from 'WRAPIT'

Good luck


On Tue, Aug 27, 2019 at 9:53 AM Ming Chen <chenming at ucar.edu> wrote:

> Hi Dennis,
>
> Thanks for the answer, which solves my problem. However, I am still
> curious why the Fortran code doesn't work and how I can make it work. I ask
> this because I heavily rely on Fortran to process some in-situ
> observations, which includes both numbers and strings. I hope to understand
> more about the combination of Fortran with NCL.
>
> Thanks again.
>
> Ming
> On 8/26/19 6:04 PM, Dennis Shea wrote:
>
> No need for fortran code.
>
> *https://www.ncl.ucar.edu/Document/Functions/Contributed/get1Dindex.shtml*
> <https://www.ncl.ucar.edu/Document/Functions/Contributed/get1Dindex.shtml>
>
> ========================
>             ;        0             1            *2*
> 3            4               <==
>   cvsid = (/"76373", "76612", "*XRKF*", "PRUK", "DENB"/)
>   obsid = (/"*XRKF*", "76737", "BSRK"/)
>
>   i = *get1Dindex*(cvsid, obsid)
>   print(i)
>   print("====")
>
>   ni = num(.not.ismissing(i))
>   print("ni="+ni)
>   print("====")
> ==================================
> Variable: i
> Type: integer
> Total Size: 12 bytes
>             3 values
> Number of Dimensions: 1
> Dimensions and sizes: [3]
> Coordinates:
> Number Of Attributes: 3
>   _FillValue : -2147483647
>   index_info : Out of nWant=3 : multiple index occurrences=0
>   nMultInd : 0
>
> (0) *2*
> (1) -2147483647   <=== No match for *76737*
> (2) -2147483647   <=== No match for *BSRK*
> (0) ====
> (0) ni=*1                 *<=== only one match
> (0) ====
>
> On Mon, Aug 26, 2019 at 5:30 PM Ming Chen via ncl-talk <ncl-talk at ucar.edu>
> wrote:
>
>> Dear All,
>>
>> I am trying to match two sets of in-situ observations. First of all, I
>> need to match stations between the two sets of data. However, my scrip
>> failed with the error message below:
>>
>> fatal:Argument type mismatch on argument (1) of (mpr_match) can not coerce
>> fatal:["Execute.c":8637]:Execute: Error occurred at or near line 19 in
>> file test4.ncl
>>
>> I attach the sample ncl script and fortran code. Any help will be highly
>> appreciated.
>>
>> Ming
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20190828/5a71a11e/attachment.html>


More information about the ncl-talk mailing list