Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Mar 11, 2015 #1
    hello,
    I have a tricky question with arrays involved. Im 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 cant 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, im using this code to change the strings into a float type, but the blank spaces doesnt list in the final result, and the final result its less data).


    Code (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!
     
  2. jcsd
  3. Mar 12, 2015 #2

    DrClaude

    User Avatar

    Staff: Mentor

    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?
     
  4. Mar 12, 2015 #3
    yes!, with the last one, the program skip and doesnt 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!!
     
  5. Mar 12, 2015 #4

    DrClaude

    User Avatar

    Staff: Mentor

    Then you need to add an if:
    Code (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 ' '.
     
  6. Mar 12, 2015 #5
    yes.. i tried that, but its doesnt work :P
     
  7. Mar 13, 2015 #6

    DrClaude

    User Avatar

    Staff: Mentor

    The following code seems to do what you want:
    Code (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
     
     
  8. Mar 13, 2015 #7
    THANKSSSSSSSSSSSSSSSS!!!! :D
    i really appreciate your time :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: {FORTRAN}How can i read blank steps without using iostat
  1. (fortran) iostat=stat (Replies: 6)

Loading...