Re: Read binary data generated by C++

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Fri Dec 17 2010 - 07:04:17 MST

Currently, NCL does not have the ability to explicitly
separate different variable types in a single read.
This type of mixed type record is often best read via
a shared object.

PART 1:

*If* you had an even number of integers (eg, 4 or 6)
you could to do a "read twice". Let's say that
instead of 5 integers, you had 6. The 1st mixed type
record in NCL (0-based) is zero.

   rnum = 0
   idata = fbindirread(path, rnum, 6, "integer")

   i1 = idata(0)
   i2 = idata(1)
      :
   i5 = idata(4)
   i6 = idata(5)

Then, a 2nd read for the type "double". The

   ddata = fbindirread(path, rnum, -1, "double")

   d = ddata(3:) ; skip 1st six 32-bit integers [3 double length]

=====
PART 2: Shared object

Don't know C++ but in fortran (untested).

C NCLFORTSTART
       subroutine dongli(fpath, idata, ddata, rlen, rnum, ni, nd)
       implicit none
       charcter*(*) fpath
       integer rlen, ni, nd, rnum, idata(ni)
       double precision ddata(nd)
C NCLEND
       integer ientry, iunit, ier
       data ientry /0/, iunit/58/

       if (ientry.eq.0) then
c open only once
           open (lsunit,file=fpath
      * ,access='direct'
      * ,form='unformatted'
      * ,recl=rlen
      * ,status='old',iostat=ier)
       end if

       read (iunit,rec=rnum) idata, ddata

       return
       end

 From the command line: if the above was named dongli.f

%> WRAPIT dongli.f

Then in NCL

external READ "./dongli.so"

    ni = 5
    nd = ???
    idata = new (5, "integer")
    ddata = new (???, "double")

    rlen = ni*4 + nd*8 ; rec length in bytes for fortran

    diri = "./" ; input directory
    fili = "foo" ; name of C++ created binary file
    path = diri + fili

    rnum = 1 ; fortran is 1-based; read 1st record
    READ::dongli(path, idata, ddata, rlen, rnum, ni, nd)

On 12/17/10 5:11 AM, DONG Li wrote:
> Dear all,
>
> I have generated a binary data using C++, and I want to read it in the NCL. There seems to be no record concept in the C++, so I have chosen "fbindirread" to do this job, but what does "rec_num" mean here? The content of the binary looks like:
>
> 1 (int)
> 2 (int)
> 3 (int)
> -999 (int)
> 12 (int)
> ... (double)
> ... (double)
>
> I have read the first five integers, but the "double" is not right. Any advice?
>
> Best regards,
>
> DONG Li
>
>
> _______________________________________________
> 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 Fri Dec 17 07:04:23 2010

This archive was generated by hypermail 2.1.8 : Wed Dec 22 2010 - 16:10:23 MST