Kine
Kine

Reputation:

New to Fortran, questions about writing to file

I am completely new to Fortran and pretty new to programming in general. I am trying to compile a script someone else has written. This is giving me a few problems. The top half of the code is:

C 
C Open direct-access output file ('JPLEPH')
C 
OPEN ( UNIT = 12, 
. FILE = 'JPLEPH', 
. ACCESS = 'DIRECT', 
. FORM = 'UNFORMATTED', 
. RECL = IRECSZ, 
. STATUS = 'NEW' )


C
C Read and write the ephemeris data records (GROUP 1070). 
C 
CALL NXTGRP ( HEADER ) 

IF ( HEADER .NE. 'GROUP 1070' ) CALL ERRPRT(1070,'NOT HEADER') 

NROUT = 0 
IN = 0 
OUT = 0 


1 READ(*,'(2i6)')NRW,NCOEFF
if(NRW .EQ. 0) GO TO 1
READ (*,'(3D26.18)',IOSTAT =IN) (DB(K),K=1,NCOEFF) 


DO WHILE ( ( IN .EQ. 0 ) 
. .AND. ( DB(2) .LT. T2) ) 

IF ( 2*NCOEFF .NE. KSIZE ) THEN 
CALL ERRPRT(NCOEFF,' 2*NCOEFF not equal to KSIZE') 
ENDIF 

C
C Skip this data block if the end of the interval is less 
C than the specified start time or if the it does not begin 
C where the previous block ended. 
C 
IF ( (DB(2) .GE. T1) .AND. (DB(1) .GE. DB2Z) ) THEN 

IF ( FIRST ) THEN 
C 
C Don't worry about the intervals overlapping 
C or abutting if this is the first applicable 
C interval. 
C 
DB2Z = DB(1) 
FIRST = .FALSE. 
ENDIF 

IF (DB(1) .NE. DB2Z ) THEN 
C 
C Beginning of current interval is past the end 
C of the previous one.

CALL ERRPRT (NRW, 'Records do not overlap or abut') 
ENDIF 

DB2Z = DB(2) 
NROUT = NROUT + 1

print*,'Out =', OUT

WRITE (12,REC=NROUT+2,IOSTAT=OUT) (DB(K),K=1,NCOEFF)

print*,'Out2 =', OUT


IF ( OUT .NE. 0 ) THEN 
CALL ERRPRT (NROUT, 
. 'th record not written because of error') 
ENDIF 

So, when I print "Out" and "Out2" to the screen I find that Out=0 and Out2=110. As it is not longer equal to zero, the program gives me an error. Therefore I am basically wondering about what is happening here:

WRITE (12,REC=NROUT+2,IOSTAT=OUT) (DB(K),K=1,NCOEFF)

I assume that 12 refers to the file I have opened (and created), and want to write to. What does the rest of the first brackets do? And what is the point of the second? Does that gives me the information I want to put in my file? In case, where does DB get filled with that?

Generally I am wondering what is going wrong? Why does OUT change value? (I need t

NCOEFF is defined as an Integer in the beginning of the programme, and DB: as DOUBLE PRECISION DB(3000), DB2Z/0.d0/ , so I assume DB is an array of some sort.

Upvotes: 1

Views: 392

Answers (4)

John R. Strohm
John R. Strohm

Reputation: 7667

The statement

WRITE (12,REC=NROUT+2,IOSTAT=OUT) (DB(K),K=1,NCOEFF)

is, if memory serves me, called an "implied DO-loop". As written, it will write NCOEFF values from array DB, starting at DB(1).

It is called an implied DO-loop because the explicit form would be (in FORTRAN IV, for the ancients: I know it a lot better than the more modern variations) something along the lines of:

      DO 10 K=1,NCOEFF
      WRITE (12,REC=NROUT+2,IOSTAT=OUT) DB(K)
10    CONTINUE

(Pretend that the first two lines are indented six columns.) This is a DO-loop. The implied DO-loop form lets you put the "loop" right in the input/output statement.

What makes it useful is that you can have multiple arrays, and multiple loops. For a simple example:

WRITE (12,REC=NROUT+2,IOSTAT=OUT) (DB(K), DC(K), K=1,NCOEFF)

Upvotes: 1

TMN
TMN

Reputation: 3070

110 is the error code thrown by the WRITE call. You need to check your FORTRAN RTL (run-time library) reference. It should list the possible error codes. I think 110 means that you're trying to convert a double-precision value to an integer, but the value is bigger than you can store in an integer. Maybe dump the values in DB and see.

Upvotes: 0

High Performance Mark
High Performance Mark

Reputation: 78316

To quote the handbook REC indicates the record number to be read or written. As advised, see the documentation which accompanies your compiler for further explanation.

(DB(K),K=1,NCOEFF) means 'all the elements in DB from 1 to NCOEFF. You are looking at an io-implied-do statement.

Upvotes: 2

SethMMorton
SethMMorton

Reputation: 48725

In this program, OUT is telling if the write statement was successful or not. (the IOSTAT parameter to the write statement means "I/O status", or input/output status). It returns 0 if the I/O operation was a success, or the number of the error code otherwise. You can find what the error codes mean here.

I'm not familiar with the REC parameter, but a starting place to investigate yourself can be found here.

Upvotes: 2

Related Questions