{FORTRAN}How can i read blank steps without using iostat

  • Context: Fortran 
  • Thread starter Thread starter camila
  • Start date Start date
  • Tags Tags
    Array Fortran
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
camila
Messages
4
Reaction score
0
hello,
I have a tricky question with arrays involved. I am trying to made a code who can read precipitation, so i have 3 kinds of strings: 1.numbers, 2.0 ,3. blank steps. I need to change the blanks into the smallest number who can read fortran. I was reading and the other options is using iostat, but this function change the blank space into 1 or zero, and i can't have a zero in a blank space.
Data=45 years, with monthly precipitation.
The data is string like this: ':35.5' or ':0' or ' '
(im using the data from excel, i changed with .csv, I am using this code to change the strings into a float type, but the blank spaces doesn't list in the final result, and the final result its less data).
Fortran:
!change strings into a float type
character*50, dimension(12,45)::a
character(len=8), dimension(12,45)::net_pr
double precision, dimension (12,45)::d

do  i=1,45
do j=1,12

read (net_pr(x,y),5000)a(x,y)
read(a(x,y),*)d(x,y)

end do
end do

5000 format(x,a8) !i made this to erase :

Thanks!
 
on Phys.org
Let me see if I understand. If net_pr = ':35.5', then a = '35.5' and d = 35.5. If net_pr = ':0', then a = '0' and d = 0. But if net_pr = ' ', what should d equal?
 
yes!, with the last one, the program skip and doesn't read anything.
e.g: if i have:
:26
:35.5
:0
:
:46

this is the result:
26.00000
35.50000
0.000000
46.00000

and with this result, i have less data, i need this:
26.00000
35.00000
-9384939 (the smallest number)
46.00000

thank u!
 
Then you need to add an if:
Fortran:
if (a(i,j) == '') then
     d(i,j) = -999999
else
     read(a(i,j),*) d(i,j)
end if
or something similar. In particular, I'm not sure if the right-hand-side of the if should be '' or ' '.
 
yes.. i tried that, but its doesn't work :P
 
The following code seems to do what you want:
Fortran:
!change strings into a float type
  character*50, dimension(5) :: a
  character(len=8), dimension(5) :: net_pr
  double precision, dimension (5) :: d

  net_pr(1) = ":26"
  net_pr(2) = ":35.5"
  net_pr(3) = ":0"
  net_pr(4) = ": "
  net_pr(5) = ":46"

  do  i=1,5
     a(i) = net_pr(i)(2:8) ! remove leading colon
     if (len_trim(a(i)) == 0) then
        ! absence of data
        d(i) = -999999
     else
       ! convert to real
        read(a(i),*) d(i)
     end if
   
     write(*,*) d(i)
  end do
 
THANKSSSSSSSSSSSSSSSS! :D
i really appreciate your time :)