Domanda

I have an large txt-file which contains measurements an the longitude/latitude of different stations, like:

 1  1 bwb02 52.38783333 13.40266667 LR 0.2
 2  2 bwb04 52.53716667 13.22583333 P1 0
 3  3 bwb05 52.55516667 13.202 SP 0
 4  4 bwb07 52.48433333 13.4385 N1 0

I just want to read the 4,5 and 7 column and put them into a matrix, like:

  do j = 1,n
   read(12,200) latB,lonB,r
   200 Format (12x,f11.9,1x,f11.9,4x,f5.3)
   beo_data(j,1) = j
   beo_data(j,2) = lonB
   beo_data(j,3) = latB
   beo_data(j,4) = r
  end do

But my format isn't working and i get an error: Fortran runtime error: Bad value during floating point read

The problem seems to be that the format of the longitude and latitude is changing. Could you tell me how i can read and work with such a file?

È stato utile?

Soluzione

Just read each line as a string using a descriptor and then you can read your data using a list-directed read (* instead of the format). Or you can use the list directed read directly, if there is no danger of reading from two lines at once.

integer :: tmp1,tmp2
character(5) :: tmp3
character(2) :: tmp4
real :: values(3)
  open(12,file="data.txt")
  do j = 1,4
   read(12,*) tmp1, tmp2, tmp3, values(1), values(2), tmp4, values(3)
   print *,values
  end do
  close(12)
end

prints

   52.3878326       13.4026670      0.200000003    
   52.5371666       13.2258329       0.00000000    
   52.5551682       13.2019997       0.00000000    
   52.4843330       13.4385004       0.00000000

Using the fixed column format is useful only when the columns are really fixed.

You can also just use single character variable tmp instead of all those different ones.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top