Finding Maximum Acceleration Values in Fortran

In summary: It makes sense now. Thank you for your patience and help!In summary, the conversation discussed a program that takes values from a .csv file and analyzes them. The data in question is located in the first 4 columns starting from row 11 and is labeled as rvar, ivar1, ivar2, and ivar3. The program has already calculated the average value of each column and the question is how to find the maximum values for each column separately. The updated code includes the use of the 'max()' function to compare the current value to the previous maximum and keep the larger value.
  • #1
ShoxPhysics
5
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
  • #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.
 
  • #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:
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:
  • #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.
 
  • #5
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?
 
  • #6
Within the same loop

Code:
 If (ivar1 > MaxVert) then
  MaxVert=ivar1
end if
 
  • #7
Thank you!
 

1. What is the maximum value that can be stored in a Fortran variable?

The maximum value that can be stored in a Fortran variable depends on the data type of the variable. For example, for an integer variable, the maximum value is 2,147,483,647. For a real variable, the maximum value is approximately 3.4 x 10^38.

2. Can the maximum value of a Fortran variable be changed?

No, the maximum value of a Fortran variable is determined by the data type and cannot be changed.

3. How is the maximum value of a Fortran variable determined?

The maximum value of a Fortran variable is determined by the number of bits used to represent the data type. The more bits used, the larger the maximum value that can be stored.

4. Can the maximum value of a Fortran variable be exceeded?

Yes, it is possible to exceed the maximum value of a Fortran variable. This is known as an overflow and can result in unexpected behavior or errors in a program.

5. Are there any special considerations for using the maximum value in Fortran?

Yes, it is important to be aware of the maximum value when performing calculations or comparisons with Fortran variables. For example, if the result of a calculation exceeds the maximum value, an overflow may occur. Additionally, when comparing values, it is important to use the correct data type to avoid unexpected results.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
4
Views
11K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
8K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top