PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

Free OpenACC Webinar

Fortran read question (raw binary in input file)

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
WRF sea ice



Joined: 24 May 2013
Posts: 2

PostPosted: Fri May 24, 2013 3:00 pm    Post subject: Fortran read question (raw binary in input file) Reply with quote

Hi I recently received a binary file and am having trouble reading it. I tried both BIG_ENDIAN and LITTLE_ENDIAN. The file consists of a 300 byte character header that I can read, and 136192 bytes that should be an integer*1 field. The file size 136492 is consistent with that.
(should be direct access, unformatted, recl = 136492, I've experimented with different settings) I think I could figure out what was there if I could just see what the actual binary sequence was. The values (according to metadata I was given) should go from 0 to 250 (which makes sense since integer*1 allows 256 possible values). However, I keep reading the file into values from -128 to 127. I suspect this is not a BIG_ENDIAN or LITTLE_ENDIAN field. I would like to be able to see the actual binary values (from 00000000 to 11111111 should be the 1 byte range) to figure out what is there in the input file. How can I do that with a fortran read?

Thanks,

Keith
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6218
Location: The Portland Group Inc.

PostPosted: Fri May 24, 2013 3:37 pm    Post subject: Reply with quote

Hi Keith,

You could probably use STREAM I/O to read in the binary values.

However, it looks to me like the problem is that since Fortran doesn't have unsigned integers, values 0 to 255 would map to -128 to 127 in an Integer*1 data type. What you may need to do is read in the integer*1 value and then convert to an integer. Something like:
Code:
% cat testa.f90
integer :: myval
integer*1 :: int1val

int1val = 250
myval = iand(int(int1val),255)
print *, int1val, myval
end
% pgf90 testa.f90
% a.out
    -6          250


- Mat
Back to top
View user's profile
WRF sea ice



Joined: 24 May 2013
Posts: 2

PostPosted: Fri May 24, 2013 4:47 pm    Post subject: Thanks, Mat Reply with quote

based upon your reply and some digging, I now see that values over 127 were folding over. I needed to create a new integer array (with larger than integer*1). Values below -5 I added 255 and got the field to 0 to 250, as expected, in way that was also physically realistic for the data. So, for instance 0 maps to 0, and -5 maps to 250.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group