Finding Maximum Acceleration Values in Fortran

  • Context: Fortran 
  • Thread starter Thread starter ShoxPhysics
  • Start date Start date
  • Tags Tags
    Fortran Maximum
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 3K views
ShoxPhysics
Messages
5
Reaction score
0
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:
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
 
Physics news on Phys.org
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.
 
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:
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)
  MaxVert = MAX(ivar1)
  MaxSide = MAX(ivar2)
  MaxForward= MAX(ivar3)
  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
  write(*,*) 'Maximum vertical acceleration', MaxVert
  write(*,*) 'Maximum sideways acceleration', MaxSide
  write(*,*) 'Maximum forward acceleration',  MaxForward
 
end program WSUPhys_csv
 
Last edited by a moderator:
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.
 
So do I create a separate do loop?

something like this:

Code:
Do i=1,72000
 read(line,*) rvar, ivar1, ivar2, ivar3
 If (ivar1 >> ivar1) then
  MaxVert=ivar1

or am I way off track?
 
Within the same loop

Code:
 If (ivar1 > MaxVert) then
  MaxVert=ivar1
end if