Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
read a C written binary file with IDL
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
bing999  
View profile  
 More options Feb 9, 10:44 pm
Newsgroups: comp.lang.idl-pvwave
From: bing999 <thibaultga...@gmail.com>
Date: Thu, 9 Feb 2012 19:44:01 -0800 (PST)
Local: Thurs, Feb 9 2012 10:44 pm
Subject: read a C written binary file with IDL
Hi,

I am having a problem with reading a C written binary file with IDL.
It may come from differences of type definitions between C and IDL but
I could not really figure out from Google...

In C, it writes a structure containing the following variable types:

struct MyStruct
{
int a;
long long b;
int c;
float d;

};

Then, in IDL, I read this with:

  MyStruct = {$
            a             : 0L, $
            b             : 0LL, $
            c             : 0L, $
            d             : 0.0  $
            }

     openr, 1, filename, /SWAP_IF_BIG_ENDIAN
     readu, 1, MyStruct
     close, 1

but this gives me wrong values.

Did I miss something about the type conversion??

If someone could please clarify this, it would really help!
Thanks !


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Manodeep Sinha  
View profile  
 More options Feb 9, 11:56 pm
Newsgroups: comp.lang.idl-pvwave
From: Manodeep Sinha <manod...@gmail.com>
Date: Thu, 9 Feb 2012 20:56:23 -0800 (PST)
Local: Thurs, Feb 9 2012 11:56 pm
Subject: Re: read a C written binary file with IDL
Hi,

This is because C pads the structure to produce alignments. Under
'normal' operations, you would expect MyStruct to be 20 bytes,
however, if you do a sizeof(struct MyStruct), you will probably see
that the size is 24. (And you can enable the warning for gcc by using
the compile time option -Wpadded).

In general, the padding is compiler specific -- so there is no
standard way of reading in those binary files into IDL/other codes.
The best bet would be to write out the individual fields of the
structure and then read them back into IDL.

HTH,
Manodeep

On Feb 9, 9:44 pm, bing999 <thibaultga...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bing999  
View profile  
 More options Feb 10, 12:08 am
Newsgroups: comp.lang.idl-pvwave
From: bing999 <thibaultga...@gmail.com>
Date: Thu, 9 Feb 2012 21:08:01 -0800 (PST)
Local: Fri, Feb 10 2012 12:08 am
Subject: Re: read a C written binary file with IDL
Hi Manodeep,

Thanks for your reply.
It turns out that a Python script can read the file correctly:

import numpy as np

 MyStruct = [                                                 # define
MyStruct
            ('a'                         ,
np.int32),
            ('b'                         ,
np.int64),
            ('c'                         , np.int32),
            ('d'                         , np.float32)
            ]

f1 = open(filename, 'rb')  # Open the file
S1 = np.fromfile(f1, MyStruct)  # read MyStruct

However, I have to use IDL (and I do not really know much about
Python...)

Do you have any idea why Python can handle it and not IDL?

cheers


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul van Delst  
View profile  
 More options Feb 10, 9:09 am
Newsgroups: comp.lang.idl-pvwave
From: Paul van Delst <paul.vande...@noaa.gov>
Date: Fri, 10 Feb 2012 09:09:14 -0500
Local: Fri, Feb 10 2012 9:09 am
Subject: Re: read a C written binary file with IDL
Have you tried ASSOC? It has a PACKED keyword you can play with:

<quote>
PACKED
When ASSOC is applied to structures, the default action is to map the actual definition of the structure for the current
machine, including any holes required to properly align the fields. (IDL uses the same rules for laying out structures
as the C language). If the PACKED keyword is specified, I/O using the resulting variable instead works in the same
manner as READU and WRITEU, and data is moved one field at a time and there are no alignment gaps between the fields.
</quote>

So you might want to experiment with ASSOC and /PACKED, e.g.

mystruct={a:0L, b:0LL, c:0L,d:0.0}
openr, 1, filename, /swap_if_big_endian
a = assoc(1, {a:0L, b:0LL, c:0L,d:0.0})
  or
a = assoc(1, {a:0L, b:0LL, c:0L,d:0.0},/packed)
help, a[0]

cheers,

paulv


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Nel  
View profile  
 More options Feb 10, 10:12 am
Newsgroups: comp.lang.idl-pvwave
From: Bill Nel <ri...@crd.ge.com>
Date: Fri, 10 Feb 2012 07:12:35 -0800 (PST)
Local: Fri, Feb 10 2012 10:12 am
Subject: Re: read a C written binary file with IDL
On Feb 9, 11:56 pm, Manodeep Sinha <manod...@gmail.com> wrote:

> Hi,

