! Matrice SSH 120x64:
! - Date in dubla-precizie
! - Continuta in fisierul "ssh.dat": stocata pe coloane.
!
! Calculeaza suma elementelor din matricea SSH:
! 1) REAL(8):
! - dupa linii: sum_row
! - dupa linii - in ordinea inversa (a coloanelor): sum_row_reverse
! - dupa coloane: sum_col
! - dupa coloane - in ordine inversa (a liniilor): sum_col_reverse
!
! Valoarea corecta: 0.357985839247703552246093750000000
!
! Compile with: Optimization = NONE; and, with Optimization = Speed.

Program Main_SSH

implicit real(8) (a-h, o-z)
real(8) ssh_data(120, 64)


open(1, file ='ssh.dat', status ='old', form ='formatted')

do j =1,64
read(1,*) ssh_data(1:120, j)
enddo

close(1)

open(1, file ='ssh-cvf.rez', status ='unknown', form ='formatted')

ssh_max =maxval(ssh_data)
ssh_min =minval(ssh_data)

write(*, '(a)') 'Extreme data:'
write(*, '(a, 1pD24.15)') ' ssh_min =', ssh_min
write(*, '(a, 1pD24.15)') ' ssh_max =', ssh_max

write(1, '(a)') 'Extreme data:'
write(1, '(a, 1pD24.15)') ' ssh_min =', ssh_min
write(1, '(a, 1pD24.15)') ' ssh_max =', ssh_max


!n_tot =64*120
! REAL(8):
! Dupa linii:
sum_row =0.
do i =1, 120
    do j =1,64
    sum_row =sum_row +ssh_data(i,j)
    enddo
enddo

! Dupa linii - Ordine inversa:
sum_row_reverse =0.
do i =1, 120
    do j =64, 1, -1
    sum_row_reverse =sum_row_reverse +ssh_data(i,j)
    enddo
enddo
   
! Dupa coloane:
sum_col =0.
do j =1, 64
    do i =1,120
    sum_col =sum_col +ssh_data(i,j)
    enddo
enddo

! Dupa coloane:
sum_col_reverse =0.
do j =1, 64
    do i =120, 1, -1
    sum_col_reverse =sum_col_reverse +ssh_data(i,j)
    enddo
enddo

! SUM on array (Real(8)):
sum_plus =sum(ssh_data, ssh_data >0)
sum_minus =sum(ssh_data, ssh_data <0)

sum_tot =sum_plus +sum_minus


! Print:
! Consola:
write(*, '(/,a)') 'Real(8):'
write(*,*) 'sum_row =', sum_row
write(*,*) 'sum_col =', sum_col

write(*,*)
write(*,*) 'sum_row_reverse =', sum_row_reverse
write(*,*) 'sum_col_reverse =', sum_col_reverse

write(*, '(/,a)') ' Sum on array:'
write(*,*) ' sum_plus =', sum_plus
write(*,*) ' sum_minus =', sum_minus
write(*,*) ' sum_plus +sum_minus =', sum_tot

write(*,'(/,a,a)') '! Valoarea corecta (Real(16)): 0.357985839247703552246093750000000'

! Fisier de iesire:
write(1, '(/,a)') 'Real(8):'
write(1,*) 'sum_row =', sum_row
write(1,*) 'sum_col =', sum_col

write(1,*)
write(1,*) 'sum_row_reverse =', sum_row_reverse
write(1,*) 'sum_col_reverse =', sum_col_reverse

write(1, '(/,a)') ' Sum on array:'
write(1,*) ' sum_plus =', sum_plus
write(1,*) ' sum_minus =', sum_minus
write(1,*) ' sum_plus +sum_minus =', sum_tot

write(1,'(/,a)') '! Valoarea corecta (Real(16)):'
write(1,'(a)') '0.357985839247703552246093750000000'

close(1)

write(*,'(/,a)') ' Fisier de iesire: "ssh-cvf.rez"'
end

  