wxy 发表于 2017-4-22 14:23:42

关于文件批量合并

本帖最后由 wxy 于 2017-4-22 14:25 编辑

我想将四个文件合并到一个新的文件中 ,出现图中的问题,通道号并没有冲突,不知道什麽原因,麻烦帮看一下
program nc2dssat
    implicit none
    integer i ,m
    character,allocatable :: pre(:), rsd(:), tasmax(:), tasmin(:)
    character(len=512) :: filename_1, filename_2, filename_3, filename_4

    open(10,file="D:\miroc-esm-chem\historical\pr_v2\1951-1960\pre.dat") !降水
    open(11,file="D:\miroc-esm-chem\historical\rsds_v1\1951-1960\rsd.dat") !辐射
    open(12,file="D:\miroc-esm-chem\historical\tasmax_v1\1951-1960\tasmax.dat") !最高温
    open(13,file="D:\miroc-esm-chem\historical\tasmin_v1\1951-1960\tasmin.dat") !最低温

    do
      read(10,*)filename_1
      read(11,*)filename_2
      read(12,*)filename_3
      read(13,*)filename_4

      open(10,file=filename_1)
      open(11,file=filename_2)
      open(12,file=filename_3)
      open(13,file=filename_4)

      i=3652 !文件行数
      m=i
!===========================================================
!文件名配对,相同文件合并
!===========================================================
      if (filename_1==filename_2.and.filename_1==filename_3.and.filename_1==filename_4)then
            allocate(pre(m), rsd(m), tasmax(m), tasmin(m))

            open(14,file="D:\miroc-esm-chem\historical\test.txt")

            do i=1,m
                read(10,*)pre(i)
                read(11,*)rsd(i)
                read(12,*)tasmax(i)
                read(13,*)tasmin(i)
                write(14,"(5a,2x,6a,2x,6a,2x,5a)") rsd(i), tasmax(i), tasmin(i), pre(i)
            end do
            close(10)
            close(11)
            close(12)
            close(13)
            close(14)
            deallocate(rsd,tasmax,tasmin,pre)
      end if
    end do
end program

li913 发表于 2017-4-22 15:36:03

同一个文件被两次打开。

fcode 发表于 2017-4-22 16:38:08

   open(10,file="D:\miroc-esm-chem\historical\pr_v2\1951-1960\pre.dat") !降水
   open(11,file="D:\miroc-esm-chem\historical\rsds_v1\1951-1960\rsd.dat") !辐射
   open(12,file="D:\miroc-esm-chem\historical\tasmax_v1\1951-1960\tasmax.dat") !最高温
   open(13,file="D:\miroc-esm-chem\historical\tasmin_v1\1951-1960\tasmin.dat") !最低温

这些,和

      open(10,file=filename_1)
      open(11,file=filename_2)
      open(12,file=filename_3)
      open(13,file=filename_4)

用了相同的通道号(10,11,12,13),并且同时打开了。

wxy 发表于 2017-4-22 18:38:50

本帖最后由 wxy 于 2017-4-22 19:21 编辑

fcode 发表于 2017-4-22 16:38
open(10,file="D:\miroc-esm-chem\historical\pr_v2\1951-1960\pre.dat") !降水
   open(11,file="D:\mi ...
改了通道号之后仍然是这个错误,现在还是同时打开吗

fcode 发表于 2017-4-22 19:28:32

给出pre.dat, rsd.dat, tasmax.dat和 tasmin.dat 文件的内容

wxy 发表于 2017-4-22 19:36:35

fcode 发表于 2017-4-22 19:28
给出pre.dat, rsd.dat, tasmax.dat和 tasmin.dat 文件的内容

四个文件内容都是一样的

wxy 发表于 2017-4-22 20:35:49

本帖最后由 wxy 于 2017-4-22 21:36 编辑

fcode 发表于 2017-4-22 19:28
给出pre.dat, rsd.dat, tasmax.dat和 tasmin.dat 文件的内容
刚才的问题解决了,麻烦了:-)

wxy 发表于 2017-4-22 23:54:05

本帖最后由 wxy 于 2017-4-23 13:09 编辑

fcode 发表于 2017-4-22 19:28
给出pre.dat, rsd.dat, tasmax.dat和 tasmin.dat 文件的内容
还想请教一下,在批量读取五个dat文件列表时出现了问题,总是end of file,并新建一个filename_1,到filename_5的空白文件,不知道为什麽program nc2dssat
    implicit none
    integer i ,m
    character,allocatable :: pre(:), rsd(:), tasmax(:), tasmin(:)
    character(len=512) ::filename_1, filename_2, filename_3, filename_4, filename_5, line
!==================================================================================
!文件名列表
!==================================================================================
    open(10,file="D:\miroc-esm-chem\historical\pr_v2\1951-1960\pre.dat") !降水
    open(11,file="D:\miroc-esm-chem\historical\rsds_v1\1951-1960\rsd.dat") !辐射
    open(12,file="D:\miroc-esm-chem\historical\tasmax_v1\1951-1960\tasmax.dat") !最高温
    open(13,file="D:\miroc-esm-chem\historical\tasmin_v1\1951-1960\tasmin.dat") !最低温
    open(14,file="D:\miroc-esm-chem\historical\test\historical.dat")!输入文件

    do
      read(10,*)filename_1
      read(11,*)filename_2
      read(12,*)filename_3
      read(13,*)filename_4
      read(14,*)filename_5
!=====================================================================================
!批量打开文件
!=====================================================================================
      open(15,file="D:\miroc-esm-chem\historical\pr_v2\1951-1960\filename_1")
      open(16,file="D:\miroc-esm-chem\historical\rsds_v1\1951-1960\filename_2")
      open(17,file="D:\miroc-esm-chem\historical\tasmax_v1\1951-1960\filename_3")
      open(18,file="D:\miroc-esm-chem\historical\tasmin_v1\1951-1960\filename_4")
      open(19,file="D:\miroc-esm-chem\historical\test\filename_5")
!=====================================================================================
!文件行数
!=====================================================================================
      i = 0
      Do
          Read (9, *, End=100) line
          i = i + 1
      End Do
      100 m = i
      Rewind (9)

!===========================================================
!文件名配对,相同文件合并
!===========================================================
      if (filename_1==filename_2.and.filename_2==filename_3.and.filename_3==filename_4.and.filename_4==filename_5)then
            allocate(pre(m), rsd(m), tasmax(m), tasmin(m))

            do i=1,m
                read(15,*)pre(i)
                read(16,*)rsd(i)
                read(17,*)tasmax(i)
                read(18,*)tasmin(i)
                write(19,"(5a,2x,6a,2x,6a,2x,5a)") rsd(i), tasmax(i), tasmin(i), pre(i)
            end do
            deallocate(rsd,tasmax,tasmin,pre)
            close(15)
            close(16)
            close(17)
            close(18)
            close(19)
      end if
    end do
end program


storm_surge 发表于 2017-10-7 21:54:21

read(9,*) 定的读取数据量,这个通道号 9没有表示出来
页: [1]
查看完整版本: 关于文件批量合并