Error in calculating the average from Inmet data using a Fortran program

Asked

Viewed 43 times

4

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

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 below (delimited by commas), 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 exit (in txt) is coming out like this:

A média de temperaturas é  0.00

From now on I thank anyone who can enlighten me

1 answer

1

One of the problems is in charge read(10,*), for the * indicates that you are reading from directed to list (list-directed), and this kind of reading is not ideal for question data due to different data types (string and numbers).

Another problem is that you’re trying to read the date (like string) to the first position of the array dados, who’s a guy real (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, at the helm read, indicate formatting according to file data:

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 as stated in 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 reported in the question):

A média de temperaturas é 28.31

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.