Finding Maximum Acceleration Values in Fortran

  • Context: Fortran 
  • Thread starter Thread starter ShoxPhysics
  • Start date Start date
  • Tags Tags
    Fortran Maximum
Click For Summary
SUMMARY

The discussion focuses on finding maximum acceleration values from a CSV file using Fortran. The user is working with a program named WSUPhys_csv that reads acceleration data from a CSV file and calculates average values. The main issue addressed is how to correctly implement the logic to find the maximum values for ivar1, ivar2, and ivar3. The solution involves initializing maximum variables before the loop and comparing each read value to update the maximum accordingly.

PREREQUISITES
  • Fortran programming language fundamentals
  • File I/O operations in Fortran
  • Understanding of arrays and scalar variables in Fortran
  • Basic knowledge of CSV file structure and data parsing
NEXT STEPS
  • Implement logic to initialize maximum variables before the data reading loop in Fortran.
  • Research the use of conditional statements in Fortran for value comparisons.
  • Learn about handling CSV files in Fortran, focusing on data extraction techniques.
  • Explore advanced Fortran features for data analysis, such as array handling and intrinsic functions.
USEFUL FOR

Fortran developers, data analysts, and students working on numerical simulations or data processing tasks that require reading and analyzing CSV files.

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
 
Technology 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
 
Thank you!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
8K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
9K