Silverfrost Forums

Welcome to our forums

Fortran error(variable or array not declarated)

19 Jun 2013 1:39 #12437

Hi everyone,

I am trying to write this program. However, the program says that i have a mistake in my line 13(e(I)=y(I)*0.5144). Thanks in advanced

IMPLICIT NONE INTEGER I DOUBLE PRECISION e(8760),x(8760),y(8760),z(8760) character*1000 C(8760),D(8760)

  OPEN (UNIT=23,FILE='C:\\Users\\Estanislao\\Desktop\\bristol chanel\\wind data\\wind4.txt',STATUS='NEW')
  OPEN (UNIT=22,FILE='C:\\Users\\Estanislao\\Desktop\\bristol chanel\\wind data\\wind.txt',STATUS='OLD',ACTION='READ')

  DO I=1,8760
  READ (22,*) C(I),D(I),x(I),y(I),z(I)      
  e(I)=y(I)*0.5144   
  write(23,10)e(I),z(I)
  ENDDO

10 FORMAT(2f10.4)
close(22) close(23)

19 Jun 2013 3:13 #12438

It compiles OK for me. What is the name of the file? Are you using fixed or free format Fortran?

20 Jun 2013 12:57 #12440

Is this error being reported when compiling the program or when you run it?

Your free format read statement looks to be a bit unusual, as

READ (22,*) C(I),D(I),x(I),y(I),z(I)

C(I) expects 1,000 characters D(I) expects 1,000 characters x(I),y(I),z(I) are three numbers after 2,000 characters of text !!

With free format, this information can be on multiple lines (records in the data file), although I am not sure how reading two x 1,000 characters might work if there is a line of only say 50 characters in the record. It might be that the following could work, by using a .csv style format.

'first comment field','second comment field',1.0,2.0,3.0

I'd be interested to see how you go, relating the file data format to the read statement.

My approach would be to read all the information into a single character buffer and then extract the data elements, assuming a .csv format, as excel or internet banking can export. This ignores the possibility that the information might optionally be in multiple 'lines' of the data file.

Then again, the free format READ (22,*) might just work. I've never tried this with large character fields.

As this is your first post, this might be a bit of a challenge

John

PS: as a suplimentary question: Do you have any control of the format of the file you are reading ?
Putting 3 numbers after 2,000 characters of comment is a not what I would define. I would have a record as: x(I),y(I),z(I), C(I),D(I) I would prefer to read it as a .csv format and then test ways to extract the comments C and D, as I don't know that read (22,*) is sufficiently flexible to do this.

20 Jun 2013 1:50 #12441

I was puzzled by how read (..,*) would read character strings. It appears that it is fairly (very) flexible: commas can seperate numbers or strings blanks can also be used as a delimiter blanks are a delimiter for strings. strings can be enclosed in quotes, such as 'this is a single string' records can span multiple lines.

The following program and sample data file demonstrate these

IMPLICIT NONE 
IMPLICIT NONE 
DOUBLE PRECISION e(8760),x(8760),y(8760),z(8760) 
character*1000 C(8760),D(8760) 
character in_file*100, out_file*100

in_file  = 'C:\\Users\\Estanislao\\Desktop\\bristol chanel\\wind data\\wind.txt'
out_file = 'C:\\Users\\Estanislao\\Desktop\\bristol chanel\\wind data\\wind4.txt'

in_file  = 'sample.dat'
out_file = 'sample.new'

OPEN (UNIT=22, FILE=in_file,  STATUS='OLD',  ACTION='READ') 
OPEN (UNIT=23, FILE=out_file, STATUS='UNKNOWN') 
!
DO I=1,8760 
!z  READ (22,*) C(I),D(I),x(I),y(I),z(I) 
   READ  (22,*,iostat=iostat) x(I),y(I),z(I), C(I),D(I)
   write (*,*) i, iostat
   if (iostat/=0) exit
!
   write (*,fmt='(3f10.3,' |',a15,'|',a)') x(I),y(I),z(I), trim(C(I)), trim(D(I))
!
   e(I)=y(I)*0.5144 
   write (23,10) e(I),z(I) 
END DO 
10 FORMAT(2f10.4) 
!
close(22) 
close(23)
!
end
<sample.dat>
1,22,33,'aaaa','bbbb'
2,22,33,'aaaa','bbbb'
3,22,33,'aaaa  bbbb ccc ','eeee fff ggg '
4,22,33,'aaaa  bbbb ccc ','eeee fff ggg '
5,22,33,'aaaa  bbbb ccc ','eeee fff ggg '
6,2,3,this is a text string outside quotes blanks appear to be a text delimiter
7,2,3,'this is a text string inside quotes', blanks still appear to be a text delimiter
8,33,4.5,'first string','this is a good format as second string reads ok'
9 22 33 'first string' 'this record uses blanks'
10   12   15    'extra spaces'     'this still works'
11 12
 13
'next line' 'an example of info over three lines'
12,  22,  33,   'leading blanks between the comma field ',  'multiple blanks are a single delimiter '
13  ,  22  ,  33   ,  ' extra blanks ' ,   'an example with blanks between the comma '
14, 1, 1, 'first comment' 'test of basic .csv data format'
15, 1, 1, 'last line' 'test of basic data format'

Based on this your original read order could work, if the two text strings are like ' text 1' 'text 2' 11 22 33

I've learnt something today.

Please login to reply.