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

Fortran- maximum values

  1. May 12, 2013 #1
    Hello I am currently working on a program that will take the values from a .csv file containing multiple data and analyzing them. The file can be found here: https://docs.google.com/file/d/0BzuDEPd26OcheVhiWlZ3STlZU0k/edit?usp=sharing The data in question starts in row 11 and is in the first 4 columns. In my program I have labeled these: rvar, ivar1, ivar2, ivar3. Rvar is the value corresponding to a time value in microseconds after start of recording whereas ivar1,ivar2,and ivar3 correspond to the value of the acceleration at that point. I have already calculated the average value of each of these columns.

    My question is- how do I find the maximum value for ivar1, ivar2 and ivar3 (separately)?

    My current code is below:
    Code (Text):
    program WSUPhys_csv
      implicit none

      integer :: stat, num_lines, ivar1, ivar2, ivar3
      real :: rvar
      character*80 :: line
     
      write(*,*) "Reading file..."

      ! open input file
      open(10,file='DATA-002.csv',status='old',iostat=stat)
      if (stat .ne. 0) then
        write(*,*) 'File cannot be opened !'
        go to 99  
      end if

      num_lines = 0
      do
        read(10,'(A)',end=99, iostat=stat) line
        if (stat .ne. 0) then
          write(*,*) 'Error reading data !'
          go to 99
        end if
        ! skip comment/header lines beginning with ";"
        if (adjustl(trim(line(1:1))) .eq. ';') then
          cycle
        end if
        ! read string line into numeric variables
        read(line,*) rvar, ivar1, ivar2, ivar3
        num_lines = num_lines + 1    
        if (num_lines <=  10) then
          write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3
        end if
      Sum_ivar1 = Sum_ivar1 + ABS(ivar1)
      Sum_ivar2 = Sum_ivar2 + ABS(ivar2)
      Sum_ivar3 = Sum_ivar3 + ABS(ivar3)
      Max
      end do

      ! close file
      99 continue
      Avg_ivar1 = (Sum_ivar1 / num_lines)
      Avg_ivar2 = (Sum_ivar2 / num_lines)
      Avg_ivar3 = (Sum_ivar3 / num_lines)
      close (10)
      !
      write(*,*) '...done.'
      write(*,*) 'Lines processed: ', num_lines
      write(*,*) 'Average vertical acceleration', Avg_ivar1/1000
      write(*,*) 'Average sideways acceleration', Avg_ivar2/1000
      write(*,*) 'Average forward acceleration', Avg_ivar3/1000
    end program WSUPhys_csv
     
  2. jcsd
  3. May 12, 2013 #2
    The program is named WSUPhys_csv ...is it because you are the same person as WSUPhys from the Tek-Tips Forum? You going around getting other people to do your work? Or are you a class mate and copied all this code from over there?

    Either way, at least give a try, this is easy stuff....you can't simply keep asking for help without showing any effort, whatsoever...o.k., you actually can; but most of us forum people don't like it that way.
     
  4. May 12, 2013 #3
    gsal, I apologize. The incorrect version posted, this version is the updated one with Maximum values but I am unsure if I have coded correctly. New code is in blue
    Code (Text):
    program WSUPhys_csv
      implicit none

      integer :: stat, num_lines, ivar1, ivar2, ivar3
      real :: rvar
      character*80 :: line
     
      write(*,*) "Reading file..."

      ! open input file
      open(10,file='DATA-002.csv',status='old',iostat=stat)
      if (stat .ne. 0) then
        write(*,*) 'File cannot be opened !'
        go to 99  
      end if

      num_lines = 0
      do
        read(10,'(A)',end=99, iostat=stat) line
        if (stat .ne. 0) then
          write(*,*) 'Error reading data !'
          go to 99
        end if
        ! skip comment/header lines beginning with ";"
        if (adjustl(trim(line(1:1))) .eq. ';') then
          cycle
        end if
        ! read string line into numeric variables
        read(line,*) rvar, ivar1, ivar2, ivar3
        num_lines = num_lines + 1    
        if (num_lines <=  10) then
          write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3
        end if
      Sum_ivar1 = Sum_ivar1 + ABS(ivar1)
      Sum_ivar2 = Sum_ivar2 + ABS(ivar2)
      Sum_ivar3 = Sum_ivar3 + ABS(ivar3)
    [COLOR="Navy"]  MaxVert = MAX(ivar1)
      MaxSide = MAX(ivar2)
      MaxForward= MAX(ivar3)[/COLOR]
      end do

      ! close file
      99 continue
      Avg_ivar1 = (Sum_ivar1 / num_lines)
      Avg_ivar2 = (Sum_ivar2 / num_lines)
      Avg_ivar3 = (Sum_ivar3 / num_lines)
      close (10)
      !
      write(*,*) '...done.'
      write(*,*) 'Lines processed: ', num_lines
      write(*,*) 'Average vertical acceleration', Avg_ivar1/1000
      write(*,*) 'Average sideways acceleration', Avg_ivar2/1000
      write(*,*) 'Average forwards acceleration', Avg_ivar3/1000
      [COLOR="Navy"]write(*,*) 'Maximum vertical acceleration', MaxVert
      write(*,*) 'Maximum sideways acceleration', MaxSide
      write(*,*) 'Maximum forward acceleration',  MaxForward[/COLOR]
     
    end program WSUPhys_csv
     
    Last edited by a moderator: May 12, 2013
  5. May 12, 2013 #4
    Your ivar1, ivar2, ivar3 variables are NOT arrays, they are scalars and they contain the value just read. So, it does not make sense to apply the 'max()' function to them.

    You need initialize your Max* variables to zero before you go into the loop; then, inside the loop, you need to compare the ivar* value just read to the one stored in the corresponding Max* variable and keep the largest of the two.
     
  6. May 12, 2013 #5
    So do I create a separate do loop?

    something like this:

    Code (Text):
    Do i=1,72000
     read(line,*) rvar, ivar1, ivar2, ivar3
     If (ivar1 >> ivar1) then
      MaxVert=ivar1
    or am I way off track?
     
  7. May 12, 2013 #6
    Within the same loop

    Code (Text):

     If (ivar1 > MaxVert) then
      MaxVert=ivar1
    end if
     
     
  8. May 12, 2013 #7
    Thank you!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran- maximum values
  1. Fortran/ Numeric value (Replies: 0)

  2. Fortran missing values (Replies: 1)

Loading...