Reading in a binary file in IDL - based on fortran reader? [message #88897] |
Wed, 02 July 2014 04:55  |
rjp23
Messages: 97 Registered: June 2010
|
Member |
|
|
I have (quite a complicated) binary file that I need to read in.
I have a fortran reader for it but would like to re-write this in IDL.
I assume for most of the variables I can just use the corresponding one in IDL but how would I define a string of a specific size in IDL?
The data needs to be:
CHARACTER(len=16) :: description
Can I just use the string format code to define a 16 character string like
a=string('', format='(A16)')
Or do I need to do something differently?
Cheers
|
|
|
|
Re: Reading in a binary file in IDL - based on fortran reader? [message #88899 is a reply to message #88897] |
Wed, 02 July 2014 06:09   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
On 07/02/14 07:55, rjp23@le.ac.uk wrote:
> I have (quite a complicated) binary file that I need to read in.
>
> I have a fortran reader for it but would like to re-write this in IDL.
>
> I assume for most of the variables I can just use the corresponding one in IDL but how would I define a string of a specific size in IDL?
>
> The data needs to be:
>
> CHARACTER(len=16) :: description
>
> Can I just use the string format code to define a 16 character string like
>
> a=string('', format='(A16)')
>
> Or do I need to do something differently?
I use BYTARR for this.
For your example, assuming the file has already been opened
appropriately, I would do something like:
strlen = 16 ; Define the string length
a = bytarr(strlen) ; Create a temporary byte array
readu, fid, a ; Read the string into the byte array
description = string(a) ; Convert the bytes to an actual string
cheers,
paulv
p.s. Here's an example from one of my Fortran-file readers:
; Read the sensor info
sensor_id = BYTARR(SENSOR_ID_STRLEN)
wmo_satellite_id = 0L
wmo_sensor_id = 0L
sensor_channel = 0L
READU, fid, $
sensor_id , $
wmo_satellite_id, $
wmo_sensor_id , $
sensor_channel
self->Set_Property, $
Debug = debug, $
Sensor_Id = STRING(sensor_id), $
WMO_Satellite_Id = wmo_satellite_id , $
WMO_Sensor_Id = wmo_sensor_id , $
Sensor_Channel = sensor_channel
; Read the algorithm name
algorithm = BYTARR(ALGORITHM_STRLEN)
READU, fid, algorithm
self->Set_Property, $
Debug = debug, $
Algorithm = STRING(algorithm)
|
|
|
Re: Reading in a binary file in IDL - based on fortran reader? [message #88900 is a reply to message #88898] |
Wed, 02 July 2014 06:17   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
On 07/02/14 09:05, rjp23@le.ac.uk wrote:
> I figured out reading the header in and the above seemed to work but I'm not sure how to progress on to a different record.
>
> The fortran code I have is similar to:
>
> READ(10,rec=1) a, b, c, d
> READ(10,rec=5) data
>
> I've been able to read in all the header info from "rec1" but how do I then proceed on to rec5?
Ah. A Fortran direct access file! IDL doesn't really have an equivalent
- a regular OPENR will give you stream file (I think that's the correct
term) just like C (or most anything else) would.
In IDL, to proceed onto record #5 (skipping recs 2-4) you can use the
POINT_LUN procedure. This will position the file pointer wherever you
want it.
Of course, you will have to know what the file record length is and
compute the position accordingly.
Note that the units of POINT_LUN are bytes. Some Fortran compilers
assume words (4-bytes) as the record length unit, so make sure you take
that into account if necessary. (And apologies if you already know this :o)
cheers,
paulv
|
|
|
Re: Reading in a binary file in IDL - based on fortran reader? [message #88901 is a reply to message #88900] |
Wed, 02 July 2014 06:34  |
rjp23
Messages: 97 Registered: June 2010
|
Member |
|
|
On Wednesday, July 2, 2014 2:17:18 PM UTC+1, Paul van Delst wrote:
> On 07/02/14 09:05, rjp23@le.ac.uk wrote:
>
>> I figured out reading the header in and the above seemed to work but I'm not sure how to progress on to a different record.
>
>>
>
>> The fortran code I have is similar to:
>
>>
>
>> READ(10,rec=1) a, b, c, d
>
>> READ(10,rec=5) data
>
>>
>
>> I've been able to read in all the header info from "rec1" but how do I then proceed on to rec5?
>
>
>
> Ah. A Fortran direct access file! IDL doesn't really have an equivalent
>
> - a regular OPENR will give you stream file (I think that's the correct
>
> term) just like C (or most anything else) would.
>
>
>
> In IDL, to proceed onto record #5 (skipping recs 2-4) you can use the
>
> POINT_LUN procedure. This will position the file pointer wherever you
>
> want it.
>
>
>
> Of course, you will have to know what the file record length is and
>
> compute the position accordingly.
>
>
>
> Note that the units of POINT_LUN are bytes. Some Fortran compilers
>
> assume words (4-bytes) as the record length unit, so make sure you take
>
> that into account if necessary. (And apologies if you already know this :o)
>
>
>
> cheers,
>
>
>
> paulv
Thanks, I knew the record length so could skip records by reading in that many bytes but I didn't know how far into the first record I was so didn't know how much to skip.
Using point_lun I managed to solve it and go directly to the start of the 5th record.
Thanks again for your help, you've just saved me several hours of head scratching :-)
|
|
|