在fortran中添加不等长的数组直到一个公共索引

Add arrays of unequal lengths in fortran up till a common index

如果这个问题很基础,我很抱歉,但我自学了 Fortran,我查看了这段代码并尝试了很多我开始感到困惑的事情。 我执行了一个插值并将输出写入一个数组,我想将该数组添加到代码中存在的另一个数组,但我的添加给出了完全奇怪的结果。从我附上的图中,我希望加法 (Total cls) 遵循红色虚线(这是相同的加法,但在代码之外手动完成),而是遵循黄线。

            use trilinear
            use array_mod
            use variables

            implicit none
            integer (cosmosis_block) :: block
            integer (cosmosis_status) :: status

            integer, parameter :: input_set = 1
            real  :: cls(2:standard_lmax_ksz,1:4)
            real(8)  :: cls_double(2:standard_lmax_ksz,1:4), cls_phi(2:standard_lmax)
            real(8)  :: cls_final(2:standard_lmax_ksz,1)
            integer  :: ell(2:standard_lmax_ksz), l
            logical, parameter :: switch_polarization_convention = .false.  
            real(8), DIMENSION(1448) :: interpolated_array
            real(8) :: inner_coord(1448,3)
            real(8) :: lambda
            real(8) :: mass
            real(8), DIMENSION(1448,27) :: coord
            integer :: m, max_rows, n, lam, inner_row
            REAL(8)  :: interpolated_value(1:standard_lmax_ksz)
            real(8) :: interpolated_index

            integer :: zeta
            Logical :: first_time = .True.
            CHARACTER(len=100) :: FN
            type(CambParams) :: params
            character(len=3),dimension(3) :: mass_array
            N=3                                     
            mass_array(1) = '2e8'
            mass_array(2) = '2e9'
            mass_array(3) = '3e9'
            !!!

            status = 0

            call CAMB_GetCls(cls, standard_lmax_ksz, input_set, switch_polarization_convention)
            cls_double(:,1:4) = cls * 7.4311e12  !cmb output scale       

            if(first_time) then
            max_rows=1448
                    lam = 80
                    DO I=1,N
                            lam = lam + 60
                            zeta=20
                            do j=1,N
                                 zeta = zeta + 20
                                    do k=1,N
                                          WRITE(FN,10)lam,zeta(mass_array(k))!,k=1,N)
                                    WRITE(6,*)FN!filename
                                    OPEN(99,FILE=FN, action='read', status='old',position='rewind')
                                    do inner_row=1,max_rows
                                            read (99,*)(inner_coord(inner_row,l),l=1,3)
                                            coord(:,9*I+3*j+k-12)=inner_coord(:,3)
                                    end do
                                    CLOSE(99)       
                                    end do
                            end do
                    END DO
            10 FORMAT('4e3_2048_',(I3.0),'',(I2.2),'',(A3),'.ksz_cl.txt') !length of thisis decided by FN
            print *, first_time
            first_time = .False.
            endif
            interpolated_array = trilinear_mod(params%lambda_ksz, params%zeta_ksz, params%mmin_ksz, coord)

           open (unit=96, file='interpolated_array_new.txt')!, action='write')!, status='replace')            
            do n = 2, 17
                    interpolated_index = n
                    interpolated_value = 0
                    write(96,'(2f16.10)') interpolated_index, interpolated_value
            end do
            do m = 1,max_rows-1
                    write(96,'(2f16.10)') inner_coord(m,1), interpolated_array(m)
                    do n = 1,17
                            interpolated_index = inner_coord(m,1)+n
                            interpolated_value = interpolated_array(m)*(1-n/18.) + interpolated_array(m+1)*(n/18.)
                            write(96,'(2f16.10)') interpolated_index, interpolated_value
                    end do
            end do
            close(96)

            cls_final(:,1) = cls_double(:,1) + interpolated_value

这里的一个问题是您将 interpolated_value 定义为数组 实数 (8) :: interpolated_value(1:standard_lmax_ksz)

并在多个地方将其用作定标器(一个例子是) interpolated_value = 0

如果你把 IMPLICIT NONE 在你的定义之前,你会更好地理解这些事情。

如果没有更多详细信息(例如输入文件的至少一部分),我们可能只能提供这些。