将2D数组写入.dat文件

时间:2016-10-25 18:17:34

标签: arrays fortran

您好我正在尝试将二维数组写入.dat文件。示例代码如下:

integer :: i,j
integer, parameter :: nx=100, ny=100
real,dimension(-nx:nx,-ny:ny) :: f,g

open(unit=1,file='file1.dat')
open(unit=2,file='file2.dat')

所以每个2D数组都由

给出
f(i,j)
g(i,j)

假设这些已填充,我如何将它们写入.dat文件?我尝试使用隐含的do循环

write(1,*) (f(i,j), i=-nx,nx), j=-ny,ny)
write(2,*) (g(i,j), i=-nx,nx), j=-ny,ny)

然而,它只是将2D数组存储为.dat文件为1行,这不是我想要的。我希望商店2D数组作为2D数组(就像存储矩阵一样)。由于nx = ny,2D数组应该只是一个方阵。

1 个答案:

答案 0 :(得分:1)

首先,一个重要的观察。将数组作为参数传递给子例程时,除非下限为1,否则不会在调用中保持实际参数的上限和下限。但是,在传递指针时,会保留下限和上限。请参阅:Fortran subroutine returning wrong values

其次,与Java,C / C ++,Python等不同,FORTRAN / Fortran中没有文件的概念。请参阅:http://docs.cray.com/books/S-3695-35/html-S-3695-35/pdollsmg.html

此外,硬编码文件识别单元极易出错。我建议完全避免使用更大的整数,而不是使用更大的整数;下面的代码使用newunit说明符来读取和写入未格式化的*.dat文件中的数组。

program main

  use, intrinsic :: ISO_Fortran_env, only: &
       stdout => OUTPUT_UNIT, &
       compiler_version, &
       compiler_options

  ! Explicit typing only
  implicit none

  ! Variables
  integer, parameter :: NX=2, NY=3
  real, pointer      :: write_ptr(:,:) => null()
  real, allocatable  :: read_alloc(:,:)
  ! Your original post did not have valid array extends
  real, target, dimension(-NX:NX, -NY:NY) :: f, g

  ! Write data to binary files
  write_ptr => f
  call write_grid_to_file(write_ptr, './file1.dat')
  nullify(write_ptr)

  write_ptr => g
  call write_grid_to_file(write_ptr, './file2.dat')
  nullify(write_ptr)

  ! Allocate memory to read from files
  allocate( read_alloc, mold=f)

  call read_grid_from_file(read_alloc, './file1.dat')

  print *, 'original f array'
  print *, f
  print *, 'read from *.dat file'
  print *, read_alloc

  write( stdout, '(/4a/)') &
       ' This file was compiled using ', compiler_version(), &
       ' using the options ', compiler_options()

contains

  subroutine write_grid_to_file(grid, file_name)
    ! Dummy arguments
    real, pointer,    intent (in) :: grid(:,:)
    character(len=*), intent(in)  :: file_name
    ! Local variable
    integer :: file_id_unit

    ! Write grid to file
    open( newunit=file_id_unit, file=file_name, &
        status='replace', form='unformatted', &
        action='write', access='stream')
    write( file_id_unit ) grid
    close( file_id_unit )

  end subroutine write_grid_to_file

  subroutine read_grid_from_file(grid, file_name)
    ! Dummy arguments
    real,             intent (out) :: grid(:,:)
    character(len=*), intent(in)   :: file_name
    ! Local variable
    integer :: file_id_unit

    ! Write grid to file
    open( newunit=file_id_unit, file=file_name, &
        status='old', form='unformatted', &
        action='read', access='stream')
    read( file_id_unit ) grid
    close( file_id_unit )

  end subroutine read_grid_from_file

end program main

命令行提示符:

gfortran -Wall -o main.exe main.f90; ./main.exe

产量

 original f array
   0.00000000       0.00000000       0.00000000       0.00000000      -4.64743227E-27   4.59121429E-41   2.93318617E+14   4.56823299E-41  -4.64740762E-27   4.59121429E-41   1.02162904E+15   4.56823299E-41   0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000    
 read from *.dat file
   0.00000000       0.00000000       0.00000000       0.00000000      -4.64743227E-27   4.59121429E-41   2.93318617E+14   4.56823299E-41  -4.64740762E-27   4.59121429E-41   1.02162904E+15   4.56823299E-41   0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000    

 This file was compiled using GCC version 6.2.0 20161010 using the options -mtune=generic -march=x86-64 -Wall