直接探査ファイルの使い方
http://mns2.c.u-tokyo.ac.jp/arasaki/fortran/direct.html
「書式なし直接探査ファイル (unformatted direct access file)」の特徴
順番探査がテープをイメージしているなら直接探査はディスクのイメージです。
書き出し
program write_direct
integer, parameter :: m=10real, allocatable :: a(:),b(:)character(100) ofnameofname="tmp_direct_access.dat"write(*,'(A,A)')'output file name: ',ofnamen=5allocate(a(n),b(n))do i=1,n a(i)=i b(i)=i*2.0enddo !iINQUIRE(IOLENGTH=nreclen)a,bopen(10,file=ofname,access='direct',recl=nreclen)write(*,*)'Record length= ',nreclenwrite(10,rec=1)a,bwrite(*,'(A)')'output data:'do i=1,n write(*,*)a(i),b(i)enddo stopend$ ifort tmp_write_directac.f90 -o tmp_write_directac
$ ./tmp_write_directac
output file name: tmp_direct_access.dat
Record length= 10
output data:
1.000000 2.000000
2.000000 4.000000
3.000000 6.000000
4.000000 8.000000
5.000000 10.00000
読み込み
program read_directinteger, parameter :: m=10real, allocatable :: a(:),b(:)character(100) ifnameifname="tmp_direct_access.dat"write(*,'(A,A)')'input file name: ',ifnamen=5allocate(a(n),b(n))INQUIRE(IOLENGTH=nreclen)a,bopen(10,file=ifname,access='direct',recl=nreclen)write(*,*)'Record length= ',nreclenread(10,rec=1)a,bwrite(*,'(A)')'Input data'do i=1,n write(*,*)a(i),b(i)enddostopend$ ifort tmp_read_directac.f90 -o tmp_read_directac
$ ./tmp_read_directac
input file name: tmp_direct_access.dat
Record length= 10
Input data
1.000000 2.000000
2.000000 4.000000
3.000000 6.000000
4.000000 8.000000
5.000000 10.00000
データは、tとsの2種類ある。
1ヶ月ごとに、1レコードとしてまとまっている。
!
! 仮定しているデータファイルの構造
!
! レコード番号 データ
! 1 t,s
! ............
! 12 t,s
!
t,sの配列のサイズは(nx,ny,nz)
サンプルプログラム
real, allocatable(t(:,:,:), s(:,:,:))integer, parameter :: nx=10,ny=20,nz=5allocate (t(nx,ny,nz),s(nx,ny,nz))INQUIRE(IOLENGTH=L) t,snt=12iu2=20open(iu2,file=infle2,form="unformatted",access='direct',recl=L)print *, ' Reading t & s'do m=1,nt read(iu2,rec=m)t,senddo !mclose(iu2)print *, 'Done.'print *stopend