Caserio
Caserio

Reputation: 492

Read error when executing Fortran code that was provided with a data file

I need to open a binary file for which the developer of the data provides a program written in Fortran. I am new to the Fortran language, but I think it is worth trying out to use the ready program rather than writing a new one myself on R.

The file I want to open is this, this is the data manual, and the lines of code to open the data can be found here. The data contain a 576-byte header line at the start which can be opened using this.

What I did: Saved the binary file in a new folder, in which I have also saved the code of the software that opens it. After setting the terminal into the latter folder (cd /home/urs/../), from the GNU-Linux terminal I run:

# compile the program using gfortran
  gfortran read_v2.2_month.f -o read_v2.2_month
# make file executable
chmod +x read_v2.2_month
# run the program
./read_v2.2_month

However, I get the following error:

 Error: read error        5016  on file gpcp_v2.2_psg.1987

EDIT: The code in Fortran to open the data excluding the first row of header is the following:

     OPEN  ( UNIT=10, FILE='gpcp_v2.2_psg.1987', ACCESS='DIRECT', 
     +          FORM='UNFORMATTED', STATUS='OLD', RECL=144, 
     +      IOSTAT=iret )
        IF  ( iret .NE. 0 )  THEN 
            WRITE (*, *) 'Error: open error', iret, 
     +                   ' on file gpcp_v2.2_psg.1987'
            STOP
        END IF
C
C       Compute the number of records to skip, namely 1 for the header 
C       and 72 for each intervening month.
C
        nskip = 1 + ( month - 1 ) * 72
C
C       Read the 72 rows of data and close the file.
C
        DO  10 j = 1, 72
            READ ( UNIT=10, REC=j+nskip, IOSTAT=iret )  
     +           ( data (i, j), i = 1, 144 )
            IF  ( iret .NE. 0 )  THEN 
                WRITE (*, *) 'Error: read error', iret, 
     +                       ' on file gpcp_v2.2_psg.1987'
                STOP
            END IF
   10   END DO
        CLOSE ( UNIT=10 )
C
C       Now array "data" is ready to be manipulated, printed, etc.
C       For example, dump the single month as unformatted direct:
C
        OPEN  ( UNIT=10, FILE='junk', ACCESS='DIRECT', 
     +          FORM='UNFORMATTED', RECL=144, IOSTAT=iret )
        IF  ( iret .NE. 0 )  THEN 
            WRITE (*, *) 'Error: open error', iret, 
     +                   ' on file junk'
            STOP
        END IF
        DO  20 j = 1, 72
            WRITE ( UNIT=10, REC=j, IOSTAT=iret )  
     +            ( data (i, j), i = 1, 144 )
            IF  ( iret .NE. 0 )  THEN 
                WRITE (*, *) 'Error: write error', iret, 
     +                       ' on file junk'
                STOP
            END IF
   20   END DO
        CLOSE ( UNIT=10 )
        STOP
        END

Upvotes: 0

Views: 547

Answers (1)

There are some things that could go wrong in this code. For exaple, the RECL is in gfortran counted in bytes, but the code assumes 4-byte words (as Intel does). The RECL values should not be indicated explicitly.

Try to increase RECL to 4*144. If it helps, then this question is a duplicate of Reading writing fortran direct access unformatted files with different compilers

Upvotes: 1

Related Questions