2
votes

Running Fortran 90 on AIX. Trying to compile and run this code and I'm getting an error that says:

The unformatted I/O statement on unit 200 cannot be completed because the unit is connected to the formatted file /filepath/TB_20160610023926_bufr_v620.dat.  
The program will recover by ignoring the I/O statement.

I'm not entirely sure what this error means. I only open the file once and the unit number is not connected to any other file.

INTEGER,PARAMETER :: GRID_TB_UNIT = 200
tbhmap=0

CALL SYSTEM('mkdir -p ' // TRIM(data_path) // '/' // &
                         TRIM(TB_PREFIX)   // '/' // &
                         date_str)

OPEN(UNIT   = GRID_TB_UNIT,             &
   file   = TRIM(TB_binary_filename),   &
   STATUS = 'REPLACE',                  &      
   ACCESS = 'SEQUENTIAL',               &
   FORM   = 'FORMATTED')                        

nrecg = 0
DO rr=1,720
 DO cc=1,1440

    IF(countgrid(cc,rr) < 1)CYCLE

    nrecg = nrecg+1

    tbhmap(cc,rr) = tbhgrid(cc,rr)/countgrid(cc,rr)
    timemap(cc,rr) = timegrid(cc,rr)/countgrid(cc,rr)

    GRID_TB_record%row = rr
    GRID_TB_record%col = cc
    GRID_TB_record%year = yeargrid(cc,rr)
    GRID_TB_record%month = monthgrid(cc,rr)
    GRID_TB_record%time = timemap(cc,rr)
    GRID_TB_record%tbh425 = tbhmap(cc,rr)

    WRITE(GRID_TB_UNIT,REC=nrecg)GRID_TB_record 

 END DO
END DO

CLOSE(GRID_TB_UNIT)
1
@HighPerformanceMark This resulted in another error: The REC= specifier must not appear in a list-directed or namelist data transfer statement. - klex52s

1 Answers

3
votes

The FORM of a connection to a file in Fortran is either "FORMATTED" (meant to be human readable) or "UNFORMATTED" (not human readable). The permitted syntax of some of the input/output statements differs depending on the file - for example FORMATTED input/output statements will always have a format specifier of some sort, UNFORMATTED input/output statements must not have such a specifier.

You have opened the connect to the file as FORMATTED, but then the problematic WRITE statement does not have the necessary format specifier - i.e. the syntax of the WRITE statement is that for an UNFORMATTED connection. Without a format specifier the compiler does not know how to render the values of the items in the output list for the WRITE statement in human readable form.

If you want the file to be FORMATTED - human readable - then add an appropriate format specifier to the WRITE statement (a format specifier of *, as suggested in the comments, specifies that a processor dependent format based on the type of the arguments in the output list be used). If you don't want the file to be human readable, then change the FORM in the OPEN statement to UNFORMATTED.

~~~

Similarly to the inconsistency between the FORM of a connection and the syntax of the input/output statements, there is also an inconsistency between the ACCESS of the connection and the statements. The ACCESS refers to the nature of the records in the file (and how those records are then "accessed"): "SEQUENTIAL" - the file contains varying length records that may be accessed in sequence, "DIRECT" - the file contains fixed length records that may be accessed in any order, and "STREAM" - the file can principally be treated as a stream of file storage units (a.k.a bytes) (FORMATTED STREAM files can have a record structure too based on the newline character in the stream).

In the example, the ACCESS for the connection to the file is explicitly nominated as SEQUENTIAL. However, the appearance of the REC specifier in the WRITE statement ("write the information to this particular record") is consistent with a DIRECT ACCESS connection. This mismatch is not permitted in a standard conforming program.