Re: reading unformatted data into a structure [message #29749 is a reply to message #29744] |
Mon, 18 March 2002 18:52  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Rick Towler (rtowler@u.washington.edu) writes:
> I have an unformatted data file which I am trying to read and I am having
> only limited success. The data file was created on a Sun machine and it is
> being read on a win32 machine (I have set the swap_if_little_endian keyword
> on the open statement). As for the file format, all I have to go on is the
> C header file that describes the records in the file.
>
> Early on in the header I find this:
>
> // Variable types:
> // int is 32 bit integer.
> // short is 16 bit integer.
>
>
> Here are the first few lines of the header (all of the data types found in
> the header are in these lines):
>
> typedef struct
> {
> int fileFormatVersion; /* The file format version number. */
> int points; /* Number of xyz points on the file. */
> int time; /* UNIX time of file generation,
> seconds since 1/1 1970. */
> unsigned short coordSys; /* The coordinate system used:
> 0 = Geographical lat/long.
> 1 = Projection */
> char projection[32]; /* Projection name (see above) */
> int projParam1; /* 1. projection parameter (see above). */
> int projParam2; /* 2. projection parameter (see above). */
> int projParam3; /* 3. projection parameter (see above). */
> int projParam4; /* 4. projection parameter (see above). */
> int projParam5; /* 5. projection parameter (see above). */
> char datum[32]; /* Name of the datum (see above). */
> char ellLargeHalfAxis[16]; /* Large half axis of the ellipsoide. */
> ....
>
>
> From the comments about variable types and the structure definition I
> created an IDL structure that I think matches the C struct (again, I'll list
> the first few lines):
>
> header = { fileFormatVersion:0L, $
> points:0L, $
> time:0L, $
> coordSys:0, $
> projection:bytarr(32), $
> projParam1:0L, $
> projParam2:0L, $
> projParam3:0L, $
> projParam4:0L, $
> projParam5:0L, $
> datum:bytarr(32), $
> ellLargeHalfAxis:bytarr(16), $
> ....
>
>
> I open the data file and read the header like so:
>
> openr, lun, 'J:\hydrographic\1.xyz', /get_lun, $
> /swap_if_little_endian
> readu, lun, header
>
>
>
> Am I doing this correctly? I know I am close because I get what seems like
> a valid time and I know I get a valid datum. But my data doesn't seem
> right. I just want to check that I am using the correct types in my IDL
> struct and that I am reading in the data correctly.
It certainly appears correct to me, at first glance,
although it is possible to run into a lot of weird
things with data. It always helps to have a good
example of what the numbers are *suppose* to be.
That's about the only way that you can find out
the documentation forgot to mention that those
integers were *unsigned* integers, for example. :-)
> Also, I know I have seen this but how do I convert the decimal ASCII chars
> in my bytarr's to a string?
Use the STRING function:
Print, String(header.projection)
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|