×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Determining the Min and Max temps in a file

Determining the Min and Max temps in a file

Determining the Min and Max temps in a file

(OP)
Hello.  My file looks like this:

2447893       0        27       16      
2447893       3600     25       15      
2447893       7200     24       16      
2447893       10800    24       16      
2447893       14400    22       15      
2447893       18000    21       13      
2447893       21600    20       13      
2447893       25200    17       12      
2447893       28800    16       12      
2447893       32400    13       9       
2447893       36000    12       8       
2447893       39600    12       8       
2447893       43200    12       8       
2447893       46800    11       7       
2447893       50400    9        5       
2447893       54000    14       9       
2447893       57600    21       16      
2447893       61200    27       17      
2447893       64800    30       17      
2447893       68400    34       17      
2447893       72000    37       17      
2447893       75600    39       18      
2447893       79200    38       18      
2447893       82800    37       18     
2447894       0        34       19      
2447894       3600     32       19      
2447894       7200     33       20      
2447894       10800    32       19      
2447894       14400    32       19      
2447894       18000    30       18      
2447894       21600    25       18      
2447894       25200    26       18      
2447894       28800    30       20      
2447894       32400    29       20      
2447894       36000    30       21      
2447894       39600    31       22      
2447894       43200    27       20      
2447894       46800    25       19      
2447894       50400    27       21      
2447894       54000    32       22      
2447894       57600    38       24      
2447894       61200    42       25      
2447894       64800    45       26      
2447894       68400    49       27      
2447894       72000    50       28      
2447894       75600    51       28      
2447894       79200    51       28      
2447894       82800    48       28      

Where the first column is the Julian Day, the second is the time the temperature was recorded (in seconds), and the last two columns are the temperature and dew point, respectively.  I need a program that can read in the temperature values for each day and determine the high and low.  I cannot assume the number of observations per day.  This is also quite a large file, consisting of 200,000 lines looking exactly like this.

I've tried a number of ways, but there is always something wrong with the high and low.  I have already read the values using a two dimensional array, I just can't determine the high and low.

RE: Determining the Min and Max temps in a file

(OP)
This is what I have:

CODE

day = first
DO 20 x = 1, lines
    hiTemp = -100
    loTemp = 200

    IF ( JD(x).eq.day ) THEN

        IF (a(5,x).gt.HiTemp) THEN !change the hiTemp value
            hiTemp = a(5,x)
            hiTempTime = a(4,x)
        ELSE IF (a(5,x).lt.LoTemp) THEN !change the loTemp value
            loTemp = a(5,x)
            loTempTime = a(4,x)
        ENDIF

    ELSE
    
    !highs
    tempHi(day) = hiTemp
    timeTempHi(day) = hiTempTime

    !lows
    tempLo(day) = loTemp
    timeTempLo(day) = loTempTime

    write(*,200) a(2,x), a(3,x), a(1,x), hiTemp, hiTempTime, loTemp, loTempTime

    day= day+1 !count to get the values in the right place
    ENDIF

20 CONTINUE

RE: Determining the Min and Max temps in a file

(OP)
I get a segmentation fault on it every time.

RE: Determining the Min and Max temps in a file

What yu posted could not standalone work.
  • I don't see how you read the file.
  • You don't use type declarations, so implicitly your X is real and I don't see what value has LINES

RE: Determining the Min and Max temps in a file

As I wrote in the thread above - you don't need arrays.
Here is an example, how to do it with Control Break Processing:
temperatures.f95

CODE

module glob_vars
  ! declaration of global variables
  implicit none
  integer :: nr_records, &
             ! file record fields
             date, time, temp, point, &
             ! save fields
             date_save, time_save, temp_save, point_save, &
             ! daily result fields
             min_time, min_temp, min_point, &
             max_time, max_temp, max_point, &
             ! total result fields
             tmin_date, tmin_time, tmin_temp, tmin_point, &
             tmax_date, tmax_time, tmax_temp, tmax_point
end module glob_vars

program temperatures
  ! Single-Level Control Break Processing

  use glob_vars

  implicit none
  
  integer :: stat ! file status

  ! open file
  open (1, file='temperatures.dat', status='old', iostat=stat)
  if (stat .ne. 0) then
    write(*,*) 'File cannot be opened !'
    go to 99
  end if

  write(*, '(80A)') '*******************************************************'
  ! process file
  nr_records = 0
  date_save = 0

  do while (.true.)
    ! read record
    read(1, *, end=99) date, time, temp, point
    nr_records = nr_records + 1
    call process_record
    call save_keys
  enddo

  99 continue
  ! at end print totals
  call print_daily_summary
  call print_total_summary  
  write(*, 10) nr_records
  write(*, '(80A)') '*******************************************************'
  ! close file
  close(1)

  10 format('Processing of',1X, I0, 1X, 'lines finished.')
end program temperatures

