Error in averaging from inmet data using a Fortran program

4

I'm starting in Fortran, and I set up the following program to get the 4th column of data from an INMET data file. The calculated mean being 0 (zero). Could someone tell me where the error is in the program?

program inmet

implicit none

integer, parameter    :: M=760, N=18   !Há 760 linhas e 18 colunas
character (4)         :: id            !Diz que o numero da estação é um string, com 4 itens.
real, dimension (M,N) :: dados         !Declaração de matriz
integer               :: i
real                  :: media

open(10,file="inmet.txt",status="old",access="sequential",action="read")
open(20,file="inmet_media_out.txt",status="replace",access="sequential",action="write")

do i = 1,M
   read(10,*) id, dados(i,:)
end do

media = sum(dados (:,4))/real(M)

write(20,'(a,f6.2)') "A média de temperaturas é", media

end program

An example of the start of the INMET txt file follows (comma-delimited), and the first column is the weather station code:

A250,17/07/2018,23,25.4,26.6,25.4,75,85,73,20.6,23.3,20.3,995.5,995.5,994.8,1.2,81,7.1,-3.09,0.0
A250,17/07/2018,22,26.1,27.6,26.1,84,84,72,23.2,23.4,22.1,994.8,994.8,994.7,0.4,103,2.0,51.86,0.0
A250,17/07/2018,21,27.6,30.0,27.6,73,75,63,22.5,23.8,22.1,994.7,995.1,994.5,0.8,85,5.2,648.0,0.0
A250,17/07/2018,20,30.0,30.5,29.9,63,66,62,22.2,23.1,21.9,995.1,995.1,994.9,2.4,107,5.7,1875.,0.0
A250,17/07/2018,19,30.0,30.3,29.7,64,67,62,22.4,23.3,21.9,995.0,995.7,994.9,2.0,108,5.5,2382.,0.0
A250,17/07/2018,18,29.8,30.3,29.6,66,70,63,22.8,23.7,22.0,995.7,996.6,995.7,2.2,103,5.5,2763.,0.0
A250,17/07/2018,17,30.1,30.4,29.3,67,69,63,23.4,23.9,22.1,996.6,997.5,996.6,2.1,104,6.0,3128.,0.0
A250,17/07/2018,16,29.4,29.7,28.9,67,72,65,22.6,23.5,22.3,997.5,998.1,997.4,2.3,109,6.0,3143.,0.0

And my output (in txt) is coming out like this:

A média de temperaturas é  0.00

From now on I thank anyone who can enlighten me

    
asked by anonymous 22.08.2018 / 21:15

1 answer

1

One of the problems is in the read(10,*) command, because * indicates that you are doing a list-directed reading, and this type of reading is not the ideal for question data because of different data types ( string and numbers).

Another problem is that you are trying to read the date (type string ) for the first position of the dados array, which is a real type (incompatible).

One possible (and simple) way to solve these problems is to declare a variable to store the date (type character (len=10) ):

character (len=10)    :: dt          ! a data

and, in the command read , indicate the formatting according to the data of the file:

read(10,'(a4,1x,a10,1x,f6.0,3f6.1,3f6.0,7f6.1,f6.0,3f6.3)') id, dt, dados(i,:)

The changed program looks like this (with the variable M changed to 8, depending on the question):

program inmet

    implicit none

    integer, parameter    :: M=8, N=18   !Há 760 linhas e 18 colunas
    character (len=4)     :: id          !Diz que o numero da estação é um string, com 4 itens.
    character (len=10)    :: dt          !a data
    real, dimension (M,N) :: dados       !Declaração de matriz
    integer               :: i
    real                  :: media

    open(10,file="inmet.txt",status="old",access="sequential",action="read")
    open(20,file="inmet_media_out.txt",status="replace",access="sequential",action="write")

    do i = 1,M
       read(10,'(a4,1x,a10,1x,f6.0,3f6.1,3f6.0,7f6.1,f6.0,3f6.3)') id, dt, dados(i,:)
    end do

    media = sum(dados (:,4))/real(M)

    write(20,'(a,f6.2)') "A média de temperaturas é", media

end program inmet

After execution, the output file inmet_media_out.txt contains the result (for the data entered in the question):

A média de temperaturas é 28.31

Another way, if the formatting of the data is not stable, would read the entire line as a string and then separate the fields ( parser ) with the command index .

    
25.08.2018 / 03:37