我正在尝试使用fortran子程序来查找长度为n的数组中的局部最大值。
该程序获取原始n长度数组的5个长度数组,看起来最大值位于3位置。然后向前移动到n长度数组中的下一个有效5长度数组。
我知道这有效,问题是我想保持最大值,与相应的wf和m参数相关联。所以我想要做的就是用这些数据写3个文件,然后循环它们。
这里count_peak是一个外部变量,因为我会将这个例程循环到几个n-lenght数组上,我想继续计算最大值。
我知道这是有效的,我的问题是,当我读取文件时,文件的数字和长度似乎是错误的。
以下是代码:
subroutine local_max_bif_v2(valdata,size_data,compare_size,count_peak,debug)
!************************************************
! makes an dim(compare_size) array from data. if the maximum value is in the middle of the array (localdata(2)) saves the value to a binary file
!************************************************
use constants !Module that defines the changing parameters for the bifurcation
integer, intent(in) :: debug
integer, intent(in) :: size_data !input array lenght.
real(8), dimension(size_data), intent(in) :: valdata !input array
integer(4), intent(in) :: compare_size !even number, greater than 3 (3 or 5 give fast, good results on soft data.)
integer(4), intent(inout) :: count_peak
real(8), dimension(compare_size) :: localdata
real(8) :: peak
integer(4) :: peak_index
integer(4), dimension(1) :: maxind
integer(4) :: midpoint
integer(4) :: j
if (debug.eq.1) then
print*,
print*, '*************************************'
print*, 'debug info for local_max_bif: '
print*,
!print*, 'local_max_biff goes from : ', size_data-evaluate_lenght, 'to ', size_data-compare_size
end if
midpoint=(1+compare_size)/2
OPEN(2,file='peak.in',form='unformatted',status='unknown',access='stream')
OPEN(3,file='varw.in',form='unformatted',status='unknown',access='stream')
OPEN(4,file='varm.in',form='unformatted',status='unknown',access='stream')
do j=1,size_data-compare_size
localdata=valdata(j:j+compare_size-1)
maxind=maxloc(localdata)
if (maxind(1).eq.(midpoint)) then
peak=localdata(midpoint)
count_peak=count_peak+1
peak_index=j+midpoint-1
WRITE(2) peak
WRITE(3) wf
WRITE(4) m
end if
end do
CLOSE(2)
CLOSE(3)
CLOSE(4)
if (debug.eq.1) then
print*, 'last peak index: ', peak_index
print*, 'amount of peaks', count_peak
print*, '*************************************'
print*,
endif
return
!To do: -need to add a way to test if the new peak is repeated or not. then if not, add no the file the new one
end subroutine