{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
Click For Summary

Discussion Overview

The discussion revolves around reading precipitation data from strings in FORTRAN, specifically addressing how to handle blank entries without using the IOSTAT function. Participants explore methods to convert blank strings into a specific numerical value while ensuring that valid data is correctly processed.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes the need to convert precipitation data strings, which may include numbers, zeros, and blank spaces, into a numerical format without losing data.
  • Another participant seeks clarification on what value should be assigned to the variable when encountering a blank string.
  • A participant provides an example of input strings and the resulting output, highlighting the issue of missing data when blanks are not handled correctly.
  • One suggestion involves using an if-statement to check for blank strings and assign a specific value (-999999) to the corresponding numerical variable.
  • Another participant reports that their attempt to implement this solution did not work as expected.
  • A later reply presents a code snippet that successfully handles the conversion, including the treatment of blank entries, and demonstrates its functionality with sample data.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of handling blank strings, with some solutions proposed but not universally accepted as effective. The discussion remains unresolved regarding the best approach to take.

Contextual Notes

There are limitations regarding the handling of string lengths and the specific conditions under which data is read, which may affect the implementation of proposed solutions.

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 20 ·
Replies
20
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
7
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K