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

In summary, the code converts the strings to floats and prints the smallest number that is equal to each one.
  • #1
camila
4
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
  • #2
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?
 
  • #3
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!
 
  • #4
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 ' '.
 
  • #5
yes.. i tried that, but its doesn't work :P
 
  • #6
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
 
  • #7
THANKSSSSSSSSSSSSSSSS! :D
i really appreciate your time :)
 

1. What is FORTRAN?

FORTRAN (short for Formula Translation) is a high-level programming language designed for scientific and engineering applications. It was first developed in the 1950s and is still widely used today.

2. Why is FORTRAN still used in scientific research?

FORTRAN is still used in scientific research because it has a long history in the field and is well-suited for mathematical and scientific calculations. It also has a large library of built-in functions and is highly optimized for numerical computations.

3. Can FORTRAN be used for modern programming?

Yes, FORTRAN can be used for modern programming. While it may not have all of the features of more modern languages, it is constantly being updated and has the ability to integrate with other programming languages.

4. Is FORTRAN difficult to learn?

FORTRAN can be a bit more challenging to learn compared to more modern languages, but it has a relatively simple syntax and is well-documented. It also has a strong community of users who are willing to help and provide resources for learning.

5. How can I read blank steps without using iostat?

The best way to read blank steps in FORTRAN without using iostat is by using the "read" statement with a format specifier that includes a blank field. For example, "read(unit, '(A)') string" will read in a blank line and store it in the variable "string".

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
33
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
4
Views
8K
Back
Top