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

  • Thread starter Thread starter camila
  • Start date Start date
  • Tags Tags
    Array Fortran
AI Thread Summary
The discussion revolves around handling precipitation data in Fortran, specifically addressing how to convert strings representing precipitation values into floating-point numbers while managing blank spaces. The user is working with data formatted as strings, which can include valid numbers, zeros, or blank spaces. The challenge is that blank spaces should be replaced with a specific placeholder value (-999999) instead of being skipped or converted to zero.The proposed solution involves using an `if` statement to check for blank strings. If a string is blank, it assigns the placeholder value to the corresponding floating-point array. If the string contains a valid number, it reads and converts it to a float. The provided code snippet demonstrates this approach effectively, ensuring that all data points are accounted for, including those that were initially blank. The user expresses gratitude for the assistance received in resolving the issue.
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!
 
Technology news 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 :)
 

Similar threads

Replies
12
Views
3K
Replies
6
Views
2K
Replies
13
Views
4K
Replies
13
Views
2K
Replies
16
Views
2K
Replies
4
Views
8K
Back
Top