Fortran Coder

标题: 怎么 循环读取二维数组并赋值? [打印本页]

作者: 吃饭叫我    时间: 2018-9-28 10:53
标题: 怎么 循环读取二维数组并赋值?
[Fortran] 纯文本查看 复制代码
program main
    implicit none

    character(*),parameter :: infile='Depth.txt',outfile='Depth2.txt'
    integer :: i,j
    real,dimension(521,1440):: m

    OPEN(UNIT=20,FILE=infile,STATUS='OLD',ACTION='read')
    OPEN(UNIT=30,FILE=outfile,status='replace',action='write')

   do i=1,521

        READ(20,'(1440(I6))',end=99) (m(i,j),j=1,1440)

        If(m(i,j)/=-999 .and. m(i,j+1)==-999) then
            m(i,j)=2                         ! 1 represents ocean
            m(i,j-1)=2                       ! 2 represents coastline
            m(i,j-2)=2                       ! -999 represents land
            m(i,j-3)=2

        else if(m(i,j)==-999 .and. m(i,j+1)/=-999) then
            m(i,j)=2
            m(i,j+1)=2
            m(i,j+2)=2
            m(i,j+3)=2

        else if (m(i,j)<200) then
            m(i,j)=2
        else
            m(i,j)=1
        End if


        write(30,'(1440(I6))') (m(i,j),j=1,1440)
        end do

!100 format (1440I6)
99 stop
end program main

在 if 条件判断那里出了问题,不知道能不能这样读?请大家帮忙看看,读写都没问题,循环读取 赋值就开始出错



作者: 吃饭叫我    时间: 2018-9-28 10:54
请大家帮忙看看,读写都没问题,循环读取 赋值就开始出错
作者: chiangtp    时间: 2018-9-28 14:31
吃饭叫我 发表于 2018-9-28 10:54
请大家帮忙看看,读写都没问题,循环读取 赋值就开始出错

do i=1,521
      READ(20,'(1440(I6))',end=99) (m(i,j),j=1,1440)
      If(m(i,j)/=-999 .and. m(i,j+1)==-999) then ! variable "j" undefined
作者: chiangtp    时间: 2018-9-28 14:38
do i=1,521                                                            
  READ(20,'(1440(I6))',end=99) (m(i,j),j=1,1440)                       
                                                                       
  DO j = (1+3), (1440-3)                                               
    if(m(i,j)/=-999 .and. m(i,j+1)==-999) then                        
        m(i,j)=2                         ! 1 represents ocean         
        m(i,j-1)=2                       ! 2 represents coastline      
        m(i,j-2)=2                       ! -999 represents land        
        m(i,j-3)=2                                                     
    else if(m(i,j)==-999 .and. m(i,j+1)/=-999) then                    
        m(i,j)=2                                                      
        m(i,j+1)=2                                                     
        m(i,j+2)=2                                                     
        m(i,j+3)=2                                                     
    else if (m(i,j)<200) then                                          
        m(i,j)=2                                                      
    else                                                               
        m(i,j)=1                                                      
    end if                                                            
  END DO
                                                                       
  write(30,'(1440(I6))') (m(i,j),j=1,1440)                             
end do                                                                 
作者: 吃饭叫我    时间: 2018-10-7 20:58
chiangtp 发表于 2018-9-28 14:38
do i=1,521                                                            
  READ(20,'(1440(I6))',end=9 ...

C:\Users\zhou\Desktop\QQ截图20181007205518.png

谢谢,这个确实是我疏忽了,不过改了之后变成了这样。循环读取和赋值 的结果都变成了 * 是格式不对吗




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2