subroutine process_record
  use glob_vars
  implicit none

  ! on 1.record initialize min and max values
  if (nr_records .eq. 1) then
    ! daily values
    min_time = time
    min_temp = temp
    min_point = point
    max_time = time
    max_temp = temp
    max_point = point
    ! total values
    tmin_date = date
    tmin_time = time
    tmin_temp = temp
    tmin_point = point
    tmax_date = date
    tmax_time = time
    tmax_temp = temp
    tmax_point = point      
  end if

  ! if not on 1.record
  if (date_save .ne. 0) then
    ! process control break
    call process_control_break
  end if

  ! print line
  write(*, 10) nr_records, date, time, temp, point

  ! compute minimum
  if (temp .lt. min_temp) then
    ! daily values
    min_time = time
    min_temp = temp
    min_point = point
  end if
  if (temp .lt. tmin_temp) then
    ! total values
    tmin_date = date
    tmin_time = time
    tmin_temp = temp
    tmin_point = point
  end if

  ! compute maximum
  if (temp .gt. max_temp) then
    ! daily values
    max_time = time
    max_temp = temp
    max_point = point
  end if
  if (temp .gt. tmax_temp) then
    ! total values
    tmax_date = date
    tmax_time = time
    tmax_temp = temp
    tmax_point = point      
  end if

  10 format(5X, i3, 1x, i7, 1X, i5, 1x, i2, 1x, i2)  
end subroutine process_record

subroutine process_control_break
  use glob_vars
  implicit none
  !
  if (date .ne. date_save) then
     call print_daily_summary
  end if  
end subroutine process_control_break

subroutine print_daily_summary
  use glob_vars
  implicit none
  !
  write(*, *)
  write(*, 10) date_save
  write(*, 20) '*  min:', min_time, min_temp, min_point
  write(*, 20) '*  max:', max_time, max_temp, max_point
  write(*, *)

  10 format('*  Summary for date',1x,i7,':')
  20 format(a7, 10x, i5, 1x, i2, 1x, i2)

  ! initialize gloabal variables
  min_time = time
  min_temp = temp
  min_point = point
  max_time = time
  max_temp = temp
  max_point = point  
end subroutine print_daily_summary

subroutine print_total_summary
  use glob_vars
  implicit none
  !
  write(*, *)
  write(*, 10)
  write(*, 20) '** min:', tmin_date, tmin_time, tmin_temp, tmin_point
  write(*, 20) '** max:', tmax_date, tmax_time, tmax_temp, tmax_point
  write(*, *)

  10 format('** Summary for all dates:')
  20 format(a7, 2x, i7, 1x, i5, 1x, i2, 1x, i2)
end subroutine print_total_summary

subroutine save_keys
  use glob_vars
  implicit none
  !
  date_save = date
end subroutine save_keys
Compling and running the example above I get these results:

CODE

$ g95 temperatures.f95 -o temperatures

$ temperatures
*******************************************************
       1 2447893     0 27 16
       2 2447893  3600 25 15
       3 2447893  7200 24 16
       4 2447893 10800 24 16
       5 2447893 14400 22 15
       6 2447893 18000 21 13
       7 2447893 21600 20 13
       8 2447893 25200 17 12
       9 2447893 28800 16 12
      10 2447893 32400 13  9
      11 2447893 36000 12  8
      12 2447893 39600 12  8
      13 2447893 43200 12  8
      14 2447893 46800 11  7
      15 2447893 50400  9  5
      16 2447893 54000 14  9
      17 2447893 57600 21 16
      18 2447893 61200 27 17
      19 2447893 64800 30 17
      20 2447893 68400 34 17
      21 2447893 72000 37 17
      22 2447893 75600 39 18
      23 2447893 79200 38 18
      24 2447893 82800 37 18

*  Summary for date 2447893:
*  min:          50400  9  5
*  max:          75600 39 18

      25 2447894     0 34 19
      26 2447894  3600 32 19
      27 2447894  7200 33 20
      28 2447894 10800 32 19
      29 2447894 14400 32 19
      30 2447894 18000 30 18
      31 2447894 21600 25 18
      32 2447894 25200 26 18
      33 2447894 28800 30 20
      34 2447894 32400 29 20
      35 2447894 36000 30 21
      36 2447894 39600 31 22
      37 2447894 43200 27 20
      38 2447894 46800 25 19
      39 2447894 50400 27 21
      40 2447894 54000 32 22
      41 2447894 57600 38 24
      42 2447894 61200 42 25
      43 2447894 64800 45 26
      44 2447894 68400 49 27
      45 2447894 72000 50 28
      46 2447894 75600 51 28
      47 2447894 79200 51 28
      48 2447894 82800 48 28

*  Summary for date 2447894:
*  min:          21600 25 18
*  max:          75600 51 28


** Summary for all dates:
** min:  2447893 50400  9  5
** max:  2447894 75600 51 28

Processing of 48 lines finished.
*******************************************************
  

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close