Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

reading from a binary file single precision to double precision

diedro
Beginner
624 Views

dear all,

I have the following problem. I Have to read a binary file written in single precision:

  OPEN(1,file=input_rain_file,STATUS='old', form='BINARY')

 !TEMP A SINGLE PRECISION VARIABLE

  DO i=1,nmacro
     READ(1) TEMP(:,i)       ! in [mm], single precision
  END DO

and then I apply

DOUBLEP = TEMP

I do not know if this is formally correct and if I can directly read the single precision file using a variable in double precision.

Thanks in advance for any help

Diego 

0 Kudos
3 Replies
mecej4
Honored Contributor III
624 Views

I do not know ... if I can directly read the single precision file using a variable in double precision.

The number of bytes in the record being read is equal to the byte-size of the I/O list. If you write a number of REALS and then read them as DOUBLE, chaos will result, because record length bytes and enough bytes from the next input record will be read in order to fulfill the READ request.

0 Kudos
diedro
Beginner
624 Views

Dear all,

thanks. There is a smart way to overcome this?

For example, imposing a format in the reading statement?

Thanks again

Diego

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
624 Views

You can conserve memory by reading one row at a time:

 OPEN(1,file=input_rain_file,STATUS='old', form='BINARY')

! TEMP A SINGLE PRECISION VARIABLE
! REAL(4) :: TEMP(nCols)
REAL(8) :: DOUBLEP(nCols,nMarco)
...
 DO i=1,nmacro
    READ(1) TEMP(:)       ! one row in [mm], single precision
    DOUBLEP(:,i) = TEMP
 END DO

If memory is not an issue, you can (may be able to) read the whole array as sp in one READ, then convert as done before.

Jim Dempsey

0 Kudos
Reply