> This is because C pads the structure to produce alignments. Under
> 'normal' operations, you would expect MyStruct to be 20 bytes,
> however, if you do a sizeof(struct MyStruct), you will probably see
> that the size is 24. (And you can enable the warning for gcc by using
> the compile time option -Wpadded).

The N_TAGS() function has LENGTH and DATA_LENGTH keywords:

IDL> print, n_tags(mystruct, /length)
          24
IDL> print, n_tags(mystruct, /data_length)
          20

I mention it because one might not think to look at the N_TAGS
function
for this sort of information.

--Wayne


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul van Delst  
View profile  
 More options Feb 10, 11:02 am
Newsgroups: comp.lang.idl-pvwave
From: Paul van Delst <paul.vande...@noaa.gov>
Date: Fri, 10 Feb 2012 11:02:07 -0500
Local: Fri, Feb 10 2012 11:02 am
Subject: Re: read a C written binary file with IDL
Cool! I did not know that.

cheers,

paulv


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bing999  
View profile  
 More options Feb 20, 8:49 pm
Newsgroups: comp.lang.idl-pvwave
From: bing999 <thibaultga...@gmail.com>
Date: Mon, 20 Feb 2012 17:49:32 -0800 (PST)
Local: Mon, Feb 20 2012 8:49 pm
Subject: Re: read a C written binary file with IDL
Hi,

sorry to reply so late, but I was away recently.

First, as the issue seems to be more complicated than i first thought,
i have to say that the structure i have to read is bigger than what I
wrote previously just to make an example.

It actually contains (in the right order):
int x 1
long long x 1
int x 5
float x 9
int x 1
float x 24

that is, 7 int, 33 float and 1 long long (written in C).

When I look at the IDL commands you suggest, i get:

  print,n_tags(mystruct,/length) 176

  print,n_tags(mystruct,/data_length) 172

So you were right.

Now, the question is "How to handle that?? " :)

I inserted an extra int (0L) in the structure of my IDL reading
routine in second position of the structure (just before the long
long), and it gives results coherent with what one gets with a correct
Python routine. However, I am not quite satisfied with this
solution...

Does anyone can think of a better way to proceed?

Thanks !!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Manodeep Sinha  
View profile  
 More options Feb 20, 10:43 pm
Newsgroups: comp.lang.idl-pvwave
From: Manodeep Sinha <manod...@gmail.com>
Date: Mon, 20 Feb 2012 19:43:50 -0800 (PST)
Local: Mon, Feb 20 2012 10:43 pm
Subject: Re: read a C written binary file with IDL
On Feb 20, 7:49 pm, bing999 <thibaultga...@gmail.com> wrote:

As Paul suggested, you should check out the assoc function. I can read
the data in just fine through IDL:

---------------
openu, 1, 'data.bin', /SWAP_IF_BIG_ENDIAN
a = assoc(1, {a:0L, b:0LL, c:lonarr(5),d:fltarr(9),e:0L,f:fltarr(24)})
print,a[0]
--------------

Cheers,
Manodeep


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bing999  
View profile  
 More options Feb 22, 12:45 am
Newsgroups: comp.lang.idl-pvwave
From: bing999 <thibaultga...@gmail.com>
Date: Tue, 21 Feb 2012 21:45:49 -0800 (PST)
Local: Wed, Feb 22 2012 12:45 am
Subject: Re: read a C written binary file with IDL
Hey,

Actually I cannot use assoc i guess. To be fully complete this
time :), the binary file contain a header (some int) + the structure
mystruct which is replicated N times. From what I understand of the
assoc function, it won't work in my case...
And the problem is that i cannot changer the compiling options of the
C code...

cheers
Thibault


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Fanning  
View profile  
 More options Feb 22, 8:24 am
Newsgroups: comp.lang.idl-pvwave
From: David Fanning <n...@idlcoyote.com>
Date: Wed, 22 Feb 2012 06:24:07 -0700
Local: Wed, Feb 22 2012 8:24 am
Subject: Re: read a C written binary file with IDL

bing999 writes:
> Actually I cannot use assoc i guess. To be fully complete this
> time :), the binary file contain a header (some int) + the structure
> mystruct which is replicated N times. From what I understand of the
> assoc function, it won't work in my case...

I think your understanding must be faulty, as this
is *exactly* the kind of file ASSOC works best with. :-)

The "offset" is used to account for the header.

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bing999  
View profile  
 More options Mar 11, 10:49 pm
Newsgroups: comp.lang.idl-pvwave
From: bing999 <thibaultga...@gmail.com>
Date: Sun, 11 Mar 2012 19:49:10 -0700 (PDT)
Local: Sun, Mar 11 2012 10:49 pm
Subject: Re: read a C written binary file with IDL
Hi all,
thanks for your help. Indeed, the assoc function works fine (without
using the /packed keyword).
Cheers
Bing


